blob: 952143003a8e215bbfbb44eadaeb5d39ebfc196c [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 Moolenaar2f1e51a2014-12-13 03:58:09 +010014static void cmd_with_count __ARGS((char *cmd, char_u *bufp, size_t bufsize, long Prenum));
Bram Moolenaar884ae642009-02-22 01:37:59 +000015static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000016static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
18static void frame_setheight __ARGS((frame_T *curfrp, int height));
19#ifdef FEAT_VERTSPLIT
20static void frame_setwidth __ARGS((frame_T *curfrp, int width));
21#endif
22static void win_exchange __ARGS((long));
23static void win_rotate __ARGS((int, int));
24static void win_totop __ARGS((int size, int flags));
25static 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 +000026static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020027static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000029static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000030static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static win_T *frame2win __ARGS((frame_T *frp));
32static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
33static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
34static int frame_fixed_height __ARGS((frame_T *frp));
35#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000036static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000038static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void frame_add_vsep __ARGS((frame_T *frp));
40static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
41static void frame_fix_width __ARGS((win_T *wp));
42#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000043#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000044static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000045static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000047static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020048static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
49static 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 +000050static void frame_fix_height __ARGS((win_T *wp));
51static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020052static 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 +000053static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static void frame_append __ARGS((frame_T *after, frame_T *frp));
55static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020057# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
Bram Moolenaarb893ac22013-06-26 14:04:47 +020070static int frame_check_height __ARGS((frame_T *topfrp, int height));
71#ifdef FEAT_VERTSPLIT
72static int frame_check_width __ARGS((frame_T *topfrp, int width));
73#endif
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000076
Bram Moolenaar746ebd32009-06-16 14:01:43 +000077static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010078static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80#define URL_SLASH 1 /* path_is_url() has found "://" */
81#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
82
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000083#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000086# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000087#else
88# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000089#endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000092
93static char *m_onlyone = N_("Already only one window");
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * all CTRL-W window commands are handled here, called from normal_cmd().
97 */
98 void
99do_window(nchar, Prenum, xchar)
100 int nchar;
101 long Prenum;
102 int xchar; /* extra char from ":wincmd gx" or NUL */
103{
104 long Prenum1;
105 win_T *wp;
106#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
107 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000108 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110#ifdef FEAT_FIND_ID
111 int type = FIND_DEFINE;
112 int len;
113#endif
114 char_u cbuf[40];
115
116 if (Prenum == 0)
117 Prenum1 = 1;
118 else
119 Prenum1 = Prenum;
120
121#ifdef FEAT_CMDWIN
122# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
123#else
124# define CHECK_CMDWIN
125#endif
126
127 switch (nchar)
128 {
129/* split current window in two parts, horizontally */
130 case 'S':
131 case Ctrl_S:
132 case 's':
133 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000135#ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143#endif
144 win_split((int)Prenum, 0);
145 break;
146
147#ifdef FEAT_VERTSPLIT
148/* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100171 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 do_cmdline_cmd(cbuf);
173 break;
174
175/* open new window */
176 case Ctrl_N:
177 case 'n':
178 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000180#ifdef FEAT_QUICKFIX
181newwindow:
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000184 /* window height */
185 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 else
187 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000188#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
189 if (nchar == 'v' || nchar == Ctrl_V)
190 STRCAT(cbuf, "v");
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100200 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100201 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 break;
203
204/* close current window */
205 case Ctrl_C:
206 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100208 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100209 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 break;
211
212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
213/* close preview window */
214 case Ctrl_Z:
215 case 'z':
216 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 do_cmdline_cmd((char_u *)"pclose");
219 break;
220
221/* cursor to preview window */
222 case 'P':
223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
224 if (wp->w_p_pvw)
225 break;
226 if (wp == NULL)
227 EMSG(_("E441: There is no preview window"));
228 else
229 win_goto(wp);
230 break;
231#endif
232
233/* close all but current window */
234 case Ctrl_O:
235 case 'o':
236 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100238 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100239 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 break;
241
242/* cursor to next window with wrap around */
243 case Ctrl_W:
244 case 'w':
245/* cursor to previous window with wrap around */
246 case 'W':
247 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000248 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 beep_flush();
250 else
251 {
252 if (Prenum) /* go to specified window */
253 {
254 for (wp = firstwin; --Prenum > 0; )
255 {
256 if (wp->w_next == NULL)
257 break;
258 else
259 wp = wp->w_next;
260 }
261 }
262 else
263 {
264 if (nchar == 'W') /* go to previous window */
265 {
266 wp = curwin->w_prev;
267 if (wp == NULL)
268 wp = lastwin; /* wrap around */
269 }
270 else /* go to next window */
271 {
272 wp = curwin->w_next;
273 if (wp == NULL)
274 wp = firstwin; /* wrap around */
275 }
276 }
277 win_goto(wp);
278 }
279 break;
280
281/* cursor to window below */
282 case 'j':
283 case K_DOWN:
284 case Ctrl_J:
285 CHECK_CMDWIN
286#ifdef FEAT_VERTSPLIT
287 win_goto_ver(FALSE, Prenum1);
288#else
289 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
290 wp = wp->w_next)
291 ;
292 win_goto(wp);
293#endif
294 break;
295
296/* cursor to window above */
297 case 'k':
298 case K_UP:
299 case Ctrl_K:
300 CHECK_CMDWIN
301#ifdef FEAT_VERTSPLIT
302 win_goto_ver(TRUE, Prenum1);
303#else
304 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
305 wp = wp->w_prev)
306 ;
307 win_goto(wp);
308#endif
309 break;
310
311#ifdef FEAT_VERTSPLIT
312/* cursor to left window */
313 case 'h':
314 case K_LEFT:
315 case Ctrl_H:
316 case K_BS:
317 CHECK_CMDWIN
318 win_goto_hor(TRUE, Prenum1);
319 break;
320
321/* cursor to right window */
322 case 'l':
323 case K_RIGHT:
324 case Ctrl_L:
325 CHECK_CMDWIN
326 win_goto_hor(FALSE, Prenum1);
327 break;
328#endif
329
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000330/* move window to new tab page */
331 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000332 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000333 MSG(_(m_onlyone));
334 else
335 {
336 tabpage_T *oldtab = curtab;
337 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000338
339 /* First create a new tab with the window, then go back to
340 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000341 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342 if (win_new_tabpage((int)Prenum) == OK
343 && valid_tabpage(oldtab))
344 {
345 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200346 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 if (curwin == wp)
348 win_close(curwin, FALSE);
349 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200350 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000351 }
352 }
353 break;
354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355/* cursor to top-left window */
356 case 't':
357 case Ctrl_T:
358 win_goto(firstwin);
359 break;
360
361/* cursor to bottom-right window */
362 case 'b':
363 case Ctrl_B:
364 win_goto(lastwin);
365 break;
366
367/* cursor to last accessed (previous) window */
368 case 'p':
369 case Ctrl_P:
370 if (prevwin == NULL)
371 beep_flush();
372 else
373 win_goto(prevwin);
374 break;
375
376/* exchange current and next window */
377 case 'x':
378 case Ctrl_X:
379 CHECK_CMDWIN
380 win_exchange(Prenum);
381 break;
382
383/* rotate windows downwards */
384 case Ctrl_R:
385 case 'r':
386 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 win_rotate(FALSE, (int)Prenum1); /* downwards */
389 break;
390
391/* rotate windows upwards */
392 case 'R':
393 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 win_rotate(TRUE, (int)Prenum1); /* upwards */
396 break;
397
398/* move window to the very top/bottom/left/right */
399 case 'K':
400 case 'J':
401#ifdef FEAT_VERTSPLIT
402 case 'H':
403 case 'L':
404#endif
405 CHECK_CMDWIN
406 win_totop((int)Prenum,
407 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
408 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
409 break;
410
411/* make all windows the same height */
412 case '=':
413#ifdef FEAT_GUI
414 need_mouse_correct = TRUE;
415#endif
416 win_equal(NULL, FALSE, 'b');
417 break;
418
419/* increase current window height */
420 case '+':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setheight(curwin->w_height + (int)Prenum1);
425 break;
426
427/* decrease current window height */
428 case '-':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_setheight(curwin->w_height - (int)Prenum1);
433 break;
434
435/* set current window height */
436 case Ctrl__:
437 case '_':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(Prenum ? (int)Prenum : 9999);
442 break;
443
444#ifdef FEAT_VERTSPLIT
445/* increase current window width */
446 case '>':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setwidth(curwin->w_width + (int)Prenum1);
451 break;
452
453/* decrease current window width */
454 case '<':
455#ifdef FEAT_GUI
456 need_mouse_correct = TRUE;
457#endif
458 win_setwidth(curwin->w_width - (int)Prenum1);
459 break;
460
461/* set current window width */
462 case '|':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
467 break;
468#endif
469
470/* jump to tag and split window if tag exists (in preview window) */
471#if defined(FEAT_QUICKFIX)
472 case '}':
473 CHECK_CMDWIN
474 if (Prenum)
475 g_do_tagpreview = Prenum;
476 else
477 g_do_tagpreview = p_pvh;
478 /*FALLTHROUGH*/
479#endif
480 case ']':
481 case Ctrl_RSB:
482 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200483 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (Prenum)
485 postponed_split = Prenum;
486 else
487 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200488#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200489 if (nchar != '}')
490 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaard355c502014-09-23 13:48:43 +0200493 /* Execute the command right here, required when "wincmd ]"
494 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 do_nv_ident(Ctrl_RSB, NUL);
496 break;
497
498#ifdef FEAT_SEARCHPATH
499/* edit file name under cursor in a new window */
500 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000503wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000505
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000506 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (ptr != NULL)
508 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000509# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 setpcmark();
513 if (win_split(0, 0) == OK)
514 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200515 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000516 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
517 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 if (nchar == 'F' && lnum >= 0)
519 {
520 curwin->w_cursor.lnum = lnum;
521 check_cursor_lnum();
522 beginline(BL_SOL | BL_FIX);
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 vim_free(ptr);
526 }
527 break;
528#endif
529
530#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000531/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 * new window -- webb
533 */
534 case 'i': /* Go to any match */
535 case Ctrl_I:
536 type = FIND_ANY;
537 /* FALLTHROUGH */
538 case 'd': /* Go to definition, using 'define' */
539 case Ctrl_D:
540 CHECK_CMDWIN
541 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
542 break;
543 find_pattern_in_path(ptr, 0, len, TRUE,
544 Prenum == 0 ? TRUE : FALSE, type,
545 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
546 curwin->w_set_curswant = TRUE;
547 break;
548#endif
549
Bram Moolenaar05159a02005-02-26 23:04:13 +0000550 case K_KENTER:
551 case CAR:
552#if defined(FEAT_QUICKFIX)
553 /*
554 * In a quickfix window a <CR> jumps to the error under the
555 * cursor in a new window.
556 */
557 if (bt_quickfix(curbuf))
558 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000559 sprintf((char *)cbuf, "split +%ld%s",
560 (long)curwin->w_cursor.lnum,
561 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000562 do_cmdline_cmd(cbuf);
563 }
564#endif
565 break;
566
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568/* CTRL-W g extended commands */
569 case 'g':
570 case Ctrl_G:
571 CHECK_CMDWIN
572#ifdef USE_ON_FLY_SCROLL
573 dont_scroll = TRUE; /* disallow scrolling here */
574#endif
575 ++no_mapping;
576 ++allow_keys; /* no mapping for xchar, but allow key codes */
577 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000578 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 --no_mapping;
581 --allow_keys;
582#ifdef FEAT_CMDL_INFO
583 (void)add_to_showcmd(xchar);
584#endif
585 switch (xchar)
586 {
587#if defined(FEAT_QUICKFIX)
588 case '}':
589 xchar = Ctrl_RSB;
590 if (Prenum)
591 g_do_tagpreview = Prenum;
592 else
593 g_do_tagpreview = p_pvh;
594 /*FALLTHROUGH*/
595#endif
596 case ']':
597 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200598 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (Prenum)
600 postponed_split = Prenum;
601 else
602 postponed_split = -1;
603
604 /* Execute the command right here, required when
605 * "wincmd g}" was used in a function. */
606 do_nv_ident('g', xchar);
607 break;
608
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609#ifdef FEAT_SEARCHPATH
610 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000611 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100612 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000613 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000614 goto wingotofile;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 default:
617 beep_flush();
618 break;
619 }
620 break;
621
622 default: beep_flush();
623 break;
624 }
625}
626
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100627/*
628 * Figure out the address type for ":wnncmd".
629 */
630 void
631get_wincmd_addr_type(arg, eap)
632 char_u *arg;
633 exarg_T *eap;
634{
635 switch (*arg)
636 {
637 case 'S':
638 case Ctrl_S:
639 case 's':
640 case Ctrl_N:
641 case 'n':
642 case 'j':
643 case Ctrl_J:
644 case 'k':
645 case Ctrl_K:
646 case 'T':
647 case Ctrl_R:
648 case 'r':
649 case 'R':
650 case 'K':
651 case 'J':
652 case '+':
653 case '-':
654 case Ctrl__:
655 case '_':
656 case '|':
657 case ']':
658 case Ctrl_RSB:
659 case 'g':
660 case Ctrl_G:
661#ifdef FEAT_VERTSPLIT
662 case Ctrl_V:
663 case 'v':
664 case 'h':
665 case Ctrl_H:
666 case 'l':
667 case Ctrl_L:
668 case 'H':
669 case 'L':
670 case '>':
671 case '<':
672#endif
673#if defined(FEAT_QUICKFIX)
674 case '}':
675#endif
676#ifdef FEAT_SEARCHPATH
677 case 'f':
678 case 'F':
679 case Ctrl_F:
680#endif
681#ifdef FEAT_FIND_ID
682 case 'i':
683 case Ctrl_I:
684 case 'd':
685 case Ctrl_D:
686#endif
687 /* window size or any count */
688 eap->addr_type = ADDR_LINES;
689 break;
690
691 case Ctrl_HAT:
692 case '^':
693 /* buffer number */
694 eap->addr_type = ADDR_BUFFERS;
695 break;
696
697 case Ctrl_Q:
698 case 'q':
699 case Ctrl_C:
700 case 'c':
701 case Ctrl_O:
702 case 'o':
703 case Ctrl_W:
704 case 'w':
705 case 'W':
706 case 'x':
707 case Ctrl_X:
708 /* window number */
709 eap->addr_type = ADDR_WINDOWS;
710 break;
711
712#if defined(FEAT_QUICKFIX)
713 case Ctrl_Z:
714 case 'z':
715 case 'P':
716#endif
717 case 't':
718 case Ctrl_T:
719 case 'b':
720 case Ctrl_B:
721 case 'p':
722 case Ctrl_P:
723 case '=':
724 case CAR:
725 /* no count */
726 eap->addr_type = 0;
727 break;
728 }
729}
730
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100731 static void
732cmd_with_count(cmd, bufp, bufsize, Prenum)
733 char *cmd;
734 char_u *bufp;
735 size_t bufsize;
736 long Prenum;
737{
738 size_t len = STRLEN(cmd);
739
740 STRCPY(bufp, cmd);
741 if (Prenum > 0)
742 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * split the current window, implements CTRL-W s and :split
747 *
748 * "size" is the height or width for the new window, 0 to use half of current
749 * height or width.
750 *
751 * "flags":
752 * WSP_ROOM: require enough room for new window
753 * WSP_VERT: vertical split.
754 * WSP_TOP: open window at the top-left of the shell (help window).
755 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
756 * WSP_HELP: creating the help window, keep layout snapshot
757 *
758 * return FAIL for failure, OK otherwise
759 */
760 int
761win_split(size, flags)
762 int size;
763 int flags;
764{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000765 /* When the ":tab" modifier was used open a new tab page instead. */
766 if (may_open_tabpage() == OK)
767 return OK;
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* Add flags from ":vertical", ":topleft" and ":botright". */
770 flags |= cmdmod.split;
771 if ((flags & WSP_TOP) && (flags & WSP_BOT))
772 {
773 EMSG(_("E442: Can't split topleft and botright at the same time"));
774 return FAIL;
775 }
776
777 /* When creating the help window make a snapshot of the window layout.
778 * Otherwise clear the snapshot, it's now invalid. */
779 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000780 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000782 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784 return win_split_ins(size, flags, NULL, 0);
785}
786
787/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100788 * When "new_wp" is NULL: split the current window in two.
789 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * top/left/right/bottom.
791 * return FAIL for failure, OK otherwise
792 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000793 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100794win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 int size;
796 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100797 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 int dir;
799{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100800 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 win_T *oldwin;
802 int new_size = size;
803 int i;
804 int need_status = 0;
805 int do_equal = FALSE;
806 int needed;
807 int available;
808 int oldwin_height = 0;
809 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200810 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200812 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200813 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 if (flags & WSP_TOP)
816 oldwin = firstwin;
817 else if (flags & WSP_BOT)
818 oldwin = lastwin;
819 else
820 oldwin = curwin;
821
822 /* add a status line when p_ls == 1 and splitting the first window */
823 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
824 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100825 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 EMSG(_(e_noroom));
828 return FAIL;
829 }
830 need_status = STATUS_HEIGHT;
831 }
832
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000833#ifdef FEAT_GUI
834 /* May be needed for the scrollbars that are going to change. */
835 if (gui.in_use)
836 out_flush();
837#endif
838
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_VERTSPLIT
840 if (flags & WSP_VERT)
841 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200842 int wmw1;
843 int minwidth;
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847 /*
848 * Check if we are able to split the current window and compute its
849 * width.
850 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200851 /* Current window requires at least 1 space. */
852 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
853 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200855 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200856 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200858 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200860 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200862 else if (p_ea)
863 {
864 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
865 prevfrp = oldwin->w_frame;
866 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
867 frp = frp->fr_parent)
868 {
869 if (frp->fr_layout == FR_ROW)
870 for (frp2 = frp->fr_child; frp2 != NULL;
871 frp2 = frp2->fr_next)
872 if (frp2 != prevfrp)
873 minwidth += frame_minwidth(frp2, NOWIN);
874 prevfrp = frp;
875 }
876 available = topframe->fr_width;
877 needed += minwidth;
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200880 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200881 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
882 available = oldwin->w_frame->fr_width;
883 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200884 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100885 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 EMSG(_(e_noroom));
888 return FAIL;
889 }
890 if (new_size == 0)
891 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200892 if (new_size > available - minwidth - 1)
893 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200894 if (new_size < wmw1)
895 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 /* if it doesn't fit in the current window, need win_equal() */
898 if (oldwin->w_width - new_size - 1 < p_wmw)
899 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000900
901 /* We don't like to take lines for the new window from a
902 * 'winfixwidth' window. Take them from a window to the left or right
903 * instead, if possible. */
904 if (oldwin->w_p_wfw)
905 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000906
907 /* Only make all windows the same width if one of them (except oldwin)
908 * is wider than one of the split windows. */
909 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
910 && oldwin->w_frame->fr_parent != NULL)
911 {
912 frp = oldwin->w_frame->fr_parent->fr_child;
913 while (frp != NULL)
914 {
915 if (frp->fr_win != oldwin && frp->fr_win != NULL
916 && (frp->fr_win->w_width > new_size
917 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100918 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000919 {
920 do_equal = TRUE;
921 break;
922 }
923 frp = frp->fr_next;
924 }
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927 else
928#endif
929 {
930 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /*
933 * Check if we are able to split the current window and compute its
934 * height.
935 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200936 /* Current window requires at least 1 space. */
937 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
938 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200940 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200941 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200943 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200945 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200947 else if (p_ea)
948 {
949 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
950 prevfrp = oldwin->w_frame;
951 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
952 frp = frp->fr_parent)
953 {
954 if (frp->fr_layout == FR_COL)
955 for (frp2 = frp->fr_child; frp2 != NULL;
956 frp2 = frp2->fr_next)
957 if (frp2 != prevfrp)
958 minheight += frame_minheight(frp2, NOWIN);
959 prevfrp = frp;
960 }
961 available = topframe->fr_height;
962 needed += minheight;
963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 else
965 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200966 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
967 available = oldwin->w_frame->fr_height;
968 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100970 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 EMSG(_(e_noroom));
973 return FAIL;
974 }
975 oldwin_height = oldwin->w_height;
976 if (need_status)
977 {
978 oldwin->w_status_height = STATUS_HEIGHT;
979 oldwin_height -= STATUS_HEIGHT;
980 }
981 if (new_size == 0)
982 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200983 if (new_size > available - minheight - STATUS_HEIGHT)
984 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200985 if (new_size < wmh1)
986 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /* if it doesn't fit in the current window, need win_equal() */
989 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
990 do_equal = TRUE;
991
992 /* We don't like to take lines for the new window from a
993 * 'winfixheight' window. Take them from a window above or below
994 * instead, if possible. */
995 if (oldwin->w_p_wfh)
996 {
997 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
998 oldwin);
999 oldwin_height = oldwin->w_height;
1000 if (need_status)
1001 oldwin_height -= STATUS_HEIGHT;
1002 }
Bram Moolenaar67f71312007-08-12 14:55:56 +00001003
1004 /* Only make all windows the same height if one of them (except oldwin)
1005 * is higher than one of the split windows. */
1006 if (!do_equal && p_ea && size == 0
1007#ifdef FEAT_VERTSPLIT
1008 && *p_ead != 'h'
1009#endif
1010 && oldwin->w_frame->fr_parent != NULL)
1011 {
1012 frp = oldwin->w_frame->fr_parent->fr_child;
1013 while (frp != NULL)
1014 {
1015 if (frp->fr_win != oldwin && frp->fr_win != NULL
1016 && (frp->fr_win->w_height > new_size
1017 || frp->fr_win->w_height > oldwin_height - new_size
1018 - STATUS_HEIGHT))
1019 {
1020 do_equal = TRUE;
1021 break;
1022 }
1023 frp = frp->fr_next;
1024 }
1025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027
1028 /*
1029 * allocate new window structure and link it in the window list
1030 */
1031 if ((flags & WSP_TOP) == 0
1032 && ((flags & WSP_BOT)
1033 || (flags & WSP_BELOW)
1034 || (!(flags & WSP_ABOVE)
1035 && (
1036#ifdef FEAT_VERTSPLIT
1037 (flags & WSP_VERT) ? p_spr :
1038#endif
1039 p_sb))))
1040 {
1041 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001042 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001043 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 else
1045 win_append(oldwin, wp);
1046 }
1047 else
1048 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001049 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001050 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else
1052 win_append(oldwin->w_prev, wp);
1053 }
1054
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001055 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 if (wp == NULL)
1058 return FAIL;
1059
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001060 new_frame(wp);
1061 if (wp->w_frame == NULL)
1062 {
1063 win_free(wp, NULL);
1064 return FAIL;
1065 }
1066
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001067 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001068 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070
1071 /*
1072 * Reorganise the tree of frames to insert the new window.
1073 */
1074 if (flags & (WSP_TOP | WSP_BOT))
1075 {
1076#ifdef FEAT_VERTSPLIT
1077 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1078 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1079#else
1080 if (topframe->fr_layout == FR_COL)
1081#endif
1082 {
1083 curfrp = topframe->fr_child;
1084 if (flags & WSP_BOT)
1085 while (curfrp->fr_next != NULL)
1086 curfrp = curfrp->fr_next;
1087 }
1088 else
1089 curfrp = topframe;
1090 before = (flags & WSP_TOP);
1091 }
1092 else
1093 {
1094 curfrp = oldwin->w_frame;
1095 if (flags & WSP_BELOW)
1096 before = FALSE;
1097 else if (flags & WSP_ABOVE)
1098 before = TRUE;
1099 else
1100#ifdef FEAT_VERTSPLIT
1101 if (flags & WSP_VERT)
1102 before = !p_spr;
1103 else
1104#endif
1105 before = !p_sb;
1106 }
1107 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1108 {
1109 /* Need to create a new frame in the tree to make a branch. */
1110 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1111 *frp = *curfrp;
1112 curfrp->fr_layout = layout;
1113 frp->fr_parent = curfrp;
1114 frp->fr_next = NULL;
1115 frp->fr_prev = NULL;
1116 curfrp->fr_child = frp;
1117 curfrp->fr_win = NULL;
1118 curfrp = frp;
1119 if (frp->fr_win != NULL)
1120 oldwin->w_frame = frp;
1121 else
1122 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1123 frp->fr_parent = curfrp;
1124 }
1125
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001126 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001127 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001129 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 frp->fr_parent = curfrp->fr_parent;
1131
1132 /* Insert the new frame at the right place in the frame list. */
1133 if (before)
1134 frame_insert(curfrp, frp);
1135 else
1136 frame_append(curfrp, frp);
1137
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001138 /* Set w_fraction now so that the cursor keeps the same relative
1139 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001140 if (oldwin->w_height > 0)
1141 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001142 wp->w_fraction = oldwin->w_fraction;
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#ifdef FEAT_VERTSPLIT
1145 if (flags & WSP_VERT)
1146 {
1147 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (need_status)
1150 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001151 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 oldwin->w_status_height = need_status;
1153 }
1154 if (flags & (WSP_TOP | WSP_BOT))
1155 {
1156 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001157 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001158 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 wp->w_status_height = (p_ls > 0);
1160 }
1161 else
1162 {
1163 /* height and row of new window is same as current window */
1164 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001165 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_status_height = oldwin->w_status_height;
1167 }
1168 frp->fr_height = curfrp->fr_height;
1169
1170 /* "new_size" of the current window goes to the new window, use
1171 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001172 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (before)
1174 wp->w_vsep_width = 1;
1175 else
1176 {
1177 wp->w_vsep_width = oldwin->w_vsep_width;
1178 oldwin->w_vsep_width = 1;
1179 }
1180 if (flags & (WSP_TOP | WSP_BOT))
1181 {
1182 if (flags & WSP_BOT)
1183 frame_add_vsep(curfrp);
1184 /* Set width of neighbor frame */
1185 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001186 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1187 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001190 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (before) /* new window left of current one */
1192 {
1193 wp->w_wincol = oldwin->w_wincol;
1194 oldwin->w_wincol += new_size + 1;
1195 }
1196 else /* new window right of current one */
1197 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1198 frame_fix_width(oldwin);
1199 frame_fix_width(wp);
1200 }
1201 else
1202#endif
1203 {
1204 /* width and column of new window is same as current window */
1205#ifdef FEAT_VERTSPLIT
1206 if (flags & (WSP_TOP | WSP_BOT))
1207 {
1208 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001209 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 wp->w_vsep_width = 0;
1211 }
1212 else
1213 {
1214 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001215 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 wp->w_vsep_width = oldwin->w_vsep_width;
1217 }
1218 frp->fr_width = curfrp->fr_width;
1219#endif
1220
1221 /* "new_size" of the current window goes to the new window, use
1222 * one row for the status line */
1223 win_new_height(wp, new_size);
1224 if (flags & (WSP_TOP | WSP_BOT))
1225 frame_new_height(curfrp, curfrp->fr_height
1226 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1227 else
1228 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1229 if (before) /* new window above current one */
1230 {
1231 wp->w_winrow = oldwin->w_winrow;
1232 wp->w_status_height = STATUS_HEIGHT;
1233 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1234 }
1235 else /* new window below current one */
1236 {
1237 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1238 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001239 /* Don't set the status_height for oldwin yet, this might break
1240 * frame_fix_height(oldwin), therefore will be set below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242#ifdef FEAT_VERTSPLIT
1243 if (flags & WSP_BOT)
1244 frame_add_statusline(curfrp);
1245#endif
1246 frame_fix_height(wp);
1247 frame_fix_height(oldwin);
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001248
1249 if (!before)
1250 /* new window above current one, set the status_height after
1251 * frame_fix_height(oldwin) */
1252 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254
1255 if (flags & (WSP_TOP | WSP_BOT))
1256 (void)win_comp_pos();
1257
1258 /*
1259 * Both windows need redrawing
1260 */
1261 redraw_win_later(wp, NOT_VALID);
1262 wp->w_redr_status = TRUE;
1263 redraw_win_later(oldwin, NOT_VALID);
1264 oldwin->w_redr_status = TRUE;
1265
1266 if (need_status)
1267 {
1268 msg_row = Rows - 1;
1269 msg_col = sc_col;
1270 msg_clr_eos_force(); /* Old command/ruler may still be there */
1271 comp_col();
1272 msg_row = Rows - 1;
1273 msg_col = 0; /* put position back at start of line */
1274 }
1275
1276 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001277 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
1279 if (do_equal || dir != 0)
1280 win_equal(wp, TRUE,
1281#ifdef FEAT_VERTSPLIT
1282 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1283 : dir == 'h' ? 'b' :
1284#endif
1285 'v');
1286
1287 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1288 * size was given. */
1289#ifdef FEAT_VERTSPLIT
1290 if (flags & WSP_VERT)
1291 {
1292 i = p_wiw;
1293 if (size != 0)
1294 p_wiw = size;
1295
1296# ifdef FEAT_GUI
1297 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1298 if (gui.in_use)
1299 gui_init_which_components(NULL);
1300# endif
1301 }
1302 else
1303#endif
1304 {
1305 i = p_wh;
1306 if (size != 0)
1307 p_wh = size;
1308 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001309
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001310#ifdef FEAT_JUMPLIST
1311 /* Keep same changelist position in new window. */
1312 wp->w_changelistidx = oldwin->w_changelistidx;
1313#endif
1314
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001315 /*
1316 * make the new window the current window
1317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 win_enter(wp, FALSE);
1319#ifdef FEAT_VERTSPLIT
1320 if (flags & WSP_VERT)
1321 p_wiw = i;
1322 else
1323#endif
1324 p_wh = i;
1325
1326 return OK;
1327}
1328
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001329
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001330/*
1331 * Initialize window "newp" from window "oldp".
1332 * Used when splitting a window and when creating a new tab page.
1333 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001334 * WSP_NEWLOC may be specified in flags to prevent the location list from
1335 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001336 */
1337 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001338win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001339 win_T *newp;
1340 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001341 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001342{
1343 int i;
1344
1345 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001346#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001347 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001348#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001349 oldp->w_buffer->b_nwindows++;
1350 newp->w_cursor = oldp->w_cursor;
1351 newp->w_valid = 0;
1352 newp->w_curswant = oldp->w_curswant;
1353 newp->w_set_curswant = oldp->w_set_curswant;
1354 newp->w_topline = oldp->w_topline;
1355#ifdef FEAT_DIFF
1356 newp->w_topfill = oldp->w_topfill;
1357#endif
1358 newp->w_leftcol = oldp->w_leftcol;
1359 newp->w_pcmark = oldp->w_pcmark;
1360 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1361 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001362 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001363 newp->w_fraction = oldp->w_fraction;
1364 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1365#ifdef FEAT_JUMPLIST
1366 copy_jumplist(oldp, newp);
1367#endif
1368#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001369 if (flags & WSP_NEWLOC)
1370 {
1371 /* Don't copy the location list. */
1372 newp->w_llist = NULL;
1373 newp->w_llist_ref = NULL;
1374 }
1375 else
1376 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001377#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001378 newp->w_localdir = (oldp->w_localdir == NULL)
1379 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001380
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001381 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001382 for (i = 0; i < oldp->w_tagstacklen; i++)
1383 {
1384 newp->w_tagstack[i] = oldp->w_tagstack[i];
1385 if (newp->w_tagstack[i].tagname != NULL)
1386 newp->w_tagstack[i].tagname =
1387 vim_strsave(newp->w_tagstack[i].tagname);
1388 }
1389 newp->w_tagstackidx = oldp->w_tagstackidx;
1390 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001391#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001392 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001393#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001394
1395 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001396
Bram Moolenaara971b822011-09-14 14:43:25 +02001397#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001398 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001399#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001400}
1401
1402/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001403 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001404 * Only the essential things are copied.
1405 */
1406 static void
1407win_init_some(newp, oldp)
1408 win_T *newp;
1409 win_T *oldp;
1410{
1411 /* Use the same argument list. */
1412 newp->w_alist = oldp->w_alist;
1413 ++newp->w_alist->al_refcount;
1414 newp->w_arg_idx = oldp->w_arg_idx;
1415
1416 /* copy options from existing window */
1417 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001418}
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#endif /* FEAT_WINDOWS */
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422#if defined(FEAT_WINDOWS) || defined(PROTO)
1423/*
1424 * Check if "win" is a pointer to an existing window.
1425 */
1426 int
1427win_valid(win)
1428 win_T *win;
1429{
1430 win_T *wp;
1431
1432 if (win == NULL)
1433 return FALSE;
1434 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1435 if (wp == win)
1436 return TRUE;
1437 return FALSE;
1438}
1439
1440/*
1441 * Return the number of windows.
1442 */
1443 int
1444win_count()
1445{
1446 win_T *wp;
1447 int count = 0;
1448
1449 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1450 ++count;
1451 return count;
1452}
1453
1454/*
1455 * Make "count" windows on the screen.
1456 * Return actual number of windows on the screen.
1457 * Must be called when there is just one window, filling the whole screen
1458 * (excluding the command line).
1459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 int
1461make_windows(count, vertical)
1462 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001463 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 int maxcount;
1466 int todo;
1467
1468#ifdef FEAT_VERTSPLIT
1469 if (vertical)
1470 {
1471 /* Each windows needs at least 'winminwidth' lines and a separator
1472 * column. */
1473 maxcount = (curwin->w_width + curwin->w_vsep_width
1474 - (p_wiw - p_wmw)) / (p_wmw + 1);
1475 }
1476 else
1477#endif
1478 {
1479 /* Each window needs at least 'winminheight' lines and a status line. */
1480 maxcount = (curwin->w_height + curwin->w_status_height
1481 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1482 }
1483
1484 if (maxcount < 2)
1485 maxcount = 2;
1486 if (count > maxcount)
1487 count = maxcount;
1488
1489 /*
1490 * add status line now, otherwise first window will be too big
1491 */
1492 if (count > 1)
1493 last_status(TRUE);
1494
1495#ifdef FEAT_AUTOCMD
1496 /*
1497 * Don't execute autocommands while creating the windows. Must do that
1498 * when putting the buffers in the windows.
1499 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001500 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502
1503 /* todo is number of windows left to create */
1504 for (todo = count - 1; todo > 0; --todo)
1505#ifdef FEAT_VERTSPLIT
1506 if (vertical)
1507 {
1508 if (win_split(curwin->w_width - (curwin->w_width - todo)
1509 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1510 break;
1511 }
1512 else
1513#endif
1514 {
1515 if (win_split(curwin->w_height - (curwin->w_height - todo
1516 * STATUS_HEIGHT) / (todo + 1)
1517 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1518 break;
1519 }
1520
1521#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001522 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523#endif
1524
1525 /* return actual number of windows */
1526 return (count - todo);
1527}
1528
1529/*
1530 * Exchange current and next window
1531 */
1532 static void
1533win_exchange(Prenum)
1534 long Prenum;
1535{
1536 frame_T *frp;
1537 frame_T *frp2;
1538 win_T *wp;
1539 win_T *wp2;
1540 int temp;
1541
1542 if (lastwin == firstwin) /* just one window */
1543 {
1544 beep_flush();
1545 return;
1546 }
1547
1548#ifdef FEAT_GUI
1549 need_mouse_correct = TRUE;
1550#endif
1551
1552 /*
1553 * find window to exchange with
1554 */
1555 if (Prenum)
1556 {
1557 frp = curwin->w_frame->fr_parent->fr_child;
1558 while (frp != NULL && --Prenum > 0)
1559 frp = frp->fr_next;
1560 }
1561 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1562 frp = curwin->w_frame->fr_next;
1563 else /* Swap last window in row/col with previous */
1564 frp = curwin->w_frame->fr_prev;
1565
1566 /* We can only exchange a window with another window, not with a frame
1567 * containing windows. */
1568 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1569 return;
1570 wp = frp->fr_win;
1571
1572/*
1573 * 1. remove curwin from the list. Remember after which window it was in wp2
1574 * 2. insert curwin before wp in the list
1575 * if wp != wp2
1576 * 3. remove wp from the list
1577 * 4. insert wp after wp2
1578 * 5. exchange the status line height and vsep width.
1579 */
1580 wp2 = curwin->w_prev;
1581 frp2 = curwin->w_frame->fr_prev;
1582 if (wp->w_prev != curwin)
1583 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001584 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 frame_remove(curwin->w_frame);
1586 win_append(wp->w_prev, curwin);
1587 frame_insert(frp, curwin->w_frame);
1588 }
1589 if (wp != wp2)
1590 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001591 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 frame_remove(wp->w_frame);
1593 win_append(wp2, wp);
1594 if (frp2 == NULL)
1595 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1596 else
1597 frame_append(frp2, wp->w_frame);
1598 }
1599 temp = curwin->w_status_height;
1600 curwin->w_status_height = wp->w_status_height;
1601 wp->w_status_height = temp;
1602#ifdef FEAT_VERTSPLIT
1603 temp = curwin->w_vsep_width;
1604 curwin->w_vsep_width = wp->w_vsep_width;
1605 wp->w_vsep_width = temp;
1606
1607 /* If the windows are not in the same frame, exchange the sizes to avoid
1608 * messing up the window layout. Otherwise fix the frame sizes. */
1609 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1610 {
1611 temp = curwin->w_height;
1612 curwin->w_height = wp->w_height;
1613 wp->w_height = temp;
1614 temp = curwin->w_width;
1615 curwin->w_width = wp->w_width;
1616 wp->w_width = temp;
1617 }
1618 else
1619 {
1620 frame_fix_height(curwin);
1621 frame_fix_height(wp);
1622 frame_fix_width(curwin);
1623 frame_fix_width(wp);
1624 }
1625#endif
1626
1627 (void)win_comp_pos(); /* recompute window positions */
1628
1629 win_enter(wp, TRUE);
1630 redraw_later(CLEAR);
1631}
1632
1633/*
1634 * rotate windows: if upwards TRUE the second window becomes the first one
1635 * if upwards FALSE the first window becomes the second one
1636 */
1637 static void
1638win_rotate(upwards, count)
1639 int upwards;
1640 int count;
1641{
1642 win_T *wp1;
1643 win_T *wp2;
1644 frame_T *frp;
1645 int n;
1646
1647 if (firstwin == lastwin) /* nothing to do */
1648 {
1649 beep_flush();
1650 return;
1651 }
1652
1653#ifdef FEAT_GUI
1654 need_mouse_correct = TRUE;
1655#endif
1656
1657#ifdef FEAT_VERTSPLIT
1658 /* Check if all frames in this row/col have one window. */
1659 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1660 frp = frp->fr_next)
1661 if (frp->fr_win == NULL)
1662 {
1663 EMSG(_("E443: Cannot rotate when another window is split"));
1664 return;
1665 }
1666#endif
1667
1668 while (count--)
1669 {
1670 if (upwards) /* first window becomes last window */
1671 {
1672 /* remove first window/frame from the list */
1673 frp = curwin->w_frame->fr_parent->fr_child;
1674 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001675 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 frame_remove(frp);
1677
1678 /* find last frame and append removed window/frame after it */
1679 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1680 ;
1681 win_append(frp->fr_win, wp1);
1682 frame_append(frp, wp1->w_frame);
1683
1684 wp2 = frp->fr_win; /* previously last window */
1685 }
1686 else /* last window becomes first window */
1687 {
1688 /* find last window/frame in the list and remove it */
1689 for (frp = curwin->w_frame; frp->fr_next != NULL;
1690 frp = frp->fr_next)
1691 ;
1692 wp1 = frp->fr_win;
1693 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001694 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 frame_remove(frp);
1696
1697 /* append the removed window/frame before the first in the list */
1698 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1699 frame_insert(frp->fr_parent->fr_child, frp);
1700 }
1701
1702 /* exchange status height and vsep width of old and new last window */
1703 n = wp2->w_status_height;
1704 wp2->w_status_height = wp1->w_status_height;
1705 wp1->w_status_height = n;
1706 frame_fix_height(wp1);
1707 frame_fix_height(wp2);
1708#ifdef FEAT_VERTSPLIT
1709 n = wp2->w_vsep_width;
1710 wp2->w_vsep_width = wp1->w_vsep_width;
1711 wp1->w_vsep_width = n;
1712 frame_fix_width(wp1);
1713 frame_fix_width(wp2);
1714#endif
1715
1716 /* recompute w_winrow and w_wincol for all windows */
1717 (void)win_comp_pos();
1718 }
1719
1720 redraw_later(CLEAR);
1721}
1722
1723/*
1724 * Move the current window to the very top/bottom/left/right of the screen.
1725 */
1726 static void
1727win_totop(size, flags)
1728 int size;
1729 int flags;
1730{
1731 int dir;
1732 int height = curwin->w_height;
1733
1734 if (lastwin == firstwin)
1735 {
1736 beep_flush();
1737 return;
1738 }
1739
1740 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001741 (void)winframe_remove(curwin, &dir, NULL);
1742 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 last_status(FALSE); /* may need to remove last status line */
1744 (void)win_comp_pos(); /* recompute window positions */
1745
1746 /* Split a window on the desired side and put the window there. */
1747 (void)win_split_ins(size, flags, curwin, dir);
1748 if (!(flags & WSP_VERT))
1749 {
1750 win_setheight(height);
1751 if (p_ea)
1752 win_equal(curwin, TRUE, 'v');
1753 }
1754
1755#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1756 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1757 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001758 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760}
1761
1762/*
1763 * Move window "win1" to below/right of "win2" and make "win1" the current
1764 * window. Only works within the same frame!
1765 */
1766 void
1767win_move_after(win1, win2)
1768 win_T *win1, *win2;
1769{
1770 int height;
1771
1772 /* check if the arguments are reasonable */
1773 if (win1 == win2)
1774 return;
1775
1776 /* check if there is something to do */
1777 if (win2->w_next != win1)
1778 {
1779 /* may need move the status line/vertical separator of the last window
1780 * */
1781 if (win1 == lastwin)
1782 {
1783 height = win1->w_prev->w_status_height;
1784 win1->w_prev->w_status_height = win1->w_status_height;
1785 win1->w_status_height = height;
1786#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001787 if (win1->w_prev->w_vsep_width == 1)
1788 {
1789 /* Remove the vertical separator from the last-but-one window,
1790 * add it to the last window. Adjust the frame widths. */
1791 win1->w_prev->w_vsep_width = 0;
1792 win1->w_prev->w_frame->fr_width -= 1;
1793 win1->w_vsep_width = 1;
1794 win1->w_frame->fr_width += 1;
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
1797 }
1798 else if (win2 == lastwin)
1799 {
1800 height = win1->w_status_height;
1801 win1->w_status_height = win2->w_status_height;
1802 win2->w_status_height = height;
1803#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001804 if (win1->w_vsep_width == 1)
1805 {
1806 /* Remove the vertical separator from win1, add it to the last
1807 * window, win2. Adjust the frame widths. */
1808 win2->w_vsep_width = 1;
1809 win2->w_frame->fr_width += 1;
1810 win1->w_vsep_width = 0;
1811 win1->w_frame->fr_width -= 1;
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#endif
1814 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001815 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 frame_remove(win1->w_frame);
1817 win_append(win2, win1);
1818 frame_append(win2->w_frame, win1->w_frame);
1819
1820 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1821 redraw_later(NOT_VALID);
1822 }
1823 win_enter(win1, FALSE);
1824}
1825
1826/*
1827 * Make all windows the same height.
1828 * 'next_curwin' will soon be the current window, make sure it has enough
1829 * rows.
1830 */
1831 void
1832win_equal(next_curwin, current, dir)
1833 win_T *next_curwin; /* pointer to current window to be or NULL */
1834 int current; /* do only frame with current window */
1835 int dir; /* 'v' for vertically, 'h' for horizontally,
1836 'b' for both, 0 for using p_ead */
1837{
1838 if (dir == 0)
1839#ifdef FEAT_VERTSPLIT
1840 dir = *p_ead;
1841#else
1842 dir = 'b';
1843#endif
1844 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001845 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001846 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847}
1848
1849/*
1850 * Set a frame to a new position and height, spreading the available room
1851 * equally over contained frames.
1852 * The window "next_curwin" (if not NULL) should at least get the size from
1853 * 'winheight' and 'winwidth' if possible.
1854 */
1855 static void
1856win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1857 win_T *next_curwin; /* pointer to current window to be or NULL */
1858 int current; /* do only frame with current window */
1859 frame_T *topfr; /* frame to set size off */
1860 int dir; /* 'v', 'h' or 'b', see win_equal() */
1861 int col; /* horizontal position for frame */
1862 int row; /* vertical position for frame */
1863 int width; /* new width of frame */
1864 int height; /* new height of frame */
1865{
1866 int n, m;
1867 int extra_sep = 0;
1868 int wincount, totwincount = 0;
1869 frame_T *fr;
1870 int next_curwin_size = 0;
1871 int room = 0;
1872 int new_size;
1873 int has_next_curwin = 0;
1874 int hnc;
1875
1876 if (topfr->fr_layout == FR_LEAF)
1877 {
1878 /* Set the width/height of this frame.
1879 * Redraw when size or position changes */
1880 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1881#ifdef FEAT_VERTSPLIT
1882 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1883#endif
1884 )
1885 {
1886 topfr->fr_win->w_winrow = row;
1887 frame_new_height(topfr, height, FALSE, FALSE);
1888#ifdef FEAT_VERTSPLIT
1889 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001890 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
1892 redraw_all_later(CLEAR);
1893 }
1894 }
1895#ifdef FEAT_VERTSPLIT
1896 else if (topfr->fr_layout == FR_ROW)
1897 {
1898 topfr->fr_width = width;
1899 topfr->fr_height = height;
1900
1901 if (dir != 'v') /* equalize frame widths */
1902 {
1903 /* Compute the maximum number of windows horizontally in this
1904 * frame. */
1905 n = frame_minwidth(topfr, NOWIN);
1906 /* add one for the rightmost window, it doesn't have a separator */
1907 if (col + width == Columns)
1908 extra_sep = 1;
1909 else
1910 extra_sep = 0;
1911 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001912 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001914 /*
1915 * Compute width for "next_curwin" window and room available for
1916 * other windows.
1917 * "m" is the minimal width when counting p_wiw for "next_curwin".
1918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 m = frame_minwidth(topfr, next_curwin);
1920 room = width - m;
1921 if (room < 0)
1922 {
1923 next_curwin_size = p_wiw + room;
1924 room = 0;
1925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 else
1927 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001928 next_curwin_size = -1;
1929 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1930 {
1931 /* If 'winfixwidth' set keep the window width if
1932 * possible.
1933 * Watch out for this window being the next_curwin. */
1934 if (frame_fixed_width(fr))
1935 {
1936 n = frame_minwidth(fr, NOWIN);
1937 new_size = fr->fr_width;
1938 if (frame_has_win(fr, next_curwin))
1939 {
1940 room += p_wiw - p_wmw;
1941 next_curwin_size = 0;
1942 if (new_size < p_wiw)
1943 new_size = p_wiw;
1944 }
1945 else
1946 /* These windows don't use up room. */
1947 totwincount -= (n + (fr->fr_next == NULL
1948 ? extra_sep : 0)) / (p_wmw + 1);
1949 room -= new_size - n;
1950 if (room < 0)
1951 {
1952 new_size += room;
1953 room = 0;
1954 }
1955 fr->fr_newwidth = new_size;
1956 }
1957 }
1958 if (next_curwin_size == -1)
1959 {
1960 if (!has_next_curwin)
1961 next_curwin_size = 0;
1962 else if (totwincount > 1
1963 && (room + (totwincount - 2))
1964 / (totwincount - 1) > p_wiw)
1965 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001966 /* Can make all windows wider than 'winwidth', spread
1967 * the room equally. */
1968 next_curwin_size = (room + p_wiw
1969 + (totwincount - 1) * p_wmw
1970 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001971 room -= next_curwin_size - p_wiw;
1972 }
1973 else
1974 next_curwin_size = p_wiw;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001977
1978 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 --totwincount; /* don't count curwin */
1980 }
1981
1982 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1983 {
1984 n = m = 0;
1985 wincount = 1;
1986 if (fr->fr_next == NULL)
1987 /* last frame gets all that remains (avoid roundoff error) */
1988 new_size = width;
1989 else if (dir == 'v')
1990 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001991 else if (frame_fixed_width(fr))
1992 {
1993 new_size = fr->fr_newwidth;
1994 wincount = 0; /* doesn't count as a sizeable window */
1995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 else
1997 {
1998 /* Compute the maximum number of windows horiz. in "fr". */
1999 n = frame_minwidth(fr, NOWIN);
2000 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2001 / (p_wmw + 1);
2002 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002003 if (has_next_curwin)
2004 hnc = frame_has_win(fr, next_curwin);
2005 else
2006 hnc = FALSE;
2007 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002009 if (totwincount == 0)
2010 new_size = room;
2011 else
2012 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002014 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 next_curwin_size -= p_wiw - (m - n);
2017 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002018 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002020 else
2021 room -= new_size;
2022 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002025 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * window, unless equalizing all frames. */
2027 if (!current || dir != 'v' || topfr->fr_parent != NULL
2028 || (new_size != fr->fr_width)
2029 || frame_has_win(fr, next_curwin))
2030 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002031 new_size, height);
2032 col += new_size;
2033 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 totwincount -= wincount;
2035 }
2036 }
2037#endif
2038 else /* topfr->fr_layout == FR_COL */
2039 {
2040#ifdef FEAT_VERTSPLIT
2041 topfr->fr_width = width;
2042#endif
2043 topfr->fr_height = height;
2044
2045 if (dir != 'h') /* equalize frame heights */
2046 {
2047 /* Compute maximum number of windows vertically in this frame. */
2048 n = frame_minheight(topfr, NOWIN);
2049 /* add one for the bottom window if it doesn't have a statusline */
2050 if (row + height == cmdline_row && p_ls == 0)
2051 extra_sep = 1;
2052 else
2053 extra_sep = 0;
2054 totwincount = (n + extra_sep) / (p_wmh + 1);
2055 has_next_curwin = frame_has_win(topfr, next_curwin);
2056
2057 /*
2058 * Compute height for "next_curwin" window and room available for
2059 * other windows.
2060 * "m" is the minimal height when counting p_wh for "next_curwin".
2061 */
2062 m = frame_minheight(topfr, next_curwin);
2063 room = height - m;
2064 if (room < 0)
2065 {
2066 /* The room is less then 'winheight', use all space for the
2067 * current window. */
2068 next_curwin_size = p_wh + room;
2069 room = 0;
2070 }
2071 else
2072 {
2073 next_curwin_size = -1;
2074 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2075 {
2076 /* If 'winfixheight' set keep the window height if
2077 * possible.
2078 * Watch out for this window being the next_curwin. */
2079 if (frame_fixed_height(fr))
2080 {
2081 n = frame_minheight(fr, NOWIN);
2082 new_size = fr->fr_height;
2083 if (frame_has_win(fr, next_curwin))
2084 {
2085 room += p_wh - p_wmh;
2086 next_curwin_size = 0;
2087 if (new_size < p_wh)
2088 new_size = p_wh;
2089 }
2090 else
2091 /* These windows don't use up room. */
2092 totwincount -= (n + (fr->fr_next == NULL
2093 ? extra_sep : 0)) / (p_wmh + 1);
2094 room -= new_size - n;
2095 if (room < 0)
2096 {
2097 new_size += room;
2098 room = 0;
2099 }
2100 fr->fr_newheight = new_size;
2101 }
2102 }
2103 if (next_curwin_size == -1)
2104 {
2105 if (!has_next_curwin)
2106 next_curwin_size = 0;
2107 else if (totwincount > 1
2108 && (room + (totwincount - 2))
2109 / (totwincount - 1) > p_wh)
2110 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002111 /* can make all windows higher than 'winheight',
2112 * spread the room equally. */
2113 next_curwin_size = (room + p_wh
2114 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 + (totwincount - 1)) / totwincount;
2116 room -= next_curwin_size - p_wh;
2117 }
2118 else
2119 next_curwin_size = p_wh;
2120 }
2121 }
2122
2123 if (has_next_curwin)
2124 --totwincount; /* don't count curwin */
2125 }
2126
2127 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2128 {
2129 n = m = 0;
2130 wincount = 1;
2131 if (fr->fr_next == NULL)
2132 /* last frame gets all that remains (avoid roundoff error) */
2133 new_size = height;
2134 else if (dir == 'h')
2135 new_size = fr->fr_height;
2136 else if (frame_fixed_height(fr))
2137 {
2138 new_size = fr->fr_newheight;
2139 wincount = 0; /* doesn't count as a sizeable window */
2140 }
2141 else
2142 {
2143 /* Compute the maximum number of windows vert. in "fr". */
2144 n = frame_minheight(fr, NOWIN);
2145 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2146 / (p_wmh + 1);
2147 m = frame_minheight(fr, next_curwin);
2148 if (has_next_curwin)
2149 hnc = frame_has_win(fr, next_curwin);
2150 else
2151 hnc = FALSE;
2152 if (hnc) /* don't count next_curwin */
2153 --wincount;
2154 if (totwincount == 0)
2155 new_size = room;
2156 else
2157 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2158 / totwincount;
2159 if (hnc) /* add next_curwin size */
2160 {
2161 next_curwin_size -= p_wh - (m - n);
2162 new_size += next_curwin_size;
2163 room -= new_size - next_curwin_size;
2164 }
2165 else
2166 room -= new_size;
2167 new_size += n;
2168 }
2169 /* Skip frame that is full width when splitting or closing a
2170 * window, unless equalizing all frames. */
2171 if (!current || dir != 'h' || topfr->fr_parent != NULL
2172 || (new_size != fr->fr_height)
2173 || frame_has_win(fr, next_curwin))
2174 win_equal_rec(next_curwin, current, fr, dir, col, row,
2175 width, new_size);
2176 row += new_size;
2177 height -= new_size;
2178 totwincount -= wincount;
2179 }
2180 }
2181}
2182
2183/*
2184 * close all windows for buffer 'buf'
2185 */
2186 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002187close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002189 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002191 win_T *wp;
2192 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002193 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002196
2197 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002199 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2200#ifdef FEAT_AUTOCMD
2201 && !(wp->w_closing || wp->w_buffer->b_closing)
2202#endif
2203 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002205 win_close(wp, FALSE);
2206
2207 /* Start all over, autocommands may change the window layout. */
2208 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002211 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002213
2214 /* Also check windows in other tab pages. */
2215 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2216 {
2217 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002218 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002219 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002220 if (wp->w_buffer == buf
2221#ifdef FEAT_AUTOCMD
2222 && !(wp->w_closing || wp->w_buffer->b_closing)
2223#endif
2224 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002225 {
2226 win_close_othertab(wp, FALSE, tp);
2227
2228 /* Start all over, the tab page may be closed and
2229 * autocommands may change the window layout. */
2230 nexttp = first_tabpage;
2231 break;
2232 }
2233 }
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002236
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002237 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002238 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002239 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240}
2241
2242/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002243 * Return TRUE if the current window is the only window that exists (ignoring
2244 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002245 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002246 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002247 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002248last_window()
2249{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002250 return (one_window() && first_tabpage->tp_next == NULL);
2251}
2252
2253/*
2254 * Return TRUE if there is only one window other than "aucmd_win" in the
2255 * current tab page.
2256 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002257 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002258one_window()
2259{
2260#ifdef FEAT_AUTOCMD
2261 win_T *wp;
2262 int seen_one = FALSE;
2263
2264 FOR_ALL_WINDOWS(wp)
2265 {
2266 if (wp != aucmd_win)
2267 {
2268 if (seen_one)
2269 return FALSE;
2270 seen_one = TRUE;
2271 }
2272 }
2273 return TRUE;
2274#else
2275 return firstwin == lastwin;
2276#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002277}
2278
2279/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002280 * Close the possibly last window in a tab page.
2281 * Returns TRUE when the window was closed already.
2282 */
2283 static int
2284close_last_window_tabpage(win, free_buf, prev_curtab)
2285 win_T *win;
2286 int free_buf;
2287 tabpage_T *prev_curtab;
2288{
2289 if (firstwin == lastwin)
2290 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002291#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002292 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002293#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002294
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002295 /*
2296 * Closing the last window in a tab page. First go to another tab
2297 * page and then close the window and the tab page. This avoids that
2298 * curwin and curtab are invalid while we are freeing memory, they may
2299 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002300 * Don't trigger autocommands yet, they may use wrong values, so do
2301 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002302 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002303 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002304 redraw_tabline = TRUE;
2305
2306 /* Safety check: Autocommands may have closed the window when jumping
2307 * to the other tab page. */
2308 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2309 {
2310 int h = tabline_height();
2311
2312 win_close_othertab(win, free_buf, prev_curtab);
2313 if (h != tabline_height())
2314 shell_new_rows();
2315 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002316 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2317 * that now. */
2318#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002319 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002320 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2321 if (old_curbuf != curbuf)
2322 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002323#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002324 return TRUE;
2325 }
2326 return FALSE;
2327}
2328
2329/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 * If "free_buf" is TRUE related buffer may be unloaded.
2332 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002333 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002334 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002336 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337win_close(win, free_buf)
2338 win_T *win;
2339 int free_buf;
2340{
2341 win_T *wp;
2342#ifdef FEAT_AUTOCMD
2343 int other_buffer = FALSE;
2344#endif
2345 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 int dir;
2347 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002348 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002350 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002356#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002357 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002358 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002359 if (win == aucmd_win)
2360 {
2361 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002362 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002363 }
2364 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2365 {
2366 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002367 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002368 }
2369#endif
2370
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002371 /* When closing the last window in a tab page first go to another tab page
2372 * and then close the window and the tab page to avoid that curwin and
2373 * curtab are invalid while we are freeing memory. */
2374 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /* When closing the help window, try restoring a snapshot after closing
2378 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002379 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 help_window = TRUE;
2381 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002382 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384#ifdef FEAT_AUTOCMD
2385 if (win == curwin)
2386 {
2387 /*
2388 * Guess which window is going to be the new current window.
2389 * This may change because of the autocommands (sigh).
2390 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002391 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002394 * Be careful: If autocommands delete the window or cause this window
2395 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
2397 if (wp->w_buffer != curbuf)
2398 {
2399 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002400 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002402 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002403 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002404 win->w_closing = FALSE;
2405 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002406 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002408 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002410 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002411 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002412 win->w_closing = FALSE;
2413 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415# ifdef FEAT_EVAL
2416 /* autocmds may abort script processing */
2417 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419# endif
2420 }
2421#endif
2422
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002423#ifdef FEAT_GUI
2424 /* Avoid trouble with scrollbars that are going to be deleted in
2425 * win_free(). */
2426 if (gui.in_use)
2427 out_flush();
2428#endif
2429
Bram Moolenaara971b822011-09-14 14:43:25 +02002430#ifdef FEAT_SYN_HL
2431 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002432 if (win->w_buffer != NULL)
2433 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002434#endif
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 /*
2437 * Close the link to the buffer.
2438 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002439 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002440 {
2441#ifdef FEAT_AUTOCMD
2442 win->w_closing = TRUE;
2443#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002444 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002445#ifdef FEAT_AUTOCMD
2446 if (win_valid(win))
2447 win->w_closing = FALSE;
2448#endif
2449 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002450
Bram Moolenaar802418d2013-01-17 14:00:11 +01002451 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2452 && (last_window() || curtab != prev_curtab
2453 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002454 {
2455 /* Autocommands have close all windows, quit now. Restore
2456 * curwin->w_buffer, otherwise writing viminfo may fail. */
2457 if (curwin->w_buffer == NULL)
2458 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002459 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002460 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002463 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002464 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002465 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002466 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
Bram Moolenaara971b822011-09-14 14:43:25 +02002468 /* Free the memory used for the window and get the window that received
2469 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002470 wp = win_free_mem(win, &dir, NULL);
2471
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* Make sure curwin isn't invalid. It can cause severe trouble when
2473 * printing an error message. For win_equal() curbuf needs to be valid
2474 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002475 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 curwin = wp;
2478#ifdef FEAT_QUICKFIX
2479 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2480 {
2481 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002482 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 * finding another window to go to.
2484 */
2485 for (;;)
2486 {
2487 if (wp->w_next == NULL)
2488 wp = firstwin;
2489 else
2490 wp = wp->w_next;
2491 if (wp == curwin)
2492 break;
2493 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2494 {
2495 curwin = wp;
2496 break;
2497 }
2498 }
2499 }
2500#endif
2501 curbuf = curwin->w_buffer;
2502 close_curwin = TRUE;
2503 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002504 if (p_ea
2505#ifdef FEAT_VERTSPLIT
2506 && (*p_ead == 'b' || *p_ead == dir)
2507#endif
2508 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 win_equal(curwin, TRUE,
2510#ifdef FEAT_VERTSPLIT
2511 dir
2512#else
2513 0
2514#endif
2515 );
2516 else
2517 win_comp_pos();
2518 if (close_curwin)
2519 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002520 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521#ifdef FEAT_AUTOCMD
2522 if (other_buffer)
2523 /* careful: after this wp and win may be invalid! */
2524 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2525#endif
2526 }
2527
2528 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002529 * If last window has a status line now and we don't want one,
2530 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
2532 last_status(FALSE);
2533
2534 /* After closing the help window, try restoring the window layout from
2535 * before it was opened. */
2536 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002537 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2540 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2541 if (gui.in_use && !win_hasvertsplit())
2542 gui_init_which_components(NULL);
2543#endif
2544
2545 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002546 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547}
2548
2549/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002550 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002551 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002552 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002553 * Caller must check if buffer is hidden and whether the tabline needs to be
2554 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002555 */
2556 void
2557win_close_othertab(win, free_buf, tp)
2558 win_T *win;
2559 int free_buf;
2560 tabpage_T *tp;
2561{
2562 win_T *wp;
2563 int dir;
2564 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002565 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566
Bram Moolenaar362ce482012-06-06 19:02:45 +02002567#ifdef FEAT_AUTOCMD
2568 if (win->w_closing || win->w_buffer->b_closing)
2569 return; /* window is already being closed */
2570#endif
2571
Bram Moolenaarf740b292006-02-16 22:11:02 +00002572 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002573 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574
2575 /* Careful: Autocommands may have closed the tab page or made it the
2576 * current tab page. */
2577 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2578 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002579 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002580 return;
2581
2582 /* Autocommands may have closed the window already. */
2583 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2584 ;
2585 if (wp == NULL)
2586 return;
2587
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002589 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 {
2591 if (tp == first_tabpage)
2592 first_tabpage = tp->tp_next;
2593 else
2594 {
2595 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2596 ptp = ptp->tp_next)
2597 ;
2598 if (ptp == NULL)
2599 {
2600 EMSG2(_(e_intern2), "win_close_othertab()");
2601 return;
2602 }
2603 ptp->tp_next = tp->tp_next;
2604 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002605 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002606 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002607
2608 /* Free the memory used for the window. */
2609 win_free_mem(win, &dir, tp);
2610
2611 if (free_tp)
2612 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002613}
2614
2615/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002616 * Free the memory used for a window.
2617 * Returns a pointer to the window that got the freed up space.
2618 */
2619 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002620win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002621 win_T *win;
2622 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002624{
2625 frame_T *frp;
2626 win_T *wp;
2627
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002628 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002629 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002630 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002631 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002632 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002633
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002634 /* When deleting the current window of another tab page select a new
2635 * current window. */
2636 if (tp != NULL && win == tp->tp_curwin)
2637 tp->tp_curwin = wp;
2638
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002639 return wp;
2640}
2641
2642#if defined(EXITFREE) || defined(PROTO)
2643 void
2644win_free_all()
2645{
2646 int dummy;
2647
Bram Moolenaarf740b292006-02-16 22:11:02 +00002648# ifdef FEAT_WINDOWS
2649 while (first_tabpage->tp_next != NULL)
2650 tabpage_close(TRUE);
2651# endif
2652
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002653# ifdef FEAT_AUTOCMD
2654 if (aucmd_win != NULL)
2655 {
2656 (void)win_free_mem(aucmd_win, &dummy, NULL);
2657 aucmd_win = NULL;
2658 }
2659# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002660
2661 while (firstwin != NULL)
2662 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002663
2664 /* No window should be used after this. Set curwin to NULL to crash
2665 * instead of using freed memory. */
2666 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002667}
2668#endif
2669
2670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * Remove a window and its frame from the tree of frames.
2672 * Returns a pointer to the window that got the freed up space.
2673 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002674 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002675winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002677 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 frame_T *frp, *frp2, *frp3;
2681 frame_T *frp_close = win->w_frame;
2682 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002685 * If there is only one window there is nothing to remove.
2686 */
2687 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2688 return NULL;
2689
2690 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 * Remove the window from its frame.
2692 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002693 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 wp = frame2win(frp2);
2695
2696 /* Remove this frame from the list of frames. */
2697 frame_remove(frp_close);
2698
2699#ifdef FEAT_VERTSPLIT
2700 if (frp_close->fr_parent->fr_layout == FR_COL)
2701 {
2702#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002703 /* When 'winfixheight' is set, try to find another frame in the column
2704 * (as close to the closed frame as possible) to distribute the height
2705 * to. */
2706 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2707 {
2708 frp = frp_close->fr_prev;
2709 frp3 = frp_close->fr_next;
2710 while (frp != NULL || frp3 != NULL)
2711 {
2712 if (frp != NULL)
2713 {
2714 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2715 {
2716 frp2 = frp;
2717 wp = frp->fr_win;
2718 break;
2719 }
2720 frp = frp->fr_prev;
2721 }
2722 if (frp3 != NULL)
2723 {
2724 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2725 {
2726 frp2 = frp3;
2727 wp = frp3->fr_win;
2728 break;
2729 }
2730 frp3 = frp3->fr_next;
2731 }
2732 }
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2735 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#ifdef FEAT_VERTSPLIT
2737 *dirp = 'v';
2738 }
2739 else
2740 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002741 /* When 'winfixwidth' is set, try to find another frame in the column
2742 * (as close to the closed frame as possible) to distribute the width
2743 * to. */
2744 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2745 {
2746 frp = frp_close->fr_prev;
2747 frp3 = frp_close->fr_next;
2748 while (frp != NULL || frp3 != NULL)
2749 {
2750 if (frp != NULL)
2751 {
2752 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2753 {
2754 frp2 = frp;
2755 wp = frp->fr_win;
2756 break;
2757 }
2758 frp = frp->fr_prev;
2759 }
2760 if (frp3 != NULL)
2761 {
2762 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2763 {
2764 frp2 = frp3;
2765 wp = frp3->fr_win;
2766 break;
2767 }
2768 frp3 = frp3->fr_next;
2769 }
2770 }
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002773 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 *dirp = 'h';
2775 }
2776#endif
2777
2778 /* If rows/columns go to a window below/right its positions need to be
2779 * updated. Can only be done after the sizes have been updated. */
2780 if (frp2 == frp_close->fr_next)
2781 {
2782 int row = win->w_winrow;
2783 int col = W_WINCOL(win);
2784
2785 frame_comp_pos(frp2, &row, &col);
2786 }
2787
2788 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2789 {
2790 /* There is no other frame in this list, move its info to the parent
2791 * and remove it. */
2792 frp2->fr_parent->fr_layout = frp2->fr_layout;
2793 frp2->fr_parent->fr_child = frp2->fr_child;
2794 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2795 frp->fr_parent = frp2->fr_parent;
2796 frp2->fr_parent->fr_win = frp2->fr_win;
2797 if (frp2->fr_win != NULL)
2798 frp2->fr_win->w_frame = frp2->fr_parent;
2799 frp = frp2->fr_parent;
2800 vim_free(frp2);
2801
2802 frp2 = frp->fr_parent;
2803 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2804 {
2805 /* The frame above the parent has the same layout, have to merge
2806 * the frames into this list. */
2807 if (frp2->fr_child == frp)
2808 frp2->fr_child = frp->fr_child;
2809 frp->fr_child->fr_prev = frp->fr_prev;
2810 if (frp->fr_prev != NULL)
2811 frp->fr_prev->fr_next = frp->fr_child;
2812 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2813 {
2814 frp3->fr_parent = frp2;
2815 if (frp3->fr_next == NULL)
2816 {
2817 frp3->fr_next = frp->fr_next;
2818 if (frp->fr_next != NULL)
2819 frp->fr_next->fr_prev = frp3;
2820 break;
2821 }
2822 }
2823 vim_free(frp);
2824 }
2825 }
2826
2827 return wp;
2828}
2829
2830/*
2831 * Find out which frame is going to get the freed up space when "win" is
2832 * closed.
2833 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2834 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2835 * This makes opening a window and closing it immediately keep the same window
2836 * layout.
2837 */
2838 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002839win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002841 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 frame_T *frp;
2844 int b;
2845
Bram Moolenaarf740b292006-02-16 22:11:02 +00002846 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002847 /* Last window in this tab page, will go to next tab page. */
2848 return alt_tabpage()->tp_curwin->w_frame;
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 frp = win->w_frame;
2851#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002852 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 b = p_spr;
2854 else
2855#endif
2856 b = p_sb;
2857 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2858 return frp->fr_next;
2859 return frp->fr_prev;
2860}
2861
2862/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002863 * Return the tabpage that will be used if the current one is closed.
2864 */
2865 static tabpage_T *
2866alt_tabpage()
2867{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002868 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002869
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002870 /* Use the next tab page if possible. */
2871 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002872 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002873
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002874 /* Find the last but one tab page. */
2875 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2876 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002877 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002878}
2879
2880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 * Find the left-upper window in frame "frp".
2882 */
2883 static win_T *
2884frame2win(frp)
2885 frame_T *frp;
2886{
2887 while (frp->fr_win == NULL)
2888 frp = frp->fr_child;
2889 return frp->fr_win;
2890}
2891
2892/*
2893 * Return TRUE if frame "frp" contains window "wp".
2894 */
2895 static int
2896frame_has_win(frp, wp)
2897 frame_T *frp;
2898 win_T *wp;
2899{
2900 frame_T *p;
2901
2902 if (frp->fr_layout == FR_LEAF)
2903 return frp->fr_win == wp;
2904
2905 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2906 if (frame_has_win(p, wp))
2907 return TRUE;
2908 return FALSE;
2909}
2910
2911/*
2912 * Set a new height for a frame. Recursively sets the height for contained
2913 * frames and windows. Caller must take care of positions.
2914 */
2915 static void
2916frame_new_height(topfrp, height, topfirst, wfh)
2917 frame_T *topfrp;
2918 int height;
2919 int topfirst; /* resize topmost contained frame first */
2920 int wfh; /* obey 'winfixheight' when there is a choice;
2921 may cause the height not to be set */
2922{
2923 frame_T *frp;
2924 int extra_lines;
2925 int h;
2926
2927 if (topfrp->fr_win != NULL)
2928 {
2929 /* Simple case: just one window. */
2930 win_new_height(topfrp->fr_win,
2931 height - topfrp->fr_win->w_status_height);
2932 }
2933#ifdef FEAT_VERTSPLIT
2934 else if (topfrp->fr_layout == FR_ROW)
2935 {
2936 do
2937 {
2938 /* All frames in this row get the same new height. */
2939 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2940 {
2941 frame_new_height(frp, height, topfirst, wfh);
2942 if (frp->fr_height > height)
2943 {
2944 /* Could not fit the windows, make the whole row higher. */
2945 height = frp->fr_height;
2946 break;
2947 }
2948 }
2949 }
2950 while (frp != NULL);
2951 }
2952#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002953 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 /* Complicated case: Resize a column of frames. Resize the bottom
2956 * frame first, frames above that when needed. */
2957
2958 frp = topfrp->fr_child;
2959 if (wfh)
2960 /* Advance past frames with one window with 'wfh' set. */
2961 while (frame_fixed_height(frp))
2962 {
2963 frp = frp->fr_next;
2964 if (frp == NULL)
2965 return; /* no frame without 'wfh', give up */
2966 }
2967 if (!topfirst)
2968 {
2969 /* Find the bottom frame of this column */
2970 while (frp->fr_next != NULL)
2971 frp = frp->fr_next;
2972 if (wfh)
2973 /* Advance back for frames with one window with 'wfh' set. */
2974 while (frame_fixed_height(frp))
2975 frp = frp->fr_prev;
2976 }
2977
2978 extra_lines = height - topfrp->fr_height;
2979 if (extra_lines < 0)
2980 {
2981 /* reduce height of contained frames, bottom or top frame first */
2982 while (frp != NULL)
2983 {
2984 h = frame_minheight(frp, NULL);
2985 if (frp->fr_height + extra_lines < h)
2986 {
2987 extra_lines += frp->fr_height - h;
2988 frame_new_height(frp, h, topfirst, wfh);
2989 }
2990 else
2991 {
2992 frame_new_height(frp, frp->fr_height + extra_lines,
2993 topfirst, wfh);
2994 break;
2995 }
2996 if (topfirst)
2997 {
2998 do
2999 frp = frp->fr_next;
3000 while (wfh && frp != NULL && frame_fixed_height(frp));
3001 }
3002 else
3003 {
3004 do
3005 frp = frp->fr_prev;
3006 while (wfh && frp != NULL && frame_fixed_height(frp));
3007 }
3008 /* Increase "height" if we could not reduce enough frames. */
3009 if (frp == NULL)
3010 height -= extra_lines;
3011 }
3012 }
3013 else if (extra_lines > 0)
3014 {
3015 /* increase height of bottom or top frame */
3016 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3017 }
3018 }
3019 topfrp->fr_height = height;
3020}
3021
3022/*
3023 * Return TRUE if height of frame "frp" should not be changed because of
3024 * the 'winfixheight' option.
3025 */
3026 static int
3027frame_fixed_height(frp)
3028 frame_T *frp;
3029{
3030 /* frame with one window: fixed height if 'winfixheight' set. */
3031 if (frp->fr_win != NULL)
3032 return frp->fr_win->w_p_wfh;
3033
3034 if (frp->fr_layout == FR_ROW)
3035 {
3036 /* The frame is fixed height if one of the frames in the row is fixed
3037 * height. */
3038 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3039 if (frame_fixed_height(frp))
3040 return TRUE;
3041 return FALSE;
3042 }
3043
3044 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3045 * frames in the row are fixed height. */
3046 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3047 if (!frame_fixed_height(frp))
3048 return FALSE;
3049 return TRUE;
3050}
3051
3052#ifdef FEAT_VERTSPLIT
3053/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003054 * Return TRUE if width of frame "frp" should not be changed because of
3055 * the 'winfixwidth' option.
3056 */
3057 static int
3058frame_fixed_width(frp)
3059 frame_T *frp;
3060{
3061 /* frame with one window: fixed width if 'winfixwidth' set. */
3062 if (frp->fr_win != NULL)
3063 return frp->fr_win->w_p_wfw;
3064
3065 if (frp->fr_layout == FR_COL)
3066 {
3067 /* The frame is fixed width if one of the frames in the row is fixed
3068 * width. */
3069 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3070 if (frame_fixed_width(frp))
3071 return TRUE;
3072 return FALSE;
3073 }
3074
3075 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3076 * frames in the row are fixed width. */
3077 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3078 if (!frame_fixed_width(frp))
3079 return FALSE;
3080 return TRUE;
3081}
3082
3083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 * Add a status line to windows at the bottom of "frp".
3085 * Note: Does not check if there is room!
3086 */
3087 static void
3088frame_add_statusline(frp)
3089 frame_T *frp;
3090{
3091 win_T *wp;
3092
3093 if (frp->fr_layout == FR_LEAF)
3094 {
3095 wp = frp->fr_win;
3096 if (wp->w_status_height == 0)
3097 {
3098 if (wp->w_height > 0) /* don't make it negative */
3099 --wp->w_height;
3100 wp->w_status_height = STATUS_HEIGHT;
3101 }
3102 }
3103 else if (frp->fr_layout == FR_ROW)
3104 {
3105 /* Handle all the frames in the row. */
3106 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3107 frame_add_statusline(frp);
3108 }
3109 else /* frp->fr_layout == FR_COL */
3110 {
3111 /* Only need to handle the last frame in the column. */
3112 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3113 ;
3114 frame_add_statusline(frp);
3115 }
3116}
3117
3118/*
3119 * Set width of a frame. Handles recursively going through contained frames.
3120 * May remove separator line for windows at the right side (for win_close()).
3121 */
3122 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003123frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 frame_T *topfrp;
3125 int width;
3126 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003127 int wfw; /* obey 'winfixwidth' when there is a choice;
3128 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 frame_T *frp;
3131 int extra_cols;
3132 int w;
3133 win_T *wp;
3134
3135 if (topfrp->fr_layout == FR_LEAF)
3136 {
3137 /* Simple case: just one window. */
3138 wp = topfrp->fr_win;
3139 /* Find out if there are any windows right of this one. */
3140 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3141 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3142 break;
3143 if (frp->fr_parent == NULL)
3144 wp->w_vsep_width = 0;
3145 win_new_width(wp, width - wp->w_vsep_width);
3146 }
3147 else if (topfrp->fr_layout == FR_COL)
3148 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003149 do
3150 {
3151 /* All frames in this column get the same new width. */
3152 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3153 {
3154 frame_new_width(frp, width, leftfirst, wfw);
3155 if (frp->fr_width > width)
3156 {
3157 /* Could not fit the windows, make whole column wider. */
3158 width = frp->fr_width;
3159 break;
3160 }
3161 }
3162 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else /* fr_layout == FR_ROW */
3165 {
3166 /* Complicated case: Resize a row of frames. Resize the rightmost
3167 * frame first, frames left of it when needed. */
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003170 if (wfw)
3171 /* Advance past frames with one window with 'wfw' set. */
3172 while (frame_fixed_width(frp))
3173 {
3174 frp = frp->fr_next;
3175 if (frp == NULL)
3176 return; /* no frame without 'wfw', give up */
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003179 {
3180 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 while (frp->fr_next != NULL)
3182 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003183 if (wfw)
3184 /* Advance back for frames with one window with 'wfw' set. */
3185 while (frame_fixed_width(frp))
3186 frp = frp->fr_prev;
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 extra_cols = width - topfrp->fr_width;
3190 if (extra_cols < 0)
3191 {
3192 /* reduce frame width, rightmost frame first */
3193 while (frp != NULL)
3194 {
3195 w = frame_minwidth(frp, NULL);
3196 if (frp->fr_width + extra_cols < w)
3197 {
3198 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003199 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 else
3202 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003203 frame_new_width(frp, frp->fr_width + extra_cols,
3204 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 break;
3206 }
3207 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003208 {
3209 do
3210 frp = frp->fr_next;
3211 while (wfw && frp != NULL && frame_fixed_width(frp));
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003214 {
3215 do
3216 frp = frp->fr_prev;
3217 while (wfw && frp != NULL && frame_fixed_width(frp));
3218 }
3219 /* Increase "width" if we could not reduce enough frames. */
3220 if (frp == NULL)
3221 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 }
3224 else if (extra_cols > 0)
3225 {
3226 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003227 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229 }
3230 topfrp->fr_width = width;
3231}
3232
3233/*
3234 * Add the vertical separator to windows at the right side of "frp".
3235 * Note: Does not check if there is room!
3236 */
3237 static void
3238frame_add_vsep(frp)
3239 frame_T *frp;
3240{
3241 win_T *wp;
3242
3243 if (frp->fr_layout == FR_LEAF)
3244 {
3245 wp = frp->fr_win;
3246 if (wp->w_vsep_width == 0)
3247 {
3248 if (wp->w_width > 0) /* don't make it negative */
3249 --wp->w_width;
3250 wp->w_vsep_width = 1;
3251 }
3252 }
3253 else if (frp->fr_layout == FR_COL)
3254 {
3255 /* Handle all the frames in the column. */
3256 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3257 frame_add_vsep(frp);
3258 }
3259 else /* frp->fr_layout == FR_ROW */
3260 {
3261 /* Only need to handle the last frame in the row. */
3262 frp = frp->fr_child;
3263 while (frp->fr_next != NULL)
3264 frp = frp->fr_next;
3265 frame_add_vsep(frp);
3266 }
3267}
3268
3269/*
3270 * Set frame width from the window it contains.
3271 */
3272 static void
3273frame_fix_width(wp)
3274 win_T *wp;
3275{
3276 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3277}
3278#endif
3279
3280/*
3281 * Set frame height from the window it contains.
3282 */
3283 static void
3284frame_fix_height(wp)
3285 win_T *wp;
3286{
3287 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3288}
3289
3290/*
3291 * Compute the minimal height for frame "topfrp".
3292 * Uses the 'winminheight' option.
3293 * When "next_curwin" isn't NULL, use p_wh for this window.
3294 * When "next_curwin" is NOWIN, don't use at least one line for the current
3295 * window.
3296 */
3297 static int
3298frame_minheight(topfrp, next_curwin)
3299 frame_T *topfrp;
3300 win_T *next_curwin;
3301{
3302 frame_T *frp;
3303 int m;
3304#ifdef FEAT_VERTSPLIT
3305 int n;
3306#endif
3307
3308 if (topfrp->fr_win != NULL)
3309 {
3310 if (topfrp->fr_win == next_curwin)
3311 m = p_wh + topfrp->fr_win->w_status_height;
3312 else
3313 {
3314 /* window: minimal height of the window plus status line */
3315 m = p_wmh + topfrp->fr_win->w_status_height;
3316 /* Current window is minimal one line high */
3317 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3318 ++m;
3319 }
3320 }
3321#ifdef FEAT_VERTSPLIT
3322 else if (topfrp->fr_layout == FR_ROW)
3323 {
3324 /* get the minimal height from each frame in this row */
3325 m = 0;
3326 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3327 {
3328 n = frame_minheight(frp, next_curwin);
3329 if (n > m)
3330 m = n;
3331 }
3332 }
3333#endif
3334 else
3335 {
3336 /* Add up the minimal heights for all frames in this column. */
3337 m = 0;
3338 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3339 m += frame_minheight(frp, next_curwin);
3340 }
3341
3342 return m;
3343}
3344
3345#ifdef FEAT_VERTSPLIT
3346/*
3347 * Compute the minimal width for frame "topfrp".
3348 * When "next_curwin" isn't NULL, use p_wiw for this window.
3349 * When "next_curwin" is NOWIN, don't use at least one column for the current
3350 * window.
3351 */
3352 static int
3353frame_minwidth(topfrp, next_curwin)
3354 frame_T *topfrp;
3355 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3356{
3357 frame_T *frp;
3358 int m, n;
3359
3360 if (topfrp->fr_win != NULL)
3361 {
3362 if (topfrp->fr_win == next_curwin)
3363 m = p_wiw + topfrp->fr_win->w_vsep_width;
3364 else
3365 {
3366 /* window: minimal width of the window plus separator column */
3367 m = p_wmw + topfrp->fr_win->w_vsep_width;
3368 /* Current window is minimal one column wide */
3369 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3370 ++m;
3371 }
3372 }
3373 else if (topfrp->fr_layout == FR_COL)
3374 {
3375 /* get the minimal width from each frame in this column */
3376 m = 0;
3377 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3378 {
3379 n = frame_minwidth(frp, next_curwin);
3380 if (n > m)
3381 m = n;
3382 }
3383 }
3384 else
3385 {
3386 /* Add up the minimal widths for all frames in this row. */
3387 m = 0;
3388 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3389 m += frame_minwidth(frp, next_curwin);
3390 }
3391
3392 return m;
3393}
3394#endif
3395
3396
3397/*
3398 * Try to close all windows except current one.
3399 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3400 * used and the buffer was modified.
3401 *
3402 * Used by ":bdel" and ":only".
3403 */
3404 void
3405close_others(message, forceit)
3406 int message;
3407 int forceit; /* always hide all other windows */
3408{
3409 win_T *wp;
3410 win_T *nextwp;
3411 int r;
3412
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003413 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
3415 if (message
3416#ifdef FEAT_AUTOCMD
3417 && !autocmd_busy
3418#endif
3419 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003420 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 return;
3422 }
3423
3424 /* Be very careful here: autocommands may change the window layout. */
3425 for (wp = firstwin; win_valid(wp); wp = nextwp)
3426 {
3427 nextwp = wp->w_next;
3428 if (wp != curwin) /* don't close current window */
3429 {
3430
3431 /* Check if it's allowed to abandon this window */
3432 r = can_abandon(wp->w_buffer, forceit);
3433#ifdef FEAT_AUTOCMD
3434 if (!win_valid(wp)) /* autocommands messed wp up */
3435 {
3436 nextwp = firstwin;
3437 continue;
3438 }
3439#endif
3440 if (!r)
3441 {
3442#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3443 if (message && (p_confirm || cmdmod.confirm) && p_write)
3444 {
3445 dialog_changed(wp->w_buffer, FALSE);
3446# ifdef FEAT_AUTOCMD
3447 if (!win_valid(wp)) /* autocommands messed wp up */
3448 {
3449 nextwp = firstwin;
3450 continue;
3451 }
3452# endif
3453 }
3454 if (bufIsChanged(wp->w_buffer))
3455#endif
3456 continue;
3457 }
3458 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3459 }
3460 }
3461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 EMSG(_("E445: Other window contains changes"));
3464}
3465
3466#endif /* FEAT_WINDOWS */
3467
3468/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003469 * Init the current window "curwin".
3470 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 */
3472 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003473curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003475 win_init_empty(curwin);
3476}
3477
3478 void
3479win_init_empty(wp)
3480 win_T *wp;
3481{
3482 redraw_win_later(wp, NOT_VALID);
3483 wp->w_lines_valid = 0;
3484 wp->w_cursor.lnum = 1;
3485 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003487 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003489 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3490 wp->w_pcmark.col = 0;
3491 wp->w_prev_pcmark.lnum = 0;
3492 wp->w_prev_pcmark.col = 0;
3493 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003495 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003497 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003499 if (wp->w_p_rl)
3500 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003502 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003504#ifdef FEAT_SYN_HL
3505 wp->w_s = &wp->w_buffer->b_s;
3506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507}
3508
3509/*
3510 * Allocate the first window and put an empty buffer in it.
3511 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003512 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003514 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515win_alloc_first()
3516{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003517 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003518 return FAIL;
3519
3520#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003521 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522 if (first_tabpage == NULL)
3523 return FAIL;
3524 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003526#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003527
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003528 return OK;
3529}
3530
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003531#if defined(FEAT_AUTOCMD) || defined(PROTO)
3532/*
3533 * Init "aucmd_win". This can only be done after the first
3534 * window is fully initialized, thus it can't be in win_alloc_first().
3535 */
3536 void
3537win_alloc_aucmd_win()
3538{
3539 aucmd_win = win_alloc(NULL, TRUE);
3540 if (aucmd_win != NULL)
3541 {
3542 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003543 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003544 new_frame(aucmd_win);
3545 }
3546}
3547#endif
3548
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003550 * Allocate the first window or the first window in a new tab page.
3551 * When "oldwin" is NULL create an empty buffer for it.
3552 * When "oldwin" is not NULL copy info from it to the new window (only with
3553 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003554 * Return FAIL when something goes wrong (out of memory).
3555 */
3556 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003557win_alloc_firstwin(oldwin)
3558 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003560 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003561 if (oldwin == NULL)
3562 {
3563 /* Very first window, need to create an empty buffer for it and
3564 * initialize from scratch. */
3565 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3566 if (curwin == NULL || curbuf == NULL)
3567 return FAIL;
3568 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003569#ifdef FEAT_SYN_HL
3570 curwin->w_s = &(curbuf->b_s);
3571#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003572 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003574 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003576 curwin_init(); /* init current window */
3577 }
3578#ifdef FEAT_WINDOWS
3579 else
3580 {
3581 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003582 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003583
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003584 /* We don't want cursor- and scroll-binding in the first window. */
3585 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003586 }
3587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003589 new_frame(curwin);
3590 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003591 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003592 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593#ifdef FEAT_VERTSPLIT
3594 topframe->fr_width = Columns;
3595#endif
3596 topframe->fr_height = Rows - p_ch;
3597 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598
3599 return OK;
3600}
3601
3602/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003603 * Create a frame for window "wp".
3604 */
3605 static void
3606new_frame(win_T *wp)
3607{
3608 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3609
3610 wp->w_frame = frp;
3611 if (frp != NULL)
3612 {
3613 frp->fr_layout = FR_LEAF;
3614 frp->fr_win = wp;
3615 }
3616}
3617
3618/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619 * Initialize the window and frame size to the maximum.
3620 */
3621 void
3622win_init_size()
3623{
3624 firstwin->w_height = ROWS_AVAIL;
3625 topframe->fr_height = ROWS_AVAIL;
3626#ifdef FEAT_VERTSPLIT
3627 firstwin->w_width = Columns;
3628 topframe->fr_width = Columns;
3629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630}
3631
3632#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003633
3634/*
3635 * Allocate a new tabpage_T and init the values.
3636 * Returns NULL when out of memory.
3637 */
3638 static tabpage_T *
3639alloc_tabpage()
3640{
3641 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003642# ifdef FEAT_GUI
3643 int i;
3644# endif
3645
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003646
3647 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003648 if (tp == NULL)
3649 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003650
Bram Moolenaar429fa852013-04-15 12:27:36 +02003651# ifdef FEAT_EVAL
3652 /* init t: variables */
3653 tp->tp_vars = dict_alloc();
3654 if (tp->tp_vars == NULL)
3655 {
3656 vim_free(tp);
3657 return NULL;
3658 }
3659 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3660# endif
3661
3662# ifdef FEAT_GUI
3663 for (i = 0; i < 3; i++)
3664 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003665# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003666# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003667 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003668# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003669 tp->tp_ch_used = p_ch;
3670
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003671 return tp;
3672}
3673
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003674 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003675free_tabpage(tp)
3676 tabpage_T *tp;
3677{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003678 int idx;
3679
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680# ifdef FEAT_DIFF
3681 diff_clear(tp);
3682# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003683 for (idx = 0; idx < SNAP_COUNT; ++idx)
3684 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003685#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003686 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3687 hash_init(&tp->tp_vars->dv_hashtab);
3688 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003689#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003690
3691#ifdef FEAT_PYTHON
3692 python_tabpage_free(tp);
3693#endif
3694
3695#ifdef FEAT_PYTHON3
3696 python3_tabpage_free(tp);
3697#endif
3698
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003699 vim_free(tp);
3700}
3701
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003703 * Create a new Tab page with one window.
3704 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003705 * When "after" is 0 put it just after the current Tab page.
3706 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003707 * Return FAIL or OK.
3708 */
3709 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003710win_new_tabpage(after)
3711 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003713 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003715 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003716
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003717 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003718 if (newtp == NULL)
3719 return FAIL;
3720
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003721 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003722 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003723 {
3724 vim_free(newtp);
3725 return FAIL;
3726 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003727 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003728
3729 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003730 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003731 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003732 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003733 if (after == 1)
3734 {
3735 /* New tab page becomes the first one. */
3736 newtp->tp_next = first_tabpage;
3737 first_tabpage = newtp;
3738 }
3739 else
3740 {
3741 if (after > 0)
3742 {
3743 /* Put new tab page before tab page "after". */
3744 n = 2;
3745 for (tp = first_tabpage; tp->tp_next != NULL
3746 && n < after; tp = tp->tp_next)
3747 ++n;
3748 }
3749 newtp->tp_next = tp->tp_next;
3750 tp->tp_next = newtp;
3751 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003752 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003754 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755
3756 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003757 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003758
3759#if defined(FEAT_GUI)
3760 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3761 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003762 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003763#endif
3764
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003765 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003766#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003767 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003769#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770 return OK;
3771 }
3772
3773 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003774 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775 return FAIL;
3776}
3777
3778/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003779 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3780 * like with ":split".
3781 * Returns OK if a new tab page was created, FAIL otherwise.
3782 */
3783 int
3784may_open_tabpage()
3785{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003786 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003787
Bram Moolenaard326ce82007-03-11 14:48:29 +00003788 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003789 {
3790 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003791 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003792 return win_new_tabpage(n);
3793 }
3794 return FAIL;
3795}
3796
3797/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003798 * Create up to "maxcount" tabpages with empty windows.
3799 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003800 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003801 int
3802make_tabpages(maxcount)
3803 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003805 int count = maxcount;
3806 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003808 /* Limit to 'tabpagemax' tabs. */
3809 if (count > p_tpm)
3810 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003811
3812#ifdef FEAT_AUTOCMD
3813 /*
3814 * Don't execute autocommands while creating the tab pages. Must do that
3815 * when putting the buffers in the windows.
3816 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003817 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003818#endif
3819
3820 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003821 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003822 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003823
3824#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003825 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003826#endif
3827
3828 /* return actual number of tab pages */
3829 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003830}
3831
3832/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003833 * Return TRUE when "tpc" points to a valid tab page.
3834 */
3835 int
3836valid_tabpage(tpc)
3837 tabpage_T *tpc;
3838{
3839 tabpage_T *tp;
3840
3841 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3842 if (tp == tpc)
3843 return TRUE;
3844 return FALSE;
3845}
3846
3847/*
3848 * Find tab page "n" (first one is 1). Returns NULL when not found.
3849 */
3850 tabpage_T *
3851find_tabpage(n)
3852 int n;
3853{
3854 tabpage_T *tp;
3855 int i = 1;
3856
3857 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3858 ++i;
3859 return tp;
3860}
3861
3862/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003863 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003864 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003865 */
3866 int
3867tabpage_index(ftp)
3868 tabpage_T *ftp;
3869{
3870 int i = 1;
3871 tabpage_T *tp;
3872
3873 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3874 ++i;
3875 return i;
3876}
3877
3878/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003879 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003880 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003881 * FAIL.
3882 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003884 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003885leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003886 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003887 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003888 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003889{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003890 tabpage_T *tp = curtab;
3891
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003893#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003894 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003895 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003896 if (new_curbuf != curbuf)
3897 {
3898 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3899 if (curtab != tp)
3900 return FAIL;
3901 }
3902 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3903 if (curtab != tp)
3904 return FAIL;
3905 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003906 if (curtab != tp)
3907 return FAIL;
3908 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003909#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003910#if defined(FEAT_GUI)
3911 /* Remove the scrollbars. They may be added back later. */
3912 if (gui.in_use)
3913 gui_remove_scrollbars();
3914#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003915 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003917 tp->tp_firstwin = firstwin;
3918 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003919 tp->tp_old_Rows = Rows;
3920 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921 firstwin = NULL;
3922 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003923 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003924}
3925
3926/*
3927 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003928 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003929 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3930 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003931 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003933enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003934 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003935 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003936 int trigger_enter_autocmds UNUSED;
3937 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003938{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003939 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003940 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003941
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003942 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003943 firstwin = tp->tp_firstwin;
3944 lastwin = tp->tp_lastwin;
3945 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003946
3947 /* We would like doing the TabEnter event first, but we don't have a
3948 * valid current window yet, which may break some commands.
3949 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003950 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3951 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003952 prevwin = next_prevwin;
3953
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003954 last_status(FALSE); /* status line may appear or disappear */
3955 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003956 must_redraw = CLEAR; /* need to redraw everything */
3957#ifdef FEAT_DIFF
3958 diff_need_scrollbind = TRUE;
3959#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003960
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003961 /* The tabpage line may have appeared or disappeared, may need to resize
3962 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003963 * frame sizes too. Use the stored value of p_ch, so that it can be
3964 * different for each tab page. */
3965 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003966 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3967#ifdef FEAT_GUI_TABLINE
3968 && !gui_use_tabline()
3969#endif
3970 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003971 shell_new_rows();
3972#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003973 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003974 shell_new_columns(); /* update window widths */
3975#endif
3976
3977#if defined(FEAT_GUI)
3978 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3979 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003980 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003981#endif
3982
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003983#ifdef FEAT_AUTOCMD
3984 /* Apply autocommands after updating the display, when 'rows' and
3985 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003986 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003987 {
3988 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3989 if (old_curbuf != curbuf)
3990 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3991 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003992#endif
3993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003994 redraw_all_later(CLEAR);
3995}
3996
3997/*
3998 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003999 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004000 */
4001 void
4002goto_tabpage(n)
4003 int n;
4004{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004005 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004006 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004007 int i;
4008
Bram Moolenaard68071d2006-05-02 22:08:30 +00004009 if (text_locked())
4010 {
4011 /* Not allowed when editing the command line. */
4012#ifdef FEAT_CMDWIN
4013 if (cmdwin_type != 0)
4014 EMSG(_(e_cmdwin));
4015 else
4016#endif
4017 EMSG(_(e_secure));
4018 return;
4019 }
4020
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004021 /* If there is only one it can't work. */
4022 if (first_tabpage->tp_next == NULL)
4023 {
4024 if (n > 1)
4025 beep_flush();
4026 return;
4027 }
4028
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004029 if (n == 0)
4030 {
4031 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004032 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004033 tp = first_tabpage;
4034 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004036 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004037 else if (n < 0)
4038 {
4039 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4040 * this N times. */
4041 ttp = curtab;
4042 for (i = n; i < 0; ++i)
4043 {
4044 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4045 tp = tp->tp_next)
4046 ;
4047 ttp = tp;
4048 }
4049 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004050 else if (n == 9999)
4051 {
4052 /* Go to last tab page. */
4053 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4054 ;
4055 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004056 else
4057 {
4058 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004059 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004060 if (tp == NULL)
4061 {
4062 beep_flush();
4063 return;
4064 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004065 }
4066
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004067 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004068
4069#ifdef FEAT_GUI_TABLINE
4070 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004071 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004072#endif
4073}
4074
4075/*
4076 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004077 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4078 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004079 * Note: doesn't update the GUI tab.
4080 */
4081 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004082goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004083 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004084 int trigger_enter_autocmds;
4085 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004086{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004087 /* Don't repeat a message in another tab page. */
4088 set_keep_msg(NULL, 0);
4089
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004090 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4091 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004092 {
4093 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004094 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4095 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004096 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004097 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4098 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004099 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
4102/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004103 * Enter window "wp" in tab page "tp".
4104 * Also updates the GUI tab.
4105 */
4106 void
4107goto_tabpage_win(tp, wp)
4108 tabpage_T *tp;
4109 win_T *wp;
4110{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004111 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004112 if (curtab == tp && win_valid(wp))
4113 {
4114 win_enter(wp, TRUE);
4115# ifdef FEAT_GUI_TABLINE
4116 if (gui_use_tabline())
4117 gui_mch_set_curtab(tabpage_index(curtab));
4118# endif
4119 }
4120}
4121
4122/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004123 * Move the current tab page to before tab page "nr".
4124 */
4125 void
4126tabpage_move(nr)
4127 int nr;
4128{
4129 int n = nr;
4130 tabpage_T *tp;
4131
4132 if (first_tabpage->tp_next == NULL)
4133 return;
4134
4135 /* Remove the current tab page from the list of tab pages. */
4136 if (curtab == first_tabpage)
4137 first_tabpage = curtab->tp_next;
4138 else
4139 {
4140 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4141 if (tp->tp_next == curtab)
4142 break;
4143 if (tp == NULL) /* "cannot happen" */
4144 return;
4145 tp->tp_next = curtab->tp_next;
4146 }
4147
4148 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004149 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004150 {
4151 curtab->tp_next = first_tabpage;
4152 first_tabpage = curtab;
4153 }
4154 else
4155 {
4156 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4157 --n;
4158 curtab->tp_next = tp->tp_next;
4159 tp->tp_next = curtab;
4160 }
4161
4162 /* Need to redraw the tabline. Tab page contents doesn't change. */
4163 redraw_tabline = TRUE;
4164}
4165
4166
4167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * Go to another window.
4169 * When jumping to another buffer, stop Visual mode. Do this before
4170 * changing windows so we can yank the selection into the '*' register.
4171 * When jumping to another window on the same buffer, adjust its cursor
4172 * position to keep the same Visual area.
4173 */
4174 void
4175win_goto(wp)
4176 win_T *wp;
4177{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004178#ifdef FEAT_CONCEAL
4179 win_T *owp = curwin;
4180#endif
4181
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004182 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
4184 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004185 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return;
4187 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004188#ifdef FEAT_AUTOCMD
4189 if (curbuf_locked())
4190 return;
4191#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (wp->w_buffer != curbuf)
4194 reset_VIsual_and_resel();
4195 else if (VIsual_active)
4196 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198#ifdef FEAT_GUI
4199 need_mouse_correct = TRUE;
4200#endif
4201 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004202
4203#ifdef FEAT_CONCEAL
4204 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004205 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004206 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004207 if (curwin->w_p_cole > 0 && !msg_scrolled)
4208 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210}
4211
4212#if defined(FEAT_PERL) || defined(PROTO)
4213/*
4214 * Find window number "winnr" (counting top to bottom).
4215 */
4216 win_T *
4217win_find_nr(winnr)
4218 int winnr;
4219{
4220 win_T *wp;
4221
4222# ifdef FEAT_WINDOWS
4223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4224 if (--winnr == 0)
4225 break;
4226 return wp;
4227# else
4228 return curwin;
4229# endif
4230}
4231#endif
4232
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004233#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4234 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004235/*
4236 * Find the tabpage for window "win".
4237 */
4238 tabpage_T *
4239win_find_tabpage(win)
4240 win_T *win;
4241{
4242 win_T *wp;
4243 tabpage_T *tp;
4244
4245 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004246 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4247 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004248 if (wp == win)
4249 return tp;
4250 return NULL;
4251}
4252#endif
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#ifdef FEAT_VERTSPLIT
4255/*
4256 * Move to window above or below "count" times.
4257 */
4258 static void
4259win_goto_ver(up, count)
4260 int up; /* TRUE to go to win above */
4261 long count;
4262{
4263 frame_T *fr;
4264 frame_T *nfr;
4265 frame_T *foundfr;
4266
4267 foundfr = curwin->w_frame;
4268 while (count--)
4269 {
4270 /*
4271 * First go upwards in the tree of frames until we find a upwards or
4272 * downwards neighbor.
4273 */
4274 fr = foundfr;
4275 for (;;)
4276 {
4277 if (fr == topframe)
4278 goto end;
4279 if (up)
4280 nfr = fr->fr_prev;
4281 else
4282 nfr = fr->fr_next;
4283 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4284 break;
4285 fr = fr->fr_parent;
4286 }
4287
4288 /*
4289 * Now go downwards to find the bottom or top frame in it.
4290 */
4291 for (;;)
4292 {
4293 if (nfr->fr_layout == FR_LEAF)
4294 {
4295 foundfr = nfr;
4296 break;
4297 }
4298 fr = nfr->fr_child;
4299 if (nfr->fr_layout == FR_ROW)
4300 {
4301 /* Find the frame at the cursor row. */
4302 while (fr->fr_next != NULL
4303 && frame2win(fr)->w_wincol + fr->fr_width
4304 <= curwin->w_wincol + curwin->w_wcol)
4305 fr = fr->fr_next;
4306 }
4307 if (nfr->fr_layout == FR_COL && up)
4308 while (fr->fr_next != NULL)
4309 fr = fr->fr_next;
4310 nfr = fr;
4311 }
4312 }
4313end:
4314 if (foundfr != NULL)
4315 win_goto(foundfr->fr_win);
4316}
4317
4318/*
4319 * Move to left or right window.
4320 */
4321 static void
4322win_goto_hor(left, count)
4323 int left; /* TRUE to go to left win */
4324 long count;
4325{
4326 frame_T *fr;
4327 frame_T *nfr;
4328 frame_T *foundfr;
4329
4330 foundfr = curwin->w_frame;
4331 while (count--)
4332 {
4333 /*
4334 * First go upwards in the tree of frames until we find a left or
4335 * right neighbor.
4336 */
4337 fr = foundfr;
4338 for (;;)
4339 {
4340 if (fr == topframe)
4341 goto end;
4342 if (left)
4343 nfr = fr->fr_prev;
4344 else
4345 nfr = fr->fr_next;
4346 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4347 break;
4348 fr = fr->fr_parent;
4349 }
4350
4351 /*
4352 * Now go downwards to find the leftmost or rightmost frame in it.
4353 */
4354 for (;;)
4355 {
4356 if (nfr->fr_layout == FR_LEAF)
4357 {
4358 foundfr = nfr;
4359 break;
4360 }
4361 fr = nfr->fr_child;
4362 if (nfr->fr_layout == FR_COL)
4363 {
4364 /* Find the frame at the cursor row. */
4365 while (fr->fr_next != NULL
4366 && frame2win(fr)->w_winrow + fr->fr_height
4367 <= curwin->w_winrow + curwin->w_wrow)
4368 fr = fr->fr_next;
4369 }
4370 if (nfr->fr_layout == FR_ROW && left)
4371 while (fr->fr_next != NULL)
4372 fr = fr->fr_next;
4373 nfr = fr;
4374 }
4375 }
4376end:
4377 if (foundfr != NULL)
4378 win_goto(foundfr->fr_win);
4379}
4380#endif
4381
4382/*
4383 * Make window "wp" the current window.
4384 */
4385 void
4386win_enter(wp, undo_sync)
4387 win_T *wp;
4388 int undo_sync;
4389{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004390 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391}
4392
4393/*
4394 * Make window wp the current window.
4395 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4396 * been closed and isn't valid.
4397 */
4398 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004399win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 win_T *wp;
4401 int undo_sync;
4402 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004403 int trigger_enter_autocmds UNUSED;
4404 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406#ifdef FEAT_AUTOCMD
4407 int other_buffer = FALSE;
4408#endif
4409
4410 if (wp == curwin && !curwin_invalid) /* nothing to do */
4411 return;
4412
4413#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004414 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 /*
4417 * Be careful: If autocommands delete the window, return now.
4418 */
4419 if (wp->w_buffer != curbuf)
4420 {
4421 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4422 other_buffer = TRUE;
4423 if (!win_valid(wp))
4424 return;
4425 }
4426 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4427 if (!win_valid(wp))
4428 return;
4429# ifdef FEAT_EVAL
4430 /* autocmds may abort script processing */
4431 if (aborting())
4432 return;
4433# endif
4434 }
4435#endif
4436
4437 /* sync undo before leaving the current buffer */
4438 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004439 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004440
4441 /* Might need to scroll the old window before switching, e.g., when the
4442 * cursor was moved. */
4443 update_topline();
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* may have to copy the buffer options when 'cpo' contains 'S' */
4446 if (wp->w_buffer != curbuf)
4447 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4448 if (!curwin_invalid)
4449 {
4450 prevwin = curwin; /* remember for CTRL-W p */
4451 curwin->w_redr_status = TRUE;
4452 }
4453 curwin = wp;
4454 curbuf = wp->w_buffer;
4455 check_cursor();
4456#ifdef FEAT_VIRTUALEDIT
4457 if (!virtual_active())
4458 curwin->w_cursor.coladd = 0;
4459#endif
4460 changed_line_abv_curs(); /* assume cursor position needs updating */
4461
4462 if (curwin->w_localdir != NULL)
4463 {
4464 /* Window has a local directory: Save current directory as global
4465 * directory (unless that was done already) and change to the local
4466 * directory. */
4467 if (globaldir == NULL)
4468 {
4469 char_u cwd[MAXPATHL];
4470
4471 if (mch_dirname(cwd, MAXPATHL) == OK)
4472 globaldir = vim_strsave(cwd);
4473 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004474 if (mch_chdir((char *)curwin->w_localdir) == 0)
4475 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else if (globaldir != NULL)
4478 {
4479 /* Window doesn't have a local directory and we are not in the global
4480 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004481 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 vim_free(globaldir);
4483 globaldir = NULL;
4484 shorten_fnames(TRUE);
4485 }
4486
4487#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004488 if (trigger_enter_autocmds)
4489 {
4490 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4491 if (other_buffer)
4492 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494#endif
4495
4496#ifdef FEAT_TITLE
4497 maketitle();
4498#endif
4499 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004500 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (restart_edit)
4502 redraw_later(VALID); /* causes status line redraw */
4503
4504 /* set window height to desired minimal value */
4505 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4506 win_setheight((int)p_wh);
4507 else if (curwin->w_height == 0)
4508 win_setheight(1);
4509
4510#ifdef FEAT_VERTSPLIT
4511 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004512 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 win_setwidth((int)p_wiw);
4514#endif
4515
4516#ifdef FEAT_MOUSE
4517 setmouse(); /* in case jumped to/from help buffer */
4518#endif
4519
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004520 /* Change directories when the 'acd' option is set. */
4521 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
4524#endif /* FEAT_WINDOWS */
4525
4526#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4527/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004528 * Jump to the first open window that contains buffer "buf", if one exists.
4529 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 */
4531 win_T *
4532buf_jump_open_win(buf)
4533 buf_T *buf;
4534{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004535 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004537 if (curwin->w_buffer == buf)
4538 wp = curwin;
4539# ifdef FEAT_WINDOWS
4540 else
4541 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4542 if (wp->w_buffer == buf)
4543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (wp != NULL)
4545 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004547 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004549
4550/*
4551 * Jump to the first open window in any tab page that contains buffer "buf",
4552 * if one exists.
4553 * Returns a pointer to the window found, otherwise NULL.
4554 */
4555 win_T *
4556buf_jump_open_tab(buf)
4557 buf_T *buf;
4558{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004559 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004560# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004561 tabpage_T *tp;
4562
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004563 if (wp != NULL)
4564 return wp;
4565
4566 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4567 if (tp != curtab)
4568 {
4569 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4570 if (wp->w_buffer == buf)
4571 break;
4572 if (wp != NULL)
4573 {
4574 goto_tabpage_win(tp, wp);
4575 if (curwin != wp)
4576 wp = NULL; /* something went wrong */
4577 break;
4578 }
4579 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004580# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004581 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#endif
4584
4585/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004586 * Allocate a window structure and link it in the window list when "hidden" is
4587 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004590win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004591 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004592 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004594 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595
4596 /*
4597 * allocate window structure and linesizes arrays
4598 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004599 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004600 if (new_wp == NULL)
4601 return NULL;
4602
4603 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004605 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004606 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
Bram Moolenaar429fa852013-04-15 12:27:36 +02004609#ifdef FEAT_EVAL
4610 /* init w: variables */
4611 new_wp->w_vars = dict_alloc();
4612 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004614 win_free_lsize(new_wp);
4615 vim_free(new_wp);
4616 return NULL;
4617 }
4618 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004619#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004620
4621#ifdef FEAT_AUTOCMD
4622 /* Don't execute autocommands while the window is not properly
4623 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4624 * event. */
4625 block_autocmds();
4626#endif
4627 /*
4628 * link the window in the window list
4629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004631 if (!hidden)
4632 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
4634#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004635 new_wp->w_wincol = 0;
4636 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
Bram Moolenaar429fa852013-04-15 12:27:36 +02004639 /* position the display and the cursor at the top of the file. */
4640 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004642 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004644 new_wp->w_botline = 2;
4645 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004647 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#endif
4649
Bram Moolenaar429fa852013-04-15 12:27:36 +02004650 /* We won't calculate w_fraction until resizing the window */
4651 new_wp->w_fraction = 0;
4652 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004655 if (gui.in_use)
4656 {
4657 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4658 SBAR_LEFT, new_wp);
4659 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4660 SBAR_RIGHT, new_wp);
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#endif
4663#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004664 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004666#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004667 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004668#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004669#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004670 new_wp->w_match_head = NULL;
4671 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004672#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004673 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674}
4675
4676#if defined(FEAT_WINDOWS) || defined(PROTO)
4677
4678/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004679 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 */
4681 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004682win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004684 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004687 buf_T *buf;
4688 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004690#ifdef FEAT_FOLDING
4691 clearFolding(wp);
4692#endif
4693
4694 /* reduce the reference count to the argument list. */
4695 alist_unlink(wp->w_alist);
4696
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004697#ifdef FEAT_AUTOCMD
4698 /* Don't execute autocommands while the window is halfway being deleted.
4699 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004700 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004701#endif
4702
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004703#ifdef FEAT_LUA
4704 lua_window_free(wp);
4705#endif
4706
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004707#ifdef FEAT_MZSCHEME
4708 mzscheme_window_free(wp);
4709#endif
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_PERL
4712 perl_win_free(wp);
4713#endif
4714
4715#ifdef FEAT_PYTHON
4716 python_window_free(wp);
4717#endif
4718
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004719#ifdef FEAT_PYTHON3
4720 python3_window_free(wp);
4721#endif
4722
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723#ifdef FEAT_TCL
4724 tcl_window_free(wp);
4725#endif
4726
4727#ifdef FEAT_RUBY
4728 ruby_window_free(wp);
4729#endif
4730
4731 clear_winopt(&wp->w_onebuf_opt);
4732 clear_winopt(&wp->w_allbuf_opt);
4733
4734#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004735 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4736 hash_init(&wp->w_vars->dv_hashtab);
4737 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#endif
4739
4740 if (prevwin == wp)
4741 prevwin = NULL;
4742 win_free_lsize(wp);
4743
4744 for (i = 0; i < wp->w_tagstacklen; ++i)
4745 vim_free(wp->w_tagstack[i].tagname);
4746
4747 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004748
Bram Moolenaarff18df02013-07-24 17:51:57 +02004749 /* Remove the window from the b_wininfo lists, it may happen that the
4750 * freed memory is re-used for another window. */
4751 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4752 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4753 if (wip->wi_win == wp)
4754 wip->wi_win = NULL;
4755
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004757 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760#ifdef FEAT_JUMPLIST
4761 free_jumplist(wp);
4762#endif
4763
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004764#ifdef FEAT_QUICKFIX
4765 qf_free_all(wp);
4766#endif
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768#ifdef FEAT_GUI
4769 if (gui.in_use)
4770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4772 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4773 }
4774#endif /* FEAT_GUI */
4775
Bram Moolenaar860cae12010-06-05 23:22:07 +02004776#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004777 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778#endif
4779
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004780#ifdef FEAT_AUTOCMD
4781 if (wp != aucmd_win)
4782#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004783 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004784#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004785 if (autocmd_busy)
4786 {
4787 wp->w_next = au_pending_free_win;
4788 au_pending_free_win = wp;
4789 }
4790 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004791#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004792 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004793
4794#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004795 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797}
4798
4799/*
4800 * Append window "wp" in the window list after window "after".
4801 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004802 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803win_append(after, wp)
4804 win_T *after, *wp;
4805{
4806 win_T *before;
4807
4808 if (after == NULL) /* after NULL is in front of the first */
4809 before = firstwin;
4810 else
4811 before = after->w_next;
4812
4813 wp->w_next = before;
4814 wp->w_prev = after;
4815 if (after == NULL)
4816 firstwin = wp;
4817 else
4818 after->w_next = wp;
4819 if (before == NULL)
4820 lastwin = wp;
4821 else
4822 before->w_prev = wp;
4823}
4824
4825/*
4826 * Remove a window from the window list.
4827 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004828 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004829win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004831 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
4833 if (wp->w_prev != NULL)
4834 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004835 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004837 else
4838 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if (wp->w_next != NULL)
4840 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004841 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004843 else
4844 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
4847/*
4848 * Append frame "frp" in a frame list after frame "after".
4849 */
4850 static void
4851frame_append(after, frp)
4852 frame_T *after, *frp;
4853{
4854 frp->fr_next = after->fr_next;
4855 after->fr_next = frp;
4856 if (frp->fr_next != NULL)
4857 frp->fr_next->fr_prev = frp;
4858 frp->fr_prev = after;
4859}
4860
4861/*
4862 * Insert frame "frp" in a frame list before frame "before".
4863 */
4864 static void
4865frame_insert(before, frp)
4866 frame_T *before, *frp;
4867{
4868 frp->fr_next = before;
4869 frp->fr_prev = before->fr_prev;
4870 before->fr_prev = frp;
4871 if (frp->fr_prev != NULL)
4872 frp->fr_prev->fr_next = frp;
4873 else
4874 frp->fr_parent->fr_child = frp;
4875}
4876
4877/*
4878 * Remove a frame from a frame list.
4879 */
4880 static void
4881frame_remove(frp)
4882 frame_T *frp;
4883{
4884 if (frp->fr_prev != NULL)
4885 frp->fr_prev->fr_next = frp->fr_next;
4886 else
4887 frp->fr_parent->fr_child = frp->fr_next;
4888 if (frp->fr_next != NULL)
4889 frp->fr_next->fr_prev = frp->fr_prev;
4890}
4891
4892#endif /* FEAT_WINDOWS */
4893
4894/*
4895 * Allocate w_lines[] for window "wp".
4896 * Return FAIL for failure, OK for success.
4897 */
4898 int
4899win_alloc_lines(wp)
4900 win_T *wp;
4901{
4902 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004903 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (wp->w_lines == NULL)
4905 return FAIL;
4906 return OK;
4907}
4908
4909/*
4910 * free lsize arrays for a window
4911 */
4912 void
4913win_free_lsize(wp)
4914 win_T *wp;
4915{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004916 /* TODO: why would wp be NULL here? */
4917 if (wp != NULL)
4918 {
4919 vim_free(wp->w_lines);
4920 wp->w_lines = NULL;
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922}
4923
4924/*
4925 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004926 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 */
4928 void
4929shell_new_rows()
4930{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004931 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 if (firstwin == NULL) /* not initialized yet */
4934 return;
4935#ifdef FEAT_WINDOWS
4936 if (h < frame_minheight(topframe, NULL))
4937 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004938
4939 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 * that doesn't result in the right height, forget about that option. */
4941 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004942 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 frame_new_height(topframe, h, FALSE, FALSE);
4944
4945 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4946#else
4947 if (h < 1)
4948 h = 1;
4949 win_new_height(firstwin, h);
4950#endif
4951 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004952#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004953 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004954#endif
4955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#if 0
4957 /* Disabled: don't want making the screen smaller make a window larger. */
4958 if (p_ea)
4959 win_equal(curwin, FALSE, 'v');
4960#endif
4961}
4962
4963#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4964/*
4965 * Called from win_new_shellsize() after Columns changed.
4966 */
4967 void
4968shell_new_columns()
4969{
4970 if (firstwin == NULL) /* not initialized yet */
4971 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004972
4973 /* First try setting the widths of windows with 'winfixwidth'. If that
4974 * doesn't result in the right width, forget about that option. */
4975 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004976 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004977 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4980#if 0
4981 /* Disabled: don't want making the screen smaller make a window larger. */
4982 if (p_ea)
4983 win_equal(curwin, FALSE, 'h');
4984#endif
4985}
4986#endif
4987
4988#if defined(FEAT_CMDWIN) || defined(PROTO)
4989/*
4990 * Save the size of all windows in "gap".
4991 */
4992 void
4993win_size_save(gap)
4994 garray_T *gap;
4995
4996{
4997 win_T *wp;
4998
4999 ga_init2(gap, (int)sizeof(int), 1);
5000 if (ga_grow(gap, win_count() * 2) == OK)
5001 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5002 {
5003 ((int *)gap->ga_data)[gap->ga_len++] =
5004 wp->w_width + wp->w_vsep_width;
5005 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5006 }
5007}
5008
5009/*
5010 * Restore window sizes, but only if the number of windows is still the same.
5011 * Does not free the growarray.
5012 */
5013 void
5014win_size_restore(gap)
5015 garray_T *gap;
5016{
5017 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005018 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 if (win_count() * 2 == gap->ga_len)
5021 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005022 /* The order matters, because frames contain other frames, but it's
5023 * difficult to get right. The easy way out is to do it twice. */
5024 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005026 i = 0;
5027 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5028 {
5029 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5030 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 /* recompute the window positions */
5034 (void)win_comp_pos();
5035 }
5036}
5037#endif /* FEAT_CMDWIN */
5038
5039#if defined(FEAT_WINDOWS) || defined(PROTO)
5040/*
5041 * Update the position for all windows, using the width and height of the
5042 * frames.
5043 * Returns the row just after the last window.
5044 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005045 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046win_comp_pos()
5047{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005048 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 int col = 0;
5050
5051 frame_comp_pos(topframe, &row, &col);
5052 return row;
5053}
5054
5055/*
5056 * Update the position of the windows in frame "topfrp", using the width and
5057 * height of the frames.
5058 * "*row" and "*col" are the top-left position of the frame. They are updated
5059 * to the bottom-right position plus one.
5060 */
5061 static void
5062frame_comp_pos(topfrp, row, col)
5063 frame_T *topfrp;
5064 int *row;
5065 int *col;
5066{
5067 win_T *wp;
5068 frame_T *frp;
5069#ifdef FEAT_VERTSPLIT
5070 int startcol;
5071 int startrow;
5072#endif
5073
5074 wp = topfrp->fr_win;
5075 if (wp != NULL)
5076 {
5077 if (wp->w_winrow != *row
5078#ifdef FEAT_VERTSPLIT
5079 || wp->w_wincol != *col
5080#endif
5081 )
5082 {
5083 /* position changed, redraw */
5084 wp->w_winrow = *row;
5085#ifdef FEAT_VERTSPLIT
5086 wp->w_wincol = *col;
5087#endif
5088 redraw_win_later(wp, NOT_VALID);
5089 wp->w_redr_status = TRUE;
5090 }
5091 *row += wp->w_height + wp->w_status_height;
5092#ifdef FEAT_VERTSPLIT
5093 *col += wp->w_width + wp->w_vsep_width;
5094#endif
5095 }
5096 else
5097 {
5098#ifdef FEAT_VERTSPLIT
5099 startrow = *row;
5100 startcol = *col;
5101#endif
5102 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5103 {
5104#ifdef FEAT_VERTSPLIT
5105 if (topfrp->fr_layout == FR_ROW)
5106 *row = startrow; /* all frames are at the same row */
5107 else
5108 *col = startcol; /* all frames are at the same col */
5109#endif
5110 frame_comp_pos(frp, row, col);
5111 }
5112 }
5113}
5114
5115#endif /* FEAT_WINDOWS */
5116
5117/*
5118 * Set current window height and take care of repositioning other windows to
5119 * fit around it.
5120 */
5121 void
5122win_setheight(height)
5123 int height;
5124{
5125 win_setheight_win(height, curwin);
5126}
5127
5128/*
5129 * Set the window height of window "win" and take care of repositioning other
5130 * windows to fit around it.
5131 */
5132 void
5133win_setheight_win(height, win)
5134 int height;
5135 win_T *win;
5136{
5137 int row;
5138
5139 if (win == curwin)
5140 {
5141 /* Always keep current window at least one line high, even when
5142 * 'winminheight' is zero. */
5143#ifdef FEAT_WINDOWS
5144 if (height < p_wmh)
5145 height = p_wmh;
5146#endif
5147 if (height == 0)
5148 height = 1;
5149 }
5150
5151#ifdef FEAT_WINDOWS
5152 frame_setheight(win->w_frame, height + win->w_status_height);
5153
5154 /* recompute the window positions */
5155 row = win_comp_pos();
5156#else
5157 if (height > topframe->fr_height)
5158 height = topframe->fr_height;
5159 win->w_height = height;
5160 row = height;
5161#endif
5162
5163 /*
5164 * If there is extra space created between the last window and the command
5165 * line, clear it.
5166 */
5167 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5168 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5169 cmdline_row = row;
5170 msg_row = row;
5171 msg_col = 0;
5172
5173 redraw_all_later(NOT_VALID);
5174}
5175
5176#if defined(FEAT_WINDOWS) || defined(PROTO)
5177
5178/*
5179 * Set the height of a frame to "height" and take care that all frames and
5180 * windows inside it are resized. Also resize frames on the left and right if
5181 * the are in the same FR_ROW frame.
5182 *
5183 * Strategy:
5184 * If the frame is part of a FR_COL frame, try fitting the frame in that
5185 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5186 * go to containing frames to resize them and make room.
5187 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5188 * Check for the minimal height of the FR_ROW frame.
5189 * At the top level we can also use change the command line height.
5190 */
5191 static void
5192frame_setheight(curfrp, height)
5193 frame_T *curfrp;
5194 int height;
5195{
5196 int room; /* total number of lines available */
5197 int take; /* number of lines taken from other windows */
5198 int room_cmdline; /* lines available from cmdline */
5199 int run;
5200 frame_T *frp;
5201 int h;
5202 int room_reserved;
5203
5204 /* If the height already is the desired value, nothing to do. */
5205 if (curfrp->fr_height == height)
5206 return;
5207
5208 if (curfrp->fr_parent == NULL)
5209 {
5210 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005211 if (height > ROWS_AVAIL)
5212 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (height > 0)
5214 frame_new_height(curfrp, height, FALSE, FALSE);
5215 }
5216 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5217 {
5218 /* Row of frames: Also need to resize frames left and right of this
5219 * one. First check for the minimal height of these. */
5220 h = frame_minheight(curfrp->fr_parent, NULL);
5221 if (height < h)
5222 height = h;
5223 frame_setheight(curfrp->fr_parent, height);
5224 }
5225 else
5226 {
5227 /*
5228 * Column of frames: try to change only frames in this column.
5229 */
5230#ifdef FEAT_VERTSPLIT
5231 /*
5232 * Do this twice:
5233 * 1: compute room available, if it's not enough try resizing the
5234 * containing frame.
5235 * 2: compute the room available and adjust the height to it.
5236 * Try not to reduce the height of a window with 'winfixheight' set.
5237 */
5238 for (run = 1; run <= 2; ++run)
5239#else
5240 for (;;)
5241#endif
5242 {
5243 room = 0;
5244 room_reserved = 0;
5245 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5246 frp = frp->fr_next)
5247 {
5248 if (frp != curfrp
5249 && frp->fr_win != NULL
5250 && frp->fr_win->w_p_wfh)
5251 room_reserved += frp->fr_height;
5252 room += frp->fr_height;
5253 if (frp != curfrp)
5254 room -= frame_minheight(frp, NULL);
5255 }
5256#ifdef FEAT_VERTSPLIT
5257 if (curfrp->fr_width != Columns)
5258 room_cmdline = 0;
5259 else
5260#endif
5261 {
5262 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5263 + lastwin->w_height + lastwin->w_status_height);
5264 if (room_cmdline < 0)
5265 room_cmdline = 0;
5266 }
5267
5268 if (height <= room + room_cmdline)
5269 break;
5270#ifdef FEAT_VERTSPLIT
5271 if (run == 2 || curfrp->fr_width == Columns)
5272#endif
5273 {
5274 if (height > room + room_cmdline)
5275 height = room + room_cmdline;
5276 break;
5277 }
5278#ifdef FEAT_VERTSPLIT
5279 frame_setheight(curfrp->fr_parent, height
5280 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5281#endif
5282 /*NOTREACHED*/
5283 }
5284
5285 /*
5286 * Compute the number of lines we will take from others frames (can be
5287 * negative!).
5288 */
5289 take = height - curfrp->fr_height;
5290
5291 /* If there is not enough room, also reduce the height of a window
5292 * with 'winfixheight' set. */
5293 if (height > room + room_cmdline - room_reserved)
5294 room_reserved = room + room_cmdline - height;
5295 /* If there is only a 'winfixheight' window and making the
5296 * window smaller, need to make the other window taller. */
5297 if (take < 0 && room - curfrp->fr_height < room_reserved)
5298 room_reserved = 0;
5299
5300 if (take > 0 && room_cmdline > 0)
5301 {
5302 /* use lines from cmdline first */
5303 if (take < room_cmdline)
5304 room_cmdline = take;
5305 take -= room_cmdline;
5306 topframe->fr_height += room_cmdline;
5307 }
5308
5309 /*
5310 * set the current frame to the new height
5311 */
5312 frame_new_height(curfrp, height, FALSE, FALSE);
5313
5314 /*
5315 * First take lines from the frames after the current frame. If
5316 * that is not enough, takes lines from frames above the current
5317 * frame.
5318 */
5319 for (run = 0; run < 2; ++run)
5320 {
5321 if (run == 0)
5322 frp = curfrp->fr_next; /* 1st run: start with next window */
5323 else
5324 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5325 while (frp != NULL && take != 0)
5326 {
5327 h = frame_minheight(frp, NULL);
5328 if (room_reserved > 0
5329 && frp->fr_win != NULL
5330 && frp->fr_win->w_p_wfh)
5331 {
5332 if (room_reserved >= frp->fr_height)
5333 room_reserved -= frp->fr_height;
5334 else
5335 {
5336 if (frp->fr_height - room_reserved > take)
5337 room_reserved = frp->fr_height - take;
5338 take -= frp->fr_height - room_reserved;
5339 frame_new_height(frp, room_reserved, FALSE, FALSE);
5340 room_reserved = 0;
5341 }
5342 }
5343 else
5344 {
5345 if (frp->fr_height - take < h)
5346 {
5347 take -= frp->fr_height - h;
5348 frame_new_height(frp, h, FALSE, FALSE);
5349 }
5350 else
5351 {
5352 frame_new_height(frp, frp->fr_height - take,
5353 FALSE, FALSE);
5354 take = 0;
5355 }
5356 }
5357 if (run == 0)
5358 frp = frp->fr_next;
5359 else
5360 frp = frp->fr_prev;
5361 }
5362 }
5363 }
5364}
5365
5366#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5367/*
5368 * Set current window width and take care of repositioning other windows to
5369 * fit around it.
5370 */
5371 void
5372win_setwidth(width)
5373 int width;
5374{
5375 win_setwidth_win(width, curwin);
5376}
5377
5378 void
5379win_setwidth_win(width, wp)
5380 int width;
5381 win_T *wp;
5382{
5383 /* Always keep current window at least one column wide, even when
5384 * 'winminwidth' is zero. */
5385 if (wp == curwin)
5386 {
5387 if (width < p_wmw)
5388 width = p_wmw;
5389 if (width == 0)
5390 width = 1;
5391 }
5392
5393 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5394
5395 /* recompute the window positions */
5396 (void)win_comp_pos();
5397
5398 redraw_all_later(NOT_VALID);
5399}
5400
5401/*
5402 * Set the width of a frame to "width" and take care that all frames and
5403 * windows inside it are resized. Also resize frames above and below if the
5404 * are in the same FR_ROW frame.
5405 *
5406 * Strategy is similar to frame_setheight().
5407 */
5408 static void
5409frame_setwidth(curfrp, width)
5410 frame_T *curfrp;
5411 int width;
5412{
5413 int room; /* total number of lines available */
5414 int take; /* number of lines taken from other windows */
5415 int run;
5416 frame_T *frp;
5417 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005418 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 /* If the width already is the desired value, nothing to do. */
5421 if (curfrp->fr_width == width)
5422 return;
5423
5424 if (curfrp->fr_parent == NULL)
5425 /* topframe: can't change width */
5426 return;
5427
5428 if (curfrp->fr_parent->fr_layout == FR_COL)
5429 {
5430 /* Column of frames: Also need to resize frames above and below of
5431 * this one. First check for the minimal width of these. */
5432 w = frame_minwidth(curfrp->fr_parent, NULL);
5433 if (width < w)
5434 width = w;
5435 frame_setwidth(curfrp->fr_parent, width);
5436 }
5437 else
5438 {
5439 /*
5440 * Row of frames: try to change only frames in this row.
5441 *
5442 * Do this twice:
5443 * 1: compute room available, if it's not enough try resizing the
5444 * containing frame.
5445 * 2: compute the room available and adjust the width to it.
5446 */
5447 for (run = 1; run <= 2; ++run)
5448 {
5449 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005450 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5452 frp = frp->fr_next)
5453 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005454 if (frp != curfrp
5455 && frp->fr_win != NULL
5456 && frp->fr_win->w_p_wfw)
5457 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 room += frp->fr_width;
5459 if (frp != curfrp)
5460 room -= frame_minwidth(frp, NULL);
5461 }
5462
5463 if (width <= room)
5464 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005465 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 if (width > room)
5468 width = room;
5469 break;
5470 }
5471 frame_setwidth(curfrp->fr_parent, width
5472 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5473 }
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 /*
5476 * Compute the number of lines we will take from others frames (can be
5477 * negative!).
5478 */
5479 take = width - curfrp->fr_width;
5480
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005481 /* If there is not enough room, also reduce the width of a window
5482 * with 'winfixwidth' set. */
5483 if (width > room - room_reserved)
5484 room_reserved = room - width;
5485 /* If there is only a 'winfixwidth' window and making the
5486 * window smaller, need to make the other window narrower. */
5487 if (take < 0 && room - curfrp->fr_width < room_reserved)
5488 room_reserved = 0;
5489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 /*
5491 * set the current frame to the new width
5492 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005493 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 /*
5496 * First take lines from the frames right of the current frame. If
5497 * that is not enough, takes lines from frames left of the current
5498 * frame.
5499 */
5500 for (run = 0; run < 2; ++run)
5501 {
5502 if (run == 0)
5503 frp = curfrp->fr_next; /* 1st run: start with next window */
5504 else
5505 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5506 while (frp != NULL && take != 0)
5507 {
5508 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005509 if (room_reserved > 0
5510 && frp->fr_win != NULL
5511 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005513 if (room_reserved >= frp->fr_width)
5514 room_reserved -= frp->fr_width;
5515 else
5516 {
5517 if (frp->fr_width - room_reserved > take)
5518 room_reserved = frp->fr_width - take;
5519 take -= frp->fr_width - room_reserved;
5520 frame_new_width(frp, room_reserved, FALSE, FALSE);
5521 room_reserved = 0;
5522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 else
5525 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005526 if (frp->fr_width - take < w)
5527 {
5528 take -= frp->fr_width - w;
5529 frame_new_width(frp, w, FALSE, FALSE);
5530 }
5531 else
5532 {
5533 frame_new_width(frp, frp->fr_width - take,
5534 FALSE, FALSE);
5535 take = 0;
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538 if (run == 0)
5539 frp = frp->fr_next;
5540 else
5541 frp = frp->fr_prev;
5542 }
5543 }
5544 }
5545}
5546#endif /* FEAT_VERTSPLIT */
5547
5548/*
5549 * Check 'winminheight' for a valid value.
5550 */
5551 void
5552win_setminheight()
5553{
5554 int room;
5555 int first = TRUE;
5556 win_T *wp;
5557
5558 /* loop until there is a 'winminheight' that is possible */
5559 while (p_wmh > 0)
5560 {
5561 /* TODO: handle vertical splits */
5562 room = -p_wh;
5563 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5564 room += wp->w_height - p_wmh;
5565 if (room >= 0)
5566 break;
5567 --p_wmh;
5568 if (first)
5569 {
5570 EMSG(_(e_noroom));
5571 first = FALSE;
5572 }
5573 }
5574}
5575
5576#ifdef FEAT_MOUSE
5577
5578/*
5579 * Status line of dragwin is dragged "offset" lines down (negative is up).
5580 */
5581 void
5582win_drag_status_line(dragwin, offset)
5583 win_T *dragwin;
5584 int offset;
5585{
5586 frame_T *curfr;
5587 frame_T *fr;
5588 int room;
5589 int row;
5590 int up; /* if TRUE, drag status line up, otherwise down */
5591 int n;
5592
5593 fr = dragwin->w_frame;
5594 curfr = fr;
5595 if (fr != topframe) /* more than one window */
5596 {
5597 fr = fr->fr_parent;
5598 /* When the parent frame is not a column of frames, its parent should
5599 * be. */
5600 if (fr->fr_layout != FR_COL)
5601 {
5602 curfr = fr;
5603 if (fr != topframe) /* only a row of windows, may drag statusline */
5604 fr = fr->fr_parent;
5605 }
5606 }
5607
5608 /* If this is the last frame in a column, may want to resize the parent
5609 * frame instead (go two up to skip a row of frames). */
5610 while (curfr != topframe && curfr->fr_next == NULL)
5611 {
5612 if (fr != topframe)
5613 fr = fr->fr_parent;
5614 curfr = fr;
5615 if (fr != topframe)
5616 fr = fr->fr_parent;
5617 }
5618
5619 if (offset < 0) /* drag up */
5620 {
5621 up = TRUE;
5622 offset = -offset;
5623 /* sum up the room of the current frame and above it */
5624 if (fr == curfr)
5625 {
5626 /* only one window */
5627 room = fr->fr_height - frame_minheight(fr, NULL);
5628 }
5629 else
5630 {
5631 room = 0;
5632 for (fr = fr->fr_child; ; fr = fr->fr_next)
5633 {
5634 room += fr->fr_height - frame_minheight(fr, NULL);
5635 if (fr == curfr)
5636 break;
5637 }
5638 }
5639 fr = curfr->fr_next; /* put fr at frame that grows */
5640 }
5641 else /* drag down */
5642 {
5643 up = FALSE;
5644 /*
5645 * Only dragging the last status line can reduce p_ch.
5646 */
5647 room = Rows - cmdline_row;
5648 if (curfr->fr_next == NULL)
5649 room -= 1;
5650 else
5651 room -= p_ch;
5652 if (room < 0)
5653 room = 0;
5654 /* sum up the room of frames below of the current one */
5655 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5656 room += fr->fr_height - frame_minheight(fr, NULL);
5657 fr = curfr; /* put fr at window that grows */
5658 }
5659
5660 if (room < offset) /* Not enough room */
5661 offset = room; /* Move as far as we can */
5662 if (offset <= 0)
5663 return;
5664
5665 /*
5666 * Grow frame fr by "offset" lines.
5667 * Doesn't happen when dragging the last status line up.
5668 */
5669 if (fr != NULL)
5670 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5671
5672 if (up)
5673 fr = curfr; /* current frame gets smaller */
5674 else
5675 fr = curfr->fr_next; /* next frame gets smaller */
5676
5677 /*
5678 * Now make the other frames smaller.
5679 */
5680 while (fr != NULL && offset > 0)
5681 {
5682 n = frame_minheight(fr, NULL);
5683 if (fr->fr_height - offset <= n)
5684 {
5685 offset -= fr->fr_height - n;
5686 frame_new_height(fr, n, !up, FALSE);
5687 }
5688 else
5689 {
5690 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5691 break;
5692 }
5693 if (up)
5694 fr = fr->fr_prev;
5695 else
5696 fr = fr->fr_next;
5697 }
5698 row = win_comp_pos();
5699 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5700 cmdline_row = row;
5701 p_ch = Rows - cmdline_row;
5702 if (p_ch < 1)
5703 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005704 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005705 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 showmode();
5707}
5708
5709#ifdef FEAT_VERTSPLIT
5710/*
5711 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5712 */
5713 void
5714win_drag_vsep_line(dragwin, offset)
5715 win_T *dragwin;
5716 int offset;
5717{
5718 frame_T *curfr;
5719 frame_T *fr;
5720 int room;
5721 int left; /* if TRUE, drag separator line left, otherwise right */
5722 int n;
5723
5724 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005725 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 return;
5727 curfr = fr;
5728 fr = fr->fr_parent;
5729 /* When the parent frame is not a row of frames, its parent should be. */
5730 if (fr->fr_layout != FR_ROW)
5731 {
5732 if (fr == topframe) /* only a column of windows (cannot happen?) */
5733 return;
5734 curfr = fr;
5735 fr = fr->fr_parent;
5736 }
5737
5738 /* If this is the last frame in a row, may want to resize a parent
5739 * frame instead. */
5740 while (curfr->fr_next == NULL)
5741 {
5742 if (fr == topframe)
5743 break;
5744 curfr = fr;
5745 fr = fr->fr_parent;
5746 if (fr != topframe)
5747 {
5748 curfr = fr;
5749 fr = fr->fr_parent;
5750 }
5751 }
5752
5753 if (offset < 0) /* drag left */
5754 {
5755 left = TRUE;
5756 offset = -offset;
5757 /* sum up the room of the current frame and left of it */
5758 room = 0;
5759 for (fr = fr->fr_child; ; fr = fr->fr_next)
5760 {
5761 room += fr->fr_width - frame_minwidth(fr, NULL);
5762 if (fr == curfr)
5763 break;
5764 }
5765 fr = curfr->fr_next; /* put fr at frame that grows */
5766 }
5767 else /* drag right */
5768 {
5769 left = FALSE;
5770 /* sum up the room of frames right of the current one */
5771 room = 0;
5772 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5773 room += fr->fr_width - frame_minwidth(fr, NULL);
5774 fr = curfr; /* put fr at window that grows */
5775 }
5776
5777 if (room < offset) /* Not enough room */
5778 offset = room; /* Move as far as we can */
5779 if (offset <= 0) /* No room at all, quit. */
5780 return;
5781
5782 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005783 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 /* shrink other frames: current and at the left or at the right */
5786 if (left)
5787 fr = curfr; /* current frame gets smaller */
5788 else
5789 fr = curfr->fr_next; /* next frame gets smaller */
5790
5791 while (fr != NULL && offset > 0)
5792 {
5793 n = frame_minwidth(fr, NULL);
5794 if (fr->fr_width - offset <= n)
5795 {
5796 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005797 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799 else
5800 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005801 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 break;
5803 }
5804 if (left)
5805 fr = fr->fr_prev;
5806 else
5807 fr = fr->fr_next;
5808 }
5809 (void)win_comp_pos();
5810 redraw_all_later(NOT_VALID);
5811}
5812#endif /* FEAT_VERTSPLIT */
5813#endif /* FEAT_MOUSE */
5814
5815#endif /* FEAT_WINDOWS */
5816
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005817#define FRACTION_MULT 16384L
5818
5819/*
5820 * Set wp->w_fraction for the current w_wrow and w_height.
5821 */
5822 static void
5823set_fraction(wp)
5824 win_T *wp;
5825{
5826 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005827 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005828}
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830/*
5831 * Set the height of a window.
5832 * This takes care of the things inside the window, not what happens to the
5833 * window position, the frame or to other windows.
5834 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005835 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836win_new_height(wp, height)
5837 win_T *wp;
5838 int height;
5839{
5840 linenr_T lnum;
5841 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005842 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 /* Don't want a negative height. Happens when splitting a tiny window.
5845 * Will equalize heights soon to fix it. */
5846 if (height < 0)
5847 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005848 if (wp->w_height == height)
5849 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005851 if (wp->w_height > 0)
5852 {
5853 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005854 /* w_wrow needs to be valid. When setting 'laststatus' this may
5855 * call win_new_height() recursively. */
5856 validate_cursor();
5857 if (wp->w_height != prev_height)
5858 return; /* Recursive call already changed the size, bail out here
5859 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005860 if (wp->w_wrow != wp->w_prev_fraction_row)
5861 set_fraction(wp);
5862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864 wp->w_height = height;
5865 wp->w_skipcol = 0;
5866
5867 /* Don't change w_topline when height is zero. Don't set w_topline when
5868 * 'scrollbind' is set and this isn't the current window. */
5869 if (height > 0
5870#ifdef FEAT_SCROLLBIND
5871 && (!wp->w_p_scb || wp == curwin)
5872#endif
5873 )
5874 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005875 /*
5876 * Find a value for w_topline that shows the cursor at the same
5877 * relative position in the window as before (more or less).
5878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 lnum = wp->w_cursor.lnum;
5880 if (lnum < 1) /* can happen when starting up */
5881 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005882 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5883 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5885 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005886
5887 if (sline >= 0)
5888 {
5889 /* Make sure the whole cursor line is visible, if possible. */
5890 int rows = plines_win(wp, lnum, FALSE);
5891
5892 if (sline > wp->w_height - rows)
5893 {
5894 sline = wp->w_height - rows;
5895 wp->w_wrow -= rows - line_size;
5896 }
5897 }
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 if (sline < 0)
5900 {
5901 /*
5902 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005903 * Make cursor line the first line in the window. If not enough
5904 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 */
5906 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005907 if (wp->w_wrow >= wp->w_height
5908 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5909 {
5910 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5911 --wp->w_wrow;
5912 while (wp->w_wrow >= wp->w_height)
5913 {
5914 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5915 + win_col_off2(wp);
5916 --wp->w_wrow;
5917 }
5918 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005919 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005921 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005923 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
5925#ifdef FEAT_FOLDING
5926 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5927 if (lnum == 1)
5928 {
5929 /* first line in buffer is folded */
5930 line_size = 1;
5931 --sline;
5932 break;
5933 }
5934#endif
5935 --lnum;
5936#ifdef FEAT_DIFF
5937 if (lnum == wp->w_topline)
5938 line_size = plines_win_nofill(wp, lnum, TRUE)
5939 + wp->w_topfill;
5940 else
5941#endif
5942 line_size = plines_win(wp, lnum, TRUE);
5943 sline -= line_size;
5944 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 if (sline < 0)
5947 {
5948 /*
5949 * Line we want at top would go off top of screen. Use next
5950 * line instead.
5951 */
5952#ifdef FEAT_FOLDING
5953 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5954#endif
5955 lnum++;
5956 wp->w_wrow -= line_size + sline;
5957 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005958 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 {
5960 /* First line of file reached, use that as topline. */
5961 lnum = 1;
5962 wp->w_wrow -= sline;
5963 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005964
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005965 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 }
5968
5969 if (wp == curwin)
5970 {
5971 if (p_so)
5972 update_topline();
5973 curs_columns(FALSE); /* validate w_wrow */
5974 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005975 if (prev_height > 0)
5976 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005979 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980#ifdef FEAT_WINDOWS
5981 wp->w_redr_status = TRUE;
5982#endif
5983 invalidate_botline_win(wp);
5984}
5985
5986#ifdef FEAT_VERTSPLIT
5987/*
5988 * Set the width of a window.
5989 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005990 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991win_new_width(wp, width)
5992 win_T *wp;
5993 int width;
5994{
5995 wp->w_width = width;
5996 wp->w_lines_valid = 0;
5997 changed_line_abv_curs_win(wp);
5998 invalidate_botline_win(wp);
5999 if (wp == curwin)
6000 {
6001 update_topline();
6002 curs_columns(TRUE); /* validate w_wrow */
6003 }
6004 redraw_win_later(wp, NOT_VALID);
6005 wp->w_redr_status = TRUE;
6006}
6007#endif
6008
6009 void
6010win_comp_scroll(wp)
6011 win_T *wp;
6012{
6013 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6014 if (wp->w_p_scr == 0)
6015 wp->w_p_scr = 1;
6016}
6017
6018/*
6019 * command_height: called whenever p_ch has been changed
6020 */
6021 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006022command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024#ifdef FEAT_WINDOWS
6025 int h;
6026 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006027 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006029 /* Use the value of p_ch that we remembered. This is needed for when the
6030 * GUI starts up, we can't be sure in what order things happen. And when
6031 * p_ch was changed in another tab page. */
6032 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006033
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 /* Find bottom frame with width of screen. */
6035 frp = lastwin->w_frame;
6036# ifdef FEAT_VERTSPLIT
6037 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6038 frp = frp->fr_parent;
6039# endif
6040
6041 /* Avoid changing the height of a window with 'winfixheight' set. */
6042 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6043 && frp->fr_win->w_p_wfh)
6044 frp = frp->fr_prev;
6045
6046 if (starting != NO_SCREEN)
6047 {
6048 cmdline_row = Rows - p_ch;
6049
6050 if (p_ch > old_p_ch) /* p_ch got bigger */
6051 {
6052 while (p_ch > old_p_ch)
6053 {
6054 if (frp == NULL)
6055 {
6056 EMSG(_(e_noroom));
6057 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006058 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 cmdline_row = Rows - p_ch;
6060 break;
6061 }
6062 h = frp->fr_height - frame_minheight(frp, NULL);
6063 if (h > p_ch - old_p_ch)
6064 h = p_ch - old_p_ch;
6065 old_p_ch += h;
6066 frame_add_height(frp, -h);
6067 frp = frp->fr_prev;
6068 }
6069
6070 /* Recompute window positions. */
6071 (void)win_comp_pos();
6072
6073 /* clear the lines added to cmdline */
6074 if (full_screen)
6075 screen_fill((int)(cmdline_row), (int)Rows, 0,
6076 (int)Columns, ' ', ' ', 0);
6077 msg_row = cmdline_row;
6078 redraw_cmdline = TRUE;
6079 return;
6080 }
6081
6082 if (msg_row < cmdline_row)
6083 msg_row = cmdline_row;
6084 redraw_cmdline = TRUE;
6085 }
6086 frame_add_height(frp, (int)(old_p_ch - p_ch));
6087
6088 /* Recompute window positions. */
6089 if (frp != lastwin->w_frame)
6090 (void)win_comp_pos();
6091#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006093 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#endif
6095}
6096
6097#if defined(FEAT_WINDOWS) || defined(PROTO)
6098/*
6099 * Resize frame "frp" to be "n" lines higher (negative for less high).
6100 * Also resize the frames it is contained in.
6101 */
6102 static void
6103frame_add_height(frp, n)
6104 frame_T *frp;
6105 int n;
6106{
6107 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6108 for (;;)
6109 {
6110 frp = frp->fr_parent;
6111 if (frp == NULL)
6112 break;
6113 frp->fr_height += n;
6114 }
6115}
6116
6117/*
6118 * Add or remove a status line for the bottom window(s), according to the
6119 * value of 'laststatus'.
6120 */
6121 void
6122last_status(morewin)
6123 int morewin; /* pretend there are two or more windows */
6124{
6125 /* Don't make a difference between horizontal or vertical split. */
6126 last_status_rec(topframe, (p_ls == 2
6127 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6128}
6129
6130 static void
6131last_status_rec(fr, statusline)
6132 frame_T *fr;
6133 int statusline;
6134{
6135 frame_T *fp;
6136 win_T *wp;
6137
6138 if (fr->fr_layout == FR_LEAF)
6139 {
6140 wp = fr->fr_win;
6141 if (wp->w_status_height != 0 && !statusline)
6142 {
6143 /* remove status line */
6144 win_new_height(wp, wp->w_height + 1);
6145 wp->w_status_height = 0;
6146 comp_col();
6147 }
6148 else if (wp->w_status_height == 0 && statusline)
6149 {
6150 /* Find a frame to take a line from. */
6151 fp = fr;
6152 while (fp->fr_height <= frame_minheight(fp, NULL))
6153 {
6154 if (fp == topframe)
6155 {
6156 EMSG(_(e_noroom));
6157 return;
6158 }
6159 /* In a column of frames: go to frame above. If already at
6160 * the top or in a row of frames: go to parent. */
6161 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6162 fp = fp->fr_prev;
6163 else
6164 fp = fp->fr_parent;
6165 }
6166 wp->w_status_height = 1;
6167 if (fp != fr)
6168 {
6169 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6170 frame_fix_height(wp);
6171 (void)win_comp_pos();
6172 }
6173 else
6174 win_new_height(wp, wp->w_height - 1);
6175 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006176 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
6178 }
6179#ifdef FEAT_VERTSPLIT
6180 else if (fr->fr_layout == FR_ROW)
6181 {
6182 /* vertically split windows, set status line for each one */
6183 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6184 last_status_rec(fp, statusline);
6185 }
6186#endif
6187 else
6188 {
6189 /* horizontally split window, set status line for last one */
6190 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6191 ;
6192 last_status_rec(fp, statusline);
6193 }
6194}
6195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006197 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 */
6199 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006200tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006202#ifdef FEAT_GUI_TABLINE
6203 /* When the GUI has the tabline then this always returns zero. */
6204 if (gui_use_tabline())
6205 return 0;
6206#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006207 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006208 {
6209 case 0: return 0;
6210 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6211 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 return 1;
6213}
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215#endif /* FEAT_WINDOWS */
6216
6217#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6218/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006219 * Get the file name at the cursor.
6220 * If Visual mode is active, use the selected text if it's in one line.
6221 * Returns the name in allocated memory, NULL for failure.
6222 */
6223 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006224grab_file_name(count, file_lnum)
6225 long count;
6226 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006227{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006228 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6229
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006230 if (VIsual_active)
6231 {
6232 int len;
6233 char_u *ptr;
6234
6235 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6236 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006237 return find_file_name_in_path(ptr, len, options,
6238 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006239 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006240 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006241
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006242}
6243
6244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 * Return the file name under or after the cursor.
6246 *
6247 * The 'path' option is searched if the file name is not absolute.
6248 * The string returned has been alloc'ed and should be freed by the caller.
6249 * NULL is returned if the file name or file is not found.
6250 *
6251 * options:
6252 * FNAME_MESS give error messages
6253 * FNAME_EXP expand to path
6254 * FNAME_HYP check for hypertext link
6255 * FNAME_INCL apply "includeexpr"
6256 */
6257 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258file_name_at_cursor(options, count, file_lnum)
6259 int options;
6260 long count;
6261 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006264 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6265 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266}
6267
6268/*
6269 * Return the name of the file under or after ptr[col].
6270 * Otherwise like file_name_at_cursor().
6271 */
6272 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006273file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 char_u *line;
6275 int col;
6276 int options;
6277 long count;
6278 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006279 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
6281 char_u *ptr;
6282 int len;
6283
6284 /*
6285 * search forward for what could be the start of a file name
6286 */
6287 ptr = line + col;
6288 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006289 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (*ptr == NUL) /* nothing found */
6291 {
6292 if (options & FNAME_MESS)
6293 EMSG(_("E446: No file name under cursor"));
6294 return NULL;
6295 }
6296
6297 /*
6298 * Search backward for first char of the file name.
6299 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6300 */
6301 while (ptr > line)
6302 {
6303#ifdef FEAT_MBYTE
6304 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6305 ptr -= len + 1;
6306 else
6307#endif
6308 if (vim_isfilec(ptr[-1])
6309 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6310 --ptr;
6311 else
6312 break;
6313 }
6314
6315 /*
6316 * Search forward for the last char of the file name.
6317 * Also allow "://" when ':' is not in 'isfname'.
6318 */
6319 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006320 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006322 {
6323 if (ptr[len] == '\\')
6324 /* Skip over the "\" in "\ ". */
6325 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326#ifdef FEAT_MBYTE
6327 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006328 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 else
6330#endif
6331 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 /*
6335 * If there is trailing punctuation, remove it.
6336 * But don't remove "..", could be a directory name.
6337 */
6338 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6339 && ptr[len - 2] != '.')
6340 --len;
6341
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006342 if (file_lnum != NULL)
6343 {
6344 char_u *p;
6345
6346 /* Get the number after the file name and a separator character */
6347 p = ptr + len;
6348 p = skipwhite(p);
6349 if (*p != NUL)
6350 {
6351 if (!isdigit(*p))
6352 ++p; /* skip the separator */
6353 p = skipwhite(p);
6354 if (isdigit(*p))
6355 *file_lnum = (int)getdigits(&p);
6356 }
6357 }
6358
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6360}
6361
6362# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6363static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6364
6365 static char_u *
6366eval_includeexpr(ptr, len)
6367 char_u *ptr;
6368 int len;
6369{
6370 char_u *res;
6371
6372 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006373 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006374 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 set_vim_var_string(VV_FNAME, NULL, 0);
6376 return res;
6377}
6378#endif
6379
6380/*
6381 * Return the name of the file ptr[len] in 'path'.
6382 * Otherwise like file_name_at_cursor().
6383 */
6384 char_u *
6385find_file_name_in_path(ptr, len, options, count, rel_fname)
6386 char_u *ptr;
6387 int len;
6388 int options;
6389 long count;
6390 char_u *rel_fname; /* file we are searching relative to */
6391{
6392 char_u *file_name;
6393 int c;
6394# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6395 char_u *tofree = NULL;
6396
6397 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6398 {
6399 tofree = eval_includeexpr(ptr, len);
6400 if (tofree != NULL)
6401 {
6402 ptr = tofree;
6403 len = (int)STRLEN(ptr);
6404 }
6405 }
6406# endif
6407
6408 if (options & FNAME_EXP)
6409 {
6410 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6411 TRUE, rel_fname);
6412
6413# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6414 /*
6415 * If the file could not be found in a normal way, try applying
6416 * 'includeexpr' (unless done already).
6417 */
6418 if (file_name == NULL
6419 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6420 {
6421 tofree = eval_includeexpr(ptr, len);
6422 if (tofree != NULL)
6423 {
6424 ptr = tofree;
6425 len = (int)STRLEN(ptr);
6426 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6427 TRUE, rel_fname);
6428 }
6429 }
6430# endif
6431 if (file_name == NULL && (options & FNAME_MESS))
6432 {
6433 c = ptr[len];
6434 ptr[len] = NUL;
6435 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6436 ptr[len] = c;
6437 }
6438
6439 /* Repeat finding the file "count" times. This matters when it
6440 * appears several times in the path. */
6441 while (file_name != NULL && --count > 0)
6442 {
6443 vim_free(file_name);
6444 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6445 }
6446 }
6447 else
6448 file_name = vim_strnsave(ptr, len);
6449
6450# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6451 vim_free(tofree);
6452# endif
6453
6454 return file_name;
6455}
6456#endif /* FEAT_SEARCHPATH */
6457
6458/*
6459 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6460 * Also check for ":\\", which MS Internet Explorer accepts, return
6461 * URL_BACKSLASH.
6462 */
6463 static int
6464path_is_url(p)
6465 char_u *p;
6466{
6467 if (STRNCMP(p, "://", (size_t)3) == 0)
6468 return URL_SLASH;
6469 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6470 return URL_BACKSLASH;
6471 return 0;
6472}
6473
6474/*
6475 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6476 * Return URL_BACKSLASH for "name:\\".
6477 * Return zero otherwise.
6478 */
6479 int
6480path_with_url(fname)
6481 char_u *fname;
6482{
6483 char_u *p;
6484
6485 for (p = fname; isalpha(*p); ++p)
6486 ;
6487 return path_is_url(p);
6488}
6489
6490/*
6491 * Return TRUE if "name" is a full (absolute) path name or URL.
6492 */
6493 int
6494vim_isAbsName(name)
6495 char_u *name;
6496{
6497 return (path_with_url(name) != 0 || mch_isFullName(name));
6498}
6499
6500/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006501 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 *
6503 * return FAIL for failure, OK otherwise
6504 */
6505 int
6506vim_FullName(fname, buf, len, force)
6507 char_u *fname, *buf;
6508 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006509 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 int retval = OK;
6512 int url;
6513
6514 *buf = NUL;
6515 if (fname == NULL)
6516 return FAIL;
6517
6518 url = path_with_url(fname);
6519 if (!url)
6520 retval = mch_FullName(fname, buf, len, force);
6521 if (url || retval == FAIL)
6522 {
6523 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006524 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 }
6526#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6527 slash_adjust(buf);
6528#endif
6529 return retval;
6530}
6531
6532/*
6533 * Return the minimal number of rows that is needed on the screen to display
6534 * the current number of windows.
6535 */
6536 int
6537min_rows()
6538{
6539 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006540#ifdef FEAT_WINDOWS
6541 tabpage_T *tp;
6542 int n;
6543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
6545 if (firstwin == NULL) /* not initialized yet */
6546 return MIN_LINES;
6547
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549 total = 0;
6550 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6551 {
6552 n = frame_minheight(tp->tp_topframe, NULL);
6553 if (total < n)
6554 total = n;
6555 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006556 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006560 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 return total;
6562}
6563
6564/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006565 * Return TRUE if there is only one window (in the current tab page), not
6566 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006567 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 */
6569 int
6570only_one_window()
6571{
6572#ifdef FEAT_WINDOWS
6573 int count = 0;
6574 win_T *wp;
6575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006576 /* If there is another tab page there always is another window. */
6577 if (first_tabpage->tp_next != NULL)
6578 return FALSE;
6579
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006581 if (wp->w_buffer != NULL
6582 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583# ifdef FEAT_QUICKFIX
6584 || wp->w_p_pvw
6585# endif
6586 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006587# ifdef FEAT_AUTOCMD
6588 && wp != aucmd_win
6589# endif
6590 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 ++count;
6592 return (count <= 1);
6593#else
6594 return TRUE;
6595#endif
6596}
6597
6598#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6599/*
6600 * Correct the cursor line number in other windows. Used after changing the
6601 * current buffer, and before applying autocommands.
6602 * When "do_curwin" is TRUE, also check current window.
6603 */
6604 void
6605check_lnums(do_curwin)
6606 int do_curwin;
6607{
6608 win_T *wp;
6609
6610#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006611 tabpage_T *tp;
6612
6613 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6615#else
6616 wp = curwin;
6617 if (do_curwin)
6618#endif
6619 {
6620 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6621 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6622 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6623 wp->w_topline = curbuf->b_ml.ml_line_count;
6624 }
6625}
6626#endif
6627
6628#if defined(FEAT_WINDOWS) || defined(PROTO)
6629
6630/*
6631 * A snapshot of the window sizes, to restore them after closing the help
6632 * window.
6633 * Only these fields are used:
6634 * fr_layout
6635 * fr_width
6636 * fr_height
6637 * fr_next
6638 * fr_child
6639 * fr_win (only valid for the old curwin, NULL otherwise)
6640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
6642/*
6643 * Create a snapshot of the current frame sizes.
6644 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006645 void
6646make_snapshot(idx)
6647 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006649 clear_snapshot(curtab, idx);
6650 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651}
6652
6653 static void
6654make_snapshot_rec(fr, frp)
6655 frame_T *fr;
6656 frame_T **frp;
6657{
6658 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6659 if (*frp == NULL)
6660 return;
6661 (*frp)->fr_layout = fr->fr_layout;
6662# ifdef FEAT_VERTSPLIT
6663 (*frp)->fr_width = fr->fr_width;
6664# endif
6665 (*frp)->fr_height = fr->fr_height;
6666 if (fr->fr_next != NULL)
6667 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6668 if (fr->fr_child != NULL)
6669 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6670 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6671 (*frp)->fr_win = curwin;
6672}
6673
6674/*
6675 * Remove any existing snapshot.
6676 */
6677 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006678clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006679 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006680 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006682 clear_snapshot_rec(tp->tp_snapshot[idx]);
6683 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684}
6685
6686 static void
6687clear_snapshot_rec(fr)
6688 frame_T *fr;
6689{
6690 if (fr != NULL)
6691 {
6692 clear_snapshot_rec(fr->fr_next);
6693 clear_snapshot_rec(fr->fr_child);
6694 vim_free(fr);
6695 }
6696}
6697
6698/*
6699 * Restore a previously created snapshot, if there is any.
6700 * This is only done if the screen size didn't change and the window layout is
6701 * still the same.
6702 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006703 void
6704restore_snapshot(idx, close_curwin)
6705 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 int close_curwin; /* closing current window */
6707{
6708 win_T *wp;
6709
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006710 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006712 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006714 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6715 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006717 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 win_comp_pos();
6719 if (wp != NULL && close_curwin)
6720 win_goto(wp);
6721 redraw_all_later(CLEAR);
6722 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006723 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724}
6725
6726/*
6727 * Check if frames "sn" and "fr" have the same layout, same following frames
6728 * and same children.
6729 */
6730 static int
6731check_snapshot_rec(sn, fr)
6732 frame_T *sn;
6733 frame_T *fr;
6734{
6735 if (sn->fr_layout != fr->fr_layout
6736 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6737 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6738 || (sn->fr_next != NULL
6739 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6740 || (sn->fr_child != NULL
6741 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6742 return FAIL;
6743 return OK;
6744}
6745
6746/*
6747 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6748 * following frames and children.
6749 * Returns a pointer to the old current window, or NULL.
6750 */
6751 static win_T *
6752restore_snapshot_rec(sn, fr)
6753 frame_T *sn;
6754 frame_T *fr;
6755{
6756 win_T *wp = NULL;
6757 win_T *wp2;
6758
6759 fr->fr_height = sn->fr_height;
6760# ifdef FEAT_VERTSPLIT
6761 fr->fr_width = sn->fr_width;
6762# endif
6763 if (fr->fr_layout == FR_LEAF)
6764 {
6765 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6766# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006767 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768# endif
6769 wp = sn->fr_win;
6770 }
6771 if (sn->fr_next != NULL)
6772 {
6773 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6774 if (wp2 != NULL)
6775 wp = wp2;
6776 }
6777 if (sn->fr_child != NULL)
6778 {
6779 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6780 if (wp2 != NULL)
6781 wp = wp2;
6782 }
6783 return wp;
6784}
6785
6786#endif
6787
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006788#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6789 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006790/*
6791 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006792 * restore_win() MUST be called to undo, also when FAIL is returned.
6793 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006794 * When "no_display" is TRUE the display won't be affected, no redraw is
6795 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006796 * Returns FAIL if switching to "win" failed.
6797 */
6798 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006799switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006800 win_T **save_curwin UNUSED;
6801 tabpage_T **save_curtab UNUSED;
6802 win_T *win UNUSED;
6803 tabpage_T *tp UNUSED;
6804 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006805{
6806# ifdef FEAT_AUTOCMD
6807 block_autocmds();
6808# endif
6809# ifdef FEAT_WINDOWS
6810 *save_curwin = curwin;
6811 if (tp != NULL)
6812 {
6813 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006814 if (no_display)
6815 {
6816 curtab->tp_firstwin = firstwin;
6817 curtab->tp_lastwin = lastwin;
6818 curtab = tp;
6819 firstwin = curtab->tp_firstwin;
6820 lastwin = curtab->tp_lastwin;
6821 }
6822 else
6823 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006824 }
6825 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006826 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006827 curwin = win;
6828 curbuf = curwin->w_buffer;
6829# endif
6830 return OK;
6831}
6832
6833/*
6834 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006835 * When "no_display" is TRUE the display won't be affected, no redraw is
6836 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006837 */
6838 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006839restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006840 win_T *save_curwin UNUSED;
6841 tabpage_T *save_curtab UNUSED;
6842 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006843{
6844# ifdef FEAT_WINDOWS
6845 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006846 {
6847 if (no_display)
6848 {
6849 curtab->tp_firstwin = firstwin;
6850 curtab->tp_lastwin = lastwin;
6851 curtab = save_curtab;
6852 firstwin = curtab->tp_firstwin;
6853 lastwin = curtab->tp_lastwin;
6854 }
6855 else
6856 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6857 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006858 if (win_valid(save_curwin))
6859 {
6860 curwin = save_curwin;
6861 curbuf = curwin->w_buffer;
6862 }
6863# endif
6864# ifdef FEAT_AUTOCMD
6865 unblock_autocmds();
6866# endif
6867}
6868
6869/*
6870 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6871 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6872 */
6873 void
6874switch_buffer(save_curbuf, buf)
6875 buf_T *buf;
6876 buf_T **save_curbuf;
6877{
6878# ifdef FEAT_AUTOCMD
6879 block_autocmds();
6880# endif
6881 *save_curbuf = curbuf;
6882 --curbuf->b_nwindows;
6883 curbuf = buf;
6884 curwin->w_buffer = buf;
6885 ++curbuf->b_nwindows;
6886}
6887
6888/*
6889 * Restore the current buffer after using switch_buffer().
6890 */
6891 void
6892restore_buffer(save_curbuf)
6893 buf_T *save_curbuf;
6894{
6895# ifdef FEAT_AUTOCMD
6896 unblock_autocmds();
6897# endif
6898 /* Check for valid buffer, just in case. */
6899 if (buf_valid(save_curbuf))
6900 {
6901 --curbuf->b_nwindows;
6902 curwin->w_buffer = save_curbuf;
6903 curbuf = save_curbuf;
6904 ++curbuf->b_nwindows;
6905 }
6906}
6907#endif
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6910/*
6911 * Return TRUE if there is any vertically split window.
6912 */
6913 int
6914win_hasvertsplit()
6915{
6916 frame_T *fr;
6917
6918 if (topframe->fr_layout == FR_ROW)
6919 return TRUE;
6920
6921 if (topframe->fr_layout == FR_COL)
6922 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6923 if (fr->fr_layout == FR_ROW)
6924 return TRUE;
6925
6926 return FALSE;
6927}
6928#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006929
6930#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6931/*
6932 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006933 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006934 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6935 * If no particular ID is desired, -1 must be specified for 'id'.
6936 * Return ID of added match, -1 on failure.
6937 */
6938 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006939match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006940 win_T *wp;
6941 char_u *grp;
6942 char_u *pat;
6943 int prio;
6944 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006945 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006946{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006947 matchitem_T *cur;
6948 matchitem_T *prev;
6949 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006950 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006951 regprog_T *regprog = NULL;
6952 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006953
Bram Moolenaarb3414592014-06-17 17:48:32 +02006954 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006955 return -1;
6956 if (id < -1 || id == 0)
6957 {
6958 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6959 return -1;
6960 }
6961 if (id != -1)
6962 {
6963 cur = wp->w_match_head;
6964 while (cur != NULL)
6965 {
6966 if (cur->id == id)
6967 {
6968 EMSGN("E801: ID already taken: %ld", id);
6969 return -1;
6970 }
6971 cur = cur->next;
6972 }
6973 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006974 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006975 {
6976 EMSG2(_(e_nogroup), grp);
6977 return -1;
6978 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006979 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006980 {
6981 EMSG2(_(e_invarg2), pat);
6982 return -1;
6983 }
6984
6985 /* Find available match ID. */
6986 while (id == -1)
6987 {
6988 cur = wp->w_match_head;
6989 while (cur != NULL && cur->id != wp->w_next_match_id)
6990 cur = cur->next;
6991 if (cur == NULL)
6992 id = wp->w_next_match_id;
6993 wp->w_next_match_id++;
6994 }
6995
6996 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006997 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006998 m->id = id;
6999 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007000 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007001 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00007002 m->match.regprog = regprog;
7003 m->match.rmm_ic = FALSE;
7004 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007005
Bram Moolenaarb3414592014-06-17 17:48:32 +02007006 /* Set up position matches */
7007 if (pos_list != NULL)
7008 {
7009 linenr_T toplnum = 0;
7010 linenr_T botlnum = 0;
7011 listitem_T *li;
7012 int i;
7013
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007014 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007015 i++, li = li->li_next)
7016 {
7017 linenr_T lnum = 0;
7018 colnr_T col = 0;
7019 int len = 1;
7020 list_T *subl;
7021 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007022 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007023
Bram Moolenaarb3414592014-06-17 17:48:32 +02007024 if (li->li_tv.v_type == VAR_LIST)
7025 {
7026 subl = li->li_tv.vval.v_list;
7027 if (subl == NULL)
7028 goto fail;
7029 subli = subl->lv_first;
7030 if (subli == NULL)
7031 goto fail;
7032 lnum = get_tv_number_chk(&subli->li_tv, &error);
7033 if (error == TRUE)
7034 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007035 if (lnum == 0)
7036 {
7037 --i;
7038 continue;
7039 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007040 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007041 subli = subli->li_next;
7042 if (subli != NULL)
7043 {
7044 col = get_tv_number_chk(&subli->li_tv, &error);
7045 if (error == TRUE)
7046 goto fail;
7047 subli = subli->li_next;
7048 if (subli != NULL)
7049 {
7050 len = get_tv_number_chk(&subli->li_tv, &error);
7051 if (error == TRUE)
7052 goto fail;
7053 }
7054 }
7055 m->pos.pos[i].col = col;
7056 m->pos.pos[i].len = len;
7057 }
7058 else if (li->li_tv.v_type == VAR_NUMBER)
7059 {
7060 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007061 {
7062 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007063 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007064 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007065 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
7066 m->pos.pos[i].col = 0;
7067 m->pos.pos[i].len = 0;
7068 }
7069 else
7070 {
7071 EMSG(_("List or number required"));
7072 goto fail;
7073 }
7074 if (toplnum == 0 || lnum < toplnum)
7075 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02007076 if (botlnum == 0 || lnum >= botlnum)
7077 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007078 }
7079
7080 /* Calculate top and bottom lines for redrawing area */
7081 if (toplnum != 0)
7082 {
7083 if (wp->w_buffer->b_mod_set)
7084 {
7085 if (wp->w_buffer->b_mod_top > toplnum)
7086 wp->w_buffer->b_mod_top = toplnum;
7087 if (wp->w_buffer->b_mod_bot < botlnum)
7088 wp->w_buffer->b_mod_bot = botlnum;
7089 }
7090 else
7091 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007092 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007093 wp->w_buffer->b_mod_top = toplnum;
7094 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007095 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007096 }
7097 m->pos.toplnum = toplnum;
7098 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007099 rtype = VALID;
7100 }
7101 }
7102
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007103 /* Insert new match. The match list is in ascending order with regard to
7104 * the match priorities. */
7105 cur = wp->w_match_head;
7106 prev = cur;
7107 while (cur != NULL && prio >= cur->priority)
7108 {
7109 prev = cur;
7110 cur = cur->next;
7111 }
7112 if (cur == prev)
7113 wp->w_match_head = m;
7114 else
7115 prev->next = m;
7116 m->next = cur;
7117
Bram Moolenaarb3414592014-06-17 17:48:32 +02007118 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007119 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007120
7121fail:
7122 vim_free(m);
7123 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007124}
7125
7126/*
7127 * Delete match with ID 'id' in the match list of window 'wp'.
7128 * Print error messages if 'perr' is TRUE.
7129 */
7130 int
7131match_delete(wp, id, perr)
7132 win_T *wp;
7133 int id;
7134 int perr;
7135{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007136 matchitem_T *cur = wp->w_match_head;
7137 matchitem_T *prev = cur;
7138 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007139
7140 if (id < 1)
7141 {
7142 if (perr == TRUE)
7143 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7144 id);
7145 return -1;
7146 }
7147 while (cur != NULL && cur->id != id)
7148 {
7149 prev = cur;
7150 cur = cur->next;
7151 }
7152 if (cur == NULL)
7153 {
7154 if (perr == TRUE)
7155 EMSGN("E803: ID not found: %ld", id);
7156 return -1;
7157 }
7158 if (cur == prev)
7159 wp->w_match_head = cur->next;
7160 else
7161 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007162 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007163 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007164 if (cur->pos.toplnum != 0)
7165 {
7166 if (wp->w_buffer->b_mod_set)
7167 {
7168 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7169 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7170 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7171 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7172 }
7173 else
7174 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007175 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007176 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7177 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007178 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007179 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007180 rtype = VALID;
7181 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007182 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007183 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007184 return 0;
7185}
7186
7187/*
7188 * Delete all matches in the match list of window 'wp'.
7189 */
7190 void
7191clear_matches(wp)
7192 win_T *wp;
7193{
7194 matchitem_T *m;
7195
7196 while (wp->w_match_head != NULL)
7197 {
7198 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007199 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007200 vim_free(wp->w_match_head->pattern);
7201 vim_free(wp->w_match_head);
7202 wp->w_match_head = m;
7203 }
7204 redraw_later(SOME_VALID);
7205}
7206
7207/*
7208 * Get match from ID 'id' in window 'wp'.
7209 * Return NULL if match not found.
7210 */
7211 matchitem_T *
7212get_match(wp, id)
7213 win_T *wp;
7214 int id;
7215{
7216 matchitem_T *cur = wp->w_match_head;
7217
7218 while (cur != NULL && cur->id != id)
7219 cur = cur->next;
7220 return cur;
7221}
7222#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007223
7224#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7225 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007226get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007227{
7228 int i = 1;
7229 win_T *w;
7230
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007231 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007232 ++i;
7233
7234 if (w == NULL)
7235 return 0;
7236 else
7237 return i;
7238}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007239
7240 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007241get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007242{
7243 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007244# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007245 tabpage_T *t;
7246
7247 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7248 ++i;
7249
7250 if (t == NULL)
7251 return 0;
7252 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007253# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007254 return i;
7255}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007256#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007257
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007258#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007259/*
7260 * Return TRUE if "topfrp" and its children are at the right height.
7261 */
7262 static int
7263frame_check_height(topfrp, height)
7264 frame_T *topfrp;
7265 int height;
7266{
7267 frame_T *frp;
7268
7269 if (topfrp->fr_height != height)
7270 return FALSE;
7271
7272 if (topfrp->fr_layout == FR_ROW)
7273 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7274 if (frp->fr_height != height)
7275 return FALSE;
7276
7277 return TRUE;
7278}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007279#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007280
7281#ifdef FEAT_VERTSPLIT
7282/*
7283 * Return TRUE if "topfrp" and its children are at the right width.
7284 */
7285 static int
7286frame_check_width(topfrp, width)
7287 frame_T *topfrp;
7288 int width;
7289{
7290 frame_T *frp;
7291
7292 if (topfrp->fr_width != width)
7293 return FALSE;
7294
7295 if (topfrp->fr_layout == FR_COL)
7296 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7297 if (frp->fr_width != width)
7298 return FALSE;
7299
7300 return TRUE;
7301}
7302#endif
7303