blob: 010d7ae50aef8776df674a25e4d78252892e16e6 [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
Bram Moolenaarcde88542015-08-11 19:14:00 +0200144 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 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 Moolenaarcde88542015-08-11 19:14:00 +0200162 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 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 Moolenaarcde88542015-08-11 19:14:00 +02002589 if (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 Moolenaar40ce3a42015-04-21 18:08:39 +02004123 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004124 */
4125 void
4126tabpage_move(nr)
4127 int nr;
4128{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004129 int n = 1;
4130 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004131
4132 if (first_tabpage->tp_next == NULL)
4133 return;
4134
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004135 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4136 ++n;
4137
4138 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4139 && tp->tp_next == curtab))
4140 return;
4141
4142 tp_dst = tp;
4143
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004144 /* Remove the current tab page from the list of tab pages. */
4145 if (curtab == first_tabpage)
4146 first_tabpage = curtab->tp_next;
4147 else
4148 {
4149 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4150 if (tp->tp_next == curtab)
4151 break;
4152 if (tp == NULL) /* "cannot happen" */
4153 return;
4154 tp->tp_next = curtab->tp_next;
4155 }
4156
4157 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004158 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004159 {
4160 curtab->tp_next = first_tabpage;
4161 first_tabpage = curtab;
4162 }
4163 else
4164 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004165 curtab->tp_next = tp_dst->tp_next;
4166 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004167 }
4168
4169 /* Need to redraw the tabline. Tab page contents doesn't change. */
4170 redraw_tabline = TRUE;
4171}
4172
4173
4174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 * Go to another window.
4176 * When jumping to another buffer, stop Visual mode. Do this before
4177 * changing windows so we can yank the selection into the '*' register.
4178 * When jumping to another window on the same buffer, adjust its cursor
4179 * position to keep the same Visual area.
4180 */
4181 void
4182win_goto(wp)
4183 win_T *wp;
4184{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004185#ifdef FEAT_CONCEAL
4186 win_T *owp = curwin;
4187#endif
4188
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004189 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
4191 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004192 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return;
4194 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004195#ifdef FEAT_AUTOCMD
4196 if (curbuf_locked())
4197 return;
4198#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if (wp->w_buffer != curbuf)
4201 reset_VIsual_and_resel();
4202 else if (VIsual_active)
4203 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205#ifdef FEAT_GUI
4206 need_mouse_correct = TRUE;
4207#endif
4208 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004209
4210#ifdef FEAT_CONCEAL
4211 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004212 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004213 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004214 if (curwin->w_p_cole > 0 && !msg_scrolled)
4215 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217}
4218
4219#if defined(FEAT_PERL) || defined(PROTO)
4220/*
4221 * Find window number "winnr" (counting top to bottom).
4222 */
4223 win_T *
4224win_find_nr(winnr)
4225 int winnr;
4226{
4227 win_T *wp;
4228
4229# ifdef FEAT_WINDOWS
4230 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4231 if (--winnr == 0)
4232 break;
4233 return wp;
4234# else
4235 return curwin;
4236# endif
4237}
4238#endif
4239
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004240#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4241 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004242/*
4243 * Find the tabpage for window "win".
4244 */
4245 tabpage_T *
4246win_find_tabpage(win)
4247 win_T *win;
4248{
4249 win_T *wp;
4250 tabpage_T *tp;
4251
4252 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004253 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4254 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004255 if (wp == win)
4256 return tp;
4257 return NULL;
4258}
4259#endif
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261#ifdef FEAT_VERTSPLIT
4262/*
4263 * Move to window above or below "count" times.
4264 */
4265 static void
4266win_goto_ver(up, count)
4267 int up; /* TRUE to go to win above */
4268 long count;
4269{
4270 frame_T *fr;
4271 frame_T *nfr;
4272 frame_T *foundfr;
4273
4274 foundfr = curwin->w_frame;
4275 while (count--)
4276 {
4277 /*
4278 * First go upwards in the tree of frames until we find a upwards or
4279 * downwards neighbor.
4280 */
4281 fr = foundfr;
4282 for (;;)
4283 {
4284 if (fr == topframe)
4285 goto end;
4286 if (up)
4287 nfr = fr->fr_prev;
4288 else
4289 nfr = fr->fr_next;
4290 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4291 break;
4292 fr = fr->fr_parent;
4293 }
4294
4295 /*
4296 * Now go downwards to find the bottom or top frame in it.
4297 */
4298 for (;;)
4299 {
4300 if (nfr->fr_layout == FR_LEAF)
4301 {
4302 foundfr = nfr;
4303 break;
4304 }
4305 fr = nfr->fr_child;
4306 if (nfr->fr_layout == FR_ROW)
4307 {
4308 /* Find the frame at the cursor row. */
4309 while (fr->fr_next != NULL
4310 && frame2win(fr)->w_wincol + fr->fr_width
4311 <= curwin->w_wincol + curwin->w_wcol)
4312 fr = fr->fr_next;
4313 }
4314 if (nfr->fr_layout == FR_COL && up)
4315 while (fr->fr_next != NULL)
4316 fr = fr->fr_next;
4317 nfr = fr;
4318 }
4319 }
4320end:
4321 if (foundfr != NULL)
4322 win_goto(foundfr->fr_win);
4323}
4324
4325/*
4326 * Move to left or right window.
4327 */
4328 static void
4329win_goto_hor(left, count)
4330 int left; /* TRUE to go to left win */
4331 long count;
4332{
4333 frame_T *fr;
4334 frame_T *nfr;
4335 frame_T *foundfr;
4336
4337 foundfr = curwin->w_frame;
4338 while (count--)
4339 {
4340 /*
4341 * First go upwards in the tree of frames until we find a left or
4342 * right neighbor.
4343 */
4344 fr = foundfr;
4345 for (;;)
4346 {
4347 if (fr == topframe)
4348 goto end;
4349 if (left)
4350 nfr = fr->fr_prev;
4351 else
4352 nfr = fr->fr_next;
4353 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4354 break;
4355 fr = fr->fr_parent;
4356 }
4357
4358 /*
4359 * Now go downwards to find the leftmost or rightmost frame in it.
4360 */
4361 for (;;)
4362 {
4363 if (nfr->fr_layout == FR_LEAF)
4364 {
4365 foundfr = nfr;
4366 break;
4367 }
4368 fr = nfr->fr_child;
4369 if (nfr->fr_layout == FR_COL)
4370 {
4371 /* Find the frame at the cursor row. */
4372 while (fr->fr_next != NULL
4373 && frame2win(fr)->w_winrow + fr->fr_height
4374 <= curwin->w_winrow + curwin->w_wrow)
4375 fr = fr->fr_next;
4376 }
4377 if (nfr->fr_layout == FR_ROW && left)
4378 while (fr->fr_next != NULL)
4379 fr = fr->fr_next;
4380 nfr = fr;
4381 }
4382 }
4383end:
4384 if (foundfr != NULL)
4385 win_goto(foundfr->fr_win);
4386}
4387#endif
4388
4389/*
4390 * Make window "wp" the current window.
4391 */
4392 void
4393win_enter(wp, undo_sync)
4394 win_T *wp;
4395 int undo_sync;
4396{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004397 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398}
4399
4400/*
4401 * Make window wp the current window.
4402 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4403 * been closed and isn't valid.
4404 */
4405 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004406win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 win_T *wp;
4408 int undo_sync;
4409 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004410 int trigger_enter_autocmds UNUSED;
4411 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413#ifdef FEAT_AUTOCMD
4414 int other_buffer = FALSE;
4415#endif
4416
4417 if (wp == curwin && !curwin_invalid) /* nothing to do */
4418 return;
4419
4420#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004421 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
4423 /*
4424 * Be careful: If autocommands delete the window, return now.
4425 */
4426 if (wp->w_buffer != curbuf)
4427 {
4428 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4429 other_buffer = TRUE;
4430 if (!win_valid(wp))
4431 return;
4432 }
4433 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4434 if (!win_valid(wp))
4435 return;
4436# ifdef FEAT_EVAL
4437 /* autocmds may abort script processing */
4438 if (aborting())
4439 return;
4440# endif
4441 }
4442#endif
4443
4444 /* sync undo before leaving the current buffer */
4445 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004446 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004447
4448 /* Might need to scroll the old window before switching, e.g., when the
4449 * cursor was moved. */
4450 update_topline();
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 /* may have to copy the buffer options when 'cpo' contains 'S' */
4453 if (wp->w_buffer != curbuf)
4454 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4455 if (!curwin_invalid)
4456 {
4457 prevwin = curwin; /* remember for CTRL-W p */
4458 curwin->w_redr_status = TRUE;
4459 }
4460 curwin = wp;
4461 curbuf = wp->w_buffer;
4462 check_cursor();
4463#ifdef FEAT_VIRTUALEDIT
4464 if (!virtual_active())
4465 curwin->w_cursor.coladd = 0;
4466#endif
4467 changed_line_abv_curs(); /* assume cursor position needs updating */
4468
4469 if (curwin->w_localdir != NULL)
4470 {
4471 /* Window has a local directory: Save current directory as global
4472 * directory (unless that was done already) and change to the local
4473 * directory. */
4474 if (globaldir == NULL)
4475 {
4476 char_u cwd[MAXPATHL];
4477
4478 if (mch_dirname(cwd, MAXPATHL) == OK)
4479 globaldir = vim_strsave(cwd);
4480 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004481 if (mch_chdir((char *)curwin->w_localdir) == 0)
4482 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484 else if (globaldir != NULL)
4485 {
4486 /* Window doesn't have a local directory and we are not in the global
4487 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004488 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 vim_free(globaldir);
4490 globaldir = NULL;
4491 shorten_fnames(TRUE);
4492 }
4493
4494#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004495 if (trigger_enter_autocmds)
4496 {
4497 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4498 if (other_buffer)
4499 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
4502
4503#ifdef FEAT_TITLE
4504 maketitle();
4505#endif
4506 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004507 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 if (restart_edit)
4509 redraw_later(VALID); /* causes status line redraw */
4510
4511 /* set window height to desired minimal value */
4512 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4513 win_setheight((int)p_wh);
4514 else if (curwin->w_height == 0)
4515 win_setheight(1);
4516
4517#ifdef FEAT_VERTSPLIT
4518 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004519 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 win_setwidth((int)p_wiw);
4521#endif
4522
4523#ifdef FEAT_MOUSE
4524 setmouse(); /* in case jumped to/from help buffer */
4525#endif
4526
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004527 /* Change directories when the 'acd' option is set. */
4528 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529}
4530
4531#endif /* FEAT_WINDOWS */
4532
4533#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4534/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004535 * Jump to the first open window that contains buffer "buf", if one exists.
4536 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
4538 win_T *
4539buf_jump_open_win(buf)
4540 buf_T *buf;
4541{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004542 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004544 if (curwin->w_buffer == buf)
4545 wp = curwin;
4546# ifdef FEAT_WINDOWS
4547 else
4548 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4549 if (wp->w_buffer == buf)
4550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (wp != NULL)
4552 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004554 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004556
4557/*
4558 * Jump to the first open window in any tab page that contains buffer "buf",
4559 * if one exists.
4560 * Returns a pointer to the window found, otherwise NULL.
4561 */
4562 win_T *
4563buf_jump_open_tab(buf)
4564 buf_T *buf;
4565{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004566 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004567# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004568 tabpage_T *tp;
4569
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004570 if (wp != NULL)
4571 return wp;
4572
4573 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4574 if (tp != curtab)
4575 {
4576 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4577 if (wp->w_buffer == buf)
4578 break;
4579 if (wp != NULL)
4580 {
4581 goto_tabpage_win(tp, wp);
4582 if (curwin != wp)
4583 wp = NULL; /* something went wrong */
4584 break;
4585 }
4586 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004587# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004588 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#endif
4591
4592/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004593 * Allocate a window structure and link it in the window list when "hidden" is
4594 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004597win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004598 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004599 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004601 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 /*
4604 * allocate window structure and linesizes arrays
4605 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004606 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004607 if (new_wp == NULL)
4608 return NULL;
4609
4610 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004612 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004613 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615
Bram Moolenaar429fa852013-04-15 12:27:36 +02004616#ifdef FEAT_EVAL
4617 /* init w: variables */
4618 new_wp->w_vars = dict_alloc();
4619 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004621 win_free_lsize(new_wp);
4622 vim_free(new_wp);
4623 return NULL;
4624 }
4625 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004626#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004627
4628#ifdef FEAT_AUTOCMD
4629 /* Don't execute autocommands while the window is not properly
4630 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4631 * event. */
4632 block_autocmds();
4633#endif
4634 /*
4635 * link the window in the window list
4636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004638 if (!hidden)
4639 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#endif
4641#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004642 new_wp->w_wincol = 0;
4643 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644#endif
4645
Bram Moolenaar429fa852013-04-15 12:27:36 +02004646 /* position the display and the cursor at the top of the file. */
4647 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004649 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004651 new_wp->w_botline = 2;
4652 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004654 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#endif
4656
Bram Moolenaar429fa852013-04-15 12:27:36 +02004657 /* We won't calculate w_fraction until resizing the window */
4658 new_wp->w_fraction = 0;
4659 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004662 if (gui.in_use)
4663 {
4664 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4665 SBAR_LEFT, new_wp);
4666 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4667 SBAR_RIGHT, new_wp);
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
4670#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004671 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004673#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004674 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004675#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004676#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004677 new_wp->w_match_head = NULL;
4678 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004679#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004680 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681}
4682
4683#if defined(FEAT_WINDOWS) || defined(PROTO)
4684
4685/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004686 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 */
4688 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004689win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004691 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004694 buf_T *buf;
4695 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004697#ifdef FEAT_FOLDING
4698 clearFolding(wp);
4699#endif
4700
4701 /* reduce the reference count to the argument list. */
4702 alist_unlink(wp->w_alist);
4703
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004704#ifdef FEAT_AUTOCMD
4705 /* Don't execute autocommands while the window is halfway being deleted.
4706 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004707 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004708#endif
4709
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004710#ifdef FEAT_LUA
4711 lua_window_free(wp);
4712#endif
4713
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004714#ifdef FEAT_MZSCHEME
4715 mzscheme_window_free(wp);
4716#endif
4717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#ifdef FEAT_PERL
4719 perl_win_free(wp);
4720#endif
4721
4722#ifdef FEAT_PYTHON
4723 python_window_free(wp);
4724#endif
4725
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004726#ifdef FEAT_PYTHON3
4727 python3_window_free(wp);
4728#endif
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730#ifdef FEAT_TCL
4731 tcl_window_free(wp);
4732#endif
4733
4734#ifdef FEAT_RUBY
4735 ruby_window_free(wp);
4736#endif
4737
4738 clear_winopt(&wp->w_onebuf_opt);
4739 clear_winopt(&wp->w_allbuf_opt);
4740
4741#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004742 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4743 hash_init(&wp->w_vars->dv_hashtab);
4744 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745#endif
4746
4747 if (prevwin == wp)
4748 prevwin = NULL;
4749 win_free_lsize(wp);
4750
4751 for (i = 0; i < wp->w_tagstacklen; ++i)
4752 vim_free(wp->w_tagstack[i].tagname);
4753
4754 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004755
Bram Moolenaarff18df02013-07-24 17:51:57 +02004756 /* Remove the window from the b_wininfo lists, it may happen that the
4757 * freed memory is re-used for another window. */
4758 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4759 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4760 if (wip->wi_win == wp)
4761 wip->wi_win = NULL;
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004764 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#ifdef FEAT_JUMPLIST
4768 free_jumplist(wp);
4769#endif
4770
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004771#ifdef FEAT_QUICKFIX
4772 qf_free_all(wp);
4773#endif
4774
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#ifdef FEAT_GUI
4776 if (gui.in_use)
4777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4779 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4780 }
4781#endif /* FEAT_GUI */
4782
Bram Moolenaar860cae12010-06-05 23:22:07 +02004783#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004784 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004785#endif
4786
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004787#ifdef FEAT_AUTOCMD
4788 if (wp != aucmd_win)
4789#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004790 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004791#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004792 if (autocmd_busy)
4793 {
4794 wp->w_next = au_pending_free_win;
4795 au_pending_free_win = wp;
4796 }
4797 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004798#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004799 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004800
4801#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004802 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804}
4805
4806/*
4807 * Append window "wp" in the window list after window "after".
4808 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004809 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810win_append(after, wp)
4811 win_T *after, *wp;
4812{
4813 win_T *before;
4814
4815 if (after == NULL) /* after NULL is in front of the first */
4816 before = firstwin;
4817 else
4818 before = after->w_next;
4819
4820 wp->w_next = before;
4821 wp->w_prev = after;
4822 if (after == NULL)
4823 firstwin = wp;
4824 else
4825 after->w_next = wp;
4826 if (before == NULL)
4827 lastwin = wp;
4828 else
4829 before->w_prev = wp;
4830}
4831
4832/*
4833 * Remove a window from the window list.
4834 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004835 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004836win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004838 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 if (wp->w_prev != NULL)
4841 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004842 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004844 else
4845 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (wp->w_next != NULL)
4847 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004848 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004850 else
4851 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852}
4853
4854/*
4855 * Append frame "frp" in a frame list after frame "after".
4856 */
4857 static void
4858frame_append(after, frp)
4859 frame_T *after, *frp;
4860{
4861 frp->fr_next = after->fr_next;
4862 after->fr_next = frp;
4863 if (frp->fr_next != NULL)
4864 frp->fr_next->fr_prev = frp;
4865 frp->fr_prev = after;
4866}
4867
4868/*
4869 * Insert frame "frp" in a frame list before frame "before".
4870 */
4871 static void
4872frame_insert(before, frp)
4873 frame_T *before, *frp;
4874{
4875 frp->fr_next = before;
4876 frp->fr_prev = before->fr_prev;
4877 before->fr_prev = frp;
4878 if (frp->fr_prev != NULL)
4879 frp->fr_prev->fr_next = frp;
4880 else
4881 frp->fr_parent->fr_child = frp;
4882}
4883
4884/*
4885 * Remove a frame from a frame list.
4886 */
4887 static void
4888frame_remove(frp)
4889 frame_T *frp;
4890{
4891 if (frp->fr_prev != NULL)
4892 frp->fr_prev->fr_next = frp->fr_next;
4893 else
4894 frp->fr_parent->fr_child = frp->fr_next;
4895 if (frp->fr_next != NULL)
4896 frp->fr_next->fr_prev = frp->fr_prev;
4897}
4898
4899#endif /* FEAT_WINDOWS */
4900
4901/*
4902 * Allocate w_lines[] for window "wp".
4903 * Return FAIL for failure, OK for success.
4904 */
4905 int
4906win_alloc_lines(wp)
4907 win_T *wp;
4908{
4909 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004910 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (wp->w_lines == NULL)
4912 return FAIL;
4913 return OK;
4914}
4915
4916/*
4917 * free lsize arrays for a window
4918 */
4919 void
4920win_free_lsize(wp)
4921 win_T *wp;
4922{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004923 /* TODO: why would wp be NULL here? */
4924 if (wp != NULL)
4925 {
4926 vim_free(wp->w_lines);
4927 wp->w_lines = NULL;
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930
4931/*
4932 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004933 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
4935 void
4936shell_new_rows()
4937{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004938 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (firstwin == NULL) /* not initialized yet */
4941 return;
4942#ifdef FEAT_WINDOWS
4943 if (h < frame_minheight(topframe, NULL))
4944 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004945
4946 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * that doesn't result in the right height, forget about that option. */
4948 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004949 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 frame_new_height(topframe, h, FALSE, FALSE);
4951
4952 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4953#else
4954 if (h < 1)
4955 h = 1;
4956 win_new_height(firstwin, h);
4957#endif
4958 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004959#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004960 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004961#endif
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963#if 0
4964 /* Disabled: don't want making the screen smaller make a window larger. */
4965 if (p_ea)
4966 win_equal(curwin, FALSE, 'v');
4967#endif
4968}
4969
4970#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4971/*
4972 * Called from win_new_shellsize() after Columns changed.
4973 */
4974 void
4975shell_new_columns()
4976{
4977 if (firstwin == NULL) /* not initialized yet */
4978 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004979
4980 /* First try setting the widths of windows with 'winfixwidth'. If that
4981 * doesn't result in the right width, forget about that option. */
4982 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004983 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004984 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4987#if 0
4988 /* Disabled: don't want making the screen smaller make a window larger. */
4989 if (p_ea)
4990 win_equal(curwin, FALSE, 'h');
4991#endif
4992}
4993#endif
4994
4995#if defined(FEAT_CMDWIN) || defined(PROTO)
4996/*
4997 * Save the size of all windows in "gap".
4998 */
4999 void
5000win_size_save(gap)
5001 garray_T *gap;
5002
5003{
5004 win_T *wp;
5005
5006 ga_init2(gap, (int)sizeof(int), 1);
5007 if (ga_grow(gap, win_count() * 2) == OK)
5008 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5009 {
5010 ((int *)gap->ga_data)[gap->ga_len++] =
5011 wp->w_width + wp->w_vsep_width;
5012 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5013 }
5014}
5015
5016/*
5017 * Restore window sizes, but only if the number of windows is still the same.
5018 * Does not free the growarray.
5019 */
5020 void
5021win_size_restore(gap)
5022 garray_T *gap;
5023{
5024 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005025 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 if (win_count() * 2 == gap->ga_len)
5028 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005029 /* The order matters, because frames contain other frames, but it's
5030 * difficult to get right. The easy way out is to do it twice. */
5031 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005033 i = 0;
5034 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5035 {
5036 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5037 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 /* recompute the window positions */
5041 (void)win_comp_pos();
5042 }
5043}
5044#endif /* FEAT_CMDWIN */
5045
5046#if defined(FEAT_WINDOWS) || defined(PROTO)
5047/*
5048 * Update the position for all windows, using the width and height of the
5049 * frames.
5050 * Returns the row just after the last window.
5051 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005052 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053win_comp_pos()
5054{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005055 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int col = 0;
5057
5058 frame_comp_pos(topframe, &row, &col);
5059 return row;
5060}
5061
5062/*
5063 * Update the position of the windows in frame "topfrp", using the width and
5064 * height of the frames.
5065 * "*row" and "*col" are the top-left position of the frame. They are updated
5066 * to the bottom-right position plus one.
5067 */
5068 static void
5069frame_comp_pos(topfrp, row, col)
5070 frame_T *topfrp;
5071 int *row;
5072 int *col;
5073{
5074 win_T *wp;
5075 frame_T *frp;
5076#ifdef FEAT_VERTSPLIT
5077 int startcol;
5078 int startrow;
5079#endif
5080
5081 wp = topfrp->fr_win;
5082 if (wp != NULL)
5083 {
5084 if (wp->w_winrow != *row
5085#ifdef FEAT_VERTSPLIT
5086 || wp->w_wincol != *col
5087#endif
5088 )
5089 {
5090 /* position changed, redraw */
5091 wp->w_winrow = *row;
5092#ifdef FEAT_VERTSPLIT
5093 wp->w_wincol = *col;
5094#endif
5095 redraw_win_later(wp, NOT_VALID);
5096 wp->w_redr_status = TRUE;
5097 }
5098 *row += wp->w_height + wp->w_status_height;
5099#ifdef FEAT_VERTSPLIT
5100 *col += wp->w_width + wp->w_vsep_width;
5101#endif
5102 }
5103 else
5104 {
5105#ifdef FEAT_VERTSPLIT
5106 startrow = *row;
5107 startcol = *col;
5108#endif
5109 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5110 {
5111#ifdef FEAT_VERTSPLIT
5112 if (topfrp->fr_layout == FR_ROW)
5113 *row = startrow; /* all frames are at the same row */
5114 else
5115 *col = startcol; /* all frames are at the same col */
5116#endif
5117 frame_comp_pos(frp, row, col);
5118 }
5119 }
5120}
5121
5122#endif /* FEAT_WINDOWS */
5123
5124/*
5125 * Set current window height and take care of repositioning other windows to
5126 * fit around it.
5127 */
5128 void
5129win_setheight(height)
5130 int height;
5131{
5132 win_setheight_win(height, curwin);
5133}
5134
5135/*
5136 * Set the window height of window "win" and take care of repositioning other
5137 * windows to fit around it.
5138 */
5139 void
5140win_setheight_win(height, win)
5141 int height;
5142 win_T *win;
5143{
5144 int row;
5145
5146 if (win == curwin)
5147 {
5148 /* Always keep current window at least one line high, even when
5149 * 'winminheight' is zero. */
5150#ifdef FEAT_WINDOWS
5151 if (height < p_wmh)
5152 height = p_wmh;
5153#endif
5154 if (height == 0)
5155 height = 1;
5156 }
5157
5158#ifdef FEAT_WINDOWS
5159 frame_setheight(win->w_frame, height + win->w_status_height);
5160
5161 /* recompute the window positions */
5162 row = win_comp_pos();
5163#else
5164 if (height > topframe->fr_height)
5165 height = topframe->fr_height;
5166 win->w_height = height;
5167 row = height;
5168#endif
5169
5170 /*
5171 * If there is extra space created between the last window and the command
5172 * line, clear it.
5173 */
5174 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5175 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5176 cmdline_row = row;
5177 msg_row = row;
5178 msg_col = 0;
5179
5180 redraw_all_later(NOT_VALID);
5181}
5182
5183#if defined(FEAT_WINDOWS) || defined(PROTO)
5184
5185/*
5186 * Set the height of a frame to "height" and take care that all frames and
5187 * windows inside it are resized. Also resize frames on the left and right if
5188 * the are in the same FR_ROW frame.
5189 *
5190 * Strategy:
5191 * If the frame is part of a FR_COL frame, try fitting the frame in that
5192 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5193 * go to containing frames to resize them and make room.
5194 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5195 * Check for the minimal height of the FR_ROW frame.
5196 * At the top level we can also use change the command line height.
5197 */
5198 static void
5199frame_setheight(curfrp, height)
5200 frame_T *curfrp;
5201 int height;
5202{
5203 int room; /* total number of lines available */
5204 int take; /* number of lines taken from other windows */
5205 int room_cmdline; /* lines available from cmdline */
5206 int run;
5207 frame_T *frp;
5208 int h;
5209 int room_reserved;
5210
5211 /* If the height already is the desired value, nothing to do. */
5212 if (curfrp->fr_height == height)
5213 return;
5214
5215 if (curfrp->fr_parent == NULL)
5216 {
5217 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005218 if (height > ROWS_AVAIL)
5219 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (height > 0)
5221 frame_new_height(curfrp, height, FALSE, FALSE);
5222 }
5223 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5224 {
5225 /* Row of frames: Also need to resize frames left and right of this
5226 * one. First check for the minimal height of these. */
5227 h = frame_minheight(curfrp->fr_parent, NULL);
5228 if (height < h)
5229 height = h;
5230 frame_setheight(curfrp->fr_parent, height);
5231 }
5232 else
5233 {
5234 /*
5235 * Column of frames: try to change only frames in this column.
5236 */
5237#ifdef FEAT_VERTSPLIT
5238 /*
5239 * Do this twice:
5240 * 1: compute room available, if it's not enough try resizing the
5241 * containing frame.
5242 * 2: compute the room available and adjust the height to it.
5243 * Try not to reduce the height of a window with 'winfixheight' set.
5244 */
5245 for (run = 1; run <= 2; ++run)
5246#else
5247 for (;;)
5248#endif
5249 {
5250 room = 0;
5251 room_reserved = 0;
5252 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5253 frp = frp->fr_next)
5254 {
5255 if (frp != curfrp
5256 && frp->fr_win != NULL
5257 && frp->fr_win->w_p_wfh)
5258 room_reserved += frp->fr_height;
5259 room += frp->fr_height;
5260 if (frp != curfrp)
5261 room -= frame_minheight(frp, NULL);
5262 }
5263#ifdef FEAT_VERTSPLIT
5264 if (curfrp->fr_width != Columns)
5265 room_cmdline = 0;
5266 else
5267#endif
5268 {
5269 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5270 + lastwin->w_height + lastwin->w_status_height);
5271 if (room_cmdline < 0)
5272 room_cmdline = 0;
5273 }
5274
5275 if (height <= room + room_cmdline)
5276 break;
5277#ifdef FEAT_VERTSPLIT
5278 if (run == 2 || curfrp->fr_width == Columns)
5279#endif
5280 {
5281 if (height > room + room_cmdline)
5282 height = room + room_cmdline;
5283 break;
5284 }
5285#ifdef FEAT_VERTSPLIT
5286 frame_setheight(curfrp->fr_parent, height
5287 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5288#endif
5289 /*NOTREACHED*/
5290 }
5291
5292 /*
5293 * Compute the number of lines we will take from others frames (can be
5294 * negative!).
5295 */
5296 take = height - curfrp->fr_height;
5297
5298 /* If there is not enough room, also reduce the height of a window
5299 * with 'winfixheight' set. */
5300 if (height > room + room_cmdline - room_reserved)
5301 room_reserved = room + room_cmdline - height;
5302 /* If there is only a 'winfixheight' window and making the
5303 * window smaller, need to make the other window taller. */
5304 if (take < 0 && room - curfrp->fr_height < room_reserved)
5305 room_reserved = 0;
5306
5307 if (take > 0 && room_cmdline > 0)
5308 {
5309 /* use lines from cmdline first */
5310 if (take < room_cmdline)
5311 room_cmdline = take;
5312 take -= room_cmdline;
5313 topframe->fr_height += room_cmdline;
5314 }
5315
5316 /*
5317 * set the current frame to the new height
5318 */
5319 frame_new_height(curfrp, height, FALSE, FALSE);
5320
5321 /*
5322 * First take lines from the frames after the current frame. If
5323 * that is not enough, takes lines from frames above the current
5324 * frame.
5325 */
5326 for (run = 0; run < 2; ++run)
5327 {
5328 if (run == 0)
5329 frp = curfrp->fr_next; /* 1st run: start with next window */
5330 else
5331 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5332 while (frp != NULL && take != 0)
5333 {
5334 h = frame_minheight(frp, NULL);
5335 if (room_reserved > 0
5336 && frp->fr_win != NULL
5337 && frp->fr_win->w_p_wfh)
5338 {
5339 if (room_reserved >= frp->fr_height)
5340 room_reserved -= frp->fr_height;
5341 else
5342 {
5343 if (frp->fr_height - room_reserved > take)
5344 room_reserved = frp->fr_height - take;
5345 take -= frp->fr_height - room_reserved;
5346 frame_new_height(frp, room_reserved, FALSE, FALSE);
5347 room_reserved = 0;
5348 }
5349 }
5350 else
5351 {
5352 if (frp->fr_height - take < h)
5353 {
5354 take -= frp->fr_height - h;
5355 frame_new_height(frp, h, FALSE, FALSE);
5356 }
5357 else
5358 {
5359 frame_new_height(frp, frp->fr_height - take,
5360 FALSE, FALSE);
5361 take = 0;
5362 }
5363 }
5364 if (run == 0)
5365 frp = frp->fr_next;
5366 else
5367 frp = frp->fr_prev;
5368 }
5369 }
5370 }
5371}
5372
5373#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5374/*
5375 * Set current window width and take care of repositioning other windows to
5376 * fit around it.
5377 */
5378 void
5379win_setwidth(width)
5380 int width;
5381{
5382 win_setwidth_win(width, curwin);
5383}
5384
5385 void
5386win_setwidth_win(width, wp)
5387 int width;
5388 win_T *wp;
5389{
5390 /* Always keep current window at least one column wide, even when
5391 * 'winminwidth' is zero. */
5392 if (wp == curwin)
5393 {
5394 if (width < p_wmw)
5395 width = p_wmw;
5396 if (width == 0)
5397 width = 1;
5398 }
5399
5400 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5401
5402 /* recompute the window positions */
5403 (void)win_comp_pos();
5404
5405 redraw_all_later(NOT_VALID);
5406}
5407
5408/*
5409 * Set the width of a frame to "width" and take care that all frames and
5410 * windows inside it are resized. Also resize frames above and below if the
5411 * are in the same FR_ROW frame.
5412 *
5413 * Strategy is similar to frame_setheight().
5414 */
5415 static void
5416frame_setwidth(curfrp, width)
5417 frame_T *curfrp;
5418 int width;
5419{
5420 int room; /* total number of lines available */
5421 int take; /* number of lines taken from other windows */
5422 int run;
5423 frame_T *frp;
5424 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005425 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 /* If the width already is the desired value, nothing to do. */
5428 if (curfrp->fr_width == width)
5429 return;
5430
5431 if (curfrp->fr_parent == NULL)
5432 /* topframe: can't change width */
5433 return;
5434
5435 if (curfrp->fr_parent->fr_layout == FR_COL)
5436 {
5437 /* Column of frames: Also need to resize frames above and below of
5438 * this one. First check for the minimal width of these. */
5439 w = frame_minwidth(curfrp->fr_parent, NULL);
5440 if (width < w)
5441 width = w;
5442 frame_setwidth(curfrp->fr_parent, width);
5443 }
5444 else
5445 {
5446 /*
5447 * Row of frames: try to change only frames in this row.
5448 *
5449 * Do this twice:
5450 * 1: compute room available, if it's not enough try resizing the
5451 * containing frame.
5452 * 2: compute the room available and adjust the width to it.
5453 */
5454 for (run = 1; run <= 2; ++run)
5455 {
5456 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005457 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5459 frp = frp->fr_next)
5460 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005461 if (frp != curfrp
5462 && frp->fr_win != NULL
5463 && frp->fr_win->w_p_wfw)
5464 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 room += frp->fr_width;
5466 if (frp != curfrp)
5467 room -= frame_minwidth(frp, NULL);
5468 }
5469
5470 if (width <= room)
5471 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005472 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 {
5474 if (width > room)
5475 width = room;
5476 break;
5477 }
5478 frame_setwidth(curfrp->fr_parent, width
5479 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5480 }
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 /*
5483 * Compute the number of lines we will take from others frames (can be
5484 * negative!).
5485 */
5486 take = width - curfrp->fr_width;
5487
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005488 /* If there is not enough room, also reduce the width of a window
5489 * with 'winfixwidth' set. */
5490 if (width > room - room_reserved)
5491 room_reserved = room - width;
5492 /* If there is only a 'winfixwidth' window and making the
5493 * window smaller, need to make the other window narrower. */
5494 if (take < 0 && room - curfrp->fr_width < room_reserved)
5495 room_reserved = 0;
5496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 /*
5498 * set the current frame to the new width
5499 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005500 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 /*
5503 * First take lines from the frames right of the current frame. If
5504 * that is not enough, takes lines from frames left of the current
5505 * frame.
5506 */
5507 for (run = 0; run < 2; ++run)
5508 {
5509 if (run == 0)
5510 frp = curfrp->fr_next; /* 1st run: start with next window */
5511 else
5512 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5513 while (frp != NULL && take != 0)
5514 {
5515 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005516 if (room_reserved > 0
5517 && frp->fr_win != NULL
5518 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005520 if (room_reserved >= frp->fr_width)
5521 room_reserved -= frp->fr_width;
5522 else
5523 {
5524 if (frp->fr_width - room_reserved > take)
5525 room_reserved = frp->fr_width - take;
5526 take -= frp->fr_width - room_reserved;
5527 frame_new_width(frp, room_reserved, FALSE, FALSE);
5528 room_reserved = 0;
5529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531 else
5532 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005533 if (frp->fr_width - take < w)
5534 {
5535 take -= frp->fr_width - w;
5536 frame_new_width(frp, w, FALSE, FALSE);
5537 }
5538 else
5539 {
5540 frame_new_width(frp, frp->fr_width - take,
5541 FALSE, FALSE);
5542 take = 0;
5543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545 if (run == 0)
5546 frp = frp->fr_next;
5547 else
5548 frp = frp->fr_prev;
5549 }
5550 }
5551 }
5552}
5553#endif /* FEAT_VERTSPLIT */
5554
5555/*
5556 * Check 'winminheight' for a valid value.
5557 */
5558 void
5559win_setminheight()
5560{
5561 int room;
5562 int first = TRUE;
5563 win_T *wp;
5564
5565 /* loop until there is a 'winminheight' that is possible */
5566 while (p_wmh > 0)
5567 {
5568 /* TODO: handle vertical splits */
5569 room = -p_wh;
5570 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5571 room += wp->w_height - p_wmh;
5572 if (room >= 0)
5573 break;
5574 --p_wmh;
5575 if (first)
5576 {
5577 EMSG(_(e_noroom));
5578 first = FALSE;
5579 }
5580 }
5581}
5582
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005583#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
5585/*
5586 * Status line of dragwin is dragged "offset" lines down (negative is up).
5587 */
5588 void
5589win_drag_status_line(dragwin, offset)
5590 win_T *dragwin;
5591 int offset;
5592{
5593 frame_T *curfr;
5594 frame_T *fr;
5595 int room;
5596 int row;
5597 int up; /* if TRUE, drag status line up, otherwise down */
5598 int n;
5599
5600 fr = dragwin->w_frame;
5601 curfr = fr;
5602 if (fr != topframe) /* more than one window */
5603 {
5604 fr = fr->fr_parent;
5605 /* When the parent frame is not a column of frames, its parent should
5606 * be. */
5607 if (fr->fr_layout != FR_COL)
5608 {
5609 curfr = fr;
5610 if (fr != topframe) /* only a row of windows, may drag statusline */
5611 fr = fr->fr_parent;
5612 }
5613 }
5614
5615 /* If this is the last frame in a column, may want to resize the parent
5616 * frame instead (go two up to skip a row of frames). */
5617 while (curfr != topframe && curfr->fr_next == NULL)
5618 {
5619 if (fr != topframe)
5620 fr = fr->fr_parent;
5621 curfr = fr;
5622 if (fr != topframe)
5623 fr = fr->fr_parent;
5624 }
5625
5626 if (offset < 0) /* drag up */
5627 {
5628 up = TRUE;
5629 offset = -offset;
5630 /* sum up the room of the current frame and above it */
5631 if (fr == curfr)
5632 {
5633 /* only one window */
5634 room = fr->fr_height - frame_minheight(fr, NULL);
5635 }
5636 else
5637 {
5638 room = 0;
5639 for (fr = fr->fr_child; ; fr = fr->fr_next)
5640 {
5641 room += fr->fr_height - frame_minheight(fr, NULL);
5642 if (fr == curfr)
5643 break;
5644 }
5645 }
5646 fr = curfr->fr_next; /* put fr at frame that grows */
5647 }
5648 else /* drag down */
5649 {
5650 up = FALSE;
5651 /*
5652 * Only dragging the last status line can reduce p_ch.
5653 */
5654 room = Rows - cmdline_row;
5655 if (curfr->fr_next == NULL)
5656 room -= 1;
5657 else
5658 room -= p_ch;
5659 if (room < 0)
5660 room = 0;
5661 /* sum up the room of frames below of the current one */
5662 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5663 room += fr->fr_height - frame_minheight(fr, NULL);
5664 fr = curfr; /* put fr at window that grows */
5665 }
5666
5667 if (room < offset) /* Not enough room */
5668 offset = room; /* Move as far as we can */
5669 if (offset <= 0)
5670 return;
5671
5672 /*
5673 * Grow frame fr by "offset" lines.
5674 * Doesn't happen when dragging the last status line up.
5675 */
5676 if (fr != NULL)
5677 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5678
5679 if (up)
5680 fr = curfr; /* current frame gets smaller */
5681 else
5682 fr = curfr->fr_next; /* next frame gets smaller */
5683
5684 /*
5685 * Now make the other frames smaller.
5686 */
5687 while (fr != NULL && offset > 0)
5688 {
5689 n = frame_minheight(fr, NULL);
5690 if (fr->fr_height - offset <= n)
5691 {
5692 offset -= fr->fr_height - n;
5693 frame_new_height(fr, n, !up, FALSE);
5694 }
5695 else
5696 {
5697 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5698 break;
5699 }
5700 if (up)
5701 fr = fr->fr_prev;
5702 else
5703 fr = fr->fr_next;
5704 }
5705 row = win_comp_pos();
5706 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5707 cmdline_row = row;
5708 p_ch = Rows - cmdline_row;
5709 if (p_ch < 1)
5710 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005711 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005712 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 showmode();
5714}
5715
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005716# if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717/*
5718 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5719 */
5720 void
5721win_drag_vsep_line(dragwin, offset)
5722 win_T *dragwin;
5723 int offset;
5724{
5725 frame_T *curfr;
5726 frame_T *fr;
5727 int room;
5728 int left; /* if TRUE, drag separator line left, otherwise right */
5729 int n;
5730
5731 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005732 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 return;
5734 curfr = fr;
5735 fr = fr->fr_parent;
5736 /* When the parent frame is not a row of frames, its parent should be. */
5737 if (fr->fr_layout != FR_ROW)
5738 {
5739 if (fr == topframe) /* only a column of windows (cannot happen?) */
5740 return;
5741 curfr = fr;
5742 fr = fr->fr_parent;
5743 }
5744
5745 /* If this is the last frame in a row, may want to resize a parent
5746 * frame instead. */
5747 while (curfr->fr_next == NULL)
5748 {
5749 if (fr == topframe)
5750 break;
5751 curfr = fr;
5752 fr = fr->fr_parent;
5753 if (fr != topframe)
5754 {
5755 curfr = fr;
5756 fr = fr->fr_parent;
5757 }
5758 }
5759
5760 if (offset < 0) /* drag left */
5761 {
5762 left = TRUE;
5763 offset = -offset;
5764 /* sum up the room of the current frame and left of it */
5765 room = 0;
5766 for (fr = fr->fr_child; ; fr = fr->fr_next)
5767 {
5768 room += fr->fr_width - frame_minwidth(fr, NULL);
5769 if (fr == curfr)
5770 break;
5771 }
5772 fr = curfr->fr_next; /* put fr at frame that grows */
5773 }
5774 else /* drag right */
5775 {
5776 left = FALSE;
5777 /* sum up the room of frames right of the current one */
5778 room = 0;
5779 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5780 room += fr->fr_width - frame_minwidth(fr, NULL);
5781 fr = curfr; /* put fr at window that grows */
5782 }
5783
5784 if (room < offset) /* Not enough room */
5785 offset = room; /* Move as far as we can */
5786 if (offset <= 0) /* No room at all, quit. */
5787 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005788 if (fr == NULL)
5789 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005792 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 /* shrink other frames: current and at the left or at the right */
5795 if (left)
5796 fr = curfr; /* current frame gets smaller */
5797 else
5798 fr = curfr->fr_next; /* next frame gets smaller */
5799
5800 while (fr != NULL && offset > 0)
5801 {
5802 n = frame_minwidth(fr, NULL);
5803 if (fr->fr_width - offset <= n)
5804 {
5805 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005806 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808 else
5809 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005810 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 break;
5812 }
5813 if (left)
5814 fr = fr->fr_prev;
5815 else
5816 fr = fr->fr_next;
5817 }
5818 (void)win_comp_pos();
5819 redraw_all_later(NOT_VALID);
5820}
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005821# endif /* FEAT_VERTSPLIT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822#endif /* FEAT_MOUSE */
5823
5824#endif /* FEAT_WINDOWS */
5825
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005826#define FRACTION_MULT 16384L
5827
5828/*
5829 * Set wp->w_fraction for the current w_wrow and w_height.
5830 */
5831 static void
5832set_fraction(wp)
5833 win_T *wp;
5834{
5835 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005836 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005837}
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839/*
5840 * Set the height of a window.
5841 * This takes care of the things inside the window, not what happens to the
5842 * window position, the frame or to other windows.
5843 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005844 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845win_new_height(wp, height)
5846 win_T *wp;
5847 int height;
5848{
5849 linenr_T lnum;
5850 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005851 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 /* Don't want a negative height. Happens when splitting a tiny window.
5854 * Will equalize heights soon to fix it. */
5855 if (height < 0)
5856 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005857 if (wp->w_height == height)
5858 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005860 if (wp->w_height > 0)
5861 {
5862 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005863 /* w_wrow needs to be valid. When setting 'laststatus' this may
5864 * call win_new_height() recursively. */
5865 validate_cursor();
5866 if (wp->w_height != prev_height)
5867 return; /* Recursive call already changed the size, bail out here
5868 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005869 if (wp->w_wrow != wp->w_prev_fraction_row)
5870 set_fraction(wp);
5871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
5873 wp->w_height = height;
5874 wp->w_skipcol = 0;
5875
5876 /* Don't change w_topline when height is zero. Don't set w_topline when
5877 * 'scrollbind' is set and this isn't the current window. */
5878 if (height > 0
5879#ifdef FEAT_SCROLLBIND
5880 && (!wp->w_p_scb || wp == curwin)
5881#endif
5882 )
5883 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005884 /*
5885 * Find a value for w_topline that shows the cursor at the same
5886 * relative position in the window as before (more or less).
5887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 lnum = wp->w_cursor.lnum;
5889 if (lnum < 1) /* can happen when starting up */
5890 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005891 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5892 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5894 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005895
5896 if (sline >= 0)
5897 {
5898 /* Make sure the whole cursor line is visible, if possible. */
5899 int rows = plines_win(wp, lnum, FALSE);
5900
5901 if (sline > wp->w_height - rows)
5902 {
5903 sline = wp->w_height - rows;
5904 wp->w_wrow -= rows - line_size;
5905 }
5906 }
5907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (sline < 0)
5909 {
5910 /*
5911 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005912 * Make cursor line the first line in the window. If not enough
5913 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 */
5915 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005916 if (wp->w_wrow >= wp->w_height
5917 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5918 {
5919 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5920 --wp->w_wrow;
5921 while (wp->w_wrow >= wp->w_height)
5922 {
5923 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5924 + win_col_off2(wp);
5925 --wp->w_wrow;
5926 }
5927 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005928 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005930 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005932 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 {
5934#ifdef FEAT_FOLDING
5935 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5936 if (lnum == 1)
5937 {
5938 /* first line in buffer is folded */
5939 line_size = 1;
5940 --sline;
5941 break;
5942 }
5943#endif
5944 --lnum;
5945#ifdef FEAT_DIFF
5946 if (lnum == wp->w_topline)
5947 line_size = plines_win_nofill(wp, lnum, TRUE)
5948 + wp->w_topfill;
5949 else
5950#endif
5951 line_size = plines_win(wp, lnum, TRUE);
5952 sline -= line_size;
5953 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (sline < 0)
5956 {
5957 /*
5958 * Line we want at top would go off top of screen. Use next
5959 * line instead.
5960 */
5961#ifdef FEAT_FOLDING
5962 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5963#endif
5964 lnum++;
5965 wp->w_wrow -= line_size + sline;
5966 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005967 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 {
5969 /* First line of file reached, use that as topline. */
5970 lnum = 1;
5971 wp->w_wrow -= sline;
5972 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005973
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005974 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
5977
5978 if (wp == curwin)
5979 {
5980 if (p_so)
5981 update_topline();
5982 curs_columns(FALSE); /* validate w_wrow */
5983 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005984 if (prev_height > 0)
5985 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005988 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#ifdef FEAT_WINDOWS
5990 wp->w_redr_status = TRUE;
5991#endif
5992 invalidate_botline_win(wp);
5993}
5994
5995#ifdef FEAT_VERTSPLIT
5996/*
5997 * Set the width of a window.
5998 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005999 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000win_new_width(wp, width)
6001 win_T *wp;
6002 int width;
6003{
6004 wp->w_width = width;
6005 wp->w_lines_valid = 0;
6006 changed_line_abv_curs_win(wp);
6007 invalidate_botline_win(wp);
6008 if (wp == curwin)
6009 {
6010 update_topline();
6011 curs_columns(TRUE); /* validate w_wrow */
6012 }
6013 redraw_win_later(wp, NOT_VALID);
6014 wp->w_redr_status = TRUE;
6015}
6016#endif
6017
6018 void
6019win_comp_scroll(wp)
6020 win_T *wp;
6021{
6022 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6023 if (wp->w_p_scr == 0)
6024 wp->w_p_scr = 1;
6025}
6026
6027/*
6028 * command_height: called whenever p_ch has been changed
6029 */
6030 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006031command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033#ifdef FEAT_WINDOWS
6034 int h;
6035 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006036 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006038 /* Use the value of p_ch that we remembered. This is needed for when the
6039 * GUI starts up, we can't be sure in what order things happen. And when
6040 * p_ch was changed in another tab page. */
6041 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 /* Find bottom frame with width of screen. */
6044 frp = lastwin->w_frame;
6045# ifdef FEAT_VERTSPLIT
6046 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6047 frp = frp->fr_parent;
6048# endif
6049
6050 /* Avoid changing the height of a window with 'winfixheight' set. */
6051 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6052 && frp->fr_win->w_p_wfh)
6053 frp = frp->fr_prev;
6054
6055 if (starting != NO_SCREEN)
6056 {
6057 cmdline_row = Rows - p_ch;
6058
6059 if (p_ch > old_p_ch) /* p_ch got bigger */
6060 {
6061 while (p_ch > old_p_ch)
6062 {
6063 if (frp == NULL)
6064 {
6065 EMSG(_(e_noroom));
6066 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006067 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 cmdline_row = Rows - p_ch;
6069 break;
6070 }
6071 h = frp->fr_height - frame_minheight(frp, NULL);
6072 if (h > p_ch - old_p_ch)
6073 h = p_ch - old_p_ch;
6074 old_p_ch += h;
6075 frame_add_height(frp, -h);
6076 frp = frp->fr_prev;
6077 }
6078
6079 /* Recompute window positions. */
6080 (void)win_comp_pos();
6081
6082 /* clear the lines added to cmdline */
6083 if (full_screen)
6084 screen_fill((int)(cmdline_row), (int)Rows, 0,
6085 (int)Columns, ' ', ' ', 0);
6086 msg_row = cmdline_row;
6087 redraw_cmdline = TRUE;
6088 return;
6089 }
6090
6091 if (msg_row < cmdline_row)
6092 msg_row = cmdline_row;
6093 redraw_cmdline = TRUE;
6094 }
6095 frame_add_height(frp, (int)(old_p_ch - p_ch));
6096
6097 /* Recompute window positions. */
6098 if (frp != lastwin->w_frame)
6099 (void)win_comp_pos();
6100#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006102 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103#endif
6104}
6105
6106#if defined(FEAT_WINDOWS) || defined(PROTO)
6107/*
6108 * Resize frame "frp" to be "n" lines higher (negative for less high).
6109 * Also resize the frames it is contained in.
6110 */
6111 static void
6112frame_add_height(frp, n)
6113 frame_T *frp;
6114 int n;
6115{
6116 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6117 for (;;)
6118 {
6119 frp = frp->fr_parent;
6120 if (frp == NULL)
6121 break;
6122 frp->fr_height += n;
6123 }
6124}
6125
6126/*
6127 * Add or remove a status line for the bottom window(s), according to the
6128 * value of 'laststatus'.
6129 */
6130 void
6131last_status(morewin)
6132 int morewin; /* pretend there are two or more windows */
6133{
6134 /* Don't make a difference between horizontal or vertical split. */
6135 last_status_rec(topframe, (p_ls == 2
6136 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6137}
6138
6139 static void
6140last_status_rec(fr, statusline)
6141 frame_T *fr;
6142 int statusline;
6143{
6144 frame_T *fp;
6145 win_T *wp;
6146
6147 if (fr->fr_layout == FR_LEAF)
6148 {
6149 wp = fr->fr_win;
6150 if (wp->w_status_height != 0 && !statusline)
6151 {
6152 /* remove status line */
6153 win_new_height(wp, wp->w_height + 1);
6154 wp->w_status_height = 0;
6155 comp_col();
6156 }
6157 else if (wp->w_status_height == 0 && statusline)
6158 {
6159 /* Find a frame to take a line from. */
6160 fp = fr;
6161 while (fp->fr_height <= frame_minheight(fp, NULL))
6162 {
6163 if (fp == topframe)
6164 {
6165 EMSG(_(e_noroom));
6166 return;
6167 }
6168 /* In a column of frames: go to frame above. If already at
6169 * the top or in a row of frames: go to parent. */
6170 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6171 fp = fp->fr_prev;
6172 else
6173 fp = fp->fr_parent;
6174 }
6175 wp->w_status_height = 1;
6176 if (fp != fr)
6177 {
6178 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6179 frame_fix_height(wp);
6180 (void)win_comp_pos();
6181 }
6182 else
6183 win_new_height(wp, wp->w_height - 1);
6184 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006185 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 }
6187 }
6188#ifdef FEAT_VERTSPLIT
6189 else if (fr->fr_layout == FR_ROW)
6190 {
6191 /* vertically split windows, set status line for each one */
6192 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6193 last_status_rec(fp, statusline);
6194 }
6195#endif
6196 else
6197 {
6198 /* horizontally split window, set status line for last one */
6199 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6200 ;
6201 last_status_rec(fp, statusline);
6202 }
6203}
6204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006206 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 */
6208 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006209tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006211#ifdef FEAT_GUI_TABLINE
6212 /* When the GUI has the tabline then this always returns zero. */
6213 if (gui_use_tabline())
6214 return 0;
6215#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006216 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006217 {
6218 case 0: return 0;
6219 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6220 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006221 return 1;
6222}
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224#endif /* FEAT_WINDOWS */
6225
6226#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6227/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006228 * Get the file name at the cursor.
6229 * If Visual mode is active, use the selected text if it's in one line.
6230 * Returns the name in allocated memory, NULL for failure.
6231 */
6232 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006233grab_file_name(count, file_lnum)
6234 long count;
6235 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006236{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006237 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6238
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006239 if (VIsual_active)
6240 {
6241 int len;
6242 char_u *ptr;
6243
6244 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6245 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006246 return find_file_name_in_path(ptr, len, options,
6247 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006248 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006249 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006250
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006251}
6252
6253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 * Return the file name under or after the cursor.
6255 *
6256 * The 'path' option is searched if the file name is not absolute.
6257 * The string returned has been alloc'ed and should be freed by the caller.
6258 * NULL is returned if the file name or file is not found.
6259 *
6260 * options:
6261 * FNAME_MESS give error messages
6262 * FNAME_EXP expand to path
6263 * FNAME_HYP check for hypertext link
6264 * FNAME_INCL apply "includeexpr"
6265 */
6266 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006267file_name_at_cursor(options, count, file_lnum)
6268 int options;
6269 long count;
6270 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006273 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6274 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275}
6276
6277/*
6278 * Return the name of the file under or after ptr[col].
6279 * Otherwise like file_name_at_cursor().
6280 */
6281 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006282file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 char_u *line;
6284 int col;
6285 int options;
6286 long count;
6287 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006288 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 char_u *ptr;
6291 int len;
6292
6293 /*
6294 * search forward for what could be the start of a file name
6295 */
6296 ptr = line + col;
6297 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006298 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (*ptr == NUL) /* nothing found */
6300 {
6301 if (options & FNAME_MESS)
6302 EMSG(_("E446: No file name under cursor"));
6303 return NULL;
6304 }
6305
6306 /*
6307 * Search backward for first char of the file name.
6308 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6309 */
6310 while (ptr > line)
6311 {
6312#ifdef FEAT_MBYTE
6313 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6314 ptr -= len + 1;
6315 else
6316#endif
6317 if (vim_isfilec(ptr[-1])
6318 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6319 --ptr;
6320 else
6321 break;
6322 }
6323
6324 /*
6325 * Search forward for the last char of the file name.
6326 * Also allow "://" when ':' is not in 'isfname'.
6327 */
6328 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006329 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006331 {
6332 if (ptr[len] == '\\')
6333 /* Skip over the "\" in "\ ". */
6334 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335#ifdef FEAT_MBYTE
6336 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006337 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 else
6339#endif
6340 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 /*
6344 * If there is trailing punctuation, remove it.
6345 * But don't remove "..", could be a directory name.
6346 */
6347 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6348 && ptr[len - 2] != '.')
6349 --len;
6350
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006351 if (file_lnum != NULL)
6352 {
6353 char_u *p;
6354
6355 /* Get the number after the file name and a separator character */
6356 p = ptr + len;
6357 p = skipwhite(p);
6358 if (*p != NUL)
6359 {
6360 if (!isdigit(*p))
6361 ++p; /* skip the separator */
6362 p = skipwhite(p);
6363 if (isdigit(*p))
6364 *file_lnum = (int)getdigits(&p);
6365 }
6366 }
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6369}
6370
6371# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6372static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6373
6374 static char_u *
6375eval_includeexpr(ptr, len)
6376 char_u *ptr;
6377 int len;
6378{
6379 char_u *res;
6380
6381 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006382 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006383 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 set_vim_var_string(VV_FNAME, NULL, 0);
6385 return res;
6386}
6387#endif
6388
6389/*
6390 * Return the name of the file ptr[len] in 'path'.
6391 * Otherwise like file_name_at_cursor().
6392 */
6393 char_u *
6394find_file_name_in_path(ptr, len, options, count, rel_fname)
6395 char_u *ptr;
6396 int len;
6397 int options;
6398 long count;
6399 char_u *rel_fname; /* file we are searching relative to */
6400{
6401 char_u *file_name;
6402 int c;
6403# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6404 char_u *tofree = NULL;
6405
6406 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6407 {
6408 tofree = eval_includeexpr(ptr, len);
6409 if (tofree != NULL)
6410 {
6411 ptr = tofree;
6412 len = (int)STRLEN(ptr);
6413 }
6414 }
6415# endif
6416
6417 if (options & FNAME_EXP)
6418 {
6419 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6420 TRUE, rel_fname);
6421
6422# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6423 /*
6424 * If the file could not be found in a normal way, try applying
6425 * 'includeexpr' (unless done already).
6426 */
6427 if (file_name == NULL
6428 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6429 {
6430 tofree = eval_includeexpr(ptr, len);
6431 if (tofree != NULL)
6432 {
6433 ptr = tofree;
6434 len = (int)STRLEN(ptr);
6435 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6436 TRUE, rel_fname);
6437 }
6438 }
6439# endif
6440 if (file_name == NULL && (options & FNAME_MESS))
6441 {
6442 c = ptr[len];
6443 ptr[len] = NUL;
6444 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6445 ptr[len] = c;
6446 }
6447
6448 /* Repeat finding the file "count" times. This matters when it
6449 * appears several times in the path. */
6450 while (file_name != NULL && --count > 0)
6451 {
6452 vim_free(file_name);
6453 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6454 }
6455 }
6456 else
6457 file_name = vim_strnsave(ptr, len);
6458
6459# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6460 vim_free(tofree);
6461# endif
6462
6463 return file_name;
6464}
6465#endif /* FEAT_SEARCHPATH */
6466
6467/*
6468 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6469 * Also check for ":\\", which MS Internet Explorer accepts, return
6470 * URL_BACKSLASH.
6471 */
6472 static int
6473path_is_url(p)
6474 char_u *p;
6475{
6476 if (STRNCMP(p, "://", (size_t)3) == 0)
6477 return URL_SLASH;
6478 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6479 return URL_BACKSLASH;
6480 return 0;
6481}
6482
6483/*
6484 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6485 * Return URL_BACKSLASH for "name:\\".
6486 * Return zero otherwise.
6487 */
6488 int
6489path_with_url(fname)
6490 char_u *fname;
6491{
6492 char_u *p;
6493
6494 for (p = fname; isalpha(*p); ++p)
6495 ;
6496 return path_is_url(p);
6497}
6498
6499/*
6500 * Return TRUE if "name" is a full (absolute) path name or URL.
6501 */
6502 int
6503vim_isAbsName(name)
6504 char_u *name;
6505{
6506 return (path_with_url(name) != 0 || mch_isFullName(name));
6507}
6508
6509/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006510 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 *
6512 * return FAIL for failure, OK otherwise
6513 */
6514 int
6515vim_FullName(fname, buf, len, force)
6516 char_u *fname, *buf;
6517 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006518 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 int retval = OK;
6521 int url;
6522
6523 *buf = NUL;
6524 if (fname == NULL)
6525 return FAIL;
6526
6527 url = path_with_url(fname);
6528 if (!url)
6529 retval = mch_FullName(fname, buf, len, force);
6530 if (url || retval == FAIL)
6531 {
6532 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006533 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6536 slash_adjust(buf);
6537#endif
6538 return retval;
6539}
6540
6541/*
6542 * Return the minimal number of rows that is needed on the screen to display
6543 * the current number of windows.
6544 */
6545 int
6546min_rows()
6547{
6548 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006549#ifdef FEAT_WINDOWS
6550 tabpage_T *tp;
6551 int n;
6552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
6554 if (firstwin == NULL) /* not initialized yet */
6555 return MIN_LINES;
6556
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006558 total = 0;
6559 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6560 {
6561 n = frame_minheight(tp->tp_topframe, NULL);
6562 if (total < n)
6563 total = n;
6564 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006565 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006567 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006569 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 return total;
6571}
6572
6573/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006574 * Return TRUE if there is only one window (in the current tab page), not
6575 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006576 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 */
6578 int
6579only_one_window()
6580{
6581#ifdef FEAT_WINDOWS
6582 int count = 0;
6583 win_T *wp;
6584
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006585 /* If there is another tab page there always is another window. */
6586 if (first_tabpage->tp_next != NULL)
6587 return FALSE;
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006590 if (wp->w_buffer != NULL
6591 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592# ifdef FEAT_QUICKFIX
6593 || wp->w_p_pvw
6594# endif
6595 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006596# ifdef FEAT_AUTOCMD
6597 && wp != aucmd_win
6598# endif
6599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 ++count;
6601 return (count <= 1);
6602#else
6603 return TRUE;
6604#endif
6605}
6606
6607#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6608/*
6609 * Correct the cursor line number in other windows. Used after changing the
6610 * current buffer, and before applying autocommands.
6611 * When "do_curwin" is TRUE, also check current window.
6612 */
6613 void
6614check_lnums(do_curwin)
6615 int do_curwin;
6616{
6617 win_T *wp;
6618
6619#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006620 tabpage_T *tp;
6621
6622 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6624#else
6625 wp = curwin;
6626 if (do_curwin)
6627#endif
6628 {
6629 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6630 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6631 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6632 wp->w_topline = curbuf->b_ml.ml_line_count;
6633 }
6634}
6635#endif
6636
6637#if defined(FEAT_WINDOWS) || defined(PROTO)
6638
6639/*
6640 * A snapshot of the window sizes, to restore them after closing the help
6641 * window.
6642 * Only these fields are used:
6643 * fr_layout
6644 * fr_width
6645 * fr_height
6646 * fr_next
6647 * fr_child
6648 * fr_win (only valid for the old curwin, NULL otherwise)
6649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651/*
6652 * Create a snapshot of the current frame sizes.
6653 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006654 void
6655make_snapshot(idx)
6656 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006658 clear_snapshot(curtab, idx);
6659 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
6662 static void
6663make_snapshot_rec(fr, frp)
6664 frame_T *fr;
6665 frame_T **frp;
6666{
6667 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6668 if (*frp == NULL)
6669 return;
6670 (*frp)->fr_layout = fr->fr_layout;
6671# ifdef FEAT_VERTSPLIT
6672 (*frp)->fr_width = fr->fr_width;
6673# endif
6674 (*frp)->fr_height = fr->fr_height;
6675 if (fr->fr_next != NULL)
6676 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6677 if (fr->fr_child != NULL)
6678 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6679 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6680 (*frp)->fr_win = curwin;
6681}
6682
6683/*
6684 * Remove any existing snapshot.
6685 */
6686 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006687clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006688 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006689 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006691 clear_snapshot_rec(tp->tp_snapshot[idx]);
6692 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
6695 static void
6696clear_snapshot_rec(fr)
6697 frame_T *fr;
6698{
6699 if (fr != NULL)
6700 {
6701 clear_snapshot_rec(fr->fr_next);
6702 clear_snapshot_rec(fr->fr_child);
6703 vim_free(fr);
6704 }
6705}
6706
6707/*
6708 * Restore a previously created snapshot, if there is any.
6709 * This is only done if the screen size didn't change and the window layout is
6710 * still the same.
6711 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006712 void
6713restore_snapshot(idx, close_curwin)
6714 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 int close_curwin; /* closing current window */
6716{
6717 win_T *wp;
6718
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006719 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006721 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006723 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6724 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006726 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 win_comp_pos();
6728 if (wp != NULL && close_curwin)
6729 win_goto(wp);
6730 redraw_all_later(CLEAR);
6731 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006732 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733}
6734
6735/*
6736 * Check if frames "sn" and "fr" have the same layout, same following frames
6737 * and same children.
6738 */
6739 static int
6740check_snapshot_rec(sn, fr)
6741 frame_T *sn;
6742 frame_T *fr;
6743{
6744 if (sn->fr_layout != fr->fr_layout
6745 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6746 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6747 || (sn->fr_next != NULL
6748 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6749 || (sn->fr_child != NULL
6750 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6751 return FAIL;
6752 return OK;
6753}
6754
6755/*
6756 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6757 * following frames and children.
6758 * Returns a pointer to the old current window, or NULL.
6759 */
6760 static win_T *
6761restore_snapshot_rec(sn, fr)
6762 frame_T *sn;
6763 frame_T *fr;
6764{
6765 win_T *wp = NULL;
6766 win_T *wp2;
6767
6768 fr->fr_height = sn->fr_height;
6769# ifdef FEAT_VERTSPLIT
6770 fr->fr_width = sn->fr_width;
6771# endif
6772 if (fr->fr_layout == FR_LEAF)
6773 {
6774 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6775# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006776 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777# endif
6778 wp = sn->fr_win;
6779 }
6780 if (sn->fr_next != NULL)
6781 {
6782 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6783 if (wp2 != NULL)
6784 wp = wp2;
6785 }
6786 if (sn->fr_child != NULL)
6787 {
6788 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6789 if (wp2 != NULL)
6790 wp = wp2;
6791 }
6792 return wp;
6793}
6794
6795#endif
6796
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006797#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6798 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006799/*
6800 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006801 * restore_win() MUST be called to undo, also when FAIL is returned.
6802 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006803 * When "no_display" is TRUE the display won't be affected, no redraw is
6804 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006805 * Returns FAIL if switching to "win" failed.
6806 */
6807 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006808switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006809 win_T **save_curwin UNUSED;
6810 tabpage_T **save_curtab UNUSED;
6811 win_T *win UNUSED;
6812 tabpage_T *tp UNUSED;
6813 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006814{
6815# ifdef FEAT_AUTOCMD
6816 block_autocmds();
6817# endif
6818# ifdef FEAT_WINDOWS
6819 *save_curwin = curwin;
6820 if (tp != NULL)
6821 {
6822 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006823 if (no_display)
6824 {
6825 curtab->tp_firstwin = firstwin;
6826 curtab->tp_lastwin = lastwin;
6827 curtab = tp;
6828 firstwin = curtab->tp_firstwin;
6829 lastwin = curtab->tp_lastwin;
6830 }
6831 else
6832 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006833 }
6834 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006835 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006836 curwin = win;
6837 curbuf = curwin->w_buffer;
6838# endif
6839 return OK;
6840}
6841
6842/*
6843 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006844 * When "no_display" is TRUE the display won't be affected, no redraw is
6845 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006846 */
6847 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006848restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006849 win_T *save_curwin UNUSED;
6850 tabpage_T *save_curtab UNUSED;
6851 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006852{
6853# ifdef FEAT_WINDOWS
6854 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006855 {
6856 if (no_display)
6857 {
6858 curtab->tp_firstwin = firstwin;
6859 curtab->tp_lastwin = lastwin;
6860 curtab = save_curtab;
6861 firstwin = curtab->tp_firstwin;
6862 lastwin = curtab->tp_lastwin;
6863 }
6864 else
6865 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6866 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006867 if (win_valid(save_curwin))
6868 {
6869 curwin = save_curwin;
6870 curbuf = curwin->w_buffer;
6871 }
6872# endif
6873# ifdef FEAT_AUTOCMD
6874 unblock_autocmds();
6875# endif
6876}
6877
6878/*
6879 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6880 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6881 */
6882 void
6883switch_buffer(save_curbuf, buf)
6884 buf_T *buf;
6885 buf_T **save_curbuf;
6886{
6887# ifdef FEAT_AUTOCMD
6888 block_autocmds();
6889# endif
6890 *save_curbuf = curbuf;
6891 --curbuf->b_nwindows;
6892 curbuf = buf;
6893 curwin->w_buffer = buf;
6894 ++curbuf->b_nwindows;
6895}
6896
6897/*
6898 * Restore the current buffer after using switch_buffer().
6899 */
6900 void
6901restore_buffer(save_curbuf)
6902 buf_T *save_curbuf;
6903{
6904# ifdef FEAT_AUTOCMD
6905 unblock_autocmds();
6906# endif
6907 /* Check for valid buffer, just in case. */
6908 if (buf_valid(save_curbuf))
6909 {
6910 --curbuf->b_nwindows;
6911 curwin->w_buffer = save_curbuf;
6912 curbuf = save_curbuf;
6913 ++curbuf->b_nwindows;
6914 }
6915}
6916#endif
6917
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6919/*
6920 * Return TRUE if there is any vertically split window.
6921 */
6922 int
6923win_hasvertsplit()
6924{
6925 frame_T *fr;
6926
6927 if (topframe->fr_layout == FR_ROW)
6928 return TRUE;
6929
6930 if (topframe->fr_layout == FR_COL)
6931 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6932 if (fr->fr_layout == FR_ROW)
6933 return TRUE;
6934
6935 return FALSE;
6936}
6937#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006938
6939#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6940/*
6941 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006942 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006943 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6944 * If no particular ID is desired, -1 must be specified for 'id'.
6945 * Return ID of added match, -1 on failure.
6946 */
6947 int
Bram Moolenaar6561d522015-07-21 15:48:27 +02006948match_add(wp, grp, pat, prio, id, pos_list, conceal_char)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006949 win_T *wp;
6950 char_u *grp;
6951 char_u *pat;
6952 int prio;
6953 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006954 list_T *pos_list;
Bram Moolenaar6561d522015-07-21 15:48:27 +02006955 char_u *conceal_char UNUSED; /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006956{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006957 matchitem_T *cur;
6958 matchitem_T *prev;
6959 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006960 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006961 regprog_T *regprog = NULL;
6962 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006963
Bram Moolenaarb3414592014-06-17 17:48:32 +02006964 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006965 return -1;
6966 if (id < -1 || id == 0)
6967 {
6968 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6969 return -1;
6970 }
6971 if (id != -1)
6972 {
6973 cur = wp->w_match_head;
6974 while (cur != NULL)
6975 {
6976 if (cur->id == id)
6977 {
6978 EMSGN("E801: ID already taken: %ld", id);
6979 return -1;
6980 }
6981 cur = cur->next;
6982 }
6983 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006984 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006985 {
6986 EMSG2(_(e_nogroup), grp);
6987 return -1;
6988 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006989 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006990 {
6991 EMSG2(_(e_invarg2), pat);
6992 return -1;
6993 }
6994
6995 /* Find available match ID. */
6996 while (id == -1)
6997 {
6998 cur = wp->w_match_head;
6999 while (cur != NULL && cur->id != wp->w_next_match_id)
7000 cur = cur->next;
7001 if (cur == NULL)
7002 id = wp->w_next_match_id;
7003 wp->w_next_match_id++;
7004 }
7005
7006 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007007 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007008 m->id = id;
7009 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007010 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007011 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00007012 m->match.regprog = regprog;
7013 m->match.rmm_ic = FALSE;
7014 m->match.rmm_maxcol = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02007015#ifdef FEAT_CONCEAL
7016 m->conceal_char = 0;
7017 if (conceal_char != NULL)
7018 m->conceal_char = (*mb_ptr2char)(conceal_char);
7019#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007020
Bram Moolenaarb3414592014-06-17 17:48:32 +02007021 /* Set up position matches */
7022 if (pos_list != NULL)
7023 {
7024 linenr_T toplnum = 0;
7025 linenr_T botlnum = 0;
7026 listitem_T *li;
7027 int i;
7028
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007029 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007030 i++, li = li->li_next)
7031 {
7032 linenr_T lnum = 0;
7033 colnr_T col = 0;
7034 int len = 1;
7035 list_T *subl;
7036 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007037 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007038
Bram Moolenaarb3414592014-06-17 17:48:32 +02007039 if (li->li_tv.v_type == VAR_LIST)
7040 {
7041 subl = li->li_tv.vval.v_list;
7042 if (subl == NULL)
7043 goto fail;
7044 subli = subl->lv_first;
7045 if (subli == NULL)
7046 goto fail;
7047 lnum = get_tv_number_chk(&subli->li_tv, &error);
7048 if (error == TRUE)
7049 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007050 if (lnum == 0)
7051 {
7052 --i;
7053 continue;
7054 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007055 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007056 subli = subli->li_next;
7057 if (subli != NULL)
7058 {
7059 col = get_tv_number_chk(&subli->li_tv, &error);
7060 if (error == TRUE)
7061 goto fail;
7062 subli = subli->li_next;
7063 if (subli != NULL)
7064 {
7065 len = get_tv_number_chk(&subli->li_tv, &error);
7066 if (error == TRUE)
7067 goto fail;
7068 }
7069 }
7070 m->pos.pos[i].col = col;
7071 m->pos.pos[i].len = len;
7072 }
7073 else if (li->li_tv.v_type == VAR_NUMBER)
7074 {
7075 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007076 {
7077 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007078 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007079 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007080 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
7081 m->pos.pos[i].col = 0;
7082 m->pos.pos[i].len = 0;
7083 }
7084 else
7085 {
7086 EMSG(_("List or number required"));
7087 goto fail;
7088 }
7089 if (toplnum == 0 || lnum < toplnum)
7090 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02007091 if (botlnum == 0 || lnum >= botlnum)
7092 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007093 }
7094
7095 /* Calculate top and bottom lines for redrawing area */
7096 if (toplnum != 0)
7097 {
7098 if (wp->w_buffer->b_mod_set)
7099 {
7100 if (wp->w_buffer->b_mod_top > toplnum)
7101 wp->w_buffer->b_mod_top = toplnum;
7102 if (wp->w_buffer->b_mod_bot < botlnum)
7103 wp->w_buffer->b_mod_bot = botlnum;
7104 }
7105 else
7106 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007107 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007108 wp->w_buffer->b_mod_top = toplnum;
7109 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007110 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007111 }
7112 m->pos.toplnum = toplnum;
7113 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007114 rtype = VALID;
7115 }
7116 }
7117
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007118 /* Insert new match. The match list is in ascending order with regard to
7119 * the match priorities. */
7120 cur = wp->w_match_head;
7121 prev = cur;
7122 while (cur != NULL && prio >= cur->priority)
7123 {
7124 prev = cur;
7125 cur = cur->next;
7126 }
7127 if (cur == prev)
7128 wp->w_match_head = m;
7129 else
7130 prev->next = m;
7131 m->next = cur;
7132
Bram Moolenaarb3414592014-06-17 17:48:32 +02007133 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007134 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007135
7136fail:
7137 vim_free(m);
7138 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007139}
7140
7141/*
7142 * Delete match with ID 'id' in the match list of window 'wp'.
7143 * Print error messages if 'perr' is TRUE.
7144 */
7145 int
7146match_delete(wp, id, perr)
7147 win_T *wp;
7148 int id;
7149 int perr;
7150{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007151 matchitem_T *cur = wp->w_match_head;
7152 matchitem_T *prev = cur;
7153 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007154
7155 if (id < 1)
7156 {
7157 if (perr == TRUE)
7158 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7159 id);
7160 return -1;
7161 }
7162 while (cur != NULL && cur->id != id)
7163 {
7164 prev = cur;
7165 cur = cur->next;
7166 }
7167 if (cur == NULL)
7168 {
7169 if (perr == TRUE)
7170 EMSGN("E803: ID not found: %ld", id);
7171 return -1;
7172 }
7173 if (cur == prev)
7174 wp->w_match_head = cur->next;
7175 else
7176 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007177 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007178 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007179 if (cur->pos.toplnum != 0)
7180 {
7181 if (wp->w_buffer->b_mod_set)
7182 {
7183 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7184 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7185 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7186 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7187 }
7188 else
7189 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007190 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007191 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7192 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007193 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007194 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007195 rtype = VALID;
7196 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007197 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007198 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007199 return 0;
7200}
7201
7202/*
7203 * Delete all matches in the match list of window 'wp'.
7204 */
7205 void
7206clear_matches(wp)
7207 win_T *wp;
7208{
7209 matchitem_T *m;
7210
7211 while (wp->w_match_head != NULL)
7212 {
7213 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007214 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007215 vim_free(wp->w_match_head->pattern);
7216 vim_free(wp->w_match_head);
7217 wp->w_match_head = m;
7218 }
7219 redraw_later(SOME_VALID);
7220}
7221
7222/*
7223 * Get match from ID 'id' in window 'wp'.
7224 * Return NULL if match not found.
7225 */
7226 matchitem_T *
7227get_match(wp, id)
7228 win_T *wp;
7229 int id;
7230{
7231 matchitem_T *cur = wp->w_match_head;
7232
7233 while (cur != NULL && cur->id != id)
7234 cur = cur->next;
7235 return cur;
7236}
7237#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007238
7239#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7240 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007241get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007242{
7243 int i = 1;
7244 win_T *w;
7245
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007246 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007247 ++i;
7248
7249 if (w == NULL)
7250 return 0;
7251 else
7252 return i;
7253}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007254
7255 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007256get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007257{
7258 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007259# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007260 tabpage_T *t;
7261
7262 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7263 ++i;
7264
7265 if (t == NULL)
7266 return 0;
7267 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007268# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007269 return i;
7270}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007271#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007272
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007273#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007274/*
7275 * Return TRUE if "topfrp" and its children are at the right height.
7276 */
7277 static int
7278frame_check_height(topfrp, height)
7279 frame_T *topfrp;
7280 int height;
7281{
7282 frame_T *frp;
7283
7284 if (topfrp->fr_height != height)
7285 return FALSE;
7286
7287 if (topfrp->fr_layout == FR_ROW)
7288 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7289 if (frp->fr_height != height)
7290 return FALSE;
7291
7292 return TRUE;
7293}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007294#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007295
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007296#if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007297/*
7298 * Return TRUE if "topfrp" and its children are at the right width.
7299 */
7300 static int
7301frame_check_width(topfrp, width)
7302 frame_T *topfrp;
7303 int width;
7304{
7305 frame_T *frp;
7306
7307 if (topfrp->fr_width != width)
7308 return FALSE;
7309
7310 if (topfrp->fr_layout == FR_COL)
7311 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7312 if (frp->fr_width != width)
7313 return FALSE;
7314
7315 return TRUE;
7316}
7317#endif
7318