blob: eb218d5603394e65f3a50caa606eaa8d68acf9e3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +010014static void cmd_with_count __ARGS((char *cmd, char_u *bufp, size_t bufsize, long Prenum));
Bram Moolenaar884ae642009-02-22 01:37:59 +000015static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000016static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
18static void frame_setheight __ARGS((frame_T *curfrp, int height));
19#ifdef FEAT_VERTSPLIT
20static void frame_setwidth __ARGS((frame_T *curfrp, int width));
21#endif
22static void win_exchange __ARGS((long));
23static void win_rotate __ARGS((int, int));
24static void win_totop __ARGS((int size, int flags));
25static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000026static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020027static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000029static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000030static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static win_T *frame2win __ARGS((frame_T *frp));
32static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
33static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
34static int frame_fixed_height __ARGS((frame_T *frp));
35#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000036static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000038static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void frame_add_vsep __ARGS((frame_T *frp));
40static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
41static void frame_fix_width __ARGS((win_T *wp));
42#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000043#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000044static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000045static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000047static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020048static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
49static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void frame_fix_height __ARGS((win_T *wp));
51static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020052static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaarf740b292006-02-16 22:11:02 +000053static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static void frame_append __ARGS((frame_T *after, frame_T *frp));
55static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020057# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
Bram Moolenaarb893ac22013-06-26 14:04:47 +020070static int frame_check_height __ARGS((frame_T *topfrp, int height));
71#ifdef FEAT_VERTSPLIT
72static int frame_check_width __ARGS((frame_T *topfrp, int width));
73#endif
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000076
Bram Moolenaar746ebd32009-06-16 14:01:43 +000077static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010078static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80#define URL_SLASH 1 /* path_is_url() has found "://" */
81#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
82
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000083#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000086# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000087#else
88# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000089#endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000092
93static char *m_onlyone = N_("Already only one window");
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * all CTRL-W window commands are handled here, called from normal_cmd().
97 */
98 void
99do_window(nchar, Prenum, xchar)
100 int nchar;
101 long Prenum;
102 int xchar; /* extra char from ":wincmd gx" or NUL */
103{
104 long Prenum1;
105 win_T *wp;
106#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
107 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000108 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110#ifdef FEAT_FIND_ID
111 int type = FIND_DEFINE;
112 int len;
113#endif
114 char_u cbuf[40];
115
116 if (Prenum == 0)
117 Prenum1 = 1;
118 else
119 Prenum1 = Prenum;
120
121#ifdef FEAT_CMDWIN
122# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
123#else
124# define CHECK_CMDWIN
125#endif
126
127 switch (nchar)
128 {
129/* split current window in two parts, horizontally */
130 case 'S':
131 case Ctrl_S:
132 case 's':
133 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000135#ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143#endif
144 win_split((int)Prenum, 0);
145 break;
146
147#ifdef FEAT_VERTSPLIT
148/* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100171 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 do_cmdline_cmd(cbuf);
173 break;
174
175/* open new window */
176 case Ctrl_N:
177 case 'n':
178 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000180#ifdef FEAT_QUICKFIX
181newwindow:
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000184 /* window height */
185 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 else
187 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000188#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
189 if (nchar == 'v' || nchar == Ctrl_V)
190 STRCAT(cbuf, "v");
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100200 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100201 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 break;
203
204/* close current window */
205 case Ctrl_C:
206 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100208 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100209 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 break;
211
212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
213/* close preview window */
214 case Ctrl_Z:
215 case 'z':
216 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 do_cmdline_cmd((char_u *)"pclose");
219 break;
220
221/* cursor to preview window */
222 case 'P':
223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
224 if (wp->w_p_pvw)
225 break;
226 if (wp == NULL)
227 EMSG(_("E441: There is no preview window"));
228 else
229 win_goto(wp);
230 break;
231#endif
232
233/* close all but current window */
234 case Ctrl_O:
235 case 'o':
236 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100238 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100239 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 break;
241
242/* cursor to next window with wrap around */
243 case Ctrl_W:
244 case 'w':
245/* cursor to previous window with wrap around */
246 case 'W':
247 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000248 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 beep_flush();
250 else
251 {
252 if (Prenum) /* go to specified window */
253 {
254 for (wp = firstwin; --Prenum > 0; )
255 {
256 if (wp->w_next == NULL)
257 break;
258 else
259 wp = wp->w_next;
260 }
261 }
262 else
263 {
264 if (nchar == 'W') /* go to previous window */
265 {
266 wp = curwin->w_prev;
267 if (wp == NULL)
268 wp = lastwin; /* wrap around */
269 }
270 else /* go to next window */
271 {
272 wp = curwin->w_next;
273 if (wp == NULL)
274 wp = firstwin; /* wrap around */
275 }
276 }
277 win_goto(wp);
278 }
279 break;
280
281/* cursor to window below */
282 case 'j':
283 case K_DOWN:
284 case Ctrl_J:
285 CHECK_CMDWIN
286#ifdef FEAT_VERTSPLIT
287 win_goto_ver(FALSE, Prenum1);
288#else
289 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
290 wp = wp->w_next)
291 ;
292 win_goto(wp);
293#endif
294 break;
295
296/* cursor to window above */
297 case 'k':
298 case K_UP:
299 case Ctrl_K:
300 CHECK_CMDWIN
301#ifdef FEAT_VERTSPLIT
302 win_goto_ver(TRUE, Prenum1);
303#else
304 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
305 wp = wp->w_prev)
306 ;
307 win_goto(wp);
308#endif
309 break;
310
311#ifdef FEAT_VERTSPLIT
312/* cursor to left window */
313 case 'h':
314 case K_LEFT:
315 case Ctrl_H:
316 case K_BS:
317 CHECK_CMDWIN
318 win_goto_hor(TRUE, Prenum1);
319 break;
320
321/* cursor to right window */
322 case 'l':
323 case K_RIGHT:
324 case Ctrl_L:
325 CHECK_CMDWIN
326 win_goto_hor(FALSE, Prenum1);
327 break;
328#endif
329
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000330/* move window to new tab page */
331 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000332 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000333 MSG(_(m_onlyone));
334 else
335 {
336 tabpage_T *oldtab = curtab;
337 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000338
339 /* First create a new tab with the window, then go back to
340 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000341 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342 if (win_new_tabpage((int)Prenum) == OK
343 && valid_tabpage(oldtab))
344 {
345 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200346 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 if (curwin == wp)
348 win_close(curwin, FALSE);
349 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200350 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000351 }
352 }
353 break;
354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355/* cursor to top-left window */
356 case 't':
357 case Ctrl_T:
358 win_goto(firstwin);
359 break;
360
361/* cursor to bottom-right window */
362 case 'b':
363 case Ctrl_B:
364 win_goto(lastwin);
365 break;
366
367/* cursor to last accessed (previous) window */
368 case 'p':
369 case Ctrl_P:
370 if (prevwin == NULL)
371 beep_flush();
372 else
373 win_goto(prevwin);
374 break;
375
376/* exchange current and next window */
377 case 'x':
378 case Ctrl_X:
379 CHECK_CMDWIN
380 win_exchange(Prenum);
381 break;
382
383/* rotate windows downwards */
384 case Ctrl_R:
385 case 'r':
386 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 win_rotate(FALSE, (int)Prenum1); /* downwards */
389 break;
390
391/* rotate windows upwards */
392 case 'R':
393 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 win_rotate(TRUE, (int)Prenum1); /* upwards */
396 break;
397
398/* move window to the very top/bottom/left/right */
399 case 'K':
400 case 'J':
401#ifdef FEAT_VERTSPLIT
402 case 'H':
403 case 'L':
404#endif
405 CHECK_CMDWIN
406 win_totop((int)Prenum,
407 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
408 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
409 break;
410
411/* make all windows the same height */
412 case '=':
413#ifdef FEAT_GUI
414 need_mouse_correct = TRUE;
415#endif
416 win_equal(NULL, FALSE, 'b');
417 break;
418
419/* increase current window height */
420 case '+':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setheight(curwin->w_height + (int)Prenum1);
425 break;
426
427/* decrease current window height */
428 case '-':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_setheight(curwin->w_height - (int)Prenum1);
433 break;
434
435/* set current window height */
436 case Ctrl__:
437 case '_':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(Prenum ? (int)Prenum : 9999);
442 break;
443
444#ifdef FEAT_VERTSPLIT
445/* increase current window width */
446 case '>':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setwidth(curwin->w_width + (int)Prenum1);
451 break;
452
453/* decrease current window width */
454 case '<':
455#ifdef FEAT_GUI
456 need_mouse_correct = TRUE;
457#endif
458 win_setwidth(curwin->w_width - (int)Prenum1);
459 break;
460
461/* set current window width */
462 case '|':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
467 break;
468#endif
469
470/* jump to tag and split window if tag exists (in preview window) */
471#if defined(FEAT_QUICKFIX)
472 case '}':
473 CHECK_CMDWIN
474 if (Prenum)
475 g_do_tagpreview = Prenum;
476 else
477 g_do_tagpreview = p_pvh;
478 /*FALLTHROUGH*/
479#endif
480 case ']':
481 case Ctrl_RSB:
482 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200483 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (Prenum)
485 postponed_split = Prenum;
486 else
487 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200488#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200489 if (nchar != '}')
490 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaard355c502014-09-23 13:48:43 +0200493 /* Execute the command right here, required when "wincmd ]"
494 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 do_nv_ident(Ctrl_RSB, NUL);
496 break;
497
498#ifdef FEAT_SEARCHPATH
499/* edit file name under cursor in a new window */
500 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000503wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000505
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000506 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (ptr != NULL)
508 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000509# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 setpcmark();
513 if (win_split(0, 0) == OK)
514 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200515 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000516 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
517 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 if (nchar == 'F' && lnum >= 0)
519 {
520 curwin->w_cursor.lnum = lnum;
521 check_cursor_lnum();
522 beginline(BL_SOL | BL_FIX);
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 vim_free(ptr);
526 }
527 break;
528#endif
529
530#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000531/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 * new window -- webb
533 */
534 case 'i': /* Go to any match */
535 case Ctrl_I:
536 type = FIND_ANY;
537 /* FALLTHROUGH */
538 case 'd': /* Go to definition, using 'define' */
539 case Ctrl_D:
540 CHECK_CMDWIN
541 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
542 break;
543 find_pattern_in_path(ptr, 0, len, TRUE,
544 Prenum == 0 ? TRUE : FALSE, type,
545 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
546 curwin->w_set_curswant = TRUE;
547 break;
548#endif
549
Bram Moolenaar05159a02005-02-26 23:04:13 +0000550 case K_KENTER:
551 case CAR:
552#if defined(FEAT_QUICKFIX)
553 /*
554 * In a quickfix window a <CR> jumps to the error under the
555 * cursor in a new window.
556 */
557 if (bt_quickfix(curbuf))
558 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000559 sprintf((char *)cbuf, "split +%ld%s",
560 (long)curwin->w_cursor.lnum,
561 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000562 do_cmdline_cmd(cbuf);
563 }
564#endif
565 break;
566
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568/* CTRL-W g extended commands */
569 case 'g':
570 case Ctrl_G:
571 CHECK_CMDWIN
572#ifdef USE_ON_FLY_SCROLL
573 dont_scroll = TRUE; /* disallow scrolling here */
574#endif
575 ++no_mapping;
576 ++allow_keys; /* no mapping for xchar, but allow key codes */
577 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000578 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 --no_mapping;
581 --allow_keys;
582#ifdef FEAT_CMDL_INFO
583 (void)add_to_showcmd(xchar);
584#endif
585 switch (xchar)
586 {
587#if defined(FEAT_QUICKFIX)
588 case '}':
589 xchar = Ctrl_RSB;
590 if (Prenum)
591 g_do_tagpreview = Prenum;
592 else
593 g_do_tagpreview = p_pvh;
594 /*FALLTHROUGH*/
595#endif
596 case ']':
597 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200598 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (Prenum)
600 postponed_split = Prenum;
601 else
602 postponed_split = -1;
603
604 /* Execute the command right here, required when
605 * "wincmd g}" was used in a function. */
606 do_nv_ident('g', xchar);
607 break;
608
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609#ifdef FEAT_SEARCHPATH
610 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000611 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100612 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000613 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000614 goto wingotofile;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 default:
617 beep_flush();
618 break;
619 }
620 break;
621
622 default: beep_flush();
623 break;
624 }
625}
626
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100627/*
628 * Figure out the address type for ":wnncmd".
629 */
630 void
631get_wincmd_addr_type(arg, eap)
632 char_u *arg;
633 exarg_T *eap;
634{
635 switch (*arg)
636 {
637 case 'S':
638 case Ctrl_S:
639 case 's':
640 case Ctrl_N:
641 case 'n':
642 case 'j':
643 case Ctrl_J:
644 case 'k':
645 case Ctrl_K:
646 case 'T':
647 case Ctrl_R:
648 case 'r':
649 case 'R':
650 case 'K':
651 case 'J':
652 case '+':
653 case '-':
654 case Ctrl__:
655 case '_':
656 case '|':
657 case ']':
658 case Ctrl_RSB:
659 case 'g':
660 case Ctrl_G:
661#ifdef FEAT_VERTSPLIT
662 case Ctrl_V:
663 case 'v':
664 case 'h':
665 case Ctrl_H:
666 case 'l':
667 case Ctrl_L:
668 case 'H':
669 case 'L':
670 case '>':
671 case '<':
672#endif
673#if defined(FEAT_QUICKFIX)
674 case '}':
675#endif
676#ifdef FEAT_SEARCHPATH
677 case 'f':
678 case 'F':
679 case Ctrl_F:
680#endif
681#ifdef FEAT_FIND_ID
682 case 'i':
683 case Ctrl_I:
684 case 'd':
685 case Ctrl_D:
686#endif
687 /* window size or any count */
688 eap->addr_type = ADDR_LINES;
689 break;
690
691 case Ctrl_HAT:
692 case '^':
693 /* buffer number */
694 eap->addr_type = ADDR_BUFFERS;
695 break;
696
697 case Ctrl_Q:
698 case 'q':
699 case Ctrl_C:
700 case 'c':
701 case Ctrl_O:
702 case 'o':
703 case Ctrl_W:
704 case 'w':
705 case 'W':
706 case 'x':
707 case Ctrl_X:
708 /* window number */
709 eap->addr_type = ADDR_WINDOWS;
710 break;
711
712#if defined(FEAT_QUICKFIX)
713 case Ctrl_Z:
714 case 'z':
715 case 'P':
716#endif
717 case 't':
718 case Ctrl_T:
719 case 'b':
720 case Ctrl_B:
721 case 'p':
722 case Ctrl_P:
723 case '=':
724 case CAR:
725 /* no count */
726 eap->addr_type = 0;
727 break;
728 }
729}
730
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100731 static void
732cmd_with_count(cmd, bufp, bufsize, Prenum)
733 char *cmd;
734 char_u *bufp;
735 size_t bufsize;
736 long Prenum;
737{
738 size_t len = STRLEN(cmd);
739
740 STRCPY(bufp, cmd);
741 if (Prenum > 0)
742 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
743}
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745/*
746 * split the current window, implements CTRL-W s and :split
747 *
748 * "size" is the height or width for the new window, 0 to use half of current
749 * height or width.
750 *
751 * "flags":
752 * WSP_ROOM: require enough room for new window
753 * WSP_VERT: vertical split.
754 * WSP_TOP: open window at the top-left of the shell (help window).
755 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
756 * WSP_HELP: creating the help window, keep layout snapshot
757 *
758 * return FAIL for failure, OK otherwise
759 */
760 int
761win_split(size, flags)
762 int size;
763 int flags;
764{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000765 /* When the ":tab" modifier was used open a new tab page instead. */
766 if (may_open_tabpage() == OK)
767 return OK;
768
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 /* Add flags from ":vertical", ":topleft" and ":botright". */
770 flags |= cmdmod.split;
771 if ((flags & WSP_TOP) && (flags & WSP_BOT))
772 {
773 EMSG(_("E442: Can't split topleft and botright at the same time"));
774 return FAIL;
775 }
776
777 /* When creating the help window make a snapshot of the window layout.
778 * Otherwise clear the snapshot, it's now invalid. */
779 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000780 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000782 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
784 return win_split_ins(size, flags, NULL, 0);
785}
786
787/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100788 * When "new_wp" is NULL: split the current window in two.
789 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * top/left/right/bottom.
791 * return FAIL for failure, OK otherwise
792 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000793 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100794win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 int size;
796 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100797 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 int dir;
799{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100800 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 win_T *oldwin;
802 int new_size = size;
803 int i;
804 int need_status = 0;
805 int do_equal = FALSE;
806 int needed;
807 int available;
808 int oldwin_height = 0;
809 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200810 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200812 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200813 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 if (flags & WSP_TOP)
816 oldwin = firstwin;
817 else if (flags & WSP_BOT)
818 oldwin = lastwin;
819 else
820 oldwin = curwin;
821
822 /* add a status line when p_ls == 1 and splitting the first window */
823 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
824 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100825 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 EMSG(_(e_noroom));
828 return FAIL;
829 }
830 need_status = STATUS_HEIGHT;
831 }
832
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000833#ifdef FEAT_GUI
834 /* May be needed for the scrollbars that are going to change. */
835 if (gui.in_use)
836 out_flush();
837#endif
838
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_VERTSPLIT
840 if (flags & WSP_VERT)
841 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200842 int wmw1;
843 int minwidth;
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846
847 /*
848 * Check if we are able to split the current window and compute its
849 * width.
850 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200851 /* Current window requires at least 1 space. */
852 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
853 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200855 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200856 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200858 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200860 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200862 else if (p_ea)
863 {
864 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
865 prevfrp = oldwin->w_frame;
866 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
867 frp = frp->fr_parent)
868 {
869 if (frp->fr_layout == FR_ROW)
870 for (frp2 = frp->fr_child; frp2 != NULL;
871 frp2 = frp2->fr_next)
872 if (frp2 != prevfrp)
873 minwidth += frame_minwidth(frp2, NOWIN);
874 prevfrp = frp;
875 }
876 available = topframe->fr_width;
877 needed += minwidth;
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200880 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200881 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
882 available = oldwin->w_frame->fr_width;
883 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200884 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100885 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 {
887 EMSG(_(e_noroom));
888 return FAIL;
889 }
890 if (new_size == 0)
891 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200892 if (new_size > available - minwidth - 1)
893 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200894 if (new_size < wmw1)
895 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 /* if it doesn't fit in the current window, need win_equal() */
898 if (oldwin->w_width - new_size - 1 < p_wmw)
899 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000900
901 /* We don't like to take lines for the new window from a
902 * 'winfixwidth' window. Take them from a window to the left or right
903 * instead, if possible. */
904 if (oldwin->w_p_wfw)
905 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000906
907 /* Only make all windows the same width if one of them (except oldwin)
908 * is wider than one of the split windows. */
909 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
910 && oldwin->w_frame->fr_parent != NULL)
911 {
912 frp = oldwin->w_frame->fr_parent->fr_child;
913 while (frp != NULL)
914 {
915 if (frp->fr_win != oldwin && frp->fr_win != NULL
916 && (frp->fr_win->w_width > new_size
917 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100918 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000919 {
920 do_equal = TRUE;
921 break;
922 }
923 frp = frp->fr_next;
924 }
925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 }
927 else
928#endif
929 {
930 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
932 /*
933 * Check if we are able to split the current window and compute its
934 * height.
935 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200936 /* Current window requires at least 1 space. */
937 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
938 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200940 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200941 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200943 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200945 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200947 else if (p_ea)
948 {
949 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
950 prevfrp = oldwin->w_frame;
951 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
952 frp = frp->fr_parent)
953 {
954 if (frp->fr_layout == FR_COL)
955 for (frp2 = frp->fr_child; frp2 != NULL;
956 frp2 = frp2->fr_next)
957 if (frp2 != prevfrp)
958 minheight += frame_minheight(frp2, NOWIN);
959 prevfrp = frp;
960 }
961 available = topframe->fr_height;
962 needed += minheight;
963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 else
965 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200966 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
967 available = oldwin->w_frame->fr_height;
968 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100970 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 EMSG(_(e_noroom));
973 return FAIL;
974 }
975 oldwin_height = oldwin->w_height;
976 if (need_status)
977 {
978 oldwin->w_status_height = STATUS_HEIGHT;
979 oldwin_height -= STATUS_HEIGHT;
980 }
981 if (new_size == 0)
982 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200983 if (new_size > available - minheight - STATUS_HEIGHT)
984 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200985 if (new_size < wmh1)
986 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 /* if it doesn't fit in the current window, need win_equal() */
989 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
990 do_equal = TRUE;
991
992 /* We don't like to take lines for the new window from a
993 * 'winfixheight' window. Take them from a window above or below
994 * instead, if possible. */
995 if (oldwin->w_p_wfh)
996 {
997 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
998 oldwin);
999 oldwin_height = oldwin->w_height;
1000 if (need_status)
1001 oldwin_height -= STATUS_HEIGHT;
1002 }
Bram Moolenaar67f71312007-08-12 14:55:56 +00001003
1004 /* Only make all windows the same height if one of them (except oldwin)
1005 * is higher than one of the split windows. */
1006 if (!do_equal && p_ea && size == 0
1007#ifdef FEAT_VERTSPLIT
1008 && *p_ead != 'h'
1009#endif
1010 && oldwin->w_frame->fr_parent != NULL)
1011 {
1012 frp = oldwin->w_frame->fr_parent->fr_child;
1013 while (frp != NULL)
1014 {
1015 if (frp->fr_win != oldwin && frp->fr_win != NULL
1016 && (frp->fr_win->w_height > new_size
1017 || frp->fr_win->w_height > oldwin_height - new_size
1018 - STATUS_HEIGHT))
1019 {
1020 do_equal = TRUE;
1021 break;
1022 }
1023 frp = frp->fr_next;
1024 }
1025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027
1028 /*
1029 * allocate new window structure and link it in the window list
1030 */
1031 if ((flags & WSP_TOP) == 0
1032 && ((flags & WSP_BOT)
1033 || (flags & WSP_BELOW)
1034 || (!(flags & WSP_ABOVE)
1035 && (
1036#ifdef FEAT_VERTSPLIT
1037 (flags & WSP_VERT) ? p_spr :
1038#endif
1039 p_sb))))
1040 {
1041 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001042 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001043 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 else
1045 win_append(oldwin, wp);
1046 }
1047 else
1048 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001049 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001050 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else
1052 win_append(oldwin->w_prev, wp);
1053 }
1054
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001055 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 {
1057 if (wp == NULL)
1058 return FAIL;
1059
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001060 new_frame(wp);
1061 if (wp->w_frame == NULL)
1062 {
1063 win_free(wp, NULL);
1064 return FAIL;
1065 }
1066
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001067 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001068 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070
1071 /*
1072 * Reorganise the tree of frames to insert the new window.
1073 */
1074 if (flags & (WSP_TOP | WSP_BOT))
1075 {
1076#ifdef FEAT_VERTSPLIT
1077 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1078 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1079#else
1080 if (topframe->fr_layout == FR_COL)
1081#endif
1082 {
1083 curfrp = topframe->fr_child;
1084 if (flags & WSP_BOT)
1085 while (curfrp->fr_next != NULL)
1086 curfrp = curfrp->fr_next;
1087 }
1088 else
1089 curfrp = topframe;
1090 before = (flags & WSP_TOP);
1091 }
1092 else
1093 {
1094 curfrp = oldwin->w_frame;
1095 if (flags & WSP_BELOW)
1096 before = FALSE;
1097 else if (flags & WSP_ABOVE)
1098 before = TRUE;
1099 else
1100#ifdef FEAT_VERTSPLIT
1101 if (flags & WSP_VERT)
1102 before = !p_spr;
1103 else
1104#endif
1105 before = !p_sb;
1106 }
1107 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1108 {
1109 /* Need to create a new frame in the tree to make a branch. */
1110 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1111 *frp = *curfrp;
1112 curfrp->fr_layout = layout;
1113 frp->fr_parent = curfrp;
1114 frp->fr_next = NULL;
1115 frp->fr_prev = NULL;
1116 curfrp->fr_child = frp;
1117 curfrp->fr_win = NULL;
1118 curfrp = frp;
1119 if (frp->fr_win != NULL)
1120 oldwin->w_frame = frp;
1121 else
1122 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1123 frp->fr_parent = curfrp;
1124 }
1125
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001126 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001127 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001129 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 frp->fr_parent = curfrp->fr_parent;
1131
1132 /* Insert the new frame at the right place in the frame list. */
1133 if (before)
1134 frame_insert(curfrp, frp);
1135 else
1136 frame_append(curfrp, frp);
1137
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001138 /* Set w_fraction now so that the cursor keeps the same relative
1139 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001140 if (oldwin->w_height > 0)
1141 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001142 wp->w_fraction = oldwin->w_fraction;
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#ifdef FEAT_VERTSPLIT
1145 if (flags & WSP_VERT)
1146 {
1147 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (need_status)
1150 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001151 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 oldwin->w_status_height = need_status;
1153 }
1154 if (flags & (WSP_TOP | WSP_BOT))
1155 {
1156 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001157 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001158 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 wp->w_status_height = (p_ls > 0);
1160 }
1161 else
1162 {
1163 /* height and row of new window is same as current window */
1164 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001165 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_status_height = oldwin->w_status_height;
1167 }
1168 frp->fr_height = curfrp->fr_height;
1169
1170 /* "new_size" of the current window goes to the new window, use
1171 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001172 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 if (before)
1174 wp->w_vsep_width = 1;
1175 else
1176 {
1177 wp->w_vsep_width = oldwin->w_vsep_width;
1178 oldwin->w_vsep_width = 1;
1179 }
1180 if (flags & (WSP_TOP | WSP_BOT))
1181 {
1182 if (flags & WSP_BOT)
1183 frame_add_vsep(curfrp);
1184 /* Set width of neighbor frame */
1185 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001186 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1187 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001190 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (before) /* new window left of current one */
1192 {
1193 wp->w_wincol = oldwin->w_wincol;
1194 oldwin->w_wincol += new_size + 1;
1195 }
1196 else /* new window right of current one */
1197 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1198 frame_fix_width(oldwin);
1199 frame_fix_width(wp);
1200 }
1201 else
1202#endif
1203 {
1204 /* width and column of new window is same as current window */
1205#ifdef FEAT_VERTSPLIT
1206 if (flags & (WSP_TOP | WSP_BOT))
1207 {
1208 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001209 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 wp->w_vsep_width = 0;
1211 }
1212 else
1213 {
1214 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001215 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 wp->w_vsep_width = oldwin->w_vsep_width;
1217 }
1218 frp->fr_width = curfrp->fr_width;
1219#endif
1220
1221 /* "new_size" of the current window goes to the new window, use
1222 * one row for the status line */
1223 win_new_height(wp, new_size);
1224 if (flags & (WSP_TOP | WSP_BOT))
1225 frame_new_height(curfrp, curfrp->fr_height
1226 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1227 else
1228 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1229 if (before) /* new window above current one */
1230 {
1231 wp->w_winrow = oldwin->w_winrow;
1232 wp->w_status_height = STATUS_HEIGHT;
1233 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1234 }
1235 else /* new window below current one */
1236 {
1237 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1238 wp->w_status_height = oldwin->w_status_height;
1239 oldwin->w_status_height = STATUS_HEIGHT;
1240 }
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);
1247 }
1248
1249 if (flags & (WSP_TOP | WSP_BOT))
1250 (void)win_comp_pos();
1251
1252 /*
1253 * Both windows need redrawing
1254 */
1255 redraw_win_later(wp, NOT_VALID);
1256 wp->w_redr_status = TRUE;
1257 redraw_win_later(oldwin, NOT_VALID);
1258 oldwin->w_redr_status = TRUE;
1259
1260 if (need_status)
1261 {
1262 msg_row = Rows - 1;
1263 msg_col = sc_col;
1264 msg_clr_eos_force(); /* Old command/ruler may still be there */
1265 comp_col();
1266 msg_row = Rows - 1;
1267 msg_col = 0; /* put position back at start of line */
1268 }
1269
1270 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001271 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 */
1273 if (do_equal || dir != 0)
1274 win_equal(wp, TRUE,
1275#ifdef FEAT_VERTSPLIT
1276 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1277 : dir == 'h' ? 'b' :
1278#endif
1279 'v');
1280
1281 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1282 * size was given. */
1283#ifdef FEAT_VERTSPLIT
1284 if (flags & WSP_VERT)
1285 {
1286 i = p_wiw;
1287 if (size != 0)
1288 p_wiw = size;
1289
1290# ifdef FEAT_GUI
1291 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1292 if (gui.in_use)
1293 gui_init_which_components(NULL);
1294# endif
1295 }
1296 else
1297#endif
1298 {
1299 i = p_wh;
1300 if (size != 0)
1301 p_wh = size;
1302 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001303
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001304#ifdef FEAT_JUMPLIST
1305 /* Keep same changelist position in new window. */
1306 wp->w_changelistidx = oldwin->w_changelistidx;
1307#endif
1308
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001309 /*
1310 * make the new window the current window
1311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 win_enter(wp, FALSE);
1313#ifdef FEAT_VERTSPLIT
1314 if (flags & WSP_VERT)
1315 p_wiw = i;
1316 else
1317#endif
1318 p_wh = i;
1319
1320 return OK;
1321}
1322
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001323
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001324/*
1325 * Initialize window "newp" from window "oldp".
1326 * Used when splitting a window and when creating a new tab page.
1327 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001328 * WSP_NEWLOC may be specified in flags to prevent the location list from
1329 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001330 */
1331 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001332win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001333 win_T *newp;
1334 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001335 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001336{
1337 int i;
1338
1339 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001340#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001341 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001343 oldp->w_buffer->b_nwindows++;
1344 newp->w_cursor = oldp->w_cursor;
1345 newp->w_valid = 0;
1346 newp->w_curswant = oldp->w_curswant;
1347 newp->w_set_curswant = oldp->w_set_curswant;
1348 newp->w_topline = oldp->w_topline;
1349#ifdef FEAT_DIFF
1350 newp->w_topfill = oldp->w_topfill;
1351#endif
1352 newp->w_leftcol = oldp->w_leftcol;
1353 newp->w_pcmark = oldp->w_pcmark;
1354 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1355 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001356 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001357 newp->w_fraction = oldp->w_fraction;
1358 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1359#ifdef FEAT_JUMPLIST
1360 copy_jumplist(oldp, newp);
1361#endif
1362#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001363 if (flags & WSP_NEWLOC)
1364 {
1365 /* Don't copy the location list. */
1366 newp->w_llist = NULL;
1367 newp->w_llist_ref = NULL;
1368 }
1369 else
1370 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001371#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001372 newp->w_localdir = (oldp->w_localdir == NULL)
1373 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001374
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001375 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001376 for (i = 0; i < oldp->w_tagstacklen; i++)
1377 {
1378 newp->w_tagstack[i] = oldp->w_tagstack[i];
1379 if (newp->w_tagstack[i].tagname != NULL)
1380 newp->w_tagstack[i].tagname =
1381 vim_strsave(newp->w_tagstack[i].tagname);
1382 }
1383 newp->w_tagstackidx = oldp->w_tagstackidx;
1384 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001385#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001386 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001387#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001388
1389 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001390
Bram Moolenaara971b822011-09-14 14:43:25 +02001391#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001392 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001393#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001394}
1395
1396/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001397 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001398 * Only the essential things are copied.
1399 */
1400 static void
1401win_init_some(newp, oldp)
1402 win_T *newp;
1403 win_T *oldp;
1404{
1405 /* Use the same argument list. */
1406 newp->w_alist = oldp->w_alist;
1407 ++newp->w_alist->al_refcount;
1408 newp->w_arg_idx = oldp->w_arg_idx;
1409
1410 /* copy options from existing window */
1411 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#endif /* FEAT_WINDOWS */
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#if defined(FEAT_WINDOWS) || defined(PROTO)
1417/*
1418 * Check if "win" is a pointer to an existing window.
1419 */
1420 int
1421win_valid(win)
1422 win_T *win;
1423{
1424 win_T *wp;
1425
1426 if (win == NULL)
1427 return FALSE;
1428 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1429 if (wp == win)
1430 return TRUE;
1431 return FALSE;
1432}
1433
1434/*
1435 * Return the number of windows.
1436 */
1437 int
1438win_count()
1439{
1440 win_T *wp;
1441 int count = 0;
1442
1443 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1444 ++count;
1445 return count;
1446}
1447
1448/*
1449 * Make "count" windows on the screen.
1450 * Return actual number of windows on the screen.
1451 * Must be called when there is just one window, filling the whole screen
1452 * (excluding the command line).
1453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 int
1455make_windows(count, vertical)
1456 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001457 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 int maxcount;
1460 int todo;
1461
1462#ifdef FEAT_VERTSPLIT
1463 if (vertical)
1464 {
1465 /* Each windows needs at least 'winminwidth' lines and a separator
1466 * column. */
1467 maxcount = (curwin->w_width + curwin->w_vsep_width
1468 - (p_wiw - p_wmw)) / (p_wmw + 1);
1469 }
1470 else
1471#endif
1472 {
1473 /* Each window needs at least 'winminheight' lines and a status line. */
1474 maxcount = (curwin->w_height + curwin->w_status_height
1475 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1476 }
1477
1478 if (maxcount < 2)
1479 maxcount = 2;
1480 if (count > maxcount)
1481 count = maxcount;
1482
1483 /*
1484 * add status line now, otherwise first window will be too big
1485 */
1486 if (count > 1)
1487 last_status(TRUE);
1488
1489#ifdef FEAT_AUTOCMD
1490 /*
1491 * Don't execute autocommands while creating the windows. Must do that
1492 * when putting the buffers in the windows.
1493 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001494 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#endif
1496
1497 /* todo is number of windows left to create */
1498 for (todo = count - 1; todo > 0; --todo)
1499#ifdef FEAT_VERTSPLIT
1500 if (vertical)
1501 {
1502 if (win_split(curwin->w_width - (curwin->w_width - todo)
1503 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1504 break;
1505 }
1506 else
1507#endif
1508 {
1509 if (win_split(curwin->w_height - (curwin->w_height - todo
1510 * STATUS_HEIGHT) / (todo + 1)
1511 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1512 break;
1513 }
1514
1515#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001516 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#endif
1518
1519 /* return actual number of windows */
1520 return (count - todo);
1521}
1522
1523/*
1524 * Exchange current and next window
1525 */
1526 static void
1527win_exchange(Prenum)
1528 long Prenum;
1529{
1530 frame_T *frp;
1531 frame_T *frp2;
1532 win_T *wp;
1533 win_T *wp2;
1534 int temp;
1535
1536 if (lastwin == firstwin) /* just one window */
1537 {
1538 beep_flush();
1539 return;
1540 }
1541
1542#ifdef FEAT_GUI
1543 need_mouse_correct = TRUE;
1544#endif
1545
1546 /*
1547 * find window to exchange with
1548 */
1549 if (Prenum)
1550 {
1551 frp = curwin->w_frame->fr_parent->fr_child;
1552 while (frp != NULL && --Prenum > 0)
1553 frp = frp->fr_next;
1554 }
1555 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1556 frp = curwin->w_frame->fr_next;
1557 else /* Swap last window in row/col with previous */
1558 frp = curwin->w_frame->fr_prev;
1559
1560 /* We can only exchange a window with another window, not with a frame
1561 * containing windows. */
1562 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1563 return;
1564 wp = frp->fr_win;
1565
1566/*
1567 * 1. remove curwin from the list. Remember after which window it was in wp2
1568 * 2. insert curwin before wp in the list
1569 * if wp != wp2
1570 * 3. remove wp from the list
1571 * 4. insert wp after wp2
1572 * 5. exchange the status line height and vsep width.
1573 */
1574 wp2 = curwin->w_prev;
1575 frp2 = curwin->w_frame->fr_prev;
1576 if (wp->w_prev != curwin)
1577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001578 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 frame_remove(curwin->w_frame);
1580 win_append(wp->w_prev, curwin);
1581 frame_insert(frp, curwin->w_frame);
1582 }
1583 if (wp != wp2)
1584 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001585 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 frame_remove(wp->w_frame);
1587 win_append(wp2, wp);
1588 if (frp2 == NULL)
1589 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1590 else
1591 frame_append(frp2, wp->w_frame);
1592 }
1593 temp = curwin->w_status_height;
1594 curwin->w_status_height = wp->w_status_height;
1595 wp->w_status_height = temp;
1596#ifdef FEAT_VERTSPLIT
1597 temp = curwin->w_vsep_width;
1598 curwin->w_vsep_width = wp->w_vsep_width;
1599 wp->w_vsep_width = temp;
1600
1601 /* If the windows are not in the same frame, exchange the sizes to avoid
1602 * messing up the window layout. Otherwise fix the frame sizes. */
1603 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1604 {
1605 temp = curwin->w_height;
1606 curwin->w_height = wp->w_height;
1607 wp->w_height = temp;
1608 temp = curwin->w_width;
1609 curwin->w_width = wp->w_width;
1610 wp->w_width = temp;
1611 }
1612 else
1613 {
1614 frame_fix_height(curwin);
1615 frame_fix_height(wp);
1616 frame_fix_width(curwin);
1617 frame_fix_width(wp);
1618 }
1619#endif
1620
1621 (void)win_comp_pos(); /* recompute window positions */
1622
1623 win_enter(wp, TRUE);
1624 redraw_later(CLEAR);
1625}
1626
1627/*
1628 * rotate windows: if upwards TRUE the second window becomes the first one
1629 * if upwards FALSE the first window becomes the second one
1630 */
1631 static void
1632win_rotate(upwards, count)
1633 int upwards;
1634 int count;
1635{
1636 win_T *wp1;
1637 win_T *wp2;
1638 frame_T *frp;
1639 int n;
1640
1641 if (firstwin == lastwin) /* nothing to do */
1642 {
1643 beep_flush();
1644 return;
1645 }
1646
1647#ifdef FEAT_GUI
1648 need_mouse_correct = TRUE;
1649#endif
1650
1651#ifdef FEAT_VERTSPLIT
1652 /* Check if all frames in this row/col have one window. */
1653 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1654 frp = frp->fr_next)
1655 if (frp->fr_win == NULL)
1656 {
1657 EMSG(_("E443: Cannot rotate when another window is split"));
1658 return;
1659 }
1660#endif
1661
1662 while (count--)
1663 {
1664 if (upwards) /* first window becomes last window */
1665 {
1666 /* remove first window/frame from the list */
1667 frp = curwin->w_frame->fr_parent->fr_child;
1668 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001669 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 frame_remove(frp);
1671
1672 /* find last frame and append removed window/frame after it */
1673 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1674 ;
1675 win_append(frp->fr_win, wp1);
1676 frame_append(frp, wp1->w_frame);
1677
1678 wp2 = frp->fr_win; /* previously last window */
1679 }
1680 else /* last window becomes first window */
1681 {
1682 /* find last window/frame in the list and remove it */
1683 for (frp = curwin->w_frame; frp->fr_next != NULL;
1684 frp = frp->fr_next)
1685 ;
1686 wp1 = frp->fr_win;
1687 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001688 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 frame_remove(frp);
1690
1691 /* append the removed window/frame before the first in the list */
1692 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1693 frame_insert(frp->fr_parent->fr_child, frp);
1694 }
1695
1696 /* exchange status height and vsep width of old and new last window */
1697 n = wp2->w_status_height;
1698 wp2->w_status_height = wp1->w_status_height;
1699 wp1->w_status_height = n;
1700 frame_fix_height(wp1);
1701 frame_fix_height(wp2);
1702#ifdef FEAT_VERTSPLIT
1703 n = wp2->w_vsep_width;
1704 wp2->w_vsep_width = wp1->w_vsep_width;
1705 wp1->w_vsep_width = n;
1706 frame_fix_width(wp1);
1707 frame_fix_width(wp2);
1708#endif
1709
1710 /* recompute w_winrow and w_wincol for all windows */
1711 (void)win_comp_pos();
1712 }
1713
1714 redraw_later(CLEAR);
1715}
1716
1717/*
1718 * Move the current window to the very top/bottom/left/right of the screen.
1719 */
1720 static void
1721win_totop(size, flags)
1722 int size;
1723 int flags;
1724{
1725 int dir;
1726 int height = curwin->w_height;
1727
1728 if (lastwin == firstwin)
1729 {
1730 beep_flush();
1731 return;
1732 }
1733
1734 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001735 (void)winframe_remove(curwin, &dir, NULL);
1736 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 last_status(FALSE); /* may need to remove last status line */
1738 (void)win_comp_pos(); /* recompute window positions */
1739
1740 /* Split a window on the desired side and put the window there. */
1741 (void)win_split_ins(size, flags, curwin, dir);
1742 if (!(flags & WSP_VERT))
1743 {
1744 win_setheight(height);
1745 if (p_ea)
1746 win_equal(curwin, TRUE, 'v');
1747 }
1748
1749#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1750 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1751 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001752 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
1756/*
1757 * Move window "win1" to below/right of "win2" and make "win1" the current
1758 * window. Only works within the same frame!
1759 */
1760 void
1761win_move_after(win1, win2)
1762 win_T *win1, *win2;
1763{
1764 int height;
1765
1766 /* check if the arguments are reasonable */
1767 if (win1 == win2)
1768 return;
1769
1770 /* check if there is something to do */
1771 if (win2->w_next != win1)
1772 {
1773 /* may need move the status line/vertical separator of the last window
1774 * */
1775 if (win1 == lastwin)
1776 {
1777 height = win1->w_prev->w_status_height;
1778 win1->w_prev->w_status_height = win1->w_status_height;
1779 win1->w_status_height = height;
1780#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001781 if (win1->w_prev->w_vsep_width == 1)
1782 {
1783 /* Remove the vertical separator from the last-but-one window,
1784 * add it to the last window. Adjust the frame widths. */
1785 win1->w_prev->w_vsep_width = 0;
1786 win1->w_prev->w_frame->fr_width -= 1;
1787 win1->w_vsep_width = 1;
1788 win1->w_frame->fr_width += 1;
1789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#endif
1791 }
1792 else if (win2 == lastwin)
1793 {
1794 height = win1->w_status_height;
1795 win1->w_status_height = win2->w_status_height;
1796 win2->w_status_height = height;
1797#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001798 if (win1->w_vsep_width == 1)
1799 {
1800 /* Remove the vertical separator from win1, add it to the last
1801 * window, win2. Adjust the frame widths. */
1802 win2->w_vsep_width = 1;
1803 win2->w_frame->fr_width += 1;
1804 win1->w_vsep_width = 0;
1805 win1->w_frame->fr_width -= 1;
1806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001809 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 frame_remove(win1->w_frame);
1811 win_append(win2, win1);
1812 frame_append(win2->w_frame, win1->w_frame);
1813
1814 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1815 redraw_later(NOT_VALID);
1816 }
1817 win_enter(win1, FALSE);
1818}
1819
1820/*
1821 * Make all windows the same height.
1822 * 'next_curwin' will soon be the current window, make sure it has enough
1823 * rows.
1824 */
1825 void
1826win_equal(next_curwin, current, dir)
1827 win_T *next_curwin; /* pointer to current window to be or NULL */
1828 int current; /* do only frame with current window */
1829 int dir; /* 'v' for vertically, 'h' for horizontally,
1830 'b' for both, 0 for using p_ead */
1831{
1832 if (dir == 0)
1833#ifdef FEAT_VERTSPLIT
1834 dir = *p_ead;
1835#else
1836 dir = 'b';
1837#endif
1838 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001839 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001840 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
1843/*
1844 * Set a frame to a new position and height, spreading the available room
1845 * equally over contained frames.
1846 * The window "next_curwin" (if not NULL) should at least get the size from
1847 * 'winheight' and 'winwidth' if possible.
1848 */
1849 static void
1850win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1851 win_T *next_curwin; /* pointer to current window to be or NULL */
1852 int current; /* do only frame with current window */
1853 frame_T *topfr; /* frame to set size off */
1854 int dir; /* 'v', 'h' or 'b', see win_equal() */
1855 int col; /* horizontal position for frame */
1856 int row; /* vertical position for frame */
1857 int width; /* new width of frame */
1858 int height; /* new height of frame */
1859{
1860 int n, m;
1861 int extra_sep = 0;
1862 int wincount, totwincount = 0;
1863 frame_T *fr;
1864 int next_curwin_size = 0;
1865 int room = 0;
1866 int new_size;
1867 int has_next_curwin = 0;
1868 int hnc;
1869
1870 if (topfr->fr_layout == FR_LEAF)
1871 {
1872 /* Set the width/height of this frame.
1873 * Redraw when size or position changes */
1874 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1875#ifdef FEAT_VERTSPLIT
1876 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1877#endif
1878 )
1879 {
1880 topfr->fr_win->w_winrow = row;
1881 frame_new_height(topfr, height, FALSE, FALSE);
1882#ifdef FEAT_VERTSPLIT
1883 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001884 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#endif
1886 redraw_all_later(CLEAR);
1887 }
1888 }
1889#ifdef FEAT_VERTSPLIT
1890 else if (topfr->fr_layout == FR_ROW)
1891 {
1892 topfr->fr_width = width;
1893 topfr->fr_height = height;
1894
1895 if (dir != 'v') /* equalize frame widths */
1896 {
1897 /* Compute the maximum number of windows horizontally in this
1898 * frame. */
1899 n = frame_minwidth(topfr, NOWIN);
1900 /* add one for the rightmost window, it doesn't have a separator */
1901 if (col + width == Columns)
1902 extra_sep = 1;
1903 else
1904 extra_sep = 0;
1905 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001906 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001908 /*
1909 * Compute width for "next_curwin" window and room available for
1910 * other windows.
1911 * "m" is the minimal width when counting p_wiw for "next_curwin".
1912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 m = frame_minwidth(topfr, next_curwin);
1914 room = width - m;
1915 if (room < 0)
1916 {
1917 next_curwin_size = p_wiw + room;
1918 room = 0;
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
1921 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001922 next_curwin_size = -1;
1923 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1924 {
1925 /* If 'winfixwidth' set keep the window width if
1926 * possible.
1927 * Watch out for this window being the next_curwin. */
1928 if (frame_fixed_width(fr))
1929 {
1930 n = frame_minwidth(fr, NOWIN);
1931 new_size = fr->fr_width;
1932 if (frame_has_win(fr, next_curwin))
1933 {
1934 room += p_wiw - p_wmw;
1935 next_curwin_size = 0;
1936 if (new_size < p_wiw)
1937 new_size = p_wiw;
1938 }
1939 else
1940 /* These windows don't use up room. */
1941 totwincount -= (n + (fr->fr_next == NULL
1942 ? extra_sep : 0)) / (p_wmw + 1);
1943 room -= new_size - n;
1944 if (room < 0)
1945 {
1946 new_size += room;
1947 room = 0;
1948 }
1949 fr->fr_newwidth = new_size;
1950 }
1951 }
1952 if (next_curwin_size == -1)
1953 {
1954 if (!has_next_curwin)
1955 next_curwin_size = 0;
1956 else if (totwincount > 1
1957 && (room + (totwincount - 2))
1958 / (totwincount - 1) > p_wiw)
1959 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001960 /* Can make all windows wider than 'winwidth', spread
1961 * the room equally. */
1962 next_curwin_size = (room + p_wiw
1963 + (totwincount - 1) * p_wmw
1964 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001965 room -= next_curwin_size - p_wiw;
1966 }
1967 else
1968 next_curwin_size = p_wiw;
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001971
1972 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 --totwincount; /* don't count curwin */
1974 }
1975
1976 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1977 {
1978 n = m = 0;
1979 wincount = 1;
1980 if (fr->fr_next == NULL)
1981 /* last frame gets all that remains (avoid roundoff error) */
1982 new_size = width;
1983 else if (dir == 'v')
1984 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001985 else if (frame_fixed_width(fr))
1986 {
1987 new_size = fr->fr_newwidth;
1988 wincount = 0; /* doesn't count as a sizeable window */
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991 {
1992 /* Compute the maximum number of windows horiz. in "fr". */
1993 n = frame_minwidth(fr, NOWIN);
1994 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1995 / (p_wmw + 1);
1996 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001997 if (has_next_curwin)
1998 hnc = frame_has_win(fr, next_curwin);
1999 else
2000 hnc = FALSE;
2001 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002003 if (totwincount == 0)
2004 new_size = room;
2005 else
2006 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002008 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 next_curwin_size -= p_wiw - (m - n);
2011 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002012 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002014 else
2015 room -= new_size;
2016 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002019 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 * window, unless equalizing all frames. */
2021 if (!current || dir != 'v' || topfr->fr_parent != NULL
2022 || (new_size != fr->fr_width)
2023 || frame_has_win(fr, next_curwin))
2024 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002025 new_size, height);
2026 col += new_size;
2027 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 totwincount -= wincount;
2029 }
2030 }
2031#endif
2032 else /* topfr->fr_layout == FR_COL */
2033 {
2034#ifdef FEAT_VERTSPLIT
2035 topfr->fr_width = width;
2036#endif
2037 topfr->fr_height = height;
2038
2039 if (dir != 'h') /* equalize frame heights */
2040 {
2041 /* Compute maximum number of windows vertically in this frame. */
2042 n = frame_minheight(topfr, NOWIN);
2043 /* add one for the bottom window if it doesn't have a statusline */
2044 if (row + height == cmdline_row && p_ls == 0)
2045 extra_sep = 1;
2046 else
2047 extra_sep = 0;
2048 totwincount = (n + extra_sep) / (p_wmh + 1);
2049 has_next_curwin = frame_has_win(topfr, next_curwin);
2050
2051 /*
2052 * Compute height for "next_curwin" window and room available for
2053 * other windows.
2054 * "m" is the minimal height when counting p_wh for "next_curwin".
2055 */
2056 m = frame_minheight(topfr, next_curwin);
2057 room = height - m;
2058 if (room < 0)
2059 {
2060 /* The room is less then 'winheight', use all space for the
2061 * current window. */
2062 next_curwin_size = p_wh + room;
2063 room = 0;
2064 }
2065 else
2066 {
2067 next_curwin_size = -1;
2068 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2069 {
2070 /* If 'winfixheight' set keep the window height if
2071 * possible.
2072 * Watch out for this window being the next_curwin. */
2073 if (frame_fixed_height(fr))
2074 {
2075 n = frame_minheight(fr, NOWIN);
2076 new_size = fr->fr_height;
2077 if (frame_has_win(fr, next_curwin))
2078 {
2079 room += p_wh - p_wmh;
2080 next_curwin_size = 0;
2081 if (new_size < p_wh)
2082 new_size = p_wh;
2083 }
2084 else
2085 /* These windows don't use up room. */
2086 totwincount -= (n + (fr->fr_next == NULL
2087 ? extra_sep : 0)) / (p_wmh + 1);
2088 room -= new_size - n;
2089 if (room < 0)
2090 {
2091 new_size += room;
2092 room = 0;
2093 }
2094 fr->fr_newheight = new_size;
2095 }
2096 }
2097 if (next_curwin_size == -1)
2098 {
2099 if (!has_next_curwin)
2100 next_curwin_size = 0;
2101 else if (totwincount > 1
2102 && (room + (totwincount - 2))
2103 / (totwincount - 1) > p_wh)
2104 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002105 /* can make all windows higher than 'winheight',
2106 * spread the room equally. */
2107 next_curwin_size = (room + p_wh
2108 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 + (totwincount - 1)) / totwincount;
2110 room -= next_curwin_size - p_wh;
2111 }
2112 else
2113 next_curwin_size = p_wh;
2114 }
2115 }
2116
2117 if (has_next_curwin)
2118 --totwincount; /* don't count curwin */
2119 }
2120
2121 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2122 {
2123 n = m = 0;
2124 wincount = 1;
2125 if (fr->fr_next == NULL)
2126 /* last frame gets all that remains (avoid roundoff error) */
2127 new_size = height;
2128 else if (dir == 'h')
2129 new_size = fr->fr_height;
2130 else if (frame_fixed_height(fr))
2131 {
2132 new_size = fr->fr_newheight;
2133 wincount = 0; /* doesn't count as a sizeable window */
2134 }
2135 else
2136 {
2137 /* Compute the maximum number of windows vert. in "fr". */
2138 n = frame_minheight(fr, NOWIN);
2139 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2140 / (p_wmh + 1);
2141 m = frame_minheight(fr, next_curwin);
2142 if (has_next_curwin)
2143 hnc = frame_has_win(fr, next_curwin);
2144 else
2145 hnc = FALSE;
2146 if (hnc) /* don't count next_curwin */
2147 --wincount;
2148 if (totwincount == 0)
2149 new_size = room;
2150 else
2151 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2152 / totwincount;
2153 if (hnc) /* add next_curwin size */
2154 {
2155 next_curwin_size -= p_wh - (m - n);
2156 new_size += next_curwin_size;
2157 room -= new_size - next_curwin_size;
2158 }
2159 else
2160 room -= new_size;
2161 new_size += n;
2162 }
2163 /* Skip frame that is full width when splitting or closing a
2164 * window, unless equalizing all frames. */
2165 if (!current || dir != 'h' || topfr->fr_parent != NULL
2166 || (new_size != fr->fr_height)
2167 || frame_has_win(fr, next_curwin))
2168 win_equal_rec(next_curwin, current, fr, dir, col, row,
2169 width, new_size);
2170 row += new_size;
2171 height -= new_size;
2172 totwincount -= wincount;
2173 }
2174 }
2175}
2176
2177/*
2178 * close all windows for buffer 'buf'
2179 */
2180 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002181close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002183 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002185 win_T *wp;
2186 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002187 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002190
2191 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002193 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2194#ifdef FEAT_AUTOCMD
2195 && !(wp->w_closing || wp->w_buffer->b_closing)
2196#endif
2197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002199 win_close(wp, FALSE);
2200
2201 /* Start all over, autocommands may change the window layout. */
2202 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
2204 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002205 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002207
2208 /* Also check windows in other tab pages. */
2209 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2210 {
2211 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002212 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002213 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002214 if (wp->w_buffer == buf
2215#ifdef FEAT_AUTOCMD
2216 && !(wp->w_closing || wp->w_buffer->b_closing)
2217#endif
2218 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002219 {
2220 win_close_othertab(wp, FALSE, tp);
2221
2222 /* Start all over, the tab page may be closed and
2223 * autocommands may change the window layout. */
2224 nexttp = first_tabpage;
2225 break;
2226 }
2227 }
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002230
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002231 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002232 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002233 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234}
2235
2236/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002237 * Return TRUE if the current window is the only window that exists (ignoring
2238 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002239 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002240 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002241 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002242last_window()
2243{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002244 return (one_window() && first_tabpage->tp_next == NULL);
2245}
2246
2247/*
2248 * Return TRUE if there is only one window other than "aucmd_win" in the
2249 * current tab page.
2250 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002251 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002252one_window()
2253{
2254#ifdef FEAT_AUTOCMD
2255 win_T *wp;
2256 int seen_one = FALSE;
2257
2258 FOR_ALL_WINDOWS(wp)
2259 {
2260 if (wp != aucmd_win)
2261 {
2262 if (seen_one)
2263 return FALSE;
2264 seen_one = TRUE;
2265 }
2266 }
2267 return TRUE;
2268#else
2269 return firstwin == lastwin;
2270#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002271}
2272
2273/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002274 * Close the possibly last window in a tab page.
2275 * Returns TRUE when the window was closed already.
2276 */
2277 static int
2278close_last_window_tabpage(win, free_buf, prev_curtab)
2279 win_T *win;
2280 int free_buf;
2281 tabpage_T *prev_curtab;
2282{
2283 if (firstwin == lastwin)
2284 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002285#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002286 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002287#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002288
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002289 /*
2290 * Closing the last window in a tab page. First go to another tab
2291 * page and then close the window and the tab page. This avoids that
2292 * curwin and curtab are invalid while we are freeing memory, they may
2293 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002294 * Don't trigger autocommands yet, they may use wrong values, so do
2295 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002296 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002297 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002298 redraw_tabline = TRUE;
2299
2300 /* Safety check: Autocommands may have closed the window when jumping
2301 * to the other tab page. */
2302 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2303 {
2304 int h = tabline_height();
2305
2306 win_close_othertab(win, free_buf, prev_curtab);
2307 if (h != tabline_height())
2308 shell_new_rows();
2309 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002310 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2311 * that now. */
2312#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002313 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002314 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2315 if (old_curbuf != curbuf)
2316 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002317#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002318 return TRUE;
2319 }
2320 return FALSE;
2321}
2322
2323/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002324 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * If "free_buf" is TRUE related buffer may be unloaded.
2326 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002327 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002328 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002330 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331win_close(win, free_buf)
2332 win_T *win;
2333 int free_buf;
2334{
2335 win_T *wp;
2336#ifdef FEAT_AUTOCMD
2337 int other_buffer = FALSE;
2338#endif
2339 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 int dir;
2341 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002342 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002344 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
2349
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002350#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002351 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002352 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002353 if (win == aucmd_win)
2354 {
2355 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002356 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002357 }
2358 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2359 {
2360 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002361 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002362 }
2363#endif
2364
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002365 /* When closing the last window in a tab page first go to another tab page
2366 * and then close the window and the tab page to avoid that curwin and
2367 * curtab are invalid while we are freeing memory. */
2368 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002369 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /* When closing the help window, try restoring a snapshot after closing
2372 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002373 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 help_window = TRUE;
2375 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002376 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifdef FEAT_AUTOCMD
2379 if (win == curwin)
2380 {
2381 /*
2382 * Guess which window is going to be the new current window.
2383 * This may change because of the autocommands (sigh).
2384 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002385 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002388 * Be careful: If autocommands delete the window or cause this window
2389 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 */
2391 if (wp->w_buffer != curbuf)
2392 {
2393 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002394 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002396 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002397 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002398 win->w_closing = FALSE;
2399 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002402 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002404 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002405 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002406 win->w_closing = FALSE;
2407 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409# ifdef FEAT_EVAL
2410 /* autocmds may abort script processing */
2411 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413# endif
2414 }
2415#endif
2416
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002417#ifdef FEAT_GUI
2418 /* Avoid trouble with scrollbars that are going to be deleted in
2419 * win_free(). */
2420 if (gui.in_use)
2421 out_flush();
2422#endif
2423
Bram Moolenaara971b822011-09-14 14:43:25 +02002424#ifdef FEAT_SYN_HL
2425 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002426 if (win->w_buffer != NULL)
2427 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002428#endif
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 /*
2431 * Close the link to the buffer.
2432 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002433 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002434 {
2435#ifdef FEAT_AUTOCMD
2436 win->w_closing = TRUE;
2437#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002438 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002439#ifdef FEAT_AUTOCMD
2440 if (win_valid(win))
2441 win->w_closing = FALSE;
2442#endif
2443 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002444
Bram Moolenaar802418d2013-01-17 14:00:11 +01002445 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2446 && (last_window() || curtab != prev_curtab
2447 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002448 {
2449 /* Autocommands have close all windows, quit now. Restore
2450 * curwin->w_buffer, otherwise writing viminfo may fail. */
2451 if (curwin->w_buffer == NULL)
2452 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002453 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002454 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002457 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002458 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002459 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
Bram Moolenaara971b822011-09-14 14:43:25 +02002462 /* Free the memory used for the window and get the window that received
2463 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002464 wp = win_free_mem(win, &dir, NULL);
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 /* Make sure curwin isn't invalid. It can cause severe trouble when
2467 * printing an error message. For win_equal() curbuf needs to be valid
2468 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002469 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 curwin = wp;
2472#ifdef FEAT_QUICKFIX
2473 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2474 {
2475 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002476 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 * finding another window to go to.
2478 */
2479 for (;;)
2480 {
2481 if (wp->w_next == NULL)
2482 wp = firstwin;
2483 else
2484 wp = wp->w_next;
2485 if (wp == curwin)
2486 break;
2487 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2488 {
2489 curwin = wp;
2490 break;
2491 }
2492 }
2493 }
2494#endif
2495 curbuf = curwin->w_buffer;
2496 close_curwin = TRUE;
2497 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002498 if (p_ea
2499#ifdef FEAT_VERTSPLIT
2500 && (*p_ead == 'b' || *p_ead == dir)
2501#endif
2502 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 win_equal(curwin, TRUE,
2504#ifdef FEAT_VERTSPLIT
2505 dir
2506#else
2507 0
2508#endif
2509 );
2510 else
2511 win_comp_pos();
2512 if (close_curwin)
2513 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002514 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#ifdef FEAT_AUTOCMD
2516 if (other_buffer)
2517 /* careful: after this wp and win may be invalid! */
2518 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2519#endif
2520 }
2521
2522 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002523 * If last window has a status line now and we don't want one,
2524 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 */
2526 last_status(FALSE);
2527
2528 /* After closing the help window, try restoring the window layout from
2529 * before it was opened. */
2530 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002531 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2534 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2535 if (gui.in_use && !win_hasvertsplit())
2536 gui_init_which_components(NULL);
2537#endif
2538
2539 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002540 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541}
2542
2543/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002544 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002545 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002546 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002547 * Caller must check if buffer is hidden and whether the tabline needs to be
2548 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002549 */
2550 void
2551win_close_othertab(win, free_buf, tp)
2552 win_T *win;
2553 int free_buf;
2554 tabpage_T *tp;
2555{
2556 win_T *wp;
2557 int dir;
2558 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002559 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002560
Bram Moolenaar362ce482012-06-06 19:02:45 +02002561#ifdef FEAT_AUTOCMD
2562 if (win->w_closing || win->w_buffer->b_closing)
2563 return; /* window is already being closed */
2564#endif
2565
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002567 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568
2569 /* Careful: Autocommands may have closed the tab page or made it the
2570 * current tab page. */
2571 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2572 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002573 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574 return;
2575
2576 /* Autocommands may have closed the window already. */
2577 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2578 ;
2579 if (wp == NULL)
2580 return;
2581
Bram Moolenaarf740b292006-02-16 22:11:02 +00002582 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002583 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002584 {
2585 if (tp == first_tabpage)
2586 first_tabpage = tp->tp_next;
2587 else
2588 {
2589 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2590 ptp = ptp->tp_next)
2591 ;
2592 if (ptp == NULL)
2593 {
2594 EMSG2(_(e_intern2), "win_close_othertab()");
2595 return;
2596 }
2597 ptp->tp_next = tp->tp_next;
2598 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002599 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002600 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002601
2602 /* Free the memory used for the window. */
2603 win_free_mem(win, &dir, tp);
2604
2605 if (free_tp)
2606 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002607}
2608
2609/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002610 * Free the memory used for a window.
2611 * Returns a pointer to the window that got the freed up space.
2612 */
2613 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002615 win_T *win;
2616 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002618{
2619 frame_T *frp;
2620 win_T *wp;
2621
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002622 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002623 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002624 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002625 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002627
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002628 /* When deleting the current window of another tab page select a new
2629 * current window. */
2630 if (tp != NULL && win == tp->tp_curwin)
2631 tp->tp_curwin = wp;
2632
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002633 return wp;
2634}
2635
2636#if defined(EXITFREE) || defined(PROTO)
2637 void
2638win_free_all()
2639{
2640 int dummy;
2641
Bram Moolenaarf740b292006-02-16 22:11:02 +00002642# ifdef FEAT_WINDOWS
2643 while (first_tabpage->tp_next != NULL)
2644 tabpage_close(TRUE);
2645# endif
2646
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002647# ifdef FEAT_AUTOCMD
2648 if (aucmd_win != NULL)
2649 {
2650 (void)win_free_mem(aucmd_win, &dummy, NULL);
2651 aucmd_win = NULL;
2652 }
2653# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002654
2655 while (firstwin != NULL)
2656 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002657
2658 /* No window should be used after this. Set curwin to NULL to crash
2659 * instead of using freed memory. */
2660 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002661}
2662#endif
2663
2664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 * Remove a window and its frame from the tree of frames.
2666 * Returns a pointer to the window that got the freed up space.
2667 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002668 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002669winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002671 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 frame_T *frp, *frp2, *frp3;
2675 frame_T *frp_close = win->w_frame;
2676 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677
2678 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002679 * If there is only one window there is nothing to remove.
2680 */
2681 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2682 return NULL;
2683
2684 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 * Remove the window from its frame.
2686 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002687 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 wp = frame2win(frp2);
2689
2690 /* Remove this frame from the list of frames. */
2691 frame_remove(frp_close);
2692
2693#ifdef FEAT_VERTSPLIT
2694 if (frp_close->fr_parent->fr_layout == FR_COL)
2695 {
2696#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002697 /* When 'winfixheight' is set, try to find another frame in the column
2698 * (as close to the closed frame as possible) to distribute the height
2699 * to. */
2700 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2701 {
2702 frp = frp_close->fr_prev;
2703 frp3 = frp_close->fr_next;
2704 while (frp != NULL || frp3 != NULL)
2705 {
2706 if (frp != NULL)
2707 {
2708 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2709 {
2710 frp2 = frp;
2711 wp = frp->fr_win;
2712 break;
2713 }
2714 frp = frp->fr_prev;
2715 }
2716 if (frp3 != NULL)
2717 {
2718 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2719 {
2720 frp2 = frp3;
2721 wp = frp3->fr_win;
2722 break;
2723 }
2724 frp3 = frp3->fr_next;
2725 }
2726 }
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2729 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#ifdef FEAT_VERTSPLIT
2731 *dirp = 'v';
2732 }
2733 else
2734 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002735 /* When 'winfixwidth' is set, try to find another frame in the column
2736 * (as close to the closed frame as possible) to distribute the width
2737 * to. */
2738 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2739 {
2740 frp = frp_close->fr_prev;
2741 frp3 = frp_close->fr_next;
2742 while (frp != NULL || frp3 != NULL)
2743 {
2744 if (frp != NULL)
2745 {
2746 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2747 {
2748 frp2 = frp;
2749 wp = frp->fr_win;
2750 break;
2751 }
2752 frp = frp->fr_prev;
2753 }
2754 if (frp3 != NULL)
2755 {
2756 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2757 {
2758 frp2 = frp3;
2759 wp = frp3->fr_win;
2760 break;
2761 }
2762 frp3 = frp3->fr_next;
2763 }
2764 }
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002767 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 *dirp = 'h';
2769 }
2770#endif
2771
2772 /* If rows/columns go to a window below/right its positions need to be
2773 * updated. Can only be done after the sizes have been updated. */
2774 if (frp2 == frp_close->fr_next)
2775 {
2776 int row = win->w_winrow;
2777 int col = W_WINCOL(win);
2778
2779 frame_comp_pos(frp2, &row, &col);
2780 }
2781
2782 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2783 {
2784 /* There is no other frame in this list, move its info to the parent
2785 * and remove it. */
2786 frp2->fr_parent->fr_layout = frp2->fr_layout;
2787 frp2->fr_parent->fr_child = frp2->fr_child;
2788 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2789 frp->fr_parent = frp2->fr_parent;
2790 frp2->fr_parent->fr_win = frp2->fr_win;
2791 if (frp2->fr_win != NULL)
2792 frp2->fr_win->w_frame = frp2->fr_parent;
2793 frp = frp2->fr_parent;
2794 vim_free(frp2);
2795
2796 frp2 = frp->fr_parent;
2797 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2798 {
2799 /* The frame above the parent has the same layout, have to merge
2800 * the frames into this list. */
2801 if (frp2->fr_child == frp)
2802 frp2->fr_child = frp->fr_child;
2803 frp->fr_child->fr_prev = frp->fr_prev;
2804 if (frp->fr_prev != NULL)
2805 frp->fr_prev->fr_next = frp->fr_child;
2806 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2807 {
2808 frp3->fr_parent = frp2;
2809 if (frp3->fr_next == NULL)
2810 {
2811 frp3->fr_next = frp->fr_next;
2812 if (frp->fr_next != NULL)
2813 frp->fr_next->fr_prev = frp3;
2814 break;
2815 }
2816 }
2817 vim_free(frp);
2818 }
2819 }
2820
2821 return wp;
2822}
2823
2824/*
2825 * Find out which frame is going to get the freed up space when "win" is
2826 * closed.
2827 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2828 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2829 * This makes opening a window and closing it immediately keep the same window
2830 * layout.
2831 */
2832 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002833win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002835 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 frame_T *frp;
2838 int b;
2839
Bram Moolenaarf740b292006-02-16 22:11:02 +00002840 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002841 /* Last window in this tab page, will go to next tab page. */
2842 return alt_tabpage()->tp_curwin->w_frame;
2843
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 frp = win->w_frame;
2845#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002846 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 b = p_spr;
2848 else
2849#endif
2850 b = p_sb;
2851 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2852 return frp->fr_next;
2853 return frp->fr_prev;
2854}
2855
2856/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002857 * Return the tabpage that will be used if the current one is closed.
2858 */
2859 static tabpage_T *
2860alt_tabpage()
2861{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002862 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002863
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002864 /* Use the next tab page if possible. */
2865 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002866 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002867
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002868 /* Find the last but one tab page. */
2869 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2870 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002871 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002872}
2873
2874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 * Find the left-upper window in frame "frp".
2876 */
2877 static win_T *
2878frame2win(frp)
2879 frame_T *frp;
2880{
2881 while (frp->fr_win == NULL)
2882 frp = frp->fr_child;
2883 return frp->fr_win;
2884}
2885
2886/*
2887 * Return TRUE if frame "frp" contains window "wp".
2888 */
2889 static int
2890frame_has_win(frp, wp)
2891 frame_T *frp;
2892 win_T *wp;
2893{
2894 frame_T *p;
2895
2896 if (frp->fr_layout == FR_LEAF)
2897 return frp->fr_win == wp;
2898
2899 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2900 if (frame_has_win(p, wp))
2901 return TRUE;
2902 return FALSE;
2903}
2904
2905/*
2906 * Set a new height for a frame. Recursively sets the height for contained
2907 * frames and windows. Caller must take care of positions.
2908 */
2909 static void
2910frame_new_height(topfrp, height, topfirst, wfh)
2911 frame_T *topfrp;
2912 int height;
2913 int topfirst; /* resize topmost contained frame first */
2914 int wfh; /* obey 'winfixheight' when there is a choice;
2915 may cause the height not to be set */
2916{
2917 frame_T *frp;
2918 int extra_lines;
2919 int h;
2920
2921 if (topfrp->fr_win != NULL)
2922 {
2923 /* Simple case: just one window. */
2924 win_new_height(topfrp->fr_win,
2925 height - topfrp->fr_win->w_status_height);
2926 }
2927#ifdef FEAT_VERTSPLIT
2928 else if (topfrp->fr_layout == FR_ROW)
2929 {
2930 do
2931 {
2932 /* All frames in this row get the same new height. */
2933 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2934 {
2935 frame_new_height(frp, height, topfirst, wfh);
2936 if (frp->fr_height > height)
2937 {
2938 /* Could not fit the windows, make the whole row higher. */
2939 height = frp->fr_height;
2940 break;
2941 }
2942 }
2943 }
2944 while (frp != NULL);
2945 }
2946#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002947 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /* Complicated case: Resize a column of frames. Resize the bottom
2950 * frame first, frames above that when needed. */
2951
2952 frp = topfrp->fr_child;
2953 if (wfh)
2954 /* Advance past frames with one window with 'wfh' set. */
2955 while (frame_fixed_height(frp))
2956 {
2957 frp = frp->fr_next;
2958 if (frp == NULL)
2959 return; /* no frame without 'wfh', give up */
2960 }
2961 if (!topfirst)
2962 {
2963 /* Find the bottom frame of this column */
2964 while (frp->fr_next != NULL)
2965 frp = frp->fr_next;
2966 if (wfh)
2967 /* Advance back for frames with one window with 'wfh' set. */
2968 while (frame_fixed_height(frp))
2969 frp = frp->fr_prev;
2970 }
2971
2972 extra_lines = height - topfrp->fr_height;
2973 if (extra_lines < 0)
2974 {
2975 /* reduce height of contained frames, bottom or top frame first */
2976 while (frp != NULL)
2977 {
2978 h = frame_minheight(frp, NULL);
2979 if (frp->fr_height + extra_lines < h)
2980 {
2981 extra_lines += frp->fr_height - h;
2982 frame_new_height(frp, h, topfirst, wfh);
2983 }
2984 else
2985 {
2986 frame_new_height(frp, frp->fr_height + extra_lines,
2987 topfirst, wfh);
2988 break;
2989 }
2990 if (topfirst)
2991 {
2992 do
2993 frp = frp->fr_next;
2994 while (wfh && frp != NULL && frame_fixed_height(frp));
2995 }
2996 else
2997 {
2998 do
2999 frp = frp->fr_prev;
3000 while (wfh && frp != NULL && frame_fixed_height(frp));
3001 }
3002 /* Increase "height" if we could not reduce enough frames. */
3003 if (frp == NULL)
3004 height -= extra_lines;
3005 }
3006 }
3007 else if (extra_lines > 0)
3008 {
3009 /* increase height of bottom or top frame */
3010 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3011 }
3012 }
3013 topfrp->fr_height = height;
3014}
3015
3016/*
3017 * Return TRUE if height of frame "frp" should not be changed because of
3018 * the 'winfixheight' option.
3019 */
3020 static int
3021frame_fixed_height(frp)
3022 frame_T *frp;
3023{
3024 /* frame with one window: fixed height if 'winfixheight' set. */
3025 if (frp->fr_win != NULL)
3026 return frp->fr_win->w_p_wfh;
3027
3028 if (frp->fr_layout == FR_ROW)
3029 {
3030 /* The frame is fixed height if one of the frames in the row is fixed
3031 * height. */
3032 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3033 if (frame_fixed_height(frp))
3034 return TRUE;
3035 return FALSE;
3036 }
3037
3038 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3039 * frames in the row are fixed height. */
3040 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3041 if (!frame_fixed_height(frp))
3042 return FALSE;
3043 return TRUE;
3044}
3045
3046#ifdef FEAT_VERTSPLIT
3047/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003048 * Return TRUE if width of frame "frp" should not be changed because of
3049 * the 'winfixwidth' option.
3050 */
3051 static int
3052frame_fixed_width(frp)
3053 frame_T *frp;
3054{
3055 /* frame with one window: fixed width if 'winfixwidth' set. */
3056 if (frp->fr_win != NULL)
3057 return frp->fr_win->w_p_wfw;
3058
3059 if (frp->fr_layout == FR_COL)
3060 {
3061 /* The frame is fixed width if one of the frames in the row is fixed
3062 * width. */
3063 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3064 if (frame_fixed_width(frp))
3065 return TRUE;
3066 return FALSE;
3067 }
3068
3069 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3070 * frames in the row are fixed width. */
3071 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3072 if (!frame_fixed_width(frp))
3073 return FALSE;
3074 return TRUE;
3075}
3076
3077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * Add a status line to windows at the bottom of "frp".
3079 * Note: Does not check if there is room!
3080 */
3081 static void
3082frame_add_statusline(frp)
3083 frame_T *frp;
3084{
3085 win_T *wp;
3086
3087 if (frp->fr_layout == FR_LEAF)
3088 {
3089 wp = frp->fr_win;
3090 if (wp->w_status_height == 0)
3091 {
3092 if (wp->w_height > 0) /* don't make it negative */
3093 --wp->w_height;
3094 wp->w_status_height = STATUS_HEIGHT;
3095 }
3096 }
3097 else if (frp->fr_layout == FR_ROW)
3098 {
3099 /* Handle all the frames in the row. */
3100 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3101 frame_add_statusline(frp);
3102 }
3103 else /* frp->fr_layout == FR_COL */
3104 {
3105 /* Only need to handle the last frame in the column. */
3106 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3107 ;
3108 frame_add_statusline(frp);
3109 }
3110}
3111
3112/*
3113 * Set width of a frame. Handles recursively going through contained frames.
3114 * May remove separator line for windows at the right side (for win_close()).
3115 */
3116 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003117frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 frame_T *topfrp;
3119 int width;
3120 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003121 int wfw; /* obey 'winfixwidth' when there is a choice;
3122 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 frame_T *frp;
3125 int extra_cols;
3126 int w;
3127 win_T *wp;
3128
3129 if (topfrp->fr_layout == FR_LEAF)
3130 {
3131 /* Simple case: just one window. */
3132 wp = topfrp->fr_win;
3133 /* Find out if there are any windows right of this one. */
3134 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3135 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3136 break;
3137 if (frp->fr_parent == NULL)
3138 wp->w_vsep_width = 0;
3139 win_new_width(wp, width - wp->w_vsep_width);
3140 }
3141 else if (topfrp->fr_layout == FR_COL)
3142 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003143 do
3144 {
3145 /* All frames in this column get the same new width. */
3146 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3147 {
3148 frame_new_width(frp, width, leftfirst, wfw);
3149 if (frp->fr_width > width)
3150 {
3151 /* Could not fit the windows, make whole column wider. */
3152 width = frp->fr_width;
3153 break;
3154 }
3155 }
3156 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 }
3158 else /* fr_layout == FR_ROW */
3159 {
3160 /* Complicated case: Resize a row of frames. Resize the rightmost
3161 * frame first, frames left of it when needed. */
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003164 if (wfw)
3165 /* Advance past frames with one window with 'wfw' set. */
3166 while (frame_fixed_width(frp))
3167 {
3168 frp = frp->fr_next;
3169 if (frp == NULL)
3170 return; /* no frame without 'wfw', give up */
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003173 {
3174 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 while (frp->fr_next != NULL)
3176 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003177 if (wfw)
3178 /* Advance back for frames with one window with 'wfw' set. */
3179 while (frame_fixed_width(frp))
3180 frp = frp->fr_prev;
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 extra_cols = width - topfrp->fr_width;
3184 if (extra_cols < 0)
3185 {
3186 /* reduce frame width, rightmost frame first */
3187 while (frp != NULL)
3188 {
3189 w = frame_minwidth(frp, NULL);
3190 if (frp->fr_width + extra_cols < w)
3191 {
3192 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003193 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 else
3196 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003197 frame_new_width(frp, frp->fr_width + extra_cols,
3198 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 break;
3200 }
3201 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003202 {
3203 do
3204 frp = frp->fr_next;
3205 while (wfw && frp != NULL && frame_fixed_width(frp));
3206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003208 {
3209 do
3210 frp = frp->fr_prev;
3211 while (wfw && frp != NULL && frame_fixed_width(frp));
3212 }
3213 /* Increase "width" if we could not reduce enough frames. */
3214 if (frp == NULL)
3215 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217 }
3218 else if (extra_cols > 0)
3219 {
3220 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003221 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 }
3224 topfrp->fr_width = width;
3225}
3226
3227/*
3228 * Add the vertical separator to windows at the right side of "frp".
3229 * Note: Does not check if there is room!
3230 */
3231 static void
3232frame_add_vsep(frp)
3233 frame_T *frp;
3234{
3235 win_T *wp;
3236
3237 if (frp->fr_layout == FR_LEAF)
3238 {
3239 wp = frp->fr_win;
3240 if (wp->w_vsep_width == 0)
3241 {
3242 if (wp->w_width > 0) /* don't make it negative */
3243 --wp->w_width;
3244 wp->w_vsep_width = 1;
3245 }
3246 }
3247 else if (frp->fr_layout == FR_COL)
3248 {
3249 /* Handle all the frames in the column. */
3250 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3251 frame_add_vsep(frp);
3252 }
3253 else /* frp->fr_layout == FR_ROW */
3254 {
3255 /* Only need to handle the last frame in the row. */
3256 frp = frp->fr_child;
3257 while (frp->fr_next != NULL)
3258 frp = frp->fr_next;
3259 frame_add_vsep(frp);
3260 }
3261}
3262
3263/*
3264 * Set frame width from the window it contains.
3265 */
3266 static void
3267frame_fix_width(wp)
3268 win_T *wp;
3269{
3270 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3271}
3272#endif
3273
3274/*
3275 * Set frame height from the window it contains.
3276 */
3277 static void
3278frame_fix_height(wp)
3279 win_T *wp;
3280{
3281 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3282}
3283
3284/*
3285 * Compute the minimal height for frame "topfrp".
3286 * Uses the 'winminheight' option.
3287 * When "next_curwin" isn't NULL, use p_wh for this window.
3288 * When "next_curwin" is NOWIN, don't use at least one line for the current
3289 * window.
3290 */
3291 static int
3292frame_minheight(topfrp, next_curwin)
3293 frame_T *topfrp;
3294 win_T *next_curwin;
3295{
3296 frame_T *frp;
3297 int m;
3298#ifdef FEAT_VERTSPLIT
3299 int n;
3300#endif
3301
3302 if (topfrp->fr_win != NULL)
3303 {
3304 if (topfrp->fr_win == next_curwin)
3305 m = p_wh + topfrp->fr_win->w_status_height;
3306 else
3307 {
3308 /* window: minimal height of the window plus status line */
3309 m = p_wmh + topfrp->fr_win->w_status_height;
3310 /* Current window is minimal one line high */
3311 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3312 ++m;
3313 }
3314 }
3315#ifdef FEAT_VERTSPLIT
3316 else if (topfrp->fr_layout == FR_ROW)
3317 {
3318 /* get the minimal height from each frame in this row */
3319 m = 0;
3320 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3321 {
3322 n = frame_minheight(frp, next_curwin);
3323 if (n > m)
3324 m = n;
3325 }
3326 }
3327#endif
3328 else
3329 {
3330 /* Add up the minimal heights for all frames in this column. */
3331 m = 0;
3332 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3333 m += frame_minheight(frp, next_curwin);
3334 }
3335
3336 return m;
3337}
3338
3339#ifdef FEAT_VERTSPLIT
3340/*
3341 * Compute the minimal width for frame "topfrp".
3342 * When "next_curwin" isn't NULL, use p_wiw for this window.
3343 * When "next_curwin" is NOWIN, don't use at least one column for the current
3344 * window.
3345 */
3346 static int
3347frame_minwidth(topfrp, next_curwin)
3348 frame_T *topfrp;
3349 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3350{
3351 frame_T *frp;
3352 int m, n;
3353
3354 if (topfrp->fr_win != NULL)
3355 {
3356 if (topfrp->fr_win == next_curwin)
3357 m = p_wiw + topfrp->fr_win->w_vsep_width;
3358 else
3359 {
3360 /* window: minimal width of the window plus separator column */
3361 m = p_wmw + topfrp->fr_win->w_vsep_width;
3362 /* Current window is minimal one column wide */
3363 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3364 ++m;
3365 }
3366 }
3367 else if (topfrp->fr_layout == FR_COL)
3368 {
3369 /* get the minimal width from each frame in this column */
3370 m = 0;
3371 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3372 {
3373 n = frame_minwidth(frp, next_curwin);
3374 if (n > m)
3375 m = n;
3376 }
3377 }
3378 else
3379 {
3380 /* Add up the minimal widths for all frames in this row. */
3381 m = 0;
3382 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3383 m += frame_minwidth(frp, next_curwin);
3384 }
3385
3386 return m;
3387}
3388#endif
3389
3390
3391/*
3392 * Try to close all windows except current one.
3393 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3394 * used and the buffer was modified.
3395 *
3396 * Used by ":bdel" and ":only".
3397 */
3398 void
3399close_others(message, forceit)
3400 int message;
3401 int forceit; /* always hide all other windows */
3402{
3403 win_T *wp;
3404 win_T *nextwp;
3405 int r;
3406
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003407 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 if (message
3410#ifdef FEAT_AUTOCMD
3411 && !autocmd_busy
3412#endif
3413 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003414 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return;
3416 }
3417
3418 /* Be very careful here: autocommands may change the window layout. */
3419 for (wp = firstwin; win_valid(wp); wp = nextwp)
3420 {
3421 nextwp = wp->w_next;
3422 if (wp != curwin) /* don't close current window */
3423 {
3424
3425 /* Check if it's allowed to abandon this window */
3426 r = can_abandon(wp->w_buffer, forceit);
3427#ifdef FEAT_AUTOCMD
3428 if (!win_valid(wp)) /* autocommands messed wp up */
3429 {
3430 nextwp = firstwin;
3431 continue;
3432 }
3433#endif
3434 if (!r)
3435 {
3436#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3437 if (message && (p_confirm || cmdmod.confirm) && p_write)
3438 {
3439 dialog_changed(wp->w_buffer, FALSE);
3440# ifdef FEAT_AUTOCMD
3441 if (!win_valid(wp)) /* autocommands messed wp up */
3442 {
3443 nextwp = firstwin;
3444 continue;
3445 }
3446# endif
3447 }
3448 if (bufIsChanged(wp->w_buffer))
3449#endif
3450 continue;
3451 }
3452 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3453 }
3454 }
3455
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003456 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 EMSG(_("E445: Other window contains changes"));
3458}
3459
3460#endif /* FEAT_WINDOWS */
3461
3462/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003463 * Init the current window "curwin".
3464 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
3466 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003467curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003469 win_init_empty(curwin);
3470}
3471
3472 void
3473win_init_empty(wp)
3474 win_T *wp;
3475{
3476 redraw_win_later(wp, NOT_VALID);
3477 wp->w_lines_valid = 0;
3478 wp->w_cursor.lnum = 1;
3479 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003481 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003483 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3484 wp->w_pcmark.col = 0;
3485 wp->w_prev_pcmark.lnum = 0;
3486 wp->w_prev_pcmark.col = 0;
3487 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003489 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003491 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003493 if (wp->w_p_rl)
3494 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003496 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003498#ifdef FEAT_SYN_HL
3499 wp->w_s = &wp->w_buffer->b_s;
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503/*
3504 * Allocate the first window and put an empty buffer in it.
3505 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003508 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509win_alloc_first()
3510{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003511 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003512 return FAIL;
3513
3514#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003515 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 if (first_tabpage == NULL)
3517 return FAIL;
3518 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003519 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003521
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522 return OK;
3523}
3524
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003525#if defined(FEAT_AUTOCMD) || defined(PROTO)
3526/*
3527 * Init "aucmd_win". This can only be done after the first
3528 * window is fully initialized, thus it can't be in win_alloc_first().
3529 */
3530 void
3531win_alloc_aucmd_win()
3532{
3533 aucmd_win = win_alloc(NULL, TRUE);
3534 if (aucmd_win != NULL)
3535 {
3536 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003537 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003538 new_frame(aucmd_win);
3539 }
3540}
3541#endif
3542
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003544 * Allocate the first window or the first window in a new tab page.
3545 * When "oldwin" is NULL create an empty buffer for it.
3546 * When "oldwin" is not NULL copy info from it to the new window (only with
3547 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548 * Return FAIL when something goes wrong (out of memory).
3549 */
3550 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003551win_alloc_firstwin(oldwin)
3552 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003554 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003555 if (oldwin == NULL)
3556 {
3557 /* Very first window, need to create an empty buffer for it and
3558 * initialize from scratch. */
3559 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3560 if (curwin == NULL || curbuf == NULL)
3561 return FAIL;
3562 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003563#ifdef FEAT_SYN_HL
3564 curwin->w_s = &(curbuf->b_s);
3565#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003566 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003568 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003570 curwin_init(); /* init current window */
3571 }
3572#ifdef FEAT_WINDOWS
3573 else
3574 {
3575 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003576 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003577
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003578 /* We don't want cursor- and scroll-binding in the first window. */
3579 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003580 }
3581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003583 new_frame(curwin);
3584 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003586 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#ifdef FEAT_VERTSPLIT
3588 topframe->fr_width = Columns;
3589#endif
3590 topframe->fr_height = Rows - p_ch;
3591 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592
3593 return OK;
3594}
3595
3596/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003597 * Create a frame for window "wp".
3598 */
3599 static void
3600new_frame(win_T *wp)
3601{
3602 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3603
3604 wp->w_frame = frp;
3605 if (frp != NULL)
3606 {
3607 frp->fr_layout = FR_LEAF;
3608 frp->fr_win = wp;
3609 }
3610}
3611
3612/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003613 * Initialize the window and frame size to the maximum.
3614 */
3615 void
3616win_init_size()
3617{
3618 firstwin->w_height = ROWS_AVAIL;
3619 topframe->fr_height = ROWS_AVAIL;
3620#ifdef FEAT_VERTSPLIT
3621 firstwin->w_width = Columns;
3622 topframe->fr_width = Columns;
3623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624}
3625
3626#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003627
3628/*
3629 * Allocate a new tabpage_T and init the values.
3630 * Returns NULL when out of memory.
3631 */
3632 static tabpage_T *
3633alloc_tabpage()
3634{
3635 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003636# ifdef FEAT_GUI
3637 int i;
3638# endif
3639
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640
3641 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003642 if (tp == NULL)
3643 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003644
Bram Moolenaar429fa852013-04-15 12:27:36 +02003645# ifdef FEAT_EVAL
3646 /* init t: variables */
3647 tp->tp_vars = dict_alloc();
3648 if (tp->tp_vars == NULL)
3649 {
3650 vim_free(tp);
3651 return NULL;
3652 }
3653 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3654# endif
3655
3656# ifdef FEAT_GUI
3657 for (i = 0; i < 3; i++)
3658 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003659# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003660# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003661 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003662# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003663 tp->tp_ch_used = p_ch;
3664
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003665 return tp;
3666}
3667
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003668 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003669free_tabpage(tp)
3670 tabpage_T *tp;
3671{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003672 int idx;
3673
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003674# ifdef FEAT_DIFF
3675 diff_clear(tp);
3676# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003677 for (idx = 0; idx < SNAP_COUNT; ++idx)
3678 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003679#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003680 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3681 hash_init(&tp->tp_vars->dv_hashtab);
3682 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003683#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003684
3685#ifdef FEAT_PYTHON
3686 python_tabpage_free(tp);
3687#endif
3688
3689#ifdef FEAT_PYTHON3
3690 python3_tabpage_free(tp);
3691#endif
3692
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003693 vim_free(tp);
3694}
3695
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003697 * Create a new Tab page with one window.
3698 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003699 * When "after" is 0 put it just after the current Tab page.
3700 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701 * Return FAIL or OK.
3702 */
3703 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003704win_new_tabpage(after)
3705 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003707 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003709 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003710
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003711 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 if (newtp == NULL)
3713 return FAIL;
3714
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003715 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003716 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003717 {
3718 vim_free(newtp);
3719 return FAIL;
3720 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003721 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722
3723 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003724 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003725 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003726 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003727 if (after == 1)
3728 {
3729 /* New tab page becomes the first one. */
3730 newtp->tp_next = first_tabpage;
3731 first_tabpage = newtp;
3732 }
3733 else
3734 {
3735 if (after > 0)
3736 {
3737 /* Put new tab page before tab page "after". */
3738 n = 2;
3739 for (tp = first_tabpage; tp->tp_next != NULL
3740 && n < after; tp = tp->tp_next)
3741 ++n;
3742 }
3743 newtp->tp_next = tp->tp_next;
3744 tp->tp_next = newtp;
3745 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003747 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003748 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749
3750 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003751 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003752
3753#if defined(FEAT_GUI)
3754 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3755 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003756 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003757#endif
3758
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003760#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003761 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003762 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003763#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764 return OK;
3765 }
3766
3767 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003769 return FAIL;
3770}
3771
3772/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003773 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3774 * like with ":split".
3775 * Returns OK if a new tab page was created, FAIL otherwise.
3776 */
3777 int
3778may_open_tabpage()
3779{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003780 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003781
Bram Moolenaard326ce82007-03-11 14:48:29 +00003782 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003783 {
3784 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003785 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003786 return win_new_tabpage(n);
3787 }
3788 return FAIL;
3789}
3790
3791/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003792 * Create up to "maxcount" tabpages with empty windows.
3793 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003794 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003795 int
3796make_tabpages(maxcount)
3797 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003799 int count = maxcount;
3800 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003801
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003802 /* Limit to 'tabpagemax' tabs. */
3803 if (count > p_tpm)
3804 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003805
3806#ifdef FEAT_AUTOCMD
3807 /*
3808 * Don't execute autocommands while creating the tab pages. Must do that
3809 * when putting the buffers in the windows.
3810 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003811 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003812#endif
3813
3814 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003815 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003817
3818#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003819 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003820#endif
3821
3822 /* return actual number of tab pages */
3823 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824}
3825
3826/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003827 * Return TRUE when "tpc" points to a valid tab page.
3828 */
3829 int
3830valid_tabpage(tpc)
3831 tabpage_T *tpc;
3832{
3833 tabpage_T *tp;
3834
3835 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3836 if (tp == tpc)
3837 return TRUE;
3838 return FALSE;
3839}
3840
3841/*
3842 * Find tab page "n" (first one is 1). Returns NULL when not found.
3843 */
3844 tabpage_T *
3845find_tabpage(n)
3846 int n;
3847{
3848 tabpage_T *tp;
3849 int i = 1;
3850
3851 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3852 ++i;
3853 return tp;
3854}
3855
3856/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003857 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003858 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003859 */
3860 int
3861tabpage_index(ftp)
3862 tabpage_T *ftp;
3863{
3864 int i = 1;
3865 tabpage_T *tp;
3866
3867 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3868 ++i;
3869 return i;
3870}
3871
3872/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003873 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003874 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003875 * FAIL.
3876 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003878 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003879leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003880 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003881 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003882 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003884 tabpage_T *tp = curtab;
3885
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003886 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003887#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003888 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003889 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003890 if (new_curbuf != curbuf)
3891 {
3892 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3893 if (curtab != tp)
3894 return FAIL;
3895 }
3896 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3897 if (curtab != tp)
3898 return FAIL;
3899 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003900 if (curtab != tp)
3901 return FAIL;
3902 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003904#if defined(FEAT_GUI)
3905 /* Remove the scrollbars. They may be added back later. */
3906 if (gui.in_use)
3907 gui_remove_scrollbars();
3908#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003909 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003910 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911 tp->tp_firstwin = firstwin;
3912 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003913 tp->tp_old_Rows = Rows;
3914 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003915 firstwin = NULL;
3916 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003917 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003918}
3919
3920/*
3921 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003922 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003923 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3924 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003925 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003926 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003927enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003928 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003929 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003930 int trigger_enter_autocmds UNUSED;
3931 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003933 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003934 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003935
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003936 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003937 firstwin = tp->tp_firstwin;
3938 lastwin = tp->tp_lastwin;
3939 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003940
3941 /* We would like doing the TabEnter event first, but we don't have a
3942 * valid current window yet, which may break some commands.
3943 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003944 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3945 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003946 prevwin = next_prevwin;
3947
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003948 last_status(FALSE); /* status line may appear or disappear */
3949 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003950 must_redraw = CLEAR; /* need to redraw everything */
3951#ifdef FEAT_DIFF
3952 diff_need_scrollbind = TRUE;
3953#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003954
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003955 /* The tabpage line may have appeared or disappeared, may need to resize
3956 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003957 * frame sizes too. Use the stored value of p_ch, so that it can be
3958 * different for each tab page. */
3959 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003960 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3961#ifdef FEAT_GUI_TABLINE
3962 && !gui_use_tabline()
3963#endif
3964 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003965 shell_new_rows();
3966#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003967 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003968 shell_new_columns(); /* update window widths */
3969#endif
3970
3971#if defined(FEAT_GUI)
3972 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3973 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003974 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003975#endif
3976
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003977#ifdef FEAT_AUTOCMD
3978 /* Apply autocommands after updating the display, when 'rows' and
3979 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003980 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003981 {
3982 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3983 if (old_curbuf != curbuf)
3984 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3985 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003986#endif
3987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003988 redraw_all_later(CLEAR);
3989}
3990
3991/*
3992 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003993 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003994 */
3995 void
3996goto_tabpage(n)
3997 int n;
3998{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003999 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004000 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004001 int i;
4002
Bram Moolenaard68071d2006-05-02 22:08:30 +00004003 if (text_locked())
4004 {
4005 /* Not allowed when editing the command line. */
4006#ifdef FEAT_CMDWIN
4007 if (cmdwin_type != 0)
4008 EMSG(_(e_cmdwin));
4009 else
4010#endif
4011 EMSG(_(e_secure));
4012 return;
4013 }
4014
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004015 /* If there is only one it can't work. */
4016 if (first_tabpage->tp_next == NULL)
4017 {
4018 if (n > 1)
4019 beep_flush();
4020 return;
4021 }
4022
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004023 if (n == 0)
4024 {
4025 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004026 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004027 tp = first_tabpage;
4028 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004029 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004030 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004031 else if (n < 0)
4032 {
4033 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4034 * this N times. */
4035 ttp = curtab;
4036 for (i = n; i < 0; ++i)
4037 {
4038 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4039 tp = tp->tp_next)
4040 ;
4041 ttp = tp;
4042 }
4043 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004044 else if (n == 9999)
4045 {
4046 /* Go to last tab page. */
4047 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4048 ;
4049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004050 else
4051 {
4052 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004053 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004054 if (tp == NULL)
4055 {
4056 beep_flush();
4057 return;
4058 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004059 }
4060
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004061 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004062
4063#ifdef FEAT_GUI_TABLINE
4064 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004065 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004066#endif
4067}
4068
4069/*
4070 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004071 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4072 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004073 * Note: doesn't update the GUI tab.
4074 */
4075 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004076goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004077 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004078 int trigger_enter_autocmds;
4079 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004080{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004081 /* Don't repeat a message in another tab page. */
4082 set_keep_msg(NULL, 0);
4083
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004084 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4085 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004086 {
4087 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004088 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4089 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004090 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004091 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4092 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004093 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004097 * Enter window "wp" in tab page "tp".
4098 * Also updates the GUI tab.
4099 */
4100 void
4101goto_tabpage_win(tp, wp)
4102 tabpage_T *tp;
4103 win_T *wp;
4104{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004105 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004106 if (curtab == tp && win_valid(wp))
4107 {
4108 win_enter(wp, TRUE);
4109# ifdef FEAT_GUI_TABLINE
4110 if (gui_use_tabline())
4111 gui_mch_set_curtab(tabpage_index(curtab));
4112# endif
4113 }
4114}
4115
4116/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004117 * Move the current tab page to before tab page "nr".
4118 */
4119 void
4120tabpage_move(nr)
4121 int nr;
4122{
4123 int n = nr;
4124 tabpage_T *tp;
4125
4126 if (first_tabpage->tp_next == NULL)
4127 return;
4128
4129 /* Remove the current tab page from the list of tab pages. */
4130 if (curtab == first_tabpage)
4131 first_tabpage = curtab->tp_next;
4132 else
4133 {
4134 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4135 if (tp->tp_next == curtab)
4136 break;
4137 if (tp == NULL) /* "cannot happen" */
4138 return;
4139 tp->tp_next = curtab->tp_next;
4140 }
4141
4142 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004143 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004144 {
4145 curtab->tp_next = first_tabpage;
4146 first_tabpage = curtab;
4147 }
4148 else
4149 {
4150 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4151 --n;
4152 curtab->tp_next = tp->tp_next;
4153 tp->tp_next = curtab;
4154 }
4155
4156 /* Need to redraw the tabline. Tab page contents doesn't change. */
4157 redraw_tabline = TRUE;
4158}
4159
4160
4161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Go to another window.
4163 * When jumping to another buffer, stop Visual mode. Do this before
4164 * changing windows so we can yank the selection into the '*' register.
4165 * When jumping to another window on the same buffer, adjust its cursor
4166 * position to keep the same Visual area.
4167 */
4168 void
4169win_goto(wp)
4170 win_T *wp;
4171{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004172#ifdef FEAT_CONCEAL
4173 win_T *owp = curwin;
4174#endif
4175
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004176 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004179 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return;
4181 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004182#ifdef FEAT_AUTOCMD
4183 if (curbuf_locked())
4184 return;
4185#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (wp->w_buffer != curbuf)
4188 reset_VIsual_and_resel();
4189 else if (VIsual_active)
4190 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192#ifdef FEAT_GUI
4193 need_mouse_correct = TRUE;
4194#endif
4195 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004196
4197#ifdef FEAT_CONCEAL
4198 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004199 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004200 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004201 if (curwin->w_p_cole > 0 && !msg_scrolled)
4202 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204}
4205
4206#if defined(FEAT_PERL) || defined(PROTO)
4207/*
4208 * Find window number "winnr" (counting top to bottom).
4209 */
4210 win_T *
4211win_find_nr(winnr)
4212 int winnr;
4213{
4214 win_T *wp;
4215
4216# ifdef FEAT_WINDOWS
4217 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4218 if (--winnr == 0)
4219 break;
4220 return wp;
4221# else
4222 return curwin;
4223# endif
4224}
4225#endif
4226
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004227#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4228 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004229/*
4230 * Find the tabpage for window "win".
4231 */
4232 tabpage_T *
4233win_find_tabpage(win)
4234 win_T *win;
4235{
4236 win_T *wp;
4237 tabpage_T *tp;
4238
4239 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004240 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4241 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004242 if (wp == win)
4243 return tp;
4244 return NULL;
4245}
4246#endif
4247
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248#ifdef FEAT_VERTSPLIT
4249/*
4250 * Move to window above or below "count" times.
4251 */
4252 static void
4253win_goto_ver(up, count)
4254 int up; /* TRUE to go to win above */
4255 long count;
4256{
4257 frame_T *fr;
4258 frame_T *nfr;
4259 frame_T *foundfr;
4260
4261 foundfr = curwin->w_frame;
4262 while (count--)
4263 {
4264 /*
4265 * First go upwards in the tree of frames until we find a upwards or
4266 * downwards neighbor.
4267 */
4268 fr = foundfr;
4269 for (;;)
4270 {
4271 if (fr == topframe)
4272 goto end;
4273 if (up)
4274 nfr = fr->fr_prev;
4275 else
4276 nfr = fr->fr_next;
4277 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4278 break;
4279 fr = fr->fr_parent;
4280 }
4281
4282 /*
4283 * Now go downwards to find the bottom or top frame in it.
4284 */
4285 for (;;)
4286 {
4287 if (nfr->fr_layout == FR_LEAF)
4288 {
4289 foundfr = nfr;
4290 break;
4291 }
4292 fr = nfr->fr_child;
4293 if (nfr->fr_layout == FR_ROW)
4294 {
4295 /* Find the frame at the cursor row. */
4296 while (fr->fr_next != NULL
4297 && frame2win(fr)->w_wincol + fr->fr_width
4298 <= curwin->w_wincol + curwin->w_wcol)
4299 fr = fr->fr_next;
4300 }
4301 if (nfr->fr_layout == FR_COL && up)
4302 while (fr->fr_next != NULL)
4303 fr = fr->fr_next;
4304 nfr = fr;
4305 }
4306 }
4307end:
4308 if (foundfr != NULL)
4309 win_goto(foundfr->fr_win);
4310}
4311
4312/*
4313 * Move to left or right window.
4314 */
4315 static void
4316win_goto_hor(left, count)
4317 int left; /* TRUE to go to left win */
4318 long count;
4319{
4320 frame_T *fr;
4321 frame_T *nfr;
4322 frame_T *foundfr;
4323
4324 foundfr = curwin->w_frame;
4325 while (count--)
4326 {
4327 /*
4328 * First go upwards in the tree of frames until we find a left or
4329 * right neighbor.
4330 */
4331 fr = foundfr;
4332 for (;;)
4333 {
4334 if (fr == topframe)
4335 goto end;
4336 if (left)
4337 nfr = fr->fr_prev;
4338 else
4339 nfr = fr->fr_next;
4340 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4341 break;
4342 fr = fr->fr_parent;
4343 }
4344
4345 /*
4346 * Now go downwards to find the leftmost or rightmost frame in it.
4347 */
4348 for (;;)
4349 {
4350 if (nfr->fr_layout == FR_LEAF)
4351 {
4352 foundfr = nfr;
4353 break;
4354 }
4355 fr = nfr->fr_child;
4356 if (nfr->fr_layout == FR_COL)
4357 {
4358 /* Find the frame at the cursor row. */
4359 while (fr->fr_next != NULL
4360 && frame2win(fr)->w_winrow + fr->fr_height
4361 <= curwin->w_winrow + curwin->w_wrow)
4362 fr = fr->fr_next;
4363 }
4364 if (nfr->fr_layout == FR_ROW && left)
4365 while (fr->fr_next != NULL)
4366 fr = fr->fr_next;
4367 nfr = fr;
4368 }
4369 }
4370end:
4371 if (foundfr != NULL)
4372 win_goto(foundfr->fr_win);
4373}
4374#endif
4375
4376/*
4377 * Make window "wp" the current window.
4378 */
4379 void
4380win_enter(wp, undo_sync)
4381 win_T *wp;
4382 int undo_sync;
4383{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004384 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385}
4386
4387/*
4388 * Make window wp the current window.
4389 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4390 * been closed and isn't valid.
4391 */
4392 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004393win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 win_T *wp;
4395 int undo_sync;
4396 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004397 int trigger_enter_autocmds UNUSED;
4398 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400#ifdef FEAT_AUTOCMD
4401 int other_buffer = FALSE;
4402#endif
4403
4404 if (wp == curwin && !curwin_invalid) /* nothing to do */
4405 return;
4406
4407#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004408 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
4410 /*
4411 * Be careful: If autocommands delete the window, return now.
4412 */
4413 if (wp->w_buffer != curbuf)
4414 {
4415 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4416 other_buffer = TRUE;
4417 if (!win_valid(wp))
4418 return;
4419 }
4420 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4421 if (!win_valid(wp))
4422 return;
4423# ifdef FEAT_EVAL
4424 /* autocmds may abort script processing */
4425 if (aborting())
4426 return;
4427# endif
4428 }
4429#endif
4430
4431 /* sync undo before leaving the current buffer */
4432 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004433 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004434
4435 /* Might need to scroll the old window before switching, e.g., when the
4436 * cursor was moved. */
4437 update_topline();
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /* may have to copy the buffer options when 'cpo' contains 'S' */
4440 if (wp->w_buffer != curbuf)
4441 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4442 if (!curwin_invalid)
4443 {
4444 prevwin = curwin; /* remember for CTRL-W p */
4445 curwin->w_redr_status = TRUE;
4446 }
4447 curwin = wp;
4448 curbuf = wp->w_buffer;
4449 check_cursor();
4450#ifdef FEAT_VIRTUALEDIT
4451 if (!virtual_active())
4452 curwin->w_cursor.coladd = 0;
4453#endif
4454 changed_line_abv_curs(); /* assume cursor position needs updating */
4455
4456 if (curwin->w_localdir != NULL)
4457 {
4458 /* Window has a local directory: Save current directory as global
4459 * directory (unless that was done already) and change to the local
4460 * directory. */
4461 if (globaldir == NULL)
4462 {
4463 char_u cwd[MAXPATHL];
4464
4465 if (mch_dirname(cwd, MAXPATHL) == OK)
4466 globaldir = vim_strsave(cwd);
4467 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004468 if (mch_chdir((char *)curwin->w_localdir) == 0)
4469 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else if (globaldir != NULL)
4472 {
4473 /* Window doesn't have a local directory and we are not in the global
4474 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004475 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 vim_free(globaldir);
4477 globaldir = NULL;
4478 shorten_fnames(TRUE);
4479 }
4480
4481#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004482 if (trigger_enter_autocmds)
4483 {
4484 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4485 if (other_buffer)
4486 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#endif
4489
4490#ifdef FEAT_TITLE
4491 maketitle();
4492#endif
4493 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004494 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (restart_edit)
4496 redraw_later(VALID); /* causes status line redraw */
4497
4498 /* set window height to desired minimal value */
4499 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4500 win_setheight((int)p_wh);
4501 else if (curwin->w_height == 0)
4502 win_setheight(1);
4503
4504#ifdef FEAT_VERTSPLIT
4505 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004506 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 win_setwidth((int)p_wiw);
4508#endif
4509
4510#ifdef FEAT_MOUSE
4511 setmouse(); /* in case jumped to/from help buffer */
4512#endif
4513
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004514 /* Change directories when the 'acd' option is set. */
4515 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516}
4517
4518#endif /* FEAT_WINDOWS */
4519
4520#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4521/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004522 * Jump to the first open window that contains buffer "buf", if one exists.
4523 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 */
4525 win_T *
4526buf_jump_open_win(buf)
4527 buf_T *buf;
4528{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004529 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004531 if (curwin->w_buffer == buf)
4532 wp = curwin;
4533# ifdef FEAT_WINDOWS
4534 else
4535 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4536 if (wp->w_buffer == buf)
4537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (wp != NULL)
4539 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004541 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004543
4544/*
4545 * Jump to the first open window in any tab page that contains buffer "buf",
4546 * if one exists.
4547 * Returns a pointer to the window found, otherwise NULL.
4548 */
4549 win_T *
4550buf_jump_open_tab(buf)
4551 buf_T *buf;
4552{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004553 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004554# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004555 tabpage_T *tp;
4556
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004557 if (wp != NULL)
4558 return wp;
4559
4560 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4561 if (tp != curtab)
4562 {
4563 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4564 if (wp->w_buffer == buf)
4565 break;
4566 if (wp != NULL)
4567 {
4568 goto_tabpage_win(tp, wp);
4569 if (curwin != wp)
4570 wp = NULL; /* something went wrong */
4571 break;
4572 }
4573 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004574# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004575 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#endif
4578
4579/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004580 * Allocate a window structure and link it in the window list when "hidden" is
4581 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004584win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004585 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004586 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004588 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 /*
4591 * allocate window structure and linesizes arrays
4592 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004593 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004594 if (new_wp == NULL)
4595 return NULL;
4596
4597 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004599 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602
Bram Moolenaar429fa852013-04-15 12:27:36 +02004603#ifdef FEAT_EVAL
4604 /* init w: variables */
4605 new_wp->w_vars = dict_alloc();
4606 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004608 win_free_lsize(new_wp);
4609 vim_free(new_wp);
4610 return NULL;
4611 }
4612 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004613#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004614
4615#ifdef FEAT_AUTOCMD
4616 /* Don't execute autocommands while the window is not properly
4617 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4618 * event. */
4619 block_autocmds();
4620#endif
4621 /*
4622 * link the window in the window list
4623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004625 if (!hidden)
4626 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627#endif
4628#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004629 new_wp->w_wincol = 0;
4630 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#endif
4632
Bram Moolenaar429fa852013-04-15 12:27:36 +02004633 /* position the display and the cursor at the top of the file. */
4634 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004636 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004638 new_wp->w_botline = 2;
4639 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004641 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
4643
Bram Moolenaar429fa852013-04-15 12:27:36 +02004644 /* We won't calculate w_fraction until resizing the window */
4645 new_wp->w_fraction = 0;
4646 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004649 if (gui.in_use)
4650 {
4651 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4652 SBAR_LEFT, new_wp);
4653 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4654 SBAR_RIGHT, new_wp);
4655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#endif
4657#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004658 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004660#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004661 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004662#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004663#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004664 new_wp->w_match_head = NULL;
4665 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004666#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004667 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668}
4669
4670#if defined(FEAT_WINDOWS) || defined(PROTO)
4671
4672/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004673 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 */
4675 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004676win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004678 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004681 buf_T *buf;
4682 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004684#ifdef FEAT_FOLDING
4685 clearFolding(wp);
4686#endif
4687
4688 /* reduce the reference count to the argument list. */
4689 alist_unlink(wp->w_alist);
4690
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004691#ifdef FEAT_AUTOCMD
4692 /* Don't execute autocommands while the window is halfway being deleted.
4693 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004694 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004695#endif
4696
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004697#ifdef FEAT_LUA
4698 lua_window_free(wp);
4699#endif
4700
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004701#ifdef FEAT_MZSCHEME
4702 mzscheme_window_free(wp);
4703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#ifdef FEAT_PERL
4706 perl_win_free(wp);
4707#endif
4708
4709#ifdef FEAT_PYTHON
4710 python_window_free(wp);
4711#endif
4712
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004713#ifdef FEAT_PYTHON3
4714 python3_window_free(wp);
4715#endif
4716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_TCL
4718 tcl_window_free(wp);
4719#endif
4720
4721#ifdef FEAT_RUBY
4722 ruby_window_free(wp);
4723#endif
4724
4725 clear_winopt(&wp->w_onebuf_opt);
4726 clear_winopt(&wp->w_allbuf_opt);
4727
4728#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004729 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4730 hash_init(&wp->w_vars->dv_hashtab);
4731 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
4733
4734 if (prevwin == wp)
4735 prevwin = NULL;
4736 win_free_lsize(wp);
4737
4738 for (i = 0; i < wp->w_tagstacklen; ++i)
4739 vim_free(wp->w_tagstack[i].tagname);
4740
4741 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004742
Bram Moolenaarff18df02013-07-24 17:51:57 +02004743 /* Remove the window from the b_wininfo lists, it may happen that the
4744 * freed memory is re-used for another window. */
4745 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4746 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4747 if (wip->wi_win == wp)
4748 wip->wi_win = NULL;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004751 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#ifdef FEAT_JUMPLIST
4755 free_jumplist(wp);
4756#endif
4757
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004758#ifdef FEAT_QUICKFIX
4759 qf_free_all(wp);
4760#endif
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#ifdef FEAT_GUI
4763 if (gui.in_use)
4764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4766 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4767 }
4768#endif /* FEAT_GUI */
4769
Bram Moolenaar860cae12010-06-05 23:22:07 +02004770#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004771 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004772#endif
4773
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004774#ifdef FEAT_AUTOCMD
4775 if (wp != aucmd_win)
4776#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004777 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004778#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004779 if (autocmd_busy)
4780 {
4781 wp->w_next = au_pending_free_win;
4782 au_pending_free_win = wp;
4783 }
4784 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004785#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004786 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004787
4788#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004789 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791}
4792
4793/*
4794 * Append window "wp" in the window list after window "after".
4795 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004796 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797win_append(after, wp)
4798 win_T *after, *wp;
4799{
4800 win_T *before;
4801
4802 if (after == NULL) /* after NULL is in front of the first */
4803 before = firstwin;
4804 else
4805 before = after->w_next;
4806
4807 wp->w_next = before;
4808 wp->w_prev = after;
4809 if (after == NULL)
4810 firstwin = wp;
4811 else
4812 after->w_next = wp;
4813 if (before == NULL)
4814 lastwin = wp;
4815 else
4816 before->w_prev = wp;
4817}
4818
4819/*
4820 * Remove a window from the window list.
4821 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004822 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004823win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004825 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 if (wp->w_prev != NULL)
4828 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004829 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004831 else
4832 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (wp->w_next != NULL)
4834 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004835 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004837 else
4838 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
4841/*
4842 * Append frame "frp" in a frame list after frame "after".
4843 */
4844 static void
4845frame_append(after, frp)
4846 frame_T *after, *frp;
4847{
4848 frp->fr_next = after->fr_next;
4849 after->fr_next = frp;
4850 if (frp->fr_next != NULL)
4851 frp->fr_next->fr_prev = frp;
4852 frp->fr_prev = after;
4853}
4854
4855/*
4856 * Insert frame "frp" in a frame list before frame "before".
4857 */
4858 static void
4859frame_insert(before, frp)
4860 frame_T *before, *frp;
4861{
4862 frp->fr_next = before;
4863 frp->fr_prev = before->fr_prev;
4864 before->fr_prev = frp;
4865 if (frp->fr_prev != NULL)
4866 frp->fr_prev->fr_next = frp;
4867 else
4868 frp->fr_parent->fr_child = frp;
4869}
4870
4871/*
4872 * Remove a frame from a frame list.
4873 */
4874 static void
4875frame_remove(frp)
4876 frame_T *frp;
4877{
4878 if (frp->fr_prev != NULL)
4879 frp->fr_prev->fr_next = frp->fr_next;
4880 else
4881 frp->fr_parent->fr_child = frp->fr_next;
4882 if (frp->fr_next != NULL)
4883 frp->fr_next->fr_prev = frp->fr_prev;
4884}
4885
4886#endif /* FEAT_WINDOWS */
4887
4888/*
4889 * Allocate w_lines[] for window "wp".
4890 * Return FAIL for failure, OK for success.
4891 */
4892 int
4893win_alloc_lines(wp)
4894 win_T *wp;
4895{
4896 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004897 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (wp->w_lines == NULL)
4899 return FAIL;
4900 return OK;
4901}
4902
4903/*
4904 * free lsize arrays for a window
4905 */
4906 void
4907win_free_lsize(wp)
4908 win_T *wp;
4909{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004910 /* TODO: why would wp be NULL here? */
4911 if (wp != NULL)
4912 {
4913 vim_free(wp->w_lines);
4914 wp->w_lines = NULL;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916}
4917
4918/*
4919 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004920 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
4922 void
4923shell_new_rows()
4924{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004925 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 if (firstwin == NULL) /* not initialized yet */
4928 return;
4929#ifdef FEAT_WINDOWS
4930 if (h < frame_minheight(topframe, NULL))
4931 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004932
4933 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 * that doesn't result in the right height, forget about that option. */
4935 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004936 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 frame_new_height(topframe, h, FALSE, FALSE);
4938
4939 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4940#else
4941 if (h < 1)
4942 h = 1;
4943 win_new_height(firstwin, h);
4944#endif
4945 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004946#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004947 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004948#endif
4949
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950#if 0
4951 /* Disabled: don't want making the screen smaller make a window larger. */
4952 if (p_ea)
4953 win_equal(curwin, FALSE, 'v');
4954#endif
4955}
4956
4957#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4958/*
4959 * Called from win_new_shellsize() after Columns changed.
4960 */
4961 void
4962shell_new_columns()
4963{
4964 if (firstwin == NULL) /* not initialized yet */
4965 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004966
4967 /* First try setting the widths of windows with 'winfixwidth'. If that
4968 * doesn't result in the right width, forget about that option. */
4969 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004970 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004971 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4974#if 0
4975 /* Disabled: don't want making the screen smaller make a window larger. */
4976 if (p_ea)
4977 win_equal(curwin, FALSE, 'h');
4978#endif
4979}
4980#endif
4981
4982#if defined(FEAT_CMDWIN) || defined(PROTO)
4983/*
4984 * Save the size of all windows in "gap".
4985 */
4986 void
4987win_size_save(gap)
4988 garray_T *gap;
4989
4990{
4991 win_T *wp;
4992
4993 ga_init2(gap, (int)sizeof(int), 1);
4994 if (ga_grow(gap, win_count() * 2) == OK)
4995 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4996 {
4997 ((int *)gap->ga_data)[gap->ga_len++] =
4998 wp->w_width + wp->w_vsep_width;
4999 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5000 }
5001}
5002
5003/*
5004 * Restore window sizes, but only if the number of windows is still the same.
5005 * Does not free the growarray.
5006 */
5007 void
5008win_size_restore(gap)
5009 garray_T *gap;
5010{
5011 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005012 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 if (win_count() * 2 == gap->ga_len)
5015 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005016 /* The order matters, because frames contain other frames, but it's
5017 * difficult to get right. The easy way out is to do it twice. */
5018 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005020 i = 0;
5021 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5022 {
5023 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5024 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 /* recompute the window positions */
5028 (void)win_comp_pos();
5029 }
5030}
5031#endif /* FEAT_CMDWIN */
5032
5033#if defined(FEAT_WINDOWS) || defined(PROTO)
5034/*
5035 * Update the position for all windows, using the width and height of the
5036 * frames.
5037 * Returns the row just after the last window.
5038 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005039 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040win_comp_pos()
5041{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005042 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 int col = 0;
5044
5045 frame_comp_pos(topframe, &row, &col);
5046 return row;
5047}
5048
5049/*
5050 * Update the position of the windows in frame "topfrp", using the width and
5051 * height of the frames.
5052 * "*row" and "*col" are the top-left position of the frame. They are updated
5053 * to the bottom-right position plus one.
5054 */
5055 static void
5056frame_comp_pos(topfrp, row, col)
5057 frame_T *topfrp;
5058 int *row;
5059 int *col;
5060{
5061 win_T *wp;
5062 frame_T *frp;
5063#ifdef FEAT_VERTSPLIT
5064 int startcol;
5065 int startrow;
5066#endif
5067
5068 wp = topfrp->fr_win;
5069 if (wp != NULL)
5070 {
5071 if (wp->w_winrow != *row
5072#ifdef FEAT_VERTSPLIT
5073 || wp->w_wincol != *col
5074#endif
5075 )
5076 {
5077 /* position changed, redraw */
5078 wp->w_winrow = *row;
5079#ifdef FEAT_VERTSPLIT
5080 wp->w_wincol = *col;
5081#endif
5082 redraw_win_later(wp, NOT_VALID);
5083 wp->w_redr_status = TRUE;
5084 }
5085 *row += wp->w_height + wp->w_status_height;
5086#ifdef FEAT_VERTSPLIT
5087 *col += wp->w_width + wp->w_vsep_width;
5088#endif
5089 }
5090 else
5091 {
5092#ifdef FEAT_VERTSPLIT
5093 startrow = *row;
5094 startcol = *col;
5095#endif
5096 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5097 {
5098#ifdef FEAT_VERTSPLIT
5099 if (topfrp->fr_layout == FR_ROW)
5100 *row = startrow; /* all frames are at the same row */
5101 else
5102 *col = startcol; /* all frames are at the same col */
5103#endif
5104 frame_comp_pos(frp, row, col);
5105 }
5106 }
5107}
5108
5109#endif /* FEAT_WINDOWS */
5110
5111/*
5112 * Set current window height and take care of repositioning other windows to
5113 * fit around it.
5114 */
5115 void
5116win_setheight(height)
5117 int height;
5118{
5119 win_setheight_win(height, curwin);
5120}
5121
5122/*
5123 * Set the window height of window "win" and take care of repositioning other
5124 * windows to fit around it.
5125 */
5126 void
5127win_setheight_win(height, win)
5128 int height;
5129 win_T *win;
5130{
5131 int row;
5132
5133 if (win == curwin)
5134 {
5135 /* Always keep current window at least one line high, even when
5136 * 'winminheight' is zero. */
5137#ifdef FEAT_WINDOWS
5138 if (height < p_wmh)
5139 height = p_wmh;
5140#endif
5141 if (height == 0)
5142 height = 1;
5143 }
5144
5145#ifdef FEAT_WINDOWS
5146 frame_setheight(win->w_frame, height + win->w_status_height);
5147
5148 /* recompute the window positions */
5149 row = win_comp_pos();
5150#else
5151 if (height > topframe->fr_height)
5152 height = topframe->fr_height;
5153 win->w_height = height;
5154 row = height;
5155#endif
5156
5157 /*
5158 * If there is extra space created between the last window and the command
5159 * line, clear it.
5160 */
5161 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5162 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5163 cmdline_row = row;
5164 msg_row = row;
5165 msg_col = 0;
5166
5167 redraw_all_later(NOT_VALID);
5168}
5169
5170#if defined(FEAT_WINDOWS) || defined(PROTO)
5171
5172/*
5173 * Set the height of a frame to "height" and take care that all frames and
5174 * windows inside it are resized. Also resize frames on the left and right if
5175 * the are in the same FR_ROW frame.
5176 *
5177 * Strategy:
5178 * If the frame is part of a FR_COL frame, try fitting the frame in that
5179 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5180 * go to containing frames to resize them and make room.
5181 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5182 * Check for the minimal height of the FR_ROW frame.
5183 * At the top level we can also use change the command line height.
5184 */
5185 static void
5186frame_setheight(curfrp, height)
5187 frame_T *curfrp;
5188 int height;
5189{
5190 int room; /* total number of lines available */
5191 int take; /* number of lines taken from other windows */
5192 int room_cmdline; /* lines available from cmdline */
5193 int run;
5194 frame_T *frp;
5195 int h;
5196 int room_reserved;
5197
5198 /* If the height already is the desired value, nothing to do. */
5199 if (curfrp->fr_height == height)
5200 return;
5201
5202 if (curfrp->fr_parent == NULL)
5203 {
5204 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205 if (height > ROWS_AVAIL)
5206 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (height > 0)
5208 frame_new_height(curfrp, height, FALSE, FALSE);
5209 }
5210 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5211 {
5212 /* Row of frames: Also need to resize frames left and right of this
5213 * one. First check for the minimal height of these. */
5214 h = frame_minheight(curfrp->fr_parent, NULL);
5215 if (height < h)
5216 height = h;
5217 frame_setheight(curfrp->fr_parent, height);
5218 }
5219 else
5220 {
5221 /*
5222 * Column of frames: try to change only frames in this column.
5223 */
5224#ifdef FEAT_VERTSPLIT
5225 /*
5226 * Do this twice:
5227 * 1: compute room available, if it's not enough try resizing the
5228 * containing frame.
5229 * 2: compute the room available and adjust the height to it.
5230 * Try not to reduce the height of a window with 'winfixheight' set.
5231 */
5232 for (run = 1; run <= 2; ++run)
5233#else
5234 for (;;)
5235#endif
5236 {
5237 room = 0;
5238 room_reserved = 0;
5239 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5240 frp = frp->fr_next)
5241 {
5242 if (frp != curfrp
5243 && frp->fr_win != NULL
5244 && frp->fr_win->w_p_wfh)
5245 room_reserved += frp->fr_height;
5246 room += frp->fr_height;
5247 if (frp != curfrp)
5248 room -= frame_minheight(frp, NULL);
5249 }
5250#ifdef FEAT_VERTSPLIT
5251 if (curfrp->fr_width != Columns)
5252 room_cmdline = 0;
5253 else
5254#endif
5255 {
5256 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5257 + lastwin->w_height + lastwin->w_status_height);
5258 if (room_cmdline < 0)
5259 room_cmdline = 0;
5260 }
5261
5262 if (height <= room + room_cmdline)
5263 break;
5264#ifdef FEAT_VERTSPLIT
5265 if (run == 2 || curfrp->fr_width == Columns)
5266#endif
5267 {
5268 if (height > room + room_cmdline)
5269 height = room + room_cmdline;
5270 break;
5271 }
5272#ifdef FEAT_VERTSPLIT
5273 frame_setheight(curfrp->fr_parent, height
5274 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5275#endif
5276 /*NOTREACHED*/
5277 }
5278
5279 /*
5280 * Compute the number of lines we will take from others frames (can be
5281 * negative!).
5282 */
5283 take = height - curfrp->fr_height;
5284
5285 /* If there is not enough room, also reduce the height of a window
5286 * with 'winfixheight' set. */
5287 if (height > room + room_cmdline - room_reserved)
5288 room_reserved = room + room_cmdline - height;
5289 /* If there is only a 'winfixheight' window and making the
5290 * window smaller, need to make the other window taller. */
5291 if (take < 0 && room - curfrp->fr_height < room_reserved)
5292 room_reserved = 0;
5293
5294 if (take > 0 && room_cmdline > 0)
5295 {
5296 /* use lines from cmdline first */
5297 if (take < room_cmdline)
5298 room_cmdline = take;
5299 take -= room_cmdline;
5300 topframe->fr_height += room_cmdline;
5301 }
5302
5303 /*
5304 * set the current frame to the new height
5305 */
5306 frame_new_height(curfrp, height, FALSE, FALSE);
5307
5308 /*
5309 * First take lines from the frames after the current frame. If
5310 * that is not enough, takes lines from frames above the current
5311 * frame.
5312 */
5313 for (run = 0; run < 2; ++run)
5314 {
5315 if (run == 0)
5316 frp = curfrp->fr_next; /* 1st run: start with next window */
5317 else
5318 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5319 while (frp != NULL && take != 0)
5320 {
5321 h = frame_minheight(frp, NULL);
5322 if (room_reserved > 0
5323 && frp->fr_win != NULL
5324 && frp->fr_win->w_p_wfh)
5325 {
5326 if (room_reserved >= frp->fr_height)
5327 room_reserved -= frp->fr_height;
5328 else
5329 {
5330 if (frp->fr_height - room_reserved > take)
5331 room_reserved = frp->fr_height - take;
5332 take -= frp->fr_height - room_reserved;
5333 frame_new_height(frp, room_reserved, FALSE, FALSE);
5334 room_reserved = 0;
5335 }
5336 }
5337 else
5338 {
5339 if (frp->fr_height - take < h)
5340 {
5341 take -= frp->fr_height - h;
5342 frame_new_height(frp, h, FALSE, FALSE);
5343 }
5344 else
5345 {
5346 frame_new_height(frp, frp->fr_height - take,
5347 FALSE, FALSE);
5348 take = 0;
5349 }
5350 }
5351 if (run == 0)
5352 frp = frp->fr_next;
5353 else
5354 frp = frp->fr_prev;
5355 }
5356 }
5357 }
5358}
5359
5360#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5361/*
5362 * Set current window width and take care of repositioning other windows to
5363 * fit around it.
5364 */
5365 void
5366win_setwidth(width)
5367 int width;
5368{
5369 win_setwidth_win(width, curwin);
5370}
5371
5372 void
5373win_setwidth_win(width, wp)
5374 int width;
5375 win_T *wp;
5376{
5377 /* Always keep current window at least one column wide, even when
5378 * 'winminwidth' is zero. */
5379 if (wp == curwin)
5380 {
5381 if (width < p_wmw)
5382 width = p_wmw;
5383 if (width == 0)
5384 width = 1;
5385 }
5386
5387 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5388
5389 /* recompute the window positions */
5390 (void)win_comp_pos();
5391
5392 redraw_all_later(NOT_VALID);
5393}
5394
5395/*
5396 * Set the width of a frame to "width" and take care that all frames and
5397 * windows inside it are resized. Also resize frames above and below if the
5398 * are in the same FR_ROW frame.
5399 *
5400 * Strategy is similar to frame_setheight().
5401 */
5402 static void
5403frame_setwidth(curfrp, width)
5404 frame_T *curfrp;
5405 int width;
5406{
5407 int room; /* total number of lines available */
5408 int take; /* number of lines taken from other windows */
5409 int run;
5410 frame_T *frp;
5411 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005412 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414 /* If the width already is the desired value, nothing to do. */
5415 if (curfrp->fr_width == width)
5416 return;
5417
5418 if (curfrp->fr_parent == NULL)
5419 /* topframe: can't change width */
5420 return;
5421
5422 if (curfrp->fr_parent->fr_layout == FR_COL)
5423 {
5424 /* Column of frames: Also need to resize frames above and below of
5425 * this one. First check for the minimal width of these. */
5426 w = frame_minwidth(curfrp->fr_parent, NULL);
5427 if (width < w)
5428 width = w;
5429 frame_setwidth(curfrp->fr_parent, width);
5430 }
5431 else
5432 {
5433 /*
5434 * Row of frames: try to change only frames in this row.
5435 *
5436 * Do this twice:
5437 * 1: compute room available, if it's not enough try resizing the
5438 * containing frame.
5439 * 2: compute the room available and adjust the width to it.
5440 */
5441 for (run = 1; run <= 2; ++run)
5442 {
5443 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005444 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5446 frp = frp->fr_next)
5447 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005448 if (frp != curfrp
5449 && frp->fr_win != NULL
5450 && frp->fr_win->w_p_wfw)
5451 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 room += frp->fr_width;
5453 if (frp != curfrp)
5454 room -= frame_minwidth(frp, NULL);
5455 }
5456
5457 if (width <= room)
5458 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005459 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 if (width > room)
5462 width = room;
5463 break;
5464 }
5465 frame_setwidth(curfrp->fr_parent, width
5466 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5467 }
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 /*
5470 * Compute the number of lines we will take from others frames (can be
5471 * negative!).
5472 */
5473 take = width - curfrp->fr_width;
5474
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005475 /* If there is not enough room, also reduce the width of a window
5476 * with 'winfixwidth' set. */
5477 if (width > room - room_reserved)
5478 room_reserved = room - width;
5479 /* If there is only a 'winfixwidth' window and making the
5480 * window smaller, need to make the other window narrower. */
5481 if (take < 0 && room - curfrp->fr_width < room_reserved)
5482 room_reserved = 0;
5483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 /*
5485 * set the current frame to the new width
5486 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005487 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
5489 /*
5490 * First take lines from the frames right of the current frame. If
5491 * that is not enough, takes lines from frames left of the current
5492 * frame.
5493 */
5494 for (run = 0; run < 2; ++run)
5495 {
5496 if (run == 0)
5497 frp = curfrp->fr_next; /* 1st run: start with next window */
5498 else
5499 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5500 while (frp != NULL && take != 0)
5501 {
5502 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005503 if (room_reserved > 0
5504 && frp->fr_win != NULL
5505 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005507 if (room_reserved >= frp->fr_width)
5508 room_reserved -= frp->fr_width;
5509 else
5510 {
5511 if (frp->fr_width - room_reserved > take)
5512 room_reserved = frp->fr_width - take;
5513 take -= frp->fr_width - room_reserved;
5514 frame_new_width(frp, room_reserved, FALSE, FALSE);
5515 room_reserved = 0;
5516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518 else
5519 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005520 if (frp->fr_width - take < w)
5521 {
5522 take -= frp->fr_width - w;
5523 frame_new_width(frp, w, FALSE, FALSE);
5524 }
5525 else
5526 {
5527 frame_new_width(frp, frp->fr_width - take,
5528 FALSE, FALSE);
5529 take = 0;
5530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532 if (run == 0)
5533 frp = frp->fr_next;
5534 else
5535 frp = frp->fr_prev;
5536 }
5537 }
5538 }
5539}
5540#endif /* FEAT_VERTSPLIT */
5541
5542/*
5543 * Check 'winminheight' for a valid value.
5544 */
5545 void
5546win_setminheight()
5547{
5548 int room;
5549 int first = TRUE;
5550 win_T *wp;
5551
5552 /* loop until there is a 'winminheight' that is possible */
5553 while (p_wmh > 0)
5554 {
5555 /* TODO: handle vertical splits */
5556 room = -p_wh;
5557 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5558 room += wp->w_height - p_wmh;
5559 if (room >= 0)
5560 break;
5561 --p_wmh;
5562 if (first)
5563 {
5564 EMSG(_(e_noroom));
5565 first = FALSE;
5566 }
5567 }
5568}
5569
5570#ifdef FEAT_MOUSE
5571
5572/*
5573 * Status line of dragwin is dragged "offset" lines down (negative is up).
5574 */
5575 void
5576win_drag_status_line(dragwin, offset)
5577 win_T *dragwin;
5578 int offset;
5579{
5580 frame_T *curfr;
5581 frame_T *fr;
5582 int room;
5583 int row;
5584 int up; /* if TRUE, drag status line up, otherwise down */
5585 int n;
5586
5587 fr = dragwin->w_frame;
5588 curfr = fr;
5589 if (fr != topframe) /* more than one window */
5590 {
5591 fr = fr->fr_parent;
5592 /* When the parent frame is not a column of frames, its parent should
5593 * be. */
5594 if (fr->fr_layout != FR_COL)
5595 {
5596 curfr = fr;
5597 if (fr != topframe) /* only a row of windows, may drag statusline */
5598 fr = fr->fr_parent;
5599 }
5600 }
5601
5602 /* If this is the last frame in a column, may want to resize the parent
5603 * frame instead (go two up to skip a row of frames). */
5604 while (curfr != topframe && curfr->fr_next == NULL)
5605 {
5606 if (fr != topframe)
5607 fr = fr->fr_parent;
5608 curfr = fr;
5609 if (fr != topframe)
5610 fr = fr->fr_parent;
5611 }
5612
5613 if (offset < 0) /* drag up */
5614 {
5615 up = TRUE;
5616 offset = -offset;
5617 /* sum up the room of the current frame and above it */
5618 if (fr == curfr)
5619 {
5620 /* only one window */
5621 room = fr->fr_height - frame_minheight(fr, NULL);
5622 }
5623 else
5624 {
5625 room = 0;
5626 for (fr = fr->fr_child; ; fr = fr->fr_next)
5627 {
5628 room += fr->fr_height - frame_minheight(fr, NULL);
5629 if (fr == curfr)
5630 break;
5631 }
5632 }
5633 fr = curfr->fr_next; /* put fr at frame that grows */
5634 }
5635 else /* drag down */
5636 {
5637 up = FALSE;
5638 /*
5639 * Only dragging the last status line can reduce p_ch.
5640 */
5641 room = Rows - cmdline_row;
5642 if (curfr->fr_next == NULL)
5643 room -= 1;
5644 else
5645 room -= p_ch;
5646 if (room < 0)
5647 room = 0;
5648 /* sum up the room of frames below of the current one */
5649 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5650 room += fr->fr_height - frame_minheight(fr, NULL);
5651 fr = curfr; /* put fr at window that grows */
5652 }
5653
5654 if (room < offset) /* Not enough room */
5655 offset = room; /* Move as far as we can */
5656 if (offset <= 0)
5657 return;
5658
5659 /*
5660 * Grow frame fr by "offset" lines.
5661 * Doesn't happen when dragging the last status line up.
5662 */
5663 if (fr != NULL)
5664 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5665
5666 if (up)
5667 fr = curfr; /* current frame gets smaller */
5668 else
5669 fr = curfr->fr_next; /* next frame gets smaller */
5670
5671 /*
5672 * Now make the other frames smaller.
5673 */
5674 while (fr != NULL && offset > 0)
5675 {
5676 n = frame_minheight(fr, NULL);
5677 if (fr->fr_height - offset <= n)
5678 {
5679 offset -= fr->fr_height - n;
5680 frame_new_height(fr, n, !up, FALSE);
5681 }
5682 else
5683 {
5684 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5685 break;
5686 }
5687 if (up)
5688 fr = fr->fr_prev;
5689 else
5690 fr = fr->fr_next;
5691 }
5692 row = win_comp_pos();
5693 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5694 cmdline_row = row;
5695 p_ch = Rows - cmdline_row;
5696 if (p_ch < 1)
5697 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005698 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005699 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 showmode();
5701}
5702
5703#ifdef FEAT_VERTSPLIT
5704/*
5705 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5706 */
5707 void
5708win_drag_vsep_line(dragwin, offset)
5709 win_T *dragwin;
5710 int offset;
5711{
5712 frame_T *curfr;
5713 frame_T *fr;
5714 int room;
5715 int left; /* if TRUE, drag separator line left, otherwise right */
5716 int n;
5717
5718 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005719 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 return;
5721 curfr = fr;
5722 fr = fr->fr_parent;
5723 /* When the parent frame is not a row of frames, its parent should be. */
5724 if (fr->fr_layout != FR_ROW)
5725 {
5726 if (fr == topframe) /* only a column of windows (cannot happen?) */
5727 return;
5728 curfr = fr;
5729 fr = fr->fr_parent;
5730 }
5731
5732 /* If this is the last frame in a row, may want to resize a parent
5733 * frame instead. */
5734 while (curfr->fr_next == NULL)
5735 {
5736 if (fr == topframe)
5737 break;
5738 curfr = fr;
5739 fr = fr->fr_parent;
5740 if (fr != topframe)
5741 {
5742 curfr = fr;
5743 fr = fr->fr_parent;
5744 }
5745 }
5746
5747 if (offset < 0) /* drag left */
5748 {
5749 left = TRUE;
5750 offset = -offset;
5751 /* sum up the room of the current frame and left of it */
5752 room = 0;
5753 for (fr = fr->fr_child; ; fr = fr->fr_next)
5754 {
5755 room += fr->fr_width - frame_minwidth(fr, NULL);
5756 if (fr == curfr)
5757 break;
5758 }
5759 fr = curfr->fr_next; /* put fr at frame that grows */
5760 }
5761 else /* drag right */
5762 {
5763 left = FALSE;
5764 /* sum up the room of frames right of the current one */
5765 room = 0;
5766 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5767 room += fr->fr_width - frame_minwidth(fr, NULL);
5768 fr = curfr; /* put fr at window that grows */
5769 }
5770
5771 if (room < offset) /* Not enough room */
5772 offset = room; /* Move as far as we can */
5773 if (offset <= 0) /* No room at all, quit. */
5774 return;
5775
5776 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005777 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 /* shrink other frames: current and at the left or at the right */
5780 if (left)
5781 fr = curfr; /* current frame gets smaller */
5782 else
5783 fr = curfr->fr_next; /* next frame gets smaller */
5784
5785 while (fr != NULL && offset > 0)
5786 {
5787 n = frame_minwidth(fr, NULL);
5788 if (fr->fr_width - offset <= n)
5789 {
5790 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005791 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
5793 else
5794 {
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 break;
5797 }
5798 if (left)
5799 fr = fr->fr_prev;
5800 else
5801 fr = fr->fr_next;
5802 }
5803 (void)win_comp_pos();
5804 redraw_all_later(NOT_VALID);
5805}
5806#endif /* FEAT_VERTSPLIT */
5807#endif /* FEAT_MOUSE */
5808
5809#endif /* FEAT_WINDOWS */
5810
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005811#define FRACTION_MULT 16384L
5812
5813/*
5814 * Set wp->w_fraction for the current w_wrow and w_height.
5815 */
5816 static void
5817set_fraction(wp)
5818 win_T *wp;
5819{
5820 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005821 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005822}
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824/*
5825 * Set the height of a window.
5826 * This takes care of the things inside the window, not what happens to the
5827 * window position, the frame or to other windows.
5828 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005829 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830win_new_height(wp, height)
5831 win_T *wp;
5832 int height;
5833{
5834 linenr_T lnum;
5835 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005836 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 /* Don't want a negative height. Happens when splitting a tiny window.
5839 * Will equalize heights soon to fix it. */
5840 if (height < 0)
5841 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005842 if (wp->w_height == height)
5843 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005845 if (wp->w_height > 0)
5846 {
5847 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005848 /* w_wrow needs to be valid. When setting 'laststatus' this may
5849 * call win_new_height() recursively. */
5850 validate_cursor();
5851 if (wp->w_height != prev_height)
5852 return; /* Recursive call already changed the size, bail out here
5853 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005854 if (wp->w_wrow != wp->w_prev_fraction_row)
5855 set_fraction(wp);
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 wp->w_height = height;
5859 wp->w_skipcol = 0;
5860
5861 /* Don't change w_topline when height is zero. Don't set w_topline when
5862 * 'scrollbind' is set and this isn't the current window. */
5863 if (height > 0
5864#ifdef FEAT_SCROLLBIND
5865 && (!wp->w_p_scb || wp == curwin)
5866#endif
5867 )
5868 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005869 /*
5870 * Find a value for w_topline that shows the cursor at the same
5871 * relative position in the window as before (more or less).
5872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 lnum = wp->w_cursor.lnum;
5874 if (lnum < 1) /* can happen when starting up */
5875 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005876 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5877 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5879 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005880
5881 if (sline >= 0)
5882 {
5883 /* Make sure the whole cursor line is visible, if possible. */
5884 int rows = plines_win(wp, lnum, FALSE);
5885
5886 if (sline > wp->w_height - rows)
5887 {
5888 sline = wp->w_height - rows;
5889 wp->w_wrow -= rows - line_size;
5890 }
5891 }
5892
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (sline < 0)
5894 {
5895 /*
5896 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005897 * Make cursor line the first line in the window. If not enough
5898 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 */
5900 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005901 if (wp->w_wrow >= wp->w_height
5902 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5903 {
5904 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5905 --wp->w_wrow;
5906 while (wp->w_wrow >= wp->w_height)
5907 {
5908 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5909 + win_col_off2(wp);
5910 --wp->w_wrow;
5911 }
5912 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005913 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005915 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005917 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
5919#ifdef FEAT_FOLDING
5920 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5921 if (lnum == 1)
5922 {
5923 /* first line in buffer is folded */
5924 line_size = 1;
5925 --sline;
5926 break;
5927 }
5928#endif
5929 --lnum;
5930#ifdef FEAT_DIFF
5931 if (lnum == wp->w_topline)
5932 line_size = plines_win_nofill(wp, lnum, TRUE)
5933 + wp->w_topfill;
5934 else
5935#endif
5936 line_size = plines_win(wp, lnum, TRUE);
5937 sline -= line_size;
5938 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (sline < 0)
5941 {
5942 /*
5943 * Line we want at top would go off top of screen. Use next
5944 * line instead.
5945 */
5946#ifdef FEAT_FOLDING
5947 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5948#endif
5949 lnum++;
5950 wp->w_wrow -= line_size + sline;
5951 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005952 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 /* First line of file reached, use that as topline. */
5955 lnum = 1;
5956 wp->w_wrow -= sline;
5957 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005958
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005959 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 }
5962
5963 if (wp == curwin)
5964 {
5965 if (p_so)
5966 update_topline();
5967 curs_columns(FALSE); /* validate w_wrow */
5968 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005969 if (prev_height > 0)
5970 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
5972 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005973 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#ifdef FEAT_WINDOWS
5975 wp->w_redr_status = TRUE;
5976#endif
5977 invalidate_botline_win(wp);
5978}
5979
5980#ifdef FEAT_VERTSPLIT
5981/*
5982 * Set the width of a window.
5983 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005984 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985win_new_width(wp, width)
5986 win_T *wp;
5987 int width;
5988{
5989 wp->w_width = width;
5990 wp->w_lines_valid = 0;
5991 changed_line_abv_curs_win(wp);
5992 invalidate_botline_win(wp);
5993 if (wp == curwin)
5994 {
5995 update_topline();
5996 curs_columns(TRUE); /* validate w_wrow */
5997 }
5998 redraw_win_later(wp, NOT_VALID);
5999 wp->w_redr_status = TRUE;
6000}
6001#endif
6002
6003 void
6004win_comp_scroll(wp)
6005 win_T *wp;
6006{
6007 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6008 if (wp->w_p_scr == 0)
6009 wp->w_p_scr = 1;
6010}
6011
6012/*
6013 * command_height: called whenever p_ch has been changed
6014 */
6015 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006016command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017{
6018#ifdef FEAT_WINDOWS
6019 int h;
6020 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006021 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006023 /* Use the value of p_ch that we remembered. This is needed for when the
6024 * GUI starts up, we can't be sure in what order things happen. And when
6025 * p_ch was changed in another tab page. */
6026 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 /* Find bottom frame with width of screen. */
6029 frp = lastwin->w_frame;
6030# ifdef FEAT_VERTSPLIT
6031 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6032 frp = frp->fr_parent;
6033# endif
6034
6035 /* Avoid changing the height of a window with 'winfixheight' set. */
6036 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6037 && frp->fr_win->w_p_wfh)
6038 frp = frp->fr_prev;
6039
6040 if (starting != NO_SCREEN)
6041 {
6042 cmdline_row = Rows - p_ch;
6043
6044 if (p_ch > old_p_ch) /* p_ch got bigger */
6045 {
6046 while (p_ch > old_p_ch)
6047 {
6048 if (frp == NULL)
6049 {
6050 EMSG(_(e_noroom));
6051 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006052 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 cmdline_row = Rows - p_ch;
6054 break;
6055 }
6056 h = frp->fr_height - frame_minheight(frp, NULL);
6057 if (h > p_ch - old_p_ch)
6058 h = p_ch - old_p_ch;
6059 old_p_ch += h;
6060 frame_add_height(frp, -h);
6061 frp = frp->fr_prev;
6062 }
6063
6064 /* Recompute window positions. */
6065 (void)win_comp_pos();
6066
6067 /* clear the lines added to cmdline */
6068 if (full_screen)
6069 screen_fill((int)(cmdline_row), (int)Rows, 0,
6070 (int)Columns, ' ', ' ', 0);
6071 msg_row = cmdline_row;
6072 redraw_cmdline = TRUE;
6073 return;
6074 }
6075
6076 if (msg_row < cmdline_row)
6077 msg_row = cmdline_row;
6078 redraw_cmdline = TRUE;
6079 }
6080 frame_add_height(frp, (int)(old_p_ch - p_ch));
6081
6082 /* Recompute window positions. */
6083 if (frp != lastwin->w_frame)
6084 (void)win_comp_pos();
6085#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006087 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088#endif
6089}
6090
6091#if defined(FEAT_WINDOWS) || defined(PROTO)
6092/*
6093 * Resize frame "frp" to be "n" lines higher (negative for less high).
6094 * Also resize the frames it is contained in.
6095 */
6096 static void
6097frame_add_height(frp, n)
6098 frame_T *frp;
6099 int n;
6100{
6101 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6102 for (;;)
6103 {
6104 frp = frp->fr_parent;
6105 if (frp == NULL)
6106 break;
6107 frp->fr_height += n;
6108 }
6109}
6110
6111/*
6112 * Add or remove a status line for the bottom window(s), according to the
6113 * value of 'laststatus'.
6114 */
6115 void
6116last_status(morewin)
6117 int morewin; /* pretend there are two or more windows */
6118{
6119 /* Don't make a difference between horizontal or vertical split. */
6120 last_status_rec(topframe, (p_ls == 2
6121 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6122}
6123
6124 static void
6125last_status_rec(fr, statusline)
6126 frame_T *fr;
6127 int statusline;
6128{
6129 frame_T *fp;
6130 win_T *wp;
6131
6132 if (fr->fr_layout == FR_LEAF)
6133 {
6134 wp = fr->fr_win;
6135 if (wp->w_status_height != 0 && !statusline)
6136 {
6137 /* remove status line */
6138 win_new_height(wp, wp->w_height + 1);
6139 wp->w_status_height = 0;
6140 comp_col();
6141 }
6142 else if (wp->w_status_height == 0 && statusline)
6143 {
6144 /* Find a frame to take a line from. */
6145 fp = fr;
6146 while (fp->fr_height <= frame_minheight(fp, NULL))
6147 {
6148 if (fp == topframe)
6149 {
6150 EMSG(_(e_noroom));
6151 return;
6152 }
6153 /* In a column of frames: go to frame above. If already at
6154 * the top or in a row of frames: go to parent. */
6155 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6156 fp = fp->fr_prev;
6157 else
6158 fp = fp->fr_parent;
6159 }
6160 wp->w_status_height = 1;
6161 if (fp != fr)
6162 {
6163 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6164 frame_fix_height(wp);
6165 (void)win_comp_pos();
6166 }
6167 else
6168 win_new_height(wp, wp->w_height - 1);
6169 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006170 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
6172 }
6173#ifdef FEAT_VERTSPLIT
6174 else if (fr->fr_layout == FR_ROW)
6175 {
6176 /* vertically split windows, set status line for each one */
6177 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6178 last_status_rec(fp, statusline);
6179 }
6180#endif
6181 else
6182 {
6183 /* horizontally split window, set status line for last one */
6184 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6185 ;
6186 last_status_rec(fp, statusline);
6187 }
6188}
6189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006191 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192 */
6193 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006194tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006196#ifdef FEAT_GUI_TABLINE
6197 /* When the GUI has the tabline then this always returns zero. */
6198 if (gui_use_tabline())
6199 return 0;
6200#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006201 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006202 {
6203 case 0: return 0;
6204 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6205 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006206 return 1;
6207}
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#endif /* FEAT_WINDOWS */
6210
6211#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6212/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006213 * Get the file name at the cursor.
6214 * If Visual mode is active, use the selected text if it's in one line.
6215 * Returns the name in allocated memory, NULL for failure.
6216 */
6217 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006218grab_file_name(count, file_lnum)
6219 long count;
6220 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006221{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006222 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6223
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006224 if (VIsual_active)
6225 {
6226 int len;
6227 char_u *ptr;
6228
6229 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6230 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006231 return find_file_name_in_path(ptr, len, options,
6232 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006233 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006234 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006235
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006236}
6237
6238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 * Return the file name under or after the cursor.
6240 *
6241 * The 'path' option is searched if the file name is not absolute.
6242 * The string returned has been alloc'ed and should be freed by the caller.
6243 * NULL is returned if the file name or file is not found.
6244 *
6245 * options:
6246 * FNAME_MESS give error messages
6247 * FNAME_EXP expand to path
6248 * FNAME_HYP check for hypertext link
6249 * FNAME_INCL apply "includeexpr"
6250 */
6251 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006252file_name_at_cursor(options, count, file_lnum)
6253 int options;
6254 long count;
6255 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6259 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261
6262/*
6263 * Return the name of the file under or after ptr[col].
6264 * Otherwise like file_name_at_cursor().
6265 */
6266 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006267file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 char_u *line;
6269 int col;
6270 int options;
6271 long count;
6272 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006273 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
6275 char_u *ptr;
6276 int len;
6277
6278 /*
6279 * search forward for what could be the start of a file name
6280 */
6281 ptr = line + col;
6282 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006283 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (*ptr == NUL) /* nothing found */
6285 {
6286 if (options & FNAME_MESS)
6287 EMSG(_("E446: No file name under cursor"));
6288 return NULL;
6289 }
6290
6291 /*
6292 * Search backward for first char of the file name.
6293 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6294 */
6295 while (ptr > line)
6296 {
6297#ifdef FEAT_MBYTE
6298 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6299 ptr -= len + 1;
6300 else
6301#endif
6302 if (vim_isfilec(ptr[-1])
6303 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6304 --ptr;
6305 else
6306 break;
6307 }
6308
6309 /*
6310 * Search forward for the last char of the file name.
6311 * Also allow "://" when ':' is not in 'isfname'.
6312 */
6313 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006314 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006316 {
6317 if (ptr[len] == '\\')
6318 /* Skip over the "\" in "\ ". */
6319 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#ifdef FEAT_MBYTE
6321 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006322 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 else
6324#endif
6325 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 /*
6329 * If there is trailing punctuation, remove it.
6330 * But don't remove "..", could be a directory name.
6331 */
6332 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6333 && ptr[len - 2] != '.')
6334 --len;
6335
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006336 if (file_lnum != NULL)
6337 {
6338 char_u *p;
6339
6340 /* Get the number after the file name and a separator character */
6341 p = ptr + len;
6342 p = skipwhite(p);
6343 if (*p != NUL)
6344 {
6345 if (!isdigit(*p))
6346 ++p; /* skip the separator */
6347 p = skipwhite(p);
6348 if (isdigit(*p))
6349 *file_lnum = (int)getdigits(&p);
6350 }
6351 }
6352
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6354}
6355
6356# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6357static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6358
6359 static char_u *
6360eval_includeexpr(ptr, len)
6361 char_u *ptr;
6362 int len;
6363{
6364 char_u *res;
6365
6366 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006367 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006368 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 set_vim_var_string(VV_FNAME, NULL, 0);
6370 return res;
6371}
6372#endif
6373
6374/*
6375 * Return the name of the file ptr[len] in 'path'.
6376 * Otherwise like file_name_at_cursor().
6377 */
6378 char_u *
6379find_file_name_in_path(ptr, len, options, count, rel_fname)
6380 char_u *ptr;
6381 int len;
6382 int options;
6383 long count;
6384 char_u *rel_fname; /* file we are searching relative to */
6385{
6386 char_u *file_name;
6387 int c;
6388# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6389 char_u *tofree = NULL;
6390
6391 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6392 {
6393 tofree = eval_includeexpr(ptr, len);
6394 if (tofree != NULL)
6395 {
6396 ptr = tofree;
6397 len = (int)STRLEN(ptr);
6398 }
6399 }
6400# endif
6401
6402 if (options & FNAME_EXP)
6403 {
6404 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6405 TRUE, rel_fname);
6406
6407# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6408 /*
6409 * If the file could not be found in a normal way, try applying
6410 * 'includeexpr' (unless done already).
6411 */
6412 if (file_name == NULL
6413 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6414 {
6415 tofree = eval_includeexpr(ptr, len);
6416 if (tofree != NULL)
6417 {
6418 ptr = tofree;
6419 len = (int)STRLEN(ptr);
6420 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6421 TRUE, rel_fname);
6422 }
6423 }
6424# endif
6425 if (file_name == NULL && (options & FNAME_MESS))
6426 {
6427 c = ptr[len];
6428 ptr[len] = NUL;
6429 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6430 ptr[len] = c;
6431 }
6432
6433 /* Repeat finding the file "count" times. This matters when it
6434 * appears several times in the path. */
6435 while (file_name != NULL && --count > 0)
6436 {
6437 vim_free(file_name);
6438 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6439 }
6440 }
6441 else
6442 file_name = vim_strnsave(ptr, len);
6443
6444# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6445 vim_free(tofree);
6446# endif
6447
6448 return file_name;
6449}
6450#endif /* FEAT_SEARCHPATH */
6451
6452/*
6453 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6454 * Also check for ":\\", which MS Internet Explorer accepts, return
6455 * URL_BACKSLASH.
6456 */
6457 static int
6458path_is_url(p)
6459 char_u *p;
6460{
6461 if (STRNCMP(p, "://", (size_t)3) == 0)
6462 return URL_SLASH;
6463 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6464 return URL_BACKSLASH;
6465 return 0;
6466}
6467
6468/*
6469 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6470 * Return URL_BACKSLASH for "name:\\".
6471 * Return zero otherwise.
6472 */
6473 int
6474path_with_url(fname)
6475 char_u *fname;
6476{
6477 char_u *p;
6478
6479 for (p = fname; isalpha(*p); ++p)
6480 ;
6481 return path_is_url(p);
6482}
6483
6484/*
6485 * Return TRUE if "name" is a full (absolute) path name or URL.
6486 */
6487 int
6488vim_isAbsName(name)
6489 char_u *name;
6490{
6491 return (path_with_url(name) != 0 || mch_isFullName(name));
6492}
6493
6494/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006495 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 *
6497 * return FAIL for failure, OK otherwise
6498 */
6499 int
6500vim_FullName(fname, buf, len, force)
6501 char_u *fname, *buf;
6502 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006503 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 int retval = OK;
6506 int url;
6507
6508 *buf = NUL;
6509 if (fname == NULL)
6510 return FAIL;
6511
6512 url = path_with_url(fname);
6513 if (!url)
6514 retval = mch_FullName(fname, buf, len, force);
6515 if (url || retval == FAIL)
6516 {
6517 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006518 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
6520#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6521 slash_adjust(buf);
6522#endif
6523 return retval;
6524}
6525
6526/*
6527 * Return the minimal number of rows that is needed on the screen to display
6528 * the current number of windows.
6529 */
6530 int
6531min_rows()
6532{
6533 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006534#ifdef FEAT_WINDOWS
6535 tabpage_T *tp;
6536 int n;
6537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538
6539 if (firstwin == NULL) /* not initialized yet */
6540 return MIN_LINES;
6541
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006543 total = 0;
6544 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6545 {
6546 n = frame_minheight(tp->tp_topframe, NULL);
6547 if (total < n)
6548 total = n;
6549 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006550 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006552 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006554 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 return total;
6556}
6557
6558/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006559 * Return TRUE if there is only one window (in the current tab page), not
6560 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006561 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 */
6563 int
6564only_one_window()
6565{
6566#ifdef FEAT_WINDOWS
6567 int count = 0;
6568 win_T *wp;
6569
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570 /* If there is another tab page there always is another window. */
6571 if (first_tabpage->tp_next != NULL)
6572 return FALSE;
6573
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006575 if (wp->w_buffer != NULL
6576 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577# ifdef FEAT_QUICKFIX
6578 || wp->w_p_pvw
6579# endif
6580 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006581# ifdef FEAT_AUTOCMD
6582 && wp != aucmd_win
6583# endif
6584 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 ++count;
6586 return (count <= 1);
6587#else
6588 return TRUE;
6589#endif
6590}
6591
6592#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6593/*
6594 * Correct the cursor line number in other windows. Used after changing the
6595 * current buffer, and before applying autocommands.
6596 * When "do_curwin" is TRUE, also check current window.
6597 */
6598 void
6599check_lnums(do_curwin)
6600 int do_curwin;
6601{
6602 win_T *wp;
6603
6604#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006605 tabpage_T *tp;
6606
6607 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6609#else
6610 wp = curwin;
6611 if (do_curwin)
6612#endif
6613 {
6614 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6615 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6616 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6617 wp->w_topline = curbuf->b_ml.ml_line_count;
6618 }
6619}
6620#endif
6621
6622#if defined(FEAT_WINDOWS) || defined(PROTO)
6623
6624/*
6625 * A snapshot of the window sizes, to restore them after closing the help
6626 * window.
6627 * Only these fields are used:
6628 * fr_layout
6629 * fr_width
6630 * fr_height
6631 * fr_next
6632 * fr_child
6633 * fr_win (only valid for the old curwin, NULL otherwise)
6634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636/*
6637 * Create a snapshot of the current frame sizes.
6638 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006639 void
6640make_snapshot(idx)
6641 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006643 clear_snapshot(curtab, idx);
6644 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
6647 static void
6648make_snapshot_rec(fr, frp)
6649 frame_T *fr;
6650 frame_T **frp;
6651{
6652 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6653 if (*frp == NULL)
6654 return;
6655 (*frp)->fr_layout = fr->fr_layout;
6656# ifdef FEAT_VERTSPLIT
6657 (*frp)->fr_width = fr->fr_width;
6658# endif
6659 (*frp)->fr_height = fr->fr_height;
6660 if (fr->fr_next != NULL)
6661 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6662 if (fr->fr_child != NULL)
6663 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6664 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6665 (*frp)->fr_win = curwin;
6666}
6667
6668/*
6669 * Remove any existing snapshot.
6670 */
6671 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006672clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006673 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006674 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006676 clear_snapshot_rec(tp->tp_snapshot[idx]);
6677 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
6680 static void
6681clear_snapshot_rec(fr)
6682 frame_T *fr;
6683{
6684 if (fr != NULL)
6685 {
6686 clear_snapshot_rec(fr->fr_next);
6687 clear_snapshot_rec(fr->fr_child);
6688 vim_free(fr);
6689 }
6690}
6691
6692/*
6693 * Restore a previously created snapshot, if there is any.
6694 * This is only done if the screen size didn't change and the window layout is
6695 * still the same.
6696 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006697 void
6698restore_snapshot(idx, close_curwin)
6699 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 int close_curwin; /* closing current window */
6701{
6702 win_T *wp;
6703
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006704 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006706 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006708 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6709 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006711 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 win_comp_pos();
6713 if (wp != NULL && close_curwin)
6714 win_goto(wp);
6715 redraw_all_later(CLEAR);
6716 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006717 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718}
6719
6720/*
6721 * Check if frames "sn" and "fr" have the same layout, same following frames
6722 * and same children.
6723 */
6724 static int
6725check_snapshot_rec(sn, fr)
6726 frame_T *sn;
6727 frame_T *fr;
6728{
6729 if (sn->fr_layout != fr->fr_layout
6730 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6731 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6732 || (sn->fr_next != NULL
6733 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6734 || (sn->fr_child != NULL
6735 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6736 return FAIL;
6737 return OK;
6738}
6739
6740/*
6741 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6742 * following frames and children.
6743 * Returns a pointer to the old current window, or NULL.
6744 */
6745 static win_T *
6746restore_snapshot_rec(sn, fr)
6747 frame_T *sn;
6748 frame_T *fr;
6749{
6750 win_T *wp = NULL;
6751 win_T *wp2;
6752
6753 fr->fr_height = sn->fr_height;
6754# ifdef FEAT_VERTSPLIT
6755 fr->fr_width = sn->fr_width;
6756# endif
6757 if (fr->fr_layout == FR_LEAF)
6758 {
6759 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6760# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006761 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762# endif
6763 wp = sn->fr_win;
6764 }
6765 if (sn->fr_next != NULL)
6766 {
6767 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6768 if (wp2 != NULL)
6769 wp = wp2;
6770 }
6771 if (sn->fr_child != NULL)
6772 {
6773 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6774 if (wp2 != NULL)
6775 wp = wp2;
6776 }
6777 return wp;
6778}
6779
6780#endif
6781
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006782#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6783 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006784/*
6785 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006786 * restore_win() MUST be called to undo, also when FAIL is returned.
6787 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006788 * When "no_display" is TRUE the display won't be affected, no redraw is
6789 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006790 * Returns FAIL if switching to "win" failed.
6791 */
6792 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006793switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006794 win_T **save_curwin UNUSED;
6795 tabpage_T **save_curtab UNUSED;
6796 win_T *win UNUSED;
6797 tabpage_T *tp UNUSED;
6798 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006799{
6800# ifdef FEAT_AUTOCMD
6801 block_autocmds();
6802# endif
6803# ifdef FEAT_WINDOWS
6804 *save_curwin = curwin;
6805 if (tp != NULL)
6806 {
6807 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006808 if (no_display)
6809 {
6810 curtab->tp_firstwin = firstwin;
6811 curtab->tp_lastwin = lastwin;
6812 curtab = tp;
6813 firstwin = curtab->tp_firstwin;
6814 lastwin = curtab->tp_lastwin;
6815 }
6816 else
6817 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006818 }
6819 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006820 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006821 curwin = win;
6822 curbuf = curwin->w_buffer;
6823# endif
6824 return OK;
6825}
6826
6827/*
6828 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006829 * When "no_display" is TRUE the display won't be affected, no redraw is
6830 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006831 */
6832 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006833restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006834 win_T *save_curwin UNUSED;
6835 tabpage_T *save_curtab UNUSED;
6836 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006837{
6838# ifdef FEAT_WINDOWS
6839 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006840 {
6841 if (no_display)
6842 {
6843 curtab->tp_firstwin = firstwin;
6844 curtab->tp_lastwin = lastwin;
6845 curtab = save_curtab;
6846 firstwin = curtab->tp_firstwin;
6847 lastwin = curtab->tp_lastwin;
6848 }
6849 else
6850 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6851 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006852 if (win_valid(save_curwin))
6853 {
6854 curwin = save_curwin;
6855 curbuf = curwin->w_buffer;
6856 }
6857# endif
6858# ifdef FEAT_AUTOCMD
6859 unblock_autocmds();
6860# endif
6861}
6862
6863/*
6864 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6865 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6866 */
6867 void
6868switch_buffer(save_curbuf, buf)
6869 buf_T *buf;
6870 buf_T **save_curbuf;
6871{
6872# ifdef FEAT_AUTOCMD
6873 block_autocmds();
6874# endif
6875 *save_curbuf = curbuf;
6876 --curbuf->b_nwindows;
6877 curbuf = buf;
6878 curwin->w_buffer = buf;
6879 ++curbuf->b_nwindows;
6880}
6881
6882/*
6883 * Restore the current buffer after using switch_buffer().
6884 */
6885 void
6886restore_buffer(save_curbuf)
6887 buf_T *save_curbuf;
6888{
6889# ifdef FEAT_AUTOCMD
6890 unblock_autocmds();
6891# endif
6892 /* Check for valid buffer, just in case. */
6893 if (buf_valid(save_curbuf))
6894 {
6895 --curbuf->b_nwindows;
6896 curwin->w_buffer = save_curbuf;
6897 curbuf = save_curbuf;
6898 ++curbuf->b_nwindows;
6899 }
6900}
6901#endif
6902
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6904/*
6905 * Return TRUE if there is any vertically split window.
6906 */
6907 int
6908win_hasvertsplit()
6909{
6910 frame_T *fr;
6911
6912 if (topframe->fr_layout == FR_ROW)
6913 return TRUE;
6914
6915 if (topframe->fr_layout == FR_COL)
6916 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6917 if (fr->fr_layout == FR_ROW)
6918 return TRUE;
6919
6920 return FALSE;
6921}
6922#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006923
6924#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6925/*
6926 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006927 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006928 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6929 * If no particular ID is desired, -1 must be specified for 'id'.
6930 * Return ID of added match, -1 on failure.
6931 */
6932 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006933match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006934 win_T *wp;
6935 char_u *grp;
6936 char_u *pat;
6937 int prio;
6938 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006939 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006940{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006941 matchitem_T *cur;
6942 matchitem_T *prev;
6943 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006944 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006945 regprog_T *regprog = NULL;
6946 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006947
Bram Moolenaarb3414592014-06-17 17:48:32 +02006948 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006949 return -1;
6950 if (id < -1 || id == 0)
6951 {
6952 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6953 return -1;
6954 }
6955 if (id != -1)
6956 {
6957 cur = wp->w_match_head;
6958 while (cur != NULL)
6959 {
6960 if (cur->id == id)
6961 {
6962 EMSGN("E801: ID already taken: %ld", id);
6963 return -1;
6964 }
6965 cur = cur->next;
6966 }
6967 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006968 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006969 {
6970 EMSG2(_(e_nogroup), grp);
6971 return -1;
6972 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006973 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006974 {
6975 EMSG2(_(e_invarg2), pat);
6976 return -1;
6977 }
6978
6979 /* Find available match ID. */
6980 while (id == -1)
6981 {
6982 cur = wp->w_match_head;
6983 while (cur != NULL && cur->id != wp->w_next_match_id)
6984 cur = cur->next;
6985 if (cur == NULL)
6986 id = wp->w_next_match_id;
6987 wp->w_next_match_id++;
6988 }
6989
6990 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006991 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006992 m->id = id;
6993 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006994 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006995 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006996 m->match.regprog = regprog;
6997 m->match.rmm_ic = FALSE;
6998 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006999
Bram Moolenaarb3414592014-06-17 17:48:32 +02007000 /* Set up position matches */
7001 if (pos_list != NULL)
7002 {
7003 linenr_T toplnum = 0;
7004 linenr_T botlnum = 0;
7005 listitem_T *li;
7006 int i;
7007
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007008 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007009 i++, li = li->li_next)
7010 {
7011 linenr_T lnum = 0;
7012 colnr_T col = 0;
7013 int len = 1;
7014 list_T *subl;
7015 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007016 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007017
Bram Moolenaarb3414592014-06-17 17:48:32 +02007018 if (li->li_tv.v_type == VAR_LIST)
7019 {
7020 subl = li->li_tv.vval.v_list;
7021 if (subl == NULL)
7022 goto fail;
7023 subli = subl->lv_first;
7024 if (subli == NULL)
7025 goto fail;
7026 lnum = get_tv_number_chk(&subli->li_tv, &error);
7027 if (error == TRUE)
7028 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007029 if (lnum == 0)
7030 {
7031 --i;
7032 continue;
7033 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007034 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007035 subli = subli->li_next;
7036 if (subli != NULL)
7037 {
7038 col = get_tv_number_chk(&subli->li_tv, &error);
7039 if (error == TRUE)
7040 goto fail;
7041 subli = subli->li_next;
7042 if (subli != NULL)
7043 {
7044 len = get_tv_number_chk(&subli->li_tv, &error);
7045 if (error == TRUE)
7046 goto fail;
7047 }
7048 }
7049 m->pos.pos[i].col = col;
7050 m->pos.pos[i].len = len;
7051 }
7052 else if (li->li_tv.v_type == VAR_NUMBER)
7053 {
7054 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007055 {
7056 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007057 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02007058 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007059 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
7060 m->pos.pos[i].col = 0;
7061 m->pos.pos[i].len = 0;
7062 }
7063 else
7064 {
7065 EMSG(_("List or number required"));
7066 goto fail;
7067 }
7068 if (toplnum == 0 || lnum < toplnum)
7069 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02007070 if (botlnum == 0 || lnum >= botlnum)
7071 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007072 }
7073
7074 /* Calculate top and bottom lines for redrawing area */
7075 if (toplnum != 0)
7076 {
7077 if (wp->w_buffer->b_mod_set)
7078 {
7079 if (wp->w_buffer->b_mod_top > toplnum)
7080 wp->w_buffer->b_mod_top = toplnum;
7081 if (wp->w_buffer->b_mod_bot < botlnum)
7082 wp->w_buffer->b_mod_bot = botlnum;
7083 }
7084 else
7085 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007086 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007087 wp->w_buffer->b_mod_top = toplnum;
7088 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007089 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007090 }
7091 m->pos.toplnum = toplnum;
7092 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007093 rtype = VALID;
7094 }
7095 }
7096
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007097 /* Insert new match. The match list is in ascending order with regard to
7098 * the match priorities. */
7099 cur = wp->w_match_head;
7100 prev = cur;
7101 while (cur != NULL && prio >= cur->priority)
7102 {
7103 prev = cur;
7104 cur = cur->next;
7105 }
7106 if (cur == prev)
7107 wp->w_match_head = m;
7108 else
7109 prev->next = m;
7110 m->next = cur;
7111
Bram Moolenaarb3414592014-06-17 17:48:32 +02007112 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007113 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007114
7115fail:
7116 vim_free(m);
7117 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007118}
7119
7120/*
7121 * Delete match with ID 'id' in the match list of window 'wp'.
7122 * Print error messages if 'perr' is TRUE.
7123 */
7124 int
7125match_delete(wp, id, perr)
7126 win_T *wp;
7127 int id;
7128 int perr;
7129{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007130 matchitem_T *cur = wp->w_match_head;
7131 matchitem_T *prev = cur;
7132 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007133
7134 if (id < 1)
7135 {
7136 if (perr == TRUE)
7137 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7138 id);
7139 return -1;
7140 }
7141 while (cur != NULL && cur->id != id)
7142 {
7143 prev = cur;
7144 cur = cur->next;
7145 }
7146 if (cur == NULL)
7147 {
7148 if (perr == TRUE)
7149 EMSGN("E803: ID not found: %ld", id);
7150 return -1;
7151 }
7152 if (cur == prev)
7153 wp->w_match_head = cur->next;
7154 else
7155 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007156 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007157 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007158 if (cur->pos.toplnum != 0)
7159 {
7160 if (wp->w_buffer->b_mod_set)
7161 {
7162 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7163 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7164 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7165 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7166 }
7167 else
7168 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007169 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007170 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7171 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007172 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007173 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007174 rtype = VALID;
7175 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007176 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007177 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007178 return 0;
7179}
7180
7181/*
7182 * Delete all matches in the match list of window 'wp'.
7183 */
7184 void
7185clear_matches(wp)
7186 win_T *wp;
7187{
7188 matchitem_T *m;
7189
7190 while (wp->w_match_head != NULL)
7191 {
7192 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007193 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007194 vim_free(wp->w_match_head->pattern);
7195 vim_free(wp->w_match_head);
7196 wp->w_match_head = m;
7197 }
7198 redraw_later(SOME_VALID);
7199}
7200
7201/*
7202 * Get match from ID 'id' in window 'wp'.
7203 * Return NULL if match not found.
7204 */
7205 matchitem_T *
7206get_match(wp, id)
7207 win_T *wp;
7208 int id;
7209{
7210 matchitem_T *cur = wp->w_match_head;
7211
7212 while (cur != NULL && cur->id != id)
7213 cur = cur->next;
7214 return cur;
7215}
7216#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007217
7218#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7219 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007220get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007221{
7222 int i = 1;
7223 win_T *w;
7224
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007225 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007226 ++i;
7227
7228 if (w == NULL)
7229 return 0;
7230 else
7231 return i;
7232}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007233
7234 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007235get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007236{
7237 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007238# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007239 tabpage_T *t;
7240
7241 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7242 ++i;
7243
7244 if (t == NULL)
7245 return 0;
7246 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007247# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007248 return i;
7249}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007250#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007251
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007252#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007253/*
7254 * Return TRUE if "topfrp" and its children are at the right height.
7255 */
7256 static int
7257frame_check_height(topfrp, height)
7258 frame_T *topfrp;
7259 int height;
7260{
7261 frame_T *frp;
7262
7263 if (topfrp->fr_height != height)
7264 return FALSE;
7265
7266 if (topfrp->fr_layout == FR_ROW)
7267 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7268 if (frp->fr_height != height)
7269 return FALSE;
7270
7271 return TRUE;
7272}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007273#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007274
7275#ifdef FEAT_VERTSPLIT
7276/*
7277 * Return TRUE if "topfrp" and its children are at the right width.
7278 */
7279 static int
7280frame_check_width(topfrp, width)
7281 frame_T *topfrp;
7282 int width;
7283{
7284 frame_T *frp;
7285
7286 if (topfrp->fr_width != width)
7287 return FALSE;
7288
7289 if (topfrp->fr_layout == FR_COL)
7290 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7291 if (frp->fr_width != width)
7292 return FALSE;
7293
7294 return TRUE;
7295}
7296#endif
7297