blob: 1cf861b476bb555c3c0f472e6ae6802752a7faaa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020026static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020047static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
48static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020051static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaarf740b292006-02-16 22:11:02 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020056# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void win_goto_ver __ARGS((int up, long count));
58static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static void frame_add_height __ARGS((frame_T *frp, int n));
61static void last_status_rec __ARGS((frame_T *fr, int statusline));
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000064static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
67static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68
Bram Moolenaarb893ac22013-06-26 14:04:47 +020069static int frame_check_height __ARGS((frame_T *topfrp, int height));
70#ifdef FEAT_VERTSPLIT
71static int frame_check_width __ARGS((frame_T *topfrp, int width));
72#endif
73
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000075
Bram Moolenaar746ebd32009-06-16 14:01:43 +000076static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010077static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79#define URL_SLASH 1 /* path_is_url() has found "://" */
80#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000082#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000085# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086#else
87# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000091
92static char *m_onlyone = N_("Already only one window");
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
98do_window(nchar, Prenum, xchar)
99 int nchar;
100 long Prenum;
101 int xchar; /* extra char from ":wincmd gx" or NUL */
102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000134#ifdef FEAT_QUICKFIX
135 /* When splitting the quickfix window open a new buffer in it,
136 * don't replicate the quickfix buffer. */
137 if (bt_quickfix(curbuf))
138 goto newwindow;
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_GUI
141 need_mouse_correct = TRUE;
142#endif
143 win_split((int)Prenum, 0);
144 break;
145
146#ifdef FEAT_VERTSPLIT
147/* split current window in two parts, vertically */
148 case Ctrl_V:
149 case 'v':
150 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCPY(cbuf, "split #");
171 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000172 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
173 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 do_cmdline_cmd(cbuf);
175 break;
176
177/* open new window */
178 case Ctrl_N:
179 case 'n':
180 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000182#ifdef FEAT_QUICKFIX
183newwindow:
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000186 /* window height */
187 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
191 if (nchar == 'v' || nchar == Ctrl_V)
192 STRCAT(cbuf, "v");
193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 STRCAT(cbuf, "new");
195 do_cmdline_cmd(cbuf);
196 break;
197
198/* quit current window */
199 case Ctrl_Q:
200 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 do_cmdline_cmd((char_u *)"close");
210 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 Moolenaar071d4272004-06-13 20:20:40 +0000238 do_cmdline_cmd((char_u *)"only");
239 break;
240
241/* cursor to next window with wrap around */
242 case Ctrl_W:
243 case 'w':
244/* cursor to previous window with wrap around */
245 case 'W':
246 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000247 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 beep_flush();
249 else
250 {
251 if (Prenum) /* go to specified window */
252 {
253 for (wp = firstwin; --Prenum > 0; )
254 {
255 if (wp->w_next == NULL)
256 break;
257 else
258 wp = wp->w_next;
259 }
260 }
261 else
262 {
263 if (nchar == 'W') /* go to previous window */
264 {
265 wp = curwin->w_prev;
266 if (wp == NULL)
267 wp = lastwin; /* wrap around */
268 }
269 else /* go to next window */
270 {
271 wp = curwin->w_next;
272 if (wp == NULL)
273 wp = firstwin; /* wrap around */
274 }
275 }
276 win_goto(wp);
277 }
278 break;
279
280/* cursor to window below */
281 case 'j':
282 case K_DOWN:
283 case Ctrl_J:
284 CHECK_CMDWIN
285#ifdef FEAT_VERTSPLIT
286 win_goto_ver(FALSE, Prenum1);
287#else
288 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
289 wp = wp->w_next)
290 ;
291 win_goto(wp);
292#endif
293 break;
294
295/* cursor to window above */
296 case 'k':
297 case K_UP:
298 case Ctrl_K:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(TRUE, Prenum1);
302#else
303 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
304 wp = wp->w_prev)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310#ifdef FEAT_VERTSPLIT
311/* cursor to left window */
312 case 'h':
313 case K_LEFT:
314 case Ctrl_H:
315 case K_BS:
316 CHECK_CMDWIN
317 win_goto_hor(TRUE, Prenum1);
318 break;
319
320/* cursor to right window */
321 case 'l':
322 case K_RIGHT:
323 case Ctrl_L:
324 CHECK_CMDWIN
325 win_goto_hor(FALSE, Prenum1);
326 break;
327#endif
328
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329/* move window to new tab page */
330 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000331 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 MSG(_(m_onlyone));
333 else
334 {
335 tabpage_T *oldtab = curtab;
336 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337
338 /* First create a new tab with the window, then go back to
339 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000340 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (win_new_tabpage((int)Prenum) == OK
342 && valid_tabpage(oldtab))
343 {
344 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200345 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 if (curwin == wp)
347 win_close(curwin, FALSE);
348 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200349 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 }
351 }
352 break;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* cursor to top-left window */
355 case 't':
356 case Ctrl_T:
357 win_goto(firstwin);
358 break;
359
360/* cursor to bottom-right window */
361 case 'b':
362 case Ctrl_B:
363 win_goto(lastwin);
364 break;
365
366/* cursor to last accessed (previous) window */
367 case 'p':
368 case Ctrl_P:
369 if (prevwin == NULL)
370 beep_flush();
371 else
372 win_goto(prevwin);
373 break;
374
375/* exchange current and next window */
376 case 'x':
377 case Ctrl_X:
378 CHECK_CMDWIN
379 win_exchange(Prenum);
380 break;
381
382/* rotate windows downwards */
383 case Ctrl_R:
384 case 'r':
385 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 win_rotate(FALSE, (int)Prenum1); /* downwards */
388 break;
389
390/* rotate windows upwards */
391 case 'R':
392 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 win_rotate(TRUE, (int)Prenum1); /* upwards */
395 break;
396
397/* move window to the very top/bottom/left/right */
398 case 'K':
399 case 'J':
400#ifdef FEAT_VERTSPLIT
401 case 'H':
402 case 'L':
403#endif
404 CHECK_CMDWIN
405 win_totop((int)Prenum,
406 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
407 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
408 break;
409
410/* make all windows the same height */
411 case '=':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_equal(NULL, FALSE, 'b');
416 break;
417
418/* increase current window height */
419 case '+':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(curwin->w_height + (int)Prenum1);
424 break;
425
426/* decrease current window height */
427 case '-':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setheight(curwin->w_height - (int)Prenum1);
432 break;
433
434/* set current window height */
435 case Ctrl__:
436 case '_':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(Prenum ? (int)Prenum : 9999);
441 break;
442
443#ifdef FEAT_VERTSPLIT
444/* increase current window width */
445 case '>':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(curwin->w_width + (int)Prenum1);
450 break;
451
452/* decrease current window width */
453 case '<':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setwidth(curwin->w_width - (int)Prenum1);
458 break;
459
460/* set current window width */
461 case '|':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
466 break;
467#endif
468
469/* jump to tag and split window if tag exists (in preview window) */
470#if defined(FEAT_QUICKFIX)
471 case '}':
472 CHECK_CMDWIN
473 if (Prenum)
474 g_do_tagpreview = Prenum;
475 else
476 g_do_tagpreview = p_pvh;
477 /*FALLTHROUGH*/
478#endif
479 case ']':
480 case Ctrl_RSB:
481 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
487
488 /* Execute the command right here, required when
489 * "wincmd ]" was used in a function. */
490 do_nv_ident(Ctrl_RSB, NUL);
491 break;
492
493#ifdef FEAT_SEARCHPATH
494/* edit file name under cursor in a new window */
495 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000498wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (ptr != NULL)
503 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000504# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 setpcmark();
508 if (win_split(0, 0) == OK)
509 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200510 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000511 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
512 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 if (nchar == 'F' && lnum >= 0)
514 {
515 curwin->w_cursor.lnum = lnum;
516 check_cursor_lnum();
517 beginline(BL_SOL | BL_FIX);
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 vim_free(ptr);
521 }
522 break;
523#endif
524
525#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000526/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 * new window -- webb
528 */
529 case 'i': /* Go to any match */
530 case Ctrl_I:
531 type = FIND_ANY;
532 /* FALLTHROUGH */
533 case 'd': /* Go to definition, using 'define' */
534 case Ctrl_D:
535 CHECK_CMDWIN
536 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
537 break;
538 find_pattern_in_path(ptr, 0, len, TRUE,
539 Prenum == 0 ? TRUE : FALSE, type,
540 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
541 curwin->w_set_curswant = TRUE;
542 break;
543#endif
544
Bram Moolenaar05159a02005-02-26 23:04:13 +0000545 case K_KENTER:
546 case CAR:
547#if defined(FEAT_QUICKFIX)
548 /*
549 * In a quickfix window a <CR> jumps to the error under the
550 * cursor in a new window.
551 */
552 if (bt_quickfix(curbuf))
553 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000554 sprintf((char *)cbuf, "split +%ld%s",
555 (long)curwin->w_cursor.lnum,
556 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000557 do_cmdline_cmd(cbuf);
558 }
559#endif
560 break;
561
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563/* CTRL-W g extended commands */
564 case 'g':
565 case Ctrl_G:
566 CHECK_CMDWIN
567#ifdef USE_ON_FLY_SCROLL
568 dont_scroll = TRUE; /* disallow scrolling here */
569#endif
570 ++no_mapping;
571 ++allow_keys; /* no mapping for xchar, but allow key codes */
572 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000573 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 --no_mapping;
576 --allow_keys;
577#ifdef FEAT_CMDL_INFO
578 (void)add_to_showcmd(xchar);
579#endif
580 switch (xchar)
581 {
582#if defined(FEAT_QUICKFIX)
583 case '}':
584 xchar = Ctrl_RSB;
585 if (Prenum)
586 g_do_tagpreview = Prenum;
587 else
588 g_do_tagpreview = p_pvh;
589 /*FALLTHROUGH*/
590#endif
591 case ']':
592 case Ctrl_RSB:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (Prenum)
595 postponed_split = Prenum;
596 else
597 postponed_split = -1;
598
599 /* Execute the command right here, required when
600 * "wincmd g}" was used in a function. */
601 do_nv_ident('g', xchar);
602 break;
603
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000604#ifdef FEAT_SEARCHPATH
605 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000606 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100607 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000608 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609 goto wingotofile;
610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 default:
612 beep_flush();
613 break;
614 }
615 break;
616
617 default: beep_flush();
618 break;
619 }
620}
621
622/*
623 * split the current window, implements CTRL-W s and :split
624 *
625 * "size" is the height or width for the new window, 0 to use half of current
626 * height or width.
627 *
628 * "flags":
629 * WSP_ROOM: require enough room for new window
630 * WSP_VERT: vertical split.
631 * WSP_TOP: open window at the top-left of the shell (help window).
632 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
633 * WSP_HELP: creating the help window, keep layout snapshot
634 *
635 * return FAIL for failure, OK otherwise
636 */
637 int
638win_split(size, flags)
639 int size;
640 int flags;
641{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000642 /* When the ":tab" modifier was used open a new tab page instead. */
643 if (may_open_tabpage() == OK)
644 return OK;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /* Add flags from ":vertical", ":topleft" and ":botright". */
647 flags |= cmdmod.split;
648 if ((flags & WSP_TOP) && (flags & WSP_BOT))
649 {
650 EMSG(_("E442: Can't split topleft and botright at the same time"));
651 return FAIL;
652 }
653
654 /* When creating the help window make a snapshot of the window layout.
655 * Otherwise clear the snapshot, it's now invalid. */
656 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000657 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000659 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 return win_split_ins(size, flags, NULL, 0);
662}
663
664/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100665 * When "new_wp" is NULL: split the current window in two.
666 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * top/left/right/bottom.
668 * return FAIL for failure, OK otherwise
669 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000670 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100671win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 int size;
673 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100674 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int dir;
676{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100677 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 win_T *oldwin;
679 int new_size = size;
680 int i;
681 int need_status = 0;
682 int do_equal = FALSE;
683 int needed;
684 int available;
685 int oldwin_height = 0;
686 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200687 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200689 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200690 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 if (flags & WSP_TOP)
693 oldwin = firstwin;
694 else if (flags & WSP_BOT)
695 oldwin = lastwin;
696 else
697 oldwin = curwin;
698
699 /* add a status line when p_ls == 1 and splitting the first window */
700 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
701 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100702 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 EMSG(_(e_noroom));
705 return FAIL;
706 }
707 need_status = STATUS_HEIGHT;
708 }
709
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000710#ifdef FEAT_GUI
711 /* May be needed for the scrollbars that are going to change. */
712 if (gui.in_use)
713 out_flush();
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_VERTSPLIT
717 if (flags & WSP_VERT)
718 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200719 int wmw1;
720 int minwidth;
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724 /*
725 * Check if we are able to split the current window and compute its
726 * width.
727 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200728 /* Current window requires at least 1 space. */
729 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
730 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200732 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200733 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200735 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200737 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200739 else if (p_ea)
740 {
741 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
742 prevfrp = oldwin->w_frame;
743 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
744 frp = frp->fr_parent)
745 {
746 if (frp->fr_layout == FR_ROW)
747 for (frp2 = frp->fr_child; frp2 != NULL;
748 frp2 = frp2->fr_next)
749 if (frp2 != prevfrp)
750 minwidth += frame_minwidth(frp2, NOWIN);
751 prevfrp = frp;
752 }
753 available = topframe->fr_width;
754 needed += minwidth;
755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200757 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200758 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
759 available = oldwin->w_frame->fr_width;
760 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200761 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100762 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
764 EMSG(_(e_noroom));
765 return FAIL;
766 }
767 if (new_size == 0)
768 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200769 if (new_size > available - minwidth - 1)
770 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200771 if (new_size < wmw1)
772 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /* if it doesn't fit in the current window, need win_equal() */
775 if (oldwin->w_width - new_size - 1 < p_wmw)
776 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000777
778 /* We don't like to take lines for the new window from a
779 * 'winfixwidth' window. Take them from a window to the left or right
780 * instead, if possible. */
781 if (oldwin->w_p_wfw)
782 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000783
784 /* Only make all windows the same width if one of them (except oldwin)
785 * is wider than one of the split windows. */
786 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
787 && oldwin->w_frame->fr_parent != NULL)
788 {
789 frp = oldwin->w_frame->fr_parent->fr_child;
790 while (frp != NULL)
791 {
792 if (frp->fr_win != oldwin && frp->fr_win != NULL
793 && (frp->fr_win->w_width > new_size
794 || frp->fr_win->w_width > oldwin->w_width
795 - new_size - STATUS_HEIGHT))
796 {
797 do_equal = TRUE;
798 break;
799 }
800 frp = frp->fr_next;
801 }
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804 else
805#endif
806 {
807 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /*
810 * Check if we are able to split the current window and compute its
811 * height.
812 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200813 /* Current window requires at least 1 space. */
814 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
815 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200818 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200820 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200822 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200824 else if (p_ea)
825 {
826 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
827 prevfrp = oldwin->w_frame;
828 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
829 frp = frp->fr_parent)
830 {
831 if (frp->fr_layout == FR_COL)
832 for (frp2 = frp->fr_child; frp2 != NULL;
833 frp2 = frp2->fr_next)
834 if (frp2 != prevfrp)
835 minheight += frame_minheight(frp2, NOWIN);
836 prevfrp = frp;
837 }
838 available = topframe->fr_height;
839 needed += minheight;
840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 else
842 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200843 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
844 available = oldwin->w_frame->fr_height;
845 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100847 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
849 EMSG(_(e_noroom));
850 return FAIL;
851 }
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 {
855 oldwin->w_status_height = STATUS_HEIGHT;
856 oldwin_height -= STATUS_HEIGHT;
857 }
858 if (new_size == 0)
859 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200860 if (new_size > available - minheight - STATUS_HEIGHT)
861 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200862 if (new_size < wmh1)
863 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 /* if it doesn't fit in the current window, need win_equal() */
866 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
867 do_equal = TRUE;
868
869 /* We don't like to take lines for the new window from a
870 * 'winfixheight' window. Take them from a window above or below
871 * instead, if possible. */
872 if (oldwin->w_p_wfh)
873 {
874 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
875 oldwin);
876 oldwin_height = oldwin->w_height;
877 if (need_status)
878 oldwin_height -= STATUS_HEIGHT;
879 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000880
881 /* Only make all windows the same height if one of them (except oldwin)
882 * is higher than one of the split windows. */
883 if (!do_equal && p_ea && size == 0
884#ifdef FEAT_VERTSPLIT
885 && *p_ead != 'h'
886#endif
887 && oldwin->w_frame->fr_parent != NULL)
888 {
889 frp = oldwin->w_frame->fr_parent->fr_child;
890 while (frp != NULL)
891 {
892 if (frp->fr_win != oldwin && frp->fr_win != NULL
893 && (frp->fr_win->w_height > new_size
894 || frp->fr_win->w_height > oldwin_height - new_size
895 - STATUS_HEIGHT))
896 {
897 do_equal = TRUE;
898 break;
899 }
900 frp = frp->fr_next;
901 }
902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
905 /*
906 * allocate new window structure and link it in the window list
907 */
908 if ((flags & WSP_TOP) == 0
909 && ((flags & WSP_BOT)
910 || (flags & WSP_BELOW)
911 || (!(flags & WSP_ABOVE)
912 && (
913#ifdef FEAT_VERTSPLIT
914 (flags & WSP_VERT) ? p_spr :
915#endif
916 p_sb))))
917 {
918 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100919 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000920 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 else
922 win_append(oldwin, wp);
923 }
924 else
925 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100926 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000927 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 win_append(oldwin->w_prev, wp);
930 }
931
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100932 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
934 if (wp == NULL)
935 return FAIL;
936
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000937 new_frame(wp);
938 if (wp->w_frame == NULL)
939 {
940 win_free(wp, NULL);
941 return FAIL;
942 }
943
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000944 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000945 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
948 /*
949 * Reorganise the tree of frames to insert the new window.
950 */
951 if (flags & (WSP_TOP | WSP_BOT))
952 {
953#ifdef FEAT_VERTSPLIT
954 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
955 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
956#else
957 if (topframe->fr_layout == FR_COL)
958#endif
959 {
960 curfrp = topframe->fr_child;
961 if (flags & WSP_BOT)
962 while (curfrp->fr_next != NULL)
963 curfrp = curfrp->fr_next;
964 }
965 else
966 curfrp = topframe;
967 before = (flags & WSP_TOP);
968 }
969 else
970 {
971 curfrp = oldwin->w_frame;
972 if (flags & WSP_BELOW)
973 before = FALSE;
974 else if (flags & WSP_ABOVE)
975 before = TRUE;
976 else
977#ifdef FEAT_VERTSPLIT
978 if (flags & WSP_VERT)
979 before = !p_spr;
980 else
981#endif
982 before = !p_sb;
983 }
984 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
985 {
986 /* Need to create a new frame in the tree to make a branch. */
987 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
988 *frp = *curfrp;
989 curfrp->fr_layout = layout;
990 frp->fr_parent = curfrp;
991 frp->fr_next = NULL;
992 frp->fr_prev = NULL;
993 curfrp->fr_child = frp;
994 curfrp->fr_win = NULL;
995 curfrp = frp;
996 if (frp->fr_win != NULL)
997 oldwin->w_frame = frp;
998 else
999 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1000 frp->fr_parent = curfrp;
1001 }
1002
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001003 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001004 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001006 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 frp->fr_parent = curfrp->fr_parent;
1008
1009 /* Insert the new frame at the right place in the frame list. */
1010 if (before)
1011 frame_insert(curfrp, frp);
1012 else
1013 frame_append(curfrp, frp);
1014
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001015 /* Set w_fraction now so that the cursor keeps the same relative
1016 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001017 if (oldwin->w_height > 0)
1018 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001019 wp->w_fraction = oldwin->w_fraction;
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_VERTSPLIT
1022 if (flags & WSP_VERT)
1023 {
1024 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (need_status)
1027 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001028 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 oldwin->w_status_height = need_status;
1030 }
1031 if (flags & (WSP_TOP | WSP_BOT))
1032 {
1033 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001034 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001035 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 wp->w_status_height = (p_ls > 0);
1037 }
1038 else
1039 {
1040 /* height and row of new window is same as current window */
1041 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001042 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 wp->w_status_height = oldwin->w_status_height;
1044 }
1045 frp->fr_height = curfrp->fr_height;
1046
1047 /* "new_size" of the current window goes to the new window, use
1048 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001049 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (before)
1051 wp->w_vsep_width = 1;
1052 else
1053 {
1054 wp->w_vsep_width = oldwin->w_vsep_width;
1055 oldwin->w_vsep_width = 1;
1056 }
1057 if (flags & (WSP_TOP | WSP_BOT))
1058 {
1059 if (flags & WSP_BOT)
1060 frame_add_vsep(curfrp);
1061 /* Set width of neighbor frame */
1062 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001063 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1064 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001067 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (before) /* new window left of current one */
1069 {
1070 wp->w_wincol = oldwin->w_wincol;
1071 oldwin->w_wincol += new_size + 1;
1072 }
1073 else /* new window right of current one */
1074 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1075 frame_fix_width(oldwin);
1076 frame_fix_width(wp);
1077 }
1078 else
1079#endif
1080 {
1081 /* width and column of new window is same as current window */
1082#ifdef FEAT_VERTSPLIT
1083 if (flags & (WSP_TOP | WSP_BOT))
1084 {
1085 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001086 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 wp->w_vsep_width = 0;
1088 }
1089 else
1090 {
1091 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001092 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 wp->w_vsep_width = oldwin->w_vsep_width;
1094 }
1095 frp->fr_width = curfrp->fr_width;
1096#endif
1097
1098 /* "new_size" of the current window goes to the new window, use
1099 * one row for the status line */
1100 win_new_height(wp, new_size);
1101 if (flags & (WSP_TOP | WSP_BOT))
1102 frame_new_height(curfrp, curfrp->fr_height
1103 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1104 else
1105 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1106 if (before) /* new window above current one */
1107 {
1108 wp->w_winrow = oldwin->w_winrow;
1109 wp->w_status_height = STATUS_HEIGHT;
1110 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1111 }
1112 else /* new window below current one */
1113 {
1114 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1115 wp->w_status_height = oldwin->w_status_height;
1116 oldwin->w_status_height = STATUS_HEIGHT;
1117 }
1118#ifdef FEAT_VERTSPLIT
1119 if (flags & WSP_BOT)
1120 frame_add_statusline(curfrp);
1121#endif
1122 frame_fix_height(wp);
1123 frame_fix_height(oldwin);
1124 }
1125
1126 if (flags & (WSP_TOP | WSP_BOT))
1127 (void)win_comp_pos();
1128
1129 /*
1130 * Both windows need redrawing
1131 */
1132 redraw_win_later(wp, NOT_VALID);
1133 wp->w_redr_status = TRUE;
1134 redraw_win_later(oldwin, NOT_VALID);
1135 oldwin->w_redr_status = TRUE;
1136
1137 if (need_status)
1138 {
1139 msg_row = Rows - 1;
1140 msg_col = sc_col;
1141 msg_clr_eos_force(); /* Old command/ruler may still be there */
1142 comp_col();
1143 msg_row = Rows - 1;
1144 msg_col = 0; /* put position back at start of line */
1145 }
1146
1147 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001148 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 */
1150 if (do_equal || dir != 0)
1151 win_equal(wp, TRUE,
1152#ifdef FEAT_VERTSPLIT
1153 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1154 : dir == 'h' ? 'b' :
1155#endif
1156 'v');
1157
1158 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1159 * size was given. */
1160#ifdef FEAT_VERTSPLIT
1161 if (flags & WSP_VERT)
1162 {
1163 i = p_wiw;
1164 if (size != 0)
1165 p_wiw = size;
1166
1167# ifdef FEAT_GUI
1168 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1169 if (gui.in_use)
1170 gui_init_which_components(NULL);
1171# endif
1172 }
1173 else
1174#endif
1175 {
1176 i = p_wh;
1177 if (size != 0)
1178 p_wh = size;
1179 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001180
1181 /*
1182 * make the new window the current window
1183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 win_enter(wp, FALSE);
1185#ifdef FEAT_VERTSPLIT
1186 if (flags & WSP_VERT)
1187 p_wiw = i;
1188 else
1189#endif
1190 p_wh = i;
1191
1192 return OK;
1193}
1194
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001195
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001196/*
1197 * Initialize window "newp" from window "oldp".
1198 * Used when splitting a window and when creating a new tab page.
1199 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001200 * WSP_NEWLOC may be specified in flags to prevent the location list from
1201 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001202 */
1203 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001204win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001205 win_T *newp;
1206 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001207 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001208{
1209 int i;
1210
1211 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001212#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001213 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001214#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001215 oldp->w_buffer->b_nwindows++;
1216 newp->w_cursor = oldp->w_cursor;
1217 newp->w_valid = 0;
1218 newp->w_curswant = oldp->w_curswant;
1219 newp->w_set_curswant = oldp->w_set_curswant;
1220 newp->w_topline = oldp->w_topline;
1221#ifdef FEAT_DIFF
1222 newp->w_topfill = oldp->w_topfill;
1223#endif
1224 newp->w_leftcol = oldp->w_leftcol;
1225 newp->w_pcmark = oldp->w_pcmark;
1226 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1227 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001228 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001229 newp->w_fraction = oldp->w_fraction;
1230 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1231#ifdef FEAT_JUMPLIST
1232 copy_jumplist(oldp, newp);
1233#endif
1234#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001235 if (flags & WSP_NEWLOC)
1236 {
1237 /* Don't copy the location list. */
1238 newp->w_llist = NULL;
1239 newp->w_llist_ref = NULL;
1240 }
1241 else
1242 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001243#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001244 newp->w_localdir = (oldp->w_localdir == NULL)
1245 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001246
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001247 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001248 for (i = 0; i < oldp->w_tagstacklen; i++)
1249 {
1250 newp->w_tagstack[i] = oldp->w_tagstack[i];
1251 if (newp->w_tagstack[i].tagname != NULL)
1252 newp->w_tagstack[i].tagname =
1253 vim_strsave(newp->w_tagstack[i].tagname);
1254 }
1255 newp->w_tagstackidx = oldp->w_tagstackidx;
1256 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001257#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001258 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001259#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001260
1261 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001262
Bram Moolenaara971b822011-09-14 14:43:25 +02001263#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001264 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001265#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001266}
1267
1268/*
1269 * Initialize window "newp" from window"old".
1270 * Only the essential things are copied.
1271 */
1272 static void
1273win_init_some(newp, oldp)
1274 win_T *newp;
1275 win_T *oldp;
1276{
1277 /* Use the same argument list. */
1278 newp->w_alist = oldp->w_alist;
1279 ++newp->w_alist->al_refcount;
1280 newp->w_arg_idx = oldp->w_arg_idx;
1281
1282 /* copy options from existing window */
1283 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001284}
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif /* FEAT_WINDOWS */
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288#if defined(FEAT_WINDOWS) || defined(PROTO)
1289/*
1290 * Check if "win" is a pointer to an existing window.
1291 */
1292 int
1293win_valid(win)
1294 win_T *win;
1295{
1296 win_T *wp;
1297
1298 if (win == NULL)
1299 return FALSE;
1300 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1301 if (wp == win)
1302 return TRUE;
1303 return FALSE;
1304}
1305
1306/*
1307 * Return the number of windows.
1308 */
1309 int
1310win_count()
1311{
1312 win_T *wp;
1313 int count = 0;
1314
1315 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1316 ++count;
1317 return count;
1318}
1319
1320/*
1321 * Make "count" windows on the screen.
1322 * Return actual number of windows on the screen.
1323 * Must be called when there is just one window, filling the whole screen
1324 * (excluding the command line).
1325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 int
1327make_windows(count, vertical)
1328 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001329 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330{
1331 int maxcount;
1332 int todo;
1333
1334#ifdef FEAT_VERTSPLIT
1335 if (vertical)
1336 {
1337 /* Each windows needs at least 'winminwidth' lines and a separator
1338 * column. */
1339 maxcount = (curwin->w_width + curwin->w_vsep_width
1340 - (p_wiw - p_wmw)) / (p_wmw + 1);
1341 }
1342 else
1343#endif
1344 {
1345 /* Each window needs at least 'winminheight' lines and a status line. */
1346 maxcount = (curwin->w_height + curwin->w_status_height
1347 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1348 }
1349
1350 if (maxcount < 2)
1351 maxcount = 2;
1352 if (count > maxcount)
1353 count = maxcount;
1354
1355 /*
1356 * add status line now, otherwise first window will be too big
1357 */
1358 if (count > 1)
1359 last_status(TRUE);
1360
1361#ifdef FEAT_AUTOCMD
1362 /*
1363 * Don't execute autocommands while creating the windows. Must do that
1364 * when putting the buffers in the windows.
1365 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001366 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#endif
1368
1369 /* todo is number of windows left to create */
1370 for (todo = count - 1; todo > 0; --todo)
1371#ifdef FEAT_VERTSPLIT
1372 if (vertical)
1373 {
1374 if (win_split(curwin->w_width - (curwin->w_width - todo)
1375 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1376 break;
1377 }
1378 else
1379#endif
1380 {
1381 if (win_split(curwin->w_height - (curwin->w_height - todo
1382 * STATUS_HEIGHT) / (todo + 1)
1383 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1384 break;
1385 }
1386
1387#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001388 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
1390
1391 /* return actual number of windows */
1392 return (count - todo);
1393}
1394
1395/*
1396 * Exchange current and next window
1397 */
1398 static void
1399win_exchange(Prenum)
1400 long Prenum;
1401{
1402 frame_T *frp;
1403 frame_T *frp2;
1404 win_T *wp;
1405 win_T *wp2;
1406 int temp;
1407
1408 if (lastwin == firstwin) /* just one window */
1409 {
1410 beep_flush();
1411 return;
1412 }
1413
1414#ifdef FEAT_GUI
1415 need_mouse_correct = TRUE;
1416#endif
1417
1418 /*
1419 * find window to exchange with
1420 */
1421 if (Prenum)
1422 {
1423 frp = curwin->w_frame->fr_parent->fr_child;
1424 while (frp != NULL && --Prenum > 0)
1425 frp = frp->fr_next;
1426 }
1427 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1428 frp = curwin->w_frame->fr_next;
1429 else /* Swap last window in row/col with previous */
1430 frp = curwin->w_frame->fr_prev;
1431
1432 /* We can only exchange a window with another window, not with a frame
1433 * containing windows. */
1434 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1435 return;
1436 wp = frp->fr_win;
1437
1438/*
1439 * 1. remove curwin from the list. Remember after which window it was in wp2
1440 * 2. insert curwin before wp in the list
1441 * if wp != wp2
1442 * 3. remove wp from the list
1443 * 4. insert wp after wp2
1444 * 5. exchange the status line height and vsep width.
1445 */
1446 wp2 = curwin->w_prev;
1447 frp2 = curwin->w_frame->fr_prev;
1448 if (wp->w_prev != curwin)
1449 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001450 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 frame_remove(curwin->w_frame);
1452 win_append(wp->w_prev, curwin);
1453 frame_insert(frp, curwin->w_frame);
1454 }
1455 if (wp != wp2)
1456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 frame_remove(wp->w_frame);
1459 win_append(wp2, wp);
1460 if (frp2 == NULL)
1461 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1462 else
1463 frame_append(frp2, wp->w_frame);
1464 }
1465 temp = curwin->w_status_height;
1466 curwin->w_status_height = wp->w_status_height;
1467 wp->w_status_height = temp;
1468#ifdef FEAT_VERTSPLIT
1469 temp = curwin->w_vsep_width;
1470 curwin->w_vsep_width = wp->w_vsep_width;
1471 wp->w_vsep_width = temp;
1472
1473 /* If the windows are not in the same frame, exchange the sizes to avoid
1474 * messing up the window layout. Otherwise fix the frame sizes. */
1475 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1476 {
1477 temp = curwin->w_height;
1478 curwin->w_height = wp->w_height;
1479 wp->w_height = temp;
1480 temp = curwin->w_width;
1481 curwin->w_width = wp->w_width;
1482 wp->w_width = temp;
1483 }
1484 else
1485 {
1486 frame_fix_height(curwin);
1487 frame_fix_height(wp);
1488 frame_fix_width(curwin);
1489 frame_fix_width(wp);
1490 }
1491#endif
1492
1493 (void)win_comp_pos(); /* recompute window positions */
1494
1495 win_enter(wp, TRUE);
1496 redraw_later(CLEAR);
1497}
1498
1499/*
1500 * rotate windows: if upwards TRUE the second window becomes the first one
1501 * if upwards FALSE the first window becomes the second one
1502 */
1503 static void
1504win_rotate(upwards, count)
1505 int upwards;
1506 int count;
1507{
1508 win_T *wp1;
1509 win_T *wp2;
1510 frame_T *frp;
1511 int n;
1512
1513 if (firstwin == lastwin) /* nothing to do */
1514 {
1515 beep_flush();
1516 return;
1517 }
1518
1519#ifdef FEAT_GUI
1520 need_mouse_correct = TRUE;
1521#endif
1522
1523#ifdef FEAT_VERTSPLIT
1524 /* Check if all frames in this row/col have one window. */
1525 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1526 frp = frp->fr_next)
1527 if (frp->fr_win == NULL)
1528 {
1529 EMSG(_("E443: Cannot rotate when another window is split"));
1530 return;
1531 }
1532#endif
1533
1534 while (count--)
1535 {
1536 if (upwards) /* first window becomes last window */
1537 {
1538 /* remove first window/frame from the list */
1539 frp = curwin->w_frame->fr_parent->fr_child;
1540 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001541 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 frame_remove(frp);
1543
1544 /* find last frame and append removed window/frame after it */
1545 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1546 ;
1547 win_append(frp->fr_win, wp1);
1548 frame_append(frp, wp1->w_frame);
1549
1550 wp2 = frp->fr_win; /* previously last window */
1551 }
1552 else /* last window becomes first window */
1553 {
1554 /* find last window/frame in the list and remove it */
1555 for (frp = curwin->w_frame; frp->fr_next != NULL;
1556 frp = frp->fr_next)
1557 ;
1558 wp1 = frp->fr_win;
1559 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001560 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 frame_remove(frp);
1562
1563 /* append the removed window/frame before the first in the list */
1564 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1565 frame_insert(frp->fr_parent->fr_child, frp);
1566 }
1567
1568 /* exchange status height and vsep width of old and new last window */
1569 n = wp2->w_status_height;
1570 wp2->w_status_height = wp1->w_status_height;
1571 wp1->w_status_height = n;
1572 frame_fix_height(wp1);
1573 frame_fix_height(wp2);
1574#ifdef FEAT_VERTSPLIT
1575 n = wp2->w_vsep_width;
1576 wp2->w_vsep_width = wp1->w_vsep_width;
1577 wp1->w_vsep_width = n;
1578 frame_fix_width(wp1);
1579 frame_fix_width(wp2);
1580#endif
1581
1582 /* recompute w_winrow and w_wincol for all windows */
1583 (void)win_comp_pos();
1584 }
1585
1586 redraw_later(CLEAR);
1587}
1588
1589/*
1590 * Move the current window to the very top/bottom/left/right of the screen.
1591 */
1592 static void
1593win_totop(size, flags)
1594 int size;
1595 int flags;
1596{
1597 int dir;
1598 int height = curwin->w_height;
1599
1600 if (lastwin == firstwin)
1601 {
1602 beep_flush();
1603 return;
1604 }
1605
1606 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001607 (void)winframe_remove(curwin, &dir, NULL);
1608 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 last_status(FALSE); /* may need to remove last status line */
1610 (void)win_comp_pos(); /* recompute window positions */
1611
1612 /* Split a window on the desired side and put the window there. */
1613 (void)win_split_ins(size, flags, curwin, dir);
1614 if (!(flags & WSP_VERT))
1615 {
1616 win_setheight(height);
1617 if (p_ea)
1618 win_equal(curwin, TRUE, 'v');
1619 }
1620
1621#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1622 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1623 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001624 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626}
1627
1628/*
1629 * Move window "win1" to below/right of "win2" and make "win1" the current
1630 * window. Only works within the same frame!
1631 */
1632 void
1633win_move_after(win1, win2)
1634 win_T *win1, *win2;
1635{
1636 int height;
1637
1638 /* check if the arguments are reasonable */
1639 if (win1 == win2)
1640 return;
1641
1642 /* check if there is something to do */
1643 if (win2->w_next != win1)
1644 {
1645 /* may need move the status line/vertical separator of the last window
1646 * */
1647 if (win1 == lastwin)
1648 {
1649 height = win1->w_prev->w_status_height;
1650 win1->w_prev->w_status_height = win1->w_status_height;
1651 win1->w_status_height = height;
1652#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001653 if (win1->w_prev->w_vsep_width == 1)
1654 {
1655 /* Remove the vertical separator from the last-but-one window,
1656 * add it to the last window. Adjust the frame widths. */
1657 win1->w_prev->w_vsep_width = 0;
1658 win1->w_prev->w_frame->fr_width -= 1;
1659 win1->w_vsep_width = 1;
1660 win1->w_frame->fr_width += 1;
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#endif
1663 }
1664 else if (win2 == lastwin)
1665 {
1666 height = win1->w_status_height;
1667 win1->w_status_height = win2->w_status_height;
1668 win2->w_status_height = height;
1669#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001670 if (win1->w_vsep_width == 1)
1671 {
1672 /* Remove the vertical separator from win1, add it to the last
1673 * window, win2. Adjust the frame widths. */
1674 win2->w_vsep_width = 1;
1675 win2->w_frame->fr_width += 1;
1676 win1->w_vsep_width = 0;
1677 win1->w_frame->fr_width -= 1;
1678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#endif
1680 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001681 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 frame_remove(win1->w_frame);
1683 win_append(win2, win1);
1684 frame_append(win2->w_frame, win1->w_frame);
1685
1686 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1687 redraw_later(NOT_VALID);
1688 }
1689 win_enter(win1, FALSE);
1690}
1691
1692/*
1693 * Make all windows the same height.
1694 * 'next_curwin' will soon be the current window, make sure it has enough
1695 * rows.
1696 */
1697 void
1698win_equal(next_curwin, current, dir)
1699 win_T *next_curwin; /* pointer to current window to be or NULL */
1700 int current; /* do only frame with current window */
1701 int dir; /* 'v' for vertically, 'h' for horizontally,
1702 'b' for both, 0 for using p_ead */
1703{
1704 if (dir == 0)
1705#ifdef FEAT_VERTSPLIT
1706 dir = *p_ead;
1707#else
1708 dir = 'b';
1709#endif
1710 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001711 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001712 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713}
1714
1715/*
1716 * Set a frame to a new position and height, spreading the available room
1717 * equally over contained frames.
1718 * The window "next_curwin" (if not NULL) should at least get the size from
1719 * 'winheight' and 'winwidth' if possible.
1720 */
1721 static void
1722win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1723 win_T *next_curwin; /* pointer to current window to be or NULL */
1724 int current; /* do only frame with current window */
1725 frame_T *topfr; /* frame to set size off */
1726 int dir; /* 'v', 'h' or 'b', see win_equal() */
1727 int col; /* horizontal position for frame */
1728 int row; /* vertical position for frame */
1729 int width; /* new width of frame */
1730 int height; /* new height of frame */
1731{
1732 int n, m;
1733 int extra_sep = 0;
1734 int wincount, totwincount = 0;
1735 frame_T *fr;
1736 int next_curwin_size = 0;
1737 int room = 0;
1738 int new_size;
1739 int has_next_curwin = 0;
1740 int hnc;
1741
1742 if (topfr->fr_layout == FR_LEAF)
1743 {
1744 /* Set the width/height of this frame.
1745 * Redraw when size or position changes */
1746 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1747#ifdef FEAT_VERTSPLIT
1748 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1749#endif
1750 )
1751 {
1752 topfr->fr_win->w_winrow = row;
1753 frame_new_height(topfr, height, FALSE, FALSE);
1754#ifdef FEAT_VERTSPLIT
1755 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001756 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757#endif
1758 redraw_all_later(CLEAR);
1759 }
1760 }
1761#ifdef FEAT_VERTSPLIT
1762 else if (topfr->fr_layout == FR_ROW)
1763 {
1764 topfr->fr_width = width;
1765 topfr->fr_height = height;
1766
1767 if (dir != 'v') /* equalize frame widths */
1768 {
1769 /* Compute the maximum number of windows horizontally in this
1770 * frame. */
1771 n = frame_minwidth(topfr, NOWIN);
1772 /* add one for the rightmost window, it doesn't have a separator */
1773 if (col + width == Columns)
1774 extra_sep = 1;
1775 else
1776 extra_sep = 0;
1777 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001778 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001780 /*
1781 * Compute width for "next_curwin" window and room available for
1782 * other windows.
1783 * "m" is the minimal width when counting p_wiw for "next_curwin".
1784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 m = frame_minwidth(topfr, next_curwin);
1786 room = width - m;
1787 if (room < 0)
1788 {
1789 next_curwin_size = p_wiw + room;
1790 room = 0;
1791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 else
1793 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001794 next_curwin_size = -1;
1795 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1796 {
1797 /* If 'winfixwidth' set keep the window width if
1798 * possible.
1799 * Watch out for this window being the next_curwin. */
1800 if (frame_fixed_width(fr))
1801 {
1802 n = frame_minwidth(fr, NOWIN);
1803 new_size = fr->fr_width;
1804 if (frame_has_win(fr, next_curwin))
1805 {
1806 room += p_wiw - p_wmw;
1807 next_curwin_size = 0;
1808 if (new_size < p_wiw)
1809 new_size = p_wiw;
1810 }
1811 else
1812 /* These windows don't use up room. */
1813 totwincount -= (n + (fr->fr_next == NULL
1814 ? extra_sep : 0)) / (p_wmw + 1);
1815 room -= new_size - n;
1816 if (room < 0)
1817 {
1818 new_size += room;
1819 room = 0;
1820 }
1821 fr->fr_newwidth = new_size;
1822 }
1823 }
1824 if (next_curwin_size == -1)
1825 {
1826 if (!has_next_curwin)
1827 next_curwin_size = 0;
1828 else if (totwincount > 1
1829 && (room + (totwincount - 2))
1830 / (totwincount - 1) > p_wiw)
1831 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001832 /* Can make all windows wider than 'winwidth', spread
1833 * the room equally. */
1834 next_curwin_size = (room + p_wiw
1835 + (totwincount - 1) * p_wmw
1836 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001837 room -= next_curwin_size - p_wiw;
1838 }
1839 else
1840 next_curwin_size = p_wiw;
1841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001843
1844 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 --totwincount; /* don't count curwin */
1846 }
1847
1848 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1849 {
1850 n = m = 0;
1851 wincount = 1;
1852 if (fr->fr_next == NULL)
1853 /* last frame gets all that remains (avoid roundoff error) */
1854 new_size = width;
1855 else if (dir == 'v')
1856 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001857 else if (frame_fixed_width(fr))
1858 {
1859 new_size = fr->fr_newwidth;
1860 wincount = 0; /* doesn't count as a sizeable window */
1861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 else
1863 {
1864 /* Compute the maximum number of windows horiz. in "fr". */
1865 n = frame_minwidth(fr, NOWIN);
1866 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1867 / (p_wmw + 1);
1868 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001869 if (has_next_curwin)
1870 hnc = frame_has_win(fr, next_curwin);
1871 else
1872 hnc = FALSE;
1873 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001875 if (totwincount == 0)
1876 new_size = room;
1877 else
1878 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001880 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
1882 next_curwin_size -= p_wiw - (m - n);
1883 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001884 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001886 else
1887 room -= new_size;
1888 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001891 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 * window, unless equalizing all frames. */
1893 if (!current || dir != 'v' || topfr->fr_parent != NULL
1894 || (new_size != fr->fr_width)
1895 || frame_has_win(fr, next_curwin))
1896 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001897 new_size, height);
1898 col += new_size;
1899 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 totwincount -= wincount;
1901 }
1902 }
1903#endif
1904 else /* topfr->fr_layout == FR_COL */
1905 {
1906#ifdef FEAT_VERTSPLIT
1907 topfr->fr_width = width;
1908#endif
1909 topfr->fr_height = height;
1910
1911 if (dir != 'h') /* equalize frame heights */
1912 {
1913 /* Compute maximum number of windows vertically in this frame. */
1914 n = frame_minheight(topfr, NOWIN);
1915 /* add one for the bottom window if it doesn't have a statusline */
1916 if (row + height == cmdline_row && p_ls == 0)
1917 extra_sep = 1;
1918 else
1919 extra_sep = 0;
1920 totwincount = (n + extra_sep) / (p_wmh + 1);
1921 has_next_curwin = frame_has_win(topfr, next_curwin);
1922
1923 /*
1924 * Compute height for "next_curwin" window and room available for
1925 * other windows.
1926 * "m" is the minimal height when counting p_wh for "next_curwin".
1927 */
1928 m = frame_minheight(topfr, next_curwin);
1929 room = height - m;
1930 if (room < 0)
1931 {
1932 /* The room is less then 'winheight', use all space for the
1933 * current window. */
1934 next_curwin_size = p_wh + room;
1935 room = 0;
1936 }
1937 else
1938 {
1939 next_curwin_size = -1;
1940 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1941 {
1942 /* If 'winfixheight' set keep the window height if
1943 * possible.
1944 * Watch out for this window being the next_curwin. */
1945 if (frame_fixed_height(fr))
1946 {
1947 n = frame_minheight(fr, NOWIN);
1948 new_size = fr->fr_height;
1949 if (frame_has_win(fr, next_curwin))
1950 {
1951 room += p_wh - p_wmh;
1952 next_curwin_size = 0;
1953 if (new_size < p_wh)
1954 new_size = p_wh;
1955 }
1956 else
1957 /* These windows don't use up room. */
1958 totwincount -= (n + (fr->fr_next == NULL
1959 ? extra_sep : 0)) / (p_wmh + 1);
1960 room -= new_size - n;
1961 if (room < 0)
1962 {
1963 new_size += room;
1964 room = 0;
1965 }
1966 fr->fr_newheight = new_size;
1967 }
1968 }
1969 if (next_curwin_size == -1)
1970 {
1971 if (!has_next_curwin)
1972 next_curwin_size = 0;
1973 else if (totwincount > 1
1974 && (room + (totwincount - 2))
1975 / (totwincount - 1) > p_wh)
1976 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001977 /* can make all windows higher than 'winheight',
1978 * spread the room equally. */
1979 next_curwin_size = (room + p_wh
1980 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 + (totwincount - 1)) / totwincount;
1982 room -= next_curwin_size - p_wh;
1983 }
1984 else
1985 next_curwin_size = p_wh;
1986 }
1987 }
1988
1989 if (has_next_curwin)
1990 --totwincount; /* don't count curwin */
1991 }
1992
1993 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1994 {
1995 n = m = 0;
1996 wincount = 1;
1997 if (fr->fr_next == NULL)
1998 /* last frame gets all that remains (avoid roundoff error) */
1999 new_size = height;
2000 else if (dir == 'h')
2001 new_size = fr->fr_height;
2002 else if (frame_fixed_height(fr))
2003 {
2004 new_size = fr->fr_newheight;
2005 wincount = 0; /* doesn't count as a sizeable window */
2006 }
2007 else
2008 {
2009 /* Compute the maximum number of windows vert. in "fr". */
2010 n = frame_minheight(fr, NOWIN);
2011 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2012 / (p_wmh + 1);
2013 m = frame_minheight(fr, next_curwin);
2014 if (has_next_curwin)
2015 hnc = frame_has_win(fr, next_curwin);
2016 else
2017 hnc = FALSE;
2018 if (hnc) /* don't count next_curwin */
2019 --wincount;
2020 if (totwincount == 0)
2021 new_size = room;
2022 else
2023 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2024 / totwincount;
2025 if (hnc) /* add next_curwin size */
2026 {
2027 next_curwin_size -= p_wh - (m - n);
2028 new_size += next_curwin_size;
2029 room -= new_size - next_curwin_size;
2030 }
2031 else
2032 room -= new_size;
2033 new_size += n;
2034 }
2035 /* Skip frame that is full width when splitting or closing a
2036 * window, unless equalizing all frames. */
2037 if (!current || dir != 'h' || topfr->fr_parent != NULL
2038 || (new_size != fr->fr_height)
2039 || frame_has_win(fr, next_curwin))
2040 win_equal_rec(next_curwin, current, fr, dir, col, row,
2041 width, new_size);
2042 row += new_size;
2043 height -= new_size;
2044 totwincount -= wincount;
2045 }
2046 }
2047}
2048
2049/*
2050 * close all windows for buffer 'buf'
2051 */
2052 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002053close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002055 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002057 win_T *wp;
2058 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002059 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002062
2063 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002065 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2066#ifdef FEAT_AUTOCMD
2067 && !(wp->w_closing || wp->w_buffer->b_closing)
2068#endif
2069 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002071 win_close(wp, FALSE);
2072
2073 /* Start all over, autocommands may change the window layout. */
2074 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002077 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002079
2080 /* Also check windows in other tab pages. */
2081 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2082 {
2083 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002084 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002085 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002086 if (wp->w_buffer == buf
2087#ifdef FEAT_AUTOCMD
2088 && !(wp->w_closing || wp->w_buffer->b_closing)
2089#endif
2090 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002091 {
2092 win_close_othertab(wp, FALSE, tp);
2093
2094 /* Start all over, the tab page may be closed and
2095 * autocommands may change the window layout. */
2096 nexttp = first_tabpage;
2097 break;
2098 }
2099 }
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002102
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002103 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002104 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002105 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
2108/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002109 * Return TRUE if the current window is the only window that exists (ignoring
2110 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002111 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002112 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002113 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002114last_window()
2115{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002116 return (one_window() && first_tabpage->tp_next == NULL);
2117}
2118
2119/*
2120 * Return TRUE if there is only one window other than "aucmd_win" in the
2121 * current tab page.
2122 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002123 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002124one_window()
2125{
2126#ifdef FEAT_AUTOCMD
2127 win_T *wp;
2128 int seen_one = FALSE;
2129
2130 FOR_ALL_WINDOWS(wp)
2131 {
2132 if (wp != aucmd_win)
2133 {
2134 if (seen_one)
2135 return FALSE;
2136 seen_one = TRUE;
2137 }
2138 }
2139 return TRUE;
2140#else
2141 return firstwin == lastwin;
2142#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002143}
2144
2145/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002146 * Close the possibly last window in a tab page.
2147 * Returns TRUE when the window was closed already.
2148 */
2149 static int
2150close_last_window_tabpage(win, free_buf, prev_curtab)
2151 win_T *win;
2152 int free_buf;
2153 tabpage_T *prev_curtab;
2154{
2155 if (firstwin == lastwin)
2156 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002157#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002158 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002159#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002160
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002161 /*
2162 * Closing the last window in a tab page. First go to another tab
2163 * page and then close the window and the tab page. This avoids that
2164 * curwin and curtab are invalid while we are freeing memory, they may
2165 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002166 * Don't trigger autocommands yet, they may use wrong values, so do
2167 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002168 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002169 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002170 redraw_tabline = TRUE;
2171
2172 /* Safety check: Autocommands may have closed the window when jumping
2173 * to the other tab page. */
2174 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2175 {
2176 int h = tabline_height();
2177
2178 win_close_othertab(win, free_buf, prev_curtab);
2179 if (h != tabline_height())
2180 shell_new_rows();
2181 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002182 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2183 * that now. */
2184#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002185 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002186 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2187 if (old_curbuf != curbuf)
2188 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002189#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002190 return TRUE;
2191 }
2192 return FALSE;
2193}
2194
2195/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 * If "free_buf" is TRUE related buffer may be unloaded.
2198 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002199 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002200 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002202 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203win_close(win, free_buf)
2204 win_T *win;
2205 int free_buf;
2206{
2207 win_T *wp;
2208#ifdef FEAT_AUTOCMD
2209 int other_buffer = FALSE;
2210#endif
2211 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 int dir;
2213 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002214 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002216 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002219 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 }
2221
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002222#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002223 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002224 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002225 if (win == aucmd_win)
2226 {
2227 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002228 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002229 }
2230 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2231 {
2232 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002233 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002234 }
2235#endif
2236
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002237 /* When closing the last window in a tab page first go to another tab page
2238 * and then close the window and the tab page to avoid that curwin and
2239 * curtab are invalid while we are freeing memory. */
2240 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002241 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /* When closing the help window, try restoring a snapshot after closing
2244 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002245 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 help_window = TRUE;
2247 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002248 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250#ifdef FEAT_AUTOCMD
2251 if (win == curwin)
2252 {
2253 /*
2254 * Guess which window is going to be the new current window.
2255 * This may change because of the autocommands (sigh).
2256 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002257 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002260 * Be careful: If autocommands delete the window or cause this window
2261 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
2263 if (wp->w_buffer != curbuf)
2264 {
2265 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002266 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002268 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002269 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002270 win->w_closing = FALSE;
2271 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002274 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002276 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002277 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002278 win->w_closing = FALSE;
2279 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281# ifdef FEAT_EVAL
2282 /* autocmds may abort script processing */
2283 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285# endif
2286 }
2287#endif
2288
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002289#ifdef FEAT_GUI
2290 /* Avoid trouble with scrollbars that are going to be deleted in
2291 * win_free(). */
2292 if (gui.in_use)
2293 out_flush();
2294#endif
2295
Bram Moolenaara971b822011-09-14 14:43:25 +02002296#ifdef FEAT_SYN_HL
2297 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002298 if (win->w_buffer != NULL)
2299 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002300#endif
2301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 /*
2303 * Close the link to the buffer.
2304 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002305 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002306 {
2307#ifdef FEAT_AUTOCMD
2308 win->w_closing = TRUE;
2309#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002310 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002311#ifdef FEAT_AUTOCMD
2312 if (win_valid(win))
2313 win->w_closing = FALSE;
2314#endif
2315 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002316
Bram Moolenaar802418d2013-01-17 14:00:11 +01002317 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2318 && (last_window() || curtab != prev_curtab
2319 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002320 {
2321 /* Autocommands have close all windows, quit now. Restore
2322 * curwin->w_buffer, otherwise writing viminfo may fail. */
2323 if (curwin->w_buffer == NULL)
2324 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002325 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002326 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002329 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002330 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002331 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
Bram Moolenaara971b822011-09-14 14:43:25 +02002334 /* Free the memory used for the window and get the window that received
2335 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002336 wp = win_free_mem(win, &dir, NULL);
2337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 /* Make sure curwin isn't invalid. It can cause severe trouble when
2339 * printing an error message. For win_equal() curbuf needs to be valid
2340 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002341 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 curwin = wp;
2344#ifdef FEAT_QUICKFIX
2345 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2346 {
2347 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002348 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 * finding another window to go to.
2350 */
2351 for (;;)
2352 {
2353 if (wp->w_next == NULL)
2354 wp = firstwin;
2355 else
2356 wp = wp->w_next;
2357 if (wp == curwin)
2358 break;
2359 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2360 {
2361 curwin = wp;
2362 break;
2363 }
2364 }
2365 }
2366#endif
2367 curbuf = curwin->w_buffer;
2368 close_curwin = TRUE;
2369 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002370 if (p_ea
2371#ifdef FEAT_VERTSPLIT
2372 && (*p_ead == 'b' || *p_ead == dir)
2373#endif
2374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 win_equal(curwin, TRUE,
2376#ifdef FEAT_VERTSPLIT
2377 dir
2378#else
2379 0
2380#endif
2381 );
2382 else
2383 win_comp_pos();
2384 if (close_curwin)
2385 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002386 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_AUTOCMD
2388 if (other_buffer)
2389 /* careful: after this wp and win may be invalid! */
2390 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2391#endif
2392 }
2393
2394 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002395 * If last window has a status line now and we don't want one,
2396 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
2398 last_status(FALSE);
2399
2400 /* After closing the help window, try restoring the window layout from
2401 * before it was opened. */
2402 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002403 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2406 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2407 if (gui.in_use && !win_hasvertsplit())
2408 gui_init_which_components(NULL);
2409#endif
2410
2411 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002412 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414
2415/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002416 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002417 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002418 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002419 * Caller must check if buffer is hidden and whether the tabline needs to be
2420 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002421 */
2422 void
2423win_close_othertab(win, free_buf, tp)
2424 win_T *win;
2425 int free_buf;
2426 tabpage_T *tp;
2427{
2428 win_T *wp;
2429 int dir;
2430 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002431 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002432
Bram Moolenaar362ce482012-06-06 19:02:45 +02002433#ifdef FEAT_AUTOCMD
2434 if (win->w_closing || win->w_buffer->b_closing)
2435 return; /* window is already being closed */
2436#endif
2437
Bram Moolenaarf740b292006-02-16 22:11:02 +00002438 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002439 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440
2441 /* Careful: Autocommands may have closed the tab page or made it the
2442 * current tab page. */
2443 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2444 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002445 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002446 return;
2447
2448 /* Autocommands may have closed the window already. */
2449 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2450 ;
2451 if (wp == NULL)
2452 return;
2453
Bram Moolenaarf740b292006-02-16 22:11:02 +00002454 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002455 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002456 {
2457 if (tp == first_tabpage)
2458 first_tabpage = tp->tp_next;
2459 else
2460 {
2461 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2462 ptp = ptp->tp_next)
2463 ;
2464 if (ptp == NULL)
2465 {
2466 EMSG2(_(e_intern2), "win_close_othertab()");
2467 return;
2468 }
2469 ptp->tp_next = tp->tp_next;
2470 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002471 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002472 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002473
2474 /* Free the memory used for the window. */
2475 win_free_mem(win, &dir, tp);
2476
2477 if (free_tp)
2478 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002479}
2480
2481/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002482 * Free the memory used for a window.
2483 * Returns a pointer to the window that got the freed up space.
2484 */
2485 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002486win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002487 win_T *win;
2488 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002489 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002490{
2491 frame_T *frp;
2492 win_T *wp;
2493
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002494 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002495 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002496 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002497 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002498 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002499
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002500 /* When deleting the current window of another tab page select a new
2501 * current window. */
2502 if (tp != NULL && win == tp->tp_curwin)
2503 tp->tp_curwin = wp;
2504
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002505 return wp;
2506}
2507
2508#if defined(EXITFREE) || defined(PROTO)
2509 void
2510win_free_all()
2511{
2512 int dummy;
2513
Bram Moolenaarf740b292006-02-16 22:11:02 +00002514# ifdef FEAT_WINDOWS
2515 while (first_tabpage->tp_next != NULL)
2516 tabpage_close(TRUE);
2517# endif
2518
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002519# ifdef FEAT_AUTOCMD
2520 if (aucmd_win != NULL)
2521 {
2522 (void)win_free_mem(aucmd_win, &dummy, NULL);
2523 aucmd_win = NULL;
2524 }
2525# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002526
2527 while (firstwin != NULL)
2528 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002529
2530 /* No window should be used after this. Set curwin to NULL to crash
2531 * instead of using freed memory. */
2532 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002533}
2534#endif
2535
2536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 * Remove a window and its frame from the tree of frames.
2538 * Returns a pointer to the window that got the freed up space.
2539 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002540 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002541winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002543 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002544 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 frame_T *frp, *frp2, *frp3;
2547 frame_T *frp_close = win->w_frame;
2548 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002551 * If there is only one window there is nothing to remove.
2552 */
2553 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2554 return NULL;
2555
2556 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 * Remove the window from its frame.
2558 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002559 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 wp = frame2win(frp2);
2561
2562 /* Remove this frame from the list of frames. */
2563 frame_remove(frp_close);
2564
2565#ifdef FEAT_VERTSPLIT
2566 if (frp_close->fr_parent->fr_layout == FR_COL)
2567 {
2568#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002569 /* When 'winfixheight' is set, try to find another frame in the column
2570 * (as close to the closed frame as possible) to distribute the height
2571 * to. */
2572 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2573 {
2574 frp = frp_close->fr_prev;
2575 frp3 = frp_close->fr_next;
2576 while (frp != NULL || frp3 != NULL)
2577 {
2578 if (frp != NULL)
2579 {
2580 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2581 {
2582 frp2 = frp;
2583 wp = frp->fr_win;
2584 break;
2585 }
2586 frp = frp->fr_prev;
2587 }
2588 if (frp3 != NULL)
2589 {
2590 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2591 {
2592 frp2 = frp3;
2593 wp = frp3->fr_win;
2594 break;
2595 }
2596 frp3 = frp3->fr_next;
2597 }
2598 }
2599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2601 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602#ifdef FEAT_VERTSPLIT
2603 *dirp = 'v';
2604 }
2605 else
2606 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002607 /* When 'winfixwidth' is set, try to find another frame in the column
2608 * (as close to the closed frame as possible) to distribute the width
2609 * to. */
2610 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2611 {
2612 frp = frp_close->fr_prev;
2613 frp3 = frp_close->fr_next;
2614 while (frp != NULL || frp3 != NULL)
2615 {
2616 if (frp != NULL)
2617 {
2618 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2619 {
2620 frp2 = frp;
2621 wp = frp->fr_win;
2622 break;
2623 }
2624 frp = frp->fr_prev;
2625 }
2626 if (frp3 != NULL)
2627 {
2628 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2629 {
2630 frp2 = frp3;
2631 wp = frp3->fr_win;
2632 break;
2633 }
2634 frp3 = frp3->fr_next;
2635 }
2636 }
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002639 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 *dirp = 'h';
2641 }
2642#endif
2643
2644 /* If rows/columns go to a window below/right its positions need to be
2645 * updated. Can only be done after the sizes have been updated. */
2646 if (frp2 == frp_close->fr_next)
2647 {
2648 int row = win->w_winrow;
2649 int col = W_WINCOL(win);
2650
2651 frame_comp_pos(frp2, &row, &col);
2652 }
2653
2654 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2655 {
2656 /* There is no other frame in this list, move its info to the parent
2657 * and remove it. */
2658 frp2->fr_parent->fr_layout = frp2->fr_layout;
2659 frp2->fr_parent->fr_child = frp2->fr_child;
2660 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2661 frp->fr_parent = frp2->fr_parent;
2662 frp2->fr_parent->fr_win = frp2->fr_win;
2663 if (frp2->fr_win != NULL)
2664 frp2->fr_win->w_frame = frp2->fr_parent;
2665 frp = frp2->fr_parent;
2666 vim_free(frp2);
2667
2668 frp2 = frp->fr_parent;
2669 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2670 {
2671 /* The frame above the parent has the same layout, have to merge
2672 * the frames into this list. */
2673 if (frp2->fr_child == frp)
2674 frp2->fr_child = frp->fr_child;
2675 frp->fr_child->fr_prev = frp->fr_prev;
2676 if (frp->fr_prev != NULL)
2677 frp->fr_prev->fr_next = frp->fr_child;
2678 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2679 {
2680 frp3->fr_parent = frp2;
2681 if (frp3->fr_next == NULL)
2682 {
2683 frp3->fr_next = frp->fr_next;
2684 if (frp->fr_next != NULL)
2685 frp->fr_next->fr_prev = frp3;
2686 break;
2687 }
2688 }
2689 vim_free(frp);
2690 }
2691 }
2692
2693 return wp;
2694}
2695
2696/*
2697 * Find out which frame is going to get the freed up space when "win" is
2698 * closed.
2699 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2700 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2701 * This makes opening a window and closing it immediately keep the same window
2702 * layout.
2703 */
2704 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002705win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002707 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 frame_T *frp;
2710 int b;
2711
Bram Moolenaarf740b292006-02-16 22:11:02 +00002712 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002713 /* Last window in this tab page, will go to next tab page. */
2714 return alt_tabpage()->tp_curwin->w_frame;
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 frp = win->w_frame;
2717#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002718 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 b = p_spr;
2720 else
2721#endif
2722 b = p_sb;
2723 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2724 return frp->fr_next;
2725 return frp->fr_prev;
2726}
2727
2728/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002729 * Return the tabpage that will be used if the current one is closed.
2730 */
2731 static tabpage_T *
2732alt_tabpage()
2733{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002734 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002735
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002736 /* Use the next tab page if possible. */
2737 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002738 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002739
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002740 /* Find the last but one tab page. */
2741 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2742 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002743 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002744}
2745
2746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 * Find the left-upper window in frame "frp".
2748 */
2749 static win_T *
2750frame2win(frp)
2751 frame_T *frp;
2752{
2753 while (frp->fr_win == NULL)
2754 frp = frp->fr_child;
2755 return frp->fr_win;
2756}
2757
2758/*
2759 * Return TRUE if frame "frp" contains window "wp".
2760 */
2761 static int
2762frame_has_win(frp, wp)
2763 frame_T *frp;
2764 win_T *wp;
2765{
2766 frame_T *p;
2767
2768 if (frp->fr_layout == FR_LEAF)
2769 return frp->fr_win == wp;
2770
2771 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2772 if (frame_has_win(p, wp))
2773 return TRUE;
2774 return FALSE;
2775}
2776
2777/*
2778 * Set a new height for a frame. Recursively sets the height for contained
2779 * frames and windows. Caller must take care of positions.
2780 */
2781 static void
2782frame_new_height(topfrp, height, topfirst, wfh)
2783 frame_T *topfrp;
2784 int height;
2785 int topfirst; /* resize topmost contained frame first */
2786 int wfh; /* obey 'winfixheight' when there is a choice;
2787 may cause the height not to be set */
2788{
2789 frame_T *frp;
2790 int extra_lines;
2791 int h;
2792
2793 if (topfrp->fr_win != NULL)
2794 {
2795 /* Simple case: just one window. */
2796 win_new_height(topfrp->fr_win,
2797 height - topfrp->fr_win->w_status_height);
2798 }
2799#ifdef FEAT_VERTSPLIT
2800 else if (topfrp->fr_layout == FR_ROW)
2801 {
2802 do
2803 {
2804 /* All frames in this row get the same new height. */
2805 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2806 {
2807 frame_new_height(frp, height, topfirst, wfh);
2808 if (frp->fr_height > height)
2809 {
2810 /* Could not fit the windows, make the whole row higher. */
2811 height = frp->fr_height;
2812 break;
2813 }
2814 }
2815 }
2816 while (frp != NULL);
2817 }
2818#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002819 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
2821 /* Complicated case: Resize a column of frames. Resize the bottom
2822 * frame first, frames above that when needed. */
2823
2824 frp = topfrp->fr_child;
2825 if (wfh)
2826 /* Advance past frames with one window with 'wfh' set. */
2827 while (frame_fixed_height(frp))
2828 {
2829 frp = frp->fr_next;
2830 if (frp == NULL)
2831 return; /* no frame without 'wfh', give up */
2832 }
2833 if (!topfirst)
2834 {
2835 /* Find the bottom frame of this column */
2836 while (frp->fr_next != NULL)
2837 frp = frp->fr_next;
2838 if (wfh)
2839 /* Advance back for frames with one window with 'wfh' set. */
2840 while (frame_fixed_height(frp))
2841 frp = frp->fr_prev;
2842 }
2843
2844 extra_lines = height - topfrp->fr_height;
2845 if (extra_lines < 0)
2846 {
2847 /* reduce height of contained frames, bottom or top frame first */
2848 while (frp != NULL)
2849 {
2850 h = frame_minheight(frp, NULL);
2851 if (frp->fr_height + extra_lines < h)
2852 {
2853 extra_lines += frp->fr_height - h;
2854 frame_new_height(frp, h, topfirst, wfh);
2855 }
2856 else
2857 {
2858 frame_new_height(frp, frp->fr_height + extra_lines,
2859 topfirst, wfh);
2860 break;
2861 }
2862 if (topfirst)
2863 {
2864 do
2865 frp = frp->fr_next;
2866 while (wfh && frp != NULL && frame_fixed_height(frp));
2867 }
2868 else
2869 {
2870 do
2871 frp = frp->fr_prev;
2872 while (wfh && frp != NULL && frame_fixed_height(frp));
2873 }
2874 /* Increase "height" if we could not reduce enough frames. */
2875 if (frp == NULL)
2876 height -= extra_lines;
2877 }
2878 }
2879 else if (extra_lines > 0)
2880 {
2881 /* increase height of bottom or top frame */
2882 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2883 }
2884 }
2885 topfrp->fr_height = height;
2886}
2887
2888/*
2889 * Return TRUE if height of frame "frp" should not be changed because of
2890 * the 'winfixheight' option.
2891 */
2892 static int
2893frame_fixed_height(frp)
2894 frame_T *frp;
2895{
2896 /* frame with one window: fixed height if 'winfixheight' set. */
2897 if (frp->fr_win != NULL)
2898 return frp->fr_win->w_p_wfh;
2899
2900 if (frp->fr_layout == FR_ROW)
2901 {
2902 /* The frame is fixed height if one of the frames in the row is fixed
2903 * height. */
2904 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2905 if (frame_fixed_height(frp))
2906 return TRUE;
2907 return FALSE;
2908 }
2909
2910 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2911 * frames in the row are fixed height. */
2912 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2913 if (!frame_fixed_height(frp))
2914 return FALSE;
2915 return TRUE;
2916}
2917
2918#ifdef FEAT_VERTSPLIT
2919/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002920 * Return TRUE if width of frame "frp" should not be changed because of
2921 * the 'winfixwidth' option.
2922 */
2923 static int
2924frame_fixed_width(frp)
2925 frame_T *frp;
2926{
2927 /* frame with one window: fixed width if 'winfixwidth' set. */
2928 if (frp->fr_win != NULL)
2929 return frp->fr_win->w_p_wfw;
2930
2931 if (frp->fr_layout == FR_COL)
2932 {
2933 /* The frame is fixed width if one of the frames in the row is fixed
2934 * width. */
2935 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2936 if (frame_fixed_width(frp))
2937 return TRUE;
2938 return FALSE;
2939 }
2940
2941 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2942 * frames in the row are fixed width. */
2943 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2944 if (!frame_fixed_width(frp))
2945 return FALSE;
2946 return TRUE;
2947}
2948
2949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 * Add a status line to windows at the bottom of "frp".
2951 * Note: Does not check if there is room!
2952 */
2953 static void
2954frame_add_statusline(frp)
2955 frame_T *frp;
2956{
2957 win_T *wp;
2958
2959 if (frp->fr_layout == FR_LEAF)
2960 {
2961 wp = frp->fr_win;
2962 if (wp->w_status_height == 0)
2963 {
2964 if (wp->w_height > 0) /* don't make it negative */
2965 --wp->w_height;
2966 wp->w_status_height = STATUS_HEIGHT;
2967 }
2968 }
2969 else if (frp->fr_layout == FR_ROW)
2970 {
2971 /* Handle all the frames in the row. */
2972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2973 frame_add_statusline(frp);
2974 }
2975 else /* frp->fr_layout == FR_COL */
2976 {
2977 /* Only need to handle the last frame in the column. */
2978 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2979 ;
2980 frame_add_statusline(frp);
2981 }
2982}
2983
2984/*
2985 * Set width of a frame. Handles recursively going through contained frames.
2986 * May remove separator line for windows at the right side (for win_close()).
2987 */
2988 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002989frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 frame_T *topfrp;
2991 int width;
2992 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002993 int wfw; /* obey 'winfixwidth' when there is a choice;
2994 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
2996 frame_T *frp;
2997 int extra_cols;
2998 int w;
2999 win_T *wp;
3000
3001 if (topfrp->fr_layout == FR_LEAF)
3002 {
3003 /* Simple case: just one window. */
3004 wp = topfrp->fr_win;
3005 /* Find out if there are any windows right of this one. */
3006 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3007 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3008 break;
3009 if (frp->fr_parent == NULL)
3010 wp->w_vsep_width = 0;
3011 win_new_width(wp, width - wp->w_vsep_width);
3012 }
3013 else if (topfrp->fr_layout == FR_COL)
3014 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003015 do
3016 {
3017 /* All frames in this column get the same new width. */
3018 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3019 {
3020 frame_new_width(frp, width, leftfirst, wfw);
3021 if (frp->fr_width > width)
3022 {
3023 /* Could not fit the windows, make whole column wider. */
3024 width = frp->fr_width;
3025 break;
3026 }
3027 }
3028 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030 else /* fr_layout == FR_ROW */
3031 {
3032 /* Complicated case: Resize a row of frames. Resize the rightmost
3033 * frame first, frames left of it when needed. */
3034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003036 if (wfw)
3037 /* Advance past frames with one window with 'wfw' set. */
3038 while (frame_fixed_width(frp))
3039 {
3040 frp = frp->fr_next;
3041 if (frp == NULL)
3042 return; /* no frame without 'wfw', give up */
3043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003045 {
3046 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 while (frp->fr_next != NULL)
3048 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003049 if (wfw)
3050 /* Advance back for frames with one window with 'wfw' set. */
3051 while (frame_fixed_width(frp))
3052 frp = frp->fr_prev;
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 extra_cols = width - topfrp->fr_width;
3056 if (extra_cols < 0)
3057 {
3058 /* reduce frame width, rightmost frame first */
3059 while (frp != NULL)
3060 {
3061 w = frame_minwidth(frp, NULL);
3062 if (frp->fr_width + extra_cols < w)
3063 {
3064 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003065 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 }
3067 else
3068 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003069 frame_new_width(frp, frp->fr_width + extra_cols,
3070 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 break;
3072 }
3073 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003074 {
3075 do
3076 frp = frp->fr_next;
3077 while (wfw && frp != NULL && frame_fixed_width(frp));
3078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003080 {
3081 do
3082 frp = frp->fr_prev;
3083 while (wfw && frp != NULL && frame_fixed_width(frp));
3084 }
3085 /* Increase "width" if we could not reduce enough frames. */
3086 if (frp == NULL)
3087 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 }
3090 else if (extra_cols > 0)
3091 {
3092 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003093 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
3095 }
3096 topfrp->fr_width = width;
3097}
3098
3099/*
3100 * Add the vertical separator to windows at the right side of "frp".
3101 * Note: Does not check if there is room!
3102 */
3103 static void
3104frame_add_vsep(frp)
3105 frame_T *frp;
3106{
3107 win_T *wp;
3108
3109 if (frp->fr_layout == FR_LEAF)
3110 {
3111 wp = frp->fr_win;
3112 if (wp->w_vsep_width == 0)
3113 {
3114 if (wp->w_width > 0) /* don't make it negative */
3115 --wp->w_width;
3116 wp->w_vsep_width = 1;
3117 }
3118 }
3119 else if (frp->fr_layout == FR_COL)
3120 {
3121 /* Handle all the frames in the column. */
3122 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3123 frame_add_vsep(frp);
3124 }
3125 else /* frp->fr_layout == FR_ROW */
3126 {
3127 /* Only need to handle the last frame in the row. */
3128 frp = frp->fr_child;
3129 while (frp->fr_next != NULL)
3130 frp = frp->fr_next;
3131 frame_add_vsep(frp);
3132 }
3133}
3134
3135/*
3136 * Set frame width from the window it contains.
3137 */
3138 static void
3139frame_fix_width(wp)
3140 win_T *wp;
3141{
3142 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3143}
3144#endif
3145
3146/*
3147 * Set frame height from the window it contains.
3148 */
3149 static void
3150frame_fix_height(wp)
3151 win_T *wp;
3152{
3153 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3154}
3155
3156/*
3157 * Compute the minimal height for frame "topfrp".
3158 * Uses the 'winminheight' option.
3159 * When "next_curwin" isn't NULL, use p_wh for this window.
3160 * When "next_curwin" is NOWIN, don't use at least one line for the current
3161 * window.
3162 */
3163 static int
3164frame_minheight(topfrp, next_curwin)
3165 frame_T *topfrp;
3166 win_T *next_curwin;
3167{
3168 frame_T *frp;
3169 int m;
3170#ifdef FEAT_VERTSPLIT
3171 int n;
3172#endif
3173
3174 if (topfrp->fr_win != NULL)
3175 {
3176 if (topfrp->fr_win == next_curwin)
3177 m = p_wh + topfrp->fr_win->w_status_height;
3178 else
3179 {
3180 /* window: minimal height of the window plus status line */
3181 m = p_wmh + topfrp->fr_win->w_status_height;
3182 /* Current window is minimal one line high */
3183 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3184 ++m;
3185 }
3186 }
3187#ifdef FEAT_VERTSPLIT
3188 else if (topfrp->fr_layout == FR_ROW)
3189 {
3190 /* get the minimal height from each frame in this row */
3191 m = 0;
3192 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3193 {
3194 n = frame_minheight(frp, next_curwin);
3195 if (n > m)
3196 m = n;
3197 }
3198 }
3199#endif
3200 else
3201 {
3202 /* Add up the minimal heights for all frames in this column. */
3203 m = 0;
3204 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3205 m += frame_minheight(frp, next_curwin);
3206 }
3207
3208 return m;
3209}
3210
3211#ifdef FEAT_VERTSPLIT
3212/*
3213 * Compute the minimal width for frame "topfrp".
3214 * When "next_curwin" isn't NULL, use p_wiw for this window.
3215 * When "next_curwin" is NOWIN, don't use at least one column for the current
3216 * window.
3217 */
3218 static int
3219frame_minwidth(topfrp, next_curwin)
3220 frame_T *topfrp;
3221 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3222{
3223 frame_T *frp;
3224 int m, n;
3225
3226 if (topfrp->fr_win != NULL)
3227 {
3228 if (topfrp->fr_win == next_curwin)
3229 m = p_wiw + topfrp->fr_win->w_vsep_width;
3230 else
3231 {
3232 /* window: minimal width of the window plus separator column */
3233 m = p_wmw + topfrp->fr_win->w_vsep_width;
3234 /* Current window is minimal one column wide */
3235 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3236 ++m;
3237 }
3238 }
3239 else if (topfrp->fr_layout == FR_COL)
3240 {
3241 /* get the minimal width from each frame in this column */
3242 m = 0;
3243 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3244 {
3245 n = frame_minwidth(frp, next_curwin);
3246 if (n > m)
3247 m = n;
3248 }
3249 }
3250 else
3251 {
3252 /* Add up the minimal widths for all frames in this row. */
3253 m = 0;
3254 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3255 m += frame_minwidth(frp, next_curwin);
3256 }
3257
3258 return m;
3259}
3260#endif
3261
3262
3263/*
3264 * Try to close all windows except current one.
3265 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3266 * used and the buffer was modified.
3267 *
3268 * Used by ":bdel" and ":only".
3269 */
3270 void
3271close_others(message, forceit)
3272 int message;
3273 int forceit; /* always hide all other windows */
3274{
3275 win_T *wp;
3276 win_T *nextwp;
3277 int r;
3278
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003279 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 if (message
3282#ifdef FEAT_AUTOCMD
3283 && !autocmd_busy
3284#endif
3285 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003286 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return;
3288 }
3289
3290 /* Be very careful here: autocommands may change the window layout. */
3291 for (wp = firstwin; win_valid(wp); wp = nextwp)
3292 {
3293 nextwp = wp->w_next;
3294 if (wp != curwin) /* don't close current window */
3295 {
3296
3297 /* Check if it's allowed to abandon this window */
3298 r = can_abandon(wp->w_buffer, forceit);
3299#ifdef FEAT_AUTOCMD
3300 if (!win_valid(wp)) /* autocommands messed wp up */
3301 {
3302 nextwp = firstwin;
3303 continue;
3304 }
3305#endif
3306 if (!r)
3307 {
3308#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3309 if (message && (p_confirm || cmdmod.confirm) && p_write)
3310 {
3311 dialog_changed(wp->w_buffer, FALSE);
3312# ifdef FEAT_AUTOCMD
3313 if (!win_valid(wp)) /* autocommands messed wp up */
3314 {
3315 nextwp = firstwin;
3316 continue;
3317 }
3318# endif
3319 }
3320 if (bufIsChanged(wp->w_buffer))
3321#endif
3322 continue;
3323 }
3324 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3325 }
3326 }
3327
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 EMSG(_("E445: Other window contains changes"));
3330}
3331
3332#endif /* FEAT_WINDOWS */
3333
3334/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003335 * Init the current window "curwin".
3336 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 */
3338 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003339curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003341 win_init_empty(curwin);
3342}
3343
3344 void
3345win_init_empty(wp)
3346 win_T *wp;
3347{
3348 redraw_win_later(wp, NOT_VALID);
3349 wp->w_lines_valid = 0;
3350 wp->w_cursor.lnum = 1;
3351 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003353 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003355 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3356 wp->w_pcmark.col = 0;
3357 wp->w_prev_pcmark.lnum = 0;
3358 wp->w_prev_pcmark.col = 0;
3359 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003361 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003363 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003365 if (wp->w_p_rl)
3366 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003368 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003370#ifdef FEAT_SYN_HL
3371 wp->w_s = &wp->w_buffer->b_s;
3372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373}
3374
3375/*
3376 * Allocate the first window and put an empty buffer in it.
3377 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003378 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381win_alloc_first()
3382{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003383 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384 return FAIL;
3385
3386#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003387 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003388 if (first_tabpage == NULL)
3389 return FAIL;
3390 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003391 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003393
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394 return OK;
3395}
3396
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003397#if defined(FEAT_AUTOCMD) || defined(PROTO)
3398/*
3399 * Init "aucmd_win". This can only be done after the first
3400 * window is fully initialized, thus it can't be in win_alloc_first().
3401 */
3402 void
3403win_alloc_aucmd_win()
3404{
3405 aucmd_win = win_alloc(NULL, TRUE);
3406 if (aucmd_win != NULL)
3407 {
3408 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003409 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003410 new_frame(aucmd_win);
3411 }
3412}
3413#endif
3414
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003416 * Allocate the first window or the first window in a new tab page.
3417 * When "oldwin" is NULL create an empty buffer for it.
3418 * When "oldwin" is not NULL copy info from it to the new window (only with
3419 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 * Return FAIL when something goes wrong (out of memory).
3421 */
3422 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003423win_alloc_firstwin(oldwin)
3424 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003426 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003427 if (oldwin == NULL)
3428 {
3429 /* Very first window, need to create an empty buffer for it and
3430 * initialize from scratch. */
3431 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3432 if (curwin == NULL || curbuf == NULL)
3433 return FAIL;
3434 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003435#ifdef FEAT_SYN_HL
3436 curwin->w_s = &(curbuf->b_s);
3437#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003438 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003440 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003442 curwin_init(); /* init current window */
3443 }
3444#ifdef FEAT_WINDOWS
3445 else
3446 {
3447 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003448 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003449
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003450 /* We don't want cursor- and scroll-binding in the first window. */
3451 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003452 }
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003455 new_frame(curwin);
3456 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003457 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003458 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#ifdef FEAT_VERTSPLIT
3460 topframe->fr_width = Columns;
3461#endif
3462 topframe->fr_height = Rows - p_ch;
3463 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003464
3465 return OK;
3466}
3467
3468/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003469 * Create a frame for window "wp".
3470 */
3471 static void
3472new_frame(win_T *wp)
3473{
3474 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3475
3476 wp->w_frame = frp;
3477 if (frp != NULL)
3478 {
3479 frp->fr_layout = FR_LEAF;
3480 frp->fr_win = wp;
3481 }
3482}
3483
3484/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 * Initialize the window and frame size to the maximum.
3486 */
3487 void
3488win_init_size()
3489{
3490 firstwin->w_height = ROWS_AVAIL;
3491 topframe->fr_height = ROWS_AVAIL;
3492#ifdef FEAT_VERTSPLIT
3493 firstwin->w_width = Columns;
3494 topframe->fr_width = Columns;
3495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496}
3497
3498#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003499
3500/*
3501 * Allocate a new tabpage_T and init the values.
3502 * Returns NULL when out of memory.
3503 */
3504 static tabpage_T *
3505alloc_tabpage()
3506{
3507 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003508# ifdef FEAT_GUI
3509 int i;
3510# endif
3511
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003512
3513 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003514 if (tp == NULL)
3515 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003516
Bram Moolenaar429fa852013-04-15 12:27:36 +02003517# ifdef FEAT_EVAL
3518 /* init t: variables */
3519 tp->tp_vars = dict_alloc();
3520 if (tp->tp_vars == NULL)
3521 {
3522 vim_free(tp);
3523 return NULL;
3524 }
3525 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3526# endif
3527
3528# ifdef FEAT_GUI
3529 for (i = 0; i < 3; i++)
3530 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003531# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003533 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003534# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003535 tp->tp_ch_used = p_ch;
3536
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003537 return tp;
3538}
3539
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003540 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003541free_tabpage(tp)
3542 tabpage_T *tp;
3543{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003544 int idx;
3545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546# ifdef FEAT_DIFF
3547 diff_clear(tp);
3548# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003549 for (idx = 0; idx < SNAP_COUNT; ++idx)
3550 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003551#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003552 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3553 hash_init(&tp->tp_vars->dv_hashtab);
3554 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003555#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003556
3557#ifdef FEAT_PYTHON
3558 python_tabpage_free(tp);
3559#endif
3560
3561#ifdef FEAT_PYTHON3
3562 python3_tabpage_free(tp);
3563#endif
3564
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003565 vim_free(tp);
3566}
3567
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003569 * Create a new Tab page with one window.
3570 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003571 * When "after" is 0 put it just after the current Tab page.
3572 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003573 * Return FAIL or OK.
3574 */
3575 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003576win_new_tabpage(after)
3577 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003579 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003580 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003581 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003583 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584 if (newtp == NULL)
3585 return FAIL;
3586
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003588 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003589 {
3590 vim_free(newtp);
3591 return FAIL;
3592 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003593 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003594
3595 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003596 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003597 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003599 if (after == 1)
3600 {
3601 /* New tab page becomes the first one. */
3602 newtp->tp_next = first_tabpage;
3603 first_tabpage = newtp;
3604 }
3605 else
3606 {
3607 if (after > 0)
3608 {
3609 /* Put new tab page before tab page "after". */
3610 n = 2;
3611 for (tp = first_tabpage; tp->tp_next != NULL
3612 && n < after; tp = tp->tp_next)
3613 ++n;
3614 }
3615 newtp->tp_next = tp->tp_next;
3616 tp->tp_next = newtp;
3617 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003618 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003619 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003620 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003621
3622 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003623 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003624
3625#if defined(FEAT_GUI)
3626 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3627 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003628 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003629#endif
3630
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003632#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003633 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003634 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003635#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003636 return OK;
3637 }
3638
3639 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003640 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641 return FAIL;
3642}
3643
3644/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003645 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3646 * like with ":split".
3647 * Returns OK if a new tab page was created, FAIL otherwise.
3648 */
3649 int
3650may_open_tabpage()
3651{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003652 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003653
Bram Moolenaard326ce82007-03-11 14:48:29 +00003654 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003655 {
3656 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003657 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003658 return win_new_tabpage(n);
3659 }
3660 return FAIL;
3661}
3662
3663/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003664 * Create up to "maxcount" tabpages with empty windows.
3665 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003666 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667 int
3668make_tabpages(maxcount)
3669 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003671 int count = maxcount;
3672 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003673
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003674 /* Limit to 'tabpagemax' tabs. */
3675 if (count > p_tpm)
3676 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003677
3678#ifdef FEAT_AUTOCMD
3679 /*
3680 * Don't execute autocommands while creating the tab pages. Must do that
3681 * when putting the buffers in the windows.
3682 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003683 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003684#endif
3685
3686 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003687 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003688 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003689
3690#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003691 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003692#endif
3693
3694 /* return actual number of tab pages */
3695 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696}
3697
3698/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003699 * Return TRUE when "tpc" points to a valid tab page.
3700 */
3701 int
3702valid_tabpage(tpc)
3703 tabpage_T *tpc;
3704{
3705 tabpage_T *tp;
3706
3707 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3708 if (tp == tpc)
3709 return TRUE;
3710 return FALSE;
3711}
3712
3713/*
3714 * Find tab page "n" (first one is 1). Returns NULL when not found.
3715 */
3716 tabpage_T *
3717find_tabpage(n)
3718 int n;
3719{
3720 tabpage_T *tp;
3721 int i = 1;
3722
3723 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3724 ++i;
3725 return tp;
3726}
3727
3728/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003729 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003730 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003731 */
3732 int
3733tabpage_index(ftp)
3734 tabpage_T *ftp;
3735{
3736 int i = 1;
3737 tabpage_T *tp;
3738
3739 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3740 ++i;
3741 return i;
3742}
3743
3744/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003745 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003746 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003747 * FAIL.
3748 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003750 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003751leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003752 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003753 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003754 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003756 tabpage_T *tp = curtab;
3757
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003758 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003760 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003761 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003762 if (new_curbuf != curbuf)
3763 {
3764 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3765 if (curtab != tp)
3766 return FAIL;
3767 }
3768 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3769 if (curtab != tp)
3770 return FAIL;
3771 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003772 if (curtab != tp)
3773 return FAIL;
3774 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003775#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003776#if defined(FEAT_GUI)
3777 /* Remove the scrollbars. They may be added back later. */
3778 if (gui.in_use)
3779 gui_remove_scrollbars();
3780#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003781 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003782 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003783 tp->tp_firstwin = firstwin;
3784 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003785 tp->tp_old_Rows = Rows;
3786 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003787 firstwin = NULL;
3788 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003789 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003790}
3791
3792/*
3793 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003794 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003795 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3796 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003797 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003799enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003800 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003801 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003802 int trigger_enter_autocmds UNUSED;
3803 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003805 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003806 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003807
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003808 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003809 firstwin = tp->tp_firstwin;
3810 lastwin = tp->tp_lastwin;
3811 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003812
3813 /* We would like doing the TabEnter event first, but we don't have a
3814 * valid current window yet, which may break some commands.
3815 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003816 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3817 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003818 prevwin = next_prevwin;
3819
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003820 last_status(FALSE); /* status line may appear or disappear */
3821 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003822 must_redraw = CLEAR; /* need to redraw everything */
3823#ifdef FEAT_DIFF
3824 diff_need_scrollbind = TRUE;
3825#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003827 /* The tabpage line may have appeared or disappeared, may need to resize
3828 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003829 * frame sizes too. Use the stored value of p_ch, so that it can be
3830 * different for each tab page. */
3831 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003832 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3833#ifdef FEAT_GUI_TABLINE
3834 && !gui_use_tabline()
3835#endif
3836 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003837 shell_new_rows();
3838#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003839 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003840 shell_new_columns(); /* update window widths */
3841#endif
3842
3843#if defined(FEAT_GUI)
3844 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3845 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003846 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003847#endif
3848
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003849#ifdef FEAT_AUTOCMD
3850 /* Apply autocommands after updating the display, when 'rows' and
3851 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003852 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003853 {
3854 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3855 if (old_curbuf != curbuf)
3856 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3857 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003858#endif
3859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003860 redraw_all_later(CLEAR);
3861}
3862
3863/*
3864 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003865 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003866 */
3867 void
3868goto_tabpage(n)
3869 int n;
3870{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003871 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003872 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003873 int i;
3874
Bram Moolenaard68071d2006-05-02 22:08:30 +00003875 if (text_locked())
3876 {
3877 /* Not allowed when editing the command line. */
3878#ifdef FEAT_CMDWIN
3879 if (cmdwin_type != 0)
3880 EMSG(_(e_cmdwin));
3881 else
3882#endif
3883 EMSG(_(e_secure));
3884 return;
3885 }
3886
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003887 /* If there is only one it can't work. */
3888 if (first_tabpage->tp_next == NULL)
3889 {
3890 if (n > 1)
3891 beep_flush();
3892 return;
3893 }
3894
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003895 if (n == 0)
3896 {
3897 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003898 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003899 tp = first_tabpage;
3900 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003901 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003902 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003903 else if (n < 0)
3904 {
3905 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3906 * this N times. */
3907 ttp = curtab;
3908 for (i = n; i < 0; ++i)
3909 {
3910 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3911 tp = tp->tp_next)
3912 ;
3913 ttp = tp;
3914 }
3915 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003916 else if (n == 9999)
3917 {
3918 /* Go to last tab page. */
3919 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3920 ;
3921 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003922 else
3923 {
3924 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003925 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003926 if (tp == NULL)
3927 {
3928 beep_flush();
3929 return;
3930 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003931 }
3932
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003933 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003934
3935#ifdef FEAT_GUI_TABLINE
3936 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003937 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003938#endif
3939}
3940
3941/*
3942 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003943 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3944 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003945 * Note: doesn't update the GUI tab.
3946 */
3947 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003948goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003949 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003950 int trigger_enter_autocmds;
3951 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003952{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003953 /* Don't repeat a message in another tab page. */
3954 set_keep_msg(NULL, 0);
3955
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003956 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3957 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003958 {
3959 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003960 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3961 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003962 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003963 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3964 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003965 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003966}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
3968/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003969 * Enter window "wp" in tab page "tp".
3970 * Also updates the GUI tab.
3971 */
3972 void
3973goto_tabpage_win(tp, wp)
3974 tabpage_T *tp;
3975 win_T *wp;
3976{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003977 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003978 if (curtab == tp && win_valid(wp))
3979 {
3980 win_enter(wp, TRUE);
3981# ifdef FEAT_GUI_TABLINE
3982 if (gui_use_tabline())
3983 gui_mch_set_curtab(tabpage_index(curtab));
3984# endif
3985 }
3986}
3987
3988/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003989 * Move the current tab page to before tab page "nr".
3990 */
3991 void
3992tabpage_move(nr)
3993 int nr;
3994{
3995 int n = nr;
3996 tabpage_T *tp;
3997
3998 if (first_tabpage->tp_next == NULL)
3999 return;
4000
4001 /* Remove the current tab page from the list of tab pages. */
4002 if (curtab == first_tabpage)
4003 first_tabpage = curtab->tp_next;
4004 else
4005 {
4006 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4007 if (tp->tp_next == curtab)
4008 break;
4009 if (tp == NULL) /* "cannot happen" */
4010 return;
4011 tp->tp_next = curtab->tp_next;
4012 }
4013
4014 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004015 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004016 {
4017 curtab->tp_next = first_tabpage;
4018 first_tabpage = curtab;
4019 }
4020 else
4021 {
4022 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4023 --n;
4024 curtab->tp_next = tp->tp_next;
4025 tp->tp_next = curtab;
4026 }
4027
4028 /* Need to redraw the tabline. Tab page contents doesn't change. */
4029 redraw_tabline = TRUE;
4030}
4031
4032
4033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * Go to another window.
4035 * When jumping to another buffer, stop Visual mode. Do this before
4036 * changing windows so we can yank the selection into the '*' register.
4037 * When jumping to another window on the same buffer, adjust its cursor
4038 * position to keep the same Visual area.
4039 */
4040 void
4041win_goto(wp)
4042 win_T *wp;
4043{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004044#ifdef FEAT_CONCEAL
4045 win_T *owp = curwin;
4046#endif
4047
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004048 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
4050 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004051 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return;
4053 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004054#ifdef FEAT_AUTOCMD
4055 if (curbuf_locked())
4056 return;
4057#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (wp->w_buffer != curbuf)
4060 reset_VIsual_and_resel();
4061 else if (VIsual_active)
4062 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#ifdef FEAT_GUI
4065 need_mouse_correct = TRUE;
4066#endif
4067 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004068
4069#ifdef FEAT_CONCEAL
4070 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004071 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004072 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004073 if (curwin->w_p_cole > 0 && !msg_scrolled)
4074 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076}
4077
4078#if defined(FEAT_PERL) || defined(PROTO)
4079/*
4080 * Find window number "winnr" (counting top to bottom).
4081 */
4082 win_T *
4083win_find_nr(winnr)
4084 int winnr;
4085{
4086 win_T *wp;
4087
4088# ifdef FEAT_WINDOWS
4089 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4090 if (--winnr == 0)
4091 break;
4092 return wp;
4093# else
4094 return curwin;
4095# endif
4096}
4097#endif
4098
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004099#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4100 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004101/*
4102 * Find the tabpage for window "win".
4103 */
4104 tabpage_T *
4105win_find_tabpage(win)
4106 win_T *win;
4107{
4108 win_T *wp;
4109 tabpage_T *tp;
4110
4111 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004112 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4113 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004114 if (wp == win)
4115 return tp;
4116 return NULL;
4117}
4118#endif
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120#ifdef FEAT_VERTSPLIT
4121/*
4122 * Move to window above or below "count" times.
4123 */
4124 static void
4125win_goto_ver(up, count)
4126 int up; /* TRUE to go to win above */
4127 long count;
4128{
4129 frame_T *fr;
4130 frame_T *nfr;
4131 frame_T *foundfr;
4132
4133 foundfr = curwin->w_frame;
4134 while (count--)
4135 {
4136 /*
4137 * First go upwards in the tree of frames until we find a upwards or
4138 * downwards neighbor.
4139 */
4140 fr = foundfr;
4141 for (;;)
4142 {
4143 if (fr == topframe)
4144 goto end;
4145 if (up)
4146 nfr = fr->fr_prev;
4147 else
4148 nfr = fr->fr_next;
4149 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4150 break;
4151 fr = fr->fr_parent;
4152 }
4153
4154 /*
4155 * Now go downwards to find the bottom or top frame in it.
4156 */
4157 for (;;)
4158 {
4159 if (nfr->fr_layout == FR_LEAF)
4160 {
4161 foundfr = nfr;
4162 break;
4163 }
4164 fr = nfr->fr_child;
4165 if (nfr->fr_layout == FR_ROW)
4166 {
4167 /* Find the frame at the cursor row. */
4168 while (fr->fr_next != NULL
4169 && frame2win(fr)->w_wincol + fr->fr_width
4170 <= curwin->w_wincol + curwin->w_wcol)
4171 fr = fr->fr_next;
4172 }
4173 if (nfr->fr_layout == FR_COL && up)
4174 while (fr->fr_next != NULL)
4175 fr = fr->fr_next;
4176 nfr = fr;
4177 }
4178 }
4179end:
4180 if (foundfr != NULL)
4181 win_goto(foundfr->fr_win);
4182}
4183
4184/*
4185 * Move to left or right window.
4186 */
4187 static void
4188win_goto_hor(left, count)
4189 int left; /* TRUE to go to left win */
4190 long count;
4191{
4192 frame_T *fr;
4193 frame_T *nfr;
4194 frame_T *foundfr;
4195
4196 foundfr = curwin->w_frame;
4197 while (count--)
4198 {
4199 /*
4200 * First go upwards in the tree of frames until we find a left or
4201 * right neighbor.
4202 */
4203 fr = foundfr;
4204 for (;;)
4205 {
4206 if (fr == topframe)
4207 goto end;
4208 if (left)
4209 nfr = fr->fr_prev;
4210 else
4211 nfr = fr->fr_next;
4212 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4213 break;
4214 fr = fr->fr_parent;
4215 }
4216
4217 /*
4218 * Now go downwards to find the leftmost or rightmost frame in it.
4219 */
4220 for (;;)
4221 {
4222 if (nfr->fr_layout == FR_LEAF)
4223 {
4224 foundfr = nfr;
4225 break;
4226 }
4227 fr = nfr->fr_child;
4228 if (nfr->fr_layout == FR_COL)
4229 {
4230 /* Find the frame at the cursor row. */
4231 while (fr->fr_next != NULL
4232 && frame2win(fr)->w_winrow + fr->fr_height
4233 <= curwin->w_winrow + curwin->w_wrow)
4234 fr = fr->fr_next;
4235 }
4236 if (nfr->fr_layout == FR_ROW && left)
4237 while (fr->fr_next != NULL)
4238 fr = fr->fr_next;
4239 nfr = fr;
4240 }
4241 }
4242end:
4243 if (foundfr != NULL)
4244 win_goto(foundfr->fr_win);
4245}
4246#endif
4247
4248/*
4249 * Make window "wp" the current window.
4250 */
4251 void
4252win_enter(wp, undo_sync)
4253 win_T *wp;
4254 int undo_sync;
4255{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004256 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257}
4258
4259/*
4260 * Make window wp the current window.
4261 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4262 * been closed and isn't valid.
4263 */
4264 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004265win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 win_T *wp;
4267 int undo_sync;
4268 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004269 int trigger_enter_autocmds UNUSED;
4270 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272#ifdef FEAT_AUTOCMD
4273 int other_buffer = FALSE;
4274#endif
4275
4276 if (wp == curwin && !curwin_invalid) /* nothing to do */
4277 return;
4278
4279#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004280 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 /*
4283 * Be careful: If autocommands delete the window, return now.
4284 */
4285 if (wp->w_buffer != curbuf)
4286 {
4287 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4288 other_buffer = TRUE;
4289 if (!win_valid(wp))
4290 return;
4291 }
4292 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4293 if (!win_valid(wp))
4294 return;
4295# ifdef FEAT_EVAL
4296 /* autocmds may abort script processing */
4297 if (aborting())
4298 return;
4299# endif
4300 }
4301#endif
4302
4303 /* sync undo before leaving the current buffer */
4304 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004305 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004306
4307 /* Might need to scroll the old window before switching, e.g., when the
4308 * cursor was moved. */
4309 update_topline();
4310
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 /* may have to copy the buffer options when 'cpo' contains 'S' */
4312 if (wp->w_buffer != curbuf)
4313 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4314 if (!curwin_invalid)
4315 {
4316 prevwin = curwin; /* remember for CTRL-W p */
4317 curwin->w_redr_status = TRUE;
4318 }
4319 curwin = wp;
4320 curbuf = wp->w_buffer;
4321 check_cursor();
4322#ifdef FEAT_VIRTUALEDIT
4323 if (!virtual_active())
4324 curwin->w_cursor.coladd = 0;
4325#endif
4326 changed_line_abv_curs(); /* assume cursor position needs updating */
4327
4328 if (curwin->w_localdir != NULL)
4329 {
4330 /* Window has a local directory: Save current directory as global
4331 * directory (unless that was done already) and change to the local
4332 * directory. */
4333 if (globaldir == NULL)
4334 {
4335 char_u cwd[MAXPATHL];
4336
4337 if (mch_dirname(cwd, MAXPATHL) == OK)
4338 globaldir = vim_strsave(cwd);
4339 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004340 if (mch_chdir((char *)curwin->w_localdir) == 0)
4341 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 else if (globaldir != NULL)
4344 {
4345 /* Window doesn't have a local directory and we are not in the global
4346 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004347 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 vim_free(globaldir);
4349 globaldir = NULL;
4350 shorten_fnames(TRUE);
4351 }
4352
4353#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004354 if (trigger_enter_autocmds)
4355 {
4356 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4357 if (other_buffer)
4358 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#endif
4361
4362#ifdef FEAT_TITLE
4363 maketitle();
4364#endif
4365 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004366 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 if (restart_edit)
4368 redraw_later(VALID); /* causes status line redraw */
4369
4370 /* set window height to desired minimal value */
4371 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4372 win_setheight((int)p_wh);
4373 else if (curwin->w_height == 0)
4374 win_setheight(1);
4375
4376#ifdef FEAT_VERTSPLIT
4377 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004378 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 win_setwidth((int)p_wiw);
4380#endif
4381
4382#ifdef FEAT_MOUSE
4383 setmouse(); /* in case jumped to/from help buffer */
4384#endif
4385
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004386 /* Change directories when the 'acd' option is set. */
4387 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388}
4389
4390#endif /* FEAT_WINDOWS */
4391
4392#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4393/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004394 * Jump to the first open window that contains buffer "buf", if one exists.
4395 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
4397 win_T *
4398buf_jump_open_win(buf)
4399 buf_T *buf;
4400{
4401# ifdef FEAT_WINDOWS
4402 win_T *wp;
4403
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004404 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (wp->w_buffer == buf)
4406 break;
4407 if (wp != NULL)
4408 win_enter(wp, FALSE);
4409 return wp;
4410# else
4411 if (curwin->w_buffer == buf)
4412 return curwin;
4413 return NULL;
4414# endif
4415}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004416
4417/*
4418 * Jump to the first open window in any tab page that contains buffer "buf",
4419 * if one exists.
4420 * Returns a pointer to the window found, otherwise NULL.
4421 */
4422 win_T *
4423buf_jump_open_tab(buf)
4424 buf_T *buf;
4425{
4426# ifdef FEAT_WINDOWS
4427 win_T *wp;
4428 tabpage_T *tp;
4429
4430 /* First try the current tab page. */
4431 wp = buf_jump_open_win(buf);
4432 if (wp != NULL)
4433 return wp;
4434
4435 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4436 if (tp != curtab)
4437 {
4438 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4439 if (wp->w_buffer == buf)
4440 break;
4441 if (wp != NULL)
4442 {
4443 goto_tabpage_win(tp, wp);
4444 if (curwin != wp)
4445 wp = NULL; /* something went wrong */
4446 break;
4447 }
4448 }
4449
4450 return wp;
4451# else
4452 if (curwin->w_buffer == buf)
4453 return curwin;
4454 return NULL;
4455# endif
4456}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457#endif
4458
4459/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004460 * Allocate a window structure and link it in the window list when "hidden" is
4461 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004464win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004465 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004466 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004468 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 /*
4471 * allocate window structure and linesizes arrays
4472 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004473 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004474 if (new_wp == NULL)
4475 return NULL;
4476
4477 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004479 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
4482
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483#ifdef FEAT_EVAL
4484 /* init w: variables */
4485 new_wp->w_vars = dict_alloc();
4486 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004488 win_free_lsize(new_wp);
4489 vim_free(new_wp);
4490 return NULL;
4491 }
4492 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004493#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004494
4495#ifdef FEAT_AUTOCMD
4496 /* Don't execute autocommands while the window is not properly
4497 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4498 * event. */
4499 block_autocmds();
4500#endif
4501 /*
4502 * link the window in the window list
4503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004505 if (!hidden)
4506 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#endif
4508#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004509 new_wp->w_wincol = 0;
4510 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#endif
4512
Bram Moolenaar429fa852013-04-15 12:27:36 +02004513 /* position the display and the cursor at the top of the file. */
4514 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004516 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004518 new_wp->w_botline = 2;
4519 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004521 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#endif
4523
Bram Moolenaar429fa852013-04-15 12:27:36 +02004524 /* We won't calculate w_fraction until resizing the window */
4525 new_wp->w_fraction = 0;
4526 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004529 if (gui.in_use)
4530 {
4531 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4532 SBAR_LEFT, new_wp);
4533 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4534 SBAR_RIGHT, new_wp);
4535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#endif
4537#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004538 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004540#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004541 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004542#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004543#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004544 new_wp->w_match_head = NULL;
4545 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004546#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004547 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548}
4549
4550#if defined(FEAT_WINDOWS) || defined(PROTO)
4551
4552/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004553 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 */
4555 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004556win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004558 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559{
4560 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004561 buf_T *buf;
4562 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004564#ifdef FEAT_FOLDING
4565 clearFolding(wp);
4566#endif
4567
4568 /* reduce the reference count to the argument list. */
4569 alist_unlink(wp->w_alist);
4570
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004571#ifdef FEAT_AUTOCMD
4572 /* Don't execute autocommands while the window is halfway being deleted.
4573 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004574 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004575#endif
4576
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004577#ifdef FEAT_LUA
4578 lua_window_free(wp);
4579#endif
4580
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004581#ifdef FEAT_MZSCHEME
4582 mzscheme_window_free(wp);
4583#endif
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#ifdef FEAT_PERL
4586 perl_win_free(wp);
4587#endif
4588
4589#ifdef FEAT_PYTHON
4590 python_window_free(wp);
4591#endif
4592
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004593#ifdef FEAT_PYTHON3
4594 python3_window_free(wp);
4595#endif
4596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597#ifdef FEAT_TCL
4598 tcl_window_free(wp);
4599#endif
4600
4601#ifdef FEAT_RUBY
4602 ruby_window_free(wp);
4603#endif
4604
4605 clear_winopt(&wp->w_onebuf_opt);
4606 clear_winopt(&wp->w_allbuf_opt);
4607
4608#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004609 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4610 hash_init(&wp->w_vars->dv_hashtab);
4611 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#endif
4613
4614 if (prevwin == wp)
4615 prevwin = NULL;
4616 win_free_lsize(wp);
4617
4618 for (i = 0; i < wp->w_tagstacklen; ++i)
4619 vim_free(wp->w_tagstack[i].tagname);
4620
4621 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004622
Bram Moolenaarff18df02013-07-24 17:51:57 +02004623 /* Remove the window from the b_wininfo lists, it may happen that the
4624 * freed memory is re-used for another window. */
4625 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4626 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4627 if (wip->wi_win == wp)
4628 wip->wi_win = NULL;
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004631 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634#ifdef FEAT_JUMPLIST
4635 free_jumplist(wp);
4636#endif
4637
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004638#ifdef FEAT_QUICKFIX
4639 qf_free_all(wp);
4640#endif
4641
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#ifdef FEAT_GUI
4643 if (gui.in_use)
4644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4646 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4647 }
4648#endif /* FEAT_GUI */
4649
Bram Moolenaar860cae12010-06-05 23:22:07 +02004650#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004651 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004652#endif
4653
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004654#ifdef FEAT_AUTOCMD
4655 if (wp != aucmd_win)
4656#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004657 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004658#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004659 if (autocmd_busy)
4660 {
4661 wp->w_next = au_pending_free_win;
4662 au_pending_free_win = wp;
4663 }
4664 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004665#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004666 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004667
4668#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004669 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671}
4672
4673/*
4674 * Append window "wp" in the window list after window "after".
4675 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004676 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677win_append(after, wp)
4678 win_T *after, *wp;
4679{
4680 win_T *before;
4681
4682 if (after == NULL) /* after NULL is in front of the first */
4683 before = firstwin;
4684 else
4685 before = after->w_next;
4686
4687 wp->w_next = before;
4688 wp->w_prev = after;
4689 if (after == NULL)
4690 firstwin = wp;
4691 else
4692 after->w_next = wp;
4693 if (before == NULL)
4694 lastwin = wp;
4695 else
4696 before->w_prev = wp;
4697}
4698
4699/*
4700 * Remove a window from the window list.
4701 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004702 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004703win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004705 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 if (wp->w_prev != NULL)
4708 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004709 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004711 else
4712 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (wp->w_next != NULL)
4714 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004715 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004717 else
4718 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719}
4720
4721/*
4722 * Append frame "frp" in a frame list after frame "after".
4723 */
4724 static void
4725frame_append(after, frp)
4726 frame_T *after, *frp;
4727{
4728 frp->fr_next = after->fr_next;
4729 after->fr_next = frp;
4730 if (frp->fr_next != NULL)
4731 frp->fr_next->fr_prev = frp;
4732 frp->fr_prev = after;
4733}
4734
4735/*
4736 * Insert frame "frp" in a frame list before frame "before".
4737 */
4738 static void
4739frame_insert(before, frp)
4740 frame_T *before, *frp;
4741{
4742 frp->fr_next = before;
4743 frp->fr_prev = before->fr_prev;
4744 before->fr_prev = frp;
4745 if (frp->fr_prev != NULL)
4746 frp->fr_prev->fr_next = frp;
4747 else
4748 frp->fr_parent->fr_child = frp;
4749}
4750
4751/*
4752 * Remove a frame from a frame list.
4753 */
4754 static void
4755frame_remove(frp)
4756 frame_T *frp;
4757{
4758 if (frp->fr_prev != NULL)
4759 frp->fr_prev->fr_next = frp->fr_next;
4760 else
4761 frp->fr_parent->fr_child = frp->fr_next;
4762 if (frp->fr_next != NULL)
4763 frp->fr_next->fr_prev = frp->fr_prev;
4764}
4765
4766#endif /* FEAT_WINDOWS */
4767
4768/*
4769 * Allocate w_lines[] for window "wp".
4770 * Return FAIL for failure, OK for success.
4771 */
4772 int
4773win_alloc_lines(wp)
4774 win_T *wp;
4775{
4776 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004777 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (wp->w_lines == NULL)
4779 return FAIL;
4780 return OK;
4781}
4782
4783/*
4784 * free lsize arrays for a window
4785 */
4786 void
4787win_free_lsize(wp)
4788 win_T *wp;
4789{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004790 /* TODO: why would wp be NULL here? */
4791 if (wp != NULL)
4792 {
4793 vim_free(wp->w_lines);
4794 wp->w_lines = NULL;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796}
4797
4798/*
4799 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004800 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
4802 void
4803shell_new_rows()
4804{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004805 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 if (firstwin == NULL) /* not initialized yet */
4808 return;
4809#ifdef FEAT_WINDOWS
4810 if (h < frame_minheight(topframe, NULL))
4811 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004812
4813 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 * that doesn't result in the right height, forget about that option. */
4815 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004816 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 frame_new_height(topframe, h, FALSE, FALSE);
4818
4819 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4820#else
4821 if (h < 1)
4822 h = 1;
4823 win_new_height(firstwin, h);
4824#endif
4825 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004826#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004827 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004828#endif
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#if 0
4831 /* Disabled: don't want making the screen smaller make a window larger. */
4832 if (p_ea)
4833 win_equal(curwin, FALSE, 'v');
4834#endif
4835}
4836
4837#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4838/*
4839 * Called from win_new_shellsize() after Columns changed.
4840 */
4841 void
4842shell_new_columns()
4843{
4844 if (firstwin == NULL) /* not initialized yet */
4845 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004846
4847 /* First try setting the widths of windows with 'winfixwidth'. If that
4848 * doesn't result in the right width, forget about that option. */
4849 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004850 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004851 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4854#if 0
4855 /* Disabled: don't want making the screen smaller make a window larger. */
4856 if (p_ea)
4857 win_equal(curwin, FALSE, 'h');
4858#endif
4859}
4860#endif
4861
4862#if defined(FEAT_CMDWIN) || defined(PROTO)
4863/*
4864 * Save the size of all windows in "gap".
4865 */
4866 void
4867win_size_save(gap)
4868 garray_T *gap;
4869
4870{
4871 win_T *wp;
4872
4873 ga_init2(gap, (int)sizeof(int), 1);
4874 if (ga_grow(gap, win_count() * 2) == OK)
4875 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4876 {
4877 ((int *)gap->ga_data)[gap->ga_len++] =
4878 wp->w_width + wp->w_vsep_width;
4879 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4880 }
4881}
4882
4883/*
4884 * Restore window sizes, but only if the number of windows is still the same.
4885 * Does not free the growarray.
4886 */
4887 void
4888win_size_restore(gap)
4889 garray_T *gap;
4890{
4891 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004892 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894 if (win_count() * 2 == gap->ga_len)
4895 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004896 /* The order matters, because frames contain other frames, but it's
4897 * difficult to get right. The easy way out is to do it twice. */
4898 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004900 i = 0;
4901 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4902 {
4903 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4904 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 /* recompute the window positions */
4908 (void)win_comp_pos();
4909 }
4910}
4911#endif /* FEAT_CMDWIN */
4912
4913#if defined(FEAT_WINDOWS) || defined(PROTO)
4914/*
4915 * Update the position for all windows, using the width and height of the
4916 * frames.
4917 * Returns the row just after the last window.
4918 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004919 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920win_comp_pos()
4921{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004922 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 int col = 0;
4924
4925 frame_comp_pos(topframe, &row, &col);
4926 return row;
4927}
4928
4929/*
4930 * Update the position of the windows in frame "topfrp", using the width and
4931 * height of the frames.
4932 * "*row" and "*col" are the top-left position of the frame. They are updated
4933 * to the bottom-right position plus one.
4934 */
4935 static void
4936frame_comp_pos(topfrp, row, col)
4937 frame_T *topfrp;
4938 int *row;
4939 int *col;
4940{
4941 win_T *wp;
4942 frame_T *frp;
4943#ifdef FEAT_VERTSPLIT
4944 int startcol;
4945 int startrow;
4946#endif
4947
4948 wp = topfrp->fr_win;
4949 if (wp != NULL)
4950 {
4951 if (wp->w_winrow != *row
4952#ifdef FEAT_VERTSPLIT
4953 || wp->w_wincol != *col
4954#endif
4955 )
4956 {
4957 /* position changed, redraw */
4958 wp->w_winrow = *row;
4959#ifdef FEAT_VERTSPLIT
4960 wp->w_wincol = *col;
4961#endif
4962 redraw_win_later(wp, NOT_VALID);
4963 wp->w_redr_status = TRUE;
4964 }
4965 *row += wp->w_height + wp->w_status_height;
4966#ifdef FEAT_VERTSPLIT
4967 *col += wp->w_width + wp->w_vsep_width;
4968#endif
4969 }
4970 else
4971 {
4972#ifdef FEAT_VERTSPLIT
4973 startrow = *row;
4974 startcol = *col;
4975#endif
4976 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4977 {
4978#ifdef FEAT_VERTSPLIT
4979 if (topfrp->fr_layout == FR_ROW)
4980 *row = startrow; /* all frames are at the same row */
4981 else
4982 *col = startcol; /* all frames are at the same col */
4983#endif
4984 frame_comp_pos(frp, row, col);
4985 }
4986 }
4987}
4988
4989#endif /* FEAT_WINDOWS */
4990
4991/*
4992 * Set current window height and take care of repositioning other windows to
4993 * fit around it.
4994 */
4995 void
4996win_setheight(height)
4997 int height;
4998{
4999 win_setheight_win(height, curwin);
5000}
5001
5002/*
5003 * Set the window height of window "win" and take care of repositioning other
5004 * windows to fit around it.
5005 */
5006 void
5007win_setheight_win(height, win)
5008 int height;
5009 win_T *win;
5010{
5011 int row;
5012
5013 if (win == curwin)
5014 {
5015 /* Always keep current window at least one line high, even when
5016 * 'winminheight' is zero. */
5017#ifdef FEAT_WINDOWS
5018 if (height < p_wmh)
5019 height = p_wmh;
5020#endif
5021 if (height == 0)
5022 height = 1;
5023 }
5024
5025#ifdef FEAT_WINDOWS
5026 frame_setheight(win->w_frame, height + win->w_status_height);
5027
5028 /* recompute the window positions */
5029 row = win_comp_pos();
5030#else
5031 if (height > topframe->fr_height)
5032 height = topframe->fr_height;
5033 win->w_height = height;
5034 row = height;
5035#endif
5036
5037 /*
5038 * If there is extra space created between the last window and the command
5039 * line, clear it.
5040 */
5041 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5042 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5043 cmdline_row = row;
5044 msg_row = row;
5045 msg_col = 0;
5046
5047 redraw_all_later(NOT_VALID);
5048}
5049
5050#if defined(FEAT_WINDOWS) || defined(PROTO)
5051
5052/*
5053 * Set the height of a frame to "height" and take care that all frames and
5054 * windows inside it are resized. Also resize frames on the left and right if
5055 * the are in the same FR_ROW frame.
5056 *
5057 * Strategy:
5058 * If the frame is part of a FR_COL frame, try fitting the frame in that
5059 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5060 * go to containing frames to resize them and make room.
5061 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5062 * Check for the minimal height of the FR_ROW frame.
5063 * At the top level we can also use change the command line height.
5064 */
5065 static void
5066frame_setheight(curfrp, height)
5067 frame_T *curfrp;
5068 int height;
5069{
5070 int room; /* total number of lines available */
5071 int take; /* number of lines taken from other windows */
5072 int room_cmdline; /* lines available from cmdline */
5073 int run;
5074 frame_T *frp;
5075 int h;
5076 int room_reserved;
5077
5078 /* If the height already is the desired value, nothing to do. */
5079 if (curfrp->fr_height == height)
5080 return;
5081
5082 if (curfrp->fr_parent == NULL)
5083 {
5084 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005085 if (height > ROWS_AVAIL)
5086 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (height > 0)
5088 frame_new_height(curfrp, height, FALSE, FALSE);
5089 }
5090 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5091 {
5092 /* Row of frames: Also need to resize frames left and right of this
5093 * one. First check for the minimal height of these. */
5094 h = frame_minheight(curfrp->fr_parent, NULL);
5095 if (height < h)
5096 height = h;
5097 frame_setheight(curfrp->fr_parent, height);
5098 }
5099 else
5100 {
5101 /*
5102 * Column of frames: try to change only frames in this column.
5103 */
5104#ifdef FEAT_VERTSPLIT
5105 /*
5106 * Do this twice:
5107 * 1: compute room available, if it's not enough try resizing the
5108 * containing frame.
5109 * 2: compute the room available and adjust the height to it.
5110 * Try not to reduce the height of a window with 'winfixheight' set.
5111 */
5112 for (run = 1; run <= 2; ++run)
5113#else
5114 for (;;)
5115#endif
5116 {
5117 room = 0;
5118 room_reserved = 0;
5119 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5120 frp = frp->fr_next)
5121 {
5122 if (frp != curfrp
5123 && frp->fr_win != NULL
5124 && frp->fr_win->w_p_wfh)
5125 room_reserved += frp->fr_height;
5126 room += frp->fr_height;
5127 if (frp != curfrp)
5128 room -= frame_minheight(frp, NULL);
5129 }
5130#ifdef FEAT_VERTSPLIT
5131 if (curfrp->fr_width != Columns)
5132 room_cmdline = 0;
5133 else
5134#endif
5135 {
5136 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5137 + lastwin->w_height + lastwin->w_status_height);
5138 if (room_cmdline < 0)
5139 room_cmdline = 0;
5140 }
5141
5142 if (height <= room + room_cmdline)
5143 break;
5144#ifdef FEAT_VERTSPLIT
5145 if (run == 2 || curfrp->fr_width == Columns)
5146#endif
5147 {
5148 if (height > room + room_cmdline)
5149 height = room + room_cmdline;
5150 break;
5151 }
5152#ifdef FEAT_VERTSPLIT
5153 frame_setheight(curfrp->fr_parent, height
5154 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5155#endif
5156 /*NOTREACHED*/
5157 }
5158
5159 /*
5160 * Compute the number of lines we will take from others frames (can be
5161 * negative!).
5162 */
5163 take = height - curfrp->fr_height;
5164
5165 /* If there is not enough room, also reduce the height of a window
5166 * with 'winfixheight' set. */
5167 if (height > room + room_cmdline - room_reserved)
5168 room_reserved = room + room_cmdline - height;
5169 /* If there is only a 'winfixheight' window and making the
5170 * window smaller, need to make the other window taller. */
5171 if (take < 0 && room - curfrp->fr_height < room_reserved)
5172 room_reserved = 0;
5173
5174 if (take > 0 && room_cmdline > 0)
5175 {
5176 /* use lines from cmdline first */
5177 if (take < room_cmdline)
5178 room_cmdline = take;
5179 take -= room_cmdline;
5180 topframe->fr_height += room_cmdline;
5181 }
5182
5183 /*
5184 * set the current frame to the new height
5185 */
5186 frame_new_height(curfrp, height, FALSE, FALSE);
5187
5188 /*
5189 * First take lines from the frames after the current frame. If
5190 * that is not enough, takes lines from frames above the current
5191 * frame.
5192 */
5193 for (run = 0; run < 2; ++run)
5194 {
5195 if (run == 0)
5196 frp = curfrp->fr_next; /* 1st run: start with next window */
5197 else
5198 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5199 while (frp != NULL && take != 0)
5200 {
5201 h = frame_minheight(frp, NULL);
5202 if (room_reserved > 0
5203 && frp->fr_win != NULL
5204 && frp->fr_win->w_p_wfh)
5205 {
5206 if (room_reserved >= frp->fr_height)
5207 room_reserved -= frp->fr_height;
5208 else
5209 {
5210 if (frp->fr_height - room_reserved > take)
5211 room_reserved = frp->fr_height - take;
5212 take -= frp->fr_height - room_reserved;
5213 frame_new_height(frp, room_reserved, FALSE, FALSE);
5214 room_reserved = 0;
5215 }
5216 }
5217 else
5218 {
5219 if (frp->fr_height - take < h)
5220 {
5221 take -= frp->fr_height - h;
5222 frame_new_height(frp, h, FALSE, FALSE);
5223 }
5224 else
5225 {
5226 frame_new_height(frp, frp->fr_height - take,
5227 FALSE, FALSE);
5228 take = 0;
5229 }
5230 }
5231 if (run == 0)
5232 frp = frp->fr_next;
5233 else
5234 frp = frp->fr_prev;
5235 }
5236 }
5237 }
5238}
5239
5240#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5241/*
5242 * Set current window width and take care of repositioning other windows to
5243 * fit around it.
5244 */
5245 void
5246win_setwidth(width)
5247 int width;
5248{
5249 win_setwidth_win(width, curwin);
5250}
5251
5252 void
5253win_setwidth_win(width, wp)
5254 int width;
5255 win_T *wp;
5256{
5257 /* Always keep current window at least one column wide, even when
5258 * 'winminwidth' is zero. */
5259 if (wp == curwin)
5260 {
5261 if (width < p_wmw)
5262 width = p_wmw;
5263 if (width == 0)
5264 width = 1;
5265 }
5266
5267 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5268
5269 /* recompute the window positions */
5270 (void)win_comp_pos();
5271
5272 redraw_all_later(NOT_VALID);
5273}
5274
5275/*
5276 * Set the width of a frame to "width" and take care that all frames and
5277 * windows inside it are resized. Also resize frames above and below if the
5278 * are in the same FR_ROW frame.
5279 *
5280 * Strategy is similar to frame_setheight().
5281 */
5282 static void
5283frame_setwidth(curfrp, width)
5284 frame_T *curfrp;
5285 int width;
5286{
5287 int room; /* total number of lines available */
5288 int take; /* number of lines taken from other windows */
5289 int run;
5290 frame_T *frp;
5291 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005292 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /* If the width already is the desired value, nothing to do. */
5295 if (curfrp->fr_width == width)
5296 return;
5297
5298 if (curfrp->fr_parent == NULL)
5299 /* topframe: can't change width */
5300 return;
5301
5302 if (curfrp->fr_parent->fr_layout == FR_COL)
5303 {
5304 /* Column of frames: Also need to resize frames above and below of
5305 * this one. First check for the minimal width of these. */
5306 w = frame_minwidth(curfrp->fr_parent, NULL);
5307 if (width < w)
5308 width = w;
5309 frame_setwidth(curfrp->fr_parent, width);
5310 }
5311 else
5312 {
5313 /*
5314 * Row of frames: try to change only frames in this row.
5315 *
5316 * Do this twice:
5317 * 1: compute room available, if it's not enough try resizing the
5318 * containing frame.
5319 * 2: compute the room available and adjust the width to it.
5320 */
5321 for (run = 1; run <= 2; ++run)
5322 {
5323 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005324 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5326 frp = frp->fr_next)
5327 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005328 if (frp != curfrp
5329 && frp->fr_win != NULL
5330 && frp->fr_win->w_p_wfw)
5331 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 room += frp->fr_width;
5333 if (frp != curfrp)
5334 room -= frame_minwidth(frp, NULL);
5335 }
5336
5337 if (width <= room)
5338 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005339 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
5341 if (width > room)
5342 width = room;
5343 break;
5344 }
5345 frame_setwidth(curfrp->fr_parent, width
5346 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5347 }
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 /*
5350 * Compute the number of lines we will take from others frames (can be
5351 * negative!).
5352 */
5353 take = width - curfrp->fr_width;
5354
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005355 /* If there is not enough room, also reduce the width of a window
5356 * with 'winfixwidth' set. */
5357 if (width > room - room_reserved)
5358 room_reserved = room - width;
5359 /* If there is only a 'winfixwidth' window and making the
5360 * window smaller, need to make the other window narrower. */
5361 if (take < 0 && room - curfrp->fr_width < room_reserved)
5362 room_reserved = 0;
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 /*
5365 * set the current frame to the new width
5366 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005367 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369 /*
5370 * First take lines from the frames right of the current frame. If
5371 * that is not enough, takes lines from frames left of the current
5372 * frame.
5373 */
5374 for (run = 0; run < 2; ++run)
5375 {
5376 if (run == 0)
5377 frp = curfrp->fr_next; /* 1st run: start with next window */
5378 else
5379 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5380 while (frp != NULL && take != 0)
5381 {
5382 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005383 if (room_reserved > 0
5384 && frp->fr_win != NULL
5385 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005387 if (room_reserved >= frp->fr_width)
5388 room_reserved -= frp->fr_width;
5389 else
5390 {
5391 if (frp->fr_width - room_reserved > take)
5392 room_reserved = frp->fr_width - take;
5393 take -= frp->fr_width - room_reserved;
5394 frame_new_width(frp, room_reserved, FALSE, FALSE);
5395 room_reserved = 0;
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 else
5399 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005400 if (frp->fr_width - take < w)
5401 {
5402 take -= frp->fr_width - w;
5403 frame_new_width(frp, w, FALSE, FALSE);
5404 }
5405 else
5406 {
5407 frame_new_width(frp, frp->fr_width - take,
5408 FALSE, FALSE);
5409 take = 0;
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412 if (run == 0)
5413 frp = frp->fr_next;
5414 else
5415 frp = frp->fr_prev;
5416 }
5417 }
5418 }
5419}
5420#endif /* FEAT_VERTSPLIT */
5421
5422/*
5423 * Check 'winminheight' for a valid value.
5424 */
5425 void
5426win_setminheight()
5427{
5428 int room;
5429 int first = TRUE;
5430 win_T *wp;
5431
5432 /* loop until there is a 'winminheight' that is possible */
5433 while (p_wmh > 0)
5434 {
5435 /* TODO: handle vertical splits */
5436 room = -p_wh;
5437 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5438 room += wp->w_height - p_wmh;
5439 if (room >= 0)
5440 break;
5441 --p_wmh;
5442 if (first)
5443 {
5444 EMSG(_(e_noroom));
5445 first = FALSE;
5446 }
5447 }
5448}
5449
5450#ifdef FEAT_MOUSE
5451
5452/*
5453 * Status line of dragwin is dragged "offset" lines down (negative is up).
5454 */
5455 void
5456win_drag_status_line(dragwin, offset)
5457 win_T *dragwin;
5458 int offset;
5459{
5460 frame_T *curfr;
5461 frame_T *fr;
5462 int room;
5463 int row;
5464 int up; /* if TRUE, drag status line up, otherwise down */
5465 int n;
5466
5467 fr = dragwin->w_frame;
5468 curfr = fr;
5469 if (fr != topframe) /* more than one window */
5470 {
5471 fr = fr->fr_parent;
5472 /* When the parent frame is not a column of frames, its parent should
5473 * be. */
5474 if (fr->fr_layout != FR_COL)
5475 {
5476 curfr = fr;
5477 if (fr != topframe) /* only a row of windows, may drag statusline */
5478 fr = fr->fr_parent;
5479 }
5480 }
5481
5482 /* If this is the last frame in a column, may want to resize the parent
5483 * frame instead (go two up to skip a row of frames). */
5484 while (curfr != topframe && curfr->fr_next == NULL)
5485 {
5486 if (fr != topframe)
5487 fr = fr->fr_parent;
5488 curfr = fr;
5489 if (fr != topframe)
5490 fr = fr->fr_parent;
5491 }
5492
5493 if (offset < 0) /* drag up */
5494 {
5495 up = TRUE;
5496 offset = -offset;
5497 /* sum up the room of the current frame and above it */
5498 if (fr == curfr)
5499 {
5500 /* only one window */
5501 room = fr->fr_height - frame_minheight(fr, NULL);
5502 }
5503 else
5504 {
5505 room = 0;
5506 for (fr = fr->fr_child; ; fr = fr->fr_next)
5507 {
5508 room += fr->fr_height - frame_minheight(fr, NULL);
5509 if (fr == curfr)
5510 break;
5511 }
5512 }
5513 fr = curfr->fr_next; /* put fr at frame that grows */
5514 }
5515 else /* drag down */
5516 {
5517 up = FALSE;
5518 /*
5519 * Only dragging the last status line can reduce p_ch.
5520 */
5521 room = Rows - cmdline_row;
5522 if (curfr->fr_next == NULL)
5523 room -= 1;
5524 else
5525 room -= p_ch;
5526 if (room < 0)
5527 room = 0;
5528 /* sum up the room of frames below of the current one */
5529 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5530 room += fr->fr_height - frame_minheight(fr, NULL);
5531 fr = curfr; /* put fr at window that grows */
5532 }
5533
5534 if (room < offset) /* Not enough room */
5535 offset = room; /* Move as far as we can */
5536 if (offset <= 0)
5537 return;
5538
5539 /*
5540 * Grow frame fr by "offset" lines.
5541 * Doesn't happen when dragging the last status line up.
5542 */
5543 if (fr != NULL)
5544 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5545
5546 if (up)
5547 fr = curfr; /* current frame gets smaller */
5548 else
5549 fr = curfr->fr_next; /* next frame gets smaller */
5550
5551 /*
5552 * Now make the other frames smaller.
5553 */
5554 while (fr != NULL && offset > 0)
5555 {
5556 n = frame_minheight(fr, NULL);
5557 if (fr->fr_height - offset <= n)
5558 {
5559 offset -= fr->fr_height - n;
5560 frame_new_height(fr, n, !up, FALSE);
5561 }
5562 else
5563 {
5564 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5565 break;
5566 }
5567 if (up)
5568 fr = fr->fr_prev;
5569 else
5570 fr = fr->fr_next;
5571 }
5572 row = win_comp_pos();
5573 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5574 cmdline_row = row;
5575 p_ch = Rows - cmdline_row;
5576 if (p_ch < 1)
5577 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005578 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005579 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 showmode();
5581}
5582
5583#ifdef FEAT_VERTSPLIT
5584/*
5585 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5586 */
5587 void
5588win_drag_vsep_line(dragwin, offset)
5589 win_T *dragwin;
5590 int offset;
5591{
5592 frame_T *curfr;
5593 frame_T *fr;
5594 int room;
5595 int left; /* if TRUE, drag separator line left, otherwise right */
5596 int n;
5597
5598 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005599 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return;
5601 curfr = fr;
5602 fr = fr->fr_parent;
5603 /* When the parent frame is not a row of frames, its parent should be. */
5604 if (fr->fr_layout != FR_ROW)
5605 {
5606 if (fr == topframe) /* only a column of windows (cannot happen?) */
5607 return;
5608 curfr = fr;
5609 fr = fr->fr_parent;
5610 }
5611
5612 /* If this is the last frame in a row, may want to resize a parent
5613 * frame instead. */
5614 while (curfr->fr_next == NULL)
5615 {
5616 if (fr == topframe)
5617 break;
5618 curfr = fr;
5619 fr = fr->fr_parent;
5620 if (fr != topframe)
5621 {
5622 curfr = fr;
5623 fr = fr->fr_parent;
5624 }
5625 }
5626
5627 if (offset < 0) /* drag left */
5628 {
5629 left = TRUE;
5630 offset = -offset;
5631 /* sum up the room of the current frame and left of it */
5632 room = 0;
5633 for (fr = fr->fr_child; ; fr = fr->fr_next)
5634 {
5635 room += fr->fr_width - frame_minwidth(fr, NULL);
5636 if (fr == curfr)
5637 break;
5638 }
5639 fr = curfr->fr_next; /* put fr at frame that grows */
5640 }
5641 else /* drag right */
5642 {
5643 left = FALSE;
5644 /* sum up the room of frames right of the current one */
5645 room = 0;
5646 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5647 room += fr->fr_width - frame_minwidth(fr, NULL);
5648 fr = curfr; /* put fr at window that grows */
5649 }
5650
5651 if (room < offset) /* Not enough room */
5652 offset = room; /* Move as far as we can */
5653 if (offset <= 0) /* No room at all, quit. */
5654 return;
5655
5656 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005657 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659 /* shrink other frames: current and at the left or at the right */
5660 if (left)
5661 fr = curfr; /* current frame gets smaller */
5662 else
5663 fr = curfr->fr_next; /* next frame gets smaller */
5664
5665 while (fr != NULL && offset > 0)
5666 {
5667 n = frame_minwidth(fr, NULL);
5668 if (fr->fr_width - offset <= n)
5669 {
5670 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005671 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673 else
5674 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005675 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 break;
5677 }
5678 if (left)
5679 fr = fr->fr_prev;
5680 else
5681 fr = fr->fr_next;
5682 }
5683 (void)win_comp_pos();
5684 redraw_all_later(NOT_VALID);
5685}
5686#endif /* FEAT_VERTSPLIT */
5687#endif /* FEAT_MOUSE */
5688
5689#endif /* FEAT_WINDOWS */
5690
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005691#define FRACTION_MULT 16384L
5692
5693/*
5694 * Set wp->w_fraction for the current w_wrow and w_height.
5695 */
5696 static void
5697set_fraction(wp)
5698 win_T *wp;
5699{
5700 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005701 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005702}
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
5705 * Set the height of a window.
5706 * This takes care of the things inside the window, not what happens to the
5707 * window position, the frame or to other windows.
5708 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005709 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710win_new_height(wp, height)
5711 win_T *wp;
5712 int height;
5713{
5714 linenr_T lnum;
5715 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005716 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 /* Don't want a negative height. Happens when splitting a tiny window.
5719 * Will equalize heights soon to fix it. */
5720 if (height < 0)
5721 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005722 if (wp->w_height == height)
5723 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005725 if (wp->w_height > 0)
5726 {
5727 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005728 /* w_wrow needs to be valid. When setting 'laststatus' this may
5729 * call win_new_height() recursively. */
5730 validate_cursor();
5731 if (wp->w_height != prev_height)
5732 return; /* Recursive call already changed the size, bail out here
5733 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005734 if (wp->w_wrow != wp->w_prev_fraction_row)
5735 set_fraction(wp);
5736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 wp->w_height = height;
5739 wp->w_skipcol = 0;
5740
5741 /* Don't change w_topline when height is zero. Don't set w_topline when
5742 * 'scrollbind' is set and this isn't the current window. */
5743 if (height > 0
5744#ifdef FEAT_SCROLLBIND
5745 && (!wp->w_p_scb || wp == curwin)
5746#endif
5747 )
5748 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005749 /*
5750 * Find a value for w_topline that shows the cursor at the same
5751 * relative position in the window as before (more or less).
5752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 lnum = wp->w_cursor.lnum;
5754 if (lnum < 1) /* can happen when starting up */
5755 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005756 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5757 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5759 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005760
5761 if (sline >= 0)
5762 {
5763 /* Make sure the whole cursor line is visible, if possible. */
5764 int rows = plines_win(wp, lnum, FALSE);
5765
5766 if (sline > wp->w_height - rows)
5767 {
5768 sline = wp->w_height - rows;
5769 wp->w_wrow -= rows - line_size;
5770 }
5771 }
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (sline < 0)
5774 {
5775 /*
5776 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005777 * Make cursor line the first line in the window. If not enough
5778 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 */
5780 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005781 if (wp->w_wrow >= wp->w_height
5782 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5783 {
5784 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5785 --wp->w_wrow;
5786 while (wp->w_wrow >= wp->w_height)
5787 {
5788 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5789 + win_col_off2(wp);
5790 --wp->w_wrow;
5791 }
5792 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005793 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005795 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005797 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
5799#ifdef FEAT_FOLDING
5800 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5801 if (lnum == 1)
5802 {
5803 /* first line in buffer is folded */
5804 line_size = 1;
5805 --sline;
5806 break;
5807 }
5808#endif
5809 --lnum;
5810#ifdef FEAT_DIFF
5811 if (lnum == wp->w_topline)
5812 line_size = plines_win_nofill(wp, lnum, TRUE)
5813 + wp->w_topfill;
5814 else
5815#endif
5816 line_size = plines_win(wp, lnum, TRUE);
5817 sline -= line_size;
5818 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (sline < 0)
5821 {
5822 /*
5823 * Line we want at top would go off top of screen. Use next
5824 * line instead.
5825 */
5826#ifdef FEAT_FOLDING
5827 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5828#endif
5829 lnum++;
5830 wp->w_wrow -= line_size + sline;
5831 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005832 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
5834 /* First line of file reached, use that as topline. */
5835 lnum = 1;
5836 wp->w_wrow -= sline;
5837 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005838
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005839 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
5842
5843 if (wp == curwin)
5844 {
5845 if (p_so)
5846 update_topline();
5847 curs_columns(FALSE); /* validate w_wrow */
5848 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005849 if (prev_height > 0)
5850 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005853 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854#ifdef FEAT_WINDOWS
5855 wp->w_redr_status = TRUE;
5856#endif
5857 invalidate_botline_win(wp);
5858}
5859
5860#ifdef FEAT_VERTSPLIT
5861/*
5862 * Set the width of a window.
5863 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005864 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865win_new_width(wp, width)
5866 win_T *wp;
5867 int width;
5868{
5869 wp->w_width = width;
5870 wp->w_lines_valid = 0;
5871 changed_line_abv_curs_win(wp);
5872 invalidate_botline_win(wp);
5873 if (wp == curwin)
5874 {
5875 update_topline();
5876 curs_columns(TRUE); /* validate w_wrow */
5877 }
5878 redraw_win_later(wp, NOT_VALID);
5879 wp->w_redr_status = TRUE;
5880}
5881#endif
5882
5883 void
5884win_comp_scroll(wp)
5885 win_T *wp;
5886{
5887 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5888 if (wp->w_p_scr == 0)
5889 wp->w_p_scr = 1;
5890}
5891
5892/*
5893 * command_height: called whenever p_ch has been changed
5894 */
5895 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005896command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898#ifdef FEAT_WINDOWS
5899 int h;
5900 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005901 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005903 /* Use the value of p_ch that we remembered. This is needed for when the
5904 * GUI starts up, we can't be sure in what order things happen. And when
5905 * p_ch was changed in another tab page. */
5906 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 /* Find bottom frame with width of screen. */
5909 frp = lastwin->w_frame;
5910# ifdef FEAT_VERTSPLIT
5911 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5912 frp = frp->fr_parent;
5913# endif
5914
5915 /* Avoid changing the height of a window with 'winfixheight' set. */
5916 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5917 && frp->fr_win->w_p_wfh)
5918 frp = frp->fr_prev;
5919
5920 if (starting != NO_SCREEN)
5921 {
5922 cmdline_row = Rows - p_ch;
5923
5924 if (p_ch > old_p_ch) /* p_ch got bigger */
5925 {
5926 while (p_ch > old_p_ch)
5927 {
5928 if (frp == NULL)
5929 {
5930 EMSG(_(e_noroom));
5931 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005932 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 cmdline_row = Rows - p_ch;
5934 break;
5935 }
5936 h = frp->fr_height - frame_minheight(frp, NULL);
5937 if (h > p_ch - old_p_ch)
5938 h = p_ch - old_p_ch;
5939 old_p_ch += h;
5940 frame_add_height(frp, -h);
5941 frp = frp->fr_prev;
5942 }
5943
5944 /* Recompute window positions. */
5945 (void)win_comp_pos();
5946
5947 /* clear the lines added to cmdline */
5948 if (full_screen)
5949 screen_fill((int)(cmdline_row), (int)Rows, 0,
5950 (int)Columns, ' ', ' ', 0);
5951 msg_row = cmdline_row;
5952 redraw_cmdline = TRUE;
5953 return;
5954 }
5955
5956 if (msg_row < cmdline_row)
5957 msg_row = cmdline_row;
5958 redraw_cmdline = TRUE;
5959 }
5960 frame_add_height(frp, (int)(old_p_ch - p_ch));
5961
5962 /* Recompute window positions. */
5963 if (frp != lastwin->w_frame)
5964 (void)win_comp_pos();
5965#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005967 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#endif
5969}
5970
5971#if defined(FEAT_WINDOWS) || defined(PROTO)
5972/*
5973 * Resize frame "frp" to be "n" lines higher (negative for less high).
5974 * Also resize the frames it is contained in.
5975 */
5976 static void
5977frame_add_height(frp, n)
5978 frame_T *frp;
5979 int n;
5980{
5981 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5982 for (;;)
5983 {
5984 frp = frp->fr_parent;
5985 if (frp == NULL)
5986 break;
5987 frp->fr_height += n;
5988 }
5989}
5990
5991/*
5992 * Add or remove a status line for the bottom window(s), according to the
5993 * value of 'laststatus'.
5994 */
5995 void
5996last_status(morewin)
5997 int morewin; /* pretend there are two or more windows */
5998{
5999 /* Don't make a difference between horizontal or vertical split. */
6000 last_status_rec(topframe, (p_ls == 2
6001 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6002}
6003
6004 static void
6005last_status_rec(fr, statusline)
6006 frame_T *fr;
6007 int statusline;
6008{
6009 frame_T *fp;
6010 win_T *wp;
6011
6012 if (fr->fr_layout == FR_LEAF)
6013 {
6014 wp = fr->fr_win;
6015 if (wp->w_status_height != 0 && !statusline)
6016 {
6017 /* remove status line */
6018 win_new_height(wp, wp->w_height + 1);
6019 wp->w_status_height = 0;
6020 comp_col();
6021 }
6022 else if (wp->w_status_height == 0 && statusline)
6023 {
6024 /* Find a frame to take a line from. */
6025 fp = fr;
6026 while (fp->fr_height <= frame_minheight(fp, NULL))
6027 {
6028 if (fp == topframe)
6029 {
6030 EMSG(_(e_noroom));
6031 return;
6032 }
6033 /* In a column of frames: go to frame above. If already at
6034 * the top or in a row of frames: go to parent. */
6035 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6036 fp = fp->fr_prev;
6037 else
6038 fp = fp->fr_parent;
6039 }
6040 wp->w_status_height = 1;
6041 if (fp != fr)
6042 {
6043 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6044 frame_fix_height(wp);
6045 (void)win_comp_pos();
6046 }
6047 else
6048 win_new_height(wp, wp->w_height - 1);
6049 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006050 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 }
6052 }
6053#ifdef FEAT_VERTSPLIT
6054 else if (fr->fr_layout == FR_ROW)
6055 {
6056 /* vertically split windows, set status line for each one */
6057 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6058 last_status_rec(fp, statusline);
6059 }
6060#endif
6061 else
6062 {
6063 /* horizontally split window, set status line for last one */
6064 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6065 ;
6066 last_status_rec(fp, statusline);
6067 }
6068}
6069
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006071 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072 */
6073 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006074tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006076#ifdef FEAT_GUI_TABLINE
6077 /* When the GUI has the tabline then this always returns zero. */
6078 if (gui_use_tabline())
6079 return 0;
6080#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006081 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006082 {
6083 case 0: return 0;
6084 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6085 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086 return 1;
6087}
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089#endif /* FEAT_WINDOWS */
6090
6091#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6092/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006093 * Get the file name at the cursor.
6094 * If Visual mode is active, use the selected text if it's in one line.
6095 * Returns the name in allocated memory, NULL for failure.
6096 */
6097 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006098grab_file_name(count, file_lnum)
6099 long count;
6100 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006101{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006102 if (VIsual_active)
6103 {
6104 int len;
6105 char_u *ptr;
6106
6107 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6108 return NULL;
6109 return find_file_name_in_path(ptr, len,
6110 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6111 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006112 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6113 file_lnum);
6114
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006115}
6116
6117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 * Return the file name under or after the cursor.
6119 *
6120 * The 'path' option is searched if the file name is not absolute.
6121 * The string returned has been alloc'ed and should be freed by the caller.
6122 * NULL is returned if the file name or file is not found.
6123 *
6124 * options:
6125 * FNAME_MESS give error messages
6126 * FNAME_EXP expand to path
6127 * FNAME_HYP check for hypertext link
6128 * FNAME_INCL apply "includeexpr"
6129 */
6130 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006131file_name_at_cursor(options, count, file_lnum)
6132 int options;
6133 long count;
6134 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6138 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139}
6140
6141/*
6142 * Return the name of the file under or after ptr[col].
6143 * Otherwise like file_name_at_cursor().
6144 */
6145 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006146file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 char_u *line;
6148 int col;
6149 int options;
6150 long count;
6151 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006152 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 char_u *ptr;
6155 int len;
6156
6157 /*
6158 * search forward for what could be the start of a file name
6159 */
6160 ptr = line + col;
6161 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006162 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (*ptr == NUL) /* nothing found */
6164 {
6165 if (options & FNAME_MESS)
6166 EMSG(_("E446: No file name under cursor"));
6167 return NULL;
6168 }
6169
6170 /*
6171 * Search backward for first char of the file name.
6172 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6173 */
6174 while (ptr > line)
6175 {
6176#ifdef FEAT_MBYTE
6177 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6178 ptr -= len + 1;
6179 else
6180#endif
6181 if (vim_isfilec(ptr[-1])
6182 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6183 --ptr;
6184 else
6185 break;
6186 }
6187
6188 /*
6189 * Search forward for the last char of the file name.
6190 * Also allow "://" when ':' is not in 'isfname'.
6191 */
6192 len = 0;
6193 while (vim_isfilec(ptr[len])
6194 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6195#ifdef FEAT_MBYTE
6196 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006197 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 else
6199#endif
6200 ++len;
6201
6202 /*
6203 * If there is trailing punctuation, remove it.
6204 * But don't remove "..", could be a directory name.
6205 */
6206 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6207 && ptr[len - 2] != '.')
6208 --len;
6209
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006210 if (file_lnum != NULL)
6211 {
6212 char_u *p;
6213
6214 /* Get the number after the file name and a separator character */
6215 p = ptr + len;
6216 p = skipwhite(p);
6217 if (*p != NUL)
6218 {
6219 if (!isdigit(*p))
6220 ++p; /* skip the separator */
6221 p = skipwhite(p);
6222 if (isdigit(*p))
6223 *file_lnum = (int)getdigits(&p);
6224 }
6225 }
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6228}
6229
6230# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6231static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6232
6233 static char_u *
6234eval_includeexpr(ptr, len)
6235 char_u *ptr;
6236 int len;
6237{
6238 char_u *res;
6239
6240 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006241 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006242 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 set_vim_var_string(VV_FNAME, NULL, 0);
6244 return res;
6245}
6246#endif
6247
6248/*
6249 * Return the name of the file ptr[len] in 'path'.
6250 * Otherwise like file_name_at_cursor().
6251 */
6252 char_u *
6253find_file_name_in_path(ptr, len, options, count, rel_fname)
6254 char_u *ptr;
6255 int len;
6256 int options;
6257 long count;
6258 char_u *rel_fname; /* file we are searching relative to */
6259{
6260 char_u *file_name;
6261 int c;
6262# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6263 char_u *tofree = NULL;
6264
6265 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6266 {
6267 tofree = eval_includeexpr(ptr, len);
6268 if (tofree != NULL)
6269 {
6270 ptr = tofree;
6271 len = (int)STRLEN(ptr);
6272 }
6273 }
6274# endif
6275
6276 if (options & FNAME_EXP)
6277 {
6278 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6279 TRUE, rel_fname);
6280
6281# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6282 /*
6283 * If the file could not be found in a normal way, try applying
6284 * 'includeexpr' (unless done already).
6285 */
6286 if (file_name == NULL
6287 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6288 {
6289 tofree = eval_includeexpr(ptr, len);
6290 if (tofree != NULL)
6291 {
6292 ptr = tofree;
6293 len = (int)STRLEN(ptr);
6294 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6295 TRUE, rel_fname);
6296 }
6297 }
6298# endif
6299 if (file_name == NULL && (options & FNAME_MESS))
6300 {
6301 c = ptr[len];
6302 ptr[len] = NUL;
6303 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6304 ptr[len] = c;
6305 }
6306
6307 /* Repeat finding the file "count" times. This matters when it
6308 * appears several times in the path. */
6309 while (file_name != NULL && --count > 0)
6310 {
6311 vim_free(file_name);
6312 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6313 }
6314 }
6315 else
6316 file_name = vim_strnsave(ptr, len);
6317
6318# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6319 vim_free(tofree);
6320# endif
6321
6322 return file_name;
6323}
6324#endif /* FEAT_SEARCHPATH */
6325
6326/*
6327 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6328 * Also check for ":\\", which MS Internet Explorer accepts, return
6329 * URL_BACKSLASH.
6330 */
6331 static int
6332path_is_url(p)
6333 char_u *p;
6334{
6335 if (STRNCMP(p, "://", (size_t)3) == 0)
6336 return URL_SLASH;
6337 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6338 return URL_BACKSLASH;
6339 return 0;
6340}
6341
6342/*
6343 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6344 * Return URL_BACKSLASH for "name:\\".
6345 * Return zero otherwise.
6346 */
6347 int
6348path_with_url(fname)
6349 char_u *fname;
6350{
6351 char_u *p;
6352
6353 for (p = fname; isalpha(*p); ++p)
6354 ;
6355 return path_is_url(p);
6356}
6357
6358/*
6359 * Return TRUE if "name" is a full (absolute) path name or URL.
6360 */
6361 int
6362vim_isAbsName(name)
6363 char_u *name;
6364{
6365 return (path_with_url(name) != 0 || mch_isFullName(name));
6366}
6367
6368/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006369 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 *
6371 * return FAIL for failure, OK otherwise
6372 */
6373 int
6374vim_FullName(fname, buf, len, force)
6375 char_u *fname, *buf;
6376 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006377 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 int retval = OK;
6380 int url;
6381
6382 *buf = NUL;
6383 if (fname == NULL)
6384 return FAIL;
6385
6386 url = path_with_url(fname);
6387 if (!url)
6388 retval = mch_FullName(fname, buf, len, force);
6389 if (url || retval == FAIL)
6390 {
6391 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006392 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6395 slash_adjust(buf);
6396#endif
6397 return retval;
6398}
6399
6400/*
6401 * Return the minimal number of rows that is needed on the screen to display
6402 * the current number of windows.
6403 */
6404 int
6405min_rows()
6406{
6407 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006408#ifdef FEAT_WINDOWS
6409 tabpage_T *tp;
6410 int n;
6411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413 if (firstwin == NULL) /* not initialized yet */
6414 return MIN_LINES;
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006417 total = 0;
6418 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6419 {
6420 n = frame_minheight(tp->tp_topframe, NULL);
6421 if (total < n)
6422 total = n;
6423 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006424 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006426 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006428 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 return total;
6430}
6431
6432/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006433 * Return TRUE if there is only one window (in the current tab page), not
6434 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006435 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 */
6437 int
6438only_one_window()
6439{
6440#ifdef FEAT_WINDOWS
6441 int count = 0;
6442 win_T *wp;
6443
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444 /* If there is another tab page there always is another window. */
6445 if (first_tabpage->tp_next != NULL)
6446 return FALSE;
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006449 if (wp->w_buffer != NULL
6450 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451# ifdef FEAT_QUICKFIX
6452 || wp->w_p_pvw
6453# endif
6454 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006455# ifdef FEAT_AUTOCMD
6456 && wp != aucmd_win
6457# endif
6458 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 ++count;
6460 return (count <= 1);
6461#else
6462 return TRUE;
6463#endif
6464}
6465
6466#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6467/*
6468 * Correct the cursor line number in other windows. Used after changing the
6469 * current buffer, and before applying autocommands.
6470 * When "do_curwin" is TRUE, also check current window.
6471 */
6472 void
6473check_lnums(do_curwin)
6474 int do_curwin;
6475{
6476 win_T *wp;
6477
6478#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006479 tabpage_T *tp;
6480
6481 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6483#else
6484 wp = curwin;
6485 if (do_curwin)
6486#endif
6487 {
6488 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6489 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6490 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6491 wp->w_topline = curbuf->b_ml.ml_line_count;
6492 }
6493}
6494#endif
6495
6496#if defined(FEAT_WINDOWS) || defined(PROTO)
6497
6498/*
6499 * A snapshot of the window sizes, to restore them after closing the help
6500 * window.
6501 * Only these fields are used:
6502 * fr_layout
6503 * fr_width
6504 * fr_height
6505 * fr_next
6506 * fr_child
6507 * fr_win (only valid for the old curwin, NULL otherwise)
6508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510/*
6511 * Create a snapshot of the current frame sizes.
6512 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006513 void
6514make_snapshot(idx)
6515 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006517 clear_snapshot(curtab, idx);
6518 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520
6521 static void
6522make_snapshot_rec(fr, frp)
6523 frame_T *fr;
6524 frame_T **frp;
6525{
6526 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6527 if (*frp == NULL)
6528 return;
6529 (*frp)->fr_layout = fr->fr_layout;
6530# ifdef FEAT_VERTSPLIT
6531 (*frp)->fr_width = fr->fr_width;
6532# endif
6533 (*frp)->fr_height = fr->fr_height;
6534 if (fr->fr_next != NULL)
6535 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6536 if (fr->fr_child != NULL)
6537 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6538 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6539 (*frp)->fr_win = curwin;
6540}
6541
6542/*
6543 * Remove any existing snapshot.
6544 */
6545 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006546clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006547 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006548 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006550 clear_snapshot_rec(tp->tp_snapshot[idx]);
6551 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552}
6553
6554 static void
6555clear_snapshot_rec(fr)
6556 frame_T *fr;
6557{
6558 if (fr != NULL)
6559 {
6560 clear_snapshot_rec(fr->fr_next);
6561 clear_snapshot_rec(fr->fr_child);
6562 vim_free(fr);
6563 }
6564}
6565
6566/*
6567 * Restore a previously created snapshot, if there is any.
6568 * This is only done if the screen size didn't change and the window layout is
6569 * still the same.
6570 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006571 void
6572restore_snapshot(idx, close_curwin)
6573 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 int close_curwin; /* closing current window */
6575{
6576 win_T *wp;
6577
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006578 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006580 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006582 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6583 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006585 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 win_comp_pos();
6587 if (wp != NULL && close_curwin)
6588 win_goto(wp);
6589 redraw_all_later(CLEAR);
6590 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006591 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592}
6593
6594/*
6595 * Check if frames "sn" and "fr" have the same layout, same following frames
6596 * and same children.
6597 */
6598 static int
6599check_snapshot_rec(sn, fr)
6600 frame_T *sn;
6601 frame_T *fr;
6602{
6603 if (sn->fr_layout != fr->fr_layout
6604 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6605 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6606 || (sn->fr_next != NULL
6607 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6608 || (sn->fr_child != NULL
6609 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6610 return FAIL;
6611 return OK;
6612}
6613
6614/*
6615 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6616 * following frames and children.
6617 * Returns a pointer to the old current window, or NULL.
6618 */
6619 static win_T *
6620restore_snapshot_rec(sn, fr)
6621 frame_T *sn;
6622 frame_T *fr;
6623{
6624 win_T *wp = NULL;
6625 win_T *wp2;
6626
6627 fr->fr_height = sn->fr_height;
6628# ifdef FEAT_VERTSPLIT
6629 fr->fr_width = sn->fr_width;
6630# endif
6631 if (fr->fr_layout == FR_LEAF)
6632 {
6633 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6634# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006635 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636# endif
6637 wp = sn->fr_win;
6638 }
6639 if (sn->fr_next != NULL)
6640 {
6641 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6642 if (wp2 != NULL)
6643 wp = wp2;
6644 }
6645 if (sn->fr_child != NULL)
6646 {
6647 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6648 if (wp2 != NULL)
6649 wp = wp2;
6650 }
6651 return wp;
6652}
6653
6654#endif
6655
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006656#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6657 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006658/*
6659 * Set "win" to be the curwin and "tp" to be the current tab page.
6660 * restore_win() MUST be called to undo.
6661 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006662 * When "no_display" is TRUE the display won't be affected, no redraw is
6663 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006664 * Returns FAIL if switching to "win" failed.
6665 */
6666 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006667switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006668 win_T **save_curwin UNUSED;
6669 tabpage_T **save_curtab UNUSED;
6670 win_T *win UNUSED;
6671 tabpage_T *tp UNUSED;
6672 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673{
6674# ifdef FEAT_AUTOCMD
6675 block_autocmds();
6676# endif
6677# ifdef FEAT_WINDOWS
6678 *save_curwin = curwin;
6679 if (tp != NULL)
6680 {
6681 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006682 if (no_display)
6683 {
6684 curtab->tp_firstwin = firstwin;
6685 curtab->tp_lastwin = lastwin;
6686 curtab = tp;
6687 firstwin = curtab->tp_firstwin;
6688 lastwin = curtab->tp_lastwin;
6689 }
6690 else
6691 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006692 }
6693 if (!win_valid(win))
6694 {
6695# ifdef FEAT_AUTOCMD
6696 unblock_autocmds();
6697# endif
6698 return FAIL;
6699 }
6700 curwin = win;
6701 curbuf = curwin->w_buffer;
6702# endif
6703 return OK;
6704}
6705
6706/*
6707 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006708 * When "no_display" is TRUE the display won't be affected, no redraw is
6709 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006710 */
6711 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006712restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006713 win_T *save_curwin UNUSED;
6714 tabpage_T *save_curtab UNUSED;
6715 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006716{
6717# ifdef FEAT_WINDOWS
6718 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006719 {
6720 if (no_display)
6721 {
6722 curtab->tp_firstwin = firstwin;
6723 curtab->tp_lastwin = lastwin;
6724 curtab = save_curtab;
6725 firstwin = curtab->tp_firstwin;
6726 lastwin = curtab->tp_lastwin;
6727 }
6728 else
6729 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6730 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006731 if (win_valid(save_curwin))
6732 {
6733 curwin = save_curwin;
6734 curbuf = curwin->w_buffer;
6735 }
6736# endif
6737# ifdef FEAT_AUTOCMD
6738 unblock_autocmds();
6739# endif
6740}
6741
6742/*
6743 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6744 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6745 */
6746 void
6747switch_buffer(save_curbuf, buf)
6748 buf_T *buf;
6749 buf_T **save_curbuf;
6750{
6751# ifdef FEAT_AUTOCMD
6752 block_autocmds();
6753# endif
6754 *save_curbuf = curbuf;
6755 --curbuf->b_nwindows;
6756 curbuf = buf;
6757 curwin->w_buffer = buf;
6758 ++curbuf->b_nwindows;
6759}
6760
6761/*
6762 * Restore the current buffer after using switch_buffer().
6763 */
6764 void
6765restore_buffer(save_curbuf)
6766 buf_T *save_curbuf;
6767{
6768# ifdef FEAT_AUTOCMD
6769 unblock_autocmds();
6770# endif
6771 /* Check for valid buffer, just in case. */
6772 if (buf_valid(save_curbuf))
6773 {
6774 --curbuf->b_nwindows;
6775 curwin->w_buffer = save_curbuf;
6776 curbuf = save_curbuf;
6777 ++curbuf->b_nwindows;
6778 }
6779}
6780#endif
6781
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6783/*
6784 * Return TRUE if there is any vertically split window.
6785 */
6786 int
6787win_hasvertsplit()
6788{
6789 frame_T *fr;
6790
6791 if (topframe->fr_layout == FR_ROW)
6792 return TRUE;
6793
6794 if (topframe->fr_layout == FR_COL)
6795 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6796 if (fr->fr_layout == FR_ROW)
6797 return TRUE;
6798
6799 return FALSE;
6800}
6801#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006802
6803#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6804/*
6805 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006806 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006807 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6808 * If no particular ID is desired, -1 must be specified for 'id'.
6809 * Return ID of added match, -1 on failure.
6810 */
6811 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006812match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006813 win_T *wp;
6814 char_u *grp;
6815 char_u *pat;
6816 int prio;
6817 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006818 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006819{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006820 matchitem_T *cur;
6821 matchitem_T *prev;
6822 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006823 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006824 regprog_T *regprog = NULL;
6825 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006826
Bram Moolenaarb3414592014-06-17 17:48:32 +02006827 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006828 return -1;
6829 if (id < -1 || id == 0)
6830 {
6831 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6832 return -1;
6833 }
6834 if (id != -1)
6835 {
6836 cur = wp->w_match_head;
6837 while (cur != NULL)
6838 {
6839 if (cur->id == id)
6840 {
6841 EMSGN("E801: ID already taken: %ld", id);
6842 return -1;
6843 }
6844 cur = cur->next;
6845 }
6846 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006847 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006848 {
6849 EMSG2(_(e_nogroup), grp);
6850 return -1;
6851 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006852 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006853 {
6854 EMSG2(_(e_invarg2), pat);
6855 return -1;
6856 }
6857
6858 /* Find available match ID. */
6859 while (id == -1)
6860 {
6861 cur = wp->w_match_head;
6862 while (cur != NULL && cur->id != wp->w_next_match_id)
6863 cur = cur->next;
6864 if (cur == NULL)
6865 id = wp->w_next_match_id;
6866 wp->w_next_match_id++;
6867 }
6868
6869 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006870 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006871 m->id = id;
6872 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006873 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006874 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006875 m->match.regprog = regprog;
6876 m->match.rmm_ic = FALSE;
6877 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006878
Bram Moolenaarb3414592014-06-17 17:48:32 +02006879 /* Set up position matches */
6880 if (pos_list != NULL)
6881 {
6882 linenr_T toplnum = 0;
6883 linenr_T botlnum = 0;
6884 listitem_T *li;
6885 int i;
6886
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006887 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006888 i++, li = li->li_next)
6889 {
6890 linenr_T lnum = 0;
6891 colnr_T col = 0;
6892 int len = 1;
6893 list_T *subl;
6894 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006895 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006896
Bram Moolenaarb3414592014-06-17 17:48:32 +02006897 if (li->li_tv.v_type == VAR_LIST)
6898 {
6899 subl = li->li_tv.vval.v_list;
6900 if (subl == NULL)
6901 goto fail;
6902 subli = subl->lv_first;
6903 if (subli == NULL)
6904 goto fail;
6905 lnum = get_tv_number_chk(&subli->li_tv, &error);
6906 if (error == TRUE)
6907 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006908 if (lnum == 0)
6909 {
6910 --i;
6911 continue;
6912 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006913 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006914 subli = subli->li_next;
6915 if (subli != NULL)
6916 {
6917 col = get_tv_number_chk(&subli->li_tv, &error);
6918 if (error == TRUE)
6919 goto fail;
6920 subli = subli->li_next;
6921 if (subli != NULL)
6922 {
6923 len = get_tv_number_chk(&subli->li_tv, &error);
6924 if (error == TRUE)
6925 goto fail;
6926 }
6927 }
6928 m->pos.pos[i].col = col;
6929 m->pos.pos[i].len = len;
6930 }
6931 else if (li->li_tv.v_type == VAR_NUMBER)
6932 {
6933 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006934 {
6935 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006936 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006937 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006938 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6939 m->pos.pos[i].col = 0;
6940 m->pos.pos[i].len = 0;
6941 }
6942 else
6943 {
6944 EMSG(_("List or number required"));
6945 goto fail;
6946 }
6947 if (toplnum == 0 || lnum < toplnum)
6948 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006949 if (botlnum == 0 || lnum >= botlnum)
6950 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006951 }
6952
6953 /* Calculate top and bottom lines for redrawing area */
6954 if (toplnum != 0)
6955 {
6956 if (wp->w_buffer->b_mod_set)
6957 {
6958 if (wp->w_buffer->b_mod_top > toplnum)
6959 wp->w_buffer->b_mod_top = toplnum;
6960 if (wp->w_buffer->b_mod_bot < botlnum)
6961 wp->w_buffer->b_mod_bot = botlnum;
6962 }
6963 else
6964 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006965 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006966 wp->w_buffer->b_mod_top = toplnum;
6967 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006968 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006969 }
6970 m->pos.toplnum = toplnum;
6971 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006972 rtype = VALID;
6973 }
6974 }
6975
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006976 /* Insert new match. The match list is in ascending order with regard to
6977 * the match priorities. */
6978 cur = wp->w_match_head;
6979 prev = cur;
6980 while (cur != NULL && prio >= cur->priority)
6981 {
6982 prev = cur;
6983 cur = cur->next;
6984 }
6985 if (cur == prev)
6986 wp->w_match_head = m;
6987 else
6988 prev->next = m;
6989 m->next = cur;
6990
Bram Moolenaarb3414592014-06-17 17:48:32 +02006991 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006992 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006993
6994fail:
6995 vim_free(m);
6996 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006997}
6998
6999/*
7000 * Delete match with ID 'id' in the match list of window 'wp'.
7001 * Print error messages if 'perr' is TRUE.
7002 */
7003 int
7004match_delete(wp, id, perr)
7005 win_T *wp;
7006 int id;
7007 int perr;
7008{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007009 matchitem_T *cur = wp->w_match_head;
7010 matchitem_T *prev = cur;
7011 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007012
7013 if (id < 1)
7014 {
7015 if (perr == TRUE)
7016 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7017 id);
7018 return -1;
7019 }
7020 while (cur != NULL && cur->id != id)
7021 {
7022 prev = cur;
7023 cur = cur->next;
7024 }
7025 if (cur == NULL)
7026 {
7027 if (perr == TRUE)
7028 EMSGN("E803: ID not found: %ld", id);
7029 return -1;
7030 }
7031 if (cur == prev)
7032 wp->w_match_head = cur->next;
7033 else
7034 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007035 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007036 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007037 if (cur->pos.toplnum != 0)
7038 {
7039 if (wp->w_buffer->b_mod_set)
7040 {
7041 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7042 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7043 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7044 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7045 }
7046 else
7047 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007048 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007049 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7050 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007051 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007052 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007053 rtype = VALID;
7054 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007055 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007056 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007057 return 0;
7058}
7059
7060/*
7061 * Delete all matches in the match list of window 'wp'.
7062 */
7063 void
7064clear_matches(wp)
7065 win_T *wp;
7066{
7067 matchitem_T *m;
7068
7069 while (wp->w_match_head != NULL)
7070 {
7071 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007072 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007073 vim_free(wp->w_match_head->pattern);
7074 vim_free(wp->w_match_head);
7075 wp->w_match_head = m;
7076 }
7077 redraw_later(SOME_VALID);
7078}
7079
7080/*
7081 * Get match from ID 'id' in window 'wp'.
7082 * Return NULL if match not found.
7083 */
7084 matchitem_T *
7085get_match(wp, id)
7086 win_T *wp;
7087 int id;
7088{
7089 matchitem_T *cur = wp->w_match_head;
7090
7091 while (cur != NULL && cur->id != id)
7092 cur = cur->next;
7093 return cur;
7094}
7095#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007096
7097#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7098 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007099get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007100{
7101 int i = 1;
7102 win_T *w;
7103
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007104 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007105 ++i;
7106
7107 if (w == NULL)
7108 return 0;
7109 else
7110 return i;
7111}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007112
7113 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007114get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007115{
7116 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007117# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007118 tabpage_T *t;
7119
7120 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7121 ++i;
7122
7123 if (t == NULL)
7124 return 0;
7125 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007126# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007127 return i;
7128}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007129#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007130
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007131#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007132/*
7133 * Return TRUE if "topfrp" and its children are at the right height.
7134 */
7135 static int
7136frame_check_height(topfrp, height)
7137 frame_T *topfrp;
7138 int height;
7139{
7140 frame_T *frp;
7141
7142 if (topfrp->fr_height != height)
7143 return FALSE;
7144
7145 if (topfrp->fr_layout == FR_ROW)
7146 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7147 if (frp->fr_height != height)
7148 return FALSE;
7149
7150 return TRUE;
7151}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007152#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007153
7154#ifdef FEAT_VERTSPLIT
7155/*
7156 * Return TRUE if "topfrp" and its children are at the right width.
7157 */
7158 static int
7159frame_check_width(topfrp, width)
7160 frame_T *topfrp;
7161 int width;
7162{
7163 frame_T *frp;
7164
7165 if (topfrp->fr_width != width)
7166 return FALSE;
7167
7168 if (topfrp->fr_layout == FR_COL)
7169 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7170 if (frp->fr_width != width)
7171 return FALSE;
7172
7173 return TRUE;
7174}
7175#endif
7176