blob: 16e0ee78b7a4db5f089c2f2850ba1d1dd0f644aa [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
133#ifdef FEAT_VISUAL
134 reset_VIsual_and_resel(); /* stop Visual mode */
135#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000136#ifdef FEAT_QUICKFIX
137 /* When splitting the quickfix window open a new buffer in it,
138 * don't replicate the quickfix buffer. */
139 if (bt_quickfix(curbuf))
140 goto newwindow;
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_GUI
143 need_mouse_correct = TRUE;
144#endif
145 win_split((int)Prenum, 0);
146 break;
147
148#ifdef FEAT_VERTSPLIT
149/* split current window in two parts, vertically */
150 case Ctrl_V:
151 case 'v':
152 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000153# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000155# endif
156# ifdef FEAT_QUICKFIX
157 /* When splitting the quickfix window open a new buffer in it,
158 * don't replicate the quickfix buffer. */
159 if (bt_quickfix(curbuf))
160 goto newwindow;
161# endif
162# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 win_split((int)Prenum, WSP_VERT);
166 break;
167#endif
168
169/* split current window and edit alternate file */
170 case Ctrl_HAT:
171 case '^':
172 CHECK_CMDWIN
173#ifdef FEAT_VISUAL
174 reset_VIsual_and_resel(); /* stop Visual mode */
175#endif
176 STRCPY(cbuf, "split #");
177 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000178 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
179 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 do_cmdline_cmd(cbuf);
181 break;
182
183/* open new window */
184 case Ctrl_N:
185 case 'n':
186 CHECK_CMDWIN
187#ifdef FEAT_VISUAL
188 reset_VIsual_and_resel(); /* stop Visual mode */
189#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000190#ifdef FEAT_QUICKFIX
191newwindow:
192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000194 /* window height */
195 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 else
197 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000198#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
199 if (nchar == 'v' || nchar == Ctrl_V)
200 STRCAT(cbuf, "v");
201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 STRCAT(cbuf, "new");
203 do_cmdline_cmd(cbuf);
204 break;
205
206/* quit current window */
207 case Ctrl_Q:
208 case 'q':
209#ifdef FEAT_VISUAL
210 reset_VIsual_and_resel(); /* stop Visual mode */
211#endif
212 do_cmdline_cmd((char_u *)"quit");
213 break;
214
215/* close current window */
216 case Ctrl_C:
217 case 'c':
218#ifdef FEAT_VISUAL
219 reset_VIsual_and_resel(); /* stop Visual mode */
220#endif
221 do_cmdline_cmd((char_u *)"close");
222 break;
223
224#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
225/* close preview window */
226 case Ctrl_Z:
227 case 'z':
228 CHECK_CMDWIN
229#ifdef FEAT_VISUAL
230 reset_VIsual_and_resel(); /* stop Visual mode */
231#endif
232 do_cmdline_cmd((char_u *)"pclose");
233 break;
234
235/* cursor to preview window */
236 case 'P':
237 for (wp = firstwin; wp != NULL; wp = wp->w_next)
238 if (wp->w_p_pvw)
239 break;
240 if (wp == NULL)
241 EMSG(_("E441: There is no preview window"));
242 else
243 win_goto(wp);
244 break;
245#endif
246
247/* close all but current window */
248 case Ctrl_O:
249 case 'o':
250 CHECK_CMDWIN
251#ifdef FEAT_VISUAL
252 reset_VIsual_and_resel(); /* stop Visual mode */
253#endif
254 do_cmdline_cmd((char_u *)"only");
255 break;
256
257/* cursor to next window with wrap around */
258 case Ctrl_W:
259 case 'w':
260/* cursor to previous window with wrap around */
261 case 'W':
262 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000263 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 beep_flush();
265 else
266 {
267 if (Prenum) /* go to specified window */
268 {
269 for (wp = firstwin; --Prenum > 0; )
270 {
271 if (wp->w_next == NULL)
272 break;
273 else
274 wp = wp->w_next;
275 }
276 }
277 else
278 {
279 if (nchar == 'W') /* go to previous window */
280 {
281 wp = curwin->w_prev;
282 if (wp == NULL)
283 wp = lastwin; /* wrap around */
284 }
285 else /* go to next window */
286 {
287 wp = curwin->w_next;
288 if (wp == NULL)
289 wp = firstwin; /* wrap around */
290 }
291 }
292 win_goto(wp);
293 }
294 break;
295
296/* cursor to window below */
297 case 'j':
298 case K_DOWN:
299 case Ctrl_J:
300 CHECK_CMDWIN
301#ifdef FEAT_VERTSPLIT
302 win_goto_ver(FALSE, Prenum1);
303#else
304 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
305 wp = wp->w_next)
306 ;
307 win_goto(wp);
308#endif
309 break;
310
311/* cursor to window above */
312 case 'k':
313 case K_UP:
314 case Ctrl_K:
315 CHECK_CMDWIN
316#ifdef FEAT_VERTSPLIT
317 win_goto_ver(TRUE, Prenum1);
318#else
319 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
320 wp = wp->w_prev)
321 ;
322 win_goto(wp);
323#endif
324 break;
325
326#ifdef FEAT_VERTSPLIT
327/* cursor to left window */
328 case 'h':
329 case K_LEFT:
330 case Ctrl_H:
331 case K_BS:
332 CHECK_CMDWIN
333 win_goto_hor(TRUE, Prenum1);
334 break;
335
336/* cursor to right window */
337 case 'l':
338 case K_RIGHT:
339 case Ctrl_L:
340 CHECK_CMDWIN
341 win_goto_hor(FALSE, Prenum1);
342 break;
343#endif
344
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345/* move window to new tab page */
346 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000347 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000348 MSG(_(m_onlyone));
349 else
350 {
351 tabpage_T *oldtab = curtab;
352 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353
354 /* First create a new tab with the window, then go back to
355 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000356 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000357 if (win_new_tabpage((int)Prenum) == OK
358 && valid_tabpage(oldtab))
359 {
360 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200361 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 if (curwin == wp)
363 win_close(curwin, FALSE);
364 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200365 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000366 }
367 }
368 break;
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370/* cursor to top-left window */
371 case 't':
372 case Ctrl_T:
373 win_goto(firstwin);
374 break;
375
376/* cursor to bottom-right window */
377 case 'b':
378 case Ctrl_B:
379 win_goto(lastwin);
380 break;
381
382/* cursor to last accessed (previous) window */
383 case 'p':
384 case Ctrl_P:
385 if (prevwin == NULL)
386 beep_flush();
387 else
388 win_goto(prevwin);
389 break;
390
391/* exchange current and next window */
392 case 'x':
393 case Ctrl_X:
394 CHECK_CMDWIN
395 win_exchange(Prenum);
396 break;
397
398/* rotate windows downwards */
399 case Ctrl_R:
400 case 'r':
401 CHECK_CMDWIN
402#ifdef FEAT_VISUAL
403 reset_VIsual_and_resel(); /* stop Visual mode */
404#endif
405 win_rotate(FALSE, (int)Prenum1); /* downwards */
406 break;
407
408/* rotate windows upwards */
409 case 'R':
410 CHECK_CMDWIN
411#ifdef FEAT_VISUAL
412 reset_VIsual_and_resel(); /* stop Visual mode */
413#endif
414 win_rotate(TRUE, (int)Prenum1); /* upwards */
415 break;
416
417/* move window to the very top/bottom/left/right */
418 case 'K':
419 case 'J':
420#ifdef FEAT_VERTSPLIT
421 case 'H':
422 case 'L':
423#endif
424 CHECK_CMDWIN
425 win_totop((int)Prenum,
426 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
427 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
428 break;
429
430/* make all windows the same height */
431 case '=':
432#ifdef FEAT_GUI
433 need_mouse_correct = TRUE;
434#endif
435 win_equal(NULL, FALSE, 'b');
436 break;
437
438/* increase current window height */
439 case '+':
440#ifdef FEAT_GUI
441 need_mouse_correct = TRUE;
442#endif
443 win_setheight(curwin->w_height + (int)Prenum1);
444 break;
445
446/* decrease current window height */
447 case '-':
448#ifdef FEAT_GUI
449 need_mouse_correct = TRUE;
450#endif
451 win_setheight(curwin->w_height - (int)Prenum1);
452 break;
453
454/* set current window height */
455 case Ctrl__:
456 case '_':
457#ifdef FEAT_GUI
458 need_mouse_correct = TRUE;
459#endif
460 win_setheight(Prenum ? (int)Prenum : 9999);
461 break;
462
463#ifdef FEAT_VERTSPLIT
464/* increase current window width */
465 case '>':
466#ifdef FEAT_GUI
467 need_mouse_correct = TRUE;
468#endif
469 win_setwidth(curwin->w_width + (int)Prenum1);
470 break;
471
472/* decrease current window width */
473 case '<':
474#ifdef FEAT_GUI
475 need_mouse_correct = TRUE;
476#endif
477 win_setwidth(curwin->w_width - (int)Prenum1);
478 break;
479
480/* set current window width */
481 case '|':
482#ifdef FEAT_GUI
483 need_mouse_correct = TRUE;
484#endif
485 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
486 break;
487#endif
488
489/* jump to tag and split window if tag exists (in preview window) */
490#if defined(FEAT_QUICKFIX)
491 case '}':
492 CHECK_CMDWIN
493 if (Prenum)
494 g_do_tagpreview = Prenum;
495 else
496 g_do_tagpreview = p_pvh;
497 /*FALLTHROUGH*/
498#endif
499 case ']':
500 case Ctrl_RSB:
501 CHECK_CMDWIN
502#ifdef FEAT_VISUAL
503 reset_VIsual_and_resel(); /* stop Visual mode */
504#endif
505 if (Prenum)
506 postponed_split = Prenum;
507 else
508 postponed_split = -1;
509
510 /* Execute the command right here, required when
511 * "wincmd ]" was used in a function. */
512 do_nv_ident(Ctrl_RSB, NUL);
513 break;
514
515#ifdef FEAT_SEARCHPATH
516/* edit file name under cursor in a new window */
517 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000520wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000522
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000523 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (ptr != NULL)
525 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000526# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 setpcmark();
530 if (win_split(0, 0) == OK)
531 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200532 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000533 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
534 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000535 if (nchar == 'F' && lnum >= 0)
536 {
537 curwin->w_cursor.lnum = lnum;
538 check_cursor_lnum();
539 beginline(BL_SOL | BL_FIX);
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
542 vim_free(ptr);
543 }
544 break;
545#endif
546
547#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000548/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 * new window -- webb
550 */
551 case 'i': /* Go to any match */
552 case Ctrl_I:
553 type = FIND_ANY;
554 /* FALLTHROUGH */
555 case 'd': /* Go to definition, using 'define' */
556 case Ctrl_D:
557 CHECK_CMDWIN
558 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
559 break;
560 find_pattern_in_path(ptr, 0, len, TRUE,
561 Prenum == 0 ? TRUE : FALSE, type,
562 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
563 curwin->w_set_curswant = TRUE;
564 break;
565#endif
566
Bram Moolenaar05159a02005-02-26 23:04:13 +0000567 case K_KENTER:
568 case CAR:
569#if defined(FEAT_QUICKFIX)
570 /*
571 * In a quickfix window a <CR> jumps to the error under the
572 * cursor in a new window.
573 */
574 if (bt_quickfix(curbuf))
575 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000576 sprintf((char *)cbuf, "split +%ld%s",
577 (long)curwin->w_cursor.lnum,
578 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000579 do_cmdline_cmd(cbuf);
580 }
581#endif
582 break;
583
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585/* CTRL-W g extended commands */
586 case 'g':
587 case Ctrl_G:
588 CHECK_CMDWIN
589#ifdef USE_ON_FLY_SCROLL
590 dont_scroll = TRUE; /* disallow scrolling here */
591#endif
592 ++no_mapping;
593 ++allow_keys; /* no mapping for xchar, but allow key codes */
594 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000595 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 --no_mapping;
598 --allow_keys;
599#ifdef FEAT_CMDL_INFO
600 (void)add_to_showcmd(xchar);
601#endif
602 switch (xchar)
603 {
604#if defined(FEAT_QUICKFIX)
605 case '}':
606 xchar = Ctrl_RSB;
607 if (Prenum)
608 g_do_tagpreview = Prenum;
609 else
610 g_do_tagpreview = p_pvh;
611 /*FALLTHROUGH*/
612#endif
613 case ']':
614 case Ctrl_RSB:
615#ifdef FEAT_VISUAL
616 reset_VIsual_and_resel(); /* stop Visual mode */
617#endif
618 if (Prenum)
619 postponed_split = Prenum;
620 else
621 postponed_split = -1;
622
623 /* Execute the command right here, required when
624 * "wincmd g}" was used in a function. */
625 do_nv_ident('g', xchar);
626 break;
627
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000628#ifdef FEAT_SEARCHPATH
629 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000630 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100631 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000632 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000633 goto wingotofile;
634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 default:
636 beep_flush();
637 break;
638 }
639 break;
640
641 default: beep_flush();
642 break;
643 }
644}
645
646/*
647 * split the current window, implements CTRL-W s and :split
648 *
649 * "size" is the height or width for the new window, 0 to use half of current
650 * height or width.
651 *
652 * "flags":
653 * WSP_ROOM: require enough room for new window
654 * WSP_VERT: vertical split.
655 * WSP_TOP: open window at the top-left of the shell (help window).
656 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
657 * WSP_HELP: creating the help window, keep layout snapshot
658 *
659 * return FAIL for failure, OK otherwise
660 */
661 int
662win_split(size, flags)
663 int size;
664 int flags;
665{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000666 /* When the ":tab" modifier was used open a new tab page instead. */
667 if (may_open_tabpage() == OK)
668 return OK;
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 /* Add flags from ":vertical", ":topleft" and ":botright". */
671 flags |= cmdmod.split;
672 if ((flags & WSP_TOP) && (flags & WSP_BOT))
673 {
674 EMSG(_("E442: Can't split topleft and botright at the same time"));
675 return FAIL;
676 }
677
678 /* When creating the help window make a snapshot of the window layout.
679 * Otherwise clear the snapshot, it's now invalid. */
680 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000681 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000683 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685 return win_split_ins(size, flags, NULL, 0);
686}
687
688/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100689 * When "new_wp" is NULL: split the current window in two.
690 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 * top/left/right/bottom.
692 * return FAIL for failure, OK otherwise
693 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000694 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100695win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 int size;
697 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100698 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 int dir;
700{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100701 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 win_T *oldwin;
703 int new_size = size;
704 int i;
705 int need_status = 0;
706 int do_equal = FALSE;
707 int needed;
708 int available;
709 int oldwin_height = 0;
710 int layout;
711 frame_T *frp, *curfrp;
712 int before;
713
714 if (flags & WSP_TOP)
715 oldwin = firstwin;
716 else if (flags & WSP_BOT)
717 oldwin = lastwin;
718 else
719 oldwin = curwin;
720
721 /* add a status line when p_ls == 1 and splitting the first window */
722 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
723 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100724 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
726 EMSG(_(e_noroom));
727 return FAIL;
728 }
729 need_status = STATUS_HEIGHT;
730 }
731
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000732#ifdef FEAT_GUI
733 /* May be needed for the scrollbars that are going to change. */
734 if (gui.in_use)
735 out_flush();
736#endif
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#ifdef FEAT_VERTSPLIT
739 if (flags & WSP_VERT)
740 {
741 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
743 /*
744 * Check if we are able to split the current window and compute its
745 * width.
746 */
747 needed = p_wmw + 1;
748 if (flags & WSP_ROOM)
749 needed += p_wiw - p_wmw;
750 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
751 {
752 available = topframe->fr_width;
753 needed += frame_minwidth(topframe, NULL);
754 }
755 else
756 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100757 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
759 EMSG(_(e_noroom));
760 return FAIL;
761 }
762 if (new_size == 0)
763 new_size = oldwin->w_width / 2;
764 if (new_size > oldwin->w_width - p_wmw - 1)
765 new_size = oldwin->w_width - p_wmw - 1;
766 if (new_size < p_wmw)
767 new_size = p_wmw;
768
769 /* if it doesn't fit in the current window, need win_equal() */
770 if (oldwin->w_width - new_size - 1 < p_wmw)
771 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000772
773 /* We don't like to take lines for the new window from a
774 * 'winfixwidth' window. Take them from a window to the left or right
775 * instead, if possible. */
776 if (oldwin->w_p_wfw)
777 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000778
779 /* Only make all windows the same width if one of them (except oldwin)
780 * is wider than one of the split windows. */
781 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
782 && oldwin->w_frame->fr_parent != NULL)
783 {
784 frp = oldwin->w_frame->fr_parent->fr_child;
785 while (frp != NULL)
786 {
787 if (frp->fr_win != oldwin && frp->fr_win != NULL
788 && (frp->fr_win->w_width > new_size
789 || frp->fr_win->w_width > oldwin->w_width
790 - new_size - STATUS_HEIGHT))
791 {
792 do_equal = TRUE;
793 break;
794 }
795 frp = frp->fr_next;
796 }
797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 else
800#endif
801 {
802 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804 /*
805 * Check if we are able to split the current window and compute its
806 * height.
807 */
808 needed = p_wmh + STATUS_HEIGHT + need_status;
809 if (flags & WSP_ROOM)
810 needed += p_wh - p_wmh;
811 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
812 {
813 available = topframe->fr_height;
814 needed += frame_minheight(topframe, NULL);
815 }
816 else
817 {
818 available = oldwin->w_height;
819 needed += p_wmh;
820 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100821 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 EMSG(_(e_noroom));
824 return FAIL;
825 }
826 oldwin_height = oldwin->w_height;
827 if (need_status)
828 {
829 oldwin->w_status_height = STATUS_HEIGHT;
830 oldwin_height -= STATUS_HEIGHT;
831 }
832 if (new_size == 0)
833 new_size = oldwin_height / 2;
834
835 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
836 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
837 if (new_size < p_wmh)
838 new_size = p_wmh;
839
840 /* if it doesn't fit in the current window, need win_equal() */
841 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
842 do_equal = TRUE;
843
844 /* We don't like to take lines for the new window from a
845 * 'winfixheight' window. Take them from a window above or below
846 * instead, if possible. */
847 if (oldwin->w_p_wfh)
848 {
849 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
850 oldwin);
851 oldwin_height = oldwin->w_height;
852 if (need_status)
853 oldwin_height -= STATUS_HEIGHT;
854 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000855
856 /* Only make all windows the same height if one of them (except oldwin)
857 * is higher than one of the split windows. */
858 if (!do_equal && p_ea && size == 0
859#ifdef FEAT_VERTSPLIT
860 && *p_ead != 'h'
861#endif
862 && oldwin->w_frame->fr_parent != NULL)
863 {
864 frp = oldwin->w_frame->fr_parent->fr_child;
865 while (frp != NULL)
866 {
867 if (frp->fr_win != oldwin && frp->fr_win != NULL
868 && (frp->fr_win->w_height > new_size
869 || frp->fr_win->w_height > oldwin_height - new_size
870 - STATUS_HEIGHT))
871 {
872 do_equal = TRUE;
873 break;
874 }
875 frp = frp->fr_next;
876 }
877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879
880 /*
881 * allocate new window structure and link it in the window list
882 */
883 if ((flags & WSP_TOP) == 0
884 && ((flags & WSP_BOT)
885 || (flags & WSP_BELOW)
886 || (!(flags & WSP_ABOVE)
887 && (
888#ifdef FEAT_VERTSPLIT
889 (flags & WSP_VERT) ? p_spr :
890#endif
891 p_sb))))
892 {
893 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100894 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000895 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else
897 win_append(oldwin, wp);
898 }
899 else
900 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100901 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000902 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 else
904 win_append(oldwin->w_prev, wp);
905 }
906
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100907 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 {
909 if (wp == NULL)
910 return FAIL;
911
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000912 new_frame(wp);
913 if (wp->w_frame == NULL)
914 {
915 win_free(wp, NULL);
916 return FAIL;
917 }
918
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000919 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000920 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922
923 /*
924 * Reorganise the tree of frames to insert the new window.
925 */
926 if (flags & (WSP_TOP | WSP_BOT))
927 {
928#ifdef FEAT_VERTSPLIT
929 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
930 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
931#else
932 if (topframe->fr_layout == FR_COL)
933#endif
934 {
935 curfrp = topframe->fr_child;
936 if (flags & WSP_BOT)
937 while (curfrp->fr_next != NULL)
938 curfrp = curfrp->fr_next;
939 }
940 else
941 curfrp = topframe;
942 before = (flags & WSP_TOP);
943 }
944 else
945 {
946 curfrp = oldwin->w_frame;
947 if (flags & WSP_BELOW)
948 before = FALSE;
949 else if (flags & WSP_ABOVE)
950 before = TRUE;
951 else
952#ifdef FEAT_VERTSPLIT
953 if (flags & WSP_VERT)
954 before = !p_spr;
955 else
956#endif
957 before = !p_sb;
958 }
959 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
960 {
961 /* Need to create a new frame in the tree to make a branch. */
962 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
963 *frp = *curfrp;
964 curfrp->fr_layout = layout;
965 frp->fr_parent = curfrp;
966 frp->fr_next = NULL;
967 frp->fr_prev = NULL;
968 curfrp->fr_child = frp;
969 curfrp->fr_win = NULL;
970 curfrp = frp;
971 if (frp->fr_win != NULL)
972 oldwin->w_frame = frp;
973 else
974 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
975 frp->fr_parent = curfrp;
976 }
977
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100978 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000979 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100981 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 frp->fr_parent = curfrp->fr_parent;
983
984 /* Insert the new frame at the right place in the frame list. */
985 if (before)
986 frame_insert(curfrp, frp);
987 else
988 frame_append(curfrp, frp);
989
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100990 /* Set w_fraction now so that the cursor keeps the same relative
991 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100992 if (oldwin->w_height > 0)
993 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100994 wp->w_fraction = oldwin->w_fraction;
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#ifdef FEAT_VERTSPLIT
997 if (flags & WSP_VERT)
998 {
999 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (need_status)
1002 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001003 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 oldwin->w_status_height = need_status;
1005 }
1006 if (flags & (WSP_TOP | WSP_BOT))
1007 {
1008 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001009 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001010 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 wp->w_status_height = (p_ls > 0);
1012 }
1013 else
1014 {
1015 /* height and row of new window is same as current window */
1016 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001017 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 wp->w_status_height = oldwin->w_status_height;
1019 }
1020 frp->fr_height = curfrp->fr_height;
1021
1022 /* "new_size" of the current window goes to the new window, use
1023 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001024 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (before)
1026 wp->w_vsep_width = 1;
1027 else
1028 {
1029 wp->w_vsep_width = oldwin->w_vsep_width;
1030 oldwin->w_vsep_width = 1;
1031 }
1032 if (flags & (WSP_TOP | WSP_BOT))
1033 {
1034 if (flags & WSP_BOT)
1035 frame_add_vsep(curfrp);
1036 /* Set width of neighbor frame */
1037 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001038 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1039 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001042 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (before) /* new window left of current one */
1044 {
1045 wp->w_wincol = oldwin->w_wincol;
1046 oldwin->w_wincol += new_size + 1;
1047 }
1048 else /* new window right of current one */
1049 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1050 frame_fix_width(oldwin);
1051 frame_fix_width(wp);
1052 }
1053 else
1054#endif
1055 {
1056 /* width and column of new window is same as current window */
1057#ifdef FEAT_VERTSPLIT
1058 if (flags & (WSP_TOP | WSP_BOT))
1059 {
1060 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001061 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 wp->w_vsep_width = 0;
1063 }
1064 else
1065 {
1066 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001067 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 wp->w_vsep_width = oldwin->w_vsep_width;
1069 }
1070 frp->fr_width = curfrp->fr_width;
1071#endif
1072
1073 /* "new_size" of the current window goes to the new window, use
1074 * one row for the status line */
1075 win_new_height(wp, new_size);
1076 if (flags & (WSP_TOP | WSP_BOT))
1077 frame_new_height(curfrp, curfrp->fr_height
1078 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1079 else
1080 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1081 if (before) /* new window above current one */
1082 {
1083 wp->w_winrow = oldwin->w_winrow;
1084 wp->w_status_height = STATUS_HEIGHT;
1085 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1086 }
1087 else /* new window below current one */
1088 {
1089 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1090 wp->w_status_height = oldwin->w_status_height;
1091 oldwin->w_status_height = STATUS_HEIGHT;
1092 }
1093#ifdef FEAT_VERTSPLIT
1094 if (flags & WSP_BOT)
1095 frame_add_statusline(curfrp);
1096#endif
1097 frame_fix_height(wp);
1098 frame_fix_height(oldwin);
1099 }
1100
1101 if (flags & (WSP_TOP | WSP_BOT))
1102 (void)win_comp_pos();
1103
1104 /*
1105 * Both windows need redrawing
1106 */
1107 redraw_win_later(wp, NOT_VALID);
1108 wp->w_redr_status = TRUE;
1109 redraw_win_later(oldwin, NOT_VALID);
1110 oldwin->w_redr_status = TRUE;
1111
1112 if (need_status)
1113 {
1114 msg_row = Rows - 1;
1115 msg_col = sc_col;
1116 msg_clr_eos_force(); /* Old command/ruler may still be there */
1117 comp_col();
1118 msg_row = Rows - 1;
1119 msg_col = 0; /* put position back at start of line */
1120 }
1121
1122 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001123 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 */
1125 if (do_equal || dir != 0)
1126 win_equal(wp, TRUE,
1127#ifdef FEAT_VERTSPLIT
1128 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1129 : dir == 'h' ? 'b' :
1130#endif
1131 'v');
1132
1133 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1134 * size was given. */
1135#ifdef FEAT_VERTSPLIT
1136 if (flags & WSP_VERT)
1137 {
1138 i = p_wiw;
1139 if (size != 0)
1140 p_wiw = size;
1141
1142# ifdef FEAT_GUI
1143 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1144 if (gui.in_use)
1145 gui_init_which_components(NULL);
1146# endif
1147 }
1148 else
1149#endif
1150 {
1151 i = p_wh;
1152 if (size != 0)
1153 p_wh = size;
1154 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001155
1156 /*
1157 * make the new window the current window
1158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 win_enter(wp, FALSE);
1160#ifdef FEAT_VERTSPLIT
1161 if (flags & WSP_VERT)
1162 p_wiw = i;
1163 else
1164#endif
1165 p_wh = i;
1166
1167 return OK;
1168}
1169
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001170
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171/*
1172 * Initialize window "newp" from window "oldp".
1173 * Used when splitting a window and when creating a new tab page.
1174 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001175 * WSP_NEWLOC may be specified in flags to prevent the location list from
1176 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001177 */
1178 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001179win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180 win_T *newp;
1181 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001182 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001183{
1184 int i;
1185
1186 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001187#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001188 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001189#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001190 oldp->w_buffer->b_nwindows++;
1191 newp->w_cursor = oldp->w_cursor;
1192 newp->w_valid = 0;
1193 newp->w_curswant = oldp->w_curswant;
1194 newp->w_set_curswant = oldp->w_set_curswant;
1195 newp->w_topline = oldp->w_topline;
1196#ifdef FEAT_DIFF
1197 newp->w_topfill = oldp->w_topfill;
1198#endif
1199 newp->w_leftcol = oldp->w_leftcol;
1200 newp->w_pcmark = oldp->w_pcmark;
1201 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1202 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001203 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001204 newp->w_fraction = oldp->w_fraction;
1205 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1206#ifdef FEAT_JUMPLIST
1207 copy_jumplist(oldp, newp);
1208#endif
1209#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001210 if (flags & WSP_NEWLOC)
1211 {
1212 /* Don't copy the location list. */
1213 newp->w_llist = NULL;
1214 newp->w_llist_ref = NULL;
1215 }
1216 else
1217 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001218#endif
1219 if (oldp->w_localdir != NULL)
1220 newp->w_localdir = vim_strsave(oldp->w_localdir);
1221
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001222 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001223 for (i = 0; i < oldp->w_tagstacklen; i++)
1224 {
1225 newp->w_tagstack[i] = oldp->w_tagstack[i];
1226 if (newp->w_tagstack[i].tagname != NULL)
1227 newp->w_tagstack[i].tagname =
1228 vim_strsave(newp->w_tagstack[i].tagname);
1229 }
1230 newp->w_tagstackidx = oldp->w_tagstackidx;
1231 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001232#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001233 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001234#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001235
1236 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001237
Bram Moolenaara971b822011-09-14 14:43:25 +02001238#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001239 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001240#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001241}
1242
1243/*
1244 * Initialize window "newp" from window"old".
1245 * Only the essential things are copied.
1246 */
1247 static void
1248win_init_some(newp, oldp)
1249 win_T *newp;
1250 win_T *oldp;
1251{
1252 /* Use the same argument list. */
1253 newp->w_alist = oldp->w_alist;
1254 ++newp->w_alist->al_refcount;
1255 newp->w_arg_idx = oldp->w_arg_idx;
1256
1257 /* copy options from existing window */
1258 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001259}
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261#endif /* FEAT_WINDOWS */
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263#if defined(FEAT_WINDOWS) || defined(PROTO)
1264/*
1265 * Check if "win" is a pointer to an existing window.
1266 */
1267 int
1268win_valid(win)
1269 win_T *win;
1270{
1271 win_T *wp;
1272
1273 if (win == NULL)
1274 return FALSE;
1275 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1276 if (wp == win)
1277 return TRUE;
1278 return FALSE;
1279}
1280
1281/*
1282 * Return the number of windows.
1283 */
1284 int
1285win_count()
1286{
1287 win_T *wp;
1288 int count = 0;
1289
1290 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1291 ++count;
1292 return count;
1293}
1294
1295/*
1296 * Make "count" windows on the screen.
1297 * Return actual number of windows on the screen.
1298 * Must be called when there is just one window, filling the whole screen
1299 * (excluding the command line).
1300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 int
1302make_windows(count, vertical)
1303 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001304 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 int maxcount;
1307 int todo;
1308
1309#ifdef FEAT_VERTSPLIT
1310 if (vertical)
1311 {
1312 /* Each windows needs at least 'winminwidth' lines and a separator
1313 * column. */
1314 maxcount = (curwin->w_width + curwin->w_vsep_width
1315 - (p_wiw - p_wmw)) / (p_wmw + 1);
1316 }
1317 else
1318#endif
1319 {
1320 /* Each window needs at least 'winminheight' lines and a status line. */
1321 maxcount = (curwin->w_height + curwin->w_status_height
1322 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1323 }
1324
1325 if (maxcount < 2)
1326 maxcount = 2;
1327 if (count > maxcount)
1328 count = maxcount;
1329
1330 /*
1331 * add status line now, otherwise first window will be too big
1332 */
1333 if (count > 1)
1334 last_status(TRUE);
1335
1336#ifdef FEAT_AUTOCMD
1337 /*
1338 * Don't execute autocommands while creating the windows. Must do that
1339 * when putting the buffers in the windows.
1340 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001341 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342#endif
1343
1344 /* todo is number of windows left to create */
1345 for (todo = count - 1; todo > 0; --todo)
1346#ifdef FEAT_VERTSPLIT
1347 if (vertical)
1348 {
1349 if (win_split(curwin->w_width - (curwin->w_width - todo)
1350 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1351 break;
1352 }
1353 else
1354#endif
1355 {
1356 if (win_split(curwin->w_height - (curwin->w_height - todo
1357 * STATUS_HEIGHT) / (todo + 1)
1358 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1359 break;
1360 }
1361
1362#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001363 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
1366 /* return actual number of windows */
1367 return (count - todo);
1368}
1369
1370/*
1371 * Exchange current and next window
1372 */
1373 static void
1374win_exchange(Prenum)
1375 long Prenum;
1376{
1377 frame_T *frp;
1378 frame_T *frp2;
1379 win_T *wp;
1380 win_T *wp2;
1381 int temp;
1382
1383 if (lastwin == firstwin) /* just one window */
1384 {
1385 beep_flush();
1386 return;
1387 }
1388
1389#ifdef FEAT_GUI
1390 need_mouse_correct = TRUE;
1391#endif
1392
1393 /*
1394 * find window to exchange with
1395 */
1396 if (Prenum)
1397 {
1398 frp = curwin->w_frame->fr_parent->fr_child;
1399 while (frp != NULL && --Prenum > 0)
1400 frp = frp->fr_next;
1401 }
1402 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1403 frp = curwin->w_frame->fr_next;
1404 else /* Swap last window in row/col with previous */
1405 frp = curwin->w_frame->fr_prev;
1406
1407 /* We can only exchange a window with another window, not with a frame
1408 * containing windows. */
1409 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1410 return;
1411 wp = frp->fr_win;
1412
1413/*
1414 * 1. remove curwin from the list. Remember after which window it was in wp2
1415 * 2. insert curwin before wp in the list
1416 * if wp != wp2
1417 * 3. remove wp from the list
1418 * 4. insert wp after wp2
1419 * 5. exchange the status line height and vsep width.
1420 */
1421 wp2 = curwin->w_prev;
1422 frp2 = curwin->w_frame->fr_prev;
1423 if (wp->w_prev != curwin)
1424 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 frame_remove(curwin->w_frame);
1427 win_append(wp->w_prev, curwin);
1428 frame_insert(frp, curwin->w_frame);
1429 }
1430 if (wp != wp2)
1431 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 frame_remove(wp->w_frame);
1434 win_append(wp2, wp);
1435 if (frp2 == NULL)
1436 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1437 else
1438 frame_append(frp2, wp->w_frame);
1439 }
1440 temp = curwin->w_status_height;
1441 curwin->w_status_height = wp->w_status_height;
1442 wp->w_status_height = temp;
1443#ifdef FEAT_VERTSPLIT
1444 temp = curwin->w_vsep_width;
1445 curwin->w_vsep_width = wp->w_vsep_width;
1446 wp->w_vsep_width = temp;
1447
1448 /* If the windows are not in the same frame, exchange the sizes to avoid
1449 * messing up the window layout. Otherwise fix the frame sizes. */
1450 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1451 {
1452 temp = curwin->w_height;
1453 curwin->w_height = wp->w_height;
1454 wp->w_height = temp;
1455 temp = curwin->w_width;
1456 curwin->w_width = wp->w_width;
1457 wp->w_width = temp;
1458 }
1459 else
1460 {
1461 frame_fix_height(curwin);
1462 frame_fix_height(wp);
1463 frame_fix_width(curwin);
1464 frame_fix_width(wp);
1465 }
1466#endif
1467
1468 (void)win_comp_pos(); /* recompute window positions */
1469
1470 win_enter(wp, TRUE);
1471 redraw_later(CLEAR);
1472}
1473
1474/*
1475 * rotate windows: if upwards TRUE the second window becomes the first one
1476 * if upwards FALSE the first window becomes the second one
1477 */
1478 static void
1479win_rotate(upwards, count)
1480 int upwards;
1481 int count;
1482{
1483 win_T *wp1;
1484 win_T *wp2;
1485 frame_T *frp;
1486 int n;
1487
1488 if (firstwin == lastwin) /* nothing to do */
1489 {
1490 beep_flush();
1491 return;
1492 }
1493
1494#ifdef FEAT_GUI
1495 need_mouse_correct = TRUE;
1496#endif
1497
1498#ifdef FEAT_VERTSPLIT
1499 /* Check if all frames in this row/col have one window. */
1500 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1501 frp = frp->fr_next)
1502 if (frp->fr_win == NULL)
1503 {
1504 EMSG(_("E443: Cannot rotate when another window is split"));
1505 return;
1506 }
1507#endif
1508
1509 while (count--)
1510 {
1511 if (upwards) /* first window becomes last window */
1512 {
1513 /* remove first window/frame from the list */
1514 frp = curwin->w_frame->fr_parent->fr_child;
1515 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001516 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 frame_remove(frp);
1518
1519 /* find last frame and append removed window/frame after it */
1520 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1521 ;
1522 win_append(frp->fr_win, wp1);
1523 frame_append(frp, wp1->w_frame);
1524
1525 wp2 = frp->fr_win; /* previously last window */
1526 }
1527 else /* last window becomes first window */
1528 {
1529 /* find last window/frame in the list and remove it */
1530 for (frp = curwin->w_frame; frp->fr_next != NULL;
1531 frp = frp->fr_next)
1532 ;
1533 wp1 = frp->fr_win;
1534 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001535 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 frame_remove(frp);
1537
1538 /* append the removed window/frame before the first in the list */
1539 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1540 frame_insert(frp->fr_parent->fr_child, frp);
1541 }
1542
1543 /* exchange status height and vsep width of old and new last window */
1544 n = wp2->w_status_height;
1545 wp2->w_status_height = wp1->w_status_height;
1546 wp1->w_status_height = n;
1547 frame_fix_height(wp1);
1548 frame_fix_height(wp2);
1549#ifdef FEAT_VERTSPLIT
1550 n = wp2->w_vsep_width;
1551 wp2->w_vsep_width = wp1->w_vsep_width;
1552 wp1->w_vsep_width = n;
1553 frame_fix_width(wp1);
1554 frame_fix_width(wp2);
1555#endif
1556
1557 /* recompute w_winrow and w_wincol for all windows */
1558 (void)win_comp_pos();
1559 }
1560
1561 redraw_later(CLEAR);
1562}
1563
1564/*
1565 * Move the current window to the very top/bottom/left/right of the screen.
1566 */
1567 static void
1568win_totop(size, flags)
1569 int size;
1570 int flags;
1571{
1572 int dir;
1573 int height = curwin->w_height;
1574
1575 if (lastwin == firstwin)
1576 {
1577 beep_flush();
1578 return;
1579 }
1580
1581 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001582 (void)winframe_remove(curwin, &dir, NULL);
1583 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 last_status(FALSE); /* may need to remove last status line */
1585 (void)win_comp_pos(); /* recompute window positions */
1586
1587 /* Split a window on the desired side and put the window there. */
1588 (void)win_split_ins(size, flags, curwin, dir);
1589 if (!(flags & WSP_VERT))
1590 {
1591 win_setheight(height);
1592 if (p_ea)
1593 win_equal(curwin, TRUE, 'v');
1594 }
1595
1596#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1597 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1598 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001599 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601}
1602
1603/*
1604 * Move window "win1" to below/right of "win2" and make "win1" the current
1605 * window. Only works within the same frame!
1606 */
1607 void
1608win_move_after(win1, win2)
1609 win_T *win1, *win2;
1610{
1611 int height;
1612
1613 /* check if the arguments are reasonable */
1614 if (win1 == win2)
1615 return;
1616
1617 /* check if there is something to do */
1618 if (win2->w_next != win1)
1619 {
1620 /* may need move the status line/vertical separator of the last window
1621 * */
1622 if (win1 == lastwin)
1623 {
1624 height = win1->w_prev->w_status_height;
1625 win1->w_prev->w_status_height = win1->w_status_height;
1626 win1->w_status_height = height;
1627#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001628 if (win1->w_prev->w_vsep_width == 1)
1629 {
1630 /* Remove the vertical separator from the last-but-one window,
1631 * add it to the last window. Adjust the frame widths. */
1632 win1->w_prev->w_vsep_width = 0;
1633 win1->w_prev->w_frame->fr_width -= 1;
1634 win1->w_vsep_width = 1;
1635 win1->w_frame->fr_width += 1;
1636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638 }
1639 else if (win2 == lastwin)
1640 {
1641 height = win1->w_status_height;
1642 win1->w_status_height = win2->w_status_height;
1643 win2->w_status_height = height;
1644#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001645 if (win1->w_vsep_width == 1)
1646 {
1647 /* Remove the vertical separator from win1, add it to the last
1648 * window, win2. Adjust the frame widths. */
1649 win2->w_vsep_width = 1;
1650 win2->w_frame->fr_width += 1;
1651 win1->w_vsep_width = 0;
1652 win1->w_frame->fr_width -= 1;
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
1655 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001656 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 frame_remove(win1->w_frame);
1658 win_append(win2, win1);
1659 frame_append(win2->w_frame, win1->w_frame);
1660
1661 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1662 redraw_later(NOT_VALID);
1663 }
1664 win_enter(win1, FALSE);
1665}
1666
1667/*
1668 * Make all windows the same height.
1669 * 'next_curwin' will soon be the current window, make sure it has enough
1670 * rows.
1671 */
1672 void
1673win_equal(next_curwin, current, dir)
1674 win_T *next_curwin; /* pointer to current window to be or NULL */
1675 int current; /* do only frame with current window */
1676 int dir; /* 'v' for vertically, 'h' for horizontally,
1677 'b' for both, 0 for using p_ead */
1678{
1679 if (dir == 0)
1680#ifdef FEAT_VERTSPLIT
1681 dir = *p_ead;
1682#else
1683 dir = 'b';
1684#endif
1685 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001686 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001687 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688}
1689
1690/*
1691 * Set a frame to a new position and height, spreading the available room
1692 * equally over contained frames.
1693 * The window "next_curwin" (if not NULL) should at least get the size from
1694 * 'winheight' and 'winwidth' if possible.
1695 */
1696 static void
1697win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1698 win_T *next_curwin; /* pointer to current window to be or NULL */
1699 int current; /* do only frame with current window */
1700 frame_T *topfr; /* frame to set size off */
1701 int dir; /* 'v', 'h' or 'b', see win_equal() */
1702 int col; /* horizontal position for frame */
1703 int row; /* vertical position for frame */
1704 int width; /* new width of frame */
1705 int height; /* new height of frame */
1706{
1707 int n, m;
1708 int extra_sep = 0;
1709 int wincount, totwincount = 0;
1710 frame_T *fr;
1711 int next_curwin_size = 0;
1712 int room = 0;
1713 int new_size;
1714 int has_next_curwin = 0;
1715 int hnc;
1716
1717 if (topfr->fr_layout == FR_LEAF)
1718 {
1719 /* Set the width/height of this frame.
1720 * Redraw when size or position changes */
1721 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1722#ifdef FEAT_VERTSPLIT
1723 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1724#endif
1725 )
1726 {
1727 topfr->fr_win->w_winrow = row;
1728 frame_new_height(topfr, height, FALSE, FALSE);
1729#ifdef FEAT_VERTSPLIT
1730 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001731 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732#endif
1733 redraw_all_later(CLEAR);
1734 }
1735 }
1736#ifdef FEAT_VERTSPLIT
1737 else if (topfr->fr_layout == FR_ROW)
1738 {
1739 topfr->fr_width = width;
1740 topfr->fr_height = height;
1741
1742 if (dir != 'v') /* equalize frame widths */
1743 {
1744 /* Compute the maximum number of windows horizontally in this
1745 * frame. */
1746 n = frame_minwidth(topfr, NOWIN);
1747 /* add one for the rightmost window, it doesn't have a separator */
1748 if (col + width == Columns)
1749 extra_sep = 1;
1750 else
1751 extra_sep = 0;
1752 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001753 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001755 /*
1756 * Compute width for "next_curwin" window and room available for
1757 * other windows.
1758 * "m" is the minimal width when counting p_wiw for "next_curwin".
1759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 m = frame_minwidth(topfr, next_curwin);
1761 room = width - m;
1762 if (room < 0)
1763 {
1764 next_curwin_size = p_wiw + room;
1765 room = 0;
1766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 else
1768 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001769 next_curwin_size = -1;
1770 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1771 {
1772 /* If 'winfixwidth' set keep the window width if
1773 * possible.
1774 * Watch out for this window being the next_curwin. */
1775 if (frame_fixed_width(fr))
1776 {
1777 n = frame_minwidth(fr, NOWIN);
1778 new_size = fr->fr_width;
1779 if (frame_has_win(fr, next_curwin))
1780 {
1781 room += p_wiw - p_wmw;
1782 next_curwin_size = 0;
1783 if (new_size < p_wiw)
1784 new_size = p_wiw;
1785 }
1786 else
1787 /* These windows don't use up room. */
1788 totwincount -= (n + (fr->fr_next == NULL
1789 ? extra_sep : 0)) / (p_wmw + 1);
1790 room -= new_size - n;
1791 if (room < 0)
1792 {
1793 new_size += room;
1794 room = 0;
1795 }
1796 fr->fr_newwidth = new_size;
1797 }
1798 }
1799 if (next_curwin_size == -1)
1800 {
1801 if (!has_next_curwin)
1802 next_curwin_size = 0;
1803 else if (totwincount > 1
1804 && (room + (totwincount - 2))
1805 / (totwincount - 1) > p_wiw)
1806 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001807 /* Can make all windows wider than 'winwidth', spread
1808 * the room equally. */
1809 next_curwin_size = (room + p_wiw
1810 + (totwincount - 1) * p_wmw
1811 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001812 room -= next_curwin_size - p_wiw;
1813 }
1814 else
1815 next_curwin_size = p_wiw;
1816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001818
1819 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 --totwincount; /* don't count curwin */
1821 }
1822
1823 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1824 {
1825 n = m = 0;
1826 wincount = 1;
1827 if (fr->fr_next == NULL)
1828 /* last frame gets all that remains (avoid roundoff error) */
1829 new_size = width;
1830 else if (dir == 'v')
1831 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001832 else if (frame_fixed_width(fr))
1833 {
1834 new_size = fr->fr_newwidth;
1835 wincount = 0; /* doesn't count as a sizeable window */
1836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 else
1838 {
1839 /* Compute the maximum number of windows horiz. in "fr". */
1840 n = frame_minwidth(fr, NOWIN);
1841 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1842 / (p_wmw + 1);
1843 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 if (has_next_curwin)
1845 hnc = frame_has_win(fr, next_curwin);
1846 else
1847 hnc = FALSE;
1848 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 if (totwincount == 0)
1851 new_size = room;
1852 else
1853 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001855 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 next_curwin_size -= p_wiw - (m - n);
1858 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001859 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001861 else
1862 room -= new_size;
1863 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 }
1865
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001866 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 * window, unless equalizing all frames. */
1868 if (!current || dir != 'v' || topfr->fr_parent != NULL
1869 || (new_size != fr->fr_width)
1870 || frame_has_win(fr, next_curwin))
1871 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001872 new_size, height);
1873 col += new_size;
1874 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 totwincount -= wincount;
1876 }
1877 }
1878#endif
1879 else /* topfr->fr_layout == FR_COL */
1880 {
1881#ifdef FEAT_VERTSPLIT
1882 topfr->fr_width = width;
1883#endif
1884 topfr->fr_height = height;
1885
1886 if (dir != 'h') /* equalize frame heights */
1887 {
1888 /* Compute maximum number of windows vertically in this frame. */
1889 n = frame_minheight(topfr, NOWIN);
1890 /* add one for the bottom window if it doesn't have a statusline */
1891 if (row + height == cmdline_row && p_ls == 0)
1892 extra_sep = 1;
1893 else
1894 extra_sep = 0;
1895 totwincount = (n + extra_sep) / (p_wmh + 1);
1896 has_next_curwin = frame_has_win(topfr, next_curwin);
1897
1898 /*
1899 * Compute height for "next_curwin" window and room available for
1900 * other windows.
1901 * "m" is the minimal height when counting p_wh for "next_curwin".
1902 */
1903 m = frame_minheight(topfr, next_curwin);
1904 room = height - m;
1905 if (room < 0)
1906 {
1907 /* The room is less then 'winheight', use all space for the
1908 * current window. */
1909 next_curwin_size = p_wh + room;
1910 room = 0;
1911 }
1912 else
1913 {
1914 next_curwin_size = -1;
1915 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1916 {
1917 /* If 'winfixheight' set keep the window height if
1918 * possible.
1919 * Watch out for this window being the next_curwin. */
1920 if (frame_fixed_height(fr))
1921 {
1922 n = frame_minheight(fr, NOWIN);
1923 new_size = fr->fr_height;
1924 if (frame_has_win(fr, next_curwin))
1925 {
1926 room += p_wh - p_wmh;
1927 next_curwin_size = 0;
1928 if (new_size < p_wh)
1929 new_size = p_wh;
1930 }
1931 else
1932 /* These windows don't use up room. */
1933 totwincount -= (n + (fr->fr_next == NULL
1934 ? extra_sep : 0)) / (p_wmh + 1);
1935 room -= new_size - n;
1936 if (room < 0)
1937 {
1938 new_size += room;
1939 room = 0;
1940 }
1941 fr->fr_newheight = new_size;
1942 }
1943 }
1944 if (next_curwin_size == -1)
1945 {
1946 if (!has_next_curwin)
1947 next_curwin_size = 0;
1948 else if (totwincount > 1
1949 && (room + (totwincount - 2))
1950 / (totwincount - 1) > p_wh)
1951 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001952 /* can make all windows higher than 'winheight',
1953 * spread the room equally. */
1954 next_curwin_size = (room + p_wh
1955 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 + (totwincount - 1)) / totwincount;
1957 room -= next_curwin_size - p_wh;
1958 }
1959 else
1960 next_curwin_size = p_wh;
1961 }
1962 }
1963
1964 if (has_next_curwin)
1965 --totwincount; /* don't count curwin */
1966 }
1967
1968 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1969 {
1970 n = m = 0;
1971 wincount = 1;
1972 if (fr->fr_next == NULL)
1973 /* last frame gets all that remains (avoid roundoff error) */
1974 new_size = height;
1975 else if (dir == 'h')
1976 new_size = fr->fr_height;
1977 else if (frame_fixed_height(fr))
1978 {
1979 new_size = fr->fr_newheight;
1980 wincount = 0; /* doesn't count as a sizeable window */
1981 }
1982 else
1983 {
1984 /* Compute the maximum number of windows vert. in "fr". */
1985 n = frame_minheight(fr, NOWIN);
1986 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1987 / (p_wmh + 1);
1988 m = frame_minheight(fr, next_curwin);
1989 if (has_next_curwin)
1990 hnc = frame_has_win(fr, next_curwin);
1991 else
1992 hnc = FALSE;
1993 if (hnc) /* don't count next_curwin */
1994 --wincount;
1995 if (totwincount == 0)
1996 new_size = room;
1997 else
1998 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1999 / totwincount;
2000 if (hnc) /* add next_curwin size */
2001 {
2002 next_curwin_size -= p_wh - (m - n);
2003 new_size += next_curwin_size;
2004 room -= new_size - next_curwin_size;
2005 }
2006 else
2007 room -= new_size;
2008 new_size += n;
2009 }
2010 /* Skip frame that is full width when splitting or closing a
2011 * window, unless equalizing all frames. */
2012 if (!current || dir != 'h' || topfr->fr_parent != NULL
2013 || (new_size != fr->fr_height)
2014 || frame_has_win(fr, next_curwin))
2015 win_equal_rec(next_curwin, current, fr, dir, col, row,
2016 width, new_size);
2017 row += new_size;
2018 height -= new_size;
2019 totwincount -= wincount;
2020 }
2021 }
2022}
2023
2024/*
2025 * close all windows for buffer 'buf'
2026 */
2027 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002030 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002032 win_T *wp;
2033 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002034 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002037
2038 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002040 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2041#ifdef FEAT_AUTOCMD
2042 && !(wp->w_closing || wp->w_buffer->b_closing)
2043#endif
2044 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002046 win_close(wp, FALSE);
2047
2048 /* Start all over, autocommands may change the window layout. */
2049 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002052 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002054
2055 /* Also check windows in other tab pages. */
2056 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2057 {
2058 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002059 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002061 if (wp->w_buffer == buf
2062#ifdef FEAT_AUTOCMD
2063 && !(wp->w_closing || wp->w_buffer->b_closing)
2064#endif
2065 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002066 {
2067 win_close_othertab(wp, FALSE, tp);
2068
2069 /* Start all over, the tab page may be closed and
2070 * autocommands may change the window layout. */
2071 nexttp = first_tabpage;
2072 break;
2073 }
2074 }
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002077
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002078 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002079 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002080 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081}
2082
2083/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002084 * Return TRUE if the current window is the only window that exists (ignoring
2085 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002086 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002087 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002088 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002089last_window()
2090{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002091 return (one_window() && first_tabpage->tp_next == NULL);
2092}
2093
2094/*
2095 * Return TRUE if there is only one window other than "aucmd_win" in the
2096 * current tab page.
2097 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002098 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002099one_window()
2100{
2101#ifdef FEAT_AUTOCMD
2102 win_T *wp;
2103 int seen_one = FALSE;
2104
2105 FOR_ALL_WINDOWS(wp)
2106 {
2107 if (wp != aucmd_win)
2108 {
2109 if (seen_one)
2110 return FALSE;
2111 seen_one = TRUE;
2112 }
2113 }
2114 return TRUE;
2115#else
2116 return firstwin == lastwin;
2117#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002118}
2119
2120/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002121 * Close the possibly last window in a tab page.
2122 * Returns TRUE when the window was closed already.
2123 */
2124 static int
2125close_last_window_tabpage(win, free_buf, prev_curtab)
2126 win_T *win;
2127 int free_buf;
2128 tabpage_T *prev_curtab;
2129{
2130 if (firstwin == lastwin)
2131 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002132#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002133 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002134#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002135
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002136 /*
2137 * Closing the last window in a tab page. First go to another tab
2138 * page and then close the window and the tab page. This avoids that
2139 * curwin and curtab are invalid while we are freeing memory, they may
2140 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002141 * Don't trigger autocommands yet, they may use wrong values, so do
2142 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002143 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002144 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002145 redraw_tabline = TRUE;
2146
2147 /* Safety check: Autocommands may have closed the window when jumping
2148 * to the other tab page. */
2149 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2150 {
2151 int h = tabline_height();
2152
2153 win_close_othertab(win, free_buf, prev_curtab);
2154 if (h != tabline_height())
2155 shell_new_rows();
2156 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002157 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2158 * that now. */
2159#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002160 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002161 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2162 if (old_curbuf != curbuf)
2163 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002164#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002165 return TRUE;
2166 }
2167 return FALSE;
2168}
2169
2170/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002171 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 * If "free_buf" is TRUE related buffer may be unloaded.
2173 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002174 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
2176 void
2177win_close(win, free_buf)
2178 win_T *win;
2179 int free_buf;
2180{
2181 win_T *wp;
2182#ifdef FEAT_AUTOCMD
2183 int other_buffer = FALSE;
2184#endif
2185 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 int dir;
2187 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002188 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002190 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 EMSG(_("E444: Cannot close last window"));
2193 return;
2194 }
2195
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002196#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002197 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002198 return; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002199 if (win == aucmd_win)
2200 {
2201 EMSG(_("E813: Cannot close autocmd window"));
2202 return;
2203 }
2204 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2205 {
2206 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2207 return;
2208 }
2209#endif
2210
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002211 /* When closing the last window in a tab page first go to another tab page
2212 * and then close the window and the tab page to avoid that curwin and
2213 * curtab are invalid while we are freeing memory. */
2214 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2215 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 /* When closing the help window, try restoring a snapshot after closing
2218 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002219 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 help_window = TRUE;
2221 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002222 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224#ifdef FEAT_AUTOCMD
2225 if (win == curwin)
2226 {
2227 /*
2228 * Guess which window is going to be the new current window.
2229 * This may change because of the autocommands (sigh).
2230 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002231 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002234 * Be careful: If autocommands delete the window or cause this window
2235 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 */
2237 if (wp->w_buffer != curbuf)
2238 {
2239 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002240 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002242 if (!win_valid(win))
2243 return;
2244 win->w_closing = FALSE;
2245 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 return;
2247 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002248 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002250 if (!win_valid(win))
2251 return;
2252 win->w_closing = FALSE;
2253 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return;
2255# ifdef FEAT_EVAL
2256 /* autocmds may abort script processing */
2257 if (aborting())
2258 return;
2259# endif
2260 }
2261#endif
2262
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002263#ifdef FEAT_GUI
2264 /* Avoid trouble with scrollbars that are going to be deleted in
2265 * win_free(). */
2266 if (gui.in_use)
2267 out_flush();
2268#endif
2269
Bram Moolenaara971b822011-09-14 14:43:25 +02002270#ifdef FEAT_SYN_HL
2271 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002272 if (win->w_buffer != NULL)
2273 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002274#endif
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /*
2277 * Close the link to the buffer.
2278 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002279 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002280 {
2281#ifdef FEAT_AUTOCMD
2282 win->w_closing = TRUE;
2283#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002284 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002285#ifdef FEAT_AUTOCMD
2286 if (win_valid(win))
2287 win->w_closing = FALSE;
2288#endif
2289 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002290
Bram Moolenaar802418d2013-01-17 14:00:11 +01002291 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2292 && (last_window() || curtab != prev_curtab
2293 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002294 {
2295 /* Autocommands have close all windows, quit now. Restore
2296 * curwin->w_buffer, otherwise writing viminfo may fail. */
2297 if (curwin->w_buffer == NULL)
2298 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002299 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002300 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002303 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002304 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002305 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 return;
2307
Bram Moolenaara971b822011-09-14 14:43:25 +02002308 /* Free the memory used for the window and get the window that received
2309 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002310 wp = win_free_mem(win, &dir, NULL);
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 /* Make sure curwin isn't invalid. It can cause severe trouble when
2313 * printing an error message. For win_equal() curbuf needs to be valid
2314 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002315 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 curwin = wp;
2318#ifdef FEAT_QUICKFIX
2319 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2320 {
2321 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002322 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * finding another window to go to.
2324 */
2325 for (;;)
2326 {
2327 if (wp->w_next == NULL)
2328 wp = firstwin;
2329 else
2330 wp = wp->w_next;
2331 if (wp == curwin)
2332 break;
2333 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2334 {
2335 curwin = wp;
2336 break;
2337 }
2338 }
2339 }
2340#endif
2341 curbuf = curwin->w_buffer;
2342 close_curwin = TRUE;
2343 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002344 if (p_ea
2345#ifdef FEAT_VERTSPLIT
2346 && (*p_ead == 'b' || *p_ead == dir)
2347#endif
2348 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 win_equal(curwin, TRUE,
2350#ifdef FEAT_VERTSPLIT
2351 dir
2352#else
2353 0
2354#endif
2355 );
2356 else
2357 win_comp_pos();
2358 if (close_curwin)
2359 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002360 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#ifdef FEAT_AUTOCMD
2362 if (other_buffer)
2363 /* careful: after this wp and win may be invalid! */
2364 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2365#endif
2366 }
2367
2368 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002369 * If last window has a status line now and we don't want one,
2370 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
2372 last_status(FALSE);
2373
2374 /* After closing the help window, try restoring the window layout from
2375 * before it was opened. */
2376 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002377 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2380 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2381 if (gui.in_use && !win_hasvertsplit())
2382 gui_init_which_components(NULL);
2383#endif
2384
2385 redraw_all_later(NOT_VALID);
2386}
2387
2388/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002389 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002390 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002391 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002392 * Caller must check if buffer is hidden and whether the tabline needs to be
2393 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002394 */
2395 void
2396win_close_othertab(win, free_buf, tp)
2397 win_T *win;
2398 int free_buf;
2399 tabpage_T *tp;
2400{
2401 win_T *wp;
2402 int dir;
2403 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002404 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002405
Bram Moolenaar362ce482012-06-06 19:02:45 +02002406#ifdef FEAT_AUTOCMD
2407 if (win->w_closing || win->w_buffer->b_closing)
2408 return; /* window is already being closed */
2409#endif
2410
Bram Moolenaarf740b292006-02-16 22:11:02 +00002411 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002412 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002413
2414 /* Careful: Autocommands may have closed the tab page or made it the
2415 * current tab page. */
2416 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2417 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002418 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 return;
2420
2421 /* Autocommands may have closed the window already. */
2422 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2423 ;
2424 if (wp == NULL)
2425 return;
2426
Bram Moolenaarf740b292006-02-16 22:11:02 +00002427 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002428 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002429 {
2430 if (tp == first_tabpage)
2431 first_tabpage = tp->tp_next;
2432 else
2433 {
2434 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2435 ptp = ptp->tp_next)
2436 ;
2437 if (ptp == NULL)
2438 {
2439 EMSG2(_(e_intern2), "win_close_othertab()");
2440 return;
2441 }
2442 ptp->tp_next = tp->tp_next;
2443 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002444 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002446
2447 /* Free the memory used for the window. */
2448 win_free_mem(win, &dir, tp);
2449
2450 if (free_tp)
2451 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452}
2453
2454/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002455 * Free the memory used for a window.
2456 * Returns a pointer to the window that got the freed up space.
2457 */
2458 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460 win_T *win;
2461 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002462 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002463{
2464 frame_T *frp;
2465 win_T *wp;
2466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002467 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002468 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002469 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002470 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002471 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002472
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002473 /* When deleting the current window of another tab page select a new
2474 * current window. */
2475 if (tp != NULL && win == tp->tp_curwin)
2476 tp->tp_curwin = wp;
2477
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002478 return wp;
2479}
2480
2481#if defined(EXITFREE) || defined(PROTO)
2482 void
2483win_free_all()
2484{
2485 int dummy;
2486
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487# ifdef FEAT_WINDOWS
2488 while (first_tabpage->tp_next != NULL)
2489 tabpage_close(TRUE);
2490# endif
2491
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002492# ifdef FEAT_AUTOCMD
2493 if (aucmd_win != NULL)
2494 {
2495 (void)win_free_mem(aucmd_win, &dummy, NULL);
2496 aucmd_win = NULL;
2497 }
2498# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002499
2500 while (firstwin != NULL)
2501 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002502}
2503#endif
2504
2505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 * Remove a window and its frame from the tree of frames.
2507 * Returns a pointer to the window that got the freed up space.
2508 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002509 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002510winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002512 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 frame_T *frp, *frp2, *frp3;
2516 frame_T *frp_close = win->w_frame;
2517 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002520 * If there is only one window there is nothing to remove.
2521 */
2522 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2523 return NULL;
2524
2525 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * Remove the window from its frame.
2527 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002528 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 wp = frame2win(frp2);
2530
2531 /* Remove this frame from the list of frames. */
2532 frame_remove(frp_close);
2533
2534#ifdef FEAT_VERTSPLIT
2535 if (frp_close->fr_parent->fr_layout == FR_COL)
2536 {
2537#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002538 /* When 'winfixheight' is set, try to find another frame in the column
2539 * (as close to the closed frame as possible) to distribute the height
2540 * to. */
2541 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2542 {
2543 frp = frp_close->fr_prev;
2544 frp3 = frp_close->fr_next;
2545 while (frp != NULL || frp3 != NULL)
2546 {
2547 if (frp != NULL)
2548 {
2549 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2550 {
2551 frp2 = frp;
2552 wp = frp->fr_win;
2553 break;
2554 }
2555 frp = frp->fr_prev;
2556 }
2557 if (frp3 != NULL)
2558 {
2559 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2560 {
2561 frp2 = frp3;
2562 wp = frp3->fr_win;
2563 break;
2564 }
2565 frp3 = frp3->fr_next;
2566 }
2567 }
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2570 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef FEAT_VERTSPLIT
2572 *dirp = 'v';
2573 }
2574 else
2575 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002576 /* When 'winfixwidth' is set, try to find another frame in the column
2577 * (as close to the closed frame as possible) to distribute the width
2578 * to. */
2579 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2580 {
2581 frp = frp_close->fr_prev;
2582 frp3 = frp_close->fr_next;
2583 while (frp != NULL || frp3 != NULL)
2584 {
2585 if (frp != NULL)
2586 {
2587 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2588 {
2589 frp2 = frp;
2590 wp = frp->fr_win;
2591 break;
2592 }
2593 frp = frp->fr_prev;
2594 }
2595 if (frp3 != NULL)
2596 {
2597 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2598 {
2599 frp2 = frp3;
2600 wp = frp3->fr_win;
2601 break;
2602 }
2603 frp3 = frp3->fr_next;
2604 }
2605 }
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002608 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 *dirp = 'h';
2610 }
2611#endif
2612
2613 /* If rows/columns go to a window below/right its positions need to be
2614 * updated. Can only be done after the sizes have been updated. */
2615 if (frp2 == frp_close->fr_next)
2616 {
2617 int row = win->w_winrow;
2618 int col = W_WINCOL(win);
2619
2620 frame_comp_pos(frp2, &row, &col);
2621 }
2622
2623 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2624 {
2625 /* There is no other frame in this list, move its info to the parent
2626 * and remove it. */
2627 frp2->fr_parent->fr_layout = frp2->fr_layout;
2628 frp2->fr_parent->fr_child = frp2->fr_child;
2629 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2630 frp->fr_parent = frp2->fr_parent;
2631 frp2->fr_parent->fr_win = frp2->fr_win;
2632 if (frp2->fr_win != NULL)
2633 frp2->fr_win->w_frame = frp2->fr_parent;
2634 frp = frp2->fr_parent;
2635 vim_free(frp2);
2636
2637 frp2 = frp->fr_parent;
2638 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2639 {
2640 /* The frame above the parent has the same layout, have to merge
2641 * the frames into this list. */
2642 if (frp2->fr_child == frp)
2643 frp2->fr_child = frp->fr_child;
2644 frp->fr_child->fr_prev = frp->fr_prev;
2645 if (frp->fr_prev != NULL)
2646 frp->fr_prev->fr_next = frp->fr_child;
2647 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2648 {
2649 frp3->fr_parent = frp2;
2650 if (frp3->fr_next == NULL)
2651 {
2652 frp3->fr_next = frp->fr_next;
2653 if (frp->fr_next != NULL)
2654 frp->fr_next->fr_prev = frp3;
2655 break;
2656 }
2657 }
2658 vim_free(frp);
2659 }
2660 }
2661
2662 return wp;
2663}
2664
2665/*
2666 * Find out which frame is going to get the freed up space when "win" is
2667 * closed.
2668 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2669 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2670 * This makes opening a window and closing it immediately keep the same window
2671 * layout.
2672 */
2673 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002674win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002676 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 frame_T *frp;
2679 int b;
2680
Bram Moolenaarf740b292006-02-16 22:11:02 +00002681 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002682 /* Last window in this tab page, will go to next tab page. */
2683 return alt_tabpage()->tp_curwin->w_frame;
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 frp = win->w_frame;
2686#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002687 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 b = p_spr;
2689 else
2690#endif
2691 b = p_sb;
2692 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2693 return frp->fr_next;
2694 return frp->fr_prev;
2695}
2696
2697/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002698 * Return the tabpage that will be used if the current one is closed.
2699 */
2700 static tabpage_T *
2701alt_tabpage()
2702{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002703 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002704
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002705 /* Use the next tab page if possible. */
2706 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002707 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002708
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002709 /* Find the last but one tab page. */
2710 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2711 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002712 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002713}
2714
2715/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 * Find the left-upper window in frame "frp".
2717 */
2718 static win_T *
2719frame2win(frp)
2720 frame_T *frp;
2721{
2722 while (frp->fr_win == NULL)
2723 frp = frp->fr_child;
2724 return frp->fr_win;
2725}
2726
2727/*
2728 * Return TRUE if frame "frp" contains window "wp".
2729 */
2730 static int
2731frame_has_win(frp, wp)
2732 frame_T *frp;
2733 win_T *wp;
2734{
2735 frame_T *p;
2736
2737 if (frp->fr_layout == FR_LEAF)
2738 return frp->fr_win == wp;
2739
2740 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2741 if (frame_has_win(p, wp))
2742 return TRUE;
2743 return FALSE;
2744}
2745
2746/*
2747 * Set a new height for a frame. Recursively sets the height for contained
2748 * frames and windows. Caller must take care of positions.
2749 */
2750 static void
2751frame_new_height(topfrp, height, topfirst, wfh)
2752 frame_T *topfrp;
2753 int height;
2754 int topfirst; /* resize topmost contained frame first */
2755 int wfh; /* obey 'winfixheight' when there is a choice;
2756 may cause the height not to be set */
2757{
2758 frame_T *frp;
2759 int extra_lines;
2760 int h;
2761
2762 if (topfrp->fr_win != NULL)
2763 {
2764 /* Simple case: just one window. */
2765 win_new_height(topfrp->fr_win,
2766 height - topfrp->fr_win->w_status_height);
2767 }
2768#ifdef FEAT_VERTSPLIT
2769 else if (topfrp->fr_layout == FR_ROW)
2770 {
2771 do
2772 {
2773 /* All frames in this row get the same new height. */
2774 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2775 {
2776 frame_new_height(frp, height, topfirst, wfh);
2777 if (frp->fr_height > height)
2778 {
2779 /* Could not fit the windows, make the whole row higher. */
2780 height = frp->fr_height;
2781 break;
2782 }
2783 }
2784 }
2785 while (frp != NULL);
2786 }
2787#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002788 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
2790 /* Complicated case: Resize a column of frames. Resize the bottom
2791 * frame first, frames above that when needed. */
2792
2793 frp = topfrp->fr_child;
2794 if (wfh)
2795 /* Advance past frames with one window with 'wfh' set. */
2796 while (frame_fixed_height(frp))
2797 {
2798 frp = frp->fr_next;
2799 if (frp == NULL)
2800 return; /* no frame without 'wfh', give up */
2801 }
2802 if (!topfirst)
2803 {
2804 /* Find the bottom frame of this column */
2805 while (frp->fr_next != NULL)
2806 frp = frp->fr_next;
2807 if (wfh)
2808 /* Advance back for frames with one window with 'wfh' set. */
2809 while (frame_fixed_height(frp))
2810 frp = frp->fr_prev;
2811 }
2812
2813 extra_lines = height - topfrp->fr_height;
2814 if (extra_lines < 0)
2815 {
2816 /* reduce height of contained frames, bottom or top frame first */
2817 while (frp != NULL)
2818 {
2819 h = frame_minheight(frp, NULL);
2820 if (frp->fr_height + extra_lines < h)
2821 {
2822 extra_lines += frp->fr_height - h;
2823 frame_new_height(frp, h, topfirst, wfh);
2824 }
2825 else
2826 {
2827 frame_new_height(frp, frp->fr_height + extra_lines,
2828 topfirst, wfh);
2829 break;
2830 }
2831 if (topfirst)
2832 {
2833 do
2834 frp = frp->fr_next;
2835 while (wfh && frp != NULL && frame_fixed_height(frp));
2836 }
2837 else
2838 {
2839 do
2840 frp = frp->fr_prev;
2841 while (wfh && frp != NULL && frame_fixed_height(frp));
2842 }
2843 /* Increase "height" if we could not reduce enough frames. */
2844 if (frp == NULL)
2845 height -= extra_lines;
2846 }
2847 }
2848 else if (extra_lines > 0)
2849 {
2850 /* increase height of bottom or top frame */
2851 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2852 }
2853 }
2854 topfrp->fr_height = height;
2855}
2856
2857/*
2858 * Return TRUE if height of frame "frp" should not be changed because of
2859 * the 'winfixheight' option.
2860 */
2861 static int
2862frame_fixed_height(frp)
2863 frame_T *frp;
2864{
2865 /* frame with one window: fixed height if 'winfixheight' set. */
2866 if (frp->fr_win != NULL)
2867 return frp->fr_win->w_p_wfh;
2868
2869 if (frp->fr_layout == FR_ROW)
2870 {
2871 /* The frame is fixed height if one of the frames in the row is fixed
2872 * height. */
2873 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2874 if (frame_fixed_height(frp))
2875 return TRUE;
2876 return FALSE;
2877 }
2878
2879 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2880 * frames in the row are fixed height. */
2881 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2882 if (!frame_fixed_height(frp))
2883 return FALSE;
2884 return TRUE;
2885}
2886
2887#ifdef FEAT_VERTSPLIT
2888/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002889 * Return TRUE if width of frame "frp" should not be changed because of
2890 * the 'winfixwidth' option.
2891 */
2892 static int
2893frame_fixed_width(frp)
2894 frame_T *frp;
2895{
2896 /* frame with one window: fixed width if 'winfixwidth' set. */
2897 if (frp->fr_win != NULL)
2898 return frp->fr_win->w_p_wfw;
2899
2900 if (frp->fr_layout == FR_COL)
2901 {
2902 /* The frame is fixed width if one of the frames in the row is fixed
2903 * width. */
2904 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2905 if (frame_fixed_width(frp))
2906 return TRUE;
2907 return FALSE;
2908 }
2909
2910 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2911 * frames in the row are fixed width. */
2912 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2913 if (!frame_fixed_width(frp))
2914 return FALSE;
2915 return TRUE;
2916}
2917
2918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 * Add a status line to windows at the bottom of "frp".
2920 * Note: Does not check if there is room!
2921 */
2922 static void
2923frame_add_statusline(frp)
2924 frame_T *frp;
2925{
2926 win_T *wp;
2927
2928 if (frp->fr_layout == FR_LEAF)
2929 {
2930 wp = frp->fr_win;
2931 if (wp->w_status_height == 0)
2932 {
2933 if (wp->w_height > 0) /* don't make it negative */
2934 --wp->w_height;
2935 wp->w_status_height = STATUS_HEIGHT;
2936 }
2937 }
2938 else if (frp->fr_layout == FR_ROW)
2939 {
2940 /* Handle all the frames in the row. */
2941 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2942 frame_add_statusline(frp);
2943 }
2944 else /* frp->fr_layout == FR_COL */
2945 {
2946 /* Only need to handle the last frame in the column. */
2947 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2948 ;
2949 frame_add_statusline(frp);
2950 }
2951}
2952
2953/*
2954 * Set width of a frame. Handles recursively going through contained frames.
2955 * May remove separator line for windows at the right side (for win_close()).
2956 */
2957 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002958frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 frame_T *topfrp;
2960 int width;
2961 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002962 int wfw; /* obey 'winfixwidth' when there is a choice;
2963 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 frame_T *frp;
2966 int extra_cols;
2967 int w;
2968 win_T *wp;
2969
2970 if (topfrp->fr_layout == FR_LEAF)
2971 {
2972 /* Simple case: just one window. */
2973 wp = topfrp->fr_win;
2974 /* Find out if there are any windows right of this one. */
2975 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2976 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2977 break;
2978 if (frp->fr_parent == NULL)
2979 wp->w_vsep_width = 0;
2980 win_new_width(wp, width - wp->w_vsep_width);
2981 }
2982 else if (topfrp->fr_layout == FR_COL)
2983 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002984 do
2985 {
2986 /* All frames in this column get the same new width. */
2987 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2988 {
2989 frame_new_width(frp, width, leftfirst, wfw);
2990 if (frp->fr_width > width)
2991 {
2992 /* Could not fit the windows, make whole column wider. */
2993 width = frp->fr_width;
2994 break;
2995 }
2996 }
2997 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
2999 else /* fr_layout == FR_ROW */
3000 {
3001 /* Complicated case: Resize a row of frames. Resize the rightmost
3002 * frame first, frames left of it when needed. */
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003005 if (wfw)
3006 /* Advance past frames with one window with 'wfw' set. */
3007 while (frame_fixed_width(frp))
3008 {
3009 frp = frp->fr_next;
3010 if (frp == NULL)
3011 return; /* no frame without 'wfw', give up */
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003014 {
3015 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 while (frp->fr_next != NULL)
3017 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003018 if (wfw)
3019 /* Advance back for frames with one window with 'wfw' set. */
3020 while (frame_fixed_width(frp))
3021 frp = frp->fr_prev;
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 extra_cols = width - topfrp->fr_width;
3025 if (extra_cols < 0)
3026 {
3027 /* reduce frame width, rightmost frame first */
3028 while (frp != NULL)
3029 {
3030 w = frame_minwidth(frp, NULL);
3031 if (frp->fr_width + extra_cols < w)
3032 {
3033 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003034 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 else
3037 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003038 frame_new_width(frp, frp->fr_width + extra_cols,
3039 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 break;
3041 }
3042 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003043 {
3044 do
3045 frp = frp->fr_next;
3046 while (wfw && frp != NULL && frame_fixed_width(frp));
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003049 {
3050 do
3051 frp = frp->fr_prev;
3052 while (wfw && frp != NULL && frame_fixed_width(frp));
3053 }
3054 /* Increase "width" if we could not reduce enough frames. */
3055 if (frp == NULL)
3056 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058 }
3059 else if (extra_cols > 0)
3060 {
3061 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003062 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 }
3064 }
3065 topfrp->fr_width = width;
3066}
3067
3068/*
3069 * Add the vertical separator to windows at the right side of "frp".
3070 * Note: Does not check if there is room!
3071 */
3072 static void
3073frame_add_vsep(frp)
3074 frame_T *frp;
3075{
3076 win_T *wp;
3077
3078 if (frp->fr_layout == FR_LEAF)
3079 {
3080 wp = frp->fr_win;
3081 if (wp->w_vsep_width == 0)
3082 {
3083 if (wp->w_width > 0) /* don't make it negative */
3084 --wp->w_width;
3085 wp->w_vsep_width = 1;
3086 }
3087 }
3088 else if (frp->fr_layout == FR_COL)
3089 {
3090 /* Handle all the frames in the column. */
3091 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3092 frame_add_vsep(frp);
3093 }
3094 else /* frp->fr_layout == FR_ROW */
3095 {
3096 /* Only need to handle the last frame in the row. */
3097 frp = frp->fr_child;
3098 while (frp->fr_next != NULL)
3099 frp = frp->fr_next;
3100 frame_add_vsep(frp);
3101 }
3102}
3103
3104/*
3105 * Set frame width from the window it contains.
3106 */
3107 static void
3108frame_fix_width(wp)
3109 win_T *wp;
3110{
3111 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3112}
3113#endif
3114
3115/*
3116 * Set frame height from the window it contains.
3117 */
3118 static void
3119frame_fix_height(wp)
3120 win_T *wp;
3121{
3122 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3123}
3124
3125/*
3126 * Compute the minimal height for frame "topfrp".
3127 * Uses the 'winminheight' option.
3128 * When "next_curwin" isn't NULL, use p_wh for this window.
3129 * When "next_curwin" is NOWIN, don't use at least one line for the current
3130 * window.
3131 */
3132 static int
3133frame_minheight(topfrp, next_curwin)
3134 frame_T *topfrp;
3135 win_T *next_curwin;
3136{
3137 frame_T *frp;
3138 int m;
3139#ifdef FEAT_VERTSPLIT
3140 int n;
3141#endif
3142
3143 if (topfrp->fr_win != NULL)
3144 {
3145 if (topfrp->fr_win == next_curwin)
3146 m = p_wh + topfrp->fr_win->w_status_height;
3147 else
3148 {
3149 /* window: minimal height of the window plus status line */
3150 m = p_wmh + topfrp->fr_win->w_status_height;
3151 /* Current window is minimal one line high */
3152 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3153 ++m;
3154 }
3155 }
3156#ifdef FEAT_VERTSPLIT
3157 else if (topfrp->fr_layout == FR_ROW)
3158 {
3159 /* get the minimal height from each frame in this row */
3160 m = 0;
3161 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3162 {
3163 n = frame_minheight(frp, next_curwin);
3164 if (n > m)
3165 m = n;
3166 }
3167 }
3168#endif
3169 else
3170 {
3171 /* Add up the minimal heights for all frames in this column. */
3172 m = 0;
3173 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3174 m += frame_minheight(frp, next_curwin);
3175 }
3176
3177 return m;
3178}
3179
3180#ifdef FEAT_VERTSPLIT
3181/*
3182 * Compute the minimal width for frame "topfrp".
3183 * When "next_curwin" isn't NULL, use p_wiw for this window.
3184 * When "next_curwin" is NOWIN, don't use at least one column for the current
3185 * window.
3186 */
3187 static int
3188frame_minwidth(topfrp, next_curwin)
3189 frame_T *topfrp;
3190 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3191{
3192 frame_T *frp;
3193 int m, n;
3194
3195 if (topfrp->fr_win != NULL)
3196 {
3197 if (topfrp->fr_win == next_curwin)
3198 m = p_wiw + topfrp->fr_win->w_vsep_width;
3199 else
3200 {
3201 /* window: minimal width of the window plus separator column */
3202 m = p_wmw + topfrp->fr_win->w_vsep_width;
3203 /* Current window is minimal one column wide */
3204 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3205 ++m;
3206 }
3207 }
3208 else if (topfrp->fr_layout == FR_COL)
3209 {
3210 /* get the minimal width from each frame in this column */
3211 m = 0;
3212 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3213 {
3214 n = frame_minwidth(frp, next_curwin);
3215 if (n > m)
3216 m = n;
3217 }
3218 }
3219 else
3220 {
3221 /* Add up the minimal widths for all frames in this row. */
3222 m = 0;
3223 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3224 m += frame_minwidth(frp, next_curwin);
3225 }
3226
3227 return m;
3228}
3229#endif
3230
3231
3232/*
3233 * Try to close all windows except current one.
3234 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3235 * used and the buffer was modified.
3236 *
3237 * Used by ":bdel" and ":only".
3238 */
3239 void
3240close_others(message, forceit)
3241 int message;
3242 int forceit; /* always hide all other windows */
3243{
3244 win_T *wp;
3245 win_T *nextwp;
3246 int r;
3247
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003248 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 if (message
3251#ifdef FEAT_AUTOCMD
3252 && !autocmd_busy
3253#endif
3254 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003255 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 return;
3257 }
3258
3259 /* Be very careful here: autocommands may change the window layout. */
3260 for (wp = firstwin; win_valid(wp); wp = nextwp)
3261 {
3262 nextwp = wp->w_next;
3263 if (wp != curwin) /* don't close current window */
3264 {
3265
3266 /* Check if it's allowed to abandon this window */
3267 r = can_abandon(wp->w_buffer, forceit);
3268#ifdef FEAT_AUTOCMD
3269 if (!win_valid(wp)) /* autocommands messed wp up */
3270 {
3271 nextwp = firstwin;
3272 continue;
3273 }
3274#endif
3275 if (!r)
3276 {
3277#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3278 if (message && (p_confirm || cmdmod.confirm) && p_write)
3279 {
3280 dialog_changed(wp->w_buffer, FALSE);
3281# ifdef FEAT_AUTOCMD
3282 if (!win_valid(wp)) /* autocommands messed wp up */
3283 {
3284 nextwp = firstwin;
3285 continue;
3286 }
3287# endif
3288 }
3289 if (bufIsChanged(wp->w_buffer))
3290#endif
3291 continue;
3292 }
3293 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3294 }
3295 }
3296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003297 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 EMSG(_("E445: Other window contains changes"));
3299}
3300
3301#endif /* FEAT_WINDOWS */
3302
3303/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003304 * Init the current window "curwin".
3305 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 */
3307 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003308curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003310 win_init_empty(curwin);
3311}
3312
3313 void
3314win_init_empty(wp)
3315 win_T *wp;
3316{
3317 redraw_win_later(wp, NOT_VALID);
3318 wp->w_lines_valid = 0;
3319 wp->w_cursor.lnum = 1;
3320 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003322 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003324 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3325 wp->w_pcmark.col = 0;
3326 wp->w_prev_pcmark.lnum = 0;
3327 wp->w_prev_pcmark.col = 0;
3328 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003330 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003332 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003334 if (wp->w_p_rl)
3335 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003337 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003339#ifdef FEAT_SYN_HL
3340 wp->w_s = &wp->w_buffer->b_s;
3341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342}
3343
3344/*
3345 * Allocate the first window and put an empty buffer in it.
3346 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003347 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350win_alloc_first()
3351{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003352 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 return FAIL;
3354
3355#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003356 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357 if (first_tabpage == NULL)
3358 return FAIL;
3359 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003360 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003361#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003362
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003363 return OK;
3364}
3365
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003366#if defined(FEAT_AUTOCMD) || defined(PROTO)
3367/*
3368 * Init "aucmd_win". This can only be done after the first
3369 * window is fully initialized, thus it can't be in win_alloc_first().
3370 */
3371 void
3372win_alloc_aucmd_win()
3373{
3374 aucmd_win = win_alloc(NULL, TRUE);
3375 if (aucmd_win != NULL)
3376 {
3377 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003378 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003379 new_frame(aucmd_win);
3380 }
3381}
3382#endif
3383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003385 * Allocate the first window or the first window in a new tab page.
3386 * When "oldwin" is NULL create an empty buffer for it.
3387 * When "oldwin" is not NULL copy info from it to the new window (only with
3388 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 * Return FAIL when something goes wrong (out of memory).
3390 */
3391 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003392win_alloc_firstwin(oldwin)
3393 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003395 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003396 if (oldwin == NULL)
3397 {
3398 /* Very first window, need to create an empty buffer for it and
3399 * initialize from scratch. */
3400 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3401 if (curwin == NULL || curbuf == NULL)
3402 return FAIL;
3403 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003404#ifdef FEAT_SYN_HL
3405 curwin->w_s = &(curbuf->b_s);
3406#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003407 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003409 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003411 curwin_init(); /* init current window */
3412 }
3413#ifdef FEAT_WINDOWS
3414 else
3415 {
3416 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003417 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003418
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003419 /* We don't want cursor- and scroll-binding in the first window. */
3420 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003421 }
3422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003424 new_frame(curwin);
3425 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003427 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#ifdef FEAT_VERTSPLIT
3429 topframe->fr_width = Columns;
3430#endif
3431 topframe->fr_height = Rows - p_ch;
3432 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433
3434 return OK;
3435}
3436
3437/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003438 * Create a frame for window "wp".
3439 */
3440 static void
3441new_frame(win_T *wp)
3442{
3443 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3444
3445 wp->w_frame = frp;
3446 if (frp != NULL)
3447 {
3448 frp->fr_layout = FR_LEAF;
3449 frp->fr_win = wp;
3450 }
3451}
3452
3453/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454 * Initialize the window and frame size to the maximum.
3455 */
3456 void
3457win_init_size()
3458{
3459 firstwin->w_height = ROWS_AVAIL;
3460 topframe->fr_height = ROWS_AVAIL;
3461#ifdef FEAT_VERTSPLIT
3462 firstwin->w_width = Columns;
3463 topframe->fr_width = Columns;
3464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465}
3466
3467#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003468
3469/*
3470 * Allocate a new tabpage_T and init the values.
3471 * Returns NULL when out of memory.
3472 */
3473 static tabpage_T *
3474alloc_tabpage()
3475{
3476 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003477# ifdef FEAT_GUI
3478 int i;
3479# endif
3480
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003481
3482 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003483 if (tp == NULL)
3484 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003485
Bram Moolenaar429fa852013-04-15 12:27:36 +02003486# ifdef FEAT_EVAL
3487 /* init t: variables */
3488 tp->tp_vars = dict_alloc();
3489 if (tp->tp_vars == NULL)
3490 {
3491 vim_free(tp);
3492 return NULL;
3493 }
3494 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3495# endif
3496
3497# ifdef FEAT_GUI
3498 for (i = 0; i < 3; i++)
3499 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003500# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003501# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003502 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003503# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003504 tp->tp_ch_used = p_ch;
3505
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003506 return tp;
3507}
3508
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003509 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003510free_tabpage(tp)
3511 tabpage_T *tp;
3512{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003513 int idx;
3514
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003515# ifdef FEAT_DIFF
3516 diff_clear(tp);
3517# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003518 for (idx = 0; idx < SNAP_COUNT; ++idx)
3519 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003520#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003521 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3522 hash_init(&tp->tp_vars->dv_hashtab);
3523 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003524#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003525
3526#ifdef FEAT_PYTHON
3527 python_tabpage_free(tp);
3528#endif
3529
3530#ifdef FEAT_PYTHON3
3531 python3_tabpage_free(tp);
3532#endif
3533
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003534 vim_free(tp);
3535}
3536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003538 * Create a new Tab page with one window.
3539 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003540 * When "after" is 0 put it just after the current Tab page.
3541 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542 * Return FAIL or OK.
3543 */
3544 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003545win_new_tabpage(after)
3546 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003547{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003548 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003550 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553 if (newtp == NULL)
3554 return FAIL;
3555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003556 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003557 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003558 {
3559 vim_free(newtp);
3560 return FAIL;
3561 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003562 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563
3564 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003565 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003568 if (after == 1)
3569 {
3570 /* New tab page becomes the first one. */
3571 newtp->tp_next = first_tabpage;
3572 first_tabpage = newtp;
3573 }
3574 else
3575 {
3576 if (after > 0)
3577 {
3578 /* Put new tab page before tab page "after". */
3579 n = 2;
3580 for (tp = first_tabpage; tp->tp_next != NULL
3581 && n < after; tp = tp->tp_next)
3582 ++n;
3583 }
3584 newtp->tp_next = tp->tp_next;
3585 tp->tp_next = newtp;
3586 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003588 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003589 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590
3591 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003592 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003593
3594#if defined(FEAT_GUI)
3595 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3596 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003597 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003598#endif
3599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003601#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003602 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003603 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003604#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003605 return OK;
3606 }
3607
3608 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003609 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003610 return FAIL;
3611}
3612
3613/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003614 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3615 * like with ":split".
3616 * Returns OK if a new tab page was created, FAIL otherwise.
3617 */
3618 int
3619may_open_tabpage()
3620{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003621 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003622
Bram Moolenaard326ce82007-03-11 14:48:29 +00003623 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003624 {
3625 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003626 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003627 return win_new_tabpage(n);
3628 }
3629 return FAIL;
3630}
3631
3632/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003633 * Create up to "maxcount" tabpages with empty windows.
3634 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636 int
3637make_tabpages(maxcount)
3638 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640 int count = maxcount;
3641 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003643 /* Limit to 'tabpagemax' tabs. */
3644 if (count > p_tpm)
3645 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003646
3647#ifdef FEAT_AUTOCMD
3648 /*
3649 * Don't execute autocommands while creating the tab pages. Must do that
3650 * when putting the buffers in the windows.
3651 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003652 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003653#endif
3654
3655 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003656 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003657 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003658
3659#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003660 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003661#endif
3662
3663 /* return actual number of tab pages */
3664 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665}
3666
3667/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003668 * Return TRUE when "tpc" points to a valid tab page.
3669 */
3670 int
3671valid_tabpage(tpc)
3672 tabpage_T *tpc;
3673{
3674 tabpage_T *tp;
3675
3676 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3677 if (tp == tpc)
3678 return TRUE;
3679 return FALSE;
3680}
3681
3682/*
3683 * Find tab page "n" (first one is 1). Returns NULL when not found.
3684 */
3685 tabpage_T *
3686find_tabpage(n)
3687 int n;
3688{
3689 tabpage_T *tp;
3690 int i = 1;
3691
3692 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3693 ++i;
3694 return tp;
3695}
3696
3697/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003698 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003699 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003700 */
3701 int
3702tabpage_index(ftp)
3703 tabpage_T *ftp;
3704{
3705 int i = 1;
3706 tabpage_T *tp;
3707
3708 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3709 ++i;
3710 return i;
3711}
3712
3713/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003714 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003715 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003716 * FAIL.
3717 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003718 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003720leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003721 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003722 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003723 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003725 tabpage_T *tp = curtab;
3726
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003727#ifdef FEAT_VISUAL
3728 reset_VIsual_and_resel(); /* stop Visual mode */
3729#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003730#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003731 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003732 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003733 if (new_curbuf != curbuf)
3734 {
3735 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3736 if (curtab != tp)
3737 return FAIL;
3738 }
3739 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3740 if (curtab != tp)
3741 return FAIL;
3742 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003743 if (curtab != tp)
3744 return FAIL;
3745 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003746#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003747#if defined(FEAT_GUI)
3748 /* Remove the scrollbars. They may be added back later. */
3749 if (gui.in_use)
3750 gui_remove_scrollbars();
3751#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003752 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003753 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003754 tp->tp_firstwin = firstwin;
3755 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003756 tp->tp_old_Rows = Rows;
3757 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003758 firstwin = NULL;
3759 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003760 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761}
3762
3763/*
3764 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003765 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003766 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3767 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003769 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003770enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003771 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003772 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003773 int trigger_enter_autocmds UNUSED;
3774 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003776 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003777 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003778
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003779 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003780 firstwin = tp->tp_firstwin;
3781 lastwin = tp->tp_lastwin;
3782 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003783
3784 /* We would like doing the TabEnter event first, but we don't have a
3785 * valid current window yet, which may break some commands.
3786 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003787 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3788 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003789 prevwin = next_prevwin;
3790
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003791 last_status(FALSE); /* status line may appear or disappear */
3792 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003793 must_redraw = CLEAR; /* need to redraw everything */
3794#ifdef FEAT_DIFF
3795 diff_need_scrollbind = TRUE;
3796#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003797
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003798 /* The tabpage line may have appeared or disappeared, may need to resize
3799 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003800 * frame sizes too. Use the stored value of p_ch, so that it can be
3801 * different for each tab page. */
3802 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003803 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3804#ifdef FEAT_GUI_TABLINE
3805 && !gui_use_tabline()
3806#endif
3807 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003808 shell_new_rows();
3809#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003810 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003811 shell_new_columns(); /* update window widths */
3812#endif
3813
3814#if defined(FEAT_GUI)
3815 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3816 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003817 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003818#endif
3819
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003820#ifdef FEAT_AUTOCMD
3821 /* Apply autocommands after updating the display, when 'rows' and
3822 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003823 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003824 {
3825 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3826 if (old_curbuf != curbuf)
3827 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3828 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003829#endif
3830
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831 redraw_all_later(CLEAR);
3832}
3833
3834/*
3835 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003836 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 */
3838 void
3839goto_tabpage(n)
3840 int n;
3841{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003842 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003843 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003844 int i;
3845
Bram Moolenaard68071d2006-05-02 22:08:30 +00003846 if (text_locked())
3847 {
3848 /* Not allowed when editing the command line. */
3849#ifdef FEAT_CMDWIN
3850 if (cmdwin_type != 0)
3851 EMSG(_(e_cmdwin));
3852 else
3853#endif
3854 EMSG(_(e_secure));
3855 return;
3856 }
3857
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003858 /* If there is only one it can't work. */
3859 if (first_tabpage->tp_next == NULL)
3860 {
3861 if (n > 1)
3862 beep_flush();
3863 return;
3864 }
3865
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003866 if (n == 0)
3867 {
3868 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003869 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003870 tp = first_tabpage;
3871 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003873 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003874 else if (n < 0)
3875 {
3876 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3877 * this N times. */
3878 ttp = curtab;
3879 for (i = n; i < 0; ++i)
3880 {
3881 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3882 tp = tp->tp_next)
3883 ;
3884 ttp = tp;
3885 }
3886 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003887 else if (n == 9999)
3888 {
3889 /* Go to last tab page. */
3890 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3891 ;
3892 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003893 else
3894 {
3895 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003896 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003897 if (tp == NULL)
3898 {
3899 beep_flush();
3900 return;
3901 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003902 }
3903
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003904 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003905
3906#ifdef FEAT_GUI_TABLINE
3907 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003908 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003909#endif
3910}
3911
3912/*
3913 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003914 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3915 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003916 * Note: doesn't update the GUI tab.
3917 */
3918 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003919goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003920 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003921 int trigger_enter_autocmds;
3922 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003923{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003924 /* Don't repeat a message in another tab page. */
3925 set_keep_msg(NULL, 0);
3926
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003927 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3928 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003929 {
3930 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003931 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3932 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003933 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003934 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3935 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003936 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003940 * Enter window "wp" in tab page "tp".
3941 * Also updates the GUI tab.
3942 */
3943 void
3944goto_tabpage_win(tp, wp)
3945 tabpage_T *tp;
3946 win_T *wp;
3947{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003948 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003949 if (curtab == tp && win_valid(wp))
3950 {
3951 win_enter(wp, TRUE);
3952# ifdef FEAT_GUI_TABLINE
3953 if (gui_use_tabline())
3954 gui_mch_set_curtab(tabpage_index(curtab));
3955# endif
3956 }
3957}
3958
3959/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003960 * Move the current tab page to before tab page "nr".
3961 */
3962 void
3963tabpage_move(nr)
3964 int nr;
3965{
3966 int n = nr;
3967 tabpage_T *tp;
3968
3969 if (first_tabpage->tp_next == NULL)
3970 return;
3971
3972 /* Remove the current tab page from the list of tab pages. */
3973 if (curtab == first_tabpage)
3974 first_tabpage = curtab->tp_next;
3975 else
3976 {
3977 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3978 if (tp->tp_next == curtab)
3979 break;
3980 if (tp == NULL) /* "cannot happen" */
3981 return;
3982 tp->tp_next = curtab->tp_next;
3983 }
3984
3985 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003986 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003987 {
3988 curtab->tp_next = first_tabpage;
3989 first_tabpage = curtab;
3990 }
3991 else
3992 {
3993 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3994 --n;
3995 curtab->tp_next = tp->tp_next;
3996 tp->tp_next = curtab;
3997 }
3998
3999 /* Need to redraw the tabline. Tab page contents doesn't change. */
4000 redraw_tabline = TRUE;
4001}
4002
4003
4004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 * Go to another window.
4006 * When jumping to another buffer, stop Visual mode. Do this before
4007 * changing windows so we can yank the selection into the '*' register.
4008 * When jumping to another window on the same buffer, adjust its cursor
4009 * position to keep the same Visual area.
4010 */
4011 void
4012win_goto(wp)
4013 win_T *wp;
4014{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004015#ifdef FEAT_CONCEAL
4016 win_T *owp = curwin;
4017#endif
4018
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004019 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 {
4021 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004022 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 return;
4024 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025#ifdef FEAT_AUTOCMD
4026 if (curbuf_locked())
4027 return;
4028#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#ifdef FEAT_VISUAL
4031 if (wp->w_buffer != curbuf)
4032 reset_VIsual_and_resel();
4033 else if (VIsual_active)
4034 wp->w_cursor = curwin->w_cursor;
4035#endif
4036
4037#ifdef FEAT_GUI
4038 need_mouse_correct = TRUE;
4039#endif
4040 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004041
4042#ifdef FEAT_CONCEAL
4043 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004044 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004045 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004046 if (curwin->w_p_cole > 0 && !msg_scrolled)
4047 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049}
4050
4051#if defined(FEAT_PERL) || defined(PROTO)
4052/*
4053 * Find window number "winnr" (counting top to bottom).
4054 */
4055 win_T *
4056win_find_nr(winnr)
4057 int winnr;
4058{
4059 win_T *wp;
4060
4061# ifdef FEAT_WINDOWS
4062 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4063 if (--winnr == 0)
4064 break;
4065 return wp;
4066# else
4067 return curwin;
4068# endif
4069}
4070#endif
4071
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004072#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4073 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004074/*
4075 * Find the tabpage for window "win".
4076 */
4077 tabpage_T *
4078win_find_tabpage(win)
4079 win_T *win;
4080{
4081 win_T *wp;
4082 tabpage_T *tp;
4083
4084 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004085 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4086 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004087 if (wp == win)
4088 return tp;
4089 return NULL;
4090}
4091#endif
4092
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#ifdef FEAT_VERTSPLIT
4094/*
4095 * Move to window above or below "count" times.
4096 */
4097 static void
4098win_goto_ver(up, count)
4099 int up; /* TRUE to go to win above */
4100 long count;
4101{
4102 frame_T *fr;
4103 frame_T *nfr;
4104 frame_T *foundfr;
4105
4106 foundfr = curwin->w_frame;
4107 while (count--)
4108 {
4109 /*
4110 * First go upwards in the tree of frames until we find a upwards or
4111 * downwards neighbor.
4112 */
4113 fr = foundfr;
4114 for (;;)
4115 {
4116 if (fr == topframe)
4117 goto end;
4118 if (up)
4119 nfr = fr->fr_prev;
4120 else
4121 nfr = fr->fr_next;
4122 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4123 break;
4124 fr = fr->fr_parent;
4125 }
4126
4127 /*
4128 * Now go downwards to find the bottom or top frame in it.
4129 */
4130 for (;;)
4131 {
4132 if (nfr->fr_layout == FR_LEAF)
4133 {
4134 foundfr = nfr;
4135 break;
4136 }
4137 fr = nfr->fr_child;
4138 if (nfr->fr_layout == FR_ROW)
4139 {
4140 /* Find the frame at the cursor row. */
4141 while (fr->fr_next != NULL
4142 && frame2win(fr)->w_wincol + fr->fr_width
4143 <= curwin->w_wincol + curwin->w_wcol)
4144 fr = fr->fr_next;
4145 }
4146 if (nfr->fr_layout == FR_COL && up)
4147 while (fr->fr_next != NULL)
4148 fr = fr->fr_next;
4149 nfr = fr;
4150 }
4151 }
4152end:
4153 if (foundfr != NULL)
4154 win_goto(foundfr->fr_win);
4155}
4156
4157/*
4158 * Move to left or right window.
4159 */
4160 static void
4161win_goto_hor(left, count)
4162 int left; /* TRUE to go to left win */
4163 long count;
4164{
4165 frame_T *fr;
4166 frame_T *nfr;
4167 frame_T *foundfr;
4168
4169 foundfr = curwin->w_frame;
4170 while (count--)
4171 {
4172 /*
4173 * First go upwards in the tree of frames until we find a left or
4174 * right neighbor.
4175 */
4176 fr = foundfr;
4177 for (;;)
4178 {
4179 if (fr == topframe)
4180 goto end;
4181 if (left)
4182 nfr = fr->fr_prev;
4183 else
4184 nfr = fr->fr_next;
4185 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4186 break;
4187 fr = fr->fr_parent;
4188 }
4189
4190 /*
4191 * Now go downwards to find the leftmost or rightmost frame in it.
4192 */
4193 for (;;)
4194 {
4195 if (nfr->fr_layout == FR_LEAF)
4196 {
4197 foundfr = nfr;
4198 break;
4199 }
4200 fr = nfr->fr_child;
4201 if (nfr->fr_layout == FR_COL)
4202 {
4203 /* Find the frame at the cursor row. */
4204 while (fr->fr_next != NULL
4205 && frame2win(fr)->w_winrow + fr->fr_height
4206 <= curwin->w_winrow + curwin->w_wrow)
4207 fr = fr->fr_next;
4208 }
4209 if (nfr->fr_layout == FR_ROW && left)
4210 while (fr->fr_next != NULL)
4211 fr = fr->fr_next;
4212 nfr = fr;
4213 }
4214 }
4215end:
4216 if (foundfr != NULL)
4217 win_goto(foundfr->fr_win);
4218}
4219#endif
4220
4221/*
4222 * Make window "wp" the current window.
4223 */
4224 void
4225win_enter(wp, undo_sync)
4226 win_T *wp;
4227 int undo_sync;
4228{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004229 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232/*
4233 * Make window wp the current window.
4234 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4235 * been closed and isn't valid.
4236 */
4237 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004238win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 win_T *wp;
4240 int undo_sync;
4241 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004242 int trigger_enter_autocmds UNUSED;
4243 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245#ifdef FEAT_AUTOCMD
4246 int other_buffer = FALSE;
4247#endif
4248
4249 if (wp == curwin && !curwin_invalid) /* nothing to do */
4250 return;
4251
4252#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004253 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 /*
4256 * Be careful: If autocommands delete the window, return now.
4257 */
4258 if (wp->w_buffer != curbuf)
4259 {
4260 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4261 other_buffer = TRUE;
4262 if (!win_valid(wp))
4263 return;
4264 }
4265 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4266 if (!win_valid(wp))
4267 return;
4268# ifdef FEAT_EVAL
4269 /* autocmds may abort script processing */
4270 if (aborting())
4271 return;
4272# endif
4273 }
4274#endif
4275
4276 /* sync undo before leaving the current buffer */
4277 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004278 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 /* may have to copy the buffer options when 'cpo' contains 'S' */
4280 if (wp->w_buffer != curbuf)
4281 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4282 if (!curwin_invalid)
4283 {
4284 prevwin = curwin; /* remember for CTRL-W p */
4285 curwin->w_redr_status = TRUE;
4286 }
4287 curwin = wp;
4288 curbuf = wp->w_buffer;
4289 check_cursor();
4290#ifdef FEAT_VIRTUALEDIT
4291 if (!virtual_active())
4292 curwin->w_cursor.coladd = 0;
4293#endif
4294 changed_line_abv_curs(); /* assume cursor position needs updating */
4295
4296 if (curwin->w_localdir != NULL)
4297 {
4298 /* Window has a local directory: Save current directory as global
4299 * directory (unless that was done already) and change to the local
4300 * directory. */
4301 if (globaldir == NULL)
4302 {
4303 char_u cwd[MAXPATHL];
4304
4305 if (mch_dirname(cwd, MAXPATHL) == OK)
4306 globaldir = vim_strsave(cwd);
4307 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004308 if (mch_chdir((char *)curwin->w_localdir) == 0)
4309 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else if (globaldir != NULL)
4312 {
4313 /* Window doesn't have a local directory and we are not in the global
4314 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004315 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 vim_free(globaldir);
4317 globaldir = NULL;
4318 shorten_fnames(TRUE);
4319 }
4320
4321#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004322 if (trigger_enter_autocmds)
4323 {
4324 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4325 if (other_buffer)
4326 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328#endif
4329
4330#ifdef FEAT_TITLE
4331 maketitle();
4332#endif
4333 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004334 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (restart_edit)
4336 redraw_later(VALID); /* causes status line redraw */
4337
4338 /* set window height to desired minimal value */
4339 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4340 win_setheight((int)p_wh);
4341 else if (curwin->w_height == 0)
4342 win_setheight(1);
4343
4344#ifdef FEAT_VERTSPLIT
4345 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004346 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 win_setwidth((int)p_wiw);
4348#endif
4349
4350#ifdef FEAT_MOUSE
4351 setmouse(); /* in case jumped to/from help buffer */
4352#endif
4353
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004354 /* Change directories when the 'acd' option is set. */
4355 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357
4358#endif /* FEAT_WINDOWS */
4359
4360#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4361/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004362 * Jump to the first open window that contains buffer "buf", if one exists.
4363 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 */
4365 win_T *
4366buf_jump_open_win(buf)
4367 buf_T *buf;
4368{
4369# ifdef FEAT_WINDOWS
4370 win_T *wp;
4371
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (wp->w_buffer == buf)
4374 break;
4375 if (wp != NULL)
4376 win_enter(wp, FALSE);
4377 return wp;
4378# else
4379 if (curwin->w_buffer == buf)
4380 return curwin;
4381 return NULL;
4382# endif
4383}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004384
4385/*
4386 * Jump to the first open window in any tab page that contains buffer "buf",
4387 * if one exists.
4388 * Returns a pointer to the window found, otherwise NULL.
4389 */
4390 win_T *
4391buf_jump_open_tab(buf)
4392 buf_T *buf;
4393{
4394# ifdef FEAT_WINDOWS
4395 win_T *wp;
4396 tabpage_T *tp;
4397
4398 /* First try the current tab page. */
4399 wp = buf_jump_open_win(buf);
4400 if (wp != NULL)
4401 return wp;
4402
4403 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4404 if (tp != curtab)
4405 {
4406 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4407 if (wp->w_buffer == buf)
4408 break;
4409 if (wp != NULL)
4410 {
4411 goto_tabpage_win(tp, wp);
4412 if (curwin != wp)
4413 wp = NULL; /* something went wrong */
4414 break;
4415 }
4416 }
4417
4418 return wp;
4419# else
4420 if (curwin->w_buffer == buf)
4421 return curwin;
4422 return NULL;
4423# endif
4424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425#endif
4426
4427/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004428 * Allocate a window structure and link it in the window list when "hidden" is
4429 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004432win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004433 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004434 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004436 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 /*
4439 * allocate window structure and linesizes arrays
4440 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004441 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004442 if (new_wp == NULL)
4443 return NULL;
4444
4445 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004447 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004448 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450
Bram Moolenaar429fa852013-04-15 12:27:36 +02004451#ifdef FEAT_EVAL
4452 /* init w: variables */
4453 new_wp->w_vars = dict_alloc();
4454 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004456 win_free_lsize(new_wp);
4457 vim_free(new_wp);
4458 return NULL;
4459 }
4460 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004461#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004462
4463#ifdef FEAT_AUTOCMD
4464 /* Don't execute autocommands while the window is not properly
4465 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4466 * event. */
4467 block_autocmds();
4468#endif
4469 /*
4470 * link the window in the window list
4471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004473 if (!hidden)
4474 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475#endif
4476#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004477 new_wp->w_wincol = 0;
4478 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#endif
4480
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 /* position the display and the cursor at the top of the file. */
4482 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004484 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 new_wp->w_botline = 2;
4487 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490#endif
4491
Bram Moolenaar429fa852013-04-15 12:27:36 +02004492 /* We won't calculate w_fraction until resizing the window */
4493 new_wp->w_fraction = 0;
4494 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004497 if (gui.in_use)
4498 {
4499 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4500 SBAR_LEFT, new_wp);
4501 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4502 SBAR_RIGHT, new_wp);
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#endif
4505#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004508#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004509 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004510#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004511#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004512 new_wp->w_match_head = NULL;
4513 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004514#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004515 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516}
4517
4518#if defined(FEAT_WINDOWS) || defined(PROTO)
4519
4520/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004521 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004524win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004526 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004529 buf_T *buf;
4530 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004532#ifdef FEAT_FOLDING
4533 clearFolding(wp);
4534#endif
4535
4536 /* reduce the reference count to the argument list. */
4537 alist_unlink(wp->w_alist);
4538
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004539#ifdef FEAT_AUTOCMD
4540 /* Don't execute autocommands while the window is halfway being deleted.
4541 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004542 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004543#endif
4544
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004545#ifdef FEAT_LUA
4546 lua_window_free(wp);
4547#endif
4548
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004549#ifdef FEAT_MZSCHEME
4550 mzscheme_window_free(wp);
4551#endif
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#ifdef FEAT_PERL
4554 perl_win_free(wp);
4555#endif
4556
4557#ifdef FEAT_PYTHON
4558 python_window_free(wp);
4559#endif
4560
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004561#ifdef FEAT_PYTHON3
4562 python3_window_free(wp);
4563#endif
4564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#ifdef FEAT_TCL
4566 tcl_window_free(wp);
4567#endif
4568
4569#ifdef FEAT_RUBY
4570 ruby_window_free(wp);
4571#endif
4572
4573 clear_winopt(&wp->w_onebuf_opt);
4574 clear_winopt(&wp->w_allbuf_opt);
4575
4576#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004577 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4578 hash_init(&wp->w_vars->dv_hashtab);
4579 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
4581
4582 if (prevwin == wp)
4583 prevwin = NULL;
4584 win_free_lsize(wp);
4585
4586 for (i = 0; i < wp->w_tagstacklen; ++i)
4587 vim_free(wp->w_tagstack[i].tagname);
4588
4589 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004590
Bram Moolenaarff18df02013-07-24 17:51:57 +02004591 /* Remove the window from the b_wininfo lists, it may happen that the
4592 * freed memory is re-used for another window. */
4593 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4594 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4595 if (wip->wi_win == wp)
4596 wip->wi_win = NULL;
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004599 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_JUMPLIST
4603 free_jumplist(wp);
4604#endif
4605
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004606#ifdef FEAT_QUICKFIX
4607 qf_free_all(wp);
4608#endif
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_GUI
4611 if (gui.in_use)
4612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4614 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4615 }
4616#endif /* FEAT_GUI */
4617
Bram Moolenaar860cae12010-06-05 23:22:07 +02004618#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004619 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004620#endif
4621
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004622#ifdef FEAT_AUTOCMD
4623 if (wp != aucmd_win)
4624#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004625 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004627
4628#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004629 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631}
4632
4633/*
4634 * Append window "wp" in the window list after window "after".
4635 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004636 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637win_append(after, wp)
4638 win_T *after, *wp;
4639{
4640 win_T *before;
4641
4642 if (after == NULL) /* after NULL is in front of the first */
4643 before = firstwin;
4644 else
4645 before = after->w_next;
4646
4647 wp->w_next = before;
4648 wp->w_prev = after;
4649 if (after == NULL)
4650 firstwin = wp;
4651 else
4652 after->w_next = wp;
4653 if (before == NULL)
4654 lastwin = wp;
4655 else
4656 before->w_prev = wp;
4657}
4658
4659/*
4660 * Remove a window from the window list.
4661 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004662 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004663win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004665 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 if (wp->w_prev != NULL)
4668 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004669 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004671 else
4672 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 if (wp->w_next != NULL)
4674 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004675 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004677 else
4678 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
4681/*
4682 * Append frame "frp" in a frame list after frame "after".
4683 */
4684 static void
4685frame_append(after, frp)
4686 frame_T *after, *frp;
4687{
4688 frp->fr_next = after->fr_next;
4689 after->fr_next = frp;
4690 if (frp->fr_next != NULL)
4691 frp->fr_next->fr_prev = frp;
4692 frp->fr_prev = after;
4693}
4694
4695/*
4696 * Insert frame "frp" in a frame list before frame "before".
4697 */
4698 static void
4699frame_insert(before, frp)
4700 frame_T *before, *frp;
4701{
4702 frp->fr_next = before;
4703 frp->fr_prev = before->fr_prev;
4704 before->fr_prev = frp;
4705 if (frp->fr_prev != NULL)
4706 frp->fr_prev->fr_next = frp;
4707 else
4708 frp->fr_parent->fr_child = frp;
4709}
4710
4711/*
4712 * Remove a frame from a frame list.
4713 */
4714 static void
4715frame_remove(frp)
4716 frame_T *frp;
4717{
4718 if (frp->fr_prev != NULL)
4719 frp->fr_prev->fr_next = frp->fr_next;
4720 else
4721 frp->fr_parent->fr_child = frp->fr_next;
4722 if (frp->fr_next != NULL)
4723 frp->fr_next->fr_prev = frp->fr_prev;
4724}
4725
4726#endif /* FEAT_WINDOWS */
4727
4728/*
4729 * Allocate w_lines[] for window "wp".
4730 * Return FAIL for failure, OK for success.
4731 */
4732 int
4733win_alloc_lines(wp)
4734 win_T *wp;
4735{
4736 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004737 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (wp->w_lines == NULL)
4739 return FAIL;
4740 return OK;
4741}
4742
4743/*
4744 * free lsize arrays for a window
4745 */
4746 void
4747win_free_lsize(wp)
4748 win_T *wp;
4749{
4750 vim_free(wp->w_lines);
4751 wp->w_lines = NULL;
4752}
4753
4754/*
4755 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004756 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 */
4758 void
4759shell_new_rows()
4760{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004761 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 if (firstwin == NULL) /* not initialized yet */
4764 return;
4765#ifdef FEAT_WINDOWS
4766 if (h < frame_minheight(topframe, NULL))
4767 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004768
4769 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 * that doesn't result in the right height, forget about that option. */
4771 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004772 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 frame_new_height(topframe, h, FALSE, FALSE);
4774
4775 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4776#else
4777 if (h < 1)
4778 h = 1;
4779 win_new_height(firstwin, h);
4780#endif
4781 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004782#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004783 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004784#endif
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#if 0
4787 /* Disabled: don't want making the screen smaller make a window larger. */
4788 if (p_ea)
4789 win_equal(curwin, FALSE, 'v');
4790#endif
4791}
4792
4793#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4794/*
4795 * Called from win_new_shellsize() after Columns changed.
4796 */
4797 void
4798shell_new_columns()
4799{
4800 if (firstwin == NULL) /* not initialized yet */
4801 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004802
4803 /* First try setting the widths of windows with 'winfixwidth'. If that
4804 * doesn't result in the right width, forget about that option. */
4805 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004806 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004807 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4810#if 0
4811 /* Disabled: don't want making the screen smaller make a window larger. */
4812 if (p_ea)
4813 win_equal(curwin, FALSE, 'h');
4814#endif
4815}
4816#endif
4817
4818#if defined(FEAT_CMDWIN) || defined(PROTO)
4819/*
4820 * Save the size of all windows in "gap".
4821 */
4822 void
4823win_size_save(gap)
4824 garray_T *gap;
4825
4826{
4827 win_T *wp;
4828
4829 ga_init2(gap, (int)sizeof(int), 1);
4830 if (ga_grow(gap, win_count() * 2) == OK)
4831 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4832 {
4833 ((int *)gap->ga_data)[gap->ga_len++] =
4834 wp->w_width + wp->w_vsep_width;
4835 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4836 }
4837}
4838
4839/*
4840 * Restore window sizes, but only if the number of windows is still the same.
4841 * Does not free the growarray.
4842 */
4843 void
4844win_size_restore(gap)
4845 garray_T *gap;
4846{
4847 win_T *wp;
4848 int i;
4849
4850 if (win_count() * 2 == gap->ga_len)
4851 {
4852 i = 0;
4853 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4854 {
4855 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4856 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4857 }
4858 /* recompute the window positions */
4859 (void)win_comp_pos();
4860 }
4861}
4862#endif /* FEAT_CMDWIN */
4863
4864#if defined(FEAT_WINDOWS) || defined(PROTO)
4865/*
4866 * Update the position for all windows, using the width and height of the
4867 * frames.
4868 * Returns the row just after the last window.
4869 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004870 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871win_comp_pos()
4872{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004873 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int col = 0;
4875
4876 frame_comp_pos(topframe, &row, &col);
4877 return row;
4878}
4879
4880/*
4881 * Update the position of the windows in frame "topfrp", using the width and
4882 * height of the frames.
4883 * "*row" and "*col" are the top-left position of the frame. They are updated
4884 * to the bottom-right position plus one.
4885 */
4886 static void
4887frame_comp_pos(topfrp, row, col)
4888 frame_T *topfrp;
4889 int *row;
4890 int *col;
4891{
4892 win_T *wp;
4893 frame_T *frp;
4894#ifdef FEAT_VERTSPLIT
4895 int startcol;
4896 int startrow;
4897#endif
4898
4899 wp = topfrp->fr_win;
4900 if (wp != NULL)
4901 {
4902 if (wp->w_winrow != *row
4903#ifdef FEAT_VERTSPLIT
4904 || wp->w_wincol != *col
4905#endif
4906 )
4907 {
4908 /* position changed, redraw */
4909 wp->w_winrow = *row;
4910#ifdef FEAT_VERTSPLIT
4911 wp->w_wincol = *col;
4912#endif
4913 redraw_win_later(wp, NOT_VALID);
4914 wp->w_redr_status = TRUE;
4915 }
4916 *row += wp->w_height + wp->w_status_height;
4917#ifdef FEAT_VERTSPLIT
4918 *col += wp->w_width + wp->w_vsep_width;
4919#endif
4920 }
4921 else
4922 {
4923#ifdef FEAT_VERTSPLIT
4924 startrow = *row;
4925 startcol = *col;
4926#endif
4927 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4928 {
4929#ifdef FEAT_VERTSPLIT
4930 if (topfrp->fr_layout == FR_ROW)
4931 *row = startrow; /* all frames are at the same row */
4932 else
4933 *col = startcol; /* all frames are at the same col */
4934#endif
4935 frame_comp_pos(frp, row, col);
4936 }
4937 }
4938}
4939
4940#endif /* FEAT_WINDOWS */
4941
4942/*
4943 * Set current window height and take care of repositioning other windows to
4944 * fit around it.
4945 */
4946 void
4947win_setheight(height)
4948 int height;
4949{
4950 win_setheight_win(height, curwin);
4951}
4952
4953/*
4954 * Set the window height of window "win" and take care of repositioning other
4955 * windows to fit around it.
4956 */
4957 void
4958win_setheight_win(height, win)
4959 int height;
4960 win_T *win;
4961{
4962 int row;
4963
4964 if (win == curwin)
4965 {
4966 /* Always keep current window at least one line high, even when
4967 * 'winminheight' is zero. */
4968#ifdef FEAT_WINDOWS
4969 if (height < p_wmh)
4970 height = p_wmh;
4971#endif
4972 if (height == 0)
4973 height = 1;
4974 }
4975
4976#ifdef FEAT_WINDOWS
4977 frame_setheight(win->w_frame, height + win->w_status_height);
4978
4979 /* recompute the window positions */
4980 row = win_comp_pos();
4981#else
4982 if (height > topframe->fr_height)
4983 height = topframe->fr_height;
4984 win->w_height = height;
4985 row = height;
4986#endif
4987
4988 /*
4989 * If there is extra space created between the last window and the command
4990 * line, clear it.
4991 */
4992 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4993 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4994 cmdline_row = row;
4995 msg_row = row;
4996 msg_col = 0;
4997
4998 redraw_all_later(NOT_VALID);
4999}
5000
5001#if defined(FEAT_WINDOWS) || defined(PROTO)
5002
5003/*
5004 * Set the height of a frame to "height" and take care that all frames and
5005 * windows inside it are resized. Also resize frames on the left and right if
5006 * the are in the same FR_ROW frame.
5007 *
5008 * Strategy:
5009 * If the frame is part of a FR_COL frame, try fitting the frame in that
5010 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5011 * go to containing frames to resize them and make room.
5012 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5013 * Check for the minimal height of the FR_ROW frame.
5014 * At the top level we can also use change the command line height.
5015 */
5016 static void
5017frame_setheight(curfrp, height)
5018 frame_T *curfrp;
5019 int height;
5020{
5021 int room; /* total number of lines available */
5022 int take; /* number of lines taken from other windows */
5023 int room_cmdline; /* lines available from cmdline */
5024 int run;
5025 frame_T *frp;
5026 int h;
5027 int room_reserved;
5028
5029 /* If the height already is the desired value, nothing to do. */
5030 if (curfrp->fr_height == height)
5031 return;
5032
5033 if (curfrp->fr_parent == NULL)
5034 {
5035 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005036 if (height > ROWS_AVAIL)
5037 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (height > 0)
5039 frame_new_height(curfrp, height, FALSE, FALSE);
5040 }
5041 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5042 {
5043 /* Row of frames: Also need to resize frames left and right of this
5044 * one. First check for the minimal height of these. */
5045 h = frame_minheight(curfrp->fr_parent, NULL);
5046 if (height < h)
5047 height = h;
5048 frame_setheight(curfrp->fr_parent, height);
5049 }
5050 else
5051 {
5052 /*
5053 * Column of frames: try to change only frames in this column.
5054 */
5055#ifdef FEAT_VERTSPLIT
5056 /*
5057 * Do this twice:
5058 * 1: compute room available, if it's not enough try resizing the
5059 * containing frame.
5060 * 2: compute the room available and adjust the height to it.
5061 * Try not to reduce the height of a window with 'winfixheight' set.
5062 */
5063 for (run = 1; run <= 2; ++run)
5064#else
5065 for (;;)
5066#endif
5067 {
5068 room = 0;
5069 room_reserved = 0;
5070 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5071 frp = frp->fr_next)
5072 {
5073 if (frp != curfrp
5074 && frp->fr_win != NULL
5075 && frp->fr_win->w_p_wfh)
5076 room_reserved += frp->fr_height;
5077 room += frp->fr_height;
5078 if (frp != curfrp)
5079 room -= frame_minheight(frp, NULL);
5080 }
5081#ifdef FEAT_VERTSPLIT
5082 if (curfrp->fr_width != Columns)
5083 room_cmdline = 0;
5084 else
5085#endif
5086 {
5087 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5088 + lastwin->w_height + lastwin->w_status_height);
5089 if (room_cmdline < 0)
5090 room_cmdline = 0;
5091 }
5092
5093 if (height <= room + room_cmdline)
5094 break;
5095#ifdef FEAT_VERTSPLIT
5096 if (run == 2 || curfrp->fr_width == Columns)
5097#endif
5098 {
5099 if (height > room + room_cmdline)
5100 height = room + room_cmdline;
5101 break;
5102 }
5103#ifdef FEAT_VERTSPLIT
5104 frame_setheight(curfrp->fr_parent, height
5105 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5106#endif
5107 /*NOTREACHED*/
5108 }
5109
5110 /*
5111 * Compute the number of lines we will take from others frames (can be
5112 * negative!).
5113 */
5114 take = height - curfrp->fr_height;
5115
5116 /* If there is not enough room, also reduce the height of a window
5117 * with 'winfixheight' set. */
5118 if (height > room + room_cmdline - room_reserved)
5119 room_reserved = room + room_cmdline - height;
5120 /* If there is only a 'winfixheight' window and making the
5121 * window smaller, need to make the other window taller. */
5122 if (take < 0 && room - curfrp->fr_height < room_reserved)
5123 room_reserved = 0;
5124
5125 if (take > 0 && room_cmdline > 0)
5126 {
5127 /* use lines from cmdline first */
5128 if (take < room_cmdline)
5129 room_cmdline = take;
5130 take -= room_cmdline;
5131 topframe->fr_height += room_cmdline;
5132 }
5133
5134 /*
5135 * set the current frame to the new height
5136 */
5137 frame_new_height(curfrp, height, FALSE, FALSE);
5138
5139 /*
5140 * First take lines from the frames after the current frame. If
5141 * that is not enough, takes lines from frames above the current
5142 * frame.
5143 */
5144 for (run = 0; run < 2; ++run)
5145 {
5146 if (run == 0)
5147 frp = curfrp->fr_next; /* 1st run: start with next window */
5148 else
5149 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5150 while (frp != NULL && take != 0)
5151 {
5152 h = frame_minheight(frp, NULL);
5153 if (room_reserved > 0
5154 && frp->fr_win != NULL
5155 && frp->fr_win->w_p_wfh)
5156 {
5157 if (room_reserved >= frp->fr_height)
5158 room_reserved -= frp->fr_height;
5159 else
5160 {
5161 if (frp->fr_height - room_reserved > take)
5162 room_reserved = frp->fr_height - take;
5163 take -= frp->fr_height - room_reserved;
5164 frame_new_height(frp, room_reserved, FALSE, FALSE);
5165 room_reserved = 0;
5166 }
5167 }
5168 else
5169 {
5170 if (frp->fr_height - take < h)
5171 {
5172 take -= frp->fr_height - h;
5173 frame_new_height(frp, h, FALSE, FALSE);
5174 }
5175 else
5176 {
5177 frame_new_height(frp, frp->fr_height - take,
5178 FALSE, FALSE);
5179 take = 0;
5180 }
5181 }
5182 if (run == 0)
5183 frp = frp->fr_next;
5184 else
5185 frp = frp->fr_prev;
5186 }
5187 }
5188 }
5189}
5190
5191#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5192/*
5193 * Set current window width and take care of repositioning other windows to
5194 * fit around it.
5195 */
5196 void
5197win_setwidth(width)
5198 int width;
5199{
5200 win_setwidth_win(width, curwin);
5201}
5202
5203 void
5204win_setwidth_win(width, wp)
5205 int width;
5206 win_T *wp;
5207{
5208 /* Always keep current window at least one column wide, even when
5209 * 'winminwidth' is zero. */
5210 if (wp == curwin)
5211 {
5212 if (width < p_wmw)
5213 width = p_wmw;
5214 if (width == 0)
5215 width = 1;
5216 }
5217
5218 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5219
5220 /* recompute the window positions */
5221 (void)win_comp_pos();
5222
5223 redraw_all_later(NOT_VALID);
5224}
5225
5226/*
5227 * Set the width of a frame to "width" and take care that all frames and
5228 * windows inside it are resized. Also resize frames above and below if the
5229 * are in the same FR_ROW frame.
5230 *
5231 * Strategy is similar to frame_setheight().
5232 */
5233 static void
5234frame_setwidth(curfrp, width)
5235 frame_T *curfrp;
5236 int width;
5237{
5238 int room; /* total number of lines available */
5239 int take; /* number of lines taken from other windows */
5240 int run;
5241 frame_T *frp;
5242 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005243 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 /* If the width already is the desired value, nothing to do. */
5246 if (curfrp->fr_width == width)
5247 return;
5248
5249 if (curfrp->fr_parent == NULL)
5250 /* topframe: can't change width */
5251 return;
5252
5253 if (curfrp->fr_parent->fr_layout == FR_COL)
5254 {
5255 /* Column of frames: Also need to resize frames above and below of
5256 * this one. First check for the minimal width of these. */
5257 w = frame_minwidth(curfrp->fr_parent, NULL);
5258 if (width < w)
5259 width = w;
5260 frame_setwidth(curfrp->fr_parent, width);
5261 }
5262 else
5263 {
5264 /*
5265 * Row of frames: try to change only frames in this row.
5266 *
5267 * Do this twice:
5268 * 1: compute room available, if it's not enough try resizing the
5269 * containing frame.
5270 * 2: compute the room available and adjust the width to it.
5271 */
5272 for (run = 1; run <= 2; ++run)
5273 {
5274 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005275 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5277 frp = frp->fr_next)
5278 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005279 if (frp != curfrp
5280 && frp->fr_win != NULL
5281 && frp->fr_win->w_p_wfw)
5282 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 room += frp->fr_width;
5284 if (frp != curfrp)
5285 room -= frame_minwidth(frp, NULL);
5286 }
5287
5288 if (width <= room)
5289 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005290 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 if (width > room)
5293 width = room;
5294 break;
5295 }
5296 frame_setwidth(curfrp->fr_parent, width
5297 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5298 }
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 /*
5301 * Compute the number of lines we will take from others frames (can be
5302 * negative!).
5303 */
5304 take = width - curfrp->fr_width;
5305
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005306 /* If there is not enough room, also reduce the width of a window
5307 * with 'winfixwidth' set. */
5308 if (width > room - room_reserved)
5309 room_reserved = room - width;
5310 /* If there is only a 'winfixwidth' window and making the
5311 * window smaller, need to make the other window narrower. */
5312 if (take < 0 && room - curfrp->fr_width < room_reserved)
5313 room_reserved = 0;
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 /*
5316 * set the current frame to the new width
5317 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005318 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
5321 * First take lines from the frames right of the current frame. If
5322 * that is not enough, takes lines from frames left of the current
5323 * frame.
5324 */
5325 for (run = 0; run < 2; ++run)
5326 {
5327 if (run == 0)
5328 frp = curfrp->fr_next; /* 1st run: start with next window */
5329 else
5330 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5331 while (frp != NULL && take != 0)
5332 {
5333 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005334 if (room_reserved > 0
5335 && frp->fr_win != NULL
5336 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005338 if (room_reserved >= frp->fr_width)
5339 room_reserved -= frp->fr_width;
5340 else
5341 {
5342 if (frp->fr_width - room_reserved > take)
5343 room_reserved = frp->fr_width - take;
5344 take -= frp->fr_width - room_reserved;
5345 frame_new_width(frp, room_reserved, FALSE, FALSE);
5346 room_reserved = 0;
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 else
5350 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005351 if (frp->fr_width - take < w)
5352 {
5353 take -= frp->fr_width - w;
5354 frame_new_width(frp, w, FALSE, FALSE);
5355 }
5356 else
5357 {
5358 frame_new_width(frp, frp->fr_width - take,
5359 FALSE, FALSE);
5360 take = 0;
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 if (run == 0)
5364 frp = frp->fr_next;
5365 else
5366 frp = frp->fr_prev;
5367 }
5368 }
5369 }
5370}
5371#endif /* FEAT_VERTSPLIT */
5372
5373/*
5374 * Check 'winminheight' for a valid value.
5375 */
5376 void
5377win_setminheight()
5378{
5379 int room;
5380 int first = TRUE;
5381 win_T *wp;
5382
5383 /* loop until there is a 'winminheight' that is possible */
5384 while (p_wmh > 0)
5385 {
5386 /* TODO: handle vertical splits */
5387 room = -p_wh;
5388 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5389 room += wp->w_height - p_wmh;
5390 if (room >= 0)
5391 break;
5392 --p_wmh;
5393 if (first)
5394 {
5395 EMSG(_(e_noroom));
5396 first = FALSE;
5397 }
5398 }
5399}
5400
5401#ifdef FEAT_MOUSE
5402
5403/*
5404 * Status line of dragwin is dragged "offset" lines down (negative is up).
5405 */
5406 void
5407win_drag_status_line(dragwin, offset)
5408 win_T *dragwin;
5409 int offset;
5410{
5411 frame_T *curfr;
5412 frame_T *fr;
5413 int room;
5414 int row;
5415 int up; /* if TRUE, drag status line up, otherwise down */
5416 int n;
5417
5418 fr = dragwin->w_frame;
5419 curfr = fr;
5420 if (fr != topframe) /* more than one window */
5421 {
5422 fr = fr->fr_parent;
5423 /* When the parent frame is not a column of frames, its parent should
5424 * be. */
5425 if (fr->fr_layout != FR_COL)
5426 {
5427 curfr = fr;
5428 if (fr != topframe) /* only a row of windows, may drag statusline */
5429 fr = fr->fr_parent;
5430 }
5431 }
5432
5433 /* If this is the last frame in a column, may want to resize the parent
5434 * frame instead (go two up to skip a row of frames). */
5435 while (curfr != topframe && curfr->fr_next == NULL)
5436 {
5437 if (fr != topframe)
5438 fr = fr->fr_parent;
5439 curfr = fr;
5440 if (fr != topframe)
5441 fr = fr->fr_parent;
5442 }
5443
5444 if (offset < 0) /* drag up */
5445 {
5446 up = TRUE;
5447 offset = -offset;
5448 /* sum up the room of the current frame and above it */
5449 if (fr == curfr)
5450 {
5451 /* only one window */
5452 room = fr->fr_height - frame_minheight(fr, NULL);
5453 }
5454 else
5455 {
5456 room = 0;
5457 for (fr = fr->fr_child; ; fr = fr->fr_next)
5458 {
5459 room += fr->fr_height - frame_minheight(fr, NULL);
5460 if (fr == curfr)
5461 break;
5462 }
5463 }
5464 fr = curfr->fr_next; /* put fr at frame that grows */
5465 }
5466 else /* drag down */
5467 {
5468 up = FALSE;
5469 /*
5470 * Only dragging the last status line can reduce p_ch.
5471 */
5472 room = Rows - cmdline_row;
5473 if (curfr->fr_next == NULL)
5474 room -= 1;
5475 else
5476 room -= p_ch;
5477 if (room < 0)
5478 room = 0;
5479 /* sum up the room of frames below of the current one */
5480 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5481 room += fr->fr_height - frame_minheight(fr, NULL);
5482 fr = curfr; /* put fr at window that grows */
5483 }
5484
5485 if (room < offset) /* Not enough room */
5486 offset = room; /* Move as far as we can */
5487 if (offset <= 0)
5488 return;
5489
5490 /*
5491 * Grow frame fr by "offset" lines.
5492 * Doesn't happen when dragging the last status line up.
5493 */
5494 if (fr != NULL)
5495 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5496
5497 if (up)
5498 fr = curfr; /* current frame gets smaller */
5499 else
5500 fr = curfr->fr_next; /* next frame gets smaller */
5501
5502 /*
5503 * Now make the other frames smaller.
5504 */
5505 while (fr != NULL && offset > 0)
5506 {
5507 n = frame_minheight(fr, NULL);
5508 if (fr->fr_height - offset <= n)
5509 {
5510 offset -= fr->fr_height - n;
5511 frame_new_height(fr, n, !up, FALSE);
5512 }
5513 else
5514 {
5515 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5516 break;
5517 }
5518 if (up)
5519 fr = fr->fr_prev;
5520 else
5521 fr = fr->fr_next;
5522 }
5523 row = win_comp_pos();
5524 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5525 cmdline_row = row;
5526 p_ch = Rows - cmdline_row;
5527 if (p_ch < 1)
5528 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005529 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005530 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 showmode();
5532}
5533
5534#ifdef FEAT_VERTSPLIT
5535/*
5536 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5537 */
5538 void
5539win_drag_vsep_line(dragwin, offset)
5540 win_T *dragwin;
5541 int offset;
5542{
5543 frame_T *curfr;
5544 frame_T *fr;
5545 int room;
5546 int left; /* if TRUE, drag separator line left, otherwise right */
5547 int n;
5548
5549 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005550 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return;
5552 curfr = fr;
5553 fr = fr->fr_parent;
5554 /* When the parent frame is not a row of frames, its parent should be. */
5555 if (fr->fr_layout != FR_ROW)
5556 {
5557 if (fr == topframe) /* only a column of windows (cannot happen?) */
5558 return;
5559 curfr = fr;
5560 fr = fr->fr_parent;
5561 }
5562
5563 /* If this is the last frame in a row, may want to resize a parent
5564 * frame instead. */
5565 while (curfr->fr_next == NULL)
5566 {
5567 if (fr == topframe)
5568 break;
5569 curfr = fr;
5570 fr = fr->fr_parent;
5571 if (fr != topframe)
5572 {
5573 curfr = fr;
5574 fr = fr->fr_parent;
5575 }
5576 }
5577
5578 if (offset < 0) /* drag left */
5579 {
5580 left = TRUE;
5581 offset = -offset;
5582 /* sum up the room of the current frame and left of it */
5583 room = 0;
5584 for (fr = fr->fr_child; ; fr = fr->fr_next)
5585 {
5586 room += fr->fr_width - frame_minwidth(fr, NULL);
5587 if (fr == curfr)
5588 break;
5589 }
5590 fr = curfr->fr_next; /* put fr at frame that grows */
5591 }
5592 else /* drag right */
5593 {
5594 left = FALSE;
5595 /* sum up the room of frames right of the current one */
5596 room = 0;
5597 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5598 room += fr->fr_width - frame_minwidth(fr, NULL);
5599 fr = curfr; /* put fr at window that grows */
5600 }
5601
5602 if (room < offset) /* Not enough room */
5603 offset = room; /* Move as far as we can */
5604 if (offset <= 0) /* No room at all, quit. */
5605 return;
5606
5607 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005608 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610 /* shrink other frames: current and at the left or at the right */
5611 if (left)
5612 fr = curfr; /* current frame gets smaller */
5613 else
5614 fr = curfr->fr_next; /* next frame gets smaller */
5615
5616 while (fr != NULL && offset > 0)
5617 {
5618 n = frame_minwidth(fr, NULL);
5619 if (fr->fr_width - offset <= n)
5620 {
5621 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005622 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624 else
5625 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005626 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 break;
5628 }
5629 if (left)
5630 fr = fr->fr_prev;
5631 else
5632 fr = fr->fr_next;
5633 }
5634 (void)win_comp_pos();
5635 redraw_all_later(NOT_VALID);
5636}
5637#endif /* FEAT_VERTSPLIT */
5638#endif /* FEAT_MOUSE */
5639
5640#endif /* FEAT_WINDOWS */
5641
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005642#define FRACTION_MULT 16384L
5643
5644/*
5645 * Set wp->w_fraction for the current w_wrow and w_height.
5646 */
5647 static void
5648set_fraction(wp)
5649 win_T *wp;
5650{
5651 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5652 + FRACTION_MULT / 2) / (long)wp->w_height;
5653}
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655/*
5656 * Set the height of a window.
5657 * This takes care of the things inside the window, not what happens to the
5658 * window position, the frame or to other windows.
5659 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005660 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661win_new_height(wp, height)
5662 win_T *wp;
5663 int height;
5664{
5665 linenr_T lnum;
5666 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 /* Don't want a negative height. Happens when splitting a tiny window.
5669 * Will equalize heights soon to fix it. */
5670 if (height < 0)
5671 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005672 if (wp->w_height == height)
5673 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005676 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 wp->w_height = height;
5679 wp->w_skipcol = 0;
5680
5681 /* Don't change w_topline when height is zero. Don't set w_topline when
5682 * 'scrollbind' is set and this isn't the current window. */
5683 if (height > 0
5684#ifdef FEAT_SCROLLBIND
5685 && (!wp->w_p_scb || wp == curwin)
5686#endif
5687 )
5688 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005689 /*
5690 * Find a value for w_topline that shows the cursor at the same
5691 * relative position in the window as before (more or less).
5692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 lnum = wp->w_cursor.lnum;
5694 if (lnum < 1) /* can happen when starting up */
5695 lnum = 1;
5696 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5697 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5698 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005699
5700 if (sline >= 0)
5701 {
5702 /* Make sure the whole cursor line is visible, if possible. */
5703 int rows = plines_win(wp, lnum, FALSE);
5704
5705 if (sline > wp->w_height - rows)
5706 {
5707 sline = wp->w_height - rows;
5708 wp->w_wrow -= rows - line_size;
5709 }
5710 }
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 if (sline < 0)
5713 {
5714 /*
5715 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005716 * Make cursor line the first line in the window. If not enough
5717 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 */
5719 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005720 if (wp->w_wrow >= wp->w_height
5721 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5722 {
5723 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5724 --wp->w_wrow;
5725 while (wp->w_wrow >= wp->w_height)
5726 {
5727 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5728 + win_col_off2(wp);
5729 --wp->w_wrow;
5730 }
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else
5734 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005735 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737#ifdef FEAT_FOLDING
5738 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5739 if (lnum == 1)
5740 {
5741 /* first line in buffer is folded */
5742 line_size = 1;
5743 --sline;
5744 break;
5745 }
5746#endif
5747 --lnum;
5748#ifdef FEAT_DIFF
5749 if (lnum == wp->w_topline)
5750 line_size = plines_win_nofill(wp, lnum, TRUE)
5751 + wp->w_topfill;
5752 else
5753#endif
5754 line_size = plines_win(wp, lnum, TRUE);
5755 sline -= line_size;
5756 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 if (sline < 0)
5759 {
5760 /*
5761 * Line we want at top would go off top of screen. Use next
5762 * line instead.
5763 */
5764#ifdef FEAT_FOLDING
5765 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5766#endif
5767 lnum++;
5768 wp->w_wrow -= line_size + sline;
5769 }
5770 else if (sline > 0)
5771 {
5772 /* First line of file reached, use that as topline. */
5773 lnum = 1;
5774 wp->w_wrow -= sline;
5775 }
5776 }
5777 set_topline(wp, lnum);
5778 }
5779
5780 if (wp == curwin)
5781 {
5782 if (p_so)
5783 update_topline();
5784 curs_columns(FALSE); /* validate w_wrow */
5785 }
5786 wp->w_prev_fraction_row = wp->w_wrow;
5787
5788 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005789 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_WINDOWS
5791 wp->w_redr_status = TRUE;
5792#endif
5793 invalidate_botline_win(wp);
5794}
5795
5796#ifdef FEAT_VERTSPLIT
5797/*
5798 * Set the width of a window.
5799 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005800 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801win_new_width(wp, width)
5802 win_T *wp;
5803 int width;
5804{
5805 wp->w_width = width;
5806 wp->w_lines_valid = 0;
5807 changed_line_abv_curs_win(wp);
5808 invalidate_botline_win(wp);
5809 if (wp == curwin)
5810 {
5811 update_topline();
5812 curs_columns(TRUE); /* validate w_wrow */
5813 }
5814 redraw_win_later(wp, NOT_VALID);
5815 wp->w_redr_status = TRUE;
5816}
5817#endif
5818
5819 void
5820win_comp_scroll(wp)
5821 win_T *wp;
5822{
5823 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5824 if (wp->w_p_scr == 0)
5825 wp->w_p_scr = 1;
5826}
5827
5828/*
5829 * command_height: called whenever p_ch has been changed
5830 */
5831 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005832command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834#ifdef FEAT_WINDOWS
5835 int h;
5836 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005837 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005839 /* Use the value of p_ch that we remembered. This is needed for when the
5840 * GUI starts up, we can't be sure in what order things happen. And when
5841 * p_ch was changed in another tab page. */
5842 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 /* Find bottom frame with width of screen. */
5845 frp = lastwin->w_frame;
5846# ifdef FEAT_VERTSPLIT
5847 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5848 frp = frp->fr_parent;
5849# endif
5850
5851 /* Avoid changing the height of a window with 'winfixheight' set. */
5852 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5853 && frp->fr_win->w_p_wfh)
5854 frp = frp->fr_prev;
5855
5856 if (starting != NO_SCREEN)
5857 {
5858 cmdline_row = Rows - p_ch;
5859
5860 if (p_ch > old_p_ch) /* p_ch got bigger */
5861 {
5862 while (p_ch > old_p_ch)
5863 {
5864 if (frp == NULL)
5865 {
5866 EMSG(_(e_noroom));
5867 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005868 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 cmdline_row = Rows - p_ch;
5870 break;
5871 }
5872 h = frp->fr_height - frame_minheight(frp, NULL);
5873 if (h > p_ch - old_p_ch)
5874 h = p_ch - old_p_ch;
5875 old_p_ch += h;
5876 frame_add_height(frp, -h);
5877 frp = frp->fr_prev;
5878 }
5879
5880 /* Recompute window positions. */
5881 (void)win_comp_pos();
5882
5883 /* clear the lines added to cmdline */
5884 if (full_screen)
5885 screen_fill((int)(cmdline_row), (int)Rows, 0,
5886 (int)Columns, ' ', ' ', 0);
5887 msg_row = cmdline_row;
5888 redraw_cmdline = TRUE;
5889 return;
5890 }
5891
5892 if (msg_row < cmdline_row)
5893 msg_row = cmdline_row;
5894 redraw_cmdline = TRUE;
5895 }
5896 frame_add_height(frp, (int)(old_p_ch - p_ch));
5897
5898 /* Recompute window positions. */
5899 if (frp != lastwin->w_frame)
5900 (void)win_comp_pos();
5901#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005903 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#endif
5905}
5906
5907#if defined(FEAT_WINDOWS) || defined(PROTO)
5908/*
5909 * Resize frame "frp" to be "n" lines higher (negative for less high).
5910 * Also resize the frames it is contained in.
5911 */
5912 static void
5913frame_add_height(frp, n)
5914 frame_T *frp;
5915 int n;
5916{
5917 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5918 for (;;)
5919 {
5920 frp = frp->fr_parent;
5921 if (frp == NULL)
5922 break;
5923 frp->fr_height += n;
5924 }
5925}
5926
5927/*
5928 * Add or remove a status line for the bottom window(s), according to the
5929 * value of 'laststatus'.
5930 */
5931 void
5932last_status(morewin)
5933 int morewin; /* pretend there are two or more windows */
5934{
5935 /* Don't make a difference between horizontal or vertical split. */
5936 last_status_rec(topframe, (p_ls == 2
5937 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5938}
5939
5940 static void
5941last_status_rec(fr, statusline)
5942 frame_T *fr;
5943 int statusline;
5944{
5945 frame_T *fp;
5946 win_T *wp;
5947
5948 if (fr->fr_layout == FR_LEAF)
5949 {
5950 wp = fr->fr_win;
5951 if (wp->w_status_height != 0 && !statusline)
5952 {
5953 /* remove status line */
5954 win_new_height(wp, wp->w_height + 1);
5955 wp->w_status_height = 0;
5956 comp_col();
5957 }
5958 else if (wp->w_status_height == 0 && statusline)
5959 {
5960 /* Find a frame to take a line from. */
5961 fp = fr;
5962 while (fp->fr_height <= frame_minheight(fp, NULL))
5963 {
5964 if (fp == topframe)
5965 {
5966 EMSG(_(e_noroom));
5967 return;
5968 }
5969 /* In a column of frames: go to frame above. If already at
5970 * the top or in a row of frames: go to parent. */
5971 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5972 fp = fp->fr_prev;
5973 else
5974 fp = fp->fr_parent;
5975 }
5976 wp->w_status_height = 1;
5977 if (fp != fr)
5978 {
5979 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5980 frame_fix_height(wp);
5981 (void)win_comp_pos();
5982 }
5983 else
5984 win_new_height(wp, wp->w_height - 1);
5985 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005986 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988 }
5989#ifdef FEAT_VERTSPLIT
5990 else if (fr->fr_layout == FR_ROW)
5991 {
5992 /* vertically split windows, set status line for each one */
5993 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5994 last_status_rec(fp, statusline);
5995 }
5996#endif
5997 else
5998 {
5999 /* horizontally split window, set status line for last one */
6000 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6001 ;
6002 last_status_rec(fp, statusline);
6003 }
6004}
6005
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006006/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006007 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006008 */
6009 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006010tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006012#ifdef FEAT_GUI_TABLINE
6013 /* When the GUI has the tabline then this always returns zero. */
6014 if (gui_use_tabline())
6015 return 0;
6016#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006017 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006018 {
6019 case 0: return 0;
6020 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6021 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022 return 1;
6023}
6024
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025#endif /* FEAT_WINDOWS */
6026
6027#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6028/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006029 * Get the file name at the cursor.
6030 * If Visual mode is active, use the selected text if it's in one line.
6031 * Returns the name in allocated memory, NULL for failure.
6032 */
6033 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006034grab_file_name(count, file_lnum)
6035 long count;
6036 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006037{
6038# ifdef FEAT_VISUAL
6039 if (VIsual_active)
6040 {
6041 int len;
6042 char_u *ptr;
6043
6044 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6045 return NULL;
6046 return find_file_name_in_path(ptr, len,
6047 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6048 }
6049# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006050 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6051 file_lnum);
6052
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006053}
6054
6055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * Return the file name under or after the cursor.
6057 *
6058 * The 'path' option is searched if the file name is not absolute.
6059 * The string returned has been alloc'ed and should be freed by the caller.
6060 * NULL is returned if the file name or file is not found.
6061 *
6062 * options:
6063 * FNAME_MESS give error messages
6064 * FNAME_EXP expand to path
6065 * FNAME_HYP check for hypertext link
6066 * FNAME_INCL apply "includeexpr"
6067 */
6068 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006069file_name_at_cursor(options, count, file_lnum)
6070 int options;
6071 long count;
6072 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
6074 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006075 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6076 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077}
6078
6079/*
6080 * Return the name of the file under or after ptr[col].
6081 * Otherwise like file_name_at_cursor().
6082 */
6083 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006084file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 char_u *line;
6086 int col;
6087 int options;
6088 long count;
6089 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006090 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 char_u *ptr;
6093 int len;
6094
6095 /*
6096 * search forward for what could be the start of a file name
6097 */
6098 ptr = line + col;
6099 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006100 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 if (*ptr == NUL) /* nothing found */
6102 {
6103 if (options & FNAME_MESS)
6104 EMSG(_("E446: No file name under cursor"));
6105 return NULL;
6106 }
6107
6108 /*
6109 * Search backward for first char of the file name.
6110 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6111 */
6112 while (ptr > line)
6113 {
6114#ifdef FEAT_MBYTE
6115 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6116 ptr -= len + 1;
6117 else
6118#endif
6119 if (vim_isfilec(ptr[-1])
6120 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6121 --ptr;
6122 else
6123 break;
6124 }
6125
6126 /*
6127 * Search forward for the last char of the file name.
6128 * Also allow "://" when ':' is not in 'isfname'.
6129 */
6130 len = 0;
6131 while (vim_isfilec(ptr[len])
6132 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6133#ifdef FEAT_MBYTE
6134 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006135 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 else
6137#endif
6138 ++len;
6139
6140 /*
6141 * If there is trailing punctuation, remove it.
6142 * But don't remove "..", could be a directory name.
6143 */
6144 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6145 && ptr[len - 2] != '.')
6146 --len;
6147
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006148 if (file_lnum != NULL)
6149 {
6150 char_u *p;
6151
6152 /* Get the number after the file name and a separator character */
6153 p = ptr + len;
6154 p = skipwhite(p);
6155 if (*p != NUL)
6156 {
6157 if (!isdigit(*p))
6158 ++p; /* skip the separator */
6159 p = skipwhite(p);
6160 if (isdigit(*p))
6161 *file_lnum = (int)getdigits(&p);
6162 }
6163 }
6164
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6166}
6167
6168# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6169static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6170
6171 static char_u *
6172eval_includeexpr(ptr, len)
6173 char_u *ptr;
6174 int len;
6175{
6176 char_u *res;
6177
6178 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006179 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006180 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 set_vim_var_string(VV_FNAME, NULL, 0);
6182 return res;
6183}
6184#endif
6185
6186/*
6187 * Return the name of the file ptr[len] in 'path'.
6188 * Otherwise like file_name_at_cursor().
6189 */
6190 char_u *
6191find_file_name_in_path(ptr, len, options, count, rel_fname)
6192 char_u *ptr;
6193 int len;
6194 int options;
6195 long count;
6196 char_u *rel_fname; /* file we are searching relative to */
6197{
6198 char_u *file_name;
6199 int c;
6200# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6201 char_u *tofree = NULL;
6202
6203 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6204 {
6205 tofree = eval_includeexpr(ptr, len);
6206 if (tofree != NULL)
6207 {
6208 ptr = tofree;
6209 len = (int)STRLEN(ptr);
6210 }
6211 }
6212# endif
6213
6214 if (options & FNAME_EXP)
6215 {
6216 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6217 TRUE, rel_fname);
6218
6219# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6220 /*
6221 * If the file could not be found in a normal way, try applying
6222 * 'includeexpr' (unless done already).
6223 */
6224 if (file_name == NULL
6225 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6226 {
6227 tofree = eval_includeexpr(ptr, len);
6228 if (tofree != NULL)
6229 {
6230 ptr = tofree;
6231 len = (int)STRLEN(ptr);
6232 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6233 TRUE, rel_fname);
6234 }
6235 }
6236# endif
6237 if (file_name == NULL && (options & FNAME_MESS))
6238 {
6239 c = ptr[len];
6240 ptr[len] = NUL;
6241 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6242 ptr[len] = c;
6243 }
6244
6245 /* Repeat finding the file "count" times. This matters when it
6246 * appears several times in the path. */
6247 while (file_name != NULL && --count > 0)
6248 {
6249 vim_free(file_name);
6250 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6251 }
6252 }
6253 else
6254 file_name = vim_strnsave(ptr, len);
6255
6256# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6257 vim_free(tofree);
6258# endif
6259
6260 return file_name;
6261}
6262#endif /* FEAT_SEARCHPATH */
6263
6264/*
6265 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6266 * Also check for ":\\", which MS Internet Explorer accepts, return
6267 * URL_BACKSLASH.
6268 */
6269 static int
6270path_is_url(p)
6271 char_u *p;
6272{
6273 if (STRNCMP(p, "://", (size_t)3) == 0)
6274 return URL_SLASH;
6275 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6276 return URL_BACKSLASH;
6277 return 0;
6278}
6279
6280/*
6281 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6282 * Return URL_BACKSLASH for "name:\\".
6283 * Return zero otherwise.
6284 */
6285 int
6286path_with_url(fname)
6287 char_u *fname;
6288{
6289 char_u *p;
6290
6291 for (p = fname; isalpha(*p); ++p)
6292 ;
6293 return path_is_url(p);
6294}
6295
6296/*
6297 * Return TRUE if "name" is a full (absolute) path name or URL.
6298 */
6299 int
6300vim_isAbsName(name)
6301 char_u *name;
6302{
6303 return (path_with_url(name) != 0 || mch_isFullName(name));
6304}
6305
6306/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006307 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 *
6309 * return FAIL for failure, OK otherwise
6310 */
6311 int
6312vim_FullName(fname, buf, len, force)
6313 char_u *fname, *buf;
6314 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006315 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int retval = OK;
6318 int url;
6319
6320 *buf = NUL;
6321 if (fname == NULL)
6322 return FAIL;
6323
6324 url = path_with_url(fname);
6325 if (!url)
6326 retval = mch_FullName(fname, buf, len, force);
6327 if (url || retval == FAIL)
6328 {
6329 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006330 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6333 slash_adjust(buf);
6334#endif
6335 return retval;
6336}
6337
6338/*
6339 * Return the minimal number of rows that is needed on the screen to display
6340 * the current number of windows.
6341 */
6342 int
6343min_rows()
6344{
6345 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006346#ifdef FEAT_WINDOWS
6347 tabpage_T *tp;
6348 int n;
6349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351 if (firstwin == NULL) /* not initialized yet */
6352 return MIN_LINES;
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355 total = 0;
6356 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6357 {
6358 n = frame_minheight(tp->tp_topframe, NULL);
6359 if (total < n)
6360 total = n;
6361 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006362 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006364 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006366 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 return total;
6368}
6369
6370/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006371 * Return TRUE if there is only one window (in the current tab page), not
6372 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006373 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 */
6375 int
6376only_one_window()
6377{
6378#ifdef FEAT_WINDOWS
6379 int count = 0;
6380 win_T *wp;
6381
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 /* If there is another tab page there always is another window. */
6383 if (first_tabpage->tp_next != NULL)
6384 return FALSE;
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006387 if (wp->w_buffer != NULL
6388 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389# ifdef FEAT_QUICKFIX
6390 || wp->w_p_pvw
6391# endif
6392 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006393# ifdef FEAT_AUTOCMD
6394 && wp != aucmd_win
6395# endif
6396 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 ++count;
6398 return (count <= 1);
6399#else
6400 return TRUE;
6401#endif
6402}
6403
6404#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6405/*
6406 * Correct the cursor line number in other windows. Used after changing the
6407 * current buffer, and before applying autocommands.
6408 * When "do_curwin" is TRUE, also check current window.
6409 */
6410 void
6411check_lnums(do_curwin)
6412 int do_curwin;
6413{
6414 win_T *wp;
6415
6416#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006417 tabpage_T *tp;
6418
6419 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6421#else
6422 wp = curwin;
6423 if (do_curwin)
6424#endif
6425 {
6426 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6427 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6428 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6429 wp->w_topline = curbuf->b_ml.ml_line_count;
6430 }
6431}
6432#endif
6433
6434#if defined(FEAT_WINDOWS) || defined(PROTO)
6435
6436/*
6437 * A snapshot of the window sizes, to restore them after closing the help
6438 * window.
6439 * Only these fields are used:
6440 * fr_layout
6441 * fr_width
6442 * fr_height
6443 * fr_next
6444 * fr_child
6445 * fr_win (only valid for the old curwin, NULL otherwise)
6446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447
6448/*
6449 * Create a snapshot of the current frame sizes.
6450 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006451 void
6452make_snapshot(idx)
6453 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006455 clear_snapshot(curtab, idx);
6456 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459 static void
6460make_snapshot_rec(fr, frp)
6461 frame_T *fr;
6462 frame_T **frp;
6463{
6464 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6465 if (*frp == NULL)
6466 return;
6467 (*frp)->fr_layout = fr->fr_layout;
6468# ifdef FEAT_VERTSPLIT
6469 (*frp)->fr_width = fr->fr_width;
6470# endif
6471 (*frp)->fr_height = fr->fr_height;
6472 if (fr->fr_next != NULL)
6473 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6474 if (fr->fr_child != NULL)
6475 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6476 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6477 (*frp)->fr_win = curwin;
6478}
6479
6480/*
6481 * Remove any existing snapshot.
6482 */
6483 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006484clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006485 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006486 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006488 clear_snapshot_rec(tp->tp_snapshot[idx]);
6489 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490}
6491
6492 static void
6493clear_snapshot_rec(fr)
6494 frame_T *fr;
6495{
6496 if (fr != NULL)
6497 {
6498 clear_snapshot_rec(fr->fr_next);
6499 clear_snapshot_rec(fr->fr_child);
6500 vim_free(fr);
6501 }
6502}
6503
6504/*
6505 * Restore a previously created snapshot, if there is any.
6506 * This is only done if the screen size didn't change and the window layout is
6507 * still the same.
6508 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006509 void
6510restore_snapshot(idx, close_curwin)
6511 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 int close_curwin; /* closing current window */
6513{
6514 win_T *wp;
6515
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006516 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006518 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006520 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6521 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006523 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 win_comp_pos();
6525 if (wp != NULL && close_curwin)
6526 win_goto(wp);
6527 redraw_all_later(CLEAR);
6528 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006529 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530}
6531
6532/*
6533 * Check if frames "sn" and "fr" have the same layout, same following frames
6534 * and same children.
6535 */
6536 static int
6537check_snapshot_rec(sn, fr)
6538 frame_T *sn;
6539 frame_T *fr;
6540{
6541 if (sn->fr_layout != fr->fr_layout
6542 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6543 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6544 || (sn->fr_next != NULL
6545 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6546 || (sn->fr_child != NULL
6547 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6548 return FAIL;
6549 return OK;
6550}
6551
6552/*
6553 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6554 * following frames and children.
6555 * Returns a pointer to the old current window, or NULL.
6556 */
6557 static win_T *
6558restore_snapshot_rec(sn, fr)
6559 frame_T *sn;
6560 frame_T *fr;
6561{
6562 win_T *wp = NULL;
6563 win_T *wp2;
6564
6565 fr->fr_height = sn->fr_height;
6566# ifdef FEAT_VERTSPLIT
6567 fr->fr_width = sn->fr_width;
6568# endif
6569 if (fr->fr_layout == FR_LEAF)
6570 {
6571 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6572# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006573 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574# endif
6575 wp = sn->fr_win;
6576 }
6577 if (sn->fr_next != NULL)
6578 {
6579 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6580 if (wp2 != NULL)
6581 wp = wp2;
6582 }
6583 if (sn->fr_child != NULL)
6584 {
6585 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6586 if (wp2 != NULL)
6587 wp = wp2;
6588 }
6589 return wp;
6590}
6591
6592#endif
6593
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006594#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6595 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006596/*
6597 * Set "win" to be the curwin and "tp" to be the current tab page.
6598 * restore_win() MUST be called to undo.
6599 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006600 * When "no_display" is TRUE the display won't be affected, no redraw is
6601 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006602 * Returns FAIL if switching to "win" failed.
6603 */
6604 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006605switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006606 win_T **save_curwin UNUSED;
6607 tabpage_T **save_curtab UNUSED;
6608 win_T *win UNUSED;
6609 tabpage_T *tp UNUSED;
6610 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006611{
6612# ifdef FEAT_AUTOCMD
6613 block_autocmds();
6614# endif
6615# ifdef FEAT_WINDOWS
6616 *save_curwin = curwin;
6617 if (tp != NULL)
6618 {
6619 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006620 if (no_display)
6621 {
6622 curtab->tp_firstwin = firstwin;
6623 curtab->tp_lastwin = lastwin;
6624 curtab = tp;
6625 firstwin = curtab->tp_firstwin;
6626 lastwin = curtab->tp_lastwin;
6627 }
6628 else
6629 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006630 }
6631 if (!win_valid(win))
6632 {
6633# ifdef FEAT_AUTOCMD
6634 unblock_autocmds();
6635# endif
6636 return FAIL;
6637 }
6638 curwin = win;
6639 curbuf = curwin->w_buffer;
6640# endif
6641 return OK;
6642}
6643
6644/*
6645 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006646 * When "no_display" is TRUE the display won't be affected, no redraw is
6647 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006648 */
6649 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006650restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006651 win_T *save_curwin UNUSED;
6652 tabpage_T *save_curtab UNUSED;
6653 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006654{
6655# ifdef FEAT_WINDOWS
6656 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006657 {
6658 if (no_display)
6659 {
6660 curtab->tp_firstwin = firstwin;
6661 curtab->tp_lastwin = lastwin;
6662 curtab = save_curtab;
6663 firstwin = curtab->tp_firstwin;
6664 lastwin = curtab->tp_lastwin;
6665 }
6666 else
6667 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6668 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006669 if (win_valid(save_curwin))
6670 {
6671 curwin = save_curwin;
6672 curbuf = curwin->w_buffer;
6673 }
6674# endif
6675# ifdef FEAT_AUTOCMD
6676 unblock_autocmds();
6677# endif
6678}
6679
6680/*
6681 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6682 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6683 */
6684 void
6685switch_buffer(save_curbuf, buf)
6686 buf_T *buf;
6687 buf_T **save_curbuf;
6688{
6689# ifdef FEAT_AUTOCMD
6690 block_autocmds();
6691# endif
6692 *save_curbuf = curbuf;
6693 --curbuf->b_nwindows;
6694 curbuf = buf;
6695 curwin->w_buffer = buf;
6696 ++curbuf->b_nwindows;
6697}
6698
6699/*
6700 * Restore the current buffer after using switch_buffer().
6701 */
6702 void
6703restore_buffer(save_curbuf)
6704 buf_T *save_curbuf;
6705{
6706# ifdef FEAT_AUTOCMD
6707 unblock_autocmds();
6708# endif
6709 /* Check for valid buffer, just in case. */
6710 if (buf_valid(save_curbuf))
6711 {
6712 --curbuf->b_nwindows;
6713 curwin->w_buffer = save_curbuf;
6714 curbuf = save_curbuf;
6715 ++curbuf->b_nwindows;
6716 }
6717}
6718#endif
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6721/*
6722 * Return TRUE if there is any vertically split window.
6723 */
6724 int
6725win_hasvertsplit()
6726{
6727 frame_T *fr;
6728
6729 if (topframe->fr_layout == FR_ROW)
6730 return TRUE;
6731
6732 if (topframe->fr_layout == FR_COL)
6733 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6734 if (fr->fr_layout == FR_ROW)
6735 return TRUE;
6736
6737 return FALSE;
6738}
6739#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006740
6741#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6742/*
6743 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006744 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006745 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6746 * If no particular ID is desired, -1 must be specified for 'id'.
6747 * Return ID of added match, -1 on failure.
6748 */
6749 int
6750match_add(wp, grp, pat, prio, id)
6751 win_T *wp;
6752 char_u *grp;
6753 char_u *pat;
6754 int prio;
6755 int id;
6756{
6757 matchitem_T *cur;
6758 matchitem_T *prev;
6759 matchitem_T *m;
6760 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006761 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006762
6763 if (*grp == NUL || *pat == NUL)
6764 return -1;
6765 if (id < -1 || id == 0)
6766 {
6767 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6768 return -1;
6769 }
6770 if (id != -1)
6771 {
6772 cur = wp->w_match_head;
6773 while (cur != NULL)
6774 {
6775 if (cur->id == id)
6776 {
6777 EMSGN("E801: ID already taken: %ld", id);
6778 return -1;
6779 }
6780 cur = cur->next;
6781 }
6782 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006783 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006784 {
6785 EMSG2(_(e_nogroup), grp);
6786 return -1;
6787 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006788 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006789 {
6790 EMSG2(_(e_invarg2), pat);
6791 return -1;
6792 }
6793
6794 /* Find available match ID. */
6795 while (id == -1)
6796 {
6797 cur = wp->w_match_head;
6798 while (cur != NULL && cur->id != wp->w_next_match_id)
6799 cur = cur->next;
6800 if (cur == NULL)
6801 id = wp->w_next_match_id;
6802 wp->w_next_match_id++;
6803 }
6804
6805 /* Build new match. */
6806 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6807 m->id = id;
6808 m->priority = prio;
6809 m->pattern = vim_strsave(pat);
6810 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006811 m->match.regprog = regprog;
6812 m->match.rmm_ic = FALSE;
6813 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006814
6815 /* Insert new match. The match list is in ascending order with regard to
6816 * the match priorities. */
6817 cur = wp->w_match_head;
6818 prev = cur;
6819 while (cur != NULL && prio >= cur->priority)
6820 {
6821 prev = cur;
6822 cur = cur->next;
6823 }
6824 if (cur == prev)
6825 wp->w_match_head = m;
6826 else
6827 prev->next = m;
6828 m->next = cur;
6829
6830 redraw_later(SOME_VALID);
6831 return id;
6832}
6833
6834/*
6835 * Delete match with ID 'id' in the match list of window 'wp'.
6836 * Print error messages if 'perr' is TRUE.
6837 */
6838 int
6839match_delete(wp, id, perr)
6840 win_T *wp;
6841 int id;
6842 int perr;
6843{
6844 matchitem_T *cur = wp->w_match_head;
6845 matchitem_T *prev = cur;
6846
6847 if (id < 1)
6848 {
6849 if (perr == TRUE)
6850 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6851 id);
6852 return -1;
6853 }
6854 while (cur != NULL && cur->id != id)
6855 {
6856 prev = cur;
6857 cur = cur->next;
6858 }
6859 if (cur == NULL)
6860 {
6861 if (perr == TRUE)
6862 EMSGN("E803: ID not found: %ld", id);
6863 return -1;
6864 }
6865 if (cur == prev)
6866 wp->w_match_head = cur->next;
6867 else
6868 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006869 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006870 vim_free(cur->pattern);
6871 vim_free(cur);
6872 redraw_later(SOME_VALID);
6873 return 0;
6874}
6875
6876/*
6877 * Delete all matches in the match list of window 'wp'.
6878 */
6879 void
6880clear_matches(wp)
6881 win_T *wp;
6882{
6883 matchitem_T *m;
6884
6885 while (wp->w_match_head != NULL)
6886 {
6887 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006888 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006889 vim_free(wp->w_match_head->pattern);
6890 vim_free(wp->w_match_head);
6891 wp->w_match_head = m;
6892 }
6893 redraw_later(SOME_VALID);
6894}
6895
6896/*
6897 * Get match from ID 'id' in window 'wp'.
6898 * Return NULL if match not found.
6899 */
6900 matchitem_T *
6901get_match(wp, id)
6902 win_T *wp;
6903 int id;
6904{
6905 matchitem_T *cur = wp->w_match_head;
6906
6907 while (cur != NULL && cur->id != id)
6908 cur = cur->next;
6909 return cur;
6910}
6911#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006912
6913#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6914 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006915get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006916{
6917 int i = 1;
6918 win_T *w;
6919
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006920 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006921 ++i;
6922
6923 if (w == NULL)
6924 return 0;
6925 else
6926 return i;
6927}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006928
6929 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006930get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006931{
6932 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006933# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006934 tabpage_T *t;
6935
6936 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6937 ++i;
6938
6939 if (t == NULL)
6940 return 0;
6941 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006942# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006943 return i;
6944}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006945#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006946
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006947#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006948/*
6949 * Return TRUE if "topfrp" and its children are at the right height.
6950 */
6951 static int
6952frame_check_height(topfrp, height)
6953 frame_T *topfrp;
6954 int height;
6955{
6956 frame_T *frp;
6957
6958 if (topfrp->fr_height != height)
6959 return FALSE;
6960
6961 if (topfrp->fr_layout == FR_ROW)
6962 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6963 if (frp->fr_height != height)
6964 return FALSE;
6965
6966 return TRUE;
6967}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006968#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006969
6970#ifdef FEAT_VERTSPLIT
6971/*
6972 * Return TRUE if "topfrp" and its children are at the right width.
6973 */
6974 static int
6975frame_check_width(topfrp, width)
6976 frame_T *topfrp;
6977 int width;
6978{
6979 frame_T *frp;
6980
6981 if (topfrp->fr_width != width)
6982 return FALSE;
6983
6984 if (topfrp->fr_layout == FR_COL)
6985 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6986 if (frp->fr_width != width)
6987 return FALSE;
6988
6989 return TRUE;
6990}
6991#endif
6992