blob: 408c4bd223d7a7aa9387bf18ae6d0a0274641016 [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 Moolenaar071d4272004-06-13 20:20:40 +000078
79#define URL_SLASH 1 /* path_is_url() has found "://" */
80#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000082#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000085# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086#else
87# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000091
92static char *m_onlyone = N_("Already only one window");
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
98do_window(nchar, Prenum, xchar)
99 int nchar;
100 long Prenum;
101 int xchar; /* extra char from ":wincmd gx" or NUL */
102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000134#ifdef FEAT_QUICKFIX
135 /* When splitting the quickfix window open a new buffer in it,
136 * don't replicate the quickfix buffer. */
137 if (bt_quickfix(curbuf))
138 goto newwindow;
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_GUI
141 need_mouse_correct = TRUE;
142#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200143 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 break;
145
146#ifdef FEAT_VERTSPLIT
147/* split current window in two parts, vertically */
148 case Ctrl_V:
149 case 'v':
150 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200161 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100170 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 do_cmdline_cmd(cbuf);
172 break;
173
174/* open new window */
175 case Ctrl_N:
176 case 'n':
177 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000179#ifdef FEAT_QUICKFIX
180newwindow:
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000183 /* window height */
184 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 else
186 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000187#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
188 if (nchar == 'v' || nchar == Ctrl_V)
189 STRCAT(cbuf, "v");
190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 STRCAT(cbuf, "new");
192 do_cmdline_cmd(cbuf);
193 break;
194
195/* quit current window */
196 case Ctrl_Q:
197 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100199 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100200 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 break;
202
203/* close current window */
204 case Ctrl_C:
205 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100207 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100208 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 break;
210
211#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
212/* close preview window */
213 case Ctrl_Z:
214 case 'z':
215 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 do_cmdline_cmd((char_u *)"pclose");
218 break;
219
220/* cursor to preview window */
221 case 'P':
222 for (wp = firstwin; wp != NULL; wp = wp->w_next)
223 if (wp->w_p_pvw)
224 break;
225 if (wp == NULL)
226 EMSG(_("E441: There is no preview window"));
227 else
228 win_goto(wp);
229 break;
230#endif
231
232/* close all but current window */
233 case Ctrl_O:
234 case 'o':
235 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100237 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100238 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 break;
240
241/* cursor to next window with wrap around */
242 case Ctrl_W:
243 case 'w':
244/* cursor to previous window with wrap around */
245 case 'W':
246 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000247 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 beep_flush();
249 else
250 {
251 if (Prenum) /* go to specified window */
252 {
253 for (wp = firstwin; --Prenum > 0; )
254 {
255 if (wp->w_next == NULL)
256 break;
257 else
258 wp = wp->w_next;
259 }
260 }
261 else
262 {
263 if (nchar == 'W') /* go to previous window */
264 {
265 wp = curwin->w_prev;
266 if (wp == NULL)
267 wp = lastwin; /* wrap around */
268 }
269 else /* go to next window */
270 {
271 wp = curwin->w_next;
272 if (wp == NULL)
273 wp = firstwin; /* wrap around */
274 }
275 }
276 win_goto(wp);
277 }
278 break;
279
280/* cursor to window below */
281 case 'j':
282 case K_DOWN:
283 case Ctrl_J:
284 CHECK_CMDWIN
285#ifdef FEAT_VERTSPLIT
286 win_goto_ver(FALSE, Prenum1);
287#else
288 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
289 wp = wp->w_next)
290 ;
291 win_goto(wp);
292#endif
293 break;
294
295/* cursor to window above */
296 case 'k':
297 case K_UP:
298 case Ctrl_K:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(TRUE, Prenum1);
302#else
303 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
304 wp = wp->w_prev)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310#ifdef FEAT_VERTSPLIT
311/* cursor to left window */
312 case 'h':
313 case K_LEFT:
314 case Ctrl_H:
315 case K_BS:
316 CHECK_CMDWIN
317 win_goto_hor(TRUE, Prenum1);
318 break;
319
320/* cursor to right window */
321 case 'l':
322 case K_RIGHT:
323 case Ctrl_L:
324 CHECK_CMDWIN
325 win_goto_hor(FALSE, Prenum1);
326 break;
327#endif
328
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329/* move window to new tab page */
330 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000331 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 MSG(_(m_onlyone));
333 else
334 {
335 tabpage_T *oldtab = curtab;
336 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337
338 /* First create a new tab with the window, then go back to
339 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000340 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (win_new_tabpage((int)Prenum) == OK
342 && valid_tabpage(oldtab))
343 {
344 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200345 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 if (curwin == wp)
347 win_close(curwin, FALSE);
348 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200349 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 }
351 }
352 break;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* cursor to top-left window */
355 case 't':
356 case Ctrl_T:
357 win_goto(firstwin);
358 break;
359
360/* cursor to bottom-right window */
361 case 'b':
362 case Ctrl_B:
363 win_goto(lastwin);
364 break;
365
366/* cursor to last accessed (previous) window */
367 case 'p':
368 case Ctrl_P:
369 if (prevwin == NULL)
370 beep_flush();
371 else
372 win_goto(prevwin);
373 break;
374
375/* exchange current and next window */
376 case 'x':
377 case Ctrl_X:
378 CHECK_CMDWIN
379 win_exchange(Prenum);
380 break;
381
382/* rotate windows downwards */
383 case Ctrl_R:
384 case 'r':
385 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 win_rotate(FALSE, (int)Prenum1); /* downwards */
388 break;
389
390/* rotate windows upwards */
391 case 'R':
392 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 win_rotate(TRUE, (int)Prenum1); /* upwards */
395 break;
396
397/* move window to the very top/bottom/left/right */
398 case 'K':
399 case 'J':
400#ifdef FEAT_VERTSPLIT
401 case 'H':
402 case 'L':
403#endif
404 CHECK_CMDWIN
405 win_totop((int)Prenum,
406 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
407 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
408 break;
409
410/* make all windows the same height */
411 case '=':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_equal(NULL, FALSE, 'b');
416 break;
417
418/* increase current window height */
419 case '+':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(curwin->w_height + (int)Prenum1);
424 break;
425
426/* decrease current window height */
427 case '-':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setheight(curwin->w_height - (int)Prenum1);
432 break;
433
434/* set current window height */
435 case Ctrl__:
436 case '_':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(Prenum ? (int)Prenum : 9999);
441 break;
442
443#ifdef FEAT_VERTSPLIT
444/* increase current window width */
445 case '>':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(curwin->w_width + (int)Prenum1);
450 break;
451
452/* decrease current window width */
453 case '<':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setwidth(curwin->w_width - (int)Prenum1);
458 break;
459
460/* set current window width */
461 case '|':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
466 break;
467#endif
468
469/* jump to tag and split window if tag exists (in preview window) */
470#if defined(FEAT_QUICKFIX)
471 case '}':
472 CHECK_CMDWIN
473 if (Prenum)
474 g_do_tagpreview = Prenum;
475 else
476 g_do_tagpreview = p_pvh;
477 /*FALLTHROUGH*/
478#endif
479 case ']':
480 case Ctrl_RSB:
481 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200482 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200487#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200488 if (nchar != '}')
489 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaard355c502014-09-23 13:48:43 +0200492 /* Execute the command right here, required when "wincmd ]"
493 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 do_nv_ident(Ctrl_RSB, NUL);
495 break;
496
497#ifdef FEAT_SEARCHPATH
498/* edit file name under cursor in a new window */
499 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000500 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000502wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000505 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (ptr != NULL)
507 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000508# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 setpcmark();
512 if (win_split(0, 0) == OK)
513 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200514 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000515 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
516 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000517 if (nchar == 'F' && lnum >= 0)
518 {
519 curwin->w_cursor.lnum = lnum;
520 check_cursor_lnum();
521 beginline(BL_SOL | BL_FIX);
522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 vim_free(ptr);
525 }
526 break;
527#endif
528
529#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000530/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * new window -- webb
532 */
533 case 'i': /* Go to any match */
534 case Ctrl_I:
535 type = FIND_ANY;
536 /* FALLTHROUGH */
537 case 'd': /* Go to definition, using 'define' */
538 case Ctrl_D:
539 CHECK_CMDWIN
540 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
541 break;
542 find_pattern_in_path(ptr, 0, len, TRUE,
543 Prenum == 0 ? TRUE : FALSE, type,
544 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
545 curwin->w_set_curswant = TRUE;
546 break;
547#endif
548
Bram Moolenaar05159a02005-02-26 23:04:13 +0000549 case K_KENTER:
550 case CAR:
551#if defined(FEAT_QUICKFIX)
552 /*
553 * In a quickfix window a <CR> jumps to the error under the
554 * cursor in a new window.
555 */
556 if (bt_quickfix(curbuf))
557 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000558 sprintf((char *)cbuf, "split +%ld%s",
559 (long)curwin->w_cursor.lnum,
560 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000561 do_cmdline_cmd(cbuf);
562 }
563#endif
564 break;
565
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567/* CTRL-W g extended commands */
568 case 'g':
569 case Ctrl_G:
570 CHECK_CMDWIN
571#ifdef USE_ON_FLY_SCROLL
572 dont_scroll = TRUE; /* disallow scrolling here */
573#endif
574 ++no_mapping;
575 ++allow_keys; /* no mapping for xchar, but allow key codes */
576 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000577 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 --no_mapping;
580 --allow_keys;
581#ifdef FEAT_CMDL_INFO
582 (void)add_to_showcmd(xchar);
583#endif
584 switch (xchar)
585 {
586#if defined(FEAT_QUICKFIX)
587 case '}':
588 xchar = Ctrl_RSB;
589 if (Prenum)
590 g_do_tagpreview = Prenum;
591 else
592 g_do_tagpreview = p_pvh;
593 /*FALLTHROUGH*/
594#endif
595 case ']':
596 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200597 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (Prenum)
599 postponed_split = Prenum;
600 else
601 postponed_split = -1;
602
603 /* Execute the command right here, required when
604 * "wincmd g}" was used in a function. */
605 do_nv_ident('g', xchar);
606 break;
607
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000608#ifdef FEAT_SEARCHPATH
609 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000610 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100611 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000612 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000613 goto wingotofile;
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 default:
616 beep_flush();
617 break;
618 }
619 break;
620
621 default: beep_flush();
622 break;
623 }
624}
625
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100626/*
627 * Figure out the address type for ":wnncmd".
628 */
629 void
630get_wincmd_addr_type(arg, eap)
631 char_u *arg;
632 exarg_T *eap;
633{
634 switch (*arg)
635 {
636 case 'S':
637 case Ctrl_S:
638 case 's':
639 case Ctrl_N:
640 case 'n':
641 case 'j':
642 case Ctrl_J:
643 case 'k':
644 case Ctrl_K:
645 case 'T':
646 case Ctrl_R:
647 case 'r':
648 case 'R':
649 case 'K':
650 case 'J':
651 case '+':
652 case '-':
653 case Ctrl__:
654 case '_':
655 case '|':
656 case ']':
657 case Ctrl_RSB:
658 case 'g':
659 case Ctrl_G:
660#ifdef FEAT_VERTSPLIT
661 case Ctrl_V:
662 case 'v':
663 case 'h':
664 case Ctrl_H:
665 case 'l':
666 case Ctrl_L:
667 case 'H':
668 case 'L':
669 case '>':
670 case '<':
671#endif
672#if defined(FEAT_QUICKFIX)
673 case '}':
674#endif
675#ifdef FEAT_SEARCHPATH
676 case 'f':
677 case 'F':
678 case Ctrl_F:
679#endif
680#ifdef FEAT_FIND_ID
681 case 'i':
682 case Ctrl_I:
683 case 'd':
684 case Ctrl_D:
685#endif
686 /* window size or any count */
687 eap->addr_type = ADDR_LINES;
688 break;
689
690 case Ctrl_HAT:
691 case '^':
692 /* buffer number */
693 eap->addr_type = ADDR_BUFFERS;
694 break;
695
696 case Ctrl_Q:
697 case 'q':
698 case Ctrl_C:
699 case 'c':
700 case Ctrl_O:
701 case 'o':
702 case Ctrl_W:
703 case 'w':
704 case 'W':
705 case 'x':
706 case Ctrl_X:
707 /* window number */
708 eap->addr_type = ADDR_WINDOWS;
709 break;
710
711#if defined(FEAT_QUICKFIX)
712 case Ctrl_Z:
713 case 'z':
714 case 'P':
715#endif
716 case 't':
717 case Ctrl_T:
718 case 'b':
719 case Ctrl_B:
720 case 'p':
721 case Ctrl_P:
722 case '=':
723 case CAR:
724 /* no count */
725 eap->addr_type = 0;
726 break;
727 }
728}
729
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100730 static void
731cmd_with_count(cmd, bufp, bufsize, Prenum)
732 char *cmd;
733 char_u *bufp;
734 size_t bufsize;
735 long Prenum;
736{
737 size_t len = STRLEN(cmd);
738
739 STRCPY(bufp, cmd);
740 if (Prenum > 0)
741 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
742}
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
745 * split the current window, implements CTRL-W s and :split
746 *
747 * "size" is the height or width for the new window, 0 to use half of current
748 * height or width.
749 *
750 * "flags":
751 * WSP_ROOM: require enough room for new window
752 * WSP_VERT: vertical split.
753 * WSP_TOP: open window at the top-left of the shell (help window).
754 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
755 * WSP_HELP: creating the help window, keep layout snapshot
756 *
757 * return FAIL for failure, OK otherwise
758 */
759 int
760win_split(size, flags)
761 int size;
762 int flags;
763{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000764 /* When the ":tab" modifier was used open a new tab page instead. */
765 if (may_open_tabpage() == OK)
766 return OK;
767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 /* Add flags from ":vertical", ":topleft" and ":botright". */
769 flags |= cmdmod.split;
770 if ((flags & WSP_TOP) && (flags & WSP_BOT))
771 {
772 EMSG(_("E442: Can't split topleft and botright at the same time"));
773 return FAIL;
774 }
775
776 /* When creating the help window make a snapshot of the window layout.
777 * Otherwise clear the snapshot, it's now invalid. */
778 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000779 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000781 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783 return win_split_ins(size, flags, NULL, 0);
784}
785
786/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100787 * When "new_wp" is NULL: split the current window in two.
788 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 * top/left/right/bottom.
790 * return FAIL for failure, OK otherwise
791 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000792 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100793win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 int size;
795 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100796 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 int dir;
798{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100799 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 win_T *oldwin;
801 int new_size = size;
802 int i;
803 int need_status = 0;
804 int do_equal = FALSE;
805 int needed;
806 int available;
807 int oldwin_height = 0;
808 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200809 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200811 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200812 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 if (flags & WSP_TOP)
815 oldwin = firstwin;
816 else if (flags & WSP_BOT)
817 oldwin = lastwin;
818 else
819 oldwin = curwin;
820
821 /* add a status line when p_ls == 1 and splitting the first window */
822 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
823 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100824 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 EMSG(_(e_noroom));
827 return FAIL;
828 }
829 need_status = STATUS_HEIGHT;
830 }
831
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000832#ifdef FEAT_GUI
833 /* May be needed for the scrollbars that are going to change. */
834 if (gui.in_use)
835 out_flush();
836#endif
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838#ifdef FEAT_VERTSPLIT
839 if (flags & WSP_VERT)
840 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200841 int wmw1;
842 int minwidth;
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846 /*
847 * Check if we are able to split the current window and compute its
848 * width.
849 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200850 /* Current window requires at least 1 space. */
851 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
852 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200854 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200855 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200857 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200859 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200861 else if (p_ea)
862 {
863 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
864 prevfrp = oldwin->w_frame;
865 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
866 frp = frp->fr_parent)
867 {
868 if (frp->fr_layout == FR_ROW)
869 for (frp2 = frp->fr_child; frp2 != NULL;
870 frp2 = frp2->fr_next)
871 if (frp2 != prevfrp)
872 minwidth += frame_minwidth(frp2, NOWIN);
873 prevfrp = frp;
874 }
875 available = topframe->fr_width;
876 needed += minwidth;
877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200879 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200880 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
881 available = oldwin->w_frame->fr_width;
882 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200883 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100884 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
886 EMSG(_(e_noroom));
887 return FAIL;
888 }
889 if (new_size == 0)
890 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200891 if (new_size > available - minwidth - 1)
892 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200893 if (new_size < wmw1)
894 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
896 /* if it doesn't fit in the current window, need win_equal() */
897 if (oldwin->w_width - new_size - 1 < p_wmw)
898 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000899
900 /* We don't like to take lines for the new window from a
901 * 'winfixwidth' window. Take them from a window to the left or right
902 * instead, if possible. */
903 if (oldwin->w_p_wfw)
904 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000905
906 /* Only make all windows the same width if one of them (except oldwin)
907 * is wider than one of the split windows. */
908 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
909 && oldwin->w_frame->fr_parent != NULL)
910 {
911 frp = oldwin->w_frame->fr_parent->fr_child;
912 while (frp != NULL)
913 {
914 if (frp->fr_win != oldwin && frp->fr_win != NULL
915 && (frp->fr_win->w_width > new_size
916 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100917 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000918 {
919 do_equal = TRUE;
920 break;
921 }
922 frp = frp->fr_next;
923 }
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 else
927#endif
928 {
929 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 /*
932 * Check if we are able to split the current window and compute its
933 * height.
934 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200935 /* Current window requires at least 1 space. */
936 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
937 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200939 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200940 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200942 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200944 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200946 else if (p_ea)
947 {
948 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
949 prevfrp = oldwin->w_frame;
950 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
951 frp = frp->fr_parent)
952 {
953 if (frp->fr_layout == FR_COL)
954 for (frp2 = frp->fr_child; frp2 != NULL;
955 frp2 = frp2->fr_next)
956 if (frp2 != prevfrp)
957 minheight += frame_minheight(frp2, NOWIN);
958 prevfrp = frp;
959 }
960 available = topframe->fr_height;
961 needed += minheight;
962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 else
964 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200965 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
966 available = oldwin->w_frame->fr_height;
967 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100969 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 {
971 EMSG(_(e_noroom));
972 return FAIL;
973 }
974 oldwin_height = oldwin->w_height;
975 if (need_status)
976 {
977 oldwin->w_status_height = STATUS_HEIGHT;
978 oldwin_height -= STATUS_HEIGHT;
979 }
980 if (new_size == 0)
981 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200982 if (new_size > available - minheight - STATUS_HEIGHT)
983 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200984 if (new_size < wmh1)
985 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /* if it doesn't fit in the current window, need win_equal() */
988 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
989 do_equal = TRUE;
990
991 /* We don't like to take lines for the new window from a
992 * 'winfixheight' window. Take them from a window above or below
993 * instead, if possible. */
994 if (oldwin->w_p_wfh)
995 {
996 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
997 oldwin);
998 oldwin_height = oldwin->w_height;
999 if (need_status)
1000 oldwin_height -= STATUS_HEIGHT;
1001 }
Bram Moolenaar67f71312007-08-12 14:55:56 +00001002
1003 /* Only make all windows the same height if one of them (except oldwin)
1004 * is higher than one of the split windows. */
1005 if (!do_equal && p_ea && size == 0
1006#ifdef FEAT_VERTSPLIT
1007 && *p_ead != 'h'
1008#endif
1009 && oldwin->w_frame->fr_parent != NULL)
1010 {
1011 frp = oldwin->w_frame->fr_parent->fr_child;
1012 while (frp != NULL)
1013 {
1014 if (frp->fr_win != oldwin && frp->fr_win != NULL
1015 && (frp->fr_win->w_height > new_size
1016 || frp->fr_win->w_height > oldwin_height - new_size
1017 - STATUS_HEIGHT))
1018 {
1019 do_equal = TRUE;
1020 break;
1021 }
1022 frp = frp->fr_next;
1023 }
1024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026
1027 /*
1028 * allocate new window structure and link it in the window list
1029 */
1030 if ((flags & WSP_TOP) == 0
1031 && ((flags & WSP_BOT)
1032 || (flags & WSP_BELOW)
1033 || (!(flags & WSP_ABOVE)
1034 && (
1035#ifdef FEAT_VERTSPLIT
1036 (flags & WSP_VERT) ? p_spr :
1037#endif
1038 p_sb))))
1039 {
1040 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001041 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001042 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 else
1044 win_append(oldwin, wp);
1045 }
1046 else
1047 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001048 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001049 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 else
1051 win_append(oldwin->w_prev, wp);
1052 }
1053
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001054 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
1056 if (wp == NULL)
1057 return FAIL;
1058
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001059 new_frame(wp);
1060 if (wp->w_frame == NULL)
1061 {
1062 win_free(wp, NULL);
1063 return FAIL;
1064 }
1065
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001066 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001067 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069
1070 /*
1071 * Reorganise the tree of frames to insert the new window.
1072 */
1073 if (flags & (WSP_TOP | WSP_BOT))
1074 {
1075#ifdef FEAT_VERTSPLIT
1076 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1077 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1078#else
1079 if (topframe->fr_layout == FR_COL)
1080#endif
1081 {
1082 curfrp = topframe->fr_child;
1083 if (flags & WSP_BOT)
1084 while (curfrp->fr_next != NULL)
1085 curfrp = curfrp->fr_next;
1086 }
1087 else
1088 curfrp = topframe;
1089 before = (flags & WSP_TOP);
1090 }
1091 else
1092 {
1093 curfrp = oldwin->w_frame;
1094 if (flags & WSP_BELOW)
1095 before = FALSE;
1096 else if (flags & WSP_ABOVE)
1097 before = TRUE;
1098 else
1099#ifdef FEAT_VERTSPLIT
1100 if (flags & WSP_VERT)
1101 before = !p_spr;
1102 else
1103#endif
1104 before = !p_sb;
1105 }
1106 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1107 {
1108 /* Need to create a new frame in the tree to make a branch. */
1109 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1110 *frp = *curfrp;
1111 curfrp->fr_layout = layout;
1112 frp->fr_parent = curfrp;
1113 frp->fr_next = NULL;
1114 frp->fr_prev = NULL;
1115 curfrp->fr_child = frp;
1116 curfrp->fr_win = NULL;
1117 curfrp = frp;
1118 if (frp->fr_win != NULL)
1119 oldwin->w_frame = frp;
1120 else
1121 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1122 frp->fr_parent = curfrp;
1123 }
1124
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001125 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001126 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001128 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 frp->fr_parent = curfrp->fr_parent;
1130
1131 /* Insert the new frame at the right place in the frame list. */
1132 if (before)
1133 frame_insert(curfrp, frp);
1134 else
1135 frame_append(curfrp, frp);
1136
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001137 /* Set w_fraction now so that the cursor keeps the same relative
1138 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001139 if (oldwin->w_height > 0)
1140 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001141 wp->w_fraction = oldwin->w_fraction;
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#ifdef FEAT_VERTSPLIT
1144 if (flags & WSP_VERT)
1145 {
1146 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 if (need_status)
1149 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 oldwin->w_status_height = need_status;
1152 }
1153 if (flags & (WSP_TOP | WSP_BOT))
1154 {
1155 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001156 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001157 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 wp->w_status_height = (p_ls > 0);
1159 }
1160 else
1161 {
1162 /* height and row of new window is same as current window */
1163 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001164 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 wp->w_status_height = oldwin->w_status_height;
1166 }
1167 frp->fr_height = curfrp->fr_height;
1168
1169 /* "new_size" of the current window goes to the new window, use
1170 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001171 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (before)
1173 wp->w_vsep_width = 1;
1174 else
1175 {
1176 wp->w_vsep_width = oldwin->w_vsep_width;
1177 oldwin->w_vsep_width = 1;
1178 }
1179 if (flags & (WSP_TOP | WSP_BOT))
1180 {
1181 if (flags & WSP_BOT)
1182 frame_add_vsep(curfrp);
1183 /* Set width of neighbor frame */
1184 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001185 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1186 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 }
1188 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001189 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (before) /* new window left of current one */
1191 {
1192 wp->w_wincol = oldwin->w_wincol;
1193 oldwin->w_wincol += new_size + 1;
1194 }
1195 else /* new window right of current one */
1196 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1197 frame_fix_width(oldwin);
1198 frame_fix_width(wp);
1199 }
1200 else
1201#endif
1202 {
1203 /* width and column of new window is same as current window */
1204#ifdef FEAT_VERTSPLIT
1205 if (flags & (WSP_TOP | WSP_BOT))
1206 {
1207 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001208 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 wp->w_vsep_width = 0;
1210 }
1211 else
1212 {
1213 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001214 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 wp->w_vsep_width = oldwin->w_vsep_width;
1216 }
1217 frp->fr_width = curfrp->fr_width;
1218#endif
1219
1220 /* "new_size" of the current window goes to the new window, use
1221 * one row for the status line */
1222 win_new_height(wp, new_size);
1223 if (flags & (WSP_TOP | WSP_BOT))
1224 frame_new_height(curfrp, curfrp->fr_height
1225 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1226 else
1227 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1228 if (before) /* new window above current one */
1229 {
1230 wp->w_winrow = oldwin->w_winrow;
1231 wp->w_status_height = STATUS_HEIGHT;
1232 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1233 }
1234 else /* new window below current one */
1235 {
1236 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1237 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001238 /* Don't set the status_height for oldwin yet, this might break
1239 * frame_fix_height(oldwin), therefore will be set below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241#ifdef FEAT_VERTSPLIT
1242 if (flags & WSP_BOT)
1243 frame_add_statusline(curfrp);
1244#endif
1245 frame_fix_height(wp);
1246 frame_fix_height(oldwin);
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001247
1248 if (!before)
1249 /* new window above current one, set the status_height after
1250 * frame_fix_height(oldwin) */
1251 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253
1254 if (flags & (WSP_TOP | WSP_BOT))
1255 (void)win_comp_pos();
1256
1257 /*
1258 * Both windows need redrawing
1259 */
1260 redraw_win_later(wp, NOT_VALID);
1261 wp->w_redr_status = TRUE;
1262 redraw_win_later(oldwin, NOT_VALID);
1263 oldwin->w_redr_status = TRUE;
1264
1265 if (need_status)
1266 {
1267 msg_row = Rows - 1;
1268 msg_col = sc_col;
1269 msg_clr_eos_force(); /* Old command/ruler may still be there */
1270 comp_col();
1271 msg_row = Rows - 1;
1272 msg_col = 0; /* put position back at start of line */
1273 }
1274
1275 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001276 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 */
1278 if (do_equal || dir != 0)
1279 win_equal(wp, TRUE,
1280#ifdef FEAT_VERTSPLIT
1281 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1282 : dir == 'h' ? 'b' :
1283#endif
1284 'v');
1285
1286 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1287 * size was given. */
1288#ifdef FEAT_VERTSPLIT
1289 if (flags & WSP_VERT)
1290 {
1291 i = p_wiw;
1292 if (size != 0)
1293 p_wiw = size;
1294
1295# ifdef FEAT_GUI
1296 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1297 if (gui.in_use)
1298 gui_init_which_components(NULL);
1299# endif
1300 }
1301 else
1302#endif
1303 {
1304 i = p_wh;
1305 if (size != 0)
1306 p_wh = size;
1307 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001308
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001309#ifdef FEAT_JUMPLIST
1310 /* Keep same changelist position in new window. */
1311 wp->w_changelistidx = oldwin->w_changelistidx;
1312#endif
1313
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001314 /*
1315 * make the new window the current window
1316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 win_enter(wp, FALSE);
1318#ifdef FEAT_VERTSPLIT
1319 if (flags & WSP_VERT)
1320 p_wiw = i;
1321 else
1322#endif
1323 p_wh = i;
1324
1325 return OK;
1326}
1327
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001328
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001329/*
1330 * Initialize window "newp" from window "oldp".
1331 * Used when splitting a window and when creating a new tab page.
1332 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001333 * WSP_NEWLOC may be specified in flags to prevent the location list from
1334 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001335 */
1336 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001337win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001338 win_T *newp;
1339 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001340 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001341{
1342 int i;
1343
1344 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001345#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001346 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001347#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001348 oldp->w_buffer->b_nwindows++;
1349 newp->w_cursor = oldp->w_cursor;
1350 newp->w_valid = 0;
1351 newp->w_curswant = oldp->w_curswant;
1352 newp->w_set_curswant = oldp->w_set_curswant;
1353 newp->w_topline = oldp->w_topline;
1354#ifdef FEAT_DIFF
1355 newp->w_topfill = oldp->w_topfill;
1356#endif
1357 newp->w_leftcol = oldp->w_leftcol;
1358 newp->w_pcmark = oldp->w_pcmark;
1359 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1360 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001361 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001362 newp->w_fraction = oldp->w_fraction;
1363 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1364#ifdef FEAT_JUMPLIST
1365 copy_jumplist(oldp, newp);
1366#endif
1367#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001368 if (flags & WSP_NEWLOC)
1369 {
1370 /* Don't copy the location list. */
1371 newp->w_llist = NULL;
1372 newp->w_llist_ref = NULL;
1373 }
1374 else
1375 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001376#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001377 newp->w_localdir = (oldp->w_localdir == NULL)
1378 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001379
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001380 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001381 for (i = 0; i < oldp->w_tagstacklen; i++)
1382 {
1383 newp->w_tagstack[i] = oldp->w_tagstack[i];
1384 if (newp->w_tagstack[i].tagname != NULL)
1385 newp->w_tagstack[i].tagname =
1386 vim_strsave(newp->w_tagstack[i].tagname);
1387 }
1388 newp->w_tagstackidx = oldp->w_tagstackidx;
1389 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001390#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001391 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001392#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001393
1394 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001395
Bram Moolenaara971b822011-09-14 14:43:25 +02001396#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001397 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001398#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001399}
1400
1401/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001402 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001403 * Only the essential things are copied.
1404 */
1405 static void
1406win_init_some(newp, oldp)
1407 win_T *newp;
1408 win_T *oldp;
1409{
1410 /* Use the same argument list. */
1411 newp->w_alist = oldp->w_alist;
1412 ++newp->w_alist->al_refcount;
1413 newp->w_arg_idx = oldp->w_arg_idx;
1414
1415 /* copy options from existing window */
1416 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001417}
1418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419#endif /* FEAT_WINDOWS */
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#if defined(FEAT_WINDOWS) || defined(PROTO)
1422/*
1423 * Check if "win" is a pointer to an existing window.
1424 */
1425 int
1426win_valid(win)
1427 win_T *win;
1428{
1429 win_T *wp;
1430
1431 if (win == NULL)
1432 return FALSE;
1433 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1434 if (wp == win)
1435 return TRUE;
1436 return FALSE;
1437}
1438
1439/*
1440 * Return the number of windows.
1441 */
1442 int
1443win_count()
1444{
1445 win_T *wp;
1446 int count = 0;
1447
1448 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1449 ++count;
1450 return count;
1451}
1452
1453/*
1454 * Make "count" windows on the screen.
1455 * Return actual number of windows on the screen.
1456 * Must be called when there is just one window, filling the whole screen
1457 * (excluding the command line).
1458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int
1460make_windows(count, vertical)
1461 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001462 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 int maxcount;
1465 int todo;
1466
1467#ifdef FEAT_VERTSPLIT
1468 if (vertical)
1469 {
1470 /* Each windows needs at least 'winminwidth' lines and a separator
1471 * column. */
1472 maxcount = (curwin->w_width + curwin->w_vsep_width
1473 - (p_wiw - p_wmw)) / (p_wmw + 1);
1474 }
1475 else
1476#endif
1477 {
1478 /* Each window needs at least 'winminheight' lines and a status line. */
1479 maxcount = (curwin->w_height + curwin->w_status_height
1480 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1481 }
1482
1483 if (maxcount < 2)
1484 maxcount = 2;
1485 if (count > maxcount)
1486 count = maxcount;
1487
1488 /*
1489 * add status line now, otherwise first window will be too big
1490 */
1491 if (count > 1)
1492 last_status(TRUE);
1493
1494#ifdef FEAT_AUTOCMD
1495 /*
1496 * Don't execute autocommands while creating the windows. Must do that
1497 * when putting the buffers in the windows.
1498 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001499 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500#endif
1501
1502 /* todo is number of windows left to create */
1503 for (todo = count - 1; todo > 0; --todo)
1504#ifdef FEAT_VERTSPLIT
1505 if (vertical)
1506 {
1507 if (win_split(curwin->w_width - (curwin->w_width - todo)
1508 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1509 break;
1510 }
1511 else
1512#endif
1513 {
1514 if (win_split(curwin->w_height - (curwin->w_height - todo
1515 * STATUS_HEIGHT) / (todo + 1)
1516 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1517 break;
1518 }
1519
1520#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001521 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522#endif
1523
1524 /* return actual number of windows */
1525 return (count - todo);
1526}
1527
1528/*
1529 * Exchange current and next window
1530 */
1531 static void
1532win_exchange(Prenum)
1533 long Prenum;
1534{
1535 frame_T *frp;
1536 frame_T *frp2;
1537 win_T *wp;
1538 win_T *wp2;
1539 int temp;
1540
1541 if (lastwin == firstwin) /* just one window */
1542 {
1543 beep_flush();
1544 return;
1545 }
1546
1547#ifdef FEAT_GUI
1548 need_mouse_correct = TRUE;
1549#endif
1550
1551 /*
1552 * find window to exchange with
1553 */
1554 if (Prenum)
1555 {
1556 frp = curwin->w_frame->fr_parent->fr_child;
1557 while (frp != NULL && --Prenum > 0)
1558 frp = frp->fr_next;
1559 }
1560 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1561 frp = curwin->w_frame->fr_next;
1562 else /* Swap last window in row/col with previous */
1563 frp = curwin->w_frame->fr_prev;
1564
1565 /* We can only exchange a window with another window, not with a frame
1566 * containing windows. */
1567 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1568 return;
1569 wp = frp->fr_win;
1570
1571/*
1572 * 1. remove curwin from the list. Remember after which window it was in wp2
1573 * 2. insert curwin before wp in the list
1574 * if wp != wp2
1575 * 3. remove wp from the list
1576 * 4. insert wp after wp2
1577 * 5. exchange the status line height and vsep width.
1578 */
1579 wp2 = curwin->w_prev;
1580 frp2 = curwin->w_frame->fr_prev;
1581 if (wp->w_prev != curwin)
1582 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001583 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 frame_remove(curwin->w_frame);
1585 win_append(wp->w_prev, curwin);
1586 frame_insert(frp, curwin->w_frame);
1587 }
1588 if (wp != wp2)
1589 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001590 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 frame_remove(wp->w_frame);
1592 win_append(wp2, wp);
1593 if (frp2 == NULL)
1594 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1595 else
1596 frame_append(frp2, wp->w_frame);
1597 }
1598 temp = curwin->w_status_height;
1599 curwin->w_status_height = wp->w_status_height;
1600 wp->w_status_height = temp;
1601#ifdef FEAT_VERTSPLIT
1602 temp = curwin->w_vsep_width;
1603 curwin->w_vsep_width = wp->w_vsep_width;
1604 wp->w_vsep_width = temp;
1605
1606 /* If the windows are not in the same frame, exchange the sizes to avoid
1607 * messing up the window layout. Otherwise fix the frame sizes. */
1608 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1609 {
1610 temp = curwin->w_height;
1611 curwin->w_height = wp->w_height;
1612 wp->w_height = temp;
1613 temp = curwin->w_width;
1614 curwin->w_width = wp->w_width;
1615 wp->w_width = temp;
1616 }
1617 else
1618 {
1619 frame_fix_height(curwin);
1620 frame_fix_height(wp);
1621 frame_fix_width(curwin);
1622 frame_fix_width(wp);
1623 }
1624#endif
1625
1626 (void)win_comp_pos(); /* recompute window positions */
1627
1628 win_enter(wp, TRUE);
1629 redraw_later(CLEAR);
1630}
1631
1632/*
1633 * rotate windows: if upwards TRUE the second window becomes the first one
1634 * if upwards FALSE the first window becomes the second one
1635 */
1636 static void
1637win_rotate(upwards, count)
1638 int upwards;
1639 int count;
1640{
1641 win_T *wp1;
1642 win_T *wp2;
1643 frame_T *frp;
1644 int n;
1645
1646 if (firstwin == lastwin) /* nothing to do */
1647 {
1648 beep_flush();
1649 return;
1650 }
1651
1652#ifdef FEAT_GUI
1653 need_mouse_correct = TRUE;
1654#endif
1655
1656#ifdef FEAT_VERTSPLIT
1657 /* Check if all frames in this row/col have one window. */
1658 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1659 frp = frp->fr_next)
1660 if (frp->fr_win == NULL)
1661 {
1662 EMSG(_("E443: Cannot rotate when another window is split"));
1663 return;
1664 }
1665#endif
1666
1667 while (count--)
1668 {
1669 if (upwards) /* first window becomes last window */
1670 {
1671 /* remove first window/frame from the list */
1672 frp = curwin->w_frame->fr_parent->fr_child;
1673 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001674 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 frame_remove(frp);
1676
1677 /* find last frame and append removed window/frame after it */
1678 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1679 ;
1680 win_append(frp->fr_win, wp1);
1681 frame_append(frp, wp1->w_frame);
1682
1683 wp2 = frp->fr_win; /* previously last window */
1684 }
1685 else /* last window becomes first window */
1686 {
1687 /* find last window/frame in the list and remove it */
1688 for (frp = curwin->w_frame; frp->fr_next != NULL;
1689 frp = frp->fr_next)
1690 ;
1691 wp1 = frp->fr_win;
1692 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001693 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 frame_remove(frp);
1695
1696 /* append the removed window/frame before the first in the list */
1697 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1698 frame_insert(frp->fr_parent->fr_child, frp);
1699 }
1700
1701 /* exchange status height and vsep width of old and new last window */
1702 n = wp2->w_status_height;
1703 wp2->w_status_height = wp1->w_status_height;
1704 wp1->w_status_height = n;
1705 frame_fix_height(wp1);
1706 frame_fix_height(wp2);
1707#ifdef FEAT_VERTSPLIT
1708 n = wp2->w_vsep_width;
1709 wp2->w_vsep_width = wp1->w_vsep_width;
1710 wp1->w_vsep_width = n;
1711 frame_fix_width(wp1);
1712 frame_fix_width(wp2);
1713#endif
1714
1715 /* recompute w_winrow and w_wincol for all windows */
1716 (void)win_comp_pos();
1717 }
1718
1719 redraw_later(CLEAR);
1720}
1721
1722/*
1723 * Move the current window to the very top/bottom/left/right of the screen.
1724 */
1725 static void
1726win_totop(size, flags)
1727 int size;
1728 int flags;
1729{
1730 int dir;
1731 int height = curwin->w_height;
1732
1733 if (lastwin == firstwin)
1734 {
1735 beep_flush();
1736 return;
1737 }
1738
1739 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001740 (void)winframe_remove(curwin, &dir, NULL);
1741 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 last_status(FALSE); /* may need to remove last status line */
1743 (void)win_comp_pos(); /* recompute window positions */
1744
1745 /* Split a window on the desired side and put the window there. */
1746 (void)win_split_ins(size, flags, curwin, dir);
1747 if (!(flags & WSP_VERT))
1748 {
1749 win_setheight(height);
1750 if (p_ea)
1751 win_equal(curwin, TRUE, 'v');
1752 }
1753
1754#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1755 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1756 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001757 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759}
1760
1761/*
1762 * Move window "win1" to below/right of "win2" and make "win1" the current
1763 * window. Only works within the same frame!
1764 */
1765 void
1766win_move_after(win1, win2)
1767 win_T *win1, *win2;
1768{
1769 int height;
1770
1771 /* check if the arguments are reasonable */
1772 if (win1 == win2)
1773 return;
1774
1775 /* check if there is something to do */
1776 if (win2->w_next != win1)
1777 {
1778 /* may need move the status line/vertical separator of the last window
1779 * */
1780 if (win1 == lastwin)
1781 {
1782 height = win1->w_prev->w_status_height;
1783 win1->w_prev->w_status_height = win1->w_status_height;
1784 win1->w_status_height = height;
1785#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001786 if (win1->w_prev->w_vsep_width == 1)
1787 {
1788 /* Remove the vertical separator from the last-but-one window,
1789 * add it to the last window. Adjust the frame widths. */
1790 win1->w_prev->w_vsep_width = 0;
1791 win1->w_prev->w_frame->fr_width -= 1;
1792 win1->w_vsep_width = 1;
1793 win1->w_frame->fr_width += 1;
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#endif
1796 }
1797 else if (win2 == lastwin)
1798 {
1799 height = win1->w_status_height;
1800 win1->w_status_height = win2->w_status_height;
1801 win2->w_status_height = height;
1802#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001803 if (win1->w_vsep_width == 1)
1804 {
1805 /* Remove the vertical separator from win1, add it to the last
1806 * window, win2. Adjust the frame widths. */
1807 win2->w_vsep_width = 1;
1808 win2->w_frame->fr_width += 1;
1809 win1->w_vsep_width = 0;
1810 win1->w_frame->fr_width -= 1;
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812#endif
1813 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001814 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 frame_remove(win1->w_frame);
1816 win_append(win2, win1);
1817 frame_append(win2->w_frame, win1->w_frame);
1818
1819 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1820 redraw_later(NOT_VALID);
1821 }
1822 win_enter(win1, FALSE);
1823}
1824
1825/*
1826 * Make all windows the same height.
1827 * 'next_curwin' will soon be the current window, make sure it has enough
1828 * rows.
1829 */
1830 void
1831win_equal(next_curwin, current, dir)
1832 win_T *next_curwin; /* pointer to current window to be or NULL */
1833 int current; /* do only frame with current window */
1834 int dir; /* 'v' for vertically, 'h' for horizontally,
1835 'b' for both, 0 for using p_ead */
1836{
1837 if (dir == 0)
1838#ifdef FEAT_VERTSPLIT
1839 dir = *p_ead;
1840#else
1841 dir = 'b';
1842#endif
1843 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001844 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001845 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846}
1847
1848/*
1849 * Set a frame to a new position and height, spreading the available room
1850 * equally over contained frames.
1851 * The window "next_curwin" (if not NULL) should at least get the size from
1852 * 'winheight' and 'winwidth' if possible.
1853 */
1854 static void
1855win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1856 win_T *next_curwin; /* pointer to current window to be or NULL */
1857 int current; /* do only frame with current window */
1858 frame_T *topfr; /* frame to set size off */
1859 int dir; /* 'v', 'h' or 'b', see win_equal() */
1860 int col; /* horizontal position for frame */
1861 int row; /* vertical position for frame */
1862 int width; /* new width of frame */
1863 int height; /* new height of frame */
1864{
1865 int n, m;
1866 int extra_sep = 0;
1867 int wincount, totwincount = 0;
1868 frame_T *fr;
1869 int next_curwin_size = 0;
1870 int room = 0;
1871 int new_size;
1872 int has_next_curwin = 0;
1873 int hnc;
1874
1875 if (topfr->fr_layout == FR_LEAF)
1876 {
1877 /* Set the width/height of this frame.
1878 * Redraw when size or position changes */
1879 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1880#ifdef FEAT_VERTSPLIT
1881 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1882#endif
1883 )
1884 {
1885 topfr->fr_win->w_winrow = row;
1886 frame_new_height(topfr, height, FALSE, FALSE);
1887#ifdef FEAT_VERTSPLIT
1888 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001889 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890#endif
1891 redraw_all_later(CLEAR);
1892 }
1893 }
1894#ifdef FEAT_VERTSPLIT
1895 else if (topfr->fr_layout == FR_ROW)
1896 {
1897 topfr->fr_width = width;
1898 topfr->fr_height = height;
1899
1900 if (dir != 'v') /* equalize frame widths */
1901 {
1902 /* Compute the maximum number of windows horizontally in this
1903 * frame. */
1904 n = frame_minwidth(topfr, NOWIN);
1905 /* add one for the rightmost window, it doesn't have a separator */
1906 if (col + width == Columns)
1907 extra_sep = 1;
1908 else
1909 extra_sep = 0;
1910 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001911 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001913 /*
1914 * Compute width for "next_curwin" window and room available for
1915 * other windows.
1916 * "m" is the minimal width when counting p_wiw for "next_curwin".
1917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 m = frame_minwidth(topfr, next_curwin);
1919 room = width - m;
1920 if (room < 0)
1921 {
1922 next_curwin_size = p_wiw + room;
1923 room = 0;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 else
1926 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001927 next_curwin_size = -1;
1928 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1929 {
1930 /* If 'winfixwidth' set keep the window width if
1931 * possible.
1932 * Watch out for this window being the next_curwin. */
1933 if (frame_fixed_width(fr))
1934 {
1935 n = frame_minwidth(fr, NOWIN);
1936 new_size = fr->fr_width;
1937 if (frame_has_win(fr, next_curwin))
1938 {
1939 room += p_wiw - p_wmw;
1940 next_curwin_size = 0;
1941 if (new_size < p_wiw)
1942 new_size = p_wiw;
1943 }
1944 else
1945 /* These windows don't use up room. */
1946 totwincount -= (n + (fr->fr_next == NULL
1947 ? extra_sep : 0)) / (p_wmw + 1);
1948 room -= new_size - n;
1949 if (room < 0)
1950 {
1951 new_size += room;
1952 room = 0;
1953 }
1954 fr->fr_newwidth = new_size;
1955 }
1956 }
1957 if (next_curwin_size == -1)
1958 {
1959 if (!has_next_curwin)
1960 next_curwin_size = 0;
1961 else if (totwincount > 1
1962 && (room + (totwincount - 2))
1963 / (totwincount - 1) > p_wiw)
1964 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001965 /* Can make all windows wider than 'winwidth', spread
1966 * the room equally. */
1967 next_curwin_size = (room + p_wiw
1968 + (totwincount - 1) * p_wmw
1969 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001970 room -= next_curwin_size - p_wiw;
1971 }
1972 else
1973 next_curwin_size = p_wiw;
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001976
1977 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 --totwincount; /* don't count curwin */
1979 }
1980
1981 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1982 {
1983 n = m = 0;
1984 wincount = 1;
1985 if (fr->fr_next == NULL)
1986 /* last frame gets all that remains (avoid roundoff error) */
1987 new_size = width;
1988 else if (dir == 'v')
1989 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001990 else if (frame_fixed_width(fr))
1991 {
1992 new_size = fr->fr_newwidth;
1993 wincount = 0; /* doesn't count as a sizeable window */
1994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 else
1996 {
1997 /* Compute the maximum number of windows horiz. in "fr". */
1998 n = frame_minwidth(fr, NOWIN);
1999 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2000 / (p_wmw + 1);
2001 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002002 if (has_next_curwin)
2003 hnc = frame_has_win(fr, next_curwin);
2004 else
2005 hnc = FALSE;
2006 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002008 if (totwincount == 0)
2009 new_size = room;
2010 else
2011 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002013 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 next_curwin_size -= p_wiw - (m - n);
2016 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002017 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002019 else
2020 room -= new_size;
2021 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002024 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 * window, unless equalizing all frames. */
2026 if (!current || dir != 'v' || topfr->fr_parent != NULL
2027 || (new_size != fr->fr_width)
2028 || frame_has_win(fr, next_curwin))
2029 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002030 new_size, height);
2031 col += new_size;
2032 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 totwincount -= wincount;
2034 }
2035 }
2036#endif
2037 else /* topfr->fr_layout == FR_COL */
2038 {
2039#ifdef FEAT_VERTSPLIT
2040 topfr->fr_width = width;
2041#endif
2042 topfr->fr_height = height;
2043
2044 if (dir != 'h') /* equalize frame heights */
2045 {
2046 /* Compute maximum number of windows vertically in this frame. */
2047 n = frame_minheight(topfr, NOWIN);
2048 /* add one for the bottom window if it doesn't have a statusline */
2049 if (row + height == cmdline_row && p_ls == 0)
2050 extra_sep = 1;
2051 else
2052 extra_sep = 0;
2053 totwincount = (n + extra_sep) / (p_wmh + 1);
2054 has_next_curwin = frame_has_win(topfr, next_curwin);
2055
2056 /*
2057 * Compute height for "next_curwin" window and room available for
2058 * other windows.
2059 * "m" is the minimal height when counting p_wh for "next_curwin".
2060 */
2061 m = frame_minheight(topfr, next_curwin);
2062 room = height - m;
2063 if (room < 0)
2064 {
2065 /* The room is less then 'winheight', use all space for the
2066 * current window. */
2067 next_curwin_size = p_wh + room;
2068 room = 0;
2069 }
2070 else
2071 {
2072 next_curwin_size = -1;
2073 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2074 {
2075 /* If 'winfixheight' set keep the window height if
2076 * possible.
2077 * Watch out for this window being the next_curwin. */
2078 if (frame_fixed_height(fr))
2079 {
2080 n = frame_minheight(fr, NOWIN);
2081 new_size = fr->fr_height;
2082 if (frame_has_win(fr, next_curwin))
2083 {
2084 room += p_wh - p_wmh;
2085 next_curwin_size = 0;
2086 if (new_size < p_wh)
2087 new_size = p_wh;
2088 }
2089 else
2090 /* These windows don't use up room. */
2091 totwincount -= (n + (fr->fr_next == NULL
2092 ? extra_sep : 0)) / (p_wmh + 1);
2093 room -= new_size - n;
2094 if (room < 0)
2095 {
2096 new_size += room;
2097 room = 0;
2098 }
2099 fr->fr_newheight = new_size;
2100 }
2101 }
2102 if (next_curwin_size == -1)
2103 {
2104 if (!has_next_curwin)
2105 next_curwin_size = 0;
2106 else if (totwincount > 1
2107 && (room + (totwincount - 2))
2108 / (totwincount - 1) > p_wh)
2109 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002110 /* can make all windows higher than 'winheight',
2111 * spread the room equally. */
2112 next_curwin_size = (room + p_wh
2113 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 + (totwincount - 1)) / totwincount;
2115 room -= next_curwin_size - p_wh;
2116 }
2117 else
2118 next_curwin_size = p_wh;
2119 }
2120 }
2121
2122 if (has_next_curwin)
2123 --totwincount; /* don't count curwin */
2124 }
2125
2126 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2127 {
2128 n = m = 0;
2129 wincount = 1;
2130 if (fr->fr_next == NULL)
2131 /* last frame gets all that remains (avoid roundoff error) */
2132 new_size = height;
2133 else if (dir == 'h')
2134 new_size = fr->fr_height;
2135 else if (frame_fixed_height(fr))
2136 {
2137 new_size = fr->fr_newheight;
2138 wincount = 0; /* doesn't count as a sizeable window */
2139 }
2140 else
2141 {
2142 /* Compute the maximum number of windows vert. in "fr". */
2143 n = frame_minheight(fr, NOWIN);
2144 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2145 / (p_wmh + 1);
2146 m = frame_minheight(fr, next_curwin);
2147 if (has_next_curwin)
2148 hnc = frame_has_win(fr, next_curwin);
2149 else
2150 hnc = FALSE;
2151 if (hnc) /* don't count next_curwin */
2152 --wincount;
2153 if (totwincount == 0)
2154 new_size = room;
2155 else
2156 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2157 / totwincount;
2158 if (hnc) /* add next_curwin size */
2159 {
2160 next_curwin_size -= p_wh - (m - n);
2161 new_size += next_curwin_size;
2162 room -= new_size - next_curwin_size;
2163 }
2164 else
2165 room -= new_size;
2166 new_size += n;
2167 }
2168 /* Skip frame that is full width when splitting or closing a
2169 * window, unless equalizing all frames. */
2170 if (!current || dir != 'h' || topfr->fr_parent != NULL
2171 || (new_size != fr->fr_height)
2172 || frame_has_win(fr, next_curwin))
2173 win_equal_rec(next_curwin, current, fr, dir, col, row,
2174 width, new_size);
2175 row += new_size;
2176 height -= new_size;
2177 totwincount -= wincount;
2178 }
2179 }
2180}
2181
2182/*
2183 * close all windows for buffer 'buf'
2184 */
2185 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002186close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002188 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002190 win_T *wp;
2191 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002192 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002195
2196 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002198 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2199#ifdef FEAT_AUTOCMD
2200 && !(wp->w_closing || wp->w_buffer->b_closing)
2201#endif
2202 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002204 win_close(wp, FALSE);
2205
2206 /* Start all over, autocommands may change the window layout. */
2207 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002210 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002212
2213 /* Also check windows in other tab pages. */
2214 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2215 {
2216 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002217 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002218 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002219 if (wp->w_buffer == buf
2220#ifdef FEAT_AUTOCMD
2221 && !(wp->w_closing || wp->w_buffer->b_closing)
2222#endif
2223 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224 {
2225 win_close_othertab(wp, FALSE, tp);
2226
2227 /* Start all over, the tab page may be closed and
2228 * autocommands may change the window layout. */
2229 nexttp = first_tabpage;
2230 break;
2231 }
2232 }
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002235
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002236 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002237 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002238 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239}
2240
2241/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002242 * Return TRUE if the current window is the only window that exists (ignoring
2243 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002244 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002245 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002246 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002247last_window()
2248{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002249 return (one_window() && first_tabpage->tp_next == NULL);
2250}
2251
2252/*
2253 * Return TRUE if there is only one window other than "aucmd_win" in the
2254 * current tab page.
2255 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002256 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002257one_window()
2258{
2259#ifdef FEAT_AUTOCMD
2260 win_T *wp;
2261 int seen_one = FALSE;
2262
2263 FOR_ALL_WINDOWS(wp)
2264 {
2265 if (wp != aucmd_win)
2266 {
2267 if (seen_one)
2268 return FALSE;
2269 seen_one = TRUE;
2270 }
2271 }
2272 return TRUE;
2273#else
2274 return firstwin == lastwin;
2275#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002276}
2277
2278/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002279 * Close the possibly last window in a tab page.
2280 * Returns TRUE when the window was closed already.
2281 */
2282 static int
2283close_last_window_tabpage(win, free_buf, prev_curtab)
2284 win_T *win;
2285 int free_buf;
2286 tabpage_T *prev_curtab;
2287{
2288 if (firstwin == lastwin)
2289 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002290#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002291 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002292#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002293
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002294 /*
2295 * Closing the last window in a tab page. First go to another tab
2296 * page and then close the window and the tab page. This avoids that
2297 * curwin and curtab are invalid while we are freeing memory, they may
2298 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002299 * Don't trigger autocommands yet, they may use wrong values, so do
2300 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002301 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002302 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002303 redraw_tabline = TRUE;
2304
2305 /* Safety check: Autocommands may have closed the window when jumping
2306 * to the other tab page. */
2307 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2308 {
2309 int h = tabline_height();
2310
2311 win_close_othertab(win, free_buf, prev_curtab);
2312 if (h != tabline_height())
2313 shell_new_rows();
2314 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002315 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2316 * that now. */
2317#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002318 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002319 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2320 if (old_curbuf != curbuf)
2321 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002322#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002323 return TRUE;
2324 }
2325 return FALSE;
2326}
2327
2328/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002329 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 * If "free_buf" is TRUE related buffer may be unloaded.
2331 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002332 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002333 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002335 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336win_close(win, free_buf)
2337 win_T *win;
2338 int free_buf;
2339{
2340 win_T *wp;
2341#ifdef FEAT_AUTOCMD
2342 int other_buffer = FALSE;
2343#endif
2344 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 int dir;
2346 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002347 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002349 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002355#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002356 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002357 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002358 if (win == aucmd_win)
2359 {
2360 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002361 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002362 }
2363 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2364 {
2365 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002366 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002367 }
2368#endif
2369
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002370 /* When closing the last window in a tab page first go to another tab page
2371 * and then close the window and the tab page to avoid that curwin and
2372 * curtab are invalid while we are freeing memory. */
2373 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002374 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002375
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 /* When closing the help window, try restoring a snapshot after closing
2377 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002378 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 help_window = TRUE;
2380 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002381 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383#ifdef FEAT_AUTOCMD
2384 if (win == curwin)
2385 {
2386 /*
2387 * Guess which window is going to be the new current window.
2388 * This may change because of the autocommands (sigh).
2389 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002390 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002393 * Be careful: If autocommands delete the window or cause this window
2394 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 */
2396 if (wp->w_buffer != curbuf)
2397 {
2398 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002399 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002401 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002402 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002403 win->w_closing = FALSE;
2404 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002407 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002409 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002410 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002411 win->w_closing = FALSE;
2412 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414# ifdef FEAT_EVAL
2415 /* autocmds may abort script processing */
2416 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418# endif
2419 }
2420#endif
2421
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002422#ifdef FEAT_GUI
2423 /* Avoid trouble with scrollbars that are going to be deleted in
2424 * win_free(). */
2425 if (gui.in_use)
2426 out_flush();
2427#endif
2428
Bram Moolenaara971b822011-09-14 14:43:25 +02002429#ifdef FEAT_SYN_HL
2430 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002431 if (win->w_buffer != NULL)
2432 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002433#endif
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 /*
2436 * Close the link to the buffer.
2437 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002438 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002439 {
2440#ifdef FEAT_AUTOCMD
2441 win->w_closing = TRUE;
2442#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002443 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002444#ifdef FEAT_AUTOCMD
2445 if (win_valid(win))
2446 win->w_closing = FALSE;
2447#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002448 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2449 * "wipe". */
2450 if (!buf_valid(curbuf))
2451 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002452 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002453
Bram Moolenaar802418d2013-01-17 14:00:11 +01002454 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2455 && (last_window() || curtab != prev_curtab
2456 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002457 {
2458 /* Autocommands have close all windows, quit now. Restore
2459 * curwin->w_buffer, otherwise writing viminfo may fail. */
2460 if (curwin->w_buffer == NULL)
2461 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002462 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002463 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002466 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002467 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002468 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002469 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaara971b822011-09-14 14:43:25 +02002471 /* Free the memory used for the window and get the window that received
2472 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002473 wp = win_free_mem(win, &dir, NULL);
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 /* Make sure curwin isn't invalid. It can cause severe trouble when
2476 * printing an error message. For win_equal() curbuf needs to be valid
2477 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002478 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 curwin = wp;
2481#ifdef FEAT_QUICKFIX
2482 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2483 {
2484 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002485 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 * finding another window to go to.
2487 */
2488 for (;;)
2489 {
2490 if (wp->w_next == NULL)
2491 wp = firstwin;
2492 else
2493 wp = wp->w_next;
2494 if (wp == curwin)
2495 break;
2496 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2497 {
2498 curwin = wp;
2499 break;
2500 }
2501 }
2502 }
2503#endif
2504 curbuf = curwin->w_buffer;
2505 close_curwin = TRUE;
2506 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002507 if (p_ea
2508#ifdef FEAT_VERTSPLIT
2509 && (*p_ead == 'b' || *p_ead == dir)
2510#endif
2511 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 win_equal(curwin, TRUE,
2513#ifdef FEAT_VERTSPLIT
2514 dir
2515#else
2516 0
2517#endif
2518 );
2519 else
2520 win_comp_pos();
2521 if (close_curwin)
2522 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002523 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524#ifdef FEAT_AUTOCMD
2525 if (other_buffer)
2526 /* careful: after this wp and win may be invalid! */
2527 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2528#endif
2529 }
2530
2531 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002532 * If last window has a status line now and we don't want one,
2533 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 */
2535 last_status(FALSE);
2536
2537 /* After closing the help window, try restoring the window layout from
2538 * before it was opened. */
2539 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002540 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2543 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2544 if (gui.in_use && !win_hasvertsplit())
2545 gui_init_which_components(NULL);
2546#endif
2547
2548 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002549 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550}
2551
2552/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002553 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002554 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002555 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002556 * Caller must check if buffer is hidden and whether the tabline needs to be
2557 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002558 */
2559 void
2560win_close_othertab(win, free_buf, tp)
2561 win_T *win;
2562 int free_buf;
2563 tabpage_T *tp;
2564{
2565 win_T *wp;
2566 int dir;
2567 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002568 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569
Bram Moolenaar362ce482012-06-06 19:02:45 +02002570#ifdef FEAT_AUTOCMD
2571 if (win->w_closing || win->w_buffer->b_closing)
2572 return; /* window is already being closed */
2573#endif
2574
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002576 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577
2578 /* Careful: Autocommands may have closed the tab page or made it the
2579 * current tab page. */
2580 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2581 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002582 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 return;
2584
2585 /* Autocommands may have closed the window already. */
2586 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2587 ;
2588 if (wp == NULL)
2589 return;
2590
Bram Moolenaarf740b292006-02-16 22:11:02 +00002591 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002592 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002593 {
2594 if (tp == first_tabpage)
2595 first_tabpage = tp->tp_next;
2596 else
2597 {
2598 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2599 ptp = ptp->tp_next)
2600 ;
2601 if (ptp == NULL)
2602 {
2603 EMSG2(_(e_intern2), "win_close_othertab()");
2604 return;
2605 }
2606 ptp->tp_next = tp->tp_next;
2607 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002608 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002609 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002610
2611 /* Free the memory used for the window. */
2612 win_free_mem(win, &dir, tp);
2613
2614 if (free_tp)
2615 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616}
2617
2618/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002619 * Free the memory used for a window.
2620 * Returns a pointer to the window that got the freed up space.
2621 */
2622 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002624 win_T *win;
2625 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002627{
2628 frame_T *frp;
2629 win_T *wp;
2630
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002631 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002632 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002633 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002634 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002635 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002636
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002637 /* When deleting the current window of another tab page select a new
2638 * current window. */
2639 if (tp != NULL && win == tp->tp_curwin)
2640 tp->tp_curwin = wp;
2641
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002642 return wp;
2643}
2644
2645#if defined(EXITFREE) || defined(PROTO)
2646 void
2647win_free_all()
2648{
2649 int dummy;
2650
Bram Moolenaarf740b292006-02-16 22:11:02 +00002651# ifdef FEAT_WINDOWS
2652 while (first_tabpage->tp_next != NULL)
2653 tabpage_close(TRUE);
2654# endif
2655
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002656# ifdef FEAT_AUTOCMD
2657 if (aucmd_win != NULL)
2658 {
2659 (void)win_free_mem(aucmd_win, &dummy, NULL);
2660 aucmd_win = NULL;
2661 }
2662# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002663
2664 while (firstwin != NULL)
2665 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002666
2667 /* No window should be used after this. Set curwin to NULL to crash
2668 * instead of using freed memory. */
2669 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002670}
2671#endif
2672
2673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 * Remove a window and its frame from the tree of frames.
2675 * Returns a pointer to the window that got the freed up space.
2676 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002677 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002680 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002681 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 frame_T *frp, *frp2, *frp3;
2684 frame_T *frp_close = win->w_frame;
2685 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002688 * If there is only one window there is nothing to remove.
2689 */
2690 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2691 return NULL;
2692
2693 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 * Remove the window from its frame.
2695 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002696 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 wp = frame2win(frp2);
2698
2699 /* Remove this frame from the list of frames. */
2700 frame_remove(frp_close);
2701
2702#ifdef FEAT_VERTSPLIT
2703 if (frp_close->fr_parent->fr_layout == FR_COL)
2704 {
2705#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002706 /* When 'winfixheight' is set, try to find another frame in the column
2707 * (as close to the closed frame as possible) to distribute the height
2708 * to. */
2709 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2710 {
2711 frp = frp_close->fr_prev;
2712 frp3 = frp_close->fr_next;
2713 while (frp != NULL || frp3 != NULL)
2714 {
2715 if (frp != NULL)
2716 {
2717 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2718 {
2719 frp2 = frp;
2720 wp = frp->fr_win;
2721 break;
2722 }
2723 frp = frp->fr_prev;
2724 }
2725 if (frp3 != NULL)
2726 {
2727 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2728 {
2729 frp2 = frp3;
2730 wp = frp3->fr_win;
2731 break;
2732 }
2733 frp3 = frp3->fr_next;
2734 }
2735 }
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2738 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#ifdef FEAT_VERTSPLIT
2740 *dirp = 'v';
2741 }
2742 else
2743 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002744 /* When 'winfixwidth' is set, try to find another frame in the column
2745 * (as close to the closed frame as possible) to distribute the width
2746 * to. */
2747 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2748 {
2749 frp = frp_close->fr_prev;
2750 frp3 = frp_close->fr_next;
2751 while (frp != NULL || frp3 != NULL)
2752 {
2753 if (frp != NULL)
2754 {
2755 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2756 {
2757 frp2 = frp;
2758 wp = frp->fr_win;
2759 break;
2760 }
2761 frp = frp->fr_prev;
2762 }
2763 if (frp3 != NULL)
2764 {
2765 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2766 {
2767 frp2 = frp3;
2768 wp = frp3->fr_win;
2769 break;
2770 }
2771 frp3 = frp3->fr_next;
2772 }
2773 }
2774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002776 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 *dirp = 'h';
2778 }
2779#endif
2780
2781 /* If rows/columns go to a window below/right its positions need to be
2782 * updated. Can only be done after the sizes have been updated. */
2783 if (frp2 == frp_close->fr_next)
2784 {
2785 int row = win->w_winrow;
2786 int col = W_WINCOL(win);
2787
2788 frame_comp_pos(frp2, &row, &col);
2789 }
2790
2791 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2792 {
2793 /* There is no other frame in this list, move its info to the parent
2794 * and remove it. */
2795 frp2->fr_parent->fr_layout = frp2->fr_layout;
2796 frp2->fr_parent->fr_child = frp2->fr_child;
2797 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2798 frp->fr_parent = frp2->fr_parent;
2799 frp2->fr_parent->fr_win = frp2->fr_win;
2800 if (frp2->fr_win != NULL)
2801 frp2->fr_win->w_frame = frp2->fr_parent;
2802 frp = frp2->fr_parent;
2803 vim_free(frp2);
2804
2805 frp2 = frp->fr_parent;
2806 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2807 {
2808 /* The frame above the parent has the same layout, have to merge
2809 * the frames into this list. */
2810 if (frp2->fr_child == frp)
2811 frp2->fr_child = frp->fr_child;
2812 frp->fr_child->fr_prev = frp->fr_prev;
2813 if (frp->fr_prev != NULL)
2814 frp->fr_prev->fr_next = frp->fr_child;
2815 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2816 {
2817 frp3->fr_parent = frp2;
2818 if (frp3->fr_next == NULL)
2819 {
2820 frp3->fr_next = frp->fr_next;
2821 if (frp->fr_next != NULL)
2822 frp->fr_next->fr_prev = frp3;
2823 break;
2824 }
2825 }
2826 vim_free(frp);
2827 }
2828 }
2829
2830 return wp;
2831}
2832
2833/*
2834 * Find out which frame is going to get the freed up space when "win" is
2835 * closed.
2836 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2837 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2838 * This makes opening a window and closing it immediately keep the same window
2839 * layout.
2840 */
2841 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002842win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002844 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
2846 frame_T *frp;
2847 int b;
2848
Bram Moolenaarf740b292006-02-16 22:11:02 +00002849 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002850 /* Last window in this tab page, will go to next tab page. */
2851 return alt_tabpage()->tp_curwin->w_frame;
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 frp = win->w_frame;
2854#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002855 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 b = p_spr;
2857 else
2858#endif
2859 b = p_sb;
2860 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2861 return frp->fr_next;
2862 return frp->fr_prev;
2863}
2864
2865/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002866 * Return the tabpage that will be used if the current one is closed.
2867 */
2868 static tabpage_T *
2869alt_tabpage()
2870{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002871 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002872
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002873 /* Use the next tab page if possible. */
2874 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002875 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002876
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002877 /* Find the last but one tab page. */
2878 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2879 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002880 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002881}
2882
2883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 * Find the left-upper window in frame "frp".
2885 */
2886 static win_T *
2887frame2win(frp)
2888 frame_T *frp;
2889{
2890 while (frp->fr_win == NULL)
2891 frp = frp->fr_child;
2892 return frp->fr_win;
2893}
2894
2895/*
2896 * Return TRUE if frame "frp" contains window "wp".
2897 */
2898 static int
2899frame_has_win(frp, wp)
2900 frame_T *frp;
2901 win_T *wp;
2902{
2903 frame_T *p;
2904
2905 if (frp->fr_layout == FR_LEAF)
2906 return frp->fr_win == wp;
2907
2908 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2909 if (frame_has_win(p, wp))
2910 return TRUE;
2911 return FALSE;
2912}
2913
2914/*
2915 * Set a new height for a frame. Recursively sets the height for contained
2916 * frames and windows. Caller must take care of positions.
2917 */
2918 static void
2919frame_new_height(topfrp, height, topfirst, wfh)
2920 frame_T *topfrp;
2921 int height;
2922 int topfirst; /* resize topmost contained frame first */
2923 int wfh; /* obey 'winfixheight' when there is a choice;
2924 may cause the height not to be set */
2925{
2926 frame_T *frp;
2927 int extra_lines;
2928 int h;
2929
2930 if (topfrp->fr_win != NULL)
2931 {
2932 /* Simple case: just one window. */
2933 win_new_height(topfrp->fr_win,
2934 height - topfrp->fr_win->w_status_height);
2935 }
2936#ifdef FEAT_VERTSPLIT
2937 else if (topfrp->fr_layout == FR_ROW)
2938 {
2939 do
2940 {
2941 /* All frames in this row get the same new height. */
2942 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2943 {
2944 frame_new_height(frp, height, topfirst, wfh);
2945 if (frp->fr_height > height)
2946 {
2947 /* Could not fit the windows, make the whole row higher. */
2948 height = frp->fr_height;
2949 break;
2950 }
2951 }
2952 }
2953 while (frp != NULL);
2954 }
2955#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002956 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
2958 /* Complicated case: Resize a column of frames. Resize the bottom
2959 * frame first, frames above that when needed. */
2960
2961 frp = topfrp->fr_child;
2962 if (wfh)
2963 /* Advance past frames with one window with 'wfh' set. */
2964 while (frame_fixed_height(frp))
2965 {
2966 frp = frp->fr_next;
2967 if (frp == NULL)
2968 return; /* no frame without 'wfh', give up */
2969 }
2970 if (!topfirst)
2971 {
2972 /* Find the bottom frame of this column */
2973 while (frp->fr_next != NULL)
2974 frp = frp->fr_next;
2975 if (wfh)
2976 /* Advance back for frames with one window with 'wfh' set. */
2977 while (frame_fixed_height(frp))
2978 frp = frp->fr_prev;
2979 }
2980
2981 extra_lines = height - topfrp->fr_height;
2982 if (extra_lines < 0)
2983 {
2984 /* reduce height of contained frames, bottom or top frame first */
2985 while (frp != NULL)
2986 {
2987 h = frame_minheight(frp, NULL);
2988 if (frp->fr_height + extra_lines < h)
2989 {
2990 extra_lines += frp->fr_height - h;
2991 frame_new_height(frp, h, topfirst, wfh);
2992 }
2993 else
2994 {
2995 frame_new_height(frp, frp->fr_height + extra_lines,
2996 topfirst, wfh);
2997 break;
2998 }
2999 if (topfirst)
3000 {
3001 do
3002 frp = frp->fr_next;
3003 while (wfh && frp != NULL && frame_fixed_height(frp));
3004 }
3005 else
3006 {
3007 do
3008 frp = frp->fr_prev;
3009 while (wfh && frp != NULL && frame_fixed_height(frp));
3010 }
3011 /* Increase "height" if we could not reduce enough frames. */
3012 if (frp == NULL)
3013 height -= extra_lines;
3014 }
3015 }
3016 else if (extra_lines > 0)
3017 {
3018 /* increase height of bottom or top frame */
3019 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3020 }
3021 }
3022 topfrp->fr_height = height;
3023}
3024
3025/*
3026 * Return TRUE if height of frame "frp" should not be changed because of
3027 * the 'winfixheight' option.
3028 */
3029 static int
3030frame_fixed_height(frp)
3031 frame_T *frp;
3032{
3033 /* frame with one window: fixed height if 'winfixheight' set. */
3034 if (frp->fr_win != NULL)
3035 return frp->fr_win->w_p_wfh;
3036
3037 if (frp->fr_layout == FR_ROW)
3038 {
3039 /* The frame is fixed height if one of the frames in the row is fixed
3040 * height. */
3041 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3042 if (frame_fixed_height(frp))
3043 return TRUE;
3044 return FALSE;
3045 }
3046
3047 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3048 * frames in the row are fixed height. */
3049 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3050 if (!frame_fixed_height(frp))
3051 return FALSE;
3052 return TRUE;
3053}
3054
3055#ifdef FEAT_VERTSPLIT
3056/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003057 * Return TRUE if width of frame "frp" should not be changed because of
3058 * the 'winfixwidth' option.
3059 */
3060 static int
3061frame_fixed_width(frp)
3062 frame_T *frp;
3063{
3064 /* frame with one window: fixed width if 'winfixwidth' set. */
3065 if (frp->fr_win != NULL)
3066 return frp->fr_win->w_p_wfw;
3067
3068 if (frp->fr_layout == FR_COL)
3069 {
3070 /* The frame is fixed width if one of the frames in the row is fixed
3071 * width. */
3072 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3073 if (frame_fixed_width(frp))
3074 return TRUE;
3075 return FALSE;
3076 }
3077
3078 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3079 * frames in the row are fixed width. */
3080 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3081 if (!frame_fixed_width(frp))
3082 return FALSE;
3083 return TRUE;
3084}
3085
3086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * Add a status line to windows at the bottom of "frp".
3088 * Note: Does not check if there is room!
3089 */
3090 static void
3091frame_add_statusline(frp)
3092 frame_T *frp;
3093{
3094 win_T *wp;
3095
3096 if (frp->fr_layout == FR_LEAF)
3097 {
3098 wp = frp->fr_win;
3099 if (wp->w_status_height == 0)
3100 {
3101 if (wp->w_height > 0) /* don't make it negative */
3102 --wp->w_height;
3103 wp->w_status_height = STATUS_HEIGHT;
3104 }
3105 }
3106 else if (frp->fr_layout == FR_ROW)
3107 {
3108 /* Handle all the frames in the row. */
3109 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3110 frame_add_statusline(frp);
3111 }
3112 else /* frp->fr_layout == FR_COL */
3113 {
3114 /* Only need to handle the last frame in the column. */
3115 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3116 ;
3117 frame_add_statusline(frp);
3118 }
3119}
3120
3121/*
3122 * Set width of a frame. Handles recursively going through contained frames.
3123 * May remove separator line for windows at the right side (for win_close()).
3124 */
3125 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003126frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 frame_T *topfrp;
3128 int width;
3129 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003130 int wfw; /* obey 'winfixwidth' when there is a choice;
3131 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 frame_T *frp;
3134 int extra_cols;
3135 int w;
3136 win_T *wp;
3137
3138 if (topfrp->fr_layout == FR_LEAF)
3139 {
3140 /* Simple case: just one window. */
3141 wp = topfrp->fr_win;
3142 /* Find out if there are any windows right of this one. */
3143 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3144 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3145 break;
3146 if (frp->fr_parent == NULL)
3147 wp->w_vsep_width = 0;
3148 win_new_width(wp, width - wp->w_vsep_width);
3149 }
3150 else if (topfrp->fr_layout == FR_COL)
3151 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003152 do
3153 {
3154 /* All frames in this column get the same new width. */
3155 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3156 {
3157 frame_new_width(frp, width, leftfirst, wfw);
3158 if (frp->fr_width > width)
3159 {
3160 /* Could not fit the windows, make whole column wider. */
3161 width = frp->fr_width;
3162 break;
3163 }
3164 }
3165 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167 else /* fr_layout == FR_ROW */
3168 {
3169 /* Complicated case: Resize a row of frames. Resize the rightmost
3170 * frame first, frames left of it when needed. */
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003173 if (wfw)
3174 /* Advance past frames with one window with 'wfw' set. */
3175 while (frame_fixed_width(frp))
3176 {
3177 frp = frp->fr_next;
3178 if (frp == NULL)
3179 return; /* no frame without 'wfw', give up */
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003182 {
3183 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 while (frp->fr_next != NULL)
3185 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003186 if (wfw)
3187 /* Advance back for frames with one window with 'wfw' set. */
3188 while (frame_fixed_width(frp))
3189 frp = frp->fr_prev;
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 extra_cols = width - topfrp->fr_width;
3193 if (extra_cols < 0)
3194 {
3195 /* reduce frame width, rightmost frame first */
3196 while (frp != NULL)
3197 {
3198 w = frame_minwidth(frp, NULL);
3199 if (frp->fr_width + extra_cols < w)
3200 {
3201 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003202 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 }
3204 else
3205 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003206 frame_new_width(frp, frp->fr_width + extra_cols,
3207 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 break;
3209 }
3210 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003211 {
3212 do
3213 frp = frp->fr_next;
3214 while (wfw && frp != NULL && frame_fixed_width(frp));
3215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003217 {
3218 do
3219 frp = frp->fr_prev;
3220 while (wfw && frp != NULL && frame_fixed_width(frp));
3221 }
3222 /* Increase "width" if we could not reduce enough frames. */
3223 if (frp == NULL)
3224 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 }
3226 }
3227 else if (extra_cols > 0)
3228 {
3229 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003230 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 }
3232 }
3233 topfrp->fr_width = width;
3234}
3235
3236/*
3237 * Add the vertical separator to windows at the right side of "frp".
3238 * Note: Does not check if there is room!
3239 */
3240 static void
3241frame_add_vsep(frp)
3242 frame_T *frp;
3243{
3244 win_T *wp;
3245
3246 if (frp->fr_layout == FR_LEAF)
3247 {
3248 wp = frp->fr_win;
3249 if (wp->w_vsep_width == 0)
3250 {
3251 if (wp->w_width > 0) /* don't make it negative */
3252 --wp->w_width;
3253 wp->w_vsep_width = 1;
3254 }
3255 }
3256 else if (frp->fr_layout == FR_COL)
3257 {
3258 /* Handle all the frames in the column. */
3259 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3260 frame_add_vsep(frp);
3261 }
3262 else /* frp->fr_layout == FR_ROW */
3263 {
3264 /* Only need to handle the last frame in the row. */
3265 frp = frp->fr_child;
3266 while (frp->fr_next != NULL)
3267 frp = frp->fr_next;
3268 frame_add_vsep(frp);
3269 }
3270}
3271
3272/*
3273 * Set frame width from the window it contains.
3274 */
3275 static void
3276frame_fix_width(wp)
3277 win_T *wp;
3278{
3279 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3280}
3281#endif
3282
3283/*
3284 * Set frame height from the window it contains.
3285 */
3286 static void
3287frame_fix_height(wp)
3288 win_T *wp;
3289{
3290 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3291}
3292
3293/*
3294 * Compute the minimal height for frame "topfrp".
3295 * Uses the 'winminheight' option.
3296 * When "next_curwin" isn't NULL, use p_wh for this window.
3297 * When "next_curwin" is NOWIN, don't use at least one line for the current
3298 * window.
3299 */
3300 static int
3301frame_minheight(topfrp, next_curwin)
3302 frame_T *topfrp;
3303 win_T *next_curwin;
3304{
3305 frame_T *frp;
3306 int m;
3307#ifdef FEAT_VERTSPLIT
3308 int n;
3309#endif
3310
3311 if (topfrp->fr_win != NULL)
3312 {
3313 if (topfrp->fr_win == next_curwin)
3314 m = p_wh + topfrp->fr_win->w_status_height;
3315 else
3316 {
3317 /* window: minimal height of the window plus status line */
3318 m = p_wmh + topfrp->fr_win->w_status_height;
3319 /* Current window is minimal one line high */
3320 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3321 ++m;
3322 }
3323 }
3324#ifdef FEAT_VERTSPLIT
3325 else if (topfrp->fr_layout == FR_ROW)
3326 {
3327 /* get the minimal height from each frame in this row */
3328 m = 0;
3329 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3330 {
3331 n = frame_minheight(frp, next_curwin);
3332 if (n > m)
3333 m = n;
3334 }
3335 }
3336#endif
3337 else
3338 {
3339 /* Add up the minimal heights for all frames in this column. */
3340 m = 0;
3341 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3342 m += frame_minheight(frp, next_curwin);
3343 }
3344
3345 return m;
3346}
3347
3348#ifdef FEAT_VERTSPLIT
3349/*
3350 * Compute the minimal width for frame "topfrp".
3351 * When "next_curwin" isn't NULL, use p_wiw for this window.
3352 * When "next_curwin" is NOWIN, don't use at least one column for the current
3353 * window.
3354 */
3355 static int
3356frame_minwidth(topfrp, next_curwin)
3357 frame_T *topfrp;
3358 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3359{
3360 frame_T *frp;
3361 int m, n;
3362
3363 if (topfrp->fr_win != NULL)
3364 {
3365 if (topfrp->fr_win == next_curwin)
3366 m = p_wiw + topfrp->fr_win->w_vsep_width;
3367 else
3368 {
3369 /* window: minimal width of the window plus separator column */
3370 m = p_wmw + topfrp->fr_win->w_vsep_width;
3371 /* Current window is minimal one column wide */
3372 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3373 ++m;
3374 }
3375 }
3376 else if (topfrp->fr_layout == FR_COL)
3377 {
3378 /* get the minimal width from each frame in this column */
3379 m = 0;
3380 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3381 {
3382 n = frame_minwidth(frp, next_curwin);
3383 if (n > m)
3384 m = n;
3385 }
3386 }
3387 else
3388 {
3389 /* Add up the minimal widths for all frames in this row. */
3390 m = 0;
3391 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3392 m += frame_minwidth(frp, next_curwin);
3393 }
3394
3395 return m;
3396}
3397#endif
3398
3399
3400/*
3401 * Try to close all windows except current one.
3402 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3403 * used and the buffer was modified.
3404 *
3405 * Used by ":bdel" and ":only".
3406 */
3407 void
3408close_others(message, forceit)
3409 int message;
3410 int forceit; /* always hide all other windows */
3411{
3412 win_T *wp;
3413 win_T *nextwp;
3414 int r;
3415
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003416 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 if (message
3419#ifdef FEAT_AUTOCMD
3420 && !autocmd_busy
3421#endif
3422 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003423 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 return;
3425 }
3426
3427 /* Be very careful here: autocommands may change the window layout. */
3428 for (wp = firstwin; win_valid(wp); wp = nextwp)
3429 {
3430 nextwp = wp->w_next;
3431 if (wp != curwin) /* don't close current window */
3432 {
3433
3434 /* Check if it's allowed to abandon this window */
3435 r = can_abandon(wp->w_buffer, forceit);
3436#ifdef FEAT_AUTOCMD
3437 if (!win_valid(wp)) /* autocommands messed wp up */
3438 {
3439 nextwp = firstwin;
3440 continue;
3441 }
3442#endif
3443 if (!r)
3444 {
3445#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3446 if (message && (p_confirm || cmdmod.confirm) && p_write)
3447 {
3448 dialog_changed(wp->w_buffer, FALSE);
3449# ifdef FEAT_AUTOCMD
3450 if (!win_valid(wp)) /* autocommands messed wp up */
3451 {
3452 nextwp = firstwin;
3453 continue;
3454 }
3455# endif
3456 }
3457 if (bufIsChanged(wp->w_buffer))
3458#endif
3459 continue;
3460 }
3461 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3462 }
3463 }
3464
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003465 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 EMSG(_("E445: Other window contains changes"));
3467}
3468
3469#endif /* FEAT_WINDOWS */
3470
3471/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003472 * Init the current window "curwin".
3473 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
3475 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003476curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003478 win_init_empty(curwin);
3479}
3480
3481 void
3482win_init_empty(wp)
3483 win_T *wp;
3484{
3485 redraw_win_later(wp, NOT_VALID);
3486 wp->w_lines_valid = 0;
3487 wp->w_cursor.lnum = 1;
3488 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003490 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003492 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3493 wp->w_pcmark.col = 0;
3494 wp->w_prev_pcmark.lnum = 0;
3495 wp->w_prev_pcmark.col = 0;
3496 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003498 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003500 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003502 if (wp->w_p_rl)
3503 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003505 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003507#ifdef FEAT_SYN_HL
3508 wp->w_s = &wp->w_buffer->b_s;
3509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510}
3511
3512/*
3513 * Allocate the first window and put an empty buffer in it.
3514 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518win_alloc_first()
3519{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003520 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521 return FAIL;
3522
3523#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003524 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525 if (first_tabpage == NULL)
3526 return FAIL;
3527 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003528 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531 return OK;
3532}
3533
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003534#if defined(FEAT_AUTOCMD) || defined(PROTO)
3535/*
3536 * Init "aucmd_win". This can only be done after the first
3537 * window is fully initialized, thus it can't be in win_alloc_first().
3538 */
3539 void
3540win_alloc_aucmd_win()
3541{
3542 aucmd_win = win_alloc(NULL, TRUE);
3543 if (aucmd_win != NULL)
3544 {
3545 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003546 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003547 new_frame(aucmd_win);
3548 }
3549}
3550#endif
3551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003553 * Allocate the first window or the first window in a new tab page.
3554 * When "oldwin" is NULL create an empty buffer for it.
3555 * When "oldwin" is not NULL copy info from it to the new window (only with
3556 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003557 * Return FAIL when something goes wrong (out of memory).
3558 */
3559 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003560win_alloc_firstwin(oldwin)
3561 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003563 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003564 if (oldwin == NULL)
3565 {
3566 /* Very first window, need to create an empty buffer for it and
3567 * initialize from scratch. */
3568 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3569 if (curwin == NULL || curbuf == NULL)
3570 return FAIL;
3571 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003572#ifdef FEAT_SYN_HL
3573 curwin->w_s = &(curbuf->b_s);
3574#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003575 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003577 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003579 curwin_init(); /* init current window */
3580 }
3581#ifdef FEAT_WINDOWS
3582 else
3583 {
3584 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003585 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003586
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003587 /* We don't want cursor- and scroll-binding in the first window. */
3588 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003589 }
3590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003592 new_frame(curwin);
3593 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003594 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003595 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#ifdef FEAT_VERTSPLIT
3597 topframe->fr_width = Columns;
3598#endif
3599 topframe->fr_height = Rows - p_ch;
3600 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003601
3602 return OK;
3603}
3604
3605/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003606 * Create a frame for window "wp".
3607 */
3608 static void
3609new_frame(win_T *wp)
3610{
3611 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3612
3613 wp->w_frame = frp;
3614 if (frp != NULL)
3615 {
3616 frp->fr_layout = FR_LEAF;
3617 frp->fr_win = wp;
3618 }
3619}
3620
3621/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 * Initialize the window and frame size to the maximum.
3623 */
3624 void
3625win_init_size()
3626{
3627 firstwin->w_height = ROWS_AVAIL;
3628 topframe->fr_height = ROWS_AVAIL;
3629#ifdef FEAT_VERTSPLIT
3630 firstwin->w_width = Columns;
3631 topframe->fr_width = Columns;
3632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633}
3634
3635#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636
3637/*
3638 * Allocate a new tabpage_T and init the values.
3639 * Returns NULL when out of memory.
3640 */
3641 static tabpage_T *
3642alloc_tabpage()
3643{
3644 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003645# ifdef FEAT_GUI
3646 int i;
3647# endif
3648
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003649
3650 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003651 if (tp == NULL)
3652 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003653
Bram Moolenaar429fa852013-04-15 12:27:36 +02003654# ifdef FEAT_EVAL
3655 /* init t: variables */
3656 tp->tp_vars = dict_alloc();
3657 if (tp->tp_vars == NULL)
3658 {
3659 vim_free(tp);
3660 return NULL;
3661 }
3662 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3663# endif
3664
3665# ifdef FEAT_GUI
3666 for (i = 0; i < 3; i++)
3667 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003668# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003669# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003670 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003671# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003672 tp->tp_ch_used = p_ch;
3673
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003674 return tp;
3675}
3676
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003677 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003678free_tabpage(tp)
3679 tabpage_T *tp;
3680{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003681 int idx;
3682
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003683# ifdef FEAT_DIFF
3684 diff_clear(tp);
3685# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003686 for (idx = 0; idx < SNAP_COUNT; ++idx)
3687 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003688#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003689 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3690 hash_init(&tp->tp_vars->dv_hashtab);
3691 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003692#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003693
3694#ifdef FEAT_PYTHON
3695 python_tabpage_free(tp);
3696#endif
3697
3698#ifdef FEAT_PYTHON3
3699 python3_tabpage_free(tp);
3700#endif
3701
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003702 vim_free(tp);
3703}
3704
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003706 * Create a new Tab page with one window.
3707 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003708 * When "after" is 0 put it just after the current Tab page.
3709 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003710 * Return FAIL or OK.
3711 */
3712 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003713win_new_tabpage(after)
3714 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003715{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003716 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003717 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003718 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003720 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003721 if (newtp == NULL)
3722 return FAIL;
3723
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003725 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003726 {
3727 vim_free(newtp);
3728 return FAIL;
3729 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003730 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003731
3732 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003733 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003734 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003735 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003736 if (after == 1)
3737 {
3738 /* New tab page becomes the first one. */
3739 newtp->tp_next = first_tabpage;
3740 first_tabpage = newtp;
3741 }
3742 else
3743 {
3744 if (after > 0)
3745 {
3746 /* Put new tab page before tab page "after". */
3747 n = 2;
3748 for (tp = first_tabpage; tp->tp_next != NULL
3749 && n < after; tp = tp->tp_next)
3750 ++n;
3751 }
3752 newtp->tp_next = tp->tp_next;
3753 tp->tp_next = newtp;
3754 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003756 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003757 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003758
3759 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003760 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003761
3762#if defined(FEAT_GUI)
3763 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3764 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003765 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003766#endif
3767
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003769#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003770 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003771 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003772#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003773 return OK;
3774 }
3775
3776 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003777 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003778 return FAIL;
3779}
3780
3781/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003782 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3783 * like with ":split".
3784 * Returns OK if a new tab page was created, FAIL otherwise.
3785 */
3786 int
3787may_open_tabpage()
3788{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003789 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003790
Bram Moolenaard326ce82007-03-11 14:48:29 +00003791 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003792 {
3793 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003794 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003795 return win_new_tabpage(n);
3796 }
3797 return FAIL;
3798}
3799
3800/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003801 * Create up to "maxcount" tabpages with empty windows.
3802 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003803 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003804 int
3805make_tabpages(maxcount)
3806 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003808 int count = maxcount;
3809 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003811 /* Limit to 'tabpagemax' tabs. */
3812 if (count > p_tpm)
3813 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003814
3815#ifdef FEAT_AUTOCMD
3816 /*
3817 * Don't execute autocommands while creating the tab pages. Must do that
3818 * when putting the buffers in the windows.
3819 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003820 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003821#endif
3822
3823 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003824 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003825 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003826
3827#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003828 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003829#endif
3830
3831 /* return actual number of tab pages */
3832 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833}
3834
3835/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003836 * Return TRUE when "tpc" points to a valid tab page.
3837 */
3838 int
3839valid_tabpage(tpc)
3840 tabpage_T *tpc;
3841{
3842 tabpage_T *tp;
3843
3844 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3845 if (tp == tpc)
3846 return TRUE;
3847 return FALSE;
3848}
3849
3850/*
3851 * Find tab page "n" (first one is 1). Returns NULL when not found.
3852 */
3853 tabpage_T *
3854find_tabpage(n)
3855 int n;
3856{
3857 tabpage_T *tp;
3858 int i = 1;
3859
3860 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3861 ++i;
3862 return tp;
3863}
3864
3865/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003866 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003867 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003868 */
3869 int
3870tabpage_index(ftp)
3871 tabpage_T *ftp;
3872{
3873 int i = 1;
3874 tabpage_T *tp;
3875
3876 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3877 ++i;
3878 return i;
3879}
3880
3881/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003882 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003883 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003884 * FAIL.
3885 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003886 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003887 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003888leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003889 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003890 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003891 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003892{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003893 tabpage_T *tp = curtab;
3894
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003895 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003896#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003897 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003898 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003899 if (new_curbuf != curbuf)
3900 {
3901 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3902 if (curtab != tp)
3903 return FAIL;
3904 }
3905 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3906 if (curtab != tp)
3907 return FAIL;
3908 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003909 if (curtab != tp)
3910 return FAIL;
3911 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003912#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003913#if defined(FEAT_GUI)
3914 /* Remove the scrollbars. They may be added back later. */
3915 if (gui.in_use)
3916 gui_remove_scrollbars();
3917#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003918 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003919 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003920 tp->tp_firstwin = firstwin;
3921 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003922 tp->tp_old_Rows = Rows;
3923 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003924 firstwin = NULL;
3925 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003926 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003927}
3928
3929/*
3930 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003931 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003932 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3933 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003934 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003935 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003936enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003937 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003938 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003939 int trigger_enter_autocmds UNUSED;
3940 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003941{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003942 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003943 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003944
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003945 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003946 firstwin = tp->tp_firstwin;
3947 lastwin = tp->tp_lastwin;
3948 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003949
3950 /* We would like doing the TabEnter event first, but we don't have a
3951 * valid current window yet, which may break some commands.
3952 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003953 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3954 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003955 prevwin = next_prevwin;
3956
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003957 last_status(FALSE); /* status line may appear or disappear */
3958 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003959 must_redraw = CLEAR; /* need to redraw everything */
3960#ifdef FEAT_DIFF
3961 diff_need_scrollbind = TRUE;
3962#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003963
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003964 /* The tabpage line may have appeared or disappeared, may need to resize
3965 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003966 * frame sizes too. Use the stored value of p_ch, so that it can be
3967 * different for each tab page. */
3968 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003969 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3970#ifdef FEAT_GUI_TABLINE
3971 && !gui_use_tabline()
3972#endif
3973 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003974 shell_new_rows();
3975#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003976 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003977 shell_new_columns(); /* update window widths */
3978#endif
3979
3980#if defined(FEAT_GUI)
3981 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3982 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003983 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003984#endif
3985
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003986#ifdef FEAT_AUTOCMD
3987 /* Apply autocommands after updating the display, when 'rows' and
3988 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003989 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003990 {
3991 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3992 if (old_curbuf != curbuf)
3993 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3994 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003995#endif
3996
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003997 redraw_all_later(CLEAR);
3998}
3999
4000/*
4001 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004002 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004003 */
4004 void
4005goto_tabpage(n)
4006 int n;
4007{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004008 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004009 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004010 int i;
4011
Bram Moolenaard68071d2006-05-02 22:08:30 +00004012 if (text_locked())
4013 {
4014 /* Not allowed when editing the command line. */
4015#ifdef FEAT_CMDWIN
4016 if (cmdwin_type != 0)
4017 EMSG(_(e_cmdwin));
4018 else
4019#endif
4020 EMSG(_(e_secure));
4021 return;
4022 }
4023
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004024 /* If there is only one it can't work. */
4025 if (first_tabpage->tp_next == NULL)
4026 {
4027 if (n > 1)
4028 beep_flush();
4029 return;
4030 }
4031
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004032 if (n == 0)
4033 {
4034 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004036 tp = first_tabpage;
4037 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004038 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004039 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004040 else if (n < 0)
4041 {
4042 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4043 * this N times. */
4044 ttp = curtab;
4045 for (i = n; i < 0; ++i)
4046 {
4047 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4048 tp = tp->tp_next)
4049 ;
4050 ttp = tp;
4051 }
4052 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004053 else if (n == 9999)
4054 {
4055 /* Go to last tab page. */
4056 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4057 ;
4058 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004059 else
4060 {
4061 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004062 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004063 if (tp == NULL)
4064 {
4065 beep_flush();
4066 return;
4067 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004068 }
4069
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004070 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004071
4072#ifdef FEAT_GUI_TABLINE
4073 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004074 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004075#endif
4076}
4077
4078/*
4079 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004080 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4081 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004082 * Note: doesn't update the GUI tab.
4083 */
4084 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004085goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004086 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004087 int trigger_enter_autocmds;
4088 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004089{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004090 /* Don't repeat a message in another tab page. */
4091 set_keep_msg(NULL, 0);
4092
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004093 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4094 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004095 {
4096 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004097 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4098 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004099 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004100 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4101 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004102 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004106 * Enter window "wp" in tab page "tp".
4107 * Also updates the GUI tab.
4108 */
4109 void
4110goto_tabpage_win(tp, wp)
4111 tabpage_T *tp;
4112 win_T *wp;
4113{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004114 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004115 if (curtab == tp && win_valid(wp))
4116 {
4117 win_enter(wp, TRUE);
4118# ifdef FEAT_GUI_TABLINE
4119 if (gui_use_tabline())
4120 gui_mch_set_curtab(tabpage_index(curtab));
4121# endif
4122 }
4123}
4124
4125/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004126 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004127 */
4128 void
4129tabpage_move(nr)
4130 int nr;
4131{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004132 int n = 1;
4133 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004134
4135 if (first_tabpage->tp_next == NULL)
4136 return;
4137
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004138 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4139 ++n;
4140
4141 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4142 && tp->tp_next == curtab))
4143 return;
4144
4145 tp_dst = tp;
4146
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004147 /* Remove the current tab page from the list of tab pages. */
4148 if (curtab == first_tabpage)
4149 first_tabpage = curtab->tp_next;
4150 else
4151 {
4152 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4153 if (tp->tp_next == curtab)
4154 break;
4155 if (tp == NULL) /* "cannot happen" */
4156 return;
4157 tp->tp_next = curtab->tp_next;
4158 }
4159
4160 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004161 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004162 {
4163 curtab->tp_next = first_tabpage;
4164 first_tabpage = curtab;
4165 }
4166 else
4167 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004168 curtab->tp_next = tp_dst->tp_next;
4169 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004170 }
4171
4172 /* Need to redraw the tabline. Tab page contents doesn't change. */
4173 redraw_tabline = TRUE;
4174}
4175
4176
4177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 * Go to another window.
4179 * When jumping to another buffer, stop Visual mode. Do this before
4180 * changing windows so we can yank the selection into the '*' register.
4181 * When jumping to another window on the same buffer, adjust its cursor
4182 * position to keep the same Visual area.
4183 */
4184 void
4185win_goto(wp)
4186 win_T *wp;
4187{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004188#ifdef FEAT_CONCEAL
4189 win_T *owp = curwin;
4190#endif
4191
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004192 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004195 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return;
4197 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004198#ifdef FEAT_AUTOCMD
4199 if (curbuf_locked())
4200 return;
4201#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (wp->w_buffer != curbuf)
4204 reset_VIsual_and_resel();
4205 else if (VIsual_active)
4206 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208#ifdef FEAT_GUI
4209 need_mouse_correct = TRUE;
4210#endif
4211 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004212
4213#ifdef FEAT_CONCEAL
4214 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004215 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004216 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004217 if (curwin->w_p_cole > 0 && !msg_scrolled)
4218 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222#if defined(FEAT_PERL) || defined(PROTO)
4223/*
4224 * Find window number "winnr" (counting top to bottom).
4225 */
4226 win_T *
4227win_find_nr(winnr)
4228 int winnr;
4229{
4230 win_T *wp;
4231
4232# ifdef FEAT_WINDOWS
4233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4234 if (--winnr == 0)
4235 break;
4236 return wp;
4237# else
4238 return curwin;
4239# endif
4240}
4241#endif
4242
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004243#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4244 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004245/*
4246 * Find the tabpage for window "win".
4247 */
4248 tabpage_T *
4249win_find_tabpage(win)
4250 win_T *win;
4251{
4252 win_T *wp;
4253 tabpage_T *tp;
4254
4255 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004256 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4257 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004258 if (wp == win)
4259 return tp;
4260 return NULL;
4261}
4262#endif
4263
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#ifdef FEAT_VERTSPLIT
4265/*
4266 * Move to window above or below "count" times.
4267 */
4268 static void
4269win_goto_ver(up, count)
4270 int up; /* TRUE to go to win above */
4271 long count;
4272{
4273 frame_T *fr;
4274 frame_T *nfr;
4275 frame_T *foundfr;
4276
4277 foundfr = curwin->w_frame;
4278 while (count--)
4279 {
4280 /*
4281 * First go upwards in the tree of frames until we find a upwards or
4282 * downwards neighbor.
4283 */
4284 fr = foundfr;
4285 for (;;)
4286 {
4287 if (fr == topframe)
4288 goto end;
4289 if (up)
4290 nfr = fr->fr_prev;
4291 else
4292 nfr = fr->fr_next;
4293 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4294 break;
4295 fr = fr->fr_parent;
4296 }
4297
4298 /*
4299 * Now go downwards to find the bottom or top frame in it.
4300 */
4301 for (;;)
4302 {
4303 if (nfr->fr_layout == FR_LEAF)
4304 {
4305 foundfr = nfr;
4306 break;
4307 }
4308 fr = nfr->fr_child;
4309 if (nfr->fr_layout == FR_ROW)
4310 {
4311 /* Find the frame at the cursor row. */
4312 while (fr->fr_next != NULL
4313 && frame2win(fr)->w_wincol + fr->fr_width
4314 <= curwin->w_wincol + curwin->w_wcol)
4315 fr = fr->fr_next;
4316 }
4317 if (nfr->fr_layout == FR_COL && up)
4318 while (fr->fr_next != NULL)
4319 fr = fr->fr_next;
4320 nfr = fr;
4321 }
4322 }
4323end:
4324 if (foundfr != NULL)
4325 win_goto(foundfr->fr_win);
4326}
4327
4328/*
4329 * Move to left or right window.
4330 */
4331 static void
4332win_goto_hor(left, count)
4333 int left; /* TRUE to go to left win */
4334 long count;
4335{
4336 frame_T *fr;
4337 frame_T *nfr;
4338 frame_T *foundfr;
4339
4340 foundfr = curwin->w_frame;
4341 while (count--)
4342 {
4343 /*
4344 * First go upwards in the tree of frames until we find a left or
4345 * right neighbor.
4346 */
4347 fr = foundfr;
4348 for (;;)
4349 {
4350 if (fr == topframe)
4351 goto end;
4352 if (left)
4353 nfr = fr->fr_prev;
4354 else
4355 nfr = fr->fr_next;
4356 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4357 break;
4358 fr = fr->fr_parent;
4359 }
4360
4361 /*
4362 * Now go downwards to find the leftmost or rightmost frame in it.
4363 */
4364 for (;;)
4365 {
4366 if (nfr->fr_layout == FR_LEAF)
4367 {
4368 foundfr = nfr;
4369 break;
4370 }
4371 fr = nfr->fr_child;
4372 if (nfr->fr_layout == FR_COL)
4373 {
4374 /* Find the frame at the cursor row. */
4375 while (fr->fr_next != NULL
4376 && frame2win(fr)->w_winrow + fr->fr_height
4377 <= curwin->w_winrow + curwin->w_wrow)
4378 fr = fr->fr_next;
4379 }
4380 if (nfr->fr_layout == FR_ROW && left)
4381 while (fr->fr_next != NULL)
4382 fr = fr->fr_next;
4383 nfr = fr;
4384 }
4385 }
4386end:
4387 if (foundfr != NULL)
4388 win_goto(foundfr->fr_win);
4389}
4390#endif
4391
4392/*
4393 * Make window "wp" the current window.
4394 */
4395 void
4396win_enter(wp, undo_sync)
4397 win_T *wp;
4398 int undo_sync;
4399{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004400 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401}
4402
4403/*
4404 * Make window wp the current window.
4405 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4406 * been closed and isn't valid.
4407 */
4408 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004409win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 win_T *wp;
4411 int undo_sync;
4412 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004413 int trigger_enter_autocmds UNUSED;
4414 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416#ifdef FEAT_AUTOCMD
4417 int other_buffer = FALSE;
4418#endif
4419
4420 if (wp == curwin && !curwin_invalid) /* nothing to do */
4421 return;
4422
4423#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004424 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 /*
4427 * Be careful: If autocommands delete the window, return now.
4428 */
4429 if (wp->w_buffer != curbuf)
4430 {
4431 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4432 other_buffer = TRUE;
4433 if (!win_valid(wp))
4434 return;
4435 }
4436 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4437 if (!win_valid(wp))
4438 return;
4439# ifdef FEAT_EVAL
4440 /* autocmds may abort script processing */
4441 if (aborting())
4442 return;
4443# endif
4444 }
4445#endif
4446
4447 /* sync undo before leaving the current buffer */
4448 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004449 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004450
4451 /* Might need to scroll the old window before switching, e.g., when the
4452 * cursor was moved. */
4453 update_topline();
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /* may have to copy the buffer options when 'cpo' contains 'S' */
4456 if (wp->w_buffer != curbuf)
4457 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4458 if (!curwin_invalid)
4459 {
4460 prevwin = curwin; /* remember for CTRL-W p */
4461 curwin->w_redr_status = TRUE;
4462 }
4463 curwin = wp;
4464 curbuf = wp->w_buffer;
4465 check_cursor();
4466#ifdef FEAT_VIRTUALEDIT
4467 if (!virtual_active())
4468 curwin->w_cursor.coladd = 0;
4469#endif
4470 changed_line_abv_curs(); /* assume cursor position needs updating */
4471
4472 if (curwin->w_localdir != NULL)
4473 {
4474 /* Window has a local directory: Save current directory as global
4475 * directory (unless that was done already) and change to the local
4476 * directory. */
4477 if (globaldir == NULL)
4478 {
4479 char_u cwd[MAXPATHL];
4480
4481 if (mch_dirname(cwd, MAXPATHL) == OK)
4482 globaldir = vim_strsave(cwd);
4483 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004484 if (mch_chdir((char *)curwin->w_localdir) == 0)
4485 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else if (globaldir != NULL)
4488 {
4489 /* Window doesn't have a local directory and we are not in the global
4490 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004491 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 vim_free(globaldir);
4493 globaldir = NULL;
4494 shorten_fnames(TRUE);
4495 }
4496
4497#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004498 if (trigger_enter_autocmds)
4499 {
4500 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4501 if (other_buffer)
4502 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#endif
4505
4506#ifdef FEAT_TITLE
4507 maketitle();
4508#endif
4509 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004510 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 if (restart_edit)
4512 redraw_later(VALID); /* causes status line redraw */
4513
4514 /* set window height to desired minimal value */
4515 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4516 win_setheight((int)p_wh);
4517 else if (curwin->w_height == 0)
4518 win_setheight(1);
4519
4520#ifdef FEAT_VERTSPLIT
4521 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004522 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 win_setwidth((int)p_wiw);
4524#endif
4525
4526#ifdef FEAT_MOUSE
4527 setmouse(); /* in case jumped to/from help buffer */
4528#endif
4529
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004530 /* Change directories when the 'acd' option is set. */
4531 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532}
4533
4534#endif /* FEAT_WINDOWS */
4535
4536#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4537/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004538 * Jump to the first open window that contains buffer "buf", if one exists.
4539 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 */
4541 win_T *
4542buf_jump_open_win(buf)
4543 buf_T *buf;
4544{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004545 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004547 if (curwin->w_buffer == buf)
4548 wp = curwin;
4549# ifdef FEAT_WINDOWS
4550 else
4551 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4552 if (wp->w_buffer == buf)
4553 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (wp != NULL)
4555 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004557 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004559
4560/*
4561 * Jump to the first open window in any tab page that contains buffer "buf",
4562 * if one exists.
4563 * Returns a pointer to the window found, otherwise NULL.
4564 */
4565 win_T *
4566buf_jump_open_tab(buf)
4567 buf_T *buf;
4568{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004569 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004570# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004571 tabpage_T *tp;
4572
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004573 if (wp != NULL)
4574 return wp;
4575
4576 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4577 if (tp != curtab)
4578 {
4579 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4580 if (wp->w_buffer == buf)
4581 break;
4582 if (wp != NULL)
4583 {
4584 goto_tabpage_win(tp, wp);
4585 if (curwin != wp)
4586 wp = NULL; /* something went wrong */
4587 break;
4588 }
4589 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004590# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004591 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004592}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#endif
4594
4595/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004596 * Allocate a window structure and link it in the window list when "hidden" is
4597 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004600win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004601 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004602 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004604 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /*
4607 * allocate window structure and linesizes arrays
4608 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004609 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004610 if (new_wp == NULL)
4611 return NULL;
4612
4613 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004615 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004616 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618
Bram Moolenaar429fa852013-04-15 12:27:36 +02004619#ifdef FEAT_EVAL
4620 /* init w: variables */
4621 new_wp->w_vars = dict_alloc();
4622 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004624 win_free_lsize(new_wp);
4625 vim_free(new_wp);
4626 return NULL;
4627 }
4628 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004629#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004630
4631#ifdef FEAT_AUTOCMD
4632 /* Don't execute autocommands while the window is not properly
4633 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4634 * event. */
4635 block_autocmds();
4636#endif
4637 /*
4638 * link the window in the window list
4639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004641 if (!hidden)
4642 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
4644#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004645 new_wp->w_wincol = 0;
4646 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#endif
4648
Bram Moolenaar429fa852013-04-15 12:27:36 +02004649 /* position the display and the cursor at the top of the file. */
4650 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004652 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004654 new_wp->w_botline = 2;
4655 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004657 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659
Bram Moolenaar429fa852013-04-15 12:27:36 +02004660 /* We won't calculate w_fraction until resizing the window */
4661 new_wp->w_fraction = 0;
4662 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004665 if (gui.in_use)
4666 {
4667 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4668 SBAR_LEFT, new_wp);
4669 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4670 SBAR_RIGHT, new_wp);
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
4673#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004674 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004676#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004677 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004678#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004679#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004680 new_wp->w_match_head = NULL;
4681 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004682#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004683 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
4685
4686#if defined(FEAT_WINDOWS) || defined(PROTO)
4687
4688/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004689 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 */
4691 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004692win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004694 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004697 buf_T *buf;
4698 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004700#ifdef FEAT_FOLDING
4701 clearFolding(wp);
4702#endif
4703
4704 /* reduce the reference count to the argument list. */
4705 alist_unlink(wp->w_alist);
4706
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004707#ifdef FEAT_AUTOCMD
4708 /* Don't execute autocommands while the window is halfway being deleted.
4709 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004710 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004711#endif
4712
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004713#ifdef FEAT_LUA
4714 lua_window_free(wp);
4715#endif
4716
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004717#ifdef FEAT_MZSCHEME
4718 mzscheme_window_free(wp);
4719#endif
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#ifdef FEAT_PERL
4722 perl_win_free(wp);
4723#endif
4724
4725#ifdef FEAT_PYTHON
4726 python_window_free(wp);
4727#endif
4728
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004729#ifdef FEAT_PYTHON3
4730 python3_window_free(wp);
4731#endif
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#ifdef FEAT_TCL
4734 tcl_window_free(wp);
4735#endif
4736
4737#ifdef FEAT_RUBY
4738 ruby_window_free(wp);
4739#endif
4740
4741 clear_winopt(&wp->w_onebuf_opt);
4742 clear_winopt(&wp->w_allbuf_opt);
4743
4744#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004745 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4746 hash_init(&wp->w_vars->dv_hashtab);
4747 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748#endif
4749
4750 if (prevwin == wp)
4751 prevwin = NULL;
4752 win_free_lsize(wp);
4753
4754 for (i = 0; i < wp->w_tagstacklen; ++i)
4755 vim_free(wp->w_tagstack[i].tagname);
4756
4757 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004758
Bram Moolenaarff18df02013-07-24 17:51:57 +02004759 /* Remove the window from the b_wininfo lists, it may happen that the
4760 * freed memory is re-used for another window. */
4761 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4762 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4763 if (wip->wi_win == wp)
4764 wip->wi_win = NULL;
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004767 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770#ifdef FEAT_JUMPLIST
4771 free_jumplist(wp);
4772#endif
4773
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004774#ifdef FEAT_QUICKFIX
4775 qf_free_all(wp);
4776#endif
4777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778#ifdef FEAT_GUI
4779 if (gui.in_use)
4780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4782 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4783 }
4784#endif /* FEAT_GUI */
4785
Bram Moolenaar860cae12010-06-05 23:22:07 +02004786#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004787 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004788#endif
4789
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004790#ifdef FEAT_AUTOCMD
4791 if (wp != aucmd_win)
4792#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004793 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004794#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004795 if (autocmd_busy)
4796 {
4797 wp->w_next = au_pending_free_win;
4798 au_pending_free_win = wp;
4799 }
4800 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004801#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004802 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004803
4804#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004805 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809/*
4810 * Append window "wp" in the window list after window "after".
4811 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004812 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813win_append(after, wp)
4814 win_T *after, *wp;
4815{
4816 win_T *before;
4817
4818 if (after == NULL) /* after NULL is in front of the first */
4819 before = firstwin;
4820 else
4821 before = after->w_next;
4822
4823 wp->w_next = before;
4824 wp->w_prev = after;
4825 if (after == NULL)
4826 firstwin = wp;
4827 else
4828 after->w_next = wp;
4829 if (before == NULL)
4830 lastwin = wp;
4831 else
4832 before->w_prev = wp;
4833}
4834
4835/*
4836 * Remove a window from the window list.
4837 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004838 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004839win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004841 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 if (wp->w_prev != NULL)
4844 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004845 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004847 else
4848 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (wp->w_next != NULL)
4850 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004851 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004853 else
4854 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
4857/*
4858 * Append frame "frp" in a frame list after frame "after".
4859 */
4860 static void
4861frame_append(after, frp)
4862 frame_T *after, *frp;
4863{
4864 frp->fr_next = after->fr_next;
4865 after->fr_next = frp;
4866 if (frp->fr_next != NULL)
4867 frp->fr_next->fr_prev = frp;
4868 frp->fr_prev = after;
4869}
4870
4871/*
4872 * Insert frame "frp" in a frame list before frame "before".
4873 */
4874 static void
4875frame_insert(before, frp)
4876 frame_T *before, *frp;
4877{
4878 frp->fr_next = before;
4879 frp->fr_prev = before->fr_prev;
4880 before->fr_prev = frp;
4881 if (frp->fr_prev != NULL)
4882 frp->fr_prev->fr_next = frp;
4883 else
4884 frp->fr_parent->fr_child = frp;
4885}
4886
4887/*
4888 * Remove a frame from a frame list.
4889 */
4890 static void
4891frame_remove(frp)
4892 frame_T *frp;
4893{
4894 if (frp->fr_prev != NULL)
4895 frp->fr_prev->fr_next = frp->fr_next;
4896 else
4897 frp->fr_parent->fr_child = frp->fr_next;
4898 if (frp->fr_next != NULL)
4899 frp->fr_next->fr_prev = frp->fr_prev;
4900}
4901
4902#endif /* FEAT_WINDOWS */
4903
4904/*
4905 * Allocate w_lines[] for window "wp".
4906 * Return FAIL for failure, OK for success.
4907 */
4908 int
4909win_alloc_lines(wp)
4910 win_T *wp;
4911{
4912 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004913 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (wp->w_lines == NULL)
4915 return FAIL;
4916 return OK;
4917}
4918
4919/*
4920 * free lsize arrays for a window
4921 */
4922 void
4923win_free_lsize(wp)
4924 win_T *wp;
4925{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004926 /* TODO: why would wp be NULL here? */
4927 if (wp != NULL)
4928 {
4929 vim_free(wp->w_lines);
4930 wp->w_lines = NULL;
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932}
4933
4934/*
4935 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004936 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 */
4938 void
4939shell_new_rows()
4940{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004941 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943 if (firstwin == NULL) /* not initialized yet */
4944 return;
4945#ifdef FEAT_WINDOWS
4946 if (h < frame_minheight(topframe, NULL))
4947 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004948
4949 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 * that doesn't result in the right height, forget about that option. */
4951 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004952 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 frame_new_height(topframe, h, FALSE, FALSE);
4954
4955 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4956#else
4957 if (h < 1)
4958 h = 1;
4959 win_new_height(firstwin, h);
4960#endif
4961 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004962#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004963 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004964#endif
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#if 0
4967 /* Disabled: don't want making the screen smaller make a window larger. */
4968 if (p_ea)
4969 win_equal(curwin, FALSE, 'v');
4970#endif
4971}
4972
4973#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4974/*
4975 * Called from win_new_shellsize() after Columns changed.
4976 */
4977 void
4978shell_new_columns()
4979{
4980 if (firstwin == NULL) /* not initialized yet */
4981 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004982
4983 /* First try setting the widths of windows with 'winfixwidth'. If that
4984 * doesn't result in the right width, forget about that option. */
4985 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004986 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004987 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4990#if 0
4991 /* Disabled: don't want making the screen smaller make a window larger. */
4992 if (p_ea)
4993 win_equal(curwin, FALSE, 'h');
4994#endif
4995}
4996#endif
4997
4998#if defined(FEAT_CMDWIN) || defined(PROTO)
4999/*
5000 * Save the size of all windows in "gap".
5001 */
5002 void
5003win_size_save(gap)
5004 garray_T *gap;
5005
5006{
5007 win_T *wp;
5008
5009 ga_init2(gap, (int)sizeof(int), 1);
5010 if (ga_grow(gap, win_count() * 2) == OK)
5011 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5012 {
5013 ((int *)gap->ga_data)[gap->ga_len++] =
5014 wp->w_width + wp->w_vsep_width;
5015 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5016 }
5017}
5018
5019/*
5020 * Restore window sizes, but only if the number of windows is still the same.
5021 * Does not free the growarray.
5022 */
5023 void
5024win_size_restore(gap)
5025 garray_T *gap;
5026{
5027 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005028 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 if (win_count() * 2 == gap->ga_len)
5031 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005032 /* The order matters, because frames contain other frames, but it's
5033 * difficult to get right. The easy way out is to do it twice. */
5034 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005036 i = 0;
5037 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5038 {
5039 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5040 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 /* recompute the window positions */
5044 (void)win_comp_pos();
5045 }
5046}
5047#endif /* FEAT_CMDWIN */
5048
5049#if defined(FEAT_WINDOWS) || defined(PROTO)
5050/*
5051 * Update the position for all windows, using the width and height of the
5052 * frames.
5053 * Returns the row just after the last window.
5054 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005055 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056win_comp_pos()
5057{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005058 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 int col = 0;
5060
5061 frame_comp_pos(topframe, &row, &col);
5062 return row;
5063}
5064
5065/*
5066 * Update the position of the windows in frame "topfrp", using the width and
5067 * height of the frames.
5068 * "*row" and "*col" are the top-left position of the frame. They are updated
5069 * to the bottom-right position plus one.
5070 */
5071 static void
5072frame_comp_pos(topfrp, row, col)
5073 frame_T *topfrp;
5074 int *row;
5075 int *col;
5076{
5077 win_T *wp;
5078 frame_T *frp;
5079#ifdef FEAT_VERTSPLIT
5080 int startcol;
5081 int startrow;
5082#endif
5083
5084 wp = topfrp->fr_win;
5085 if (wp != NULL)
5086 {
5087 if (wp->w_winrow != *row
5088#ifdef FEAT_VERTSPLIT
5089 || wp->w_wincol != *col
5090#endif
5091 )
5092 {
5093 /* position changed, redraw */
5094 wp->w_winrow = *row;
5095#ifdef FEAT_VERTSPLIT
5096 wp->w_wincol = *col;
5097#endif
5098 redraw_win_later(wp, NOT_VALID);
5099 wp->w_redr_status = TRUE;
5100 }
5101 *row += wp->w_height + wp->w_status_height;
5102#ifdef FEAT_VERTSPLIT
5103 *col += wp->w_width + wp->w_vsep_width;
5104#endif
5105 }
5106 else
5107 {
5108#ifdef FEAT_VERTSPLIT
5109 startrow = *row;
5110 startcol = *col;
5111#endif
5112 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5113 {
5114#ifdef FEAT_VERTSPLIT
5115 if (topfrp->fr_layout == FR_ROW)
5116 *row = startrow; /* all frames are at the same row */
5117 else
5118 *col = startcol; /* all frames are at the same col */
5119#endif
5120 frame_comp_pos(frp, row, col);
5121 }
5122 }
5123}
5124
5125#endif /* FEAT_WINDOWS */
5126
5127/*
5128 * Set current window height and take care of repositioning other windows to
5129 * fit around it.
5130 */
5131 void
5132win_setheight(height)
5133 int height;
5134{
5135 win_setheight_win(height, curwin);
5136}
5137
5138/*
5139 * Set the window height of window "win" and take care of repositioning other
5140 * windows to fit around it.
5141 */
5142 void
5143win_setheight_win(height, win)
5144 int height;
5145 win_T *win;
5146{
5147 int row;
5148
5149 if (win == curwin)
5150 {
5151 /* Always keep current window at least one line high, even when
5152 * 'winminheight' is zero. */
5153#ifdef FEAT_WINDOWS
5154 if (height < p_wmh)
5155 height = p_wmh;
5156#endif
5157 if (height == 0)
5158 height = 1;
5159 }
5160
5161#ifdef FEAT_WINDOWS
5162 frame_setheight(win->w_frame, height + win->w_status_height);
5163
5164 /* recompute the window positions */
5165 row = win_comp_pos();
5166#else
5167 if (height > topframe->fr_height)
5168 height = topframe->fr_height;
5169 win->w_height = height;
5170 row = height;
5171#endif
5172
5173 /*
5174 * If there is extra space created between the last window and the command
5175 * line, clear it.
5176 */
5177 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5178 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5179 cmdline_row = row;
5180 msg_row = row;
5181 msg_col = 0;
5182
5183 redraw_all_later(NOT_VALID);
5184}
5185
5186#if defined(FEAT_WINDOWS) || defined(PROTO)
5187
5188/*
5189 * Set the height of a frame to "height" and take care that all frames and
5190 * windows inside it are resized. Also resize frames on the left and right if
5191 * the are in the same FR_ROW frame.
5192 *
5193 * Strategy:
5194 * If the frame is part of a FR_COL frame, try fitting the frame in that
5195 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5196 * go to containing frames to resize them and make room.
5197 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5198 * Check for the minimal height of the FR_ROW frame.
5199 * At the top level we can also use change the command line height.
5200 */
5201 static void
5202frame_setheight(curfrp, height)
5203 frame_T *curfrp;
5204 int height;
5205{
5206 int room; /* total number of lines available */
5207 int take; /* number of lines taken from other windows */
5208 int room_cmdline; /* lines available from cmdline */
5209 int run;
5210 frame_T *frp;
5211 int h;
5212 int room_reserved;
5213
5214 /* If the height already is the desired value, nothing to do. */
5215 if (curfrp->fr_height == height)
5216 return;
5217
5218 if (curfrp->fr_parent == NULL)
5219 {
5220 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005221 if (height > ROWS_AVAIL)
5222 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (height > 0)
5224 frame_new_height(curfrp, height, FALSE, FALSE);
5225 }
5226 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5227 {
5228 /* Row of frames: Also need to resize frames left and right of this
5229 * one. First check for the minimal height of these. */
5230 h = frame_minheight(curfrp->fr_parent, NULL);
5231 if (height < h)
5232 height = h;
5233 frame_setheight(curfrp->fr_parent, height);
5234 }
5235 else
5236 {
5237 /*
5238 * Column of frames: try to change only frames in this column.
5239 */
5240#ifdef FEAT_VERTSPLIT
5241 /*
5242 * Do this twice:
5243 * 1: compute room available, if it's not enough try resizing the
5244 * containing frame.
5245 * 2: compute the room available and adjust the height to it.
5246 * Try not to reduce the height of a window with 'winfixheight' set.
5247 */
5248 for (run = 1; run <= 2; ++run)
5249#else
5250 for (;;)
5251#endif
5252 {
5253 room = 0;
5254 room_reserved = 0;
5255 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5256 frp = frp->fr_next)
5257 {
5258 if (frp != curfrp
5259 && frp->fr_win != NULL
5260 && frp->fr_win->w_p_wfh)
5261 room_reserved += frp->fr_height;
5262 room += frp->fr_height;
5263 if (frp != curfrp)
5264 room -= frame_minheight(frp, NULL);
5265 }
5266#ifdef FEAT_VERTSPLIT
5267 if (curfrp->fr_width != Columns)
5268 room_cmdline = 0;
5269 else
5270#endif
5271 {
5272 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5273 + lastwin->w_height + lastwin->w_status_height);
5274 if (room_cmdline < 0)
5275 room_cmdline = 0;
5276 }
5277
5278 if (height <= room + room_cmdline)
5279 break;
5280#ifdef FEAT_VERTSPLIT
5281 if (run == 2 || curfrp->fr_width == Columns)
5282#endif
5283 {
5284 if (height > room + room_cmdline)
5285 height = room + room_cmdline;
5286 break;
5287 }
5288#ifdef FEAT_VERTSPLIT
5289 frame_setheight(curfrp->fr_parent, height
5290 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5291#endif
5292 /*NOTREACHED*/
5293 }
5294
5295 /*
5296 * Compute the number of lines we will take from others frames (can be
5297 * negative!).
5298 */
5299 take = height - curfrp->fr_height;
5300
5301 /* If there is not enough room, also reduce the height of a window
5302 * with 'winfixheight' set. */
5303 if (height > room + room_cmdline - room_reserved)
5304 room_reserved = room + room_cmdline - height;
5305 /* If there is only a 'winfixheight' window and making the
5306 * window smaller, need to make the other window taller. */
5307 if (take < 0 && room - curfrp->fr_height < room_reserved)
5308 room_reserved = 0;
5309
5310 if (take > 0 && room_cmdline > 0)
5311 {
5312 /* use lines from cmdline first */
5313 if (take < room_cmdline)
5314 room_cmdline = take;
5315 take -= room_cmdline;
5316 topframe->fr_height += room_cmdline;
5317 }
5318
5319 /*
5320 * set the current frame to the new height
5321 */
5322 frame_new_height(curfrp, height, FALSE, FALSE);
5323
5324 /*
5325 * First take lines from the frames after the current frame. If
5326 * that is not enough, takes lines from frames above the current
5327 * frame.
5328 */
5329 for (run = 0; run < 2; ++run)
5330 {
5331 if (run == 0)
5332 frp = curfrp->fr_next; /* 1st run: start with next window */
5333 else
5334 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5335 while (frp != NULL && take != 0)
5336 {
5337 h = frame_minheight(frp, NULL);
5338 if (room_reserved > 0
5339 && frp->fr_win != NULL
5340 && frp->fr_win->w_p_wfh)
5341 {
5342 if (room_reserved >= frp->fr_height)
5343 room_reserved -= frp->fr_height;
5344 else
5345 {
5346 if (frp->fr_height - room_reserved > take)
5347 room_reserved = frp->fr_height - take;
5348 take -= frp->fr_height - room_reserved;
5349 frame_new_height(frp, room_reserved, FALSE, FALSE);
5350 room_reserved = 0;
5351 }
5352 }
5353 else
5354 {
5355 if (frp->fr_height - take < h)
5356 {
5357 take -= frp->fr_height - h;
5358 frame_new_height(frp, h, FALSE, FALSE);
5359 }
5360 else
5361 {
5362 frame_new_height(frp, frp->fr_height - take,
5363 FALSE, FALSE);
5364 take = 0;
5365 }
5366 }
5367 if (run == 0)
5368 frp = frp->fr_next;
5369 else
5370 frp = frp->fr_prev;
5371 }
5372 }
5373 }
5374}
5375
5376#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5377/*
5378 * Set current window width and take care of repositioning other windows to
5379 * fit around it.
5380 */
5381 void
5382win_setwidth(width)
5383 int width;
5384{
5385 win_setwidth_win(width, curwin);
5386}
5387
5388 void
5389win_setwidth_win(width, wp)
5390 int width;
5391 win_T *wp;
5392{
5393 /* Always keep current window at least one column wide, even when
5394 * 'winminwidth' is zero. */
5395 if (wp == curwin)
5396 {
5397 if (width < p_wmw)
5398 width = p_wmw;
5399 if (width == 0)
5400 width = 1;
5401 }
5402
5403 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5404
5405 /* recompute the window positions */
5406 (void)win_comp_pos();
5407
5408 redraw_all_later(NOT_VALID);
5409}
5410
5411/*
5412 * Set the width of a frame to "width" and take care that all frames and
5413 * windows inside it are resized. Also resize frames above and below if the
5414 * are in the same FR_ROW frame.
5415 *
5416 * Strategy is similar to frame_setheight().
5417 */
5418 static void
5419frame_setwidth(curfrp, width)
5420 frame_T *curfrp;
5421 int width;
5422{
5423 int room; /* total number of lines available */
5424 int take; /* number of lines taken from other windows */
5425 int run;
5426 frame_T *frp;
5427 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005428 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
5430 /* If the width already is the desired value, nothing to do. */
5431 if (curfrp->fr_width == width)
5432 return;
5433
5434 if (curfrp->fr_parent == NULL)
5435 /* topframe: can't change width */
5436 return;
5437
5438 if (curfrp->fr_parent->fr_layout == FR_COL)
5439 {
5440 /* Column of frames: Also need to resize frames above and below of
5441 * this one. First check for the minimal width of these. */
5442 w = frame_minwidth(curfrp->fr_parent, NULL);
5443 if (width < w)
5444 width = w;
5445 frame_setwidth(curfrp->fr_parent, width);
5446 }
5447 else
5448 {
5449 /*
5450 * Row of frames: try to change only frames in this row.
5451 *
5452 * Do this twice:
5453 * 1: compute room available, if it's not enough try resizing the
5454 * containing frame.
5455 * 2: compute the room available and adjust the width to it.
5456 */
5457 for (run = 1; run <= 2; ++run)
5458 {
5459 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005460 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5462 frp = frp->fr_next)
5463 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005464 if (frp != curfrp
5465 && frp->fr_win != NULL
5466 && frp->fr_win->w_p_wfw)
5467 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 room += frp->fr_width;
5469 if (frp != curfrp)
5470 room -= frame_minwidth(frp, NULL);
5471 }
5472
5473 if (width <= room)
5474 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005475 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 if (width > room)
5478 width = room;
5479 break;
5480 }
5481 frame_setwidth(curfrp->fr_parent, width
5482 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5483 }
5484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 /*
5486 * Compute the number of lines we will take from others frames (can be
5487 * negative!).
5488 */
5489 take = width - curfrp->fr_width;
5490
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005491 /* If there is not enough room, also reduce the width of a window
5492 * with 'winfixwidth' set. */
5493 if (width > room - room_reserved)
5494 room_reserved = room - width;
5495 /* If there is only a 'winfixwidth' window and making the
5496 * window smaller, need to make the other window narrower. */
5497 if (take < 0 && room - curfrp->fr_width < room_reserved)
5498 room_reserved = 0;
5499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 /*
5501 * set the current frame to the new width
5502 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005503 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 /*
5506 * First take lines from the frames right of the current frame. If
5507 * that is not enough, takes lines from frames left of the current
5508 * frame.
5509 */
5510 for (run = 0; run < 2; ++run)
5511 {
5512 if (run == 0)
5513 frp = curfrp->fr_next; /* 1st run: start with next window */
5514 else
5515 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5516 while (frp != NULL && take != 0)
5517 {
5518 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005519 if (room_reserved > 0
5520 && frp->fr_win != NULL
5521 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005523 if (room_reserved >= frp->fr_width)
5524 room_reserved -= frp->fr_width;
5525 else
5526 {
5527 if (frp->fr_width - room_reserved > take)
5528 room_reserved = frp->fr_width - take;
5529 take -= frp->fr_width - room_reserved;
5530 frame_new_width(frp, room_reserved, FALSE, FALSE);
5531 room_reserved = 0;
5532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534 else
5535 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005536 if (frp->fr_width - take < w)
5537 {
5538 take -= frp->fr_width - w;
5539 frame_new_width(frp, w, FALSE, FALSE);
5540 }
5541 else
5542 {
5543 frame_new_width(frp, frp->fr_width - take,
5544 FALSE, FALSE);
5545 take = 0;
5546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 }
5548 if (run == 0)
5549 frp = frp->fr_next;
5550 else
5551 frp = frp->fr_prev;
5552 }
5553 }
5554 }
5555}
5556#endif /* FEAT_VERTSPLIT */
5557
5558/*
5559 * Check 'winminheight' for a valid value.
5560 */
5561 void
5562win_setminheight()
5563{
5564 int room;
5565 int first = TRUE;
5566 win_T *wp;
5567
5568 /* loop until there is a 'winminheight' that is possible */
5569 while (p_wmh > 0)
5570 {
5571 /* TODO: handle vertical splits */
5572 room = -p_wh;
5573 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5574 room += wp->w_height - p_wmh;
5575 if (room >= 0)
5576 break;
5577 --p_wmh;
5578 if (first)
5579 {
5580 EMSG(_(e_noroom));
5581 first = FALSE;
5582 }
5583 }
5584}
5585
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005586#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588/*
5589 * Status line of dragwin is dragged "offset" lines down (negative is up).
5590 */
5591 void
5592win_drag_status_line(dragwin, offset)
5593 win_T *dragwin;
5594 int offset;
5595{
5596 frame_T *curfr;
5597 frame_T *fr;
5598 int room;
5599 int row;
5600 int up; /* if TRUE, drag status line up, otherwise down */
5601 int n;
5602
5603 fr = dragwin->w_frame;
5604 curfr = fr;
5605 if (fr != topframe) /* more than one window */
5606 {
5607 fr = fr->fr_parent;
5608 /* When the parent frame is not a column of frames, its parent should
5609 * be. */
5610 if (fr->fr_layout != FR_COL)
5611 {
5612 curfr = fr;
5613 if (fr != topframe) /* only a row of windows, may drag statusline */
5614 fr = fr->fr_parent;
5615 }
5616 }
5617
5618 /* If this is the last frame in a column, may want to resize the parent
5619 * frame instead (go two up to skip a row of frames). */
5620 while (curfr != topframe && curfr->fr_next == NULL)
5621 {
5622 if (fr != topframe)
5623 fr = fr->fr_parent;
5624 curfr = fr;
5625 if (fr != topframe)
5626 fr = fr->fr_parent;
5627 }
5628
5629 if (offset < 0) /* drag up */
5630 {
5631 up = TRUE;
5632 offset = -offset;
5633 /* sum up the room of the current frame and above it */
5634 if (fr == curfr)
5635 {
5636 /* only one window */
5637 room = fr->fr_height - frame_minheight(fr, NULL);
5638 }
5639 else
5640 {
5641 room = 0;
5642 for (fr = fr->fr_child; ; fr = fr->fr_next)
5643 {
5644 room += fr->fr_height - frame_minheight(fr, NULL);
5645 if (fr == curfr)
5646 break;
5647 }
5648 }
5649 fr = curfr->fr_next; /* put fr at frame that grows */
5650 }
5651 else /* drag down */
5652 {
5653 up = FALSE;
5654 /*
5655 * Only dragging the last status line can reduce p_ch.
5656 */
5657 room = Rows - cmdline_row;
5658 if (curfr->fr_next == NULL)
5659 room -= 1;
5660 else
5661 room -= p_ch;
5662 if (room < 0)
5663 room = 0;
5664 /* sum up the room of frames below of the current one */
5665 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5666 room += fr->fr_height - frame_minheight(fr, NULL);
5667 fr = curfr; /* put fr at window that grows */
5668 }
5669
5670 if (room < offset) /* Not enough room */
5671 offset = room; /* Move as far as we can */
5672 if (offset <= 0)
5673 return;
5674
5675 /*
5676 * Grow frame fr by "offset" lines.
5677 * Doesn't happen when dragging the last status line up.
5678 */
5679 if (fr != NULL)
5680 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5681
5682 if (up)
5683 fr = curfr; /* current frame gets smaller */
5684 else
5685 fr = curfr->fr_next; /* next frame gets smaller */
5686
5687 /*
5688 * Now make the other frames smaller.
5689 */
5690 while (fr != NULL && offset > 0)
5691 {
5692 n = frame_minheight(fr, NULL);
5693 if (fr->fr_height - offset <= n)
5694 {
5695 offset -= fr->fr_height - n;
5696 frame_new_height(fr, n, !up, FALSE);
5697 }
5698 else
5699 {
5700 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5701 break;
5702 }
5703 if (up)
5704 fr = fr->fr_prev;
5705 else
5706 fr = fr->fr_next;
5707 }
5708 row = win_comp_pos();
5709 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5710 cmdline_row = row;
5711 p_ch = Rows - cmdline_row;
5712 if (p_ch < 1)
5713 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005714 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005715 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 showmode();
5717}
5718
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005719# if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720/*
5721 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5722 */
5723 void
5724win_drag_vsep_line(dragwin, offset)
5725 win_T *dragwin;
5726 int offset;
5727{
5728 frame_T *curfr;
5729 frame_T *fr;
5730 int room;
5731 int left; /* if TRUE, drag separator line left, otherwise right */
5732 int n;
5733
5734 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005735 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 return;
5737 curfr = fr;
5738 fr = fr->fr_parent;
5739 /* When the parent frame is not a row of frames, its parent should be. */
5740 if (fr->fr_layout != FR_ROW)
5741 {
5742 if (fr == topframe) /* only a column of windows (cannot happen?) */
5743 return;
5744 curfr = fr;
5745 fr = fr->fr_parent;
5746 }
5747
5748 /* If this is the last frame in a row, may want to resize a parent
5749 * frame instead. */
5750 while (curfr->fr_next == NULL)
5751 {
5752 if (fr == topframe)
5753 break;
5754 curfr = fr;
5755 fr = fr->fr_parent;
5756 if (fr != topframe)
5757 {
5758 curfr = fr;
5759 fr = fr->fr_parent;
5760 }
5761 }
5762
5763 if (offset < 0) /* drag left */
5764 {
5765 left = TRUE;
5766 offset = -offset;
5767 /* sum up the room of the current frame and left of it */
5768 room = 0;
5769 for (fr = fr->fr_child; ; fr = fr->fr_next)
5770 {
5771 room += fr->fr_width - frame_minwidth(fr, NULL);
5772 if (fr == curfr)
5773 break;
5774 }
5775 fr = curfr->fr_next; /* put fr at frame that grows */
5776 }
5777 else /* drag right */
5778 {
5779 left = FALSE;
5780 /* sum up the room of frames right of the current one */
5781 room = 0;
5782 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5783 room += fr->fr_width - frame_minwidth(fr, NULL);
5784 fr = curfr; /* put fr at window that grows */
5785 }
5786
5787 if (room < offset) /* Not enough room */
5788 offset = room; /* Move as far as we can */
5789 if (offset <= 0) /* No room at all, quit. */
5790 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005791 if (fr == NULL)
5792 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005795 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 /* shrink other frames: current and at the left or at the right */
5798 if (left)
5799 fr = curfr; /* current frame gets smaller */
5800 else
5801 fr = curfr->fr_next; /* next frame gets smaller */
5802
5803 while (fr != NULL && offset > 0)
5804 {
5805 n = frame_minwidth(fr, NULL);
5806 if (fr->fr_width - offset <= n)
5807 {
5808 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005809 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 else
5812 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005813 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815 }
5816 if (left)
5817 fr = fr->fr_prev;
5818 else
5819 fr = fr->fr_next;
5820 }
5821 (void)win_comp_pos();
5822 redraw_all_later(NOT_VALID);
5823}
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005824# endif /* FEAT_VERTSPLIT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#endif /* FEAT_MOUSE */
5826
5827#endif /* FEAT_WINDOWS */
5828
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005829#define FRACTION_MULT 16384L
5830
5831/*
5832 * Set wp->w_fraction for the current w_wrow and w_height.
5833 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005834 void
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005835set_fraction(wp)
5836 win_T *wp;
5837{
5838 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005839 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005840}
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/*
5843 * Set the height of a window.
5844 * This takes care of the things inside the window, not what happens to the
5845 * window position, the frame or to other windows.
5846 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005847 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848win_new_height(wp, height)
5849 win_T *wp;
5850 int height;
5851{
5852 linenr_T lnum;
5853 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005854 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856 /* Don't want a negative height. Happens when splitting a tiny window.
5857 * Will equalize heights soon to fix it. */
5858 if (height < 0)
5859 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005860 if (wp->w_height == height)
5861 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005863 if (wp->w_height > 0)
5864 {
5865 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005866 /* w_wrow needs to be valid. When setting 'laststatus' this may
5867 * call win_new_height() recursively. */
5868 validate_cursor();
5869 if (wp->w_height != prev_height)
5870 return; /* Recursive call already changed the size, bail out here
5871 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005872 if (wp->w_wrow != wp->w_prev_fraction_row)
5873 set_fraction(wp);
5874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 wp->w_height = height;
5877 wp->w_skipcol = 0;
5878
5879 /* Don't change w_topline when height is zero. Don't set w_topline when
5880 * 'scrollbind' is set and this isn't the current window. */
5881 if (height > 0
5882#ifdef FEAT_SCROLLBIND
5883 && (!wp->w_p_scb || wp == curwin)
5884#endif
5885 )
5886 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005887 /*
5888 * Find a value for w_topline that shows the cursor at the same
5889 * relative position in the window as before (more or less).
5890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 lnum = wp->w_cursor.lnum;
5892 if (lnum < 1) /* can happen when starting up */
5893 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005894 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5895 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5897 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005898
5899 if (sline >= 0)
5900 {
5901 /* Make sure the whole cursor line is visible, if possible. */
5902 int rows = plines_win(wp, lnum, FALSE);
5903
5904 if (sline > wp->w_height - rows)
5905 {
5906 sline = wp->w_height - rows;
5907 wp->w_wrow -= rows - line_size;
5908 }
5909 }
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (sline < 0)
5912 {
5913 /*
5914 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005915 * Make cursor line the first line in the window. If not enough
5916 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005919 if (wp->w_wrow >= wp->w_height
5920 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5921 {
5922 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5923 --wp->w_wrow;
5924 while (wp->w_wrow >= wp->w_height)
5925 {
5926 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5927 + win_col_off2(wp);
5928 --wp->w_wrow;
5929 }
5930 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005931 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005933 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005935 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937#ifdef FEAT_FOLDING
5938 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5939 if (lnum == 1)
5940 {
5941 /* first line in buffer is folded */
5942 line_size = 1;
5943 --sline;
5944 break;
5945 }
5946#endif
5947 --lnum;
5948#ifdef FEAT_DIFF
5949 if (lnum == wp->w_topline)
5950 line_size = plines_win_nofill(wp, lnum, TRUE)
5951 + wp->w_topfill;
5952 else
5953#endif
5954 line_size = plines_win(wp, lnum, TRUE);
5955 sline -= line_size;
5956 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (sline < 0)
5959 {
5960 /*
5961 * Line we want at top would go off top of screen. Use next
5962 * line instead.
5963 */
5964#ifdef FEAT_FOLDING
5965 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5966#endif
5967 lnum++;
5968 wp->w_wrow -= line_size + sline;
5969 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005970 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
5972 /* First line of file reached, use that as topline. */
5973 lnum = 1;
5974 wp->w_wrow -= sline;
5975 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005976
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005977 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980
5981 if (wp == curwin)
5982 {
5983 if (p_so)
5984 update_topline();
5985 curs_columns(FALSE); /* validate w_wrow */
5986 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005987 if (prev_height > 0)
5988 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005991 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992#ifdef FEAT_WINDOWS
5993 wp->w_redr_status = TRUE;
5994#endif
5995 invalidate_botline_win(wp);
5996}
5997
5998#ifdef FEAT_VERTSPLIT
5999/*
6000 * Set the width of a window.
6001 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006002 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003win_new_width(wp, width)
6004 win_T *wp;
6005 int width;
6006{
6007 wp->w_width = width;
6008 wp->w_lines_valid = 0;
6009 changed_line_abv_curs_win(wp);
6010 invalidate_botline_win(wp);
6011 if (wp == curwin)
6012 {
6013 update_topline();
6014 curs_columns(TRUE); /* validate w_wrow */
6015 }
6016 redraw_win_later(wp, NOT_VALID);
6017 wp->w_redr_status = TRUE;
6018}
6019#endif
6020
6021 void
6022win_comp_scroll(wp)
6023 win_T *wp;
6024{
6025 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6026 if (wp->w_p_scr == 0)
6027 wp->w_p_scr = 1;
6028}
6029
6030/*
6031 * command_height: called whenever p_ch has been changed
6032 */
6033 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006034command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036#ifdef FEAT_WINDOWS
6037 int h;
6038 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006039 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006041 /* Use the value of p_ch that we remembered. This is needed for when the
6042 * GUI starts up, we can't be sure in what order things happen. And when
6043 * p_ch was changed in another tab page. */
6044 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 /* Find bottom frame with width of screen. */
6047 frp = lastwin->w_frame;
6048# ifdef FEAT_VERTSPLIT
6049 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6050 frp = frp->fr_parent;
6051# endif
6052
6053 /* Avoid changing the height of a window with 'winfixheight' set. */
6054 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6055 && frp->fr_win->w_p_wfh)
6056 frp = frp->fr_prev;
6057
6058 if (starting != NO_SCREEN)
6059 {
6060 cmdline_row = Rows - p_ch;
6061
6062 if (p_ch > old_p_ch) /* p_ch got bigger */
6063 {
6064 while (p_ch > old_p_ch)
6065 {
6066 if (frp == NULL)
6067 {
6068 EMSG(_(e_noroom));
6069 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006070 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 cmdline_row = Rows - p_ch;
6072 break;
6073 }
6074 h = frp->fr_height - frame_minheight(frp, NULL);
6075 if (h > p_ch - old_p_ch)
6076 h = p_ch - old_p_ch;
6077 old_p_ch += h;
6078 frame_add_height(frp, -h);
6079 frp = frp->fr_prev;
6080 }
6081
6082 /* Recompute window positions. */
6083 (void)win_comp_pos();
6084
6085 /* clear the lines added to cmdline */
6086 if (full_screen)
6087 screen_fill((int)(cmdline_row), (int)Rows, 0,
6088 (int)Columns, ' ', ' ', 0);
6089 msg_row = cmdline_row;
6090 redraw_cmdline = TRUE;
6091 return;
6092 }
6093
6094 if (msg_row < cmdline_row)
6095 msg_row = cmdline_row;
6096 redraw_cmdline = TRUE;
6097 }
6098 frame_add_height(frp, (int)(old_p_ch - p_ch));
6099
6100 /* Recompute window positions. */
6101 if (frp != lastwin->w_frame)
6102 (void)win_comp_pos();
6103#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006105 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106#endif
6107}
6108
6109#if defined(FEAT_WINDOWS) || defined(PROTO)
6110/*
6111 * Resize frame "frp" to be "n" lines higher (negative for less high).
6112 * Also resize the frames it is contained in.
6113 */
6114 static void
6115frame_add_height(frp, n)
6116 frame_T *frp;
6117 int n;
6118{
6119 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6120 for (;;)
6121 {
6122 frp = frp->fr_parent;
6123 if (frp == NULL)
6124 break;
6125 frp->fr_height += n;
6126 }
6127}
6128
6129/*
6130 * Add or remove a status line for the bottom window(s), according to the
6131 * value of 'laststatus'.
6132 */
6133 void
6134last_status(morewin)
6135 int morewin; /* pretend there are two or more windows */
6136{
6137 /* Don't make a difference between horizontal or vertical split. */
6138 last_status_rec(topframe, (p_ls == 2
6139 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6140}
6141
6142 static void
6143last_status_rec(fr, statusline)
6144 frame_T *fr;
6145 int statusline;
6146{
6147 frame_T *fp;
6148 win_T *wp;
6149
6150 if (fr->fr_layout == FR_LEAF)
6151 {
6152 wp = fr->fr_win;
6153 if (wp->w_status_height != 0 && !statusline)
6154 {
6155 /* remove status line */
6156 win_new_height(wp, wp->w_height + 1);
6157 wp->w_status_height = 0;
6158 comp_col();
6159 }
6160 else if (wp->w_status_height == 0 && statusline)
6161 {
6162 /* Find a frame to take a line from. */
6163 fp = fr;
6164 while (fp->fr_height <= frame_minheight(fp, NULL))
6165 {
6166 if (fp == topframe)
6167 {
6168 EMSG(_(e_noroom));
6169 return;
6170 }
6171 /* In a column of frames: go to frame above. If already at
6172 * the top or in a row of frames: go to parent. */
6173 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6174 fp = fp->fr_prev;
6175 else
6176 fp = fp->fr_parent;
6177 }
6178 wp->w_status_height = 1;
6179 if (fp != fr)
6180 {
6181 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6182 frame_fix_height(wp);
6183 (void)win_comp_pos();
6184 }
6185 else
6186 win_new_height(wp, wp->w_height - 1);
6187 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006188 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190 }
6191#ifdef FEAT_VERTSPLIT
6192 else if (fr->fr_layout == FR_ROW)
6193 {
6194 /* vertically split windows, set status line for each one */
6195 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6196 last_status_rec(fp, statusline);
6197 }
6198#endif
6199 else
6200 {
6201 /* horizontally split window, set status line for last one */
6202 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6203 ;
6204 last_status_rec(fp, statusline);
6205 }
6206}
6207
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006209 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210 */
6211 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006212tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006214#ifdef FEAT_GUI_TABLINE
6215 /* When the GUI has the tabline then this always returns zero. */
6216 if (gui_use_tabline())
6217 return 0;
6218#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006219 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006220 {
6221 case 0: return 0;
6222 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6223 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 return 1;
6225}
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227#endif /* FEAT_WINDOWS */
6228
6229#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6230/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006231 * Get the file name at the cursor.
6232 * If Visual mode is active, use the selected text if it's in one line.
6233 * Returns the name in allocated memory, NULL for failure.
6234 */
6235 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006236grab_file_name(count, file_lnum)
6237 long count;
6238 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006239{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006240 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6241
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006242 if (VIsual_active)
6243 {
6244 int len;
6245 char_u *ptr;
6246
6247 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6248 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006249 return find_file_name_in_path(ptr, len, options,
6250 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006251 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006252 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006253
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006254}
6255
6256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 * Return the file name under or after the cursor.
6258 *
6259 * The 'path' option is searched if the file name is not absolute.
6260 * The string returned has been alloc'ed and should be freed by the caller.
6261 * NULL is returned if the file name or file is not found.
6262 *
6263 * options:
6264 * FNAME_MESS give error messages
6265 * FNAME_EXP expand to path
6266 * FNAME_HYP check for hypertext link
6267 * FNAME_INCL apply "includeexpr"
6268 */
6269 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006270file_name_at_cursor(options, count, file_lnum)
6271 int options;
6272 long count;
6273 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006276 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6277 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278}
6279
6280/*
6281 * Return the name of the file under or after ptr[col].
6282 * Otherwise like file_name_at_cursor().
6283 */
6284 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006285file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 char_u *line;
6287 int col;
6288 int options;
6289 long count;
6290 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006291 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 char_u *ptr;
6294 int len;
6295
6296 /*
6297 * search forward for what could be the start of a file name
6298 */
6299 ptr = line + col;
6300 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006301 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (*ptr == NUL) /* nothing found */
6303 {
6304 if (options & FNAME_MESS)
6305 EMSG(_("E446: No file name under cursor"));
6306 return NULL;
6307 }
6308
6309 /*
6310 * Search backward for first char of the file name.
6311 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6312 */
6313 while (ptr > line)
6314 {
6315#ifdef FEAT_MBYTE
6316 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6317 ptr -= len + 1;
6318 else
6319#endif
6320 if (vim_isfilec(ptr[-1])
6321 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6322 --ptr;
6323 else
6324 break;
6325 }
6326
6327 /*
6328 * Search forward for the last char of the file name.
6329 * Also allow "://" when ':' is not in 'isfname'.
6330 */
6331 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006332 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006334 {
6335 if (ptr[len] == '\\')
6336 /* Skip over the "\" in "\ ". */
6337 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338#ifdef FEAT_MBYTE
6339 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006340 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 else
6342#endif
6343 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346 /*
6347 * If there is trailing punctuation, remove it.
6348 * But don't remove "..", could be a directory name.
6349 */
6350 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6351 && ptr[len - 2] != '.')
6352 --len;
6353
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006354 if (file_lnum != NULL)
6355 {
6356 char_u *p;
6357
6358 /* Get the number after the file name and a separator character */
6359 p = ptr + len;
6360 p = skipwhite(p);
6361 if (*p != NUL)
6362 {
6363 if (!isdigit(*p))
6364 ++p; /* skip the separator */
6365 p = skipwhite(p);
6366 if (isdigit(*p))
6367 *file_lnum = (int)getdigits(&p);
6368 }
6369 }
6370
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6372}
6373
6374# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6375static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6376
6377 static char_u *
6378eval_includeexpr(ptr, len)
6379 char_u *ptr;
6380 int len;
6381{
6382 char_u *res;
6383
6384 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006385 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006386 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 set_vim_var_string(VV_FNAME, NULL, 0);
6388 return res;
6389}
6390#endif
6391
6392/*
6393 * Return the name of the file ptr[len] in 'path'.
6394 * Otherwise like file_name_at_cursor().
6395 */
6396 char_u *
6397find_file_name_in_path(ptr, len, options, count, rel_fname)
6398 char_u *ptr;
6399 int len;
6400 int options;
6401 long count;
6402 char_u *rel_fname; /* file we are searching relative to */
6403{
6404 char_u *file_name;
6405 int c;
6406# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6407 char_u *tofree = NULL;
6408
6409 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6410 {
6411 tofree = eval_includeexpr(ptr, len);
6412 if (tofree != NULL)
6413 {
6414 ptr = tofree;
6415 len = (int)STRLEN(ptr);
6416 }
6417 }
6418# endif
6419
6420 if (options & FNAME_EXP)
6421 {
6422 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6423 TRUE, rel_fname);
6424
6425# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6426 /*
6427 * If the file could not be found in a normal way, try applying
6428 * 'includeexpr' (unless done already).
6429 */
6430 if (file_name == NULL
6431 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6432 {
6433 tofree = eval_includeexpr(ptr, len);
6434 if (tofree != NULL)
6435 {
6436 ptr = tofree;
6437 len = (int)STRLEN(ptr);
6438 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6439 TRUE, rel_fname);
6440 }
6441 }
6442# endif
6443 if (file_name == NULL && (options & FNAME_MESS))
6444 {
6445 c = ptr[len];
6446 ptr[len] = NUL;
6447 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6448 ptr[len] = c;
6449 }
6450
6451 /* Repeat finding the file "count" times. This matters when it
6452 * appears several times in the path. */
6453 while (file_name != NULL && --count > 0)
6454 {
6455 vim_free(file_name);
6456 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6457 }
6458 }
6459 else
6460 file_name = vim_strnsave(ptr, len);
6461
6462# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6463 vim_free(tofree);
6464# endif
6465
6466 return file_name;
6467}
6468#endif /* FEAT_SEARCHPATH */
6469
6470/*
6471 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6472 * Also check for ":\\", which MS Internet Explorer accepts, return
6473 * URL_BACKSLASH.
6474 */
6475 static int
6476path_is_url(p)
6477 char_u *p;
6478{
6479 if (STRNCMP(p, "://", (size_t)3) == 0)
6480 return URL_SLASH;
6481 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6482 return URL_BACKSLASH;
6483 return 0;
6484}
6485
6486/*
6487 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6488 * Return URL_BACKSLASH for "name:\\".
6489 * Return zero otherwise.
6490 */
6491 int
6492path_with_url(fname)
6493 char_u *fname;
6494{
6495 char_u *p;
6496
6497 for (p = fname; isalpha(*p); ++p)
6498 ;
6499 return path_is_url(p);
6500}
6501
6502/*
6503 * Return TRUE if "name" is a full (absolute) path name or URL.
6504 */
6505 int
6506vim_isAbsName(name)
6507 char_u *name;
6508{
6509 return (path_with_url(name) != 0 || mch_isFullName(name));
6510}
6511
6512/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006513 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 *
6515 * return FAIL for failure, OK otherwise
6516 */
6517 int
6518vim_FullName(fname, buf, len, force)
6519 char_u *fname, *buf;
6520 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006521 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 int retval = OK;
6524 int url;
6525
6526 *buf = NUL;
6527 if (fname == NULL)
6528 return FAIL;
6529
6530 url = path_with_url(fname);
6531 if (!url)
6532 retval = mch_FullName(fname, buf, len, force);
6533 if (url || retval == FAIL)
6534 {
6535 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006536 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
Bram Moolenaare7fedb62015-12-31 19:07:19 +01006538#if defined(MACOS_CLASSIC) || defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 slash_adjust(buf);
6540#endif
6541 return retval;
6542}
6543
6544/*
6545 * Return the minimal number of rows that is needed on the screen to display
6546 * the current number of windows.
6547 */
6548 int
6549min_rows()
6550{
6551 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006552#ifdef FEAT_WINDOWS
6553 tabpage_T *tp;
6554 int n;
6555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 if (firstwin == NULL) /* not initialized yet */
6558 return MIN_LINES;
6559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006561 total = 0;
6562 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6563 {
6564 n = frame_minheight(tp->tp_topframe, NULL);
6565 if (total < n)
6566 total = n;
6567 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006568 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006570 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006572 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 return total;
6574}
6575
6576/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006577 * Return TRUE if there is only one window (in the current tab page), not
6578 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006579 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
6581 int
6582only_one_window()
6583{
6584#ifdef FEAT_WINDOWS
6585 int count = 0;
6586 win_T *wp;
6587
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006588 /* If there is another tab page there always is another window. */
6589 if (first_tabpage->tp_next != NULL)
6590 return FALSE;
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006593 if (wp->w_buffer != NULL
6594 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595# ifdef FEAT_QUICKFIX
6596 || wp->w_p_pvw
6597# endif
6598 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006599# ifdef FEAT_AUTOCMD
6600 && wp != aucmd_win
6601# endif
6602 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 ++count;
6604 return (count <= 1);
6605#else
6606 return TRUE;
6607#endif
6608}
6609
6610#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6611/*
6612 * Correct the cursor line number in other windows. Used after changing the
6613 * current buffer, and before applying autocommands.
6614 * When "do_curwin" is TRUE, also check current window.
6615 */
6616 void
6617check_lnums(do_curwin)
6618 int do_curwin;
6619{
6620 win_T *wp;
6621
6622#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006623 tabpage_T *tp;
6624
6625 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6627#else
6628 wp = curwin;
6629 if (do_curwin)
6630#endif
6631 {
6632 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6633 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6634 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6635 wp->w_topline = curbuf->b_ml.ml_line_count;
6636 }
6637}
6638#endif
6639
6640#if defined(FEAT_WINDOWS) || defined(PROTO)
6641
6642/*
6643 * A snapshot of the window sizes, to restore them after closing the help
6644 * window.
6645 * Only these fields are used:
6646 * fr_layout
6647 * fr_width
6648 * fr_height
6649 * fr_next
6650 * fr_child
6651 * fr_win (only valid for the old curwin, NULL otherwise)
6652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654/*
6655 * Create a snapshot of the current frame sizes.
6656 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006657 void
6658make_snapshot(idx)
6659 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006661 clear_snapshot(curtab, idx);
6662 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663}
6664
6665 static void
6666make_snapshot_rec(fr, frp)
6667 frame_T *fr;
6668 frame_T **frp;
6669{
6670 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6671 if (*frp == NULL)
6672 return;
6673 (*frp)->fr_layout = fr->fr_layout;
6674# ifdef FEAT_VERTSPLIT
6675 (*frp)->fr_width = fr->fr_width;
6676# endif
6677 (*frp)->fr_height = fr->fr_height;
6678 if (fr->fr_next != NULL)
6679 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6680 if (fr->fr_child != NULL)
6681 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6682 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6683 (*frp)->fr_win = curwin;
6684}
6685
6686/*
6687 * Remove any existing snapshot.
6688 */
6689 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006690clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006691 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006692 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006694 clear_snapshot_rec(tp->tp_snapshot[idx]);
6695 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697
6698 static void
6699clear_snapshot_rec(fr)
6700 frame_T *fr;
6701{
6702 if (fr != NULL)
6703 {
6704 clear_snapshot_rec(fr->fr_next);
6705 clear_snapshot_rec(fr->fr_child);
6706 vim_free(fr);
6707 }
6708}
6709
6710/*
6711 * Restore a previously created snapshot, if there is any.
6712 * This is only done if the screen size didn't change and the window layout is
6713 * still the same.
6714 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006715 void
6716restore_snapshot(idx, close_curwin)
6717 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 int close_curwin; /* closing current window */
6719{
6720 win_T *wp;
6721
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006722 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006724 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006726 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6727 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006729 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 win_comp_pos();
6731 if (wp != NULL && close_curwin)
6732 win_goto(wp);
6733 redraw_all_later(CLEAR);
6734 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006735 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736}
6737
6738/*
6739 * Check if frames "sn" and "fr" have the same layout, same following frames
6740 * and same children.
6741 */
6742 static int
6743check_snapshot_rec(sn, fr)
6744 frame_T *sn;
6745 frame_T *fr;
6746{
6747 if (sn->fr_layout != fr->fr_layout
6748 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6749 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6750 || (sn->fr_next != NULL
6751 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6752 || (sn->fr_child != NULL
6753 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6754 return FAIL;
6755 return OK;
6756}
6757
6758/*
6759 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6760 * following frames and children.
6761 * Returns a pointer to the old current window, or NULL.
6762 */
6763 static win_T *
6764restore_snapshot_rec(sn, fr)
6765 frame_T *sn;
6766 frame_T *fr;
6767{
6768 win_T *wp = NULL;
6769 win_T *wp2;
6770
6771 fr->fr_height = sn->fr_height;
6772# ifdef FEAT_VERTSPLIT
6773 fr->fr_width = sn->fr_width;
6774# endif
6775 if (fr->fr_layout == FR_LEAF)
6776 {
6777 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6778# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006779 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780# endif
6781 wp = sn->fr_win;
6782 }
6783 if (sn->fr_next != NULL)
6784 {
6785 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6786 if (wp2 != NULL)
6787 wp = wp2;
6788 }
6789 if (sn->fr_child != NULL)
6790 {
6791 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6792 if (wp2 != NULL)
6793 wp = wp2;
6794 }
6795 return wp;
6796}
6797
6798#endif
6799
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006800#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6801 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006802/*
6803 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006804 * restore_win() MUST be called to undo, also when FAIL is returned.
6805 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006806 * When "no_display" is TRUE the display won't be affected, no redraw is
6807 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006808 * Returns FAIL if switching to "win" failed.
6809 */
6810 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006811switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006812 win_T **save_curwin UNUSED;
6813 tabpage_T **save_curtab UNUSED;
6814 win_T *win UNUSED;
6815 tabpage_T *tp UNUSED;
6816 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006817{
6818# ifdef FEAT_AUTOCMD
6819 block_autocmds();
6820# endif
6821# ifdef FEAT_WINDOWS
6822 *save_curwin = curwin;
6823 if (tp != NULL)
6824 {
6825 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006826 if (no_display)
6827 {
6828 curtab->tp_firstwin = firstwin;
6829 curtab->tp_lastwin = lastwin;
6830 curtab = tp;
6831 firstwin = curtab->tp_firstwin;
6832 lastwin = curtab->tp_lastwin;
6833 }
6834 else
6835 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006836 }
6837 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006838 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006839 curwin = win;
6840 curbuf = curwin->w_buffer;
6841# endif
6842 return OK;
6843}
6844
6845/*
6846 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006847 * When "no_display" is TRUE the display won't be affected, no redraw is
6848 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006849 */
6850 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006851restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006852 win_T *save_curwin UNUSED;
6853 tabpage_T *save_curtab UNUSED;
6854 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006855{
6856# ifdef FEAT_WINDOWS
6857 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006858 {
6859 if (no_display)
6860 {
6861 curtab->tp_firstwin = firstwin;
6862 curtab->tp_lastwin = lastwin;
6863 curtab = save_curtab;
6864 firstwin = curtab->tp_firstwin;
6865 lastwin = curtab->tp_lastwin;
6866 }
6867 else
6868 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6869 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006870 if (win_valid(save_curwin))
6871 {
6872 curwin = save_curwin;
6873 curbuf = curwin->w_buffer;
6874 }
6875# endif
6876# ifdef FEAT_AUTOCMD
6877 unblock_autocmds();
6878# endif
6879}
6880
6881/*
6882 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6883 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6884 */
6885 void
6886switch_buffer(save_curbuf, buf)
6887 buf_T *buf;
6888 buf_T **save_curbuf;
6889{
6890# ifdef FEAT_AUTOCMD
6891 block_autocmds();
6892# endif
6893 *save_curbuf = curbuf;
6894 --curbuf->b_nwindows;
6895 curbuf = buf;
6896 curwin->w_buffer = buf;
6897 ++curbuf->b_nwindows;
6898}
6899
6900/*
6901 * Restore the current buffer after using switch_buffer().
6902 */
6903 void
6904restore_buffer(save_curbuf)
6905 buf_T *save_curbuf;
6906{
6907# ifdef FEAT_AUTOCMD
6908 unblock_autocmds();
6909# endif
6910 /* Check for valid buffer, just in case. */
6911 if (buf_valid(save_curbuf))
6912 {
6913 --curbuf->b_nwindows;
6914 curwin->w_buffer = save_curbuf;
6915 curbuf = save_curbuf;
6916 ++curbuf->b_nwindows;
6917 }
6918}
6919#endif
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6922/*
6923 * Return TRUE if there is any vertically split window.
6924 */
6925 int
6926win_hasvertsplit()
6927{
6928 frame_T *fr;
6929
6930 if (topframe->fr_layout == FR_ROW)
6931 return TRUE;
6932
6933 if (topframe->fr_layout == FR_COL)
6934 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6935 if (fr->fr_layout == FR_ROW)
6936 return TRUE;
6937
6938 return FALSE;
6939}
6940#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941
6942#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6943/*
6944 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006945 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006946 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6947 * If no particular ID is desired, -1 must be specified for 'id'.
6948 * Return ID of added match, -1 on failure.
6949 */
6950 int
Bram Moolenaar6561d522015-07-21 15:48:27 +02006951match_add(wp, grp, pat, prio, id, pos_list, conceal_char)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006952 win_T *wp;
6953 char_u *grp;
6954 char_u *pat;
6955 int prio;
6956 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006957 list_T *pos_list;
Bram Moolenaar6561d522015-07-21 15:48:27 +02006958 char_u *conceal_char UNUSED; /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006959{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006960 matchitem_T *cur;
6961 matchitem_T *prev;
6962 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006963 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006964 regprog_T *regprog = NULL;
6965 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006966
Bram Moolenaarb3414592014-06-17 17:48:32 +02006967 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006968 return -1;
6969 if (id < -1 || id == 0)
6970 {
6971 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6972 return -1;
6973 }
6974 if (id != -1)
6975 {
6976 cur = wp->w_match_head;
6977 while (cur != NULL)
6978 {
6979 if (cur->id == id)
6980 {
6981 EMSGN("E801: ID already taken: %ld", id);
6982 return -1;
6983 }
6984 cur = cur->next;
6985 }
6986 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006987 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006988 {
6989 EMSG2(_(e_nogroup), grp);
6990 return -1;
6991 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006992 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006993 {
6994 EMSG2(_(e_invarg2), pat);
6995 return -1;
6996 }
6997
6998 /* Find available match ID. */
6999 while (id == -1)
7000 {
7001 cur = wp->w_match_head;
7002 while (cur != NULL && cur->id != wp->w_next_match_id)
7003 cur = cur->next;
7004 if (cur == NULL)
7005 id = wp->w_next_match_id;
7006 wp->w_next_match_id++;
7007 }
7008
7009 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007010 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007011 m->id = id;
7012 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007013 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007014 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00007015 m->match.regprog = regprog;
7016 m->match.rmm_ic = FALSE;
7017 m->match.rmm_maxcol = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02007018#ifdef FEAT_CONCEAL
7019 m->conceal_char = 0;
7020 if (conceal_char != NULL)
7021 m->conceal_char = (*mb_ptr2char)(conceal_char);
7022#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007023
Bram Moolenaarb3414592014-06-17 17:48:32 +02007024 /* Set up position matches */
7025 if (pos_list != NULL)
7026 {
7027 linenr_T toplnum = 0;
7028 linenr_T botlnum = 0;
7029 listitem_T *li;
7030 int i;
7031
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007032 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007033 i++, li = li->li_next)
7034 {
7035 linenr_T lnum = 0;
7036 colnr_T col = 0;
7037 int len = 1;
7038 list_T *subl;
7039 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007040 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007041
Bram Moolenaarb3414592014-06-17 17:48:32 +02007042 if (li->li_tv.v_type == VAR_LIST)
7043 {
7044 subl = li->li_tv.vval.v_list;
7045 if (subl == NULL)
7046 goto fail;
7047 subli = subl->lv_first;
7048 if (subli == NULL)
7049 goto fail;
7050 lnum = get_tv_number_chk(&subli->li_tv, &error);
7051 if (error == TRUE)
7052 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007053 if (lnum == 0)
7054 {
7055 --i;
7056 continue;
7057 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007058 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007059 subli = subli->li_next;
7060 if (subli != NULL)
7061 {
7062 col = get_tv_number_chk(&subli->li_tv, &error);
7063 if (error == TRUE)
7064 goto fail;
7065 subli = subli->li_next;
7066 if (subli != NULL)
7067 {
7068 len = get_tv_number_chk(&subli->li_tv, &error);
7069 if (error == TRUE)
7070 goto fail;
7071 }
7072 }
7073 m->pos.pos[i].col = col;
7074 m->pos.pos[i].len = len;
7075 }
7076 else if (li->li_tv.v_type == VAR_NUMBER)
7077 {
7078 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007079 {
7080 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007081 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007082 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007083 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
7084 m->pos.pos[i].col = 0;
7085 m->pos.pos[i].len = 0;
7086 }
7087 else
7088 {
7089 EMSG(_("List or number required"));
7090 goto fail;
7091 }
7092 if (toplnum == 0 || lnum < toplnum)
7093 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02007094 if (botlnum == 0 || lnum >= botlnum)
7095 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007096 }
7097
7098 /* Calculate top and bottom lines for redrawing area */
7099 if (toplnum != 0)
7100 {
7101 if (wp->w_buffer->b_mod_set)
7102 {
7103 if (wp->w_buffer->b_mod_top > toplnum)
7104 wp->w_buffer->b_mod_top = toplnum;
7105 if (wp->w_buffer->b_mod_bot < botlnum)
7106 wp->w_buffer->b_mod_bot = botlnum;
7107 }
7108 else
7109 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007110 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007111 wp->w_buffer->b_mod_top = toplnum;
7112 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007113 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007114 }
7115 m->pos.toplnum = toplnum;
7116 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007117 rtype = VALID;
7118 }
7119 }
7120
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007121 /* Insert new match. The match list is in ascending order with regard to
7122 * the match priorities. */
7123 cur = wp->w_match_head;
7124 prev = cur;
7125 while (cur != NULL && prio >= cur->priority)
7126 {
7127 prev = cur;
7128 cur = cur->next;
7129 }
7130 if (cur == prev)
7131 wp->w_match_head = m;
7132 else
7133 prev->next = m;
7134 m->next = cur;
7135
Bram Moolenaarb3414592014-06-17 17:48:32 +02007136 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007137 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007138
7139fail:
7140 vim_free(m);
7141 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007142}
7143
7144/*
7145 * Delete match with ID 'id' in the match list of window 'wp'.
7146 * Print error messages if 'perr' is TRUE.
7147 */
7148 int
7149match_delete(wp, id, perr)
7150 win_T *wp;
7151 int id;
7152 int perr;
7153{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007154 matchitem_T *cur = wp->w_match_head;
7155 matchitem_T *prev = cur;
7156 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007157
7158 if (id < 1)
7159 {
7160 if (perr == TRUE)
7161 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7162 id);
7163 return -1;
7164 }
7165 while (cur != NULL && cur->id != id)
7166 {
7167 prev = cur;
7168 cur = cur->next;
7169 }
7170 if (cur == NULL)
7171 {
7172 if (perr == TRUE)
7173 EMSGN("E803: ID not found: %ld", id);
7174 return -1;
7175 }
7176 if (cur == prev)
7177 wp->w_match_head = cur->next;
7178 else
7179 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007180 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007181 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007182 if (cur->pos.toplnum != 0)
7183 {
7184 if (wp->w_buffer->b_mod_set)
7185 {
7186 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7187 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7188 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7189 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7190 }
7191 else
7192 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007193 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007194 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7195 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007196 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007197 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007198 rtype = VALID;
7199 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007200 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007201 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007202 return 0;
7203}
7204
7205/*
7206 * Delete all matches in the match list of window 'wp'.
7207 */
7208 void
7209clear_matches(wp)
7210 win_T *wp;
7211{
7212 matchitem_T *m;
7213
7214 while (wp->w_match_head != NULL)
7215 {
7216 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007217 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007218 vim_free(wp->w_match_head->pattern);
7219 vim_free(wp->w_match_head);
7220 wp->w_match_head = m;
7221 }
7222 redraw_later(SOME_VALID);
7223}
7224
7225/*
7226 * Get match from ID 'id' in window 'wp'.
7227 * Return NULL if match not found.
7228 */
7229 matchitem_T *
7230get_match(wp, id)
7231 win_T *wp;
7232 int id;
7233{
7234 matchitem_T *cur = wp->w_match_head;
7235
7236 while (cur != NULL && cur->id != id)
7237 cur = cur->next;
7238 return cur;
7239}
7240#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007241
7242#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7243 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007244get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007245{
7246 int i = 1;
7247 win_T *w;
7248
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007249 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007250 ++i;
7251
7252 if (w == NULL)
7253 return 0;
7254 else
7255 return i;
7256}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007257
7258 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007259get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007260{
7261 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007262# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007263 tabpage_T *t;
7264
7265 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7266 ++i;
7267
7268 if (t == NULL)
7269 return 0;
7270 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007271# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007272 return i;
7273}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007274#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007275
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007276#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007277/*
7278 * Return TRUE if "topfrp" and its children are at the right height.
7279 */
7280 static int
7281frame_check_height(topfrp, height)
7282 frame_T *topfrp;
7283 int height;
7284{
7285 frame_T *frp;
7286
7287 if (topfrp->fr_height != height)
7288 return FALSE;
7289
7290 if (topfrp->fr_layout == FR_ROW)
7291 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7292 if (frp->fr_height != height)
7293 return FALSE;
7294
7295 return TRUE;
7296}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007297#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007298
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007299#if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007300/*
7301 * Return TRUE if "topfrp" and its children are at the right width.
7302 */
7303 static int
7304frame_check_width(topfrp, width)
7305 frame_T *topfrp;
7306 int width;
7307{
7308 frame_T *frp;
7309
7310 if (topfrp->fr_width != width)
7311 return FALSE;
7312
7313 if (topfrp->fr_layout == FR_COL)
7314 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7315 if (frp->fr_width != width)
7316 return FALSE;
7317
7318 return TRUE;
7319}
7320#endif
7321