blob: 54ab200807cbde877c3b1266d518087099643c01 [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)))
2294 /* Autocommands have close all windows, quit now. */
2295 getout(0);
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002298 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002299 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002300 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return;
2302
Bram Moolenaara971b822011-09-14 14:43:25 +02002303 /* Free the memory used for the window and get the window that received
2304 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002305 wp = win_free_mem(win, &dir, NULL);
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 /* Make sure curwin isn't invalid. It can cause severe trouble when
2308 * printing an error message. For win_equal() curbuf needs to be valid
2309 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002310 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 curwin = wp;
2313#ifdef FEAT_QUICKFIX
2314 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2315 {
2316 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002317 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 * finding another window to go to.
2319 */
2320 for (;;)
2321 {
2322 if (wp->w_next == NULL)
2323 wp = firstwin;
2324 else
2325 wp = wp->w_next;
2326 if (wp == curwin)
2327 break;
2328 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2329 {
2330 curwin = wp;
2331 break;
2332 }
2333 }
2334 }
2335#endif
2336 curbuf = curwin->w_buffer;
2337 close_curwin = TRUE;
2338 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002339 if (p_ea
2340#ifdef FEAT_VERTSPLIT
2341 && (*p_ead == 'b' || *p_ead == dir)
2342#endif
2343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 win_equal(curwin, TRUE,
2345#ifdef FEAT_VERTSPLIT
2346 dir
2347#else
2348 0
2349#endif
2350 );
2351 else
2352 win_comp_pos();
2353 if (close_curwin)
2354 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002355 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#ifdef FEAT_AUTOCMD
2357 if (other_buffer)
2358 /* careful: after this wp and win may be invalid! */
2359 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2360#endif
2361 }
2362
2363 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364 * If last window has a status line now and we don't want one,
2365 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 */
2367 last_status(FALSE);
2368
2369 /* After closing the help window, try restoring the window layout from
2370 * before it was opened. */
2371 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002372 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2375 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2376 if (gui.in_use && !win_hasvertsplit())
2377 gui_init_which_components(NULL);
2378#endif
2379
2380 redraw_all_later(NOT_VALID);
2381}
2382
2383/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002385 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002386 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002387 * Caller must check if buffer is hidden and whether the tabline needs to be
2388 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002389 */
2390 void
2391win_close_othertab(win, free_buf, tp)
2392 win_T *win;
2393 int free_buf;
2394 tabpage_T *tp;
2395{
2396 win_T *wp;
2397 int dir;
2398 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002399 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002400
Bram Moolenaar362ce482012-06-06 19:02:45 +02002401#ifdef FEAT_AUTOCMD
2402 if (win->w_closing || win->w_buffer->b_closing)
2403 return; /* window is already being closed */
2404#endif
2405
Bram Moolenaarf740b292006-02-16 22:11:02 +00002406 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002407 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002408
2409 /* Careful: Autocommands may have closed the tab page or made it the
2410 * current tab page. */
2411 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2412 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002413 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002414 return;
2415
2416 /* Autocommands may have closed the window already. */
2417 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2418 ;
2419 if (wp == NULL)
2420 return;
2421
Bram Moolenaarf740b292006-02-16 22:11:02 +00002422 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002423 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002424 {
2425 if (tp == first_tabpage)
2426 first_tabpage = tp->tp_next;
2427 else
2428 {
2429 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2430 ptp = ptp->tp_next)
2431 ;
2432 if (ptp == NULL)
2433 {
2434 EMSG2(_(e_intern2), "win_close_othertab()");
2435 return;
2436 }
2437 ptp->tp_next = tp->tp_next;
2438 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002439 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002441
2442 /* Free the memory used for the window. */
2443 win_free_mem(win, &dir, tp);
2444
2445 if (free_tp)
2446 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002447}
2448
2449/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002450 * Free the memory used for a window.
2451 * Returns a pointer to the window that got the freed up space.
2452 */
2453 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002454win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002455 win_T *win;
2456 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002457 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002458{
2459 frame_T *frp;
2460 win_T *wp;
2461
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002462 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002463 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002464 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002465 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002466 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002467
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002468 /* When deleting the current window of another tab page select a new
2469 * current window. */
2470 if (tp != NULL && win == tp->tp_curwin)
2471 tp->tp_curwin = wp;
2472
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002473 return wp;
2474}
2475
2476#if defined(EXITFREE) || defined(PROTO)
2477 void
2478win_free_all()
2479{
2480 int dummy;
2481
Bram Moolenaarf740b292006-02-16 22:11:02 +00002482# ifdef FEAT_WINDOWS
2483 while (first_tabpage->tp_next != NULL)
2484 tabpage_close(TRUE);
2485# endif
2486
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002487# ifdef FEAT_AUTOCMD
2488 if (aucmd_win != NULL)
2489 {
2490 (void)win_free_mem(aucmd_win, &dummy, NULL);
2491 aucmd_win = NULL;
2492 }
2493# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002494
2495 while (firstwin != NULL)
2496 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002497}
2498#endif
2499
2500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 * Remove a window and its frame from the tree of frames.
2502 * Returns a pointer to the window that got the freed up space.
2503 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002504 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002505winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002507 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002508 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 frame_T *frp, *frp2, *frp3;
2511 frame_T *frp_close = win->w_frame;
2512 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 * If there is only one window there is nothing to remove.
2516 */
2517 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2518 return NULL;
2519
2520 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * Remove the window from its frame.
2522 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002523 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 wp = frame2win(frp2);
2525
2526 /* Remove this frame from the list of frames. */
2527 frame_remove(frp_close);
2528
2529#ifdef FEAT_VERTSPLIT
2530 if (frp_close->fr_parent->fr_layout == FR_COL)
2531 {
2532#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002533 /* When 'winfixheight' is set, try to find another frame in the column
2534 * (as close to the closed frame as possible) to distribute the height
2535 * to. */
2536 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2537 {
2538 frp = frp_close->fr_prev;
2539 frp3 = frp_close->fr_next;
2540 while (frp != NULL || frp3 != NULL)
2541 {
2542 if (frp != NULL)
2543 {
2544 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2545 {
2546 frp2 = frp;
2547 wp = frp->fr_win;
2548 break;
2549 }
2550 frp = frp->fr_prev;
2551 }
2552 if (frp3 != NULL)
2553 {
2554 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2555 {
2556 frp2 = frp3;
2557 wp = frp3->fr_win;
2558 break;
2559 }
2560 frp3 = frp3->fr_next;
2561 }
2562 }
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2565 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566#ifdef FEAT_VERTSPLIT
2567 *dirp = 'v';
2568 }
2569 else
2570 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002571 /* When 'winfixwidth' is set, try to find another frame in the column
2572 * (as close to the closed frame as possible) to distribute the width
2573 * to. */
2574 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2575 {
2576 frp = frp_close->fr_prev;
2577 frp3 = frp_close->fr_next;
2578 while (frp != NULL || frp3 != NULL)
2579 {
2580 if (frp != NULL)
2581 {
2582 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2583 {
2584 frp2 = frp;
2585 wp = frp->fr_win;
2586 break;
2587 }
2588 frp = frp->fr_prev;
2589 }
2590 if (frp3 != NULL)
2591 {
2592 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2593 {
2594 frp2 = frp3;
2595 wp = frp3->fr_win;
2596 break;
2597 }
2598 frp3 = frp3->fr_next;
2599 }
2600 }
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002603 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 *dirp = 'h';
2605 }
2606#endif
2607
2608 /* If rows/columns go to a window below/right its positions need to be
2609 * updated. Can only be done after the sizes have been updated. */
2610 if (frp2 == frp_close->fr_next)
2611 {
2612 int row = win->w_winrow;
2613 int col = W_WINCOL(win);
2614
2615 frame_comp_pos(frp2, &row, &col);
2616 }
2617
2618 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2619 {
2620 /* There is no other frame in this list, move its info to the parent
2621 * and remove it. */
2622 frp2->fr_parent->fr_layout = frp2->fr_layout;
2623 frp2->fr_parent->fr_child = frp2->fr_child;
2624 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2625 frp->fr_parent = frp2->fr_parent;
2626 frp2->fr_parent->fr_win = frp2->fr_win;
2627 if (frp2->fr_win != NULL)
2628 frp2->fr_win->w_frame = frp2->fr_parent;
2629 frp = frp2->fr_parent;
2630 vim_free(frp2);
2631
2632 frp2 = frp->fr_parent;
2633 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2634 {
2635 /* The frame above the parent has the same layout, have to merge
2636 * the frames into this list. */
2637 if (frp2->fr_child == frp)
2638 frp2->fr_child = frp->fr_child;
2639 frp->fr_child->fr_prev = frp->fr_prev;
2640 if (frp->fr_prev != NULL)
2641 frp->fr_prev->fr_next = frp->fr_child;
2642 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2643 {
2644 frp3->fr_parent = frp2;
2645 if (frp3->fr_next == NULL)
2646 {
2647 frp3->fr_next = frp->fr_next;
2648 if (frp->fr_next != NULL)
2649 frp->fr_next->fr_prev = frp3;
2650 break;
2651 }
2652 }
2653 vim_free(frp);
2654 }
2655 }
2656
2657 return wp;
2658}
2659
2660/*
2661 * Find out which frame is going to get the freed up space when "win" is
2662 * closed.
2663 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2664 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2665 * This makes opening a window and closing it immediately keep the same window
2666 * layout.
2667 */
2668 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002669win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002671 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672{
2673 frame_T *frp;
2674 int b;
2675
Bram Moolenaarf740b292006-02-16 22:11:02 +00002676 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002677 /* Last window in this tab page, will go to next tab page. */
2678 return alt_tabpage()->tp_curwin->w_frame;
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 frp = win->w_frame;
2681#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002682 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 b = p_spr;
2684 else
2685#endif
2686 b = p_sb;
2687 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2688 return frp->fr_next;
2689 return frp->fr_prev;
2690}
2691
2692/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002693 * Return the tabpage that will be used if the current one is closed.
2694 */
2695 static tabpage_T *
2696alt_tabpage()
2697{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002698 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002699
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002700 /* Use the next tab page if possible. */
2701 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002702 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002703
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002704 /* Find the last but one tab page. */
2705 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2706 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002707 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002708}
2709
2710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 * Find the left-upper window in frame "frp".
2712 */
2713 static win_T *
2714frame2win(frp)
2715 frame_T *frp;
2716{
2717 while (frp->fr_win == NULL)
2718 frp = frp->fr_child;
2719 return frp->fr_win;
2720}
2721
2722/*
2723 * Return TRUE if frame "frp" contains window "wp".
2724 */
2725 static int
2726frame_has_win(frp, wp)
2727 frame_T *frp;
2728 win_T *wp;
2729{
2730 frame_T *p;
2731
2732 if (frp->fr_layout == FR_LEAF)
2733 return frp->fr_win == wp;
2734
2735 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2736 if (frame_has_win(p, wp))
2737 return TRUE;
2738 return FALSE;
2739}
2740
2741/*
2742 * Set a new height for a frame. Recursively sets the height for contained
2743 * frames and windows. Caller must take care of positions.
2744 */
2745 static void
2746frame_new_height(topfrp, height, topfirst, wfh)
2747 frame_T *topfrp;
2748 int height;
2749 int topfirst; /* resize topmost contained frame first */
2750 int wfh; /* obey 'winfixheight' when there is a choice;
2751 may cause the height not to be set */
2752{
2753 frame_T *frp;
2754 int extra_lines;
2755 int h;
2756
2757 if (topfrp->fr_win != NULL)
2758 {
2759 /* Simple case: just one window. */
2760 win_new_height(topfrp->fr_win,
2761 height - topfrp->fr_win->w_status_height);
2762 }
2763#ifdef FEAT_VERTSPLIT
2764 else if (topfrp->fr_layout == FR_ROW)
2765 {
2766 do
2767 {
2768 /* All frames in this row get the same new height. */
2769 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2770 {
2771 frame_new_height(frp, height, topfirst, wfh);
2772 if (frp->fr_height > height)
2773 {
2774 /* Could not fit the windows, make the whole row higher. */
2775 height = frp->fr_height;
2776 break;
2777 }
2778 }
2779 }
2780 while (frp != NULL);
2781 }
2782#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002783 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
2785 /* Complicated case: Resize a column of frames. Resize the bottom
2786 * frame first, frames above that when needed. */
2787
2788 frp = topfrp->fr_child;
2789 if (wfh)
2790 /* Advance past frames with one window with 'wfh' set. */
2791 while (frame_fixed_height(frp))
2792 {
2793 frp = frp->fr_next;
2794 if (frp == NULL)
2795 return; /* no frame without 'wfh', give up */
2796 }
2797 if (!topfirst)
2798 {
2799 /* Find the bottom frame of this column */
2800 while (frp->fr_next != NULL)
2801 frp = frp->fr_next;
2802 if (wfh)
2803 /* Advance back for frames with one window with 'wfh' set. */
2804 while (frame_fixed_height(frp))
2805 frp = frp->fr_prev;
2806 }
2807
2808 extra_lines = height - topfrp->fr_height;
2809 if (extra_lines < 0)
2810 {
2811 /* reduce height of contained frames, bottom or top frame first */
2812 while (frp != NULL)
2813 {
2814 h = frame_minheight(frp, NULL);
2815 if (frp->fr_height + extra_lines < h)
2816 {
2817 extra_lines += frp->fr_height - h;
2818 frame_new_height(frp, h, topfirst, wfh);
2819 }
2820 else
2821 {
2822 frame_new_height(frp, frp->fr_height + extra_lines,
2823 topfirst, wfh);
2824 break;
2825 }
2826 if (topfirst)
2827 {
2828 do
2829 frp = frp->fr_next;
2830 while (wfh && frp != NULL && frame_fixed_height(frp));
2831 }
2832 else
2833 {
2834 do
2835 frp = frp->fr_prev;
2836 while (wfh && frp != NULL && frame_fixed_height(frp));
2837 }
2838 /* Increase "height" if we could not reduce enough frames. */
2839 if (frp == NULL)
2840 height -= extra_lines;
2841 }
2842 }
2843 else if (extra_lines > 0)
2844 {
2845 /* increase height of bottom or top frame */
2846 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2847 }
2848 }
2849 topfrp->fr_height = height;
2850}
2851
2852/*
2853 * Return TRUE if height of frame "frp" should not be changed because of
2854 * the 'winfixheight' option.
2855 */
2856 static int
2857frame_fixed_height(frp)
2858 frame_T *frp;
2859{
2860 /* frame with one window: fixed height if 'winfixheight' set. */
2861 if (frp->fr_win != NULL)
2862 return frp->fr_win->w_p_wfh;
2863
2864 if (frp->fr_layout == FR_ROW)
2865 {
2866 /* The frame is fixed height if one of the frames in the row is fixed
2867 * height. */
2868 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2869 if (frame_fixed_height(frp))
2870 return TRUE;
2871 return FALSE;
2872 }
2873
2874 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2875 * frames in the row are fixed height. */
2876 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2877 if (!frame_fixed_height(frp))
2878 return FALSE;
2879 return TRUE;
2880}
2881
2882#ifdef FEAT_VERTSPLIT
2883/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002884 * Return TRUE if width of frame "frp" should not be changed because of
2885 * the 'winfixwidth' option.
2886 */
2887 static int
2888frame_fixed_width(frp)
2889 frame_T *frp;
2890{
2891 /* frame with one window: fixed width if 'winfixwidth' set. */
2892 if (frp->fr_win != NULL)
2893 return frp->fr_win->w_p_wfw;
2894
2895 if (frp->fr_layout == FR_COL)
2896 {
2897 /* The frame is fixed width if one of the frames in the row is fixed
2898 * width. */
2899 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2900 if (frame_fixed_width(frp))
2901 return TRUE;
2902 return FALSE;
2903 }
2904
2905 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2906 * frames in the row are fixed width. */
2907 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2908 if (!frame_fixed_width(frp))
2909 return FALSE;
2910 return TRUE;
2911}
2912
2913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 * Add a status line to windows at the bottom of "frp".
2915 * Note: Does not check if there is room!
2916 */
2917 static void
2918frame_add_statusline(frp)
2919 frame_T *frp;
2920{
2921 win_T *wp;
2922
2923 if (frp->fr_layout == FR_LEAF)
2924 {
2925 wp = frp->fr_win;
2926 if (wp->w_status_height == 0)
2927 {
2928 if (wp->w_height > 0) /* don't make it negative */
2929 --wp->w_height;
2930 wp->w_status_height = STATUS_HEIGHT;
2931 }
2932 }
2933 else if (frp->fr_layout == FR_ROW)
2934 {
2935 /* Handle all the frames in the row. */
2936 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2937 frame_add_statusline(frp);
2938 }
2939 else /* frp->fr_layout == FR_COL */
2940 {
2941 /* Only need to handle the last frame in the column. */
2942 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2943 ;
2944 frame_add_statusline(frp);
2945 }
2946}
2947
2948/*
2949 * Set width of a frame. Handles recursively going through contained frames.
2950 * May remove separator line for windows at the right side (for win_close()).
2951 */
2952 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002953frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 frame_T *topfrp;
2955 int width;
2956 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002957 int wfw; /* obey 'winfixwidth' when there is a choice;
2958 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 frame_T *frp;
2961 int extra_cols;
2962 int w;
2963 win_T *wp;
2964
2965 if (topfrp->fr_layout == FR_LEAF)
2966 {
2967 /* Simple case: just one window. */
2968 wp = topfrp->fr_win;
2969 /* Find out if there are any windows right of this one. */
2970 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2971 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2972 break;
2973 if (frp->fr_parent == NULL)
2974 wp->w_vsep_width = 0;
2975 win_new_width(wp, width - wp->w_vsep_width);
2976 }
2977 else if (topfrp->fr_layout == FR_COL)
2978 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002979 do
2980 {
2981 /* All frames in this column get the same new width. */
2982 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2983 {
2984 frame_new_width(frp, width, leftfirst, wfw);
2985 if (frp->fr_width > width)
2986 {
2987 /* Could not fit the windows, make whole column wider. */
2988 width = frp->fr_width;
2989 break;
2990 }
2991 }
2992 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994 else /* fr_layout == FR_ROW */
2995 {
2996 /* Complicated case: Resize a row of frames. Resize the rightmost
2997 * frame first, frames left of it when needed. */
2998
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003000 if (wfw)
3001 /* Advance past frames with one window with 'wfw' set. */
3002 while (frame_fixed_width(frp))
3003 {
3004 frp = frp->fr_next;
3005 if (frp == NULL)
3006 return; /* no frame without 'wfw', give up */
3007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003009 {
3010 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 while (frp->fr_next != NULL)
3012 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003013 if (wfw)
3014 /* Advance back for frames with one window with 'wfw' set. */
3015 while (frame_fixed_width(frp))
3016 frp = frp->fr_prev;
3017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 extra_cols = width - topfrp->fr_width;
3020 if (extra_cols < 0)
3021 {
3022 /* reduce frame width, rightmost frame first */
3023 while (frp != NULL)
3024 {
3025 w = frame_minwidth(frp, NULL);
3026 if (frp->fr_width + extra_cols < w)
3027 {
3028 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003029 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031 else
3032 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003033 frame_new_width(frp, frp->fr_width + extra_cols,
3034 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
3036 }
3037 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003038 {
3039 do
3040 frp = frp->fr_next;
3041 while (wfw && frp != NULL && frame_fixed_width(frp));
3042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003044 {
3045 do
3046 frp = frp->fr_prev;
3047 while (wfw && frp != NULL && frame_fixed_width(frp));
3048 }
3049 /* Increase "width" if we could not reduce enough frames. */
3050 if (frp == NULL)
3051 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 }
3053 }
3054 else if (extra_cols > 0)
3055 {
3056 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003057 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059 }
3060 topfrp->fr_width = width;
3061}
3062
3063/*
3064 * Add the vertical separator to windows at the right side of "frp".
3065 * Note: Does not check if there is room!
3066 */
3067 static void
3068frame_add_vsep(frp)
3069 frame_T *frp;
3070{
3071 win_T *wp;
3072
3073 if (frp->fr_layout == FR_LEAF)
3074 {
3075 wp = frp->fr_win;
3076 if (wp->w_vsep_width == 0)
3077 {
3078 if (wp->w_width > 0) /* don't make it negative */
3079 --wp->w_width;
3080 wp->w_vsep_width = 1;
3081 }
3082 }
3083 else if (frp->fr_layout == FR_COL)
3084 {
3085 /* Handle all the frames in the column. */
3086 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3087 frame_add_vsep(frp);
3088 }
3089 else /* frp->fr_layout == FR_ROW */
3090 {
3091 /* Only need to handle the last frame in the row. */
3092 frp = frp->fr_child;
3093 while (frp->fr_next != NULL)
3094 frp = frp->fr_next;
3095 frame_add_vsep(frp);
3096 }
3097}
3098
3099/*
3100 * Set frame width from the window it contains.
3101 */
3102 static void
3103frame_fix_width(wp)
3104 win_T *wp;
3105{
3106 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3107}
3108#endif
3109
3110/*
3111 * Set frame height from the window it contains.
3112 */
3113 static void
3114frame_fix_height(wp)
3115 win_T *wp;
3116{
3117 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3118}
3119
3120/*
3121 * Compute the minimal height for frame "topfrp".
3122 * Uses the 'winminheight' option.
3123 * When "next_curwin" isn't NULL, use p_wh for this window.
3124 * When "next_curwin" is NOWIN, don't use at least one line for the current
3125 * window.
3126 */
3127 static int
3128frame_minheight(topfrp, next_curwin)
3129 frame_T *topfrp;
3130 win_T *next_curwin;
3131{
3132 frame_T *frp;
3133 int m;
3134#ifdef FEAT_VERTSPLIT
3135 int n;
3136#endif
3137
3138 if (topfrp->fr_win != NULL)
3139 {
3140 if (topfrp->fr_win == next_curwin)
3141 m = p_wh + topfrp->fr_win->w_status_height;
3142 else
3143 {
3144 /* window: minimal height of the window plus status line */
3145 m = p_wmh + topfrp->fr_win->w_status_height;
3146 /* Current window is minimal one line high */
3147 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3148 ++m;
3149 }
3150 }
3151#ifdef FEAT_VERTSPLIT
3152 else if (topfrp->fr_layout == FR_ROW)
3153 {
3154 /* get the minimal height from each frame in this row */
3155 m = 0;
3156 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3157 {
3158 n = frame_minheight(frp, next_curwin);
3159 if (n > m)
3160 m = n;
3161 }
3162 }
3163#endif
3164 else
3165 {
3166 /* Add up the minimal heights for all frames in this column. */
3167 m = 0;
3168 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3169 m += frame_minheight(frp, next_curwin);
3170 }
3171
3172 return m;
3173}
3174
3175#ifdef FEAT_VERTSPLIT
3176/*
3177 * Compute the minimal width for frame "topfrp".
3178 * When "next_curwin" isn't NULL, use p_wiw for this window.
3179 * When "next_curwin" is NOWIN, don't use at least one column for the current
3180 * window.
3181 */
3182 static int
3183frame_minwidth(topfrp, next_curwin)
3184 frame_T *topfrp;
3185 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3186{
3187 frame_T *frp;
3188 int m, n;
3189
3190 if (topfrp->fr_win != NULL)
3191 {
3192 if (topfrp->fr_win == next_curwin)
3193 m = p_wiw + topfrp->fr_win->w_vsep_width;
3194 else
3195 {
3196 /* window: minimal width of the window plus separator column */
3197 m = p_wmw + topfrp->fr_win->w_vsep_width;
3198 /* Current window is minimal one column wide */
3199 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3200 ++m;
3201 }
3202 }
3203 else if (topfrp->fr_layout == FR_COL)
3204 {
3205 /* get the minimal width from each frame in this column */
3206 m = 0;
3207 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3208 {
3209 n = frame_minwidth(frp, next_curwin);
3210 if (n > m)
3211 m = n;
3212 }
3213 }
3214 else
3215 {
3216 /* Add up the minimal widths for all frames in this row. */
3217 m = 0;
3218 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3219 m += frame_minwidth(frp, next_curwin);
3220 }
3221
3222 return m;
3223}
3224#endif
3225
3226
3227/*
3228 * Try to close all windows except current one.
3229 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3230 * used and the buffer was modified.
3231 *
3232 * Used by ":bdel" and ":only".
3233 */
3234 void
3235close_others(message, forceit)
3236 int message;
3237 int forceit; /* always hide all other windows */
3238{
3239 win_T *wp;
3240 win_T *nextwp;
3241 int r;
3242
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003243 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 {
3245 if (message
3246#ifdef FEAT_AUTOCMD
3247 && !autocmd_busy
3248#endif
3249 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003250 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 return;
3252 }
3253
3254 /* Be very careful here: autocommands may change the window layout. */
3255 for (wp = firstwin; win_valid(wp); wp = nextwp)
3256 {
3257 nextwp = wp->w_next;
3258 if (wp != curwin) /* don't close current window */
3259 {
3260
3261 /* Check if it's allowed to abandon this window */
3262 r = can_abandon(wp->w_buffer, forceit);
3263#ifdef FEAT_AUTOCMD
3264 if (!win_valid(wp)) /* autocommands messed wp up */
3265 {
3266 nextwp = firstwin;
3267 continue;
3268 }
3269#endif
3270 if (!r)
3271 {
3272#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3273 if (message && (p_confirm || cmdmod.confirm) && p_write)
3274 {
3275 dialog_changed(wp->w_buffer, FALSE);
3276# ifdef FEAT_AUTOCMD
3277 if (!win_valid(wp)) /* autocommands messed wp up */
3278 {
3279 nextwp = firstwin;
3280 continue;
3281 }
3282# endif
3283 }
3284 if (bufIsChanged(wp->w_buffer))
3285#endif
3286 continue;
3287 }
3288 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3289 }
3290 }
3291
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 EMSG(_("E445: Other window contains changes"));
3294}
3295
3296#endif /* FEAT_WINDOWS */
3297
3298/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003299 * Init the current window "curwin".
3300 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
3302 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003303curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003305 win_init_empty(curwin);
3306}
3307
3308 void
3309win_init_empty(wp)
3310 win_T *wp;
3311{
3312 redraw_win_later(wp, NOT_VALID);
3313 wp->w_lines_valid = 0;
3314 wp->w_cursor.lnum = 1;
3315 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003317 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003319 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3320 wp->w_pcmark.col = 0;
3321 wp->w_prev_pcmark.lnum = 0;
3322 wp->w_prev_pcmark.col = 0;
3323 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003325 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003327 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003329 if (wp->w_p_rl)
3330 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003332 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003334#ifdef FEAT_SYN_HL
3335 wp->w_s = &wp->w_buffer->b_s;
3336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
3339/*
3340 * Allocate the first window and put an empty buffer in it.
3341 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345win_alloc_first()
3346{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003348 return FAIL;
3349
3350#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003351 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 if (first_tabpage == NULL)
3353 return FAIL;
3354 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003355 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003356#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358 return OK;
3359}
3360
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003361#if defined(FEAT_AUTOCMD) || defined(PROTO)
3362/*
3363 * Init "aucmd_win". This can only be done after the first
3364 * window is fully initialized, thus it can't be in win_alloc_first().
3365 */
3366 void
3367win_alloc_aucmd_win()
3368{
3369 aucmd_win = win_alloc(NULL, TRUE);
3370 if (aucmd_win != NULL)
3371 {
3372 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003373 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003374 new_frame(aucmd_win);
3375 }
3376}
3377#endif
3378
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003380 * Allocate the first window or the first window in a new tab page.
3381 * When "oldwin" is NULL create an empty buffer for it.
3382 * When "oldwin" is not NULL copy info from it to the new window (only with
3383 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384 * Return FAIL when something goes wrong (out of memory).
3385 */
3386 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003387win_alloc_firstwin(oldwin)
3388 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003390 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003391 if (oldwin == NULL)
3392 {
3393 /* Very first window, need to create an empty buffer for it and
3394 * initialize from scratch. */
3395 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3396 if (curwin == NULL || curbuf == NULL)
3397 return FAIL;
3398 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003399#ifdef FEAT_SYN_HL
3400 curwin->w_s = &(curbuf->b_s);
3401#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003402 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003404 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003406 curwin_init(); /* init current window */
3407 }
3408#ifdef FEAT_WINDOWS
3409 else
3410 {
3411 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003412 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003413
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003414 /* We don't want cursor- and scroll-binding in the first window. */
3415 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003416 }
3417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003419 new_frame(curwin);
3420 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003422 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#ifdef FEAT_VERTSPLIT
3424 topframe->fr_width = Columns;
3425#endif
3426 topframe->fr_height = Rows - p_ch;
3427 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428
3429 return OK;
3430}
3431
3432/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003433 * Create a frame for window "wp".
3434 */
3435 static void
3436new_frame(win_T *wp)
3437{
3438 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3439
3440 wp->w_frame = frp;
3441 if (frp != NULL)
3442 {
3443 frp->fr_layout = FR_LEAF;
3444 frp->fr_win = wp;
3445 }
3446}
3447
3448/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003449 * Initialize the window and frame size to the maximum.
3450 */
3451 void
3452win_init_size()
3453{
3454 firstwin->w_height = ROWS_AVAIL;
3455 topframe->fr_height = ROWS_AVAIL;
3456#ifdef FEAT_VERTSPLIT
3457 firstwin->w_width = Columns;
3458 topframe->fr_width = Columns;
3459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
3462#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003463
3464/*
3465 * Allocate a new tabpage_T and init the values.
3466 * Returns NULL when out of memory.
3467 */
3468 static tabpage_T *
3469alloc_tabpage()
3470{
3471 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003472# ifdef FEAT_GUI
3473 int i;
3474# endif
3475
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003476
3477 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003478 if (tp == NULL)
3479 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003480
Bram Moolenaar429fa852013-04-15 12:27:36 +02003481# ifdef FEAT_EVAL
3482 /* init t: variables */
3483 tp->tp_vars = dict_alloc();
3484 if (tp->tp_vars == NULL)
3485 {
3486 vim_free(tp);
3487 return NULL;
3488 }
3489 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3490# endif
3491
3492# ifdef FEAT_GUI
3493 for (i = 0; i < 3; i++)
3494 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003495# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003496# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003497 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003499 tp->tp_ch_used = p_ch;
3500
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003501 return tp;
3502}
3503
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003504 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003505free_tabpage(tp)
3506 tabpage_T *tp;
3507{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003508 int idx;
3509
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003510# ifdef FEAT_DIFF
3511 diff_clear(tp);
3512# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003513 for (idx = 0; idx < SNAP_COUNT; ++idx)
3514 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003515#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003516 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3517 hash_init(&tp->tp_vars->dv_hashtab);
3518 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003519#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003520
3521#ifdef FEAT_PYTHON
3522 python_tabpage_free(tp);
3523#endif
3524
3525#ifdef FEAT_PYTHON3
3526 python3_tabpage_free(tp);
3527#endif
3528
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003529 vim_free(tp);
3530}
3531
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003533 * Create a new Tab page with one window.
3534 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003535 * When "after" is 0 put it just after the current Tab page.
3536 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537 * Return FAIL or OK.
3538 */
3539 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003540win_new_tabpage(after)
3541 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003545 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003546
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003547 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548 if (newtp == NULL)
3549 return FAIL;
3550
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003551 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003552 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003553 {
3554 vim_free(newtp);
3555 return FAIL;
3556 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003557 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558
3559 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003560 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003561 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003563 if (after == 1)
3564 {
3565 /* New tab page becomes the first one. */
3566 newtp->tp_next = first_tabpage;
3567 first_tabpage = newtp;
3568 }
3569 else
3570 {
3571 if (after > 0)
3572 {
3573 /* Put new tab page before tab page "after". */
3574 n = 2;
3575 for (tp = first_tabpage; tp->tp_next != NULL
3576 && n < after; tp = tp->tp_next)
3577 ++n;
3578 }
3579 newtp->tp_next = tp->tp_next;
3580 tp->tp_next = newtp;
3581 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003584 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585
3586 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003587 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003588
3589#if defined(FEAT_GUI)
3590 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3591 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003592 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003593#endif
3594
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003596#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003597 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003598 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003599#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 return OK;
3601 }
3602
3603 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003604 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003605 return FAIL;
3606}
3607
3608/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003609 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3610 * like with ":split".
3611 * Returns OK if a new tab page was created, FAIL otherwise.
3612 */
3613 int
3614may_open_tabpage()
3615{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003616 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003617
Bram Moolenaard326ce82007-03-11 14:48:29 +00003618 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003619 {
3620 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003621 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003622 return win_new_tabpage(n);
3623 }
3624 return FAIL;
3625}
3626
3627/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003628 * Create up to "maxcount" tabpages with empty windows.
3629 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003630 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003631 int
3632make_tabpages(maxcount)
3633 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003634{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003635 int count = maxcount;
3636 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003637
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003638 /* Limit to 'tabpagemax' tabs. */
3639 if (count > p_tpm)
3640 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003641
3642#ifdef FEAT_AUTOCMD
3643 /*
3644 * Don't execute autocommands while creating the tab pages. Must do that
3645 * when putting the buffers in the windows.
3646 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003647 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003648#endif
3649
3650 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003651 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003653
3654#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003655 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003656#endif
3657
3658 /* return actual number of tab pages */
3659 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003660}
3661
3662/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003663 * Return TRUE when "tpc" points to a valid tab page.
3664 */
3665 int
3666valid_tabpage(tpc)
3667 tabpage_T *tpc;
3668{
3669 tabpage_T *tp;
3670
3671 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3672 if (tp == tpc)
3673 return TRUE;
3674 return FALSE;
3675}
3676
3677/*
3678 * Find tab page "n" (first one is 1). Returns NULL when not found.
3679 */
3680 tabpage_T *
3681find_tabpage(n)
3682 int n;
3683{
3684 tabpage_T *tp;
3685 int i = 1;
3686
3687 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3688 ++i;
3689 return tp;
3690}
3691
3692/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003693 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003694 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695 */
3696 int
3697tabpage_index(ftp)
3698 tabpage_T *ftp;
3699{
3700 int i = 1;
3701 tabpage_T *tp;
3702
3703 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3704 ++i;
3705 return i;
3706}
3707
3708/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003709 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003710 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003711 * FAIL.
3712 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003714 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003715leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003716 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003717 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003718 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003720 tabpage_T *tp = curtab;
3721
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003722#ifdef FEAT_VISUAL
3723 reset_VIsual_and_resel(); /* stop Visual mode */
3724#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003725#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003726 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003727 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003728 if (new_curbuf != curbuf)
3729 {
3730 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3731 if (curtab != tp)
3732 return FAIL;
3733 }
3734 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3735 if (curtab != tp)
3736 return FAIL;
3737 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003738 if (curtab != tp)
3739 return FAIL;
3740 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003741#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003742#if defined(FEAT_GUI)
3743 /* Remove the scrollbars. They may be added back later. */
3744 if (gui.in_use)
3745 gui_remove_scrollbars();
3746#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003747 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003748 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 tp->tp_firstwin = firstwin;
3750 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003751 tp->tp_old_Rows = Rows;
3752 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003753 firstwin = NULL;
3754 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756}
3757
3758/*
3759 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003760 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003761 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3762 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003765enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003766 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003767 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 int trigger_enter_autocmds UNUSED;
3769 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003771 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003772 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003773
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003774 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775 firstwin = tp->tp_firstwin;
3776 lastwin = tp->tp_lastwin;
3777 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003778
3779 /* We would like doing the TabEnter event first, but we don't have a
3780 * valid current window yet, which may break some commands.
3781 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003782 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3783 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003784 prevwin = next_prevwin;
3785
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003786 last_status(FALSE); /* status line may appear or disappear */
3787 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003788 must_redraw = CLEAR; /* need to redraw everything */
3789#ifdef FEAT_DIFF
3790 diff_need_scrollbind = TRUE;
3791#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003792
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003793 /* The tabpage line may have appeared or disappeared, may need to resize
3794 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003795 * frame sizes too. Use the stored value of p_ch, so that it can be
3796 * different for each tab page. */
3797 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003798 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3799#ifdef FEAT_GUI_TABLINE
3800 && !gui_use_tabline()
3801#endif
3802 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003803 shell_new_rows();
3804#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003805 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003806 shell_new_columns(); /* update window widths */
3807#endif
3808
3809#if defined(FEAT_GUI)
3810 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3811 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003812 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813#endif
3814
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003815#ifdef FEAT_AUTOCMD
3816 /* Apply autocommands after updating the display, when 'rows' and
3817 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003818 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003819 {
3820 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3821 if (old_curbuf != curbuf)
3822 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3823 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003824#endif
3825
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826 redraw_all_later(CLEAR);
3827}
3828
3829/*
3830 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003831 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003832 */
3833 void
3834goto_tabpage(n)
3835 int n;
3836{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003838 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003839 int i;
3840
Bram Moolenaard68071d2006-05-02 22:08:30 +00003841 if (text_locked())
3842 {
3843 /* Not allowed when editing the command line. */
3844#ifdef FEAT_CMDWIN
3845 if (cmdwin_type != 0)
3846 EMSG(_(e_cmdwin));
3847 else
3848#endif
3849 EMSG(_(e_secure));
3850 return;
3851 }
3852
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003853 /* If there is only one it can't work. */
3854 if (first_tabpage->tp_next == NULL)
3855 {
3856 if (n > 1)
3857 beep_flush();
3858 return;
3859 }
3860
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003861 if (n == 0)
3862 {
3863 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003864 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003865 tp = first_tabpage;
3866 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003867 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003868 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003869 else if (n < 0)
3870 {
3871 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3872 * this N times. */
3873 ttp = curtab;
3874 for (i = n; i < 0; ++i)
3875 {
3876 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3877 tp = tp->tp_next)
3878 ;
3879 ttp = tp;
3880 }
3881 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003882 else if (n == 9999)
3883 {
3884 /* Go to last tab page. */
3885 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3886 ;
3887 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003888 else
3889 {
3890 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003891 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003892 if (tp == NULL)
3893 {
3894 beep_flush();
3895 return;
3896 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003897 }
3898
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003899 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003900
3901#ifdef FEAT_GUI_TABLINE
3902 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003903 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003904#endif
3905}
3906
3907/*
3908 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003909 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3910 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003911 * Note: doesn't update the GUI tab.
3912 */
3913 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003914goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003915 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003916 int trigger_enter_autocmds;
3917 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003918{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003919 /* Don't repeat a message in another tab page. */
3920 set_keep_msg(NULL, 0);
3921
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003922 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3923 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 {
3925 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003926 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3927 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003928 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003929 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3930 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003931 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003935 * Enter window "wp" in tab page "tp".
3936 * Also updates the GUI tab.
3937 */
3938 void
3939goto_tabpage_win(tp, wp)
3940 tabpage_T *tp;
3941 win_T *wp;
3942{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003943 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003944 if (curtab == tp && win_valid(wp))
3945 {
3946 win_enter(wp, TRUE);
3947# ifdef FEAT_GUI_TABLINE
3948 if (gui_use_tabline())
3949 gui_mch_set_curtab(tabpage_index(curtab));
3950# endif
3951 }
3952}
3953
3954/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003955 * Move the current tab page to before tab page "nr".
3956 */
3957 void
3958tabpage_move(nr)
3959 int nr;
3960{
3961 int n = nr;
3962 tabpage_T *tp;
3963
3964 if (first_tabpage->tp_next == NULL)
3965 return;
3966
3967 /* Remove the current tab page from the list of tab pages. */
3968 if (curtab == first_tabpage)
3969 first_tabpage = curtab->tp_next;
3970 else
3971 {
3972 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3973 if (tp->tp_next == curtab)
3974 break;
3975 if (tp == NULL) /* "cannot happen" */
3976 return;
3977 tp->tp_next = curtab->tp_next;
3978 }
3979
3980 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003981 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003982 {
3983 curtab->tp_next = first_tabpage;
3984 first_tabpage = curtab;
3985 }
3986 else
3987 {
3988 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3989 --n;
3990 curtab->tp_next = tp->tp_next;
3991 tp->tp_next = curtab;
3992 }
3993
3994 /* Need to redraw the tabline. Tab page contents doesn't change. */
3995 redraw_tabline = TRUE;
3996}
3997
3998
3999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Go to another window.
4001 * When jumping to another buffer, stop Visual mode. Do this before
4002 * changing windows so we can yank the selection into the '*' register.
4003 * When jumping to another window on the same buffer, adjust its cursor
4004 * position to keep the same Visual area.
4005 */
4006 void
4007win_goto(wp)
4008 win_T *wp;
4009{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004010#ifdef FEAT_CONCEAL
4011 win_T *owp = curwin;
4012#endif
4013
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004014 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004017 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return;
4019 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_AUTOCMD
4021 if (curbuf_locked())
4022 return;
4023#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#ifdef FEAT_VISUAL
4026 if (wp->w_buffer != curbuf)
4027 reset_VIsual_and_resel();
4028 else if (VIsual_active)
4029 wp->w_cursor = curwin->w_cursor;
4030#endif
4031
4032#ifdef FEAT_GUI
4033 need_mouse_correct = TRUE;
4034#endif
4035 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004036
4037#ifdef FEAT_CONCEAL
4038 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004039 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004040 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004041 if (curwin->w_p_cole > 0 && !msg_scrolled)
4042 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044}
4045
4046#if defined(FEAT_PERL) || defined(PROTO)
4047/*
4048 * Find window number "winnr" (counting top to bottom).
4049 */
4050 win_T *
4051win_find_nr(winnr)
4052 int winnr;
4053{
4054 win_T *wp;
4055
4056# ifdef FEAT_WINDOWS
4057 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4058 if (--winnr == 0)
4059 break;
4060 return wp;
4061# else
4062 return curwin;
4063# endif
4064}
4065#endif
4066
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004067#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4068 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004069/*
4070 * Find the tabpage for window "win".
4071 */
4072 tabpage_T *
4073win_find_tabpage(win)
4074 win_T *win;
4075{
4076 win_T *wp;
4077 tabpage_T *tp;
4078
4079 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4080 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4081 if (wp == win)
4082 return tp;
4083 return NULL;
4084}
4085#endif
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_VERTSPLIT
4088/*
4089 * Move to window above or below "count" times.
4090 */
4091 static void
4092win_goto_ver(up, count)
4093 int up; /* TRUE to go to win above */
4094 long count;
4095{
4096 frame_T *fr;
4097 frame_T *nfr;
4098 frame_T *foundfr;
4099
4100 foundfr = curwin->w_frame;
4101 while (count--)
4102 {
4103 /*
4104 * First go upwards in the tree of frames until we find a upwards or
4105 * downwards neighbor.
4106 */
4107 fr = foundfr;
4108 for (;;)
4109 {
4110 if (fr == topframe)
4111 goto end;
4112 if (up)
4113 nfr = fr->fr_prev;
4114 else
4115 nfr = fr->fr_next;
4116 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4117 break;
4118 fr = fr->fr_parent;
4119 }
4120
4121 /*
4122 * Now go downwards to find the bottom or top frame in it.
4123 */
4124 for (;;)
4125 {
4126 if (nfr->fr_layout == FR_LEAF)
4127 {
4128 foundfr = nfr;
4129 break;
4130 }
4131 fr = nfr->fr_child;
4132 if (nfr->fr_layout == FR_ROW)
4133 {
4134 /* Find the frame at the cursor row. */
4135 while (fr->fr_next != NULL
4136 && frame2win(fr)->w_wincol + fr->fr_width
4137 <= curwin->w_wincol + curwin->w_wcol)
4138 fr = fr->fr_next;
4139 }
4140 if (nfr->fr_layout == FR_COL && up)
4141 while (fr->fr_next != NULL)
4142 fr = fr->fr_next;
4143 nfr = fr;
4144 }
4145 }
4146end:
4147 if (foundfr != NULL)
4148 win_goto(foundfr->fr_win);
4149}
4150
4151/*
4152 * Move to left or right window.
4153 */
4154 static void
4155win_goto_hor(left, count)
4156 int left; /* TRUE to go to left win */
4157 long count;
4158{
4159 frame_T *fr;
4160 frame_T *nfr;
4161 frame_T *foundfr;
4162
4163 foundfr = curwin->w_frame;
4164 while (count--)
4165 {
4166 /*
4167 * First go upwards in the tree of frames until we find a left or
4168 * right neighbor.
4169 */
4170 fr = foundfr;
4171 for (;;)
4172 {
4173 if (fr == topframe)
4174 goto end;
4175 if (left)
4176 nfr = fr->fr_prev;
4177 else
4178 nfr = fr->fr_next;
4179 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4180 break;
4181 fr = fr->fr_parent;
4182 }
4183
4184 /*
4185 * Now go downwards to find the leftmost or rightmost frame in it.
4186 */
4187 for (;;)
4188 {
4189 if (nfr->fr_layout == FR_LEAF)
4190 {
4191 foundfr = nfr;
4192 break;
4193 }
4194 fr = nfr->fr_child;
4195 if (nfr->fr_layout == FR_COL)
4196 {
4197 /* Find the frame at the cursor row. */
4198 while (fr->fr_next != NULL
4199 && frame2win(fr)->w_winrow + fr->fr_height
4200 <= curwin->w_winrow + curwin->w_wrow)
4201 fr = fr->fr_next;
4202 }
4203 if (nfr->fr_layout == FR_ROW && left)
4204 while (fr->fr_next != NULL)
4205 fr = fr->fr_next;
4206 nfr = fr;
4207 }
4208 }
4209end:
4210 if (foundfr != NULL)
4211 win_goto(foundfr->fr_win);
4212}
4213#endif
4214
4215/*
4216 * Make window "wp" the current window.
4217 */
4218 void
4219win_enter(wp, undo_sync)
4220 win_T *wp;
4221 int undo_sync;
4222{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004223 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224}
4225
4226/*
4227 * Make window wp the current window.
4228 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4229 * been closed and isn't valid.
4230 */
4231 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004232win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 win_T *wp;
4234 int undo_sync;
4235 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004236 int trigger_enter_autocmds UNUSED;
4237 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239#ifdef FEAT_AUTOCMD
4240 int other_buffer = FALSE;
4241#endif
4242
4243 if (wp == curwin && !curwin_invalid) /* nothing to do */
4244 return;
4245
4246#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004247 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 /*
4250 * Be careful: If autocommands delete the window, return now.
4251 */
4252 if (wp->w_buffer != curbuf)
4253 {
4254 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4255 other_buffer = TRUE;
4256 if (!win_valid(wp))
4257 return;
4258 }
4259 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4260 if (!win_valid(wp))
4261 return;
4262# ifdef FEAT_EVAL
4263 /* autocmds may abort script processing */
4264 if (aborting())
4265 return;
4266# endif
4267 }
4268#endif
4269
4270 /* sync undo before leaving the current buffer */
4271 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004272 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 /* may have to copy the buffer options when 'cpo' contains 'S' */
4274 if (wp->w_buffer != curbuf)
4275 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4276 if (!curwin_invalid)
4277 {
4278 prevwin = curwin; /* remember for CTRL-W p */
4279 curwin->w_redr_status = TRUE;
4280 }
4281 curwin = wp;
4282 curbuf = wp->w_buffer;
4283 check_cursor();
4284#ifdef FEAT_VIRTUALEDIT
4285 if (!virtual_active())
4286 curwin->w_cursor.coladd = 0;
4287#endif
4288 changed_line_abv_curs(); /* assume cursor position needs updating */
4289
4290 if (curwin->w_localdir != NULL)
4291 {
4292 /* Window has a local directory: Save current directory as global
4293 * directory (unless that was done already) and change to the local
4294 * directory. */
4295 if (globaldir == NULL)
4296 {
4297 char_u cwd[MAXPATHL];
4298
4299 if (mch_dirname(cwd, MAXPATHL) == OK)
4300 globaldir = vim_strsave(cwd);
4301 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004302 if (mch_chdir((char *)curwin->w_localdir) == 0)
4303 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 else if (globaldir != NULL)
4306 {
4307 /* Window doesn't have a local directory and we are not in the global
4308 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004309 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 vim_free(globaldir);
4311 globaldir = NULL;
4312 shorten_fnames(TRUE);
4313 }
4314
4315#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004316 if (trigger_enter_autocmds)
4317 {
4318 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4319 if (other_buffer)
4320 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322#endif
4323
4324#ifdef FEAT_TITLE
4325 maketitle();
4326#endif
4327 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004328 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (restart_edit)
4330 redraw_later(VALID); /* causes status line redraw */
4331
4332 /* set window height to desired minimal value */
4333 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4334 win_setheight((int)p_wh);
4335 else if (curwin->w_height == 0)
4336 win_setheight(1);
4337
4338#ifdef FEAT_VERTSPLIT
4339 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004340 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 win_setwidth((int)p_wiw);
4342#endif
4343
4344#ifdef FEAT_MOUSE
4345 setmouse(); /* in case jumped to/from help buffer */
4346#endif
4347
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004348 /* Change directories when the 'acd' option is set. */
4349 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350}
4351
4352#endif /* FEAT_WINDOWS */
4353
4354#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4355/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004356 * Jump to the first open window that contains buffer "buf", if one exists.
4357 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 */
4359 win_T *
4360buf_jump_open_win(buf)
4361 buf_T *buf;
4362{
4363# ifdef FEAT_WINDOWS
4364 win_T *wp;
4365
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004366 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 if (wp->w_buffer == buf)
4368 break;
4369 if (wp != NULL)
4370 win_enter(wp, FALSE);
4371 return wp;
4372# else
4373 if (curwin->w_buffer == buf)
4374 return curwin;
4375 return NULL;
4376# endif
4377}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004378
4379/*
4380 * Jump to the first open window in any tab page that contains buffer "buf",
4381 * if one exists.
4382 * Returns a pointer to the window found, otherwise NULL.
4383 */
4384 win_T *
4385buf_jump_open_tab(buf)
4386 buf_T *buf;
4387{
4388# ifdef FEAT_WINDOWS
4389 win_T *wp;
4390 tabpage_T *tp;
4391
4392 /* First try the current tab page. */
4393 wp = buf_jump_open_win(buf);
4394 if (wp != NULL)
4395 return wp;
4396
4397 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4398 if (tp != curtab)
4399 {
4400 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4401 if (wp->w_buffer == buf)
4402 break;
4403 if (wp != NULL)
4404 {
4405 goto_tabpage_win(tp, wp);
4406 if (curwin != wp)
4407 wp = NULL; /* something went wrong */
4408 break;
4409 }
4410 }
4411
4412 return wp;
4413# else
4414 if (curwin->w_buffer == buf)
4415 return curwin;
4416 return NULL;
4417# endif
4418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420
4421/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004422 * Allocate a window structure and link it in the window list when "hidden" is
4423 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004426win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004427 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004428 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004430 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * allocate window structure and linesizes arrays
4434 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004435 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004436 if (new_wp == NULL)
4437 return NULL;
4438
4439 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004441 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004442 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444
Bram Moolenaar429fa852013-04-15 12:27:36 +02004445#ifdef FEAT_EVAL
4446 /* init w: variables */
4447 new_wp->w_vars = dict_alloc();
4448 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004450 win_free_lsize(new_wp);
4451 vim_free(new_wp);
4452 return NULL;
4453 }
4454 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004455#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004456
4457#ifdef FEAT_AUTOCMD
4458 /* Don't execute autocommands while the window is not properly
4459 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4460 * event. */
4461 block_autocmds();
4462#endif
4463 /*
4464 * link the window in the window list
4465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004467 if (!hidden)
4468 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469#endif
4470#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 new_wp->w_wincol = 0;
4472 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 /* position the display and the cursor at the top of the file. */
4476 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004478 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 new_wp->w_botline = 2;
4481 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#endif
4485
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 /* We won't calculate w_fraction until resizing the window */
4487 new_wp->w_fraction = 0;
4488 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004491 if (gui.in_use)
4492 {
4493 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4494 SBAR_LEFT, new_wp);
4495 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4496 SBAR_RIGHT, new_wp);
4497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#endif
4499#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004500 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004502#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004504#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004505#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 new_wp->w_match_head = NULL;
4507 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004508#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004509 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510}
4511
4512#if defined(FEAT_WINDOWS) || defined(PROTO)
4513
4514/*
4515 * remove window 'wp' from the window list and free the structure
4516 */
4517 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004518win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004520 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
4522 int i;
4523
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004524#ifdef FEAT_FOLDING
4525 clearFolding(wp);
4526#endif
4527
4528 /* reduce the reference count to the argument list. */
4529 alist_unlink(wp->w_alist);
4530
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004531#ifdef FEAT_AUTOCMD
4532 /* Don't execute autocommands while the window is halfway being deleted.
4533 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004534 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004535#endif
4536
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004537#ifdef FEAT_LUA
4538 lua_window_free(wp);
4539#endif
4540
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004541#ifdef FEAT_MZSCHEME
4542 mzscheme_window_free(wp);
4543#endif
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#ifdef FEAT_PERL
4546 perl_win_free(wp);
4547#endif
4548
4549#ifdef FEAT_PYTHON
4550 python_window_free(wp);
4551#endif
4552
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004553#ifdef FEAT_PYTHON3
4554 python3_window_free(wp);
4555#endif
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#ifdef FEAT_TCL
4558 tcl_window_free(wp);
4559#endif
4560
4561#ifdef FEAT_RUBY
4562 ruby_window_free(wp);
4563#endif
4564
4565 clear_winopt(&wp->w_onebuf_opt);
4566 clear_winopt(&wp->w_allbuf_opt);
4567
4568#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004569 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4570 hash_init(&wp->w_vars->dv_hashtab);
4571 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#endif
4573
4574 if (prevwin == wp)
4575 prevwin = NULL;
4576 win_free_lsize(wp);
4577
4578 for (i = 0; i < wp->w_tagstacklen; ++i)
4579 vim_free(wp->w_tagstack[i].tagname);
4580
4581 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004584 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#ifdef FEAT_JUMPLIST
4588 free_jumplist(wp);
4589#endif
4590
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004591#ifdef FEAT_QUICKFIX
4592 qf_free_all(wp);
4593#endif
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_GUI
4596 if (gui.in_use)
4597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4599 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4600 }
4601#endif /* FEAT_GUI */
4602
Bram Moolenaar860cae12010-06-05 23:22:07 +02004603#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004604 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004605#endif
4606
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004607#ifdef FEAT_AUTOCMD
4608 if (wp != aucmd_win)
4609#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004610 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004612
4613#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004614 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616}
4617
4618/*
4619 * Append window "wp" in the window list after window "after".
4620 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004621 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622win_append(after, wp)
4623 win_T *after, *wp;
4624{
4625 win_T *before;
4626
4627 if (after == NULL) /* after NULL is in front of the first */
4628 before = firstwin;
4629 else
4630 before = after->w_next;
4631
4632 wp->w_next = before;
4633 wp->w_prev = after;
4634 if (after == NULL)
4635 firstwin = wp;
4636 else
4637 after->w_next = wp;
4638 if (before == NULL)
4639 lastwin = wp;
4640 else
4641 before->w_prev = wp;
4642}
4643
4644/*
4645 * Remove a window from the window list.
4646 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004647 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004648win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004650 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
4652 if (wp->w_prev != NULL)
4653 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004654 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004656 else
4657 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (wp->w_next != NULL)
4659 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004660 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004662 else
4663 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664}
4665
4666/*
4667 * Append frame "frp" in a frame list after frame "after".
4668 */
4669 static void
4670frame_append(after, frp)
4671 frame_T *after, *frp;
4672{
4673 frp->fr_next = after->fr_next;
4674 after->fr_next = frp;
4675 if (frp->fr_next != NULL)
4676 frp->fr_next->fr_prev = frp;
4677 frp->fr_prev = after;
4678}
4679
4680/*
4681 * Insert frame "frp" in a frame list before frame "before".
4682 */
4683 static void
4684frame_insert(before, frp)
4685 frame_T *before, *frp;
4686{
4687 frp->fr_next = before;
4688 frp->fr_prev = before->fr_prev;
4689 before->fr_prev = frp;
4690 if (frp->fr_prev != NULL)
4691 frp->fr_prev->fr_next = frp;
4692 else
4693 frp->fr_parent->fr_child = frp;
4694}
4695
4696/*
4697 * Remove a frame from a frame list.
4698 */
4699 static void
4700frame_remove(frp)
4701 frame_T *frp;
4702{
4703 if (frp->fr_prev != NULL)
4704 frp->fr_prev->fr_next = frp->fr_next;
4705 else
4706 frp->fr_parent->fr_child = frp->fr_next;
4707 if (frp->fr_next != NULL)
4708 frp->fr_next->fr_prev = frp->fr_prev;
4709}
4710
4711#endif /* FEAT_WINDOWS */
4712
4713/*
4714 * Allocate w_lines[] for window "wp".
4715 * Return FAIL for failure, OK for success.
4716 */
4717 int
4718win_alloc_lines(wp)
4719 win_T *wp;
4720{
4721 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004722 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (wp->w_lines == NULL)
4724 return FAIL;
4725 return OK;
4726}
4727
4728/*
4729 * free lsize arrays for a window
4730 */
4731 void
4732win_free_lsize(wp)
4733 win_T *wp;
4734{
4735 vim_free(wp->w_lines);
4736 wp->w_lines = NULL;
4737}
4738
4739/*
4740 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004741 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
4743 void
4744shell_new_rows()
4745{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004746 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 if (firstwin == NULL) /* not initialized yet */
4749 return;
4750#ifdef FEAT_WINDOWS
4751 if (h < frame_minheight(topframe, NULL))
4752 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004753
4754 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 * that doesn't result in the right height, forget about that option. */
4756 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004757 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 frame_new_height(topframe, h, FALSE, FALSE);
4759
4760 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4761#else
4762 if (h < 1)
4763 h = 1;
4764 win_new_height(firstwin, h);
4765#endif
4766 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004767#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004768 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004769#endif
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771#if 0
4772 /* Disabled: don't want making the screen smaller make a window larger. */
4773 if (p_ea)
4774 win_equal(curwin, FALSE, 'v');
4775#endif
4776}
4777
4778#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4779/*
4780 * Called from win_new_shellsize() after Columns changed.
4781 */
4782 void
4783shell_new_columns()
4784{
4785 if (firstwin == NULL) /* not initialized yet */
4786 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004787
4788 /* First try setting the widths of windows with 'winfixwidth'. If that
4789 * doesn't result in the right width, forget about that option. */
4790 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004791 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004792 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4795#if 0
4796 /* Disabled: don't want making the screen smaller make a window larger. */
4797 if (p_ea)
4798 win_equal(curwin, FALSE, 'h');
4799#endif
4800}
4801#endif
4802
4803#if defined(FEAT_CMDWIN) || defined(PROTO)
4804/*
4805 * Save the size of all windows in "gap".
4806 */
4807 void
4808win_size_save(gap)
4809 garray_T *gap;
4810
4811{
4812 win_T *wp;
4813
4814 ga_init2(gap, (int)sizeof(int), 1);
4815 if (ga_grow(gap, win_count() * 2) == OK)
4816 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4817 {
4818 ((int *)gap->ga_data)[gap->ga_len++] =
4819 wp->w_width + wp->w_vsep_width;
4820 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4821 }
4822}
4823
4824/*
4825 * Restore window sizes, but only if the number of windows is still the same.
4826 * Does not free the growarray.
4827 */
4828 void
4829win_size_restore(gap)
4830 garray_T *gap;
4831{
4832 win_T *wp;
4833 int i;
4834
4835 if (win_count() * 2 == gap->ga_len)
4836 {
4837 i = 0;
4838 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4839 {
4840 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4841 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4842 }
4843 /* recompute the window positions */
4844 (void)win_comp_pos();
4845 }
4846}
4847#endif /* FEAT_CMDWIN */
4848
4849#if defined(FEAT_WINDOWS) || defined(PROTO)
4850/*
4851 * Update the position for all windows, using the width and height of the
4852 * frames.
4853 * Returns the row just after the last window.
4854 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004855 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856win_comp_pos()
4857{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004858 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int col = 0;
4860
4861 frame_comp_pos(topframe, &row, &col);
4862 return row;
4863}
4864
4865/*
4866 * Update the position of the windows in frame "topfrp", using the width and
4867 * height of the frames.
4868 * "*row" and "*col" are the top-left position of the frame. They are updated
4869 * to the bottom-right position plus one.
4870 */
4871 static void
4872frame_comp_pos(topfrp, row, col)
4873 frame_T *topfrp;
4874 int *row;
4875 int *col;
4876{
4877 win_T *wp;
4878 frame_T *frp;
4879#ifdef FEAT_VERTSPLIT
4880 int startcol;
4881 int startrow;
4882#endif
4883
4884 wp = topfrp->fr_win;
4885 if (wp != NULL)
4886 {
4887 if (wp->w_winrow != *row
4888#ifdef FEAT_VERTSPLIT
4889 || wp->w_wincol != *col
4890#endif
4891 )
4892 {
4893 /* position changed, redraw */
4894 wp->w_winrow = *row;
4895#ifdef FEAT_VERTSPLIT
4896 wp->w_wincol = *col;
4897#endif
4898 redraw_win_later(wp, NOT_VALID);
4899 wp->w_redr_status = TRUE;
4900 }
4901 *row += wp->w_height + wp->w_status_height;
4902#ifdef FEAT_VERTSPLIT
4903 *col += wp->w_width + wp->w_vsep_width;
4904#endif
4905 }
4906 else
4907 {
4908#ifdef FEAT_VERTSPLIT
4909 startrow = *row;
4910 startcol = *col;
4911#endif
4912 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4913 {
4914#ifdef FEAT_VERTSPLIT
4915 if (topfrp->fr_layout == FR_ROW)
4916 *row = startrow; /* all frames are at the same row */
4917 else
4918 *col = startcol; /* all frames are at the same col */
4919#endif
4920 frame_comp_pos(frp, row, col);
4921 }
4922 }
4923}
4924
4925#endif /* FEAT_WINDOWS */
4926
4927/*
4928 * Set current window height and take care of repositioning other windows to
4929 * fit around it.
4930 */
4931 void
4932win_setheight(height)
4933 int height;
4934{
4935 win_setheight_win(height, curwin);
4936}
4937
4938/*
4939 * Set the window height of window "win" and take care of repositioning other
4940 * windows to fit around it.
4941 */
4942 void
4943win_setheight_win(height, win)
4944 int height;
4945 win_T *win;
4946{
4947 int row;
4948
4949 if (win == curwin)
4950 {
4951 /* Always keep current window at least one line high, even when
4952 * 'winminheight' is zero. */
4953#ifdef FEAT_WINDOWS
4954 if (height < p_wmh)
4955 height = p_wmh;
4956#endif
4957 if (height == 0)
4958 height = 1;
4959 }
4960
4961#ifdef FEAT_WINDOWS
4962 frame_setheight(win->w_frame, height + win->w_status_height);
4963
4964 /* recompute the window positions */
4965 row = win_comp_pos();
4966#else
4967 if (height > topframe->fr_height)
4968 height = topframe->fr_height;
4969 win->w_height = height;
4970 row = height;
4971#endif
4972
4973 /*
4974 * If there is extra space created between the last window and the command
4975 * line, clear it.
4976 */
4977 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4978 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4979 cmdline_row = row;
4980 msg_row = row;
4981 msg_col = 0;
4982
4983 redraw_all_later(NOT_VALID);
4984}
4985
4986#if defined(FEAT_WINDOWS) || defined(PROTO)
4987
4988/*
4989 * Set the height of a frame to "height" and take care that all frames and
4990 * windows inside it are resized. Also resize frames on the left and right if
4991 * the are in the same FR_ROW frame.
4992 *
4993 * Strategy:
4994 * If the frame is part of a FR_COL frame, try fitting the frame in that
4995 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4996 * go to containing frames to resize them and make room.
4997 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4998 * Check for the minimal height of the FR_ROW frame.
4999 * At the top level we can also use change the command line height.
5000 */
5001 static void
5002frame_setheight(curfrp, height)
5003 frame_T *curfrp;
5004 int height;
5005{
5006 int room; /* total number of lines available */
5007 int take; /* number of lines taken from other windows */
5008 int room_cmdline; /* lines available from cmdline */
5009 int run;
5010 frame_T *frp;
5011 int h;
5012 int room_reserved;
5013
5014 /* If the height already is the desired value, nothing to do. */
5015 if (curfrp->fr_height == height)
5016 return;
5017
5018 if (curfrp->fr_parent == NULL)
5019 {
5020 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005021 if (height > ROWS_AVAIL)
5022 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (height > 0)
5024 frame_new_height(curfrp, height, FALSE, FALSE);
5025 }
5026 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5027 {
5028 /* Row of frames: Also need to resize frames left and right of this
5029 * one. First check for the minimal height of these. */
5030 h = frame_minheight(curfrp->fr_parent, NULL);
5031 if (height < h)
5032 height = h;
5033 frame_setheight(curfrp->fr_parent, height);
5034 }
5035 else
5036 {
5037 /*
5038 * Column of frames: try to change only frames in this column.
5039 */
5040#ifdef FEAT_VERTSPLIT
5041 /*
5042 * Do this twice:
5043 * 1: compute room available, if it's not enough try resizing the
5044 * containing frame.
5045 * 2: compute the room available and adjust the height to it.
5046 * Try not to reduce the height of a window with 'winfixheight' set.
5047 */
5048 for (run = 1; run <= 2; ++run)
5049#else
5050 for (;;)
5051#endif
5052 {
5053 room = 0;
5054 room_reserved = 0;
5055 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5056 frp = frp->fr_next)
5057 {
5058 if (frp != curfrp
5059 && frp->fr_win != NULL
5060 && frp->fr_win->w_p_wfh)
5061 room_reserved += frp->fr_height;
5062 room += frp->fr_height;
5063 if (frp != curfrp)
5064 room -= frame_minheight(frp, NULL);
5065 }
5066#ifdef FEAT_VERTSPLIT
5067 if (curfrp->fr_width != Columns)
5068 room_cmdline = 0;
5069 else
5070#endif
5071 {
5072 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5073 + lastwin->w_height + lastwin->w_status_height);
5074 if (room_cmdline < 0)
5075 room_cmdline = 0;
5076 }
5077
5078 if (height <= room + room_cmdline)
5079 break;
5080#ifdef FEAT_VERTSPLIT
5081 if (run == 2 || curfrp->fr_width == Columns)
5082#endif
5083 {
5084 if (height > room + room_cmdline)
5085 height = room + room_cmdline;
5086 break;
5087 }
5088#ifdef FEAT_VERTSPLIT
5089 frame_setheight(curfrp->fr_parent, height
5090 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5091#endif
5092 /*NOTREACHED*/
5093 }
5094
5095 /*
5096 * Compute the number of lines we will take from others frames (can be
5097 * negative!).
5098 */
5099 take = height - curfrp->fr_height;
5100
5101 /* If there is not enough room, also reduce the height of a window
5102 * with 'winfixheight' set. */
5103 if (height > room + room_cmdline - room_reserved)
5104 room_reserved = room + room_cmdline - height;
5105 /* If there is only a 'winfixheight' window and making the
5106 * window smaller, need to make the other window taller. */
5107 if (take < 0 && room - curfrp->fr_height < room_reserved)
5108 room_reserved = 0;
5109
5110 if (take > 0 && room_cmdline > 0)
5111 {
5112 /* use lines from cmdline first */
5113 if (take < room_cmdline)
5114 room_cmdline = take;
5115 take -= room_cmdline;
5116 topframe->fr_height += room_cmdline;
5117 }
5118
5119 /*
5120 * set the current frame to the new height
5121 */
5122 frame_new_height(curfrp, height, FALSE, FALSE);
5123
5124 /*
5125 * First take lines from the frames after the current frame. If
5126 * that is not enough, takes lines from frames above the current
5127 * frame.
5128 */
5129 for (run = 0; run < 2; ++run)
5130 {
5131 if (run == 0)
5132 frp = curfrp->fr_next; /* 1st run: start with next window */
5133 else
5134 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5135 while (frp != NULL && take != 0)
5136 {
5137 h = frame_minheight(frp, NULL);
5138 if (room_reserved > 0
5139 && frp->fr_win != NULL
5140 && frp->fr_win->w_p_wfh)
5141 {
5142 if (room_reserved >= frp->fr_height)
5143 room_reserved -= frp->fr_height;
5144 else
5145 {
5146 if (frp->fr_height - room_reserved > take)
5147 room_reserved = frp->fr_height - take;
5148 take -= frp->fr_height - room_reserved;
5149 frame_new_height(frp, room_reserved, FALSE, FALSE);
5150 room_reserved = 0;
5151 }
5152 }
5153 else
5154 {
5155 if (frp->fr_height - take < h)
5156 {
5157 take -= frp->fr_height - h;
5158 frame_new_height(frp, h, FALSE, FALSE);
5159 }
5160 else
5161 {
5162 frame_new_height(frp, frp->fr_height - take,
5163 FALSE, FALSE);
5164 take = 0;
5165 }
5166 }
5167 if (run == 0)
5168 frp = frp->fr_next;
5169 else
5170 frp = frp->fr_prev;
5171 }
5172 }
5173 }
5174}
5175
5176#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5177/*
5178 * Set current window width and take care of repositioning other windows to
5179 * fit around it.
5180 */
5181 void
5182win_setwidth(width)
5183 int width;
5184{
5185 win_setwidth_win(width, curwin);
5186}
5187
5188 void
5189win_setwidth_win(width, wp)
5190 int width;
5191 win_T *wp;
5192{
5193 /* Always keep current window at least one column wide, even when
5194 * 'winminwidth' is zero. */
5195 if (wp == curwin)
5196 {
5197 if (width < p_wmw)
5198 width = p_wmw;
5199 if (width == 0)
5200 width = 1;
5201 }
5202
5203 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5204
5205 /* recompute the window positions */
5206 (void)win_comp_pos();
5207
5208 redraw_all_later(NOT_VALID);
5209}
5210
5211/*
5212 * Set the width of a frame to "width" and take care that all frames and
5213 * windows inside it are resized. Also resize frames above and below if the
5214 * are in the same FR_ROW frame.
5215 *
5216 * Strategy is similar to frame_setheight().
5217 */
5218 static void
5219frame_setwidth(curfrp, width)
5220 frame_T *curfrp;
5221 int width;
5222{
5223 int room; /* total number of lines available */
5224 int take; /* number of lines taken from other windows */
5225 int run;
5226 frame_T *frp;
5227 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005228 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 /* If the width already is the desired value, nothing to do. */
5231 if (curfrp->fr_width == width)
5232 return;
5233
5234 if (curfrp->fr_parent == NULL)
5235 /* topframe: can't change width */
5236 return;
5237
5238 if (curfrp->fr_parent->fr_layout == FR_COL)
5239 {
5240 /* Column of frames: Also need to resize frames above and below of
5241 * this one. First check for the minimal width of these. */
5242 w = frame_minwidth(curfrp->fr_parent, NULL);
5243 if (width < w)
5244 width = w;
5245 frame_setwidth(curfrp->fr_parent, width);
5246 }
5247 else
5248 {
5249 /*
5250 * Row of frames: try to change only frames in this row.
5251 *
5252 * Do this twice:
5253 * 1: compute room available, if it's not enough try resizing the
5254 * containing frame.
5255 * 2: compute the room available and adjust the width to it.
5256 */
5257 for (run = 1; run <= 2; ++run)
5258 {
5259 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005260 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5262 frp = frp->fr_next)
5263 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005264 if (frp != curfrp
5265 && frp->fr_win != NULL
5266 && frp->fr_win->w_p_wfw)
5267 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 room += frp->fr_width;
5269 if (frp != curfrp)
5270 room -= frame_minwidth(frp, NULL);
5271 }
5272
5273 if (width <= room)
5274 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005275 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 if (width > room)
5278 width = room;
5279 break;
5280 }
5281 frame_setwidth(curfrp->fr_parent, width
5282 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5283 }
5284
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 /*
5286 * Compute the number of lines we will take from others frames (can be
5287 * negative!).
5288 */
5289 take = width - curfrp->fr_width;
5290
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005291 /* If there is not enough room, also reduce the width of a window
5292 * with 'winfixwidth' set. */
5293 if (width > room - room_reserved)
5294 room_reserved = room - width;
5295 /* If there is only a 'winfixwidth' window and making the
5296 * window smaller, need to make the other window narrower. */
5297 if (take < 0 && room - curfrp->fr_width < room_reserved)
5298 room_reserved = 0;
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 /*
5301 * set the current frame to the new width
5302 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005303 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 /*
5306 * First take lines from the frames right of the current frame. If
5307 * that is not enough, takes lines from frames left of the current
5308 * frame.
5309 */
5310 for (run = 0; run < 2; ++run)
5311 {
5312 if (run == 0)
5313 frp = curfrp->fr_next; /* 1st run: start with next window */
5314 else
5315 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5316 while (frp != NULL && take != 0)
5317 {
5318 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005319 if (room_reserved > 0
5320 && frp->fr_win != NULL
5321 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005323 if (room_reserved >= frp->fr_width)
5324 room_reserved -= frp->fr_width;
5325 else
5326 {
5327 if (frp->fr_width - room_reserved > take)
5328 room_reserved = frp->fr_width - take;
5329 take -= frp->fr_width - room_reserved;
5330 frame_new_width(frp, room_reserved, FALSE, FALSE);
5331 room_reserved = 0;
5332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334 else
5335 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005336 if (frp->fr_width - take < w)
5337 {
5338 take -= frp->fr_width - w;
5339 frame_new_width(frp, w, FALSE, FALSE);
5340 }
5341 else
5342 {
5343 frame_new_width(frp, frp->fr_width - take,
5344 FALSE, FALSE);
5345 take = 0;
5346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348 if (run == 0)
5349 frp = frp->fr_next;
5350 else
5351 frp = frp->fr_prev;
5352 }
5353 }
5354 }
5355}
5356#endif /* FEAT_VERTSPLIT */
5357
5358/*
5359 * Check 'winminheight' for a valid value.
5360 */
5361 void
5362win_setminheight()
5363{
5364 int room;
5365 int first = TRUE;
5366 win_T *wp;
5367
5368 /* loop until there is a 'winminheight' that is possible */
5369 while (p_wmh > 0)
5370 {
5371 /* TODO: handle vertical splits */
5372 room = -p_wh;
5373 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5374 room += wp->w_height - p_wmh;
5375 if (room >= 0)
5376 break;
5377 --p_wmh;
5378 if (first)
5379 {
5380 EMSG(_(e_noroom));
5381 first = FALSE;
5382 }
5383 }
5384}
5385
5386#ifdef FEAT_MOUSE
5387
5388/*
5389 * Status line of dragwin is dragged "offset" lines down (negative is up).
5390 */
5391 void
5392win_drag_status_line(dragwin, offset)
5393 win_T *dragwin;
5394 int offset;
5395{
5396 frame_T *curfr;
5397 frame_T *fr;
5398 int room;
5399 int row;
5400 int up; /* if TRUE, drag status line up, otherwise down */
5401 int n;
5402
5403 fr = dragwin->w_frame;
5404 curfr = fr;
5405 if (fr != topframe) /* more than one window */
5406 {
5407 fr = fr->fr_parent;
5408 /* When the parent frame is not a column of frames, its parent should
5409 * be. */
5410 if (fr->fr_layout != FR_COL)
5411 {
5412 curfr = fr;
5413 if (fr != topframe) /* only a row of windows, may drag statusline */
5414 fr = fr->fr_parent;
5415 }
5416 }
5417
5418 /* If this is the last frame in a column, may want to resize the parent
5419 * frame instead (go two up to skip a row of frames). */
5420 while (curfr != topframe && curfr->fr_next == NULL)
5421 {
5422 if (fr != topframe)
5423 fr = fr->fr_parent;
5424 curfr = fr;
5425 if (fr != topframe)
5426 fr = fr->fr_parent;
5427 }
5428
5429 if (offset < 0) /* drag up */
5430 {
5431 up = TRUE;
5432 offset = -offset;
5433 /* sum up the room of the current frame and above it */
5434 if (fr == curfr)
5435 {
5436 /* only one window */
5437 room = fr->fr_height - frame_minheight(fr, NULL);
5438 }
5439 else
5440 {
5441 room = 0;
5442 for (fr = fr->fr_child; ; fr = fr->fr_next)
5443 {
5444 room += fr->fr_height - frame_minheight(fr, NULL);
5445 if (fr == curfr)
5446 break;
5447 }
5448 }
5449 fr = curfr->fr_next; /* put fr at frame that grows */
5450 }
5451 else /* drag down */
5452 {
5453 up = FALSE;
5454 /*
5455 * Only dragging the last status line can reduce p_ch.
5456 */
5457 room = Rows - cmdline_row;
5458 if (curfr->fr_next == NULL)
5459 room -= 1;
5460 else
5461 room -= p_ch;
5462 if (room < 0)
5463 room = 0;
5464 /* sum up the room of frames below of the current one */
5465 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5466 room += fr->fr_height - frame_minheight(fr, NULL);
5467 fr = curfr; /* put fr at window that grows */
5468 }
5469
5470 if (room < offset) /* Not enough room */
5471 offset = room; /* Move as far as we can */
5472 if (offset <= 0)
5473 return;
5474
5475 /*
5476 * Grow frame fr by "offset" lines.
5477 * Doesn't happen when dragging the last status line up.
5478 */
5479 if (fr != NULL)
5480 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5481
5482 if (up)
5483 fr = curfr; /* current frame gets smaller */
5484 else
5485 fr = curfr->fr_next; /* next frame gets smaller */
5486
5487 /*
5488 * Now make the other frames smaller.
5489 */
5490 while (fr != NULL && offset > 0)
5491 {
5492 n = frame_minheight(fr, NULL);
5493 if (fr->fr_height - offset <= n)
5494 {
5495 offset -= fr->fr_height - n;
5496 frame_new_height(fr, n, !up, FALSE);
5497 }
5498 else
5499 {
5500 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5501 break;
5502 }
5503 if (up)
5504 fr = fr->fr_prev;
5505 else
5506 fr = fr->fr_next;
5507 }
5508 row = win_comp_pos();
5509 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5510 cmdline_row = row;
5511 p_ch = Rows - cmdline_row;
5512 if (p_ch < 1)
5513 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005514 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005515 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 showmode();
5517}
5518
5519#ifdef FEAT_VERTSPLIT
5520/*
5521 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5522 */
5523 void
5524win_drag_vsep_line(dragwin, offset)
5525 win_T *dragwin;
5526 int offset;
5527{
5528 frame_T *curfr;
5529 frame_T *fr;
5530 int room;
5531 int left; /* if TRUE, drag separator line left, otherwise right */
5532 int n;
5533
5534 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005535 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 return;
5537 curfr = fr;
5538 fr = fr->fr_parent;
5539 /* When the parent frame is not a row of frames, its parent should be. */
5540 if (fr->fr_layout != FR_ROW)
5541 {
5542 if (fr == topframe) /* only a column of windows (cannot happen?) */
5543 return;
5544 curfr = fr;
5545 fr = fr->fr_parent;
5546 }
5547
5548 /* If this is the last frame in a row, may want to resize a parent
5549 * frame instead. */
5550 while (curfr->fr_next == NULL)
5551 {
5552 if (fr == topframe)
5553 break;
5554 curfr = fr;
5555 fr = fr->fr_parent;
5556 if (fr != topframe)
5557 {
5558 curfr = fr;
5559 fr = fr->fr_parent;
5560 }
5561 }
5562
5563 if (offset < 0) /* drag left */
5564 {
5565 left = TRUE;
5566 offset = -offset;
5567 /* sum up the room of the current frame and left of it */
5568 room = 0;
5569 for (fr = fr->fr_child; ; fr = fr->fr_next)
5570 {
5571 room += fr->fr_width - frame_minwidth(fr, NULL);
5572 if (fr == curfr)
5573 break;
5574 }
5575 fr = curfr->fr_next; /* put fr at frame that grows */
5576 }
5577 else /* drag right */
5578 {
5579 left = FALSE;
5580 /* sum up the room of frames right of the current one */
5581 room = 0;
5582 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5583 room += fr->fr_width - frame_minwidth(fr, NULL);
5584 fr = curfr; /* put fr at window that grows */
5585 }
5586
5587 if (room < offset) /* Not enough room */
5588 offset = room; /* Move as far as we can */
5589 if (offset <= 0) /* No room at all, quit. */
5590 return;
5591
5592 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005593 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 /* shrink other frames: current and at the left or at the right */
5596 if (left)
5597 fr = curfr; /* current frame gets smaller */
5598 else
5599 fr = curfr->fr_next; /* next frame gets smaller */
5600
5601 while (fr != NULL && offset > 0)
5602 {
5603 n = frame_minwidth(fr, NULL);
5604 if (fr->fr_width - offset <= n)
5605 {
5606 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005607 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609 else
5610 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005611 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break;
5613 }
5614 if (left)
5615 fr = fr->fr_prev;
5616 else
5617 fr = fr->fr_next;
5618 }
5619 (void)win_comp_pos();
5620 redraw_all_later(NOT_VALID);
5621}
5622#endif /* FEAT_VERTSPLIT */
5623#endif /* FEAT_MOUSE */
5624
5625#endif /* FEAT_WINDOWS */
5626
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005627#define FRACTION_MULT 16384L
5628
5629/*
5630 * Set wp->w_fraction for the current w_wrow and w_height.
5631 */
5632 static void
5633set_fraction(wp)
5634 win_T *wp;
5635{
5636 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5637 + FRACTION_MULT / 2) / (long)wp->w_height;
5638}
5639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640/*
5641 * Set the height of a window.
5642 * This takes care of the things inside the window, not what happens to the
5643 * window position, the frame or to other windows.
5644 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005645 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646win_new_height(wp, height)
5647 win_T *wp;
5648 int height;
5649{
5650 linenr_T lnum;
5651 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 /* Don't want a negative height. Happens when splitting a tiny window.
5654 * Will equalize heights soon to fix it. */
5655 if (height < 0)
5656 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005657 if (wp->w_height == height)
5658 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005661 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 wp->w_height = height;
5664 wp->w_skipcol = 0;
5665
5666 /* Don't change w_topline when height is zero. Don't set w_topline when
5667 * 'scrollbind' is set and this isn't the current window. */
5668 if (height > 0
5669#ifdef FEAT_SCROLLBIND
5670 && (!wp->w_p_scb || wp == curwin)
5671#endif
5672 )
5673 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005674 /*
5675 * Find a value for w_topline that shows the cursor at the same
5676 * relative position in the window as before (more or less).
5677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 lnum = wp->w_cursor.lnum;
5679 if (lnum < 1) /* can happen when starting up */
5680 lnum = 1;
5681 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5682 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5683 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005684
5685 if (sline >= 0)
5686 {
5687 /* Make sure the whole cursor line is visible, if possible. */
5688 int rows = plines_win(wp, lnum, FALSE);
5689
5690 if (sline > wp->w_height - rows)
5691 {
5692 sline = wp->w_height - rows;
5693 wp->w_wrow -= rows - line_size;
5694 }
5695 }
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (sline < 0)
5698 {
5699 /*
5700 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005701 * Make cursor line the first line in the window. If not enough
5702 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005705 if (wp->w_wrow >= wp->w_height
5706 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5707 {
5708 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5709 --wp->w_wrow;
5710 while (wp->w_wrow >= wp->w_height)
5711 {
5712 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5713 + win_col_off2(wp);
5714 --wp->w_wrow;
5715 }
5716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else
5719 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005720 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722#ifdef FEAT_FOLDING
5723 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5724 if (lnum == 1)
5725 {
5726 /* first line in buffer is folded */
5727 line_size = 1;
5728 --sline;
5729 break;
5730 }
5731#endif
5732 --lnum;
5733#ifdef FEAT_DIFF
5734 if (lnum == wp->w_topline)
5735 line_size = plines_win_nofill(wp, lnum, TRUE)
5736 + wp->w_topfill;
5737 else
5738#endif
5739 line_size = plines_win(wp, lnum, TRUE);
5740 sline -= line_size;
5741 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if (sline < 0)
5744 {
5745 /*
5746 * Line we want at top would go off top of screen. Use next
5747 * line instead.
5748 */
5749#ifdef FEAT_FOLDING
5750 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5751#endif
5752 lnum++;
5753 wp->w_wrow -= line_size + sline;
5754 }
5755 else if (sline > 0)
5756 {
5757 /* First line of file reached, use that as topline. */
5758 lnum = 1;
5759 wp->w_wrow -= sline;
5760 }
5761 }
5762 set_topline(wp, lnum);
5763 }
5764
5765 if (wp == curwin)
5766 {
5767 if (p_so)
5768 update_topline();
5769 curs_columns(FALSE); /* validate w_wrow */
5770 }
5771 wp->w_prev_fraction_row = wp->w_wrow;
5772
5773 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005774 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_WINDOWS
5776 wp->w_redr_status = TRUE;
5777#endif
5778 invalidate_botline_win(wp);
5779}
5780
5781#ifdef FEAT_VERTSPLIT
5782/*
5783 * Set the width of a window.
5784 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005785 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786win_new_width(wp, width)
5787 win_T *wp;
5788 int width;
5789{
5790 wp->w_width = width;
5791 wp->w_lines_valid = 0;
5792 changed_line_abv_curs_win(wp);
5793 invalidate_botline_win(wp);
5794 if (wp == curwin)
5795 {
5796 update_topline();
5797 curs_columns(TRUE); /* validate w_wrow */
5798 }
5799 redraw_win_later(wp, NOT_VALID);
5800 wp->w_redr_status = TRUE;
5801}
5802#endif
5803
5804 void
5805win_comp_scroll(wp)
5806 win_T *wp;
5807{
5808 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5809 if (wp->w_p_scr == 0)
5810 wp->w_p_scr = 1;
5811}
5812
5813/*
5814 * command_height: called whenever p_ch has been changed
5815 */
5816 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005817command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819#ifdef FEAT_WINDOWS
5820 int h;
5821 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005822 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005824 /* Use the value of p_ch that we remembered. This is needed for when the
5825 * GUI starts up, we can't be sure in what order things happen. And when
5826 * p_ch was changed in another tab page. */
5827 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 /* Find bottom frame with width of screen. */
5830 frp = lastwin->w_frame;
5831# ifdef FEAT_VERTSPLIT
5832 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5833 frp = frp->fr_parent;
5834# endif
5835
5836 /* Avoid changing the height of a window with 'winfixheight' set. */
5837 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5838 && frp->fr_win->w_p_wfh)
5839 frp = frp->fr_prev;
5840
5841 if (starting != NO_SCREEN)
5842 {
5843 cmdline_row = Rows - p_ch;
5844
5845 if (p_ch > old_p_ch) /* p_ch got bigger */
5846 {
5847 while (p_ch > old_p_ch)
5848 {
5849 if (frp == NULL)
5850 {
5851 EMSG(_(e_noroom));
5852 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005853 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 cmdline_row = Rows - p_ch;
5855 break;
5856 }
5857 h = frp->fr_height - frame_minheight(frp, NULL);
5858 if (h > p_ch - old_p_ch)
5859 h = p_ch - old_p_ch;
5860 old_p_ch += h;
5861 frame_add_height(frp, -h);
5862 frp = frp->fr_prev;
5863 }
5864
5865 /* Recompute window positions. */
5866 (void)win_comp_pos();
5867
5868 /* clear the lines added to cmdline */
5869 if (full_screen)
5870 screen_fill((int)(cmdline_row), (int)Rows, 0,
5871 (int)Columns, ' ', ' ', 0);
5872 msg_row = cmdline_row;
5873 redraw_cmdline = TRUE;
5874 return;
5875 }
5876
5877 if (msg_row < cmdline_row)
5878 msg_row = cmdline_row;
5879 redraw_cmdline = TRUE;
5880 }
5881 frame_add_height(frp, (int)(old_p_ch - p_ch));
5882
5883 /* Recompute window positions. */
5884 if (frp != lastwin->w_frame)
5885 (void)win_comp_pos();
5886#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005888 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
5890}
5891
5892#if defined(FEAT_WINDOWS) || defined(PROTO)
5893/*
5894 * Resize frame "frp" to be "n" lines higher (negative for less high).
5895 * Also resize the frames it is contained in.
5896 */
5897 static void
5898frame_add_height(frp, n)
5899 frame_T *frp;
5900 int n;
5901{
5902 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5903 for (;;)
5904 {
5905 frp = frp->fr_parent;
5906 if (frp == NULL)
5907 break;
5908 frp->fr_height += n;
5909 }
5910}
5911
5912/*
5913 * Add or remove a status line for the bottom window(s), according to the
5914 * value of 'laststatus'.
5915 */
5916 void
5917last_status(morewin)
5918 int morewin; /* pretend there are two or more windows */
5919{
5920 /* Don't make a difference between horizontal or vertical split. */
5921 last_status_rec(topframe, (p_ls == 2
5922 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5923}
5924
5925 static void
5926last_status_rec(fr, statusline)
5927 frame_T *fr;
5928 int statusline;
5929{
5930 frame_T *fp;
5931 win_T *wp;
5932
5933 if (fr->fr_layout == FR_LEAF)
5934 {
5935 wp = fr->fr_win;
5936 if (wp->w_status_height != 0 && !statusline)
5937 {
5938 /* remove status line */
5939 win_new_height(wp, wp->w_height + 1);
5940 wp->w_status_height = 0;
5941 comp_col();
5942 }
5943 else if (wp->w_status_height == 0 && statusline)
5944 {
5945 /* Find a frame to take a line from. */
5946 fp = fr;
5947 while (fp->fr_height <= frame_minheight(fp, NULL))
5948 {
5949 if (fp == topframe)
5950 {
5951 EMSG(_(e_noroom));
5952 return;
5953 }
5954 /* In a column of frames: go to frame above. If already at
5955 * the top or in a row of frames: go to parent. */
5956 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5957 fp = fp->fr_prev;
5958 else
5959 fp = fp->fr_parent;
5960 }
5961 wp->w_status_height = 1;
5962 if (fp != fr)
5963 {
5964 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5965 frame_fix_height(wp);
5966 (void)win_comp_pos();
5967 }
5968 else
5969 win_new_height(wp, wp->w_height - 1);
5970 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005971 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 }
5974#ifdef FEAT_VERTSPLIT
5975 else if (fr->fr_layout == FR_ROW)
5976 {
5977 /* vertically split windows, set status line for each one */
5978 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5979 last_status_rec(fp, statusline);
5980 }
5981#endif
5982 else
5983 {
5984 /* horizontally split window, set status line for last one */
5985 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5986 ;
5987 last_status_rec(fp, statusline);
5988 }
5989}
5990
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005992 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 */
5994 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005995tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005997#ifdef FEAT_GUI_TABLINE
5998 /* When the GUI has the tabline then this always returns zero. */
5999 if (gui_use_tabline())
6000 return 0;
6001#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006002 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006003 {
6004 case 0: return 0;
6005 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6006 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006007 return 1;
6008}
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010#endif /* FEAT_WINDOWS */
6011
6012#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6013/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006014 * Get the file name at the cursor.
6015 * If Visual mode is active, use the selected text if it's in one line.
6016 * Returns the name in allocated memory, NULL for failure.
6017 */
6018 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006019grab_file_name(count, file_lnum)
6020 long count;
6021 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006022{
6023# ifdef FEAT_VISUAL
6024 if (VIsual_active)
6025 {
6026 int len;
6027 char_u *ptr;
6028
6029 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6030 return NULL;
6031 return find_file_name_in_path(ptr, len,
6032 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6033 }
6034# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006035 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6036 file_lnum);
6037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006038}
6039
6040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * Return the file name under or after the cursor.
6042 *
6043 * The 'path' option is searched if the file name is not absolute.
6044 * The string returned has been alloc'ed and should be freed by the caller.
6045 * NULL is returned if the file name or file is not found.
6046 *
6047 * options:
6048 * FNAME_MESS give error messages
6049 * FNAME_EXP expand to path
6050 * FNAME_HYP check for hypertext link
6051 * FNAME_INCL apply "includeexpr"
6052 */
6053 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006054file_name_at_cursor(options, count, file_lnum)
6055 int options;
6056 long count;
6057 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006060 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6061 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062}
6063
6064/*
6065 * Return the name of the file under or after ptr[col].
6066 * Otherwise like file_name_at_cursor().
6067 */
6068 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006069file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 char_u *line;
6071 int col;
6072 int options;
6073 long count;
6074 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006075 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 char_u *ptr;
6078 int len;
6079
6080 /*
6081 * search forward for what could be the start of a file name
6082 */
6083 ptr = line + col;
6084 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006085 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (*ptr == NUL) /* nothing found */
6087 {
6088 if (options & FNAME_MESS)
6089 EMSG(_("E446: No file name under cursor"));
6090 return NULL;
6091 }
6092
6093 /*
6094 * Search backward for first char of the file name.
6095 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6096 */
6097 while (ptr > line)
6098 {
6099#ifdef FEAT_MBYTE
6100 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6101 ptr -= len + 1;
6102 else
6103#endif
6104 if (vim_isfilec(ptr[-1])
6105 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6106 --ptr;
6107 else
6108 break;
6109 }
6110
6111 /*
6112 * Search forward for the last char of the file name.
6113 * Also allow "://" when ':' is not in 'isfname'.
6114 */
6115 len = 0;
6116 while (vim_isfilec(ptr[len])
6117 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6118#ifdef FEAT_MBYTE
6119 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006120 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 else
6122#endif
6123 ++len;
6124
6125 /*
6126 * If there is trailing punctuation, remove it.
6127 * But don't remove "..", could be a directory name.
6128 */
6129 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6130 && ptr[len - 2] != '.')
6131 --len;
6132
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006133 if (file_lnum != NULL)
6134 {
6135 char_u *p;
6136
6137 /* Get the number after the file name and a separator character */
6138 p = ptr + len;
6139 p = skipwhite(p);
6140 if (*p != NUL)
6141 {
6142 if (!isdigit(*p))
6143 ++p; /* skip the separator */
6144 p = skipwhite(p);
6145 if (isdigit(*p))
6146 *file_lnum = (int)getdigits(&p);
6147 }
6148 }
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6151}
6152
6153# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6154static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6155
6156 static char_u *
6157eval_includeexpr(ptr, len)
6158 char_u *ptr;
6159 int len;
6160{
6161 char_u *res;
6162
6163 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006164 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006165 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 set_vim_var_string(VV_FNAME, NULL, 0);
6167 return res;
6168}
6169#endif
6170
6171/*
6172 * Return the name of the file ptr[len] in 'path'.
6173 * Otherwise like file_name_at_cursor().
6174 */
6175 char_u *
6176find_file_name_in_path(ptr, len, options, count, rel_fname)
6177 char_u *ptr;
6178 int len;
6179 int options;
6180 long count;
6181 char_u *rel_fname; /* file we are searching relative to */
6182{
6183 char_u *file_name;
6184 int c;
6185# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6186 char_u *tofree = NULL;
6187
6188 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6189 {
6190 tofree = eval_includeexpr(ptr, len);
6191 if (tofree != NULL)
6192 {
6193 ptr = tofree;
6194 len = (int)STRLEN(ptr);
6195 }
6196 }
6197# endif
6198
6199 if (options & FNAME_EXP)
6200 {
6201 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6202 TRUE, rel_fname);
6203
6204# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6205 /*
6206 * If the file could not be found in a normal way, try applying
6207 * 'includeexpr' (unless done already).
6208 */
6209 if (file_name == NULL
6210 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6211 {
6212 tofree = eval_includeexpr(ptr, len);
6213 if (tofree != NULL)
6214 {
6215 ptr = tofree;
6216 len = (int)STRLEN(ptr);
6217 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6218 TRUE, rel_fname);
6219 }
6220 }
6221# endif
6222 if (file_name == NULL && (options & FNAME_MESS))
6223 {
6224 c = ptr[len];
6225 ptr[len] = NUL;
6226 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6227 ptr[len] = c;
6228 }
6229
6230 /* Repeat finding the file "count" times. This matters when it
6231 * appears several times in the path. */
6232 while (file_name != NULL && --count > 0)
6233 {
6234 vim_free(file_name);
6235 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6236 }
6237 }
6238 else
6239 file_name = vim_strnsave(ptr, len);
6240
6241# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6242 vim_free(tofree);
6243# endif
6244
6245 return file_name;
6246}
6247#endif /* FEAT_SEARCHPATH */
6248
6249/*
6250 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6251 * Also check for ":\\", which MS Internet Explorer accepts, return
6252 * URL_BACKSLASH.
6253 */
6254 static int
6255path_is_url(p)
6256 char_u *p;
6257{
6258 if (STRNCMP(p, "://", (size_t)3) == 0)
6259 return URL_SLASH;
6260 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6261 return URL_BACKSLASH;
6262 return 0;
6263}
6264
6265/*
6266 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6267 * Return URL_BACKSLASH for "name:\\".
6268 * Return zero otherwise.
6269 */
6270 int
6271path_with_url(fname)
6272 char_u *fname;
6273{
6274 char_u *p;
6275
6276 for (p = fname; isalpha(*p); ++p)
6277 ;
6278 return path_is_url(p);
6279}
6280
6281/*
6282 * Return TRUE if "name" is a full (absolute) path name or URL.
6283 */
6284 int
6285vim_isAbsName(name)
6286 char_u *name;
6287{
6288 return (path_with_url(name) != 0 || mch_isFullName(name));
6289}
6290
6291/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006292 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 *
6294 * return FAIL for failure, OK otherwise
6295 */
6296 int
6297vim_FullName(fname, buf, len, force)
6298 char_u *fname, *buf;
6299 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006300 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 int retval = OK;
6303 int url;
6304
6305 *buf = NUL;
6306 if (fname == NULL)
6307 return FAIL;
6308
6309 url = path_with_url(fname);
6310 if (!url)
6311 retval = mch_FullName(fname, buf, len, force);
6312 if (url || retval == FAIL)
6313 {
6314 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006315 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6318 slash_adjust(buf);
6319#endif
6320 return retval;
6321}
6322
6323/*
6324 * Return the minimal number of rows that is needed on the screen to display
6325 * the current number of windows.
6326 */
6327 int
6328min_rows()
6329{
6330 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006331#ifdef FEAT_WINDOWS
6332 tabpage_T *tp;
6333 int n;
6334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336 if (firstwin == NULL) /* not initialized yet */
6337 return MIN_LINES;
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006340 total = 0;
6341 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6342 {
6343 n = frame_minheight(tp->tp_topframe, NULL);
6344 if (total < n)
6345 total = n;
6346 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006347 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006349 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006351 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return total;
6353}
6354
6355/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006356 * Return TRUE if there is only one window (in the current tab page), not
6357 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006358 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 */
6360 int
6361only_one_window()
6362{
6363#ifdef FEAT_WINDOWS
6364 int count = 0;
6365 win_T *wp;
6366
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006367 /* If there is another tab page there always is another window. */
6368 if (first_tabpage->tp_next != NULL)
6369 return FALSE;
6370
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006372 if (wp->w_buffer != NULL
6373 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374# ifdef FEAT_QUICKFIX
6375 || wp->w_p_pvw
6376# endif
6377 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006378# ifdef FEAT_AUTOCMD
6379 && wp != aucmd_win
6380# endif
6381 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 ++count;
6383 return (count <= 1);
6384#else
6385 return TRUE;
6386#endif
6387}
6388
6389#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6390/*
6391 * Correct the cursor line number in other windows. Used after changing the
6392 * current buffer, and before applying autocommands.
6393 * When "do_curwin" is TRUE, also check current window.
6394 */
6395 void
6396check_lnums(do_curwin)
6397 int do_curwin;
6398{
6399 win_T *wp;
6400
6401#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006402 tabpage_T *tp;
6403
6404 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6406#else
6407 wp = curwin;
6408 if (do_curwin)
6409#endif
6410 {
6411 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6412 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6413 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6414 wp->w_topline = curbuf->b_ml.ml_line_count;
6415 }
6416}
6417#endif
6418
6419#if defined(FEAT_WINDOWS) || defined(PROTO)
6420
6421/*
6422 * A snapshot of the window sizes, to restore them after closing the help
6423 * window.
6424 * Only these fields are used:
6425 * fr_layout
6426 * fr_width
6427 * fr_height
6428 * fr_next
6429 * fr_child
6430 * fr_win (only valid for the old curwin, NULL otherwise)
6431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433/*
6434 * Create a snapshot of the current frame sizes.
6435 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006436 void
6437make_snapshot(idx)
6438 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006440 clear_snapshot(curtab, idx);
6441 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442}
6443
6444 static void
6445make_snapshot_rec(fr, frp)
6446 frame_T *fr;
6447 frame_T **frp;
6448{
6449 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6450 if (*frp == NULL)
6451 return;
6452 (*frp)->fr_layout = fr->fr_layout;
6453# ifdef FEAT_VERTSPLIT
6454 (*frp)->fr_width = fr->fr_width;
6455# endif
6456 (*frp)->fr_height = fr->fr_height;
6457 if (fr->fr_next != NULL)
6458 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6459 if (fr->fr_child != NULL)
6460 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6461 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6462 (*frp)->fr_win = curwin;
6463}
6464
6465/*
6466 * Remove any existing snapshot.
6467 */
6468 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006469clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006470 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006471 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006473 clear_snapshot_rec(tp->tp_snapshot[idx]);
6474 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475}
6476
6477 static void
6478clear_snapshot_rec(fr)
6479 frame_T *fr;
6480{
6481 if (fr != NULL)
6482 {
6483 clear_snapshot_rec(fr->fr_next);
6484 clear_snapshot_rec(fr->fr_child);
6485 vim_free(fr);
6486 }
6487}
6488
6489/*
6490 * Restore a previously created snapshot, if there is any.
6491 * This is only done if the screen size didn't change and the window layout is
6492 * still the same.
6493 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006494 void
6495restore_snapshot(idx, close_curwin)
6496 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 int close_curwin; /* closing current window */
6498{
6499 win_T *wp;
6500
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006501 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006503 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006505 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6506 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006508 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 win_comp_pos();
6510 if (wp != NULL && close_curwin)
6511 win_goto(wp);
6512 redraw_all_later(CLEAR);
6513 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006514 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517/*
6518 * Check if frames "sn" and "fr" have the same layout, same following frames
6519 * and same children.
6520 */
6521 static int
6522check_snapshot_rec(sn, fr)
6523 frame_T *sn;
6524 frame_T *fr;
6525{
6526 if (sn->fr_layout != fr->fr_layout
6527 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6528 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6529 || (sn->fr_next != NULL
6530 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6531 || (sn->fr_child != NULL
6532 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6533 return FAIL;
6534 return OK;
6535}
6536
6537/*
6538 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6539 * following frames and children.
6540 * Returns a pointer to the old current window, or NULL.
6541 */
6542 static win_T *
6543restore_snapshot_rec(sn, fr)
6544 frame_T *sn;
6545 frame_T *fr;
6546{
6547 win_T *wp = NULL;
6548 win_T *wp2;
6549
6550 fr->fr_height = sn->fr_height;
6551# ifdef FEAT_VERTSPLIT
6552 fr->fr_width = sn->fr_width;
6553# endif
6554 if (fr->fr_layout == FR_LEAF)
6555 {
6556 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6557# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006558 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559# endif
6560 wp = sn->fr_win;
6561 }
6562 if (sn->fr_next != NULL)
6563 {
6564 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6565 if (wp2 != NULL)
6566 wp = wp2;
6567 }
6568 if (sn->fr_child != NULL)
6569 {
6570 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6571 if (wp2 != NULL)
6572 wp = wp2;
6573 }
6574 return wp;
6575}
6576
6577#endif
6578
Bram Moolenaar105bc352013-05-17 16:03:57 +02006579#if defined(FEAT_EVAL) || defined(PROTO)
6580/*
6581 * Set "win" to be the curwin and "tp" to be the current tab page.
6582 * restore_win() MUST be called to undo.
6583 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006584 * When "no_display" is TRUE the display won't be affected, no redraw is
6585 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006586 * Returns FAIL if switching to "win" failed.
6587 */
6588 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006589switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006590 win_T **save_curwin UNUSED;
6591 tabpage_T **save_curtab UNUSED;
6592 win_T *win UNUSED;
6593 tabpage_T *tp UNUSED;
6594 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006595{
6596# ifdef FEAT_AUTOCMD
6597 block_autocmds();
6598# endif
6599# ifdef FEAT_WINDOWS
6600 *save_curwin = curwin;
6601 if (tp != NULL)
6602 {
6603 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006604 if (no_display)
6605 {
6606 curtab->tp_firstwin = firstwin;
6607 curtab->tp_lastwin = lastwin;
6608 curtab = tp;
6609 firstwin = curtab->tp_firstwin;
6610 lastwin = curtab->tp_lastwin;
6611 }
6612 else
6613 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006614 }
6615 if (!win_valid(win))
6616 {
6617# ifdef FEAT_AUTOCMD
6618 unblock_autocmds();
6619# endif
6620 return FAIL;
6621 }
6622 curwin = win;
6623 curbuf = curwin->w_buffer;
6624# endif
6625 return OK;
6626}
6627
6628/*
6629 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006630 * When "no_display" is TRUE the display won't be affected, no redraw is
6631 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006632 */
6633 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006634restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006635 win_T *save_curwin UNUSED;
6636 tabpage_T *save_curtab UNUSED;
6637 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006638{
6639# ifdef FEAT_WINDOWS
6640 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006641 {
6642 if (no_display)
6643 {
6644 curtab->tp_firstwin = firstwin;
6645 curtab->tp_lastwin = lastwin;
6646 curtab = save_curtab;
6647 firstwin = curtab->tp_firstwin;
6648 lastwin = curtab->tp_lastwin;
6649 }
6650 else
6651 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6652 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006653 if (win_valid(save_curwin))
6654 {
6655 curwin = save_curwin;
6656 curbuf = curwin->w_buffer;
6657 }
6658# endif
6659# ifdef FEAT_AUTOCMD
6660 unblock_autocmds();
6661# endif
6662}
6663
6664/*
6665 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6666 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6667 */
6668 void
6669switch_buffer(save_curbuf, buf)
6670 buf_T *buf;
6671 buf_T **save_curbuf;
6672{
6673# ifdef FEAT_AUTOCMD
6674 block_autocmds();
6675# endif
6676 *save_curbuf = curbuf;
6677 --curbuf->b_nwindows;
6678 curbuf = buf;
6679 curwin->w_buffer = buf;
6680 ++curbuf->b_nwindows;
6681}
6682
6683/*
6684 * Restore the current buffer after using switch_buffer().
6685 */
6686 void
6687restore_buffer(save_curbuf)
6688 buf_T *save_curbuf;
6689{
6690# ifdef FEAT_AUTOCMD
6691 unblock_autocmds();
6692# endif
6693 /* Check for valid buffer, just in case. */
6694 if (buf_valid(save_curbuf))
6695 {
6696 --curbuf->b_nwindows;
6697 curwin->w_buffer = save_curbuf;
6698 curbuf = save_curbuf;
6699 ++curbuf->b_nwindows;
6700 }
6701}
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6705/*
6706 * Return TRUE if there is any vertically split window.
6707 */
6708 int
6709win_hasvertsplit()
6710{
6711 frame_T *fr;
6712
6713 if (topframe->fr_layout == FR_ROW)
6714 return TRUE;
6715
6716 if (topframe->fr_layout == FR_COL)
6717 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6718 if (fr->fr_layout == FR_ROW)
6719 return TRUE;
6720
6721 return FALSE;
6722}
6723#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006724
6725#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6726/*
6727 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006728 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006729 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6730 * If no particular ID is desired, -1 must be specified for 'id'.
6731 * Return ID of added match, -1 on failure.
6732 */
6733 int
6734match_add(wp, grp, pat, prio, id)
6735 win_T *wp;
6736 char_u *grp;
6737 char_u *pat;
6738 int prio;
6739 int id;
6740{
6741 matchitem_T *cur;
6742 matchitem_T *prev;
6743 matchitem_T *m;
6744 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006745 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006746
6747 if (*grp == NUL || *pat == NUL)
6748 return -1;
6749 if (id < -1 || id == 0)
6750 {
6751 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6752 return -1;
6753 }
6754 if (id != -1)
6755 {
6756 cur = wp->w_match_head;
6757 while (cur != NULL)
6758 {
6759 if (cur->id == id)
6760 {
6761 EMSGN("E801: ID already taken: %ld", id);
6762 return -1;
6763 }
6764 cur = cur->next;
6765 }
6766 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006767 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006768 {
6769 EMSG2(_(e_nogroup), grp);
6770 return -1;
6771 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006772 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773 {
6774 EMSG2(_(e_invarg2), pat);
6775 return -1;
6776 }
6777
6778 /* Find available match ID. */
6779 while (id == -1)
6780 {
6781 cur = wp->w_match_head;
6782 while (cur != NULL && cur->id != wp->w_next_match_id)
6783 cur = cur->next;
6784 if (cur == NULL)
6785 id = wp->w_next_match_id;
6786 wp->w_next_match_id++;
6787 }
6788
6789 /* Build new match. */
6790 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6791 m->id = id;
6792 m->priority = prio;
6793 m->pattern = vim_strsave(pat);
6794 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006795 m->match.regprog = regprog;
6796 m->match.rmm_ic = FALSE;
6797 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006798
6799 /* Insert new match. The match list is in ascending order with regard to
6800 * the match priorities. */
6801 cur = wp->w_match_head;
6802 prev = cur;
6803 while (cur != NULL && prio >= cur->priority)
6804 {
6805 prev = cur;
6806 cur = cur->next;
6807 }
6808 if (cur == prev)
6809 wp->w_match_head = m;
6810 else
6811 prev->next = m;
6812 m->next = cur;
6813
6814 redraw_later(SOME_VALID);
6815 return id;
6816}
6817
6818/*
6819 * Delete match with ID 'id' in the match list of window 'wp'.
6820 * Print error messages if 'perr' is TRUE.
6821 */
6822 int
6823match_delete(wp, id, perr)
6824 win_T *wp;
6825 int id;
6826 int perr;
6827{
6828 matchitem_T *cur = wp->w_match_head;
6829 matchitem_T *prev = cur;
6830
6831 if (id < 1)
6832 {
6833 if (perr == TRUE)
6834 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6835 id);
6836 return -1;
6837 }
6838 while (cur != NULL && cur->id != id)
6839 {
6840 prev = cur;
6841 cur = cur->next;
6842 }
6843 if (cur == NULL)
6844 {
6845 if (perr == TRUE)
6846 EMSGN("E803: ID not found: %ld", id);
6847 return -1;
6848 }
6849 if (cur == prev)
6850 wp->w_match_head = cur->next;
6851 else
6852 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006853 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006854 vim_free(cur->pattern);
6855 vim_free(cur);
6856 redraw_later(SOME_VALID);
6857 return 0;
6858}
6859
6860/*
6861 * Delete all matches in the match list of window 'wp'.
6862 */
6863 void
6864clear_matches(wp)
6865 win_T *wp;
6866{
6867 matchitem_T *m;
6868
6869 while (wp->w_match_head != NULL)
6870 {
6871 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006872 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006873 vim_free(wp->w_match_head->pattern);
6874 vim_free(wp->w_match_head);
6875 wp->w_match_head = m;
6876 }
6877 redraw_later(SOME_VALID);
6878}
6879
6880/*
6881 * Get match from ID 'id' in window 'wp'.
6882 * Return NULL if match not found.
6883 */
6884 matchitem_T *
6885get_match(wp, id)
6886 win_T *wp;
6887 int id;
6888{
6889 matchitem_T *cur = wp->w_match_head;
6890
6891 while (cur != NULL && cur->id != id)
6892 cur = cur->next;
6893 return cur;
6894}
6895#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006896
6897#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6898 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006899get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006900{
6901 int i = 1;
6902 win_T *w;
6903
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006904 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006905 ++i;
6906
6907 if (w == NULL)
6908 return 0;
6909 else
6910 return i;
6911}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006912
6913 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006914get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006915{
6916 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006917# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006918 tabpage_T *t;
6919
6920 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6921 ++i;
6922
6923 if (t == NULL)
6924 return 0;
6925 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006926# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006927 return i;
6928}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006929#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006930
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006931#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006932/*
6933 * Return TRUE if "topfrp" and its children are at the right height.
6934 */
6935 static int
6936frame_check_height(topfrp, height)
6937 frame_T *topfrp;
6938 int height;
6939{
6940 frame_T *frp;
6941
6942 if (topfrp->fr_height != height)
6943 return FALSE;
6944
6945 if (topfrp->fr_layout == FR_ROW)
6946 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6947 if (frp->fr_height != height)
6948 return FALSE;
6949
6950 return TRUE;
6951}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006952#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006953
6954#ifdef FEAT_VERTSPLIT
6955/*
6956 * Return TRUE if "topfrp" and its children are at the right width.
6957 */
6958 static int
6959frame_check_width(topfrp, width)
6960 frame_T *topfrp;
6961 int width;
6962{
6963 frame_T *frp;
6964
6965 if (topfrp->fr_width != width)
6966 return FALSE;
6967
6968 if (topfrp->fr_layout == FR_COL)
6969 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6970 if (frp->fr_width != width)
6971 return FALSE;
6972
6973 return TRUE;
6974}
6975#endif
6976