blob: fadbb925924c2f76b0cd66a53c897dd232dc5732 [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 Moolenaar7e8fd632006-02-18 22:14:51 +000047static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaara8596c42012-06-13 14:28:20 +020048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_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));
51static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
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));
56#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));
59#endif
60static 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
69#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000070
Bram Moolenaar746ebd32009-06-16 14:01:43 +000071static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010072static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
74#define URL_SLASH 1 /* path_is_url() has found "://" */
75#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
76
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000077#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar05159a02005-02-26 23:04:13 +000079#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000081#else
82# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000083#endif
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000086
87static char *m_onlyone = N_("Already only one window");
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089/*
90 * all CTRL-W window commands are handled here, called from normal_cmd().
91 */
92 void
93do_window(nchar, Prenum, xchar)
94 int nchar;
95 long Prenum;
96 int xchar; /* extra char from ":wincmd gx" or NUL */
97{
98 long Prenum1;
99 win_T *wp;
100#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
101 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000102 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
104#ifdef FEAT_FIND_ID
105 int type = FIND_DEFINE;
106 int len;
107#endif
108 char_u cbuf[40];
109
110 if (Prenum == 0)
111 Prenum1 = 1;
112 else
113 Prenum1 = Prenum;
114
115#ifdef FEAT_CMDWIN
116# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
117#else
118# define CHECK_CMDWIN
119#endif
120
121 switch (nchar)
122 {
123/* split current window in two parts, horizontally */
124 case 'S':
125 case Ctrl_S:
126 case 's':
127 CHECK_CMDWIN
128#ifdef FEAT_VISUAL
129 reset_VIsual_and_resel(); /* stop Visual mode */
130#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000131#ifdef FEAT_QUICKFIX
132 /* When splitting the quickfix window open a new buffer in it,
133 * don't replicate the quickfix buffer. */
134 if (bt_quickfix(curbuf))
135 goto newwindow;
136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_GUI
138 need_mouse_correct = TRUE;
139#endif
140 win_split((int)Prenum, 0);
141 break;
142
143#ifdef FEAT_VERTSPLIT
144/* split current window in two parts, vertically */
145 case Ctrl_V:
146 case 'v':
147 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000148# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000150# endif
151# ifdef FEAT_QUICKFIX
152 /* When splitting the quickfix window open a new buffer in it,
153 * don't replicate the quickfix buffer. */
154 if (bt_quickfix(curbuf))
155 goto newwindow;
156# endif
157# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 win_split((int)Prenum, WSP_VERT);
161 break;
162#endif
163
164/* split current window and edit alternate file */
165 case Ctrl_HAT:
166 case '^':
167 CHECK_CMDWIN
168#ifdef FEAT_VISUAL
169 reset_VIsual_and_resel(); /* stop Visual mode */
170#endif
171 STRCPY(cbuf, "split #");
172 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000173 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
174 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000189 /* window height */
190 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 else
192 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000193#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
194 if (nchar == 'v' || nchar == Ctrl_V)
195 STRCAT(cbuf, "v");
196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 STRCAT(cbuf, "new");
198 do_cmdline_cmd(cbuf);
199 break;
200
201/* quit current window */
202 case Ctrl_Q:
203 case 'q':
204#ifdef FEAT_VISUAL
205 reset_VIsual_and_resel(); /* stop Visual mode */
206#endif
207 do_cmdline_cmd((char_u *)"quit");
208 break;
209
210/* close current window */
211 case Ctrl_C:
212 case 'c':
213#ifdef FEAT_VISUAL
214 reset_VIsual_and_resel(); /* stop Visual mode */
215#endif
216 do_cmdline_cmd((char_u *)"close");
217 break;
218
219#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
220/* close preview window */
221 case Ctrl_Z:
222 case 'z':
223 CHECK_CMDWIN
224#ifdef FEAT_VISUAL
225 reset_VIsual_and_resel(); /* stop Visual mode */
226#endif
227 do_cmdline_cmd((char_u *)"pclose");
228 break;
229
230/* cursor to preview window */
231 case 'P':
232 for (wp = firstwin; wp != NULL; wp = wp->w_next)
233 if (wp->w_p_pvw)
234 break;
235 if (wp == NULL)
236 EMSG(_("E441: There is no preview window"));
237 else
238 win_goto(wp);
239 break;
240#endif
241
242/* close all but current window */
243 case Ctrl_O:
244 case 'o':
245 CHECK_CMDWIN
246#ifdef FEAT_VISUAL
247 reset_VIsual_and_resel(); /* stop Visual mode */
248#endif
249 do_cmdline_cmd((char_u *)"only");
250 break;
251
252/* cursor to next window with wrap around */
253 case Ctrl_W:
254 case 'w':
255/* cursor to previous window with wrap around */
256 case 'W':
257 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000258 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 beep_flush();
260 else
261 {
262 if (Prenum) /* go to specified window */
263 {
264 for (wp = firstwin; --Prenum > 0; )
265 {
266 if (wp->w_next == NULL)
267 break;
268 else
269 wp = wp->w_next;
270 }
271 }
272 else
273 {
274 if (nchar == 'W') /* go to previous window */
275 {
276 wp = curwin->w_prev;
277 if (wp == NULL)
278 wp = lastwin; /* wrap around */
279 }
280 else /* go to next window */
281 {
282 wp = curwin->w_next;
283 if (wp == NULL)
284 wp = firstwin; /* wrap around */
285 }
286 }
287 win_goto(wp);
288 }
289 break;
290
291/* cursor to window below */
292 case 'j':
293 case K_DOWN:
294 case Ctrl_J:
295 CHECK_CMDWIN
296#ifdef FEAT_VERTSPLIT
297 win_goto_ver(FALSE, Prenum1);
298#else
299 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
300 wp = wp->w_next)
301 ;
302 win_goto(wp);
303#endif
304 break;
305
306/* cursor to window above */
307 case 'k':
308 case K_UP:
309 case Ctrl_K:
310 CHECK_CMDWIN
311#ifdef FEAT_VERTSPLIT
312 win_goto_ver(TRUE, Prenum1);
313#else
314 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
315 wp = wp->w_prev)
316 ;
317 win_goto(wp);
318#endif
319 break;
320
321#ifdef FEAT_VERTSPLIT
322/* cursor to left window */
323 case 'h':
324 case K_LEFT:
325 case Ctrl_H:
326 case K_BS:
327 CHECK_CMDWIN
328 win_goto_hor(TRUE, Prenum1);
329 break;
330
331/* cursor to right window */
332 case 'l':
333 case K_RIGHT:
334 case Ctrl_L:
335 CHECK_CMDWIN
336 win_goto_hor(FALSE, Prenum1);
337 break;
338#endif
339
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000340/* move window to new tab page */
341 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000342 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000343 MSG(_(m_onlyone));
344 else
345 {
346 tabpage_T *oldtab = curtab;
347 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000348
349 /* First create a new tab with the window, then go back to
350 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000351 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000352 if (win_new_tabpage((int)Prenum) == OK
353 && valid_tabpage(oldtab))
354 {
355 newtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +0200356 goto_tabpage_tp(oldtab, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000357 if (curwin == wp)
358 win_close(curwin, FALSE);
359 if (valid_tabpage(newtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +0200360 goto_tabpage_tp(newtab, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000361 }
362 }
363 break;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365/* cursor to top-left window */
366 case 't':
367 case Ctrl_T:
368 win_goto(firstwin);
369 break;
370
371/* cursor to bottom-right window */
372 case 'b':
373 case Ctrl_B:
374 win_goto(lastwin);
375 break;
376
377/* cursor to last accessed (previous) window */
378 case 'p':
379 case Ctrl_P:
380 if (prevwin == NULL)
381 beep_flush();
382 else
383 win_goto(prevwin);
384 break;
385
386/* exchange current and next window */
387 case 'x':
388 case Ctrl_X:
389 CHECK_CMDWIN
390 win_exchange(Prenum);
391 break;
392
393/* rotate windows downwards */
394 case Ctrl_R:
395 case 'r':
396 CHECK_CMDWIN
397#ifdef FEAT_VISUAL
398 reset_VIsual_and_resel(); /* stop Visual mode */
399#endif
400 win_rotate(FALSE, (int)Prenum1); /* downwards */
401 break;
402
403/* rotate windows upwards */
404 case 'R':
405 CHECK_CMDWIN
406#ifdef FEAT_VISUAL
407 reset_VIsual_and_resel(); /* stop Visual mode */
408#endif
409 win_rotate(TRUE, (int)Prenum1); /* upwards */
410 break;
411
412/* move window to the very top/bottom/left/right */
413 case 'K':
414 case 'J':
415#ifdef FEAT_VERTSPLIT
416 case 'H':
417 case 'L':
418#endif
419 CHECK_CMDWIN
420 win_totop((int)Prenum,
421 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
422 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
423 break;
424
425/* make all windows the same height */
426 case '=':
427#ifdef FEAT_GUI
428 need_mouse_correct = TRUE;
429#endif
430 win_equal(NULL, FALSE, 'b');
431 break;
432
433/* increase current window height */
434 case '+':
435#ifdef FEAT_GUI
436 need_mouse_correct = TRUE;
437#endif
438 win_setheight(curwin->w_height + (int)Prenum1);
439 break;
440
441/* decrease current window height */
442 case '-':
443#ifdef FEAT_GUI
444 need_mouse_correct = TRUE;
445#endif
446 win_setheight(curwin->w_height - (int)Prenum1);
447 break;
448
449/* set current window height */
450 case Ctrl__:
451 case '_':
452#ifdef FEAT_GUI
453 need_mouse_correct = TRUE;
454#endif
455 win_setheight(Prenum ? (int)Prenum : 9999);
456 break;
457
458#ifdef FEAT_VERTSPLIT
459/* increase current window width */
460 case '>':
461#ifdef FEAT_GUI
462 need_mouse_correct = TRUE;
463#endif
464 win_setwidth(curwin->w_width + (int)Prenum1);
465 break;
466
467/* decrease current window width */
468 case '<':
469#ifdef FEAT_GUI
470 need_mouse_correct = TRUE;
471#endif
472 win_setwidth(curwin->w_width - (int)Prenum1);
473 break;
474
475/* set current window width */
476 case '|':
477#ifdef FEAT_GUI
478 need_mouse_correct = TRUE;
479#endif
480 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
481 break;
482#endif
483
484/* jump to tag and split window if tag exists (in preview window) */
485#if defined(FEAT_QUICKFIX)
486 case '}':
487 CHECK_CMDWIN
488 if (Prenum)
489 g_do_tagpreview = Prenum;
490 else
491 g_do_tagpreview = p_pvh;
492 /*FALLTHROUGH*/
493#endif
494 case ']':
495 case Ctrl_RSB:
496 CHECK_CMDWIN
497#ifdef FEAT_VISUAL
498 reset_VIsual_and_resel(); /* stop Visual mode */
499#endif
500 if (Prenum)
501 postponed_split = Prenum;
502 else
503 postponed_split = -1;
504
505 /* Execute the command right here, required when
506 * "wincmd ]" was used in a function. */
507 do_nv_ident(Ctrl_RSB, NUL);
508 break;
509
510#ifdef FEAT_SEARCHPATH
511/* edit file name under cursor in a new window */
512 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000515wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000517
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (ptr != NULL)
520 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000521# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 setpcmark();
525 if (win_split(0, 0) == OK)
526 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200527 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000528 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
529 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000530 if (nchar == 'F' && lnum >= 0)
531 {
532 curwin->w_cursor.lnum = lnum;
533 check_cursor_lnum();
534 beginline(BL_SOL | BL_FIX);
535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
537 vim_free(ptr);
538 }
539 break;
540#endif
541
542#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000543/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 * new window -- webb
545 */
546 case 'i': /* Go to any match */
547 case Ctrl_I:
548 type = FIND_ANY;
549 /* FALLTHROUGH */
550 case 'd': /* Go to definition, using 'define' */
551 case Ctrl_D:
552 CHECK_CMDWIN
553 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
554 break;
555 find_pattern_in_path(ptr, 0, len, TRUE,
556 Prenum == 0 ? TRUE : FALSE, type,
557 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
558 curwin->w_set_curswant = TRUE;
559 break;
560#endif
561
Bram Moolenaar05159a02005-02-26 23:04:13 +0000562 case K_KENTER:
563 case CAR:
564#if defined(FEAT_QUICKFIX)
565 /*
566 * In a quickfix window a <CR> jumps to the error under the
567 * cursor in a new window.
568 */
569 if (bt_quickfix(curbuf))
570 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000571 sprintf((char *)cbuf, "split +%ld%s",
572 (long)curwin->w_cursor.lnum,
573 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000574 do_cmdline_cmd(cbuf);
575 }
576#endif
577 break;
578
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/* CTRL-W g extended commands */
581 case 'g':
582 case Ctrl_G:
583 CHECK_CMDWIN
584#ifdef USE_ON_FLY_SCROLL
585 dont_scroll = TRUE; /* disallow scrolling here */
586#endif
587 ++no_mapping;
588 ++allow_keys; /* no mapping for xchar, but allow key codes */
589 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000590 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 --no_mapping;
593 --allow_keys;
594#ifdef FEAT_CMDL_INFO
595 (void)add_to_showcmd(xchar);
596#endif
597 switch (xchar)
598 {
599#if defined(FEAT_QUICKFIX)
600 case '}':
601 xchar = Ctrl_RSB;
602 if (Prenum)
603 g_do_tagpreview = Prenum;
604 else
605 g_do_tagpreview = p_pvh;
606 /*FALLTHROUGH*/
607#endif
608 case ']':
609 case Ctrl_RSB:
610#ifdef FEAT_VISUAL
611 reset_VIsual_and_resel(); /* stop Visual mode */
612#endif
613 if (Prenum)
614 postponed_split = Prenum;
615 else
616 postponed_split = -1;
617
618 /* Execute the command right here, required when
619 * "wincmd g}" was used in a function. */
620 do_nv_ident('g', xchar);
621 break;
622
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000623#ifdef FEAT_SEARCHPATH
624 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100626 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000627 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000628 goto wingotofile;
629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 default:
631 beep_flush();
632 break;
633 }
634 break;
635
636 default: beep_flush();
637 break;
638 }
639}
640
641/*
642 * split the current window, implements CTRL-W s and :split
643 *
644 * "size" is the height or width for the new window, 0 to use half of current
645 * height or width.
646 *
647 * "flags":
648 * WSP_ROOM: require enough room for new window
649 * WSP_VERT: vertical split.
650 * WSP_TOP: open window at the top-left of the shell (help window).
651 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
652 * WSP_HELP: creating the help window, keep layout snapshot
653 *
654 * return FAIL for failure, OK otherwise
655 */
656 int
657win_split(size, flags)
658 int size;
659 int flags;
660{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000661 /* When the ":tab" modifier was used open a new tab page instead. */
662 if (may_open_tabpage() == OK)
663 return OK;
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 /* Add flags from ":vertical", ":topleft" and ":botright". */
666 flags |= cmdmod.split;
667 if ((flags & WSP_TOP) && (flags & WSP_BOT))
668 {
669 EMSG(_("E442: Can't split topleft and botright at the same time"));
670 return FAIL;
671 }
672
673 /* When creating the help window make a snapshot of the window layout.
674 * Otherwise clear the snapshot, it's now invalid. */
675 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000676 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 return win_split_ins(size, flags, NULL, 0);
681}
682
683/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100684 * When "new_wp" is NULL: split the current window in two.
685 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * top/left/right/bottom.
687 * return FAIL for failure, OK otherwise
688 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000689 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100690win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 int size;
692 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100693 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 int dir;
695{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100696 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 win_T *oldwin;
698 int new_size = size;
699 int i;
700 int need_status = 0;
701 int do_equal = FALSE;
702 int needed;
703 int available;
704 int oldwin_height = 0;
705 int layout;
706 frame_T *frp, *curfrp;
707 int before;
708
709 if (flags & WSP_TOP)
710 oldwin = firstwin;
711 else if (flags & WSP_BOT)
712 oldwin = lastwin;
713 else
714 oldwin = curwin;
715
716 /* add a status line when p_ls == 1 and splitting the first window */
717 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
718 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100719 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
721 EMSG(_(e_noroom));
722 return FAIL;
723 }
724 need_status = STATUS_HEIGHT;
725 }
726
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000727#ifdef FEAT_GUI
728 /* May be needed for the scrollbars that are going to change. */
729 if (gui.in_use)
730 out_flush();
731#endif
732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#ifdef FEAT_VERTSPLIT
734 if (flags & WSP_VERT)
735 {
736 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 /*
739 * Check if we are able to split the current window and compute its
740 * width.
741 */
742 needed = p_wmw + 1;
743 if (flags & WSP_ROOM)
744 needed += p_wiw - p_wmw;
745 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
746 {
747 available = topframe->fr_width;
748 needed += frame_minwidth(topframe, NULL);
749 }
750 else
751 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100752 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 EMSG(_(e_noroom));
755 return FAIL;
756 }
757 if (new_size == 0)
758 new_size = oldwin->w_width / 2;
759 if (new_size > oldwin->w_width - p_wmw - 1)
760 new_size = oldwin->w_width - p_wmw - 1;
761 if (new_size < p_wmw)
762 new_size = p_wmw;
763
764 /* if it doesn't fit in the current window, need win_equal() */
765 if (oldwin->w_width - new_size - 1 < p_wmw)
766 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000767
768 /* We don't like to take lines for the new window from a
769 * 'winfixwidth' window. Take them from a window to the left or right
770 * instead, if possible. */
771 if (oldwin->w_p_wfw)
772 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000773
774 /* Only make all windows the same width if one of them (except oldwin)
775 * is wider than one of the split windows. */
776 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
777 && oldwin->w_frame->fr_parent != NULL)
778 {
779 frp = oldwin->w_frame->fr_parent->fr_child;
780 while (frp != NULL)
781 {
782 if (frp->fr_win != oldwin && frp->fr_win != NULL
783 && (frp->fr_win->w_width > new_size
784 || frp->fr_win->w_width > oldwin->w_width
785 - new_size - STATUS_HEIGHT))
786 {
787 do_equal = TRUE;
788 break;
789 }
790 frp = frp->fr_next;
791 }
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 else
795#endif
796 {
797 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799 /*
800 * Check if we are able to split the current window and compute its
801 * height.
802 */
803 needed = p_wmh + STATUS_HEIGHT + need_status;
804 if (flags & WSP_ROOM)
805 needed += p_wh - p_wmh;
806 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
807 {
808 available = topframe->fr_height;
809 needed += frame_minheight(topframe, NULL);
810 }
811 else
812 {
813 available = oldwin->w_height;
814 needed += p_wmh;
815 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100816 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 EMSG(_(e_noroom));
819 return FAIL;
820 }
821 oldwin_height = oldwin->w_height;
822 if (need_status)
823 {
824 oldwin->w_status_height = STATUS_HEIGHT;
825 oldwin_height -= STATUS_HEIGHT;
826 }
827 if (new_size == 0)
828 new_size = oldwin_height / 2;
829
830 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
831 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
832 if (new_size < p_wmh)
833 new_size = p_wmh;
834
835 /* if it doesn't fit in the current window, need win_equal() */
836 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
837 do_equal = TRUE;
838
839 /* We don't like to take lines for the new window from a
840 * 'winfixheight' window. Take them from a window above or below
841 * instead, if possible. */
842 if (oldwin->w_p_wfh)
843 {
844 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
845 oldwin);
846 oldwin_height = oldwin->w_height;
847 if (need_status)
848 oldwin_height -= STATUS_HEIGHT;
849 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000850
851 /* Only make all windows the same height if one of them (except oldwin)
852 * is higher than one of the split windows. */
853 if (!do_equal && p_ea && size == 0
854#ifdef FEAT_VERTSPLIT
855 && *p_ead != 'h'
856#endif
857 && oldwin->w_frame->fr_parent != NULL)
858 {
859 frp = oldwin->w_frame->fr_parent->fr_child;
860 while (frp != NULL)
861 {
862 if (frp->fr_win != oldwin && frp->fr_win != NULL
863 && (frp->fr_win->w_height > new_size
864 || frp->fr_win->w_height > oldwin_height - new_size
865 - STATUS_HEIGHT))
866 {
867 do_equal = TRUE;
868 break;
869 }
870 frp = frp->fr_next;
871 }
872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874
875 /*
876 * allocate new window structure and link it in the window list
877 */
878 if ((flags & WSP_TOP) == 0
879 && ((flags & WSP_BOT)
880 || (flags & WSP_BELOW)
881 || (!(flags & WSP_ABOVE)
882 && (
883#ifdef FEAT_VERTSPLIT
884 (flags & WSP_VERT) ? p_spr :
885#endif
886 p_sb))))
887 {
888 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100889 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000890 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 else
892 win_append(oldwin, wp);
893 }
894 else
895 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100896 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000897 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else
899 win_append(oldwin->w_prev, wp);
900 }
901
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100902 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 if (wp == NULL)
905 return FAIL;
906
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000907 new_frame(wp);
908 if (wp->w_frame == NULL)
909 {
910 win_free(wp, NULL);
911 return FAIL;
912 }
913
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000914 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000915 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 /*
919 * Reorganise the tree of frames to insert the new window.
920 */
921 if (flags & (WSP_TOP | WSP_BOT))
922 {
923#ifdef FEAT_VERTSPLIT
924 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
925 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
926#else
927 if (topframe->fr_layout == FR_COL)
928#endif
929 {
930 curfrp = topframe->fr_child;
931 if (flags & WSP_BOT)
932 while (curfrp->fr_next != NULL)
933 curfrp = curfrp->fr_next;
934 }
935 else
936 curfrp = topframe;
937 before = (flags & WSP_TOP);
938 }
939 else
940 {
941 curfrp = oldwin->w_frame;
942 if (flags & WSP_BELOW)
943 before = FALSE;
944 else if (flags & WSP_ABOVE)
945 before = TRUE;
946 else
947#ifdef FEAT_VERTSPLIT
948 if (flags & WSP_VERT)
949 before = !p_spr;
950 else
951#endif
952 before = !p_sb;
953 }
954 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
955 {
956 /* Need to create a new frame in the tree to make a branch. */
957 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
958 *frp = *curfrp;
959 curfrp->fr_layout = layout;
960 frp->fr_parent = curfrp;
961 frp->fr_next = NULL;
962 frp->fr_prev = NULL;
963 curfrp->fr_child = frp;
964 curfrp->fr_win = NULL;
965 curfrp = frp;
966 if (frp->fr_win != NULL)
967 oldwin->w_frame = frp;
968 else
969 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
970 frp->fr_parent = curfrp;
971 }
972
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100973 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000974 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100976 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 frp->fr_parent = curfrp->fr_parent;
978
979 /* Insert the new frame at the right place in the frame list. */
980 if (before)
981 frame_insert(curfrp, frp);
982 else
983 frame_append(curfrp, frp);
984
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100985 /* Set w_fraction now so that the cursor keeps the same relative
986 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100987 if (oldwin->w_height > 0)
988 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100989 wp->w_fraction = oldwin->w_fraction;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_VERTSPLIT
992 if (flags & WSP_VERT)
993 {
994 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (need_status)
997 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100998 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 oldwin->w_status_height = need_status;
1000 }
1001 if (flags & (WSP_TOP | WSP_BOT))
1002 {
1003 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001004 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001005 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 wp->w_status_height = (p_ls > 0);
1007 }
1008 else
1009 {
1010 /* height and row of new window is same as current window */
1011 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001012 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 wp->w_status_height = oldwin->w_status_height;
1014 }
1015 frp->fr_height = curfrp->fr_height;
1016
1017 /* "new_size" of the current window goes to the new window, use
1018 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001019 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (before)
1021 wp->w_vsep_width = 1;
1022 else
1023 {
1024 wp->w_vsep_width = oldwin->w_vsep_width;
1025 oldwin->w_vsep_width = 1;
1026 }
1027 if (flags & (WSP_TOP | WSP_BOT))
1028 {
1029 if (flags & WSP_BOT)
1030 frame_add_vsep(curfrp);
1031 /* Set width of neighbor frame */
1032 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1034 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001037 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (before) /* new window left of current one */
1039 {
1040 wp->w_wincol = oldwin->w_wincol;
1041 oldwin->w_wincol += new_size + 1;
1042 }
1043 else /* new window right of current one */
1044 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1045 frame_fix_width(oldwin);
1046 frame_fix_width(wp);
1047 }
1048 else
1049#endif
1050 {
1051 /* width and column of new window is same as current window */
1052#ifdef FEAT_VERTSPLIT
1053 if (flags & (WSP_TOP | WSP_BOT))
1054 {
1055 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001056 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 wp->w_vsep_width = 0;
1058 }
1059 else
1060 {
1061 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001062 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 wp->w_vsep_width = oldwin->w_vsep_width;
1064 }
1065 frp->fr_width = curfrp->fr_width;
1066#endif
1067
1068 /* "new_size" of the current window goes to the new window, use
1069 * one row for the status line */
1070 win_new_height(wp, new_size);
1071 if (flags & (WSP_TOP | WSP_BOT))
1072 frame_new_height(curfrp, curfrp->fr_height
1073 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1074 else
1075 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1076 if (before) /* new window above current one */
1077 {
1078 wp->w_winrow = oldwin->w_winrow;
1079 wp->w_status_height = STATUS_HEIGHT;
1080 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1081 }
1082 else /* new window below current one */
1083 {
1084 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1085 wp->w_status_height = oldwin->w_status_height;
1086 oldwin->w_status_height = STATUS_HEIGHT;
1087 }
1088#ifdef FEAT_VERTSPLIT
1089 if (flags & WSP_BOT)
1090 frame_add_statusline(curfrp);
1091#endif
1092 frame_fix_height(wp);
1093 frame_fix_height(oldwin);
1094 }
1095
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 (void)win_comp_pos();
1098
1099 /*
1100 * Both windows need redrawing
1101 */
1102 redraw_win_later(wp, NOT_VALID);
1103 wp->w_redr_status = TRUE;
1104 redraw_win_later(oldwin, NOT_VALID);
1105 oldwin->w_redr_status = TRUE;
1106
1107 if (need_status)
1108 {
1109 msg_row = Rows - 1;
1110 msg_col = sc_col;
1111 msg_clr_eos_force(); /* Old command/ruler may still be there */
1112 comp_col();
1113 msg_row = Rows - 1;
1114 msg_col = 0; /* put position back at start of line */
1115 }
1116
1117 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001118 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 */
1120 if (do_equal || dir != 0)
1121 win_equal(wp, TRUE,
1122#ifdef FEAT_VERTSPLIT
1123 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1124 : dir == 'h' ? 'b' :
1125#endif
1126 'v');
1127
1128 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1129 * size was given. */
1130#ifdef FEAT_VERTSPLIT
1131 if (flags & WSP_VERT)
1132 {
1133 i = p_wiw;
1134 if (size != 0)
1135 p_wiw = size;
1136
1137# ifdef FEAT_GUI
1138 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1139 if (gui.in_use)
1140 gui_init_which_components(NULL);
1141# endif
1142 }
1143 else
1144#endif
1145 {
1146 i = p_wh;
1147 if (size != 0)
1148 p_wh = size;
1149 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150
1151 /*
1152 * make the new window the current window
1153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 win_enter(wp, FALSE);
1155#ifdef FEAT_VERTSPLIT
1156 if (flags & WSP_VERT)
1157 p_wiw = i;
1158 else
1159#endif
1160 p_wh = i;
1161
1162 return OK;
1163}
1164
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001165
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001166/*
1167 * Initialize window "newp" from window "oldp".
1168 * Used when splitting a window and when creating a new tab page.
1169 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170 * WSP_NEWLOC may be specified in flags to prevent the location list from
1171 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001172 */
1173 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001174win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001175 win_T *newp;
1176 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001177 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001178{
1179 int i;
1180
1181 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001182#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001183 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001185 oldp->w_buffer->b_nwindows++;
1186 newp->w_cursor = oldp->w_cursor;
1187 newp->w_valid = 0;
1188 newp->w_curswant = oldp->w_curswant;
1189 newp->w_set_curswant = oldp->w_set_curswant;
1190 newp->w_topline = oldp->w_topline;
1191#ifdef FEAT_DIFF
1192 newp->w_topfill = oldp->w_topfill;
1193#endif
1194 newp->w_leftcol = oldp->w_leftcol;
1195 newp->w_pcmark = oldp->w_pcmark;
1196 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1197 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001198 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001199 newp->w_fraction = oldp->w_fraction;
1200 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1201#ifdef FEAT_JUMPLIST
1202 copy_jumplist(oldp, newp);
1203#endif
1204#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001205 if (flags & WSP_NEWLOC)
1206 {
1207 /* Don't copy the location list. */
1208 newp->w_llist = NULL;
1209 newp->w_llist_ref = NULL;
1210 }
1211 else
1212 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001213#endif
1214 if (oldp->w_localdir != NULL)
1215 newp->w_localdir = vim_strsave(oldp->w_localdir);
1216
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001217 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001218 for (i = 0; i < oldp->w_tagstacklen; i++)
1219 {
1220 newp->w_tagstack[i] = oldp->w_tagstack[i];
1221 if (newp->w_tagstack[i].tagname != NULL)
1222 newp->w_tagstack[i].tagname =
1223 vim_strsave(newp->w_tagstack[i].tagname);
1224 }
1225 newp->w_tagstackidx = oldp->w_tagstackidx;
1226 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001227#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001228 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001230
1231 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001232
Bram Moolenaara971b822011-09-14 14:43:25 +02001233#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001234 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001235#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001236}
1237
1238/*
1239 * Initialize window "newp" from window"old".
1240 * Only the essential things are copied.
1241 */
1242 static void
1243win_init_some(newp, oldp)
1244 win_T *newp;
1245 win_T *oldp;
1246{
1247 /* Use the same argument list. */
1248 newp->w_alist = oldp->w_alist;
1249 ++newp->w_alist->al_refcount;
1250 newp->w_arg_idx = oldp->w_arg_idx;
1251
1252 /* copy options from existing window */
1253 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001254}
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif /* FEAT_WINDOWS */
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#if defined(FEAT_WINDOWS) || defined(PROTO)
1259/*
1260 * Check if "win" is a pointer to an existing window.
1261 */
1262 int
1263win_valid(win)
1264 win_T *win;
1265{
1266 win_T *wp;
1267
1268 if (win == NULL)
1269 return FALSE;
1270 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1271 if (wp == win)
1272 return TRUE;
1273 return FALSE;
1274}
1275
1276/*
1277 * Return the number of windows.
1278 */
1279 int
1280win_count()
1281{
1282 win_T *wp;
1283 int count = 0;
1284
1285 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1286 ++count;
1287 return count;
1288}
1289
1290/*
1291 * Make "count" windows on the screen.
1292 * Return actual number of windows on the screen.
1293 * Must be called when there is just one window, filling the whole screen
1294 * (excluding the command line).
1295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 int
1297make_windows(count, vertical)
1298 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001299 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int maxcount;
1302 int todo;
1303
1304#ifdef FEAT_VERTSPLIT
1305 if (vertical)
1306 {
1307 /* Each windows needs at least 'winminwidth' lines and a separator
1308 * column. */
1309 maxcount = (curwin->w_width + curwin->w_vsep_width
1310 - (p_wiw - p_wmw)) / (p_wmw + 1);
1311 }
1312 else
1313#endif
1314 {
1315 /* Each window needs at least 'winminheight' lines and a status line. */
1316 maxcount = (curwin->w_height + curwin->w_status_height
1317 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1318 }
1319
1320 if (maxcount < 2)
1321 maxcount = 2;
1322 if (count > maxcount)
1323 count = maxcount;
1324
1325 /*
1326 * add status line now, otherwise first window will be too big
1327 */
1328 if (count > 1)
1329 last_status(TRUE);
1330
1331#ifdef FEAT_AUTOCMD
1332 /*
1333 * Don't execute autocommands while creating the windows. Must do that
1334 * when putting the buffers in the windows.
1335 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001336 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
1338
1339 /* todo is number of windows left to create */
1340 for (todo = count - 1; todo > 0; --todo)
1341#ifdef FEAT_VERTSPLIT
1342 if (vertical)
1343 {
1344 if (win_split(curwin->w_width - (curwin->w_width - todo)
1345 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1346 break;
1347 }
1348 else
1349#endif
1350 {
1351 if (win_split(curwin->w_height - (curwin->w_height - todo
1352 * STATUS_HEIGHT) / (todo + 1)
1353 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1354 break;
1355 }
1356
1357#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001358 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#endif
1360
1361 /* return actual number of windows */
1362 return (count - todo);
1363}
1364
1365/*
1366 * Exchange current and next window
1367 */
1368 static void
1369win_exchange(Prenum)
1370 long Prenum;
1371{
1372 frame_T *frp;
1373 frame_T *frp2;
1374 win_T *wp;
1375 win_T *wp2;
1376 int temp;
1377
1378 if (lastwin == firstwin) /* just one window */
1379 {
1380 beep_flush();
1381 return;
1382 }
1383
1384#ifdef FEAT_GUI
1385 need_mouse_correct = TRUE;
1386#endif
1387
1388 /*
1389 * find window to exchange with
1390 */
1391 if (Prenum)
1392 {
1393 frp = curwin->w_frame->fr_parent->fr_child;
1394 while (frp != NULL && --Prenum > 0)
1395 frp = frp->fr_next;
1396 }
1397 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1398 frp = curwin->w_frame->fr_next;
1399 else /* Swap last window in row/col with previous */
1400 frp = curwin->w_frame->fr_prev;
1401
1402 /* We can only exchange a window with another window, not with a frame
1403 * containing windows. */
1404 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1405 return;
1406 wp = frp->fr_win;
1407
1408/*
1409 * 1. remove curwin from the list. Remember after which window it was in wp2
1410 * 2. insert curwin before wp in the list
1411 * if wp != wp2
1412 * 3. remove wp from the list
1413 * 4. insert wp after wp2
1414 * 5. exchange the status line height and vsep width.
1415 */
1416 wp2 = curwin->w_prev;
1417 frp2 = curwin->w_frame->fr_prev;
1418 if (wp->w_prev != curwin)
1419 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 frame_remove(curwin->w_frame);
1422 win_append(wp->w_prev, curwin);
1423 frame_insert(frp, curwin->w_frame);
1424 }
1425 if (wp != wp2)
1426 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001427 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 frame_remove(wp->w_frame);
1429 win_append(wp2, wp);
1430 if (frp2 == NULL)
1431 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1432 else
1433 frame_append(frp2, wp->w_frame);
1434 }
1435 temp = curwin->w_status_height;
1436 curwin->w_status_height = wp->w_status_height;
1437 wp->w_status_height = temp;
1438#ifdef FEAT_VERTSPLIT
1439 temp = curwin->w_vsep_width;
1440 curwin->w_vsep_width = wp->w_vsep_width;
1441 wp->w_vsep_width = temp;
1442
1443 /* If the windows are not in the same frame, exchange the sizes to avoid
1444 * messing up the window layout. Otherwise fix the frame sizes. */
1445 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1446 {
1447 temp = curwin->w_height;
1448 curwin->w_height = wp->w_height;
1449 wp->w_height = temp;
1450 temp = curwin->w_width;
1451 curwin->w_width = wp->w_width;
1452 wp->w_width = temp;
1453 }
1454 else
1455 {
1456 frame_fix_height(curwin);
1457 frame_fix_height(wp);
1458 frame_fix_width(curwin);
1459 frame_fix_width(wp);
1460 }
1461#endif
1462
1463 (void)win_comp_pos(); /* recompute window positions */
1464
1465 win_enter(wp, TRUE);
1466 redraw_later(CLEAR);
1467}
1468
1469/*
1470 * rotate windows: if upwards TRUE the second window becomes the first one
1471 * if upwards FALSE the first window becomes the second one
1472 */
1473 static void
1474win_rotate(upwards, count)
1475 int upwards;
1476 int count;
1477{
1478 win_T *wp1;
1479 win_T *wp2;
1480 frame_T *frp;
1481 int n;
1482
1483 if (firstwin == lastwin) /* nothing to do */
1484 {
1485 beep_flush();
1486 return;
1487 }
1488
1489#ifdef FEAT_GUI
1490 need_mouse_correct = TRUE;
1491#endif
1492
1493#ifdef FEAT_VERTSPLIT
1494 /* Check if all frames in this row/col have one window. */
1495 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1496 frp = frp->fr_next)
1497 if (frp->fr_win == NULL)
1498 {
1499 EMSG(_("E443: Cannot rotate when another window is split"));
1500 return;
1501 }
1502#endif
1503
1504 while (count--)
1505 {
1506 if (upwards) /* first window becomes last window */
1507 {
1508 /* remove first window/frame from the list */
1509 frp = curwin->w_frame->fr_parent->fr_child;
1510 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001511 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 frame_remove(frp);
1513
1514 /* find last frame and append removed window/frame after it */
1515 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1516 ;
1517 win_append(frp->fr_win, wp1);
1518 frame_append(frp, wp1->w_frame);
1519
1520 wp2 = frp->fr_win; /* previously last window */
1521 }
1522 else /* last window becomes first window */
1523 {
1524 /* find last window/frame in the list and remove it */
1525 for (frp = curwin->w_frame; frp->fr_next != NULL;
1526 frp = frp->fr_next)
1527 ;
1528 wp1 = frp->fr_win;
1529 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001530 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 frame_remove(frp);
1532
1533 /* append the removed window/frame before the first in the list */
1534 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1535 frame_insert(frp->fr_parent->fr_child, frp);
1536 }
1537
1538 /* exchange status height and vsep width of old and new last window */
1539 n = wp2->w_status_height;
1540 wp2->w_status_height = wp1->w_status_height;
1541 wp1->w_status_height = n;
1542 frame_fix_height(wp1);
1543 frame_fix_height(wp2);
1544#ifdef FEAT_VERTSPLIT
1545 n = wp2->w_vsep_width;
1546 wp2->w_vsep_width = wp1->w_vsep_width;
1547 wp1->w_vsep_width = n;
1548 frame_fix_width(wp1);
1549 frame_fix_width(wp2);
1550#endif
1551
1552 /* recompute w_winrow and w_wincol for all windows */
1553 (void)win_comp_pos();
1554 }
1555
1556 redraw_later(CLEAR);
1557}
1558
1559/*
1560 * Move the current window to the very top/bottom/left/right of the screen.
1561 */
1562 static void
1563win_totop(size, flags)
1564 int size;
1565 int flags;
1566{
1567 int dir;
1568 int height = curwin->w_height;
1569
1570 if (lastwin == firstwin)
1571 {
1572 beep_flush();
1573 return;
1574 }
1575
1576 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001577 (void)winframe_remove(curwin, &dir, NULL);
1578 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 last_status(FALSE); /* may need to remove last status line */
1580 (void)win_comp_pos(); /* recompute window positions */
1581
1582 /* Split a window on the desired side and put the window there. */
1583 (void)win_split_ins(size, flags, curwin, dir);
1584 if (!(flags & WSP_VERT))
1585 {
1586 win_setheight(height);
1587 if (p_ea)
1588 win_equal(curwin, TRUE, 'v');
1589 }
1590
1591#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1592 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1593 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001594 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596}
1597
1598/*
1599 * Move window "win1" to below/right of "win2" and make "win1" the current
1600 * window. Only works within the same frame!
1601 */
1602 void
1603win_move_after(win1, win2)
1604 win_T *win1, *win2;
1605{
1606 int height;
1607
1608 /* check if the arguments are reasonable */
1609 if (win1 == win2)
1610 return;
1611
1612 /* check if there is something to do */
1613 if (win2->w_next != win1)
1614 {
1615 /* may need move the status line/vertical separator of the last window
1616 * */
1617 if (win1 == lastwin)
1618 {
1619 height = win1->w_prev->w_status_height;
1620 win1->w_prev->w_status_height = win1->w_status_height;
1621 win1->w_status_height = height;
1622#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001623 if (win1->w_prev->w_vsep_width == 1)
1624 {
1625 /* Remove the vertical separator from the last-but-one window,
1626 * add it to the last window. Adjust the frame widths. */
1627 win1->w_prev->w_vsep_width = 0;
1628 win1->w_prev->w_frame->fr_width -= 1;
1629 win1->w_vsep_width = 1;
1630 win1->w_frame->fr_width += 1;
1631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633 }
1634 else if (win2 == lastwin)
1635 {
1636 height = win1->w_status_height;
1637 win1->w_status_height = win2->w_status_height;
1638 win2->w_status_height = height;
1639#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001640 if (win1->w_vsep_width == 1)
1641 {
1642 /* Remove the vertical separator from win1, add it to the last
1643 * window, win2. Adjust the frame widths. */
1644 win2->w_vsep_width = 1;
1645 win2->w_frame->fr_width += 1;
1646 win1->w_vsep_width = 0;
1647 win1->w_frame->fr_width -= 1;
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001651 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 frame_remove(win1->w_frame);
1653 win_append(win2, win1);
1654 frame_append(win2->w_frame, win1->w_frame);
1655
1656 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1657 redraw_later(NOT_VALID);
1658 }
1659 win_enter(win1, FALSE);
1660}
1661
1662/*
1663 * Make all windows the same height.
1664 * 'next_curwin' will soon be the current window, make sure it has enough
1665 * rows.
1666 */
1667 void
1668win_equal(next_curwin, current, dir)
1669 win_T *next_curwin; /* pointer to current window to be or NULL */
1670 int current; /* do only frame with current window */
1671 int dir; /* 'v' for vertically, 'h' for horizontally,
1672 'b' for both, 0 for using p_ead */
1673{
1674 if (dir == 0)
1675#ifdef FEAT_VERTSPLIT
1676 dir = *p_ead;
1677#else
1678 dir = 'b';
1679#endif
1680 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001681 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001682 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683}
1684
1685/*
1686 * Set a frame to a new position and height, spreading the available room
1687 * equally over contained frames.
1688 * The window "next_curwin" (if not NULL) should at least get the size from
1689 * 'winheight' and 'winwidth' if possible.
1690 */
1691 static void
1692win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1693 win_T *next_curwin; /* pointer to current window to be or NULL */
1694 int current; /* do only frame with current window */
1695 frame_T *topfr; /* frame to set size off */
1696 int dir; /* 'v', 'h' or 'b', see win_equal() */
1697 int col; /* horizontal position for frame */
1698 int row; /* vertical position for frame */
1699 int width; /* new width of frame */
1700 int height; /* new height of frame */
1701{
1702 int n, m;
1703 int extra_sep = 0;
1704 int wincount, totwincount = 0;
1705 frame_T *fr;
1706 int next_curwin_size = 0;
1707 int room = 0;
1708 int new_size;
1709 int has_next_curwin = 0;
1710 int hnc;
1711
1712 if (topfr->fr_layout == FR_LEAF)
1713 {
1714 /* Set the width/height of this frame.
1715 * Redraw when size or position changes */
1716 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1717#ifdef FEAT_VERTSPLIT
1718 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1719#endif
1720 )
1721 {
1722 topfr->fr_win->w_winrow = row;
1723 frame_new_height(topfr, height, FALSE, FALSE);
1724#ifdef FEAT_VERTSPLIT
1725 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001726 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 redraw_all_later(CLEAR);
1729 }
1730 }
1731#ifdef FEAT_VERTSPLIT
1732 else if (topfr->fr_layout == FR_ROW)
1733 {
1734 topfr->fr_width = width;
1735 topfr->fr_height = height;
1736
1737 if (dir != 'v') /* equalize frame widths */
1738 {
1739 /* Compute the maximum number of windows horizontally in this
1740 * frame. */
1741 n = frame_minwidth(topfr, NOWIN);
1742 /* add one for the rightmost window, it doesn't have a separator */
1743 if (col + width == Columns)
1744 extra_sep = 1;
1745 else
1746 extra_sep = 0;
1747 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001748 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001750 /*
1751 * Compute width for "next_curwin" window and room available for
1752 * other windows.
1753 * "m" is the minimal width when counting p_wiw for "next_curwin".
1754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 m = frame_minwidth(topfr, next_curwin);
1756 room = width - m;
1757 if (room < 0)
1758 {
1759 next_curwin_size = p_wiw + room;
1760 room = 0;
1761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 else
1763 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001764 next_curwin_size = -1;
1765 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1766 {
1767 /* If 'winfixwidth' set keep the window width if
1768 * possible.
1769 * Watch out for this window being the next_curwin. */
1770 if (frame_fixed_width(fr))
1771 {
1772 n = frame_minwidth(fr, NOWIN);
1773 new_size = fr->fr_width;
1774 if (frame_has_win(fr, next_curwin))
1775 {
1776 room += p_wiw - p_wmw;
1777 next_curwin_size = 0;
1778 if (new_size < p_wiw)
1779 new_size = p_wiw;
1780 }
1781 else
1782 /* These windows don't use up room. */
1783 totwincount -= (n + (fr->fr_next == NULL
1784 ? extra_sep : 0)) / (p_wmw + 1);
1785 room -= new_size - n;
1786 if (room < 0)
1787 {
1788 new_size += room;
1789 room = 0;
1790 }
1791 fr->fr_newwidth = new_size;
1792 }
1793 }
1794 if (next_curwin_size == -1)
1795 {
1796 if (!has_next_curwin)
1797 next_curwin_size = 0;
1798 else if (totwincount > 1
1799 && (room + (totwincount - 2))
1800 / (totwincount - 1) > p_wiw)
1801 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001802 /* Can make all windows wider than 'winwidth', spread
1803 * the room equally. */
1804 next_curwin_size = (room + p_wiw
1805 + (totwincount - 1) * p_wmw
1806 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001807 room -= next_curwin_size - p_wiw;
1808 }
1809 else
1810 next_curwin_size = p_wiw;
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001813
1814 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 --totwincount; /* don't count curwin */
1816 }
1817
1818 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1819 {
1820 n = m = 0;
1821 wincount = 1;
1822 if (fr->fr_next == NULL)
1823 /* last frame gets all that remains (avoid roundoff error) */
1824 new_size = width;
1825 else if (dir == 'v')
1826 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001827 else if (frame_fixed_width(fr))
1828 {
1829 new_size = fr->fr_newwidth;
1830 wincount = 0; /* doesn't count as a sizeable window */
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 else
1833 {
1834 /* Compute the maximum number of windows horiz. in "fr". */
1835 n = frame_minwidth(fr, NOWIN);
1836 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1837 / (p_wmw + 1);
1838 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 if (has_next_curwin)
1840 hnc = frame_has_win(fr, next_curwin);
1841 else
1842 hnc = FALSE;
1843 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 if (totwincount == 0)
1846 new_size = room;
1847 else
1848 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 next_curwin_size -= p_wiw - (m - n);
1853 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001854 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 else
1857 room -= new_size;
1858 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001861 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 * window, unless equalizing all frames. */
1863 if (!current || dir != 'v' || topfr->fr_parent != NULL
1864 || (new_size != fr->fr_width)
1865 || frame_has_win(fr, next_curwin))
1866 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001867 new_size, height);
1868 col += new_size;
1869 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 totwincount -= wincount;
1871 }
1872 }
1873#endif
1874 else /* topfr->fr_layout == FR_COL */
1875 {
1876#ifdef FEAT_VERTSPLIT
1877 topfr->fr_width = width;
1878#endif
1879 topfr->fr_height = height;
1880
1881 if (dir != 'h') /* equalize frame heights */
1882 {
1883 /* Compute maximum number of windows vertically in this frame. */
1884 n = frame_minheight(topfr, NOWIN);
1885 /* add one for the bottom window if it doesn't have a statusline */
1886 if (row + height == cmdline_row && p_ls == 0)
1887 extra_sep = 1;
1888 else
1889 extra_sep = 0;
1890 totwincount = (n + extra_sep) / (p_wmh + 1);
1891 has_next_curwin = frame_has_win(topfr, next_curwin);
1892
1893 /*
1894 * Compute height for "next_curwin" window and room available for
1895 * other windows.
1896 * "m" is the minimal height when counting p_wh for "next_curwin".
1897 */
1898 m = frame_minheight(topfr, next_curwin);
1899 room = height - m;
1900 if (room < 0)
1901 {
1902 /* The room is less then 'winheight', use all space for the
1903 * current window. */
1904 next_curwin_size = p_wh + room;
1905 room = 0;
1906 }
1907 else
1908 {
1909 next_curwin_size = -1;
1910 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1911 {
1912 /* If 'winfixheight' set keep the window height if
1913 * possible.
1914 * Watch out for this window being the next_curwin. */
1915 if (frame_fixed_height(fr))
1916 {
1917 n = frame_minheight(fr, NOWIN);
1918 new_size = fr->fr_height;
1919 if (frame_has_win(fr, next_curwin))
1920 {
1921 room += p_wh - p_wmh;
1922 next_curwin_size = 0;
1923 if (new_size < p_wh)
1924 new_size = p_wh;
1925 }
1926 else
1927 /* These windows don't use up room. */
1928 totwincount -= (n + (fr->fr_next == NULL
1929 ? extra_sep : 0)) / (p_wmh + 1);
1930 room -= new_size - n;
1931 if (room < 0)
1932 {
1933 new_size += room;
1934 room = 0;
1935 }
1936 fr->fr_newheight = new_size;
1937 }
1938 }
1939 if (next_curwin_size == -1)
1940 {
1941 if (!has_next_curwin)
1942 next_curwin_size = 0;
1943 else if (totwincount > 1
1944 && (room + (totwincount - 2))
1945 / (totwincount - 1) > p_wh)
1946 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001947 /* can make all windows higher than 'winheight',
1948 * spread the room equally. */
1949 next_curwin_size = (room + p_wh
1950 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 + (totwincount - 1)) / totwincount;
1952 room -= next_curwin_size - p_wh;
1953 }
1954 else
1955 next_curwin_size = p_wh;
1956 }
1957 }
1958
1959 if (has_next_curwin)
1960 --totwincount; /* don't count curwin */
1961 }
1962
1963 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1964 {
1965 n = m = 0;
1966 wincount = 1;
1967 if (fr->fr_next == NULL)
1968 /* last frame gets all that remains (avoid roundoff error) */
1969 new_size = height;
1970 else if (dir == 'h')
1971 new_size = fr->fr_height;
1972 else if (frame_fixed_height(fr))
1973 {
1974 new_size = fr->fr_newheight;
1975 wincount = 0; /* doesn't count as a sizeable window */
1976 }
1977 else
1978 {
1979 /* Compute the maximum number of windows vert. in "fr". */
1980 n = frame_minheight(fr, NOWIN);
1981 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1982 / (p_wmh + 1);
1983 m = frame_minheight(fr, next_curwin);
1984 if (has_next_curwin)
1985 hnc = frame_has_win(fr, next_curwin);
1986 else
1987 hnc = FALSE;
1988 if (hnc) /* don't count next_curwin */
1989 --wincount;
1990 if (totwincount == 0)
1991 new_size = room;
1992 else
1993 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1994 / totwincount;
1995 if (hnc) /* add next_curwin size */
1996 {
1997 next_curwin_size -= p_wh - (m - n);
1998 new_size += next_curwin_size;
1999 room -= new_size - next_curwin_size;
2000 }
2001 else
2002 room -= new_size;
2003 new_size += n;
2004 }
2005 /* Skip frame that is full width when splitting or closing a
2006 * window, unless equalizing all frames. */
2007 if (!current || dir != 'h' || topfr->fr_parent != NULL
2008 || (new_size != fr->fr_height)
2009 || frame_has_win(fr, next_curwin))
2010 win_equal_rec(next_curwin, current, fr, dir, col, row,
2011 width, new_size);
2012 row += new_size;
2013 height -= new_size;
2014 totwincount -= wincount;
2015 }
2016 }
2017}
2018
2019/*
2020 * close all windows for buffer 'buf'
2021 */
2022 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 win_T *wp;
2028 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002029 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002032
2033 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002035 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2036#ifdef FEAT_AUTOCMD
2037 && !(wp->w_closing || wp->w_buffer->b_closing)
2038#endif
2039 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002041 win_close(wp, FALSE);
2042
2043 /* Start all over, autocommands may change the window layout. */
2044 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049
2050 /* Also check windows in other tab pages. */
2051 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2052 {
2053 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002054 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002055 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002056 if (wp->w_buffer == buf
2057#ifdef FEAT_AUTOCMD
2058 && !(wp->w_closing || wp->w_buffer->b_closing)
2059#endif
2060 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002061 {
2062 win_close_othertab(wp, FALSE, tp);
2063
2064 /* Start all over, the tab page may be closed and
2065 * autocommands may change the window layout. */
2066 nexttp = first_tabpage;
2067 break;
2068 }
2069 }
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002072
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002073 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002074 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075}
2076
2077/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002078 * Return TRUE if the current window is the only window that exists (ignoring
2079 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002080 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002081 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002082 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002083last_window()
2084{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002085 return (one_window() && first_tabpage->tp_next == NULL);
2086}
2087
2088/*
2089 * Return TRUE if there is only one window other than "aucmd_win" in the
2090 * current tab page.
2091 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002092 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002093one_window()
2094{
2095#ifdef FEAT_AUTOCMD
2096 win_T *wp;
2097 int seen_one = FALSE;
2098
2099 FOR_ALL_WINDOWS(wp)
2100 {
2101 if (wp != aucmd_win)
2102 {
2103 if (seen_one)
2104 return FALSE;
2105 seen_one = TRUE;
2106 }
2107 }
2108 return TRUE;
2109#else
2110 return firstwin == lastwin;
2111#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002112}
2113
2114/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002115 * Close the possibly last window in a tab page.
2116 * Returns TRUE when the window was closed already.
2117 */
2118 static int
2119close_last_window_tabpage(win, free_buf, prev_curtab)
2120 win_T *win;
2121 int free_buf;
2122 tabpage_T *prev_curtab;
2123{
2124 if (firstwin == lastwin)
2125 {
2126 /*
2127 * Closing the last window in a tab page. First go to another tab
2128 * page and then close the window and the tab page. This avoids that
2129 * curwin and curtab are invalid while we are freeing memory, they may
2130 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002131 * Don't trigger autocommands yet, they may use wrong values, so do
2132 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002133 */
Bram Moolenaara8596c42012-06-13 14:28:20 +02002134 goto_tabpage_tp(alt_tabpage(), FALSE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002135 redraw_tabline = TRUE;
2136
2137 /* Safety check: Autocommands may have closed the window when jumping
2138 * to the other tab page. */
2139 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2140 {
2141 int h = tabline_height();
2142
2143 win_close_othertab(win, free_buf, prev_curtab);
2144 if (h != tabline_height())
2145 shell_new_rows();
2146 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002147 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2148 * that now. */
2149#ifdef FEAT_AUTOCMD
2150 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2151 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2152#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002153 return TRUE;
2154 }
2155 return FALSE;
2156}
2157
2158/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * If "free_buf" is TRUE related buffer may be unloaded.
2161 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002162 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
2164 void
2165win_close(win, free_buf)
2166 win_T *win;
2167 int free_buf;
2168{
2169 win_T *wp;
2170#ifdef FEAT_AUTOCMD
2171 int other_buffer = FALSE;
2172#endif
2173 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 int dir;
2175 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002176 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002178 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
2180 EMSG(_("E444: Cannot close last window"));
2181 return;
2182 }
2183
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002184#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002185 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002186 return; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002187 if (win == aucmd_win)
2188 {
2189 EMSG(_("E813: Cannot close autocmd window"));
2190 return;
2191 }
2192 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2193 {
2194 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2195 return;
2196 }
2197#endif
2198
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002199 /* When closing the last window in a tab page first go to another tab page
2200 * and then close the window and the tab page to avoid that curwin and
2201 * curtab are invalid while we are freeing memory. */
2202 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2203 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 /* When closing the help window, try restoring a snapshot after closing
2206 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002207 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 help_window = TRUE;
2209 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002210 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212#ifdef FEAT_AUTOCMD
2213 if (win == curwin)
2214 {
2215 /*
2216 * Guess which window is going to be the new current window.
2217 * This may change because of the autocommands (sigh).
2218 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002219 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002222 * Be careful: If autocommands delete the window or cause this window
2223 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 */
2225 if (wp->w_buffer != curbuf)
2226 {
2227 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002228 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002230 if (!win_valid(win))
2231 return;
2232 win->w_closing = FALSE;
2233 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return;
2235 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002236 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002238 if (!win_valid(win))
2239 return;
2240 win->w_closing = FALSE;
2241 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 return;
2243# ifdef FEAT_EVAL
2244 /* autocmds may abort script processing */
2245 if (aborting())
2246 return;
2247# endif
2248 }
2249#endif
2250
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002251#ifdef FEAT_GUI
2252 /* Avoid trouble with scrollbars that are going to be deleted in
2253 * win_free(). */
2254 if (gui.in_use)
2255 out_flush();
2256#endif
2257
Bram Moolenaara971b822011-09-14 14:43:25 +02002258#ifdef FEAT_SYN_HL
2259 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002260 if (win->w_buffer != NULL)
2261 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002262#endif
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 /*
2265 * Close the link to the buffer.
2266 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002267 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002268 {
2269#ifdef FEAT_AUTOCMD
2270 win->w_closing = TRUE;
2271#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002272 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002273#ifdef FEAT_AUTOCMD
2274 if (win_valid(win))
2275 win->w_closing = FALSE;
2276#endif
2277 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002278
Bram Moolenaar802418d2013-01-17 14:00:11 +01002279 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2280 && (last_window() || curtab != prev_curtab
2281 || close_last_window_tabpage(win, free_buf, prev_curtab)))
2282 /* Autocommands have close all windows, quit now. */
2283 getout(0);
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002286 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002287 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002288 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return;
2290
Bram Moolenaara971b822011-09-14 14:43:25 +02002291 /* Free the memory used for the window and get the window that received
2292 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002293 wp = win_free_mem(win, &dir, NULL);
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 /* Make sure curwin isn't invalid. It can cause severe trouble when
2296 * printing an error message. For win_equal() curbuf needs to be valid
2297 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002298 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 curwin = wp;
2301#ifdef FEAT_QUICKFIX
2302 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2303 {
2304 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002305 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * finding another window to go to.
2307 */
2308 for (;;)
2309 {
2310 if (wp->w_next == NULL)
2311 wp = firstwin;
2312 else
2313 wp = wp->w_next;
2314 if (wp == curwin)
2315 break;
2316 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2317 {
2318 curwin = wp;
2319 break;
2320 }
2321 }
2322 }
2323#endif
2324 curbuf = curwin->w_buffer;
2325 close_curwin = TRUE;
2326 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002327 if (p_ea
2328#ifdef FEAT_VERTSPLIT
2329 && (*p_ead == 'b' || *p_ead == dir)
2330#endif
2331 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 win_equal(curwin, TRUE,
2333#ifdef FEAT_VERTSPLIT
2334 dir
2335#else
2336 0
2337#endif
2338 );
2339 else
2340 win_comp_pos();
2341 if (close_curwin)
2342 {
2343 win_enter_ext(wp, FALSE, TRUE);
2344#ifdef FEAT_AUTOCMD
2345 if (other_buffer)
2346 /* careful: after this wp and win may be invalid! */
2347 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2348#endif
2349 }
2350
2351 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002352 * If last window has a status line now and we don't want one,
2353 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 */
2355 last_status(FALSE);
2356
2357 /* After closing the help window, try restoring the window layout from
2358 * before it was opened. */
2359 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002360 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2363 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2364 if (gui.in_use && !win_hasvertsplit())
2365 gui_init_which_components(NULL);
2366#endif
2367
2368 redraw_all_later(NOT_VALID);
2369}
2370
2371/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002372 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002373 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002374 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002375 * Caller must check if buffer is hidden and whether the tabline needs to be
2376 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002377 */
2378 void
2379win_close_othertab(win, free_buf, tp)
2380 win_T *win;
2381 int free_buf;
2382 tabpage_T *tp;
2383{
2384 win_T *wp;
2385 int dir;
2386 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002387 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002388
Bram Moolenaar362ce482012-06-06 19:02:45 +02002389#ifdef FEAT_AUTOCMD
2390 if (win->w_closing || win->w_buffer->b_closing)
2391 return; /* window is already being closed */
2392#endif
2393
Bram Moolenaarf740b292006-02-16 22:11:02 +00002394 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002395 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002396
2397 /* Careful: Autocommands may have closed the tab page or made it the
2398 * current tab page. */
2399 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2400 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002401 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002402 return;
2403
2404 /* Autocommands may have closed the window already. */
2405 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2406 ;
2407 if (wp == NULL)
2408 return;
2409
Bram Moolenaarf740b292006-02-16 22:11:02 +00002410 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002411 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002412 {
2413 if (tp == first_tabpage)
2414 first_tabpage = tp->tp_next;
2415 else
2416 {
2417 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2418 ptp = ptp->tp_next)
2419 ;
2420 if (ptp == NULL)
2421 {
2422 EMSG2(_(e_intern2), "win_close_othertab()");
2423 return;
2424 }
2425 ptp->tp_next = tp->tp_next;
2426 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002427 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002428 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002429
2430 /* Free the memory used for the window. */
2431 win_free_mem(win, &dir, tp);
2432
2433 if (free_tp)
2434 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002435}
2436
2437/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002438 * Free the memory used for a window.
2439 * Returns a pointer to the window that got the freed up space.
2440 */
2441 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002443 win_T *win;
2444 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002446{
2447 frame_T *frp;
2448 win_T *wp;
2449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002450 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002451 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002453 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002454 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002455
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002456 /* When deleting the current window of another tab page select a new
2457 * current window. */
2458 if (tp != NULL && win == tp->tp_curwin)
2459 tp->tp_curwin = wp;
2460
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002461 return wp;
2462}
2463
2464#if defined(EXITFREE) || defined(PROTO)
2465 void
2466win_free_all()
2467{
2468 int dummy;
2469
Bram Moolenaarf740b292006-02-16 22:11:02 +00002470# ifdef FEAT_WINDOWS
2471 while (first_tabpage->tp_next != NULL)
2472 tabpage_close(TRUE);
2473# endif
2474
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002475# ifdef FEAT_AUTOCMD
2476 if (aucmd_win != NULL)
2477 {
2478 (void)win_free_mem(aucmd_win, &dummy, NULL);
2479 aucmd_win = NULL;
2480 }
2481# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002482
2483 while (firstwin != NULL)
2484 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002485}
2486#endif
2487
2488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * Remove a window and its frame from the tree of frames.
2490 * Returns a pointer to the window that got the freed up space.
2491 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002492 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002493winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002495 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002496 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 frame_T *frp, *frp2, *frp3;
2499 frame_T *frp_close = win->w_frame;
2500 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002503 * If there is only one window there is nothing to remove.
2504 */
2505 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2506 return NULL;
2507
2508 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * Remove the window from its frame.
2510 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002511 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 wp = frame2win(frp2);
2513
2514 /* Remove this frame from the list of frames. */
2515 frame_remove(frp_close);
2516
2517#ifdef FEAT_VERTSPLIT
2518 if (frp_close->fr_parent->fr_layout == FR_COL)
2519 {
2520#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002521 /* When 'winfixheight' is set, try to find another frame in the column
2522 * (as close to the closed frame as possible) to distribute the height
2523 * to. */
2524 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2525 {
2526 frp = frp_close->fr_prev;
2527 frp3 = frp_close->fr_next;
2528 while (frp != NULL || frp3 != NULL)
2529 {
2530 if (frp != NULL)
2531 {
2532 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2533 {
2534 frp2 = frp;
2535 wp = frp->fr_win;
2536 break;
2537 }
2538 frp = frp->fr_prev;
2539 }
2540 if (frp3 != NULL)
2541 {
2542 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2543 {
2544 frp2 = frp3;
2545 wp = frp3->fr_win;
2546 break;
2547 }
2548 frp3 = frp3->fr_next;
2549 }
2550 }
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2553 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554#ifdef FEAT_VERTSPLIT
2555 *dirp = 'v';
2556 }
2557 else
2558 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002559 /* When 'winfixwidth' is set, try to find another frame in the column
2560 * (as close to the closed frame as possible) to distribute the width
2561 * to. */
2562 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2563 {
2564 frp = frp_close->fr_prev;
2565 frp3 = frp_close->fr_next;
2566 while (frp != NULL || frp3 != NULL)
2567 {
2568 if (frp != NULL)
2569 {
2570 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2571 {
2572 frp2 = frp;
2573 wp = frp->fr_win;
2574 break;
2575 }
2576 frp = frp->fr_prev;
2577 }
2578 if (frp3 != NULL)
2579 {
2580 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2581 {
2582 frp2 = frp3;
2583 wp = frp3->fr_win;
2584 break;
2585 }
2586 frp3 = frp3->fr_next;
2587 }
2588 }
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002591 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 *dirp = 'h';
2593 }
2594#endif
2595
2596 /* If rows/columns go to a window below/right its positions need to be
2597 * updated. Can only be done after the sizes have been updated. */
2598 if (frp2 == frp_close->fr_next)
2599 {
2600 int row = win->w_winrow;
2601 int col = W_WINCOL(win);
2602
2603 frame_comp_pos(frp2, &row, &col);
2604 }
2605
2606 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2607 {
2608 /* There is no other frame in this list, move its info to the parent
2609 * and remove it. */
2610 frp2->fr_parent->fr_layout = frp2->fr_layout;
2611 frp2->fr_parent->fr_child = frp2->fr_child;
2612 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2613 frp->fr_parent = frp2->fr_parent;
2614 frp2->fr_parent->fr_win = frp2->fr_win;
2615 if (frp2->fr_win != NULL)
2616 frp2->fr_win->w_frame = frp2->fr_parent;
2617 frp = frp2->fr_parent;
2618 vim_free(frp2);
2619
2620 frp2 = frp->fr_parent;
2621 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2622 {
2623 /* The frame above the parent has the same layout, have to merge
2624 * the frames into this list. */
2625 if (frp2->fr_child == frp)
2626 frp2->fr_child = frp->fr_child;
2627 frp->fr_child->fr_prev = frp->fr_prev;
2628 if (frp->fr_prev != NULL)
2629 frp->fr_prev->fr_next = frp->fr_child;
2630 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2631 {
2632 frp3->fr_parent = frp2;
2633 if (frp3->fr_next == NULL)
2634 {
2635 frp3->fr_next = frp->fr_next;
2636 if (frp->fr_next != NULL)
2637 frp->fr_next->fr_prev = frp3;
2638 break;
2639 }
2640 }
2641 vim_free(frp);
2642 }
2643 }
2644
2645 return wp;
2646}
2647
2648/*
2649 * Find out which frame is going to get the freed up space when "win" is
2650 * closed.
2651 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2652 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2653 * This makes opening a window and closing it immediately keep the same window
2654 * layout.
2655 */
2656 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002657win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002659 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 frame_T *frp;
2662 int b;
2663
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002665 /* Last window in this tab page, will go to next tab page. */
2666 return alt_tabpage()->tp_curwin->w_frame;
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 frp = win->w_frame;
2669#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002670 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 b = p_spr;
2672 else
2673#endif
2674 b = p_sb;
2675 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2676 return frp->fr_next;
2677 return frp->fr_prev;
2678}
2679
2680/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002681 * Return the tabpage that will be used if the current one is closed.
2682 */
2683 static tabpage_T *
2684alt_tabpage()
2685{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002686 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002687
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002688 /* Use the next tab page if possible. */
2689 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002690 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002691
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002692 /* Find the last but one tab page. */
2693 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2694 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002695 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002696}
2697
2698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 * Find the left-upper window in frame "frp".
2700 */
2701 static win_T *
2702frame2win(frp)
2703 frame_T *frp;
2704{
2705 while (frp->fr_win == NULL)
2706 frp = frp->fr_child;
2707 return frp->fr_win;
2708}
2709
2710/*
2711 * Return TRUE if frame "frp" contains window "wp".
2712 */
2713 static int
2714frame_has_win(frp, wp)
2715 frame_T *frp;
2716 win_T *wp;
2717{
2718 frame_T *p;
2719
2720 if (frp->fr_layout == FR_LEAF)
2721 return frp->fr_win == wp;
2722
2723 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2724 if (frame_has_win(p, wp))
2725 return TRUE;
2726 return FALSE;
2727}
2728
2729/*
2730 * Set a new height for a frame. Recursively sets the height for contained
2731 * frames and windows. Caller must take care of positions.
2732 */
2733 static void
2734frame_new_height(topfrp, height, topfirst, wfh)
2735 frame_T *topfrp;
2736 int height;
2737 int topfirst; /* resize topmost contained frame first */
2738 int wfh; /* obey 'winfixheight' when there is a choice;
2739 may cause the height not to be set */
2740{
2741 frame_T *frp;
2742 int extra_lines;
2743 int h;
2744
2745 if (topfrp->fr_win != NULL)
2746 {
2747 /* Simple case: just one window. */
2748 win_new_height(topfrp->fr_win,
2749 height - topfrp->fr_win->w_status_height);
2750 }
2751#ifdef FEAT_VERTSPLIT
2752 else if (topfrp->fr_layout == FR_ROW)
2753 {
2754 do
2755 {
2756 /* All frames in this row get the same new height. */
2757 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2758 {
2759 frame_new_height(frp, height, topfirst, wfh);
2760 if (frp->fr_height > height)
2761 {
2762 /* Could not fit the windows, make the whole row higher. */
2763 height = frp->fr_height;
2764 break;
2765 }
2766 }
2767 }
2768 while (frp != NULL);
2769 }
2770#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002771 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 /* Complicated case: Resize a column of frames. Resize the bottom
2774 * frame first, frames above that when needed. */
2775
2776 frp = topfrp->fr_child;
2777 if (wfh)
2778 /* Advance past frames with one window with 'wfh' set. */
2779 while (frame_fixed_height(frp))
2780 {
2781 frp = frp->fr_next;
2782 if (frp == NULL)
2783 return; /* no frame without 'wfh', give up */
2784 }
2785 if (!topfirst)
2786 {
2787 /* Find the bottom frame of this column */
2788 while (frp->fr_next != NULL)
2789 frp = frp->fr_next;
2790 if (wfh)
2791 /* Advance back for frames with one window with 'wfh' set. */
2792 while (frame_fixed_height(frp))
2793 frp = frp->fr_prev;
2794 }
2795
2796 extra_lines = height - topfrp->fr_height;
2797 if (extra_lines < 0)
2798 {
2799 /* reduce height of contained frames, bottom or top frame first */
2800 while (frp != NULL)
2801 {
2802 h = frame_minheight(frp, NULL);
2803 if (frp->fr_height + extra_lines < h)
2804 {
2805 extra_lines += frp->fr_height - h;
2806 frame_new_height(frp, h, topfirst, wfh);
2807 }
2808 else
2809 {
2810 frame_new_height(frp, frp->fr_height + extra_lines,
2811 topfirst, wfh);
2812 break;
2813 }
2814 if (topfirst)
2815 {
2816 do
2817 frp = frp->fr_next;
2818 while (wfh && frp != NULL && frame_fixed_height(frp));
2819 }
2820 else
2821 {
2822 do
2823 frp = frp->fr_prev;
2824 while (wfh && frp != NULL && frame_fixed_height(frp));
2825 }
2826 /* Increase "height" if we could not reduce enough frames. */
2827 if (frp == NULL)
2828 height -= extra_lines;
2829 }
2830 }
2831 else if (extra_lines > 0)
2832 {
2833 /* increase height of bottom or top frame */
2834 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2835 }
2836 }
2837 topfrp->fr_height = height;
2838}
2839
2840/*
2841 * Return TRUE if height of frame "frp" should not be changed because of
2842 * the 'winfixheight' option.
2843 */
2844 static int
2845frame_fixed_height(frp)
2846 frame_T *frp;
2847{
2848 /* frame with one window: fixed height if 'winfixheight' set. */
2849 if (frp->fr_win != NULL)
2850 return frp->fr_win->w_p_wfh;
2851
2852 if (frp->fr_layout == FR_ROW)
2853 {
2854 /* The frame is fixed height if one of the frames in the row is fixed
2855 * height. */
2856 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2857 if (frame_fixed_height(frp))
2858 return TRUE;
2859 return FALSE;
2860 }
2861
2862 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2863 * frames in the row are fixed height. */
2864 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2865 if (!frame_fixed_height(frp))
2866 return FALSE;
2867 return TRUE;
2868}
2869
2870#ifdef FEAT_VERTSPLIT
2871/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002872 * Return TRUE if width of frame "frp" should not be changed because of
2873 * the 'winfixwidth' option.
2874 */
2875 static int
2876frame_fixed_width(frp)
2877 frame_T *frp;
2878{
2879 /* frame with one window: fixed width if 'winfixwidth' set. */
2880 if (frp->fr_win != NULL)
2881 return frp->fr_win->w_p_wfw;
2882
2883 if (frp->fr_layout == FR_COL)
2884 {
2885 /* The frame is fixed width if one of the frames in the row is fixed
2886 * width. */
2887 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2888 if (frame_fixed_width(frp))
2889 return TRUE;
2890 return FALSE;
2891 }
2892
2893 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2894 * frames in the row are fixed width. */
2895 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2896 if (!frame_fixed_width(frp))
2897 return FALSE;
2898 return TRUE;
2899}
2900
2901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 * Add a status line to windows at the bottom of "frp".
2903 * Note: Does not check if there is room!
2904 */
2905 static void
2906frame_add_statusline(frp)
2907 frame_T *frp;
2908{
2909 win_T *wp;
2910
2911 if (frp->fr_layout == FR_LEAF)
2912 {
2913 wp = frp->fr_win;
2914 if (wp->w_status_height == 0)
2915 {
2916 if (wp->w_height > 0) /* don't make it negative */
2917 --wp->w_height;
2918 wp->w_status_height = STATUS_HEIGHT;
2919 }
2920 }
2921 else if (frp->fr_layout == FR_ROW)
2922 {
2923 /* Handle all the frames in the row. */
2924 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2925 frame_add_statusline(frp);
2926 }
2927 else /* frp->fr_layout == FR_COL */
2928 {
2929 /* Only need to handle the last frame in the column. */
2930 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2931 ;
2932 frame_add_statusline(frp);
2933 }
2934}
2935
2936/*
2937 * Set width of a frame. Handles recursively going through contained frames.
2938 * May remove separator line for windows at the right side (for win_close()).
2939 */
2940 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002941frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 frame_T *topfrp;
2943 int width;
2944 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002945 int wfw; /* obey 'winfixwidth' when there is a choice;
2946 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 frame_T *frp;
2949 int extra_cols;
2950 int w;
2951 win_T *wp;
2952
2953 if (topfrp->fr_layout == FR_LEAF)
2954 {
2955 /* Simple case: just one window. */
2956 wp = topfrp->fr_win;
2957 /* Find out if there are any windows right of this one. */
2958 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2959 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2960 break;
2961 if (frp->fr_parent == NULL)
2962 wp->w_vsep_width = 0;
2963 win_new_width(wp, width - wp->w_vsep_width);
2964 }
2965 else if (topfrp->fr_layout == FR_COL)
2966 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002967 do
2968 {
2969 /* All frames in this column get the same new width. */
2970 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2971 {
2972 frame_new_width(frp, width, leftfirst, wfw);
2973 if (frp->fr_width > width)
2974 {
2975 /* Could not fit the windows, make whole column wider. */
2976 width = frp->fr_width;
2977 break;
2978 }
2979 }
2980 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
2982 else /* fr_layout == FR_ROW */
2983 {
2984 /* Complicated case: Resize a row of frames. Resize the rightmost
2985 * frame first, frames left of it when needed. */
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002988 if (wfw)
2989 /* Advance past frames with one window with 'wfw' set. */
2990 while (frame_fixed_width(frp))
2991 {
2992 frp = frp->fr_next;
2993 if (frp == NULL)
2994 return; /* no frame without 'wfw', give up */
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002997 {
2998 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 while (frp->fr_next != NULL)
3000 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003001 if (wfw)
3002 /* Advance back for frames with one window with 'wfw' set. */
3003 while (frame_fixed_width(frp))
3004 frp = frp->fr_prev;
3005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 extra_cols = width - topfrp->fr_width;
3008 if (extra_cols < 0)
3009 {
3010 /* reduce frame width, rightmost frame first */
3011 while (frp != NULL)
3012 {
3013 w = frame_minwidth(frp, NULL);
3014 if (frp->fr_width + extra_cols < w)
3015 {
3016 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003017 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 else
3020 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003021 frame_new_width(frp, frp->fr_width + extra_cols,
3022 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 break;
3024 }
3025 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003026 {
3027 do
3028 frp = frp->fr_next;
3029 while (wfw && frp != NULL && frame_fixed_width(frp));
3030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003032 {
3033 do
3034 frp = frp->fr_prev;
3035 while (wfw && frp != NULL && frame_fixed_width(frp));
3036 }
3037 /* Increase "width" if we could not reduce enough frames. */
3038 if (frp == NULL)
3039 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
3041 }
3042 else if (extra_cols > 0)
3043 {
3044 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003045 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 }
3048 topfrp->fr_width = width;
3049}
3050
3051/*
3052 * Add the vertical separator to windows at the right side of "frp".
3053 * Note: Does not check if there is room!
3054 */
3055 static void
3056frame_add_vsep(frp)
3057 frame_T *frp;
3058{
3059 win_T *wp;
3060
3061 if (frp->fr_layout == FR_LEAF)
3062 {
3063 wp = frp->fr_win;
3064 if (wp->w_vsep_width == 0)
3065 {
3066 if (wp->w_width > 0) /* don't make it negative */
3067 --wp->w_width;
3068 wp->w_vsep_width = 1;
3069 }
3070 }
3071 else if (frp->fr_layout == FR_COL)
3072 {
3073 /* Handle all the frames in the column. */
3074 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3075 frame_add_vsep(frp);
3076 }
3077 else /* frp->fr_layout == FR_ROW */
3078 {
3079 /* Only need to handle the last frame in the row. */
3080 frp = frp->fr_child;
3081 while (frp->fr_next != NULL)
3082 frp = frp->fr_next;
3083 frame_add_vsep(frp);
3084 }
3085}
3086
3087/*
3088 * Set frame width from the window it contains.
3089 */
3090 static void
3091frame_fix_width(wp)
3092 win_T *wp;
3093{
3094 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3095}
3096#endif
3097
3098/*
3099 * Set frame height from the window it contains.
3100 */
3101 static void
3102frame_fix_height(wp)
3103 win_T *wp;
3104{
3105 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3106}
3107
3108/*
3109 * Compute the minimal height for frame "topfrp".
3110 * Uses the 'winminheight' option.
3111 * When "next_curwin" isn't NULL, use p_wh for this window.
3112 * When "next_curwin" is NOWIN, don't use at least one line for the current
3113 * window.
3114 */
3115 static int
3116frame_minheight(topfrp, next_curwin)
3117 frame_T *topfrp;
3118 win_T *next_curwin;
3119{
3120 frame_T *frp;
3121 int m;
3122#ifdef FEAT_VERTSPLIT
3123 int n;
3124#endif
3125
3126 if (topfrp->fr_win != NULL)
3127 {
3128 if (topfrp->fr_win == next_curwin)
3129 m = p_wh + topfrp->fr_win->w_status_height;
3130 else
3131 {
3132 /* window: minimal height of the window plus status line */
3133 m = p_wmh + topfrp->fr_win->w_status_height;
3134 /* Current window is minimal one line high */
3135 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3136 ++m;
3137 }
3138 }
3139#ifdef FEAT_VERTSPLIT
3140 else if (topfrp->fr_layout == FR_ROW)
3141 {
3142 /* get the minimal height from each frame in this row */
3143 m = 0;
3144 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3145 {
3146 n = frame_minheight(frp, next_curwin);
3147 if (n > m)
3148 m = n;
3149 }
3150 }
3151#endif
3152 else
3153 {
3154 /* Add up the minimal heights for all frames in this column. */
3155 m = 0;
3156 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3157 m += frame_minheight(frp, next_curwin);
3158 }
3159
3160 return m;
3161}
3162
3163#ifdef FEAT_VERTSPLIT
3164/*
3165 * Compute the minimal width for frame "topfrp".
3166 * When "next_curwin" isn't NULL, use p_wiw for this window.
3167 * When "next_curwin" is NOWIN, don't use at least one column for the current
3168 * window.
3169 */
3170 static int
3171frame_minwidth(topfrp, next_curwin)
3172 frame_T *topfrp;
3173 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3174{
3175 frame_T *frp;
3176 int m, n;
3177
3178 if (topfrp->fr_win != NULL)
3179 {
3180 if (topfrp->fr_win == next_curwin)
3181 m = p_wiw + topfrp->fr_win->w_vsep_width;
3182 else
3183 {
3184 /* window: minimal width of the window plus separator column */
3185 m = p_wmw + topfrp->fr_win->w_vsep_width;
3186 /* Current window is minimal one column wide */
3187 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3188 ++m;
3189 }
3190 }
3191 else if (topfrp->fr_layout == FR_COL)
3192 {
3193 /* get the minimal width from each frame in this column */
3194 m = 0;
3195 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3196 {
3197 n = frame_minwidth(frp, next_curwin);
3198 if (n > m)
3199 m = n;
3200 }
3201 }
3202 else
3203 {
3204 /* Add up the minimal widths for all frames in this row. */
3205 m = 0;
3206 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3207 m += frame_minwidth(frp, next_curwin);
3208 }
3209
3210 return m;
3211}
3212#endif
3213
3214
3215/*
3216 * Try to close all windows except current one.
3217 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3218 * used and the buffer was modified.
3219 *
3220 * Used by ":bdel" and ":only".
3221 */
3222 void
3223close_others(message, forceit)
3224 int message;
3225 int forceit; /* always hide all other windows */
3226{
3227 win_T *wp;
3228 win_T *nextwp;
3229 int r;
3230
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003231 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
3233 if (message
3234#ifdef FEAT_AUTOCMD
3235 && !autocmd_busy
3236#endif
3237 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003238 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 return;
3240 }
3241
3242 /* Be very careful here: autocommands may change the window layout. */
3243 for (wp = firstwin; win_valid(wp); wp = nextwp)
3244 {
3245 nextwp = wp->w_next;
3246 if (wp != curwin) /* don't close current window */
3247 {
3248
3249 /* Check if it's allowed to abandon this window */
3250 r = can_abandon(wp->w_buffer, forceit);
3251#ifdef FEAT_AUTOCMD
3252 if (!win_valid(wp)) /* autocommands messed wp up */
3253 {
3254 nextwp = firstwin;
3255 continue;
3256 }
3257#endif
3258 if (!r)
3259 {
3260#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3261 if (message && (p_confirm || cmdmod.confirm) && p_write)
3262 {
3263 dialog_changed(wp->w_buffer, FALSE);
3264# ifdef FEAT_AUTOCMD
3265 if (!win_valid(wp)) /* autocommands messed wp up */
3266 {
3267 nextwp = firstwin;
3268 continue;
3269 }
3270# endif
3271 }
3272 if (bufIsChanged(wp->w_buffer))
3273#endif
3274 continue;
3275 }
3276 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3277 }
3278 }
3279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003280 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 EMSG(_("E445: Other window contains changes"));
3282}
3283
3284#endif /* FEAT_WINDOWS */
3285
3286/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003287 * Init the current window "curwin".
3288 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 */
3290 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003291curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003293 win_init_empty(curwin);
3294}
3295
3296 void
3297win_init_empty(wp)
3298 win_T *wp;
3299{
3300 redraw_win_later(wp, NOT_VALID);
3301 wp->w_lines_valid = 0;
3302 wp->w_cursor.lnum = 1;
3303 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003305 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003307 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3308 wp->w_pcmark.col = 0;
3309 wp->w_prev_pcmark.lnum = 0;
3310 wp->w_prev_pcmark.col = 0;
3311 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003313 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003315 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003317 if (wp->w_p_rl)
3318 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003320 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003322#ifdef FEAT_SYN_HL
3323 wp->w_s = &wp->w_buffer->b_s;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327/*
3328 * Allocate the first window and put an empty buffer in it.
3329 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003330 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333win_alloc_first()
3334{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003335 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 return FAIL;
3337
3338#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003339 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003340 if (first_tabpage == NULL)
3341 return FAIL;
3342 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003343 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003345
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 return OK;
3347}
3348
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003349#if defined(FEAT_AUTOCMD) || defined(PROTO)
3350/*
3351 * Init "aucmd_win". This can only be done after the first
3352 * window is fully initialized, thus it can't be in win_alloc_first().
3353 */
3354 void
3355win_alloc_aucmd_win()
3356{
3357 aucmd_win = win_alloc(NULL, TRUE);
3358 if (aucmd_win != NULL)
3359 {
3360 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003361 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003362 new_frame(aucmd_win);
3363 }
3364}
3365#endif
3366
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003367/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003368 * Allocate the first window or the first window in a new tab page.
3369 * When "oldwin" is NULL create an empty buffer for it.
3370 * When "oldwin" is not NULL copy info from it to the new window (only with
3371 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372 * Return FAIL when something goes wrong (out of memory).
3373 */
3374 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003375win_alloc_firstwin(oldwin)
3376 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003377{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003378 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003379 if (oldwin == NULL)
3380 {
3381 /* Very first window, need to create an empty buffer for it and
3382 * initialize from scratch. */
3383 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3384 if (curwin == NULL || curbuf == NULL)
3385 return FAIL;
3386 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003387#ifdef FEAT_SYN_HL
3388 curwin->w_s = &(curbuf->b_s);
3389#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003390 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003392 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003394 curwin_init(); /* init current window */
3395 }
3396#ifdef FEAT_WINDOWS
3397 else
3398 {
3399 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003400 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003402 /* We don't want cursor- and scroll-binding in the first window. */
3403 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003404 }
3405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003407 new_frame(curwin);
3408 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003409 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003410 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#ifdef FEAT_VERTSPLIT
3412 topframe->fr_width = Columns;
3413#endif
3414 topframe->fr_height = Rows - p_ch;
3415 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416
3417 return OK;
3418}
3419
3420/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003421 * Create a frame for window "wp".
3422 */
3423 static void
3424new_frame(win_T *wp)
3425{
3426 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3427
3428 wp->w_frame = frp;
3429 if (frp != NULL)
3430 {
3431 frp->fr_layout = FR_LEAF;
3432 frp->fr_win = wp;
3433 }
3434}
3435
3436/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 * Initialize the window and frame size to the maximum.
3438 */
3439 void
3440win_init_size()
3441{
3442 firstwin->w_height = ROWS_AVAIL;
3443 topframe->fr_height = ROWS_AVAIL;
3444#ifdef FEAT_VERTSPLIT
3445 firstwin->w_width = Columns;
3446 topframe->fr_width = Columns;
3447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449
3450#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003451
3452/*
3453 * Allocate a new tabpage_T and init the values.
3454 * Returns NULL when out of memory.
3455 */
3456 static tabpage_T *
3457alloc_tabpage()
3458{
3459 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003460# ifdef FEAT_GUI
3461 int i;
3462# endif
3463
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003464
3465 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003466 if (tp == NULL)
3467 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003468
Bram Moolenaar429fa852013-04-15 12:27:36 +02003469# ifdef FEAT_EVAL
3470 /* init t: variables */
3471 tp->tp_vars = dict_alloc();
3472 if (tp->tp_vars == NULL)
3473 {
3474 vim_free(tp);
3475 return NULL;
3476 }
3477 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3478# endif
3479
3480# ifdef FEAT_GUI
3481 for (i = 0; i < 3; i++)
3482 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003483# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003484# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003485 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003486# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003487 tp->tp_ch_used = p_ch;
3488
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003489 return tp;
3490}
3491
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003492 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003493free_tabpage(tp)
3494 tabpage_T *tp;
3495{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003496 int idx;
3497
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498# ifdef FEAT_DIFF
3499 diff_clear(tp);
3500# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501 for (idx = 0; idx < SNAP_COUNT; ++idx)
3502 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003503#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003504 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3505 hash_init(&tp->tp_vars->dv_hashtab);
3506 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003507#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003508 vim_free(tp);
3509}
3510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003511/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003512 * Create a new Tab page with one window.
3513 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003514 * When "after" is 0 put it just after the current Tab page.
3515 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 * Return FAIL or OK.
3517 */
3518 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003519win_new_tabpage(after)
3520 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003521{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003524 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003526 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527 if (newtp == NULL)
3528 return FAIL;
3529
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003531 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003532 {
3533 vim_free(newtp);
3534 return FAIL;
3535 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003536 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537
3538 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003539 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003540 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003542 if (after == 1)
3543 {
3544 /* New tab page becomes the first one. */
3545 newtp->tp_next = first_tabpage;
3546 first_tabpage = newtp;
3547 }
3548 else
3549 {
3550 if (after > 0)
3551 {
3552 /* Put new tab page before tab page "after". */
3553 n = 2;
3554 for (tp = first_tabpage; tp->tp_next != NULL
3555 && n < after; tp = tp->tp_next)
3556 ++n;
3557 }
3558 newtp->tp_next = tp->tp_next;
3559 tp->tp_next = newtp;
3560 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003561 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003562 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003563 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564
3565 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003566 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003567
3568#if defined(FEAT_GUI)
3569 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3570 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003571 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003572#endif
3573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003574 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003575#ifdef FEAT_AUTOCMD
3576 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3577 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3578#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003579 return OK;
3580 }
3581
3582 /* Failed, get back the previous Tab page */
Bram Moolenaara8596c42012-06-13 14:28:20 +02003583 enter_tabpage(curtab, curbuf, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584 return FAIL;
3585}
3586
3587/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003588 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3589 * like with ":split".
3590 * Returns OK if a new tab page was created, FAIL otherwise.
3591 */
3592 int
3593may_open_tabpage()
3594{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003595 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003596
Bram Moolenaard326ce82007-03-11 14:48:29 +00003597 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003598 {
3599 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003600 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003601 return win_new_tabpage(n);
3602 }
3603 return FAIL;
3604}
3605
3606/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003607 * Create up to "maxcount" tabpages with empty windows.
3608 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003609 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003610 int
3611make_tabpages(maxcount)
3612 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003613{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003614 int count = maxcount;
3615 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003616
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003617 /* Limit to 'tabpagemax' tabs. */
3618 if (count > p_tpm)
3619 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003620
3621#ifdef FEAT_AUTOCMD
3622 /*
3623 * Don't execute autocommands while creating the tab pages. Must do that
3624 * when putting the buffers in the windows.
3625 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003626 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003627#endif
3628
3629 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003630 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003632
3633#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003634 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003635#endif
3636
3637 /* return actual number of tab pages */
3638 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639}
3640
3641/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003642 * Return TRUE when "tpc" points to a valid tab page.
3643 */
3644 int
3645valid_tabpage(tpc)
3646 tabpage_T *tpc;
3647{
3648 tabpage_T *tp;
3649
3650 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3651 if (tp == tpc)
3652 return TRUE;
3653 return FALSE;
3654}
3655
3656/*
3657 * Find tab page "n" (first one is 1). Returns NULL when not found.
3658 */
3659 tabpage_T *
3660find_tabpage(n)
3661 int n;
3662{
3663 tabpage_T *tp;
3664 int i = 1;
3665
3666 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3667 ++i;
3668 return tp;
3669}
3670
3671/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003673 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674 */
3675 int
3676tabpage_index(ftp)
3677 tabpage_T *ftp;
3678{
3679 int i = 1;
3680 tabpage_T *tp;
3681
3682 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3683 ++i;
3684 return i;
3685}
3686
3687/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003688 * Prepare for leaving the current tab page.
3689 * When autocomands change "curtab" we don't leave the tab page and return
3690 * FAIL.
3691 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003692 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003693 static int
3694leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003695 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003696 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003698 tabpage_T *tp = curtab;
3699
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003700#ifdef FEAT_VISUAL
3701 reset_VIsual_and_resel(); /* stop Visual mode */
3702#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003703#ifdef FEAT_AUTOCMD
3704 if (new_curbuf != curbuf)
3705 {
3706 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3707 if (curtab != tp)
3708 return FAIL;
3709 }
3710 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3711 if (curtab != tp)
3712 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003713 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003714 if (curtab != tp)
3715 return FAIL;
3716#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003717#if defined(FEAT_GUI)
3718 /* Remove the scrollbars. They may be added back later. */
3719 if (gui.in_use)
3720 gui_remove_scrollbars();
3721#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003723 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003724 tp->tp_firstwin = firstwin;
3725 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003726 tp->tp_old_Rows = Rows;
3727 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003728 firstwin = NULL;
3729 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003730 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003731}
3732
3733/*
3734 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003735 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaara8596c42012-06-13 14:28:20 +02003736 * Only trigger *Enter autocommands when trigger_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003737 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738 static void
Bram Moolenaara8596c42012-06-13 14:28:20 +02003739enter_tabpage(tp, old_curbuf, trigger_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003740 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003741 buf_T *old_curbuf UNUSED;
Bram Moolenaar756287d2012-07-06 16:39:47 +02003742 int trigger_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003743{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003744 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003745 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003746
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003747 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003748 firstwin = tp->tp_firstwin;
3749 lastwin = tp->tp_lastwin;
3750 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003751
3752 /* We would like doing the TabEnter event first, but we don't have a
3753 * valid current window yet, which may break some commands.
3754 * This triggers autocommands, thus may make "tp" invalid. */
3755 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3756 prevwin = next_prevwin;
3757
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003758 last_status(FALSE); /* status line may appear or disappear */
3759 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003760 must_redraw = CLEAR; /* need to redraw everything */
3761#ifdef FEAT_DIFF
3762 diff_need_scrollbind = TRUE;
3763#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003765 /* The tabpage line may have appeared or disappeared, may need to resize
3766 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003767 * frame sizes too. Use the stored value of p_ch, so that it can be
3768 * different for each tab page. */
3769 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003770 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3771#ifdef FEAT_GUI_TABLINE
3772 && !gui_use_tabline()
3773#endif
3774 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003775 shell_new_rows();
3776#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003777 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003778 shell_new_columns(); /* update window widths */
3779#endif
3780
3781#if defined(FEAT_GUI)
3782 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3783 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003784 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003785#endif
3786
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003787#ifdef FEAT_AUTOCMD
3788 /* Apply autocommands after updating the display, when 'rows' and
3789 * 'columns' have been set correctly. */
Bram Moolenaara8596c42012-06-13 14:28:20 +02003790 if (trigger_autocmds)
3791 {
3792 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3793 if (old_curbuf != curbuf)
3794 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3795 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003796#endif
3797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798 redraw_all_later(CLEAR);
3799}
3800
3801/*
3802 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003803 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 */
3805 void
3806goto_tabpage(n)
3807 int n;
3808{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003809 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003810 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 int i;
3812
Bram Moolenaard68071d2006-05-02 22:08:30 +00003813 if (text_locked())
3814 {
3815 /* Not allowed when editing the command line. */
3816#ifdef FEAT_CMDWIN
3817 if (cmdwin_type != 0)
3818 EMSG(_(e_cmdwin));
3819 else
3820#endif
3821 EMSG(_(e_secure));
3822 return;
3823 }
3824
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003825 /* If there is only one it can't work. */
3826 if (first_tabpage->tp_next == NULL)
3827 {
3828 if (n > 1)
3829 beep_flush();
3830 return;
3831 }
3832
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833 if (n == 0)
3834 {
3835 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003836 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 tp = first_tabpage;
3838 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003839 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003841 else if (n < 0)
3842 {
3843 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3844 * this N times. */
3845 ttp = curtab;
3846 for (i = n; i < 0; ++i)
3847 {
3848 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3849 tp = tp->tp_next)
3850 ;
3851 ttp = tp;
3852 }
3853 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003854 else if (n == 9999)
3855 {
3856 /* Go to last tab page. */
3857 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3858 ;
3859 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003860 else
3861 {
3862 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003863 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003864 if (tp == NULL)
3865 {
3866 beep_flush();
3867 return;
3868 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003869 }
3870
Bram Moolenaara8596c42012-06-13 14:28:20 +02003871 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003872
3873#ifdef FEAT_GUI_TABLINE
3874 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003875 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003876#endif
3877}
3878
3879/*
3880 * Go to tabpage "tp".
Bram Moolenaara8596c42012-06-13 14:28:20 +02003881 * Only trigger *Enter autocommands when trigger_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003882 * Note: doesn't update the GUI tab.
3883 */
3884 void
Bram Moolenaara8596c42012-06-13 14:28:20 +02003885goto_tabpage_tp(tp, trigger_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003886 tabpage_T *tp;
Bram Moolenaar756287d2012-07-06 16:39:47 +02003887 int trigger_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003888{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003889 /* Don't repeat a message in another tab page. */
3890 set_keep_msg(NULL, 0);
3891
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003892 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003893 {
3894 if (valid_tabpage(tp))
Bram Moolenaara8596c42012-06-13 14:28:20 +02003895 enter_tabpage(tp, curbuf, trigger_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003896 else
Bram Moolenaara8596c42012-06-13 14:28:20 +02003897 enter_tabpage(curtab, curbuf, trigger_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003898 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003902 * Enter window "wp" in tab page "tp".
3903 * Also updates the GUI tab.
3904 */
3905 void
3906goto_tabpage_win(tp, wp)
3907 tabpage_T *tp;
3908 win_T *wp;
3909{
Bram Moolenaara8596c42012-06-13 14:28:20 +02003910 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003911 if (curtab == tp && win_valid(wp))
3912 {
3913 win_enter(wp, TRUE);
3914# ifdef FEAT_GUI_TABLINE
3915 if (gui_use_tabline())
3916 gui_mch_set_curtab(tabpage_index(curtab));
3917# endif
3918 }
3919}
3920
3921/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003922 * Move the current tab page to before tab page "nr".
3923 */
3924 void
3925tabpage_move(nr)
3926 int nr;
3927{
3928 int n = nr;
3929 tabpage_T *tp;
3930
3931 if (first_tabpage->tp_next == NULL)
3932 return;
3933
3934 /* Remove the current tab page from the list of tab pages. */
3935 if (curtab == first_tabpage)
3936 first_tabpage = curtab->tp_next;
3937 else
3938 {
3939 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3940 if (tp->tp_next == curtab)
3941 break;
3942 if (tp == NULL) /* "cannot happen" */
3943 return;
3944 tp->tp_next = curtab->tp_next;
3945 }
3946
3947 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003948 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003949 {
3950 curtab->tp_next = first_tabpage;
3951 first_tabpage = curtab;
3952 }
3953 else
3954 {
3955 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3956 --n;
3957 curtab->tp_next = tp->tp_next;
3958 tp->tp_next = curtab;
3959 }
3960
3961 /* Need to redraw the tabline. Tab page contents doesn't change. */
3962 redraw_tabline = TRUE;
3963}
3964
3965
3966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Go to another window.
3968 * When jumping to another buffer, stop Visual mode. Do this before
3969 * changing windows so we can yank the selection into the '*' register.
3970 * When jumping to another window on the same buffer, adjust its cursor
3971 * position to keep the same Visual area.
3972 */
3973 void
3974win_goto(wp)
3975 win_T *wp;
3976{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003977#ifdef FEAT_CONCEAL
3978 win_T *owp = curwin;
3979#endif
3980
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003981 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003984 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return;
3986 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003987#ifdef FEAT_AUTOCMD
3988 if (curbuf_locked())
3989 return;
3990#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992#ifdef FEAT_VISUAL
3993 if (wp->w_buffer != curbuf)
3994 reset_VIsual_and_resel();
3995 else if (VIsual_active)
3996 wp->w_cursor = curwin->w_cursor;
3997#endif
3998
3999#ifdef FEAT_GUI
4000 need_mouse_correct = TRUE;
4001#endif
4002 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004003
4004#ifdef FEAT_CONCEAL
4005 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004006 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004007 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004008 if (curwin->w_p_cole > 0 && !msg_scrolled)
4009 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011}
4012
4013#if defined(FEAT_PERL) || defined(PROTO)
4014/*
4015 * Find window number "winnr" (counting top to bottom).
4016 */
4017 win_T *
4018win_find_nr(winnr)
4019 int winnr;
4020{
4021 win_T *wp;
4022
4023# ifdef FEAT_WINDOWS
4024 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4025 if (--winnr == 0)
4026 break;
4027 return wp;
4028# else
4029 return curwin;
4030# endif
4031}
4032#endif
4033
4034#ifdef FEAT_VERTSPLIT
4035/*
4036 * Move to window above or below "count" times.
4037 */
4038 static void
4039win_goto_ver(up, count)
4040 int up; /* TRUE to go to win above */
4041 long count;
4042{
4043 frame_T *fr;
4044 frame_T *nfr;
4045 frame_T *foundfr;
4046
4047 foundfr = curwin->w_frame;
4048 while (count--)
4049 {
4050 /*
4051 * First go upwards in the tree of frames until we find a upwards or
4052 * downwards neighbor.
4053 */
4054 fr = foundfr;
4055 for (;;)
4056 {
4057 if (fr == topframe)
4058 goto end;
4059 if (up)
4060 nfr = fr->fr_prev;
4061 else
4062 nfr = fr->fr_next;
4063 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4064 break;
4065 fr = fr->fr_parent;
4066 }
4067
4068 /*
4069 * Now go downwards to find the bottom or top frame in it.
4070 */
4071 for (;;)
4072 {
4073 if (nfr->fr_layout == FR_LEAF)
4074 {
4075 foundfr = nfr;
4076 break;
4077 }
4078 fr = nfr->fr_child;
4079 if (nfr->fr_layout == FR_ROW)
4080 {
4081 /* Find the frame at the cursor row. */
4082 while (fr->fr_next != NULL
4083 && frame2win(fr)->w_wincol + fr->fr_width
4084 <= curwin->w_wincol + curwin->w_wcol)
4085 fr = fr->fr_next;
4086 }
4087 if (nfr->fr_layout == FR_COL && up)
4088 while (fr->fr_next != NULL)
4089 fr = fr->fr_next;
4090 nfr = fr;
4091 }
4092 }
4093end:
4094 if (foundfr != NULL)
4095 win_goto(foundfr->fr_win);
4096}
4097
4098/*
4099 * Move to left or right window.
4100 */
4101 static void
4102win_goto_hor(left, count)
4103 int left; /* TRUE to go to left win */
4104 long count;
4105{
4106 frame_T *fr;
4107 frame_T *nfr;
4108 frame_T *foundfr;
4109
4110 foundfr = curwin->w_frame;
4111 while (count--)
4112 {
4113 /*
4114 * First go upwards in the tree of frames until we find a left or
4115 * right neighbor.
4116 */
4117 fr = foundfr;
4118 for (;;)
4119 {
4120 if (fr == topframe)
4121 goto end;
4122 if (left)
4123 nfr = fr->fr_prev;
4124 else
4125 nfr = fr->fr_next;
4126 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4127 break;
4128 fr = fr->fr_parent;
4129 }
4130
4131 /*
4132 * Now go downwards to find the leftmost or rightmost frame in it.
4133 */
4134 for (;;)
4135 {
4136 if (nfr->fr_layout == FR_LEAF)
4137 {
4138 foundfr = nfr;
4139 break;
4140 }
4141 fr = nfr->fr_child;
4142 if (nfr->fr_layout == FR_COL)
4143 {
4144 /* Find the frame at the cursor row. */
4145 while (fr->fr_next != NULL
4146 && frame2win(fr)->w_winrow + fr->fr_height
4147 <= curwin->w_winrow + curwin->w_wrow)
4148 fr = fr->fr_next;
4149 }
4150 if (nfr->fr_layout == FR_ROW && left)
4151 while (fr->fr_next != NULL)
4152 fr = fr->fr_next;
4153 nfr = fr;
4154 }
4155 }
4156end:
4157 if (foundfr != NULL)
4158 win_goto(foundfr->fr_win);
4159}
4160#endif
4161
4162/*
4163 * Make window "wp" the current window.
4164 */
4165 void
4166win_enter(wp, undo_sync)
4167 win_T *wp;
4168 int undo_sync;
4169{
4170 win_enter_ext(wp, undo_sync, FALSE);
4171}
4172
4173/*
4174 * Make window wp the current window.
4175 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4176 * been closed and isn't valid.
4177 */
4178 static void
4179win_enter_ext(wp, undo_sync, curwin_invalid)
4180 win_T *wp;
4181 int undo_sync;
4182 int curwin_invalid;
4183{
4184#ifdef FEAT_AUTOCMD
4185 int other_buffer = FALSE;
4186#endif
4187
4188 if (wp == curwin && !curwin_invalid) /* nothing to do */
4189 return;
4190
4191#ifdef FEAT_AUTOCMD
4192 if (!curwin_invalid)
4193 {
4194 /*
4195 * Be careful: If autocommands delete the window, return now.
4196 */
4197 if (wp->w_buffer != curbuf)
4198 {
4199 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4200 other_buffer = TRUE;
4201 if (!win_valid(wp))
4202 return;
4203 }
4204 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4205 if (!win_valid(wp))
4206 return;
4207# ifdef FEAT_EVAL
4208 /* autocmds may abort script processing */
4209 if (aborting())
4210 return;
4211# endif
4212 }
4213#endif
4214
4215 /* sync undo before leaving the current buffer */
4216 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004217 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 /* may have to copy the buffer options when 'cpo' contains 'S' */
4219 if (wp->w_buffer != curbuf)
4220 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4221 if (!curwin_invalid)
4222 {
4223 prevwin = curwin; /* remember for CTRL-W p */
4224 curwin->w_redr_status = TRUE;
4225 }
4226 curwin = wp;
4227 curbuf = wp->w_buffer;
4228 check_cursor();
4229#ifdef FEAT_VIRTUALEDIT
4230 if (!virtual_active())
4231 curwin->w_cursor.coladd = 0;
4232#endif
4233 changed_line_abv_curs(); /* assume cursor position needs updating */
4234
4235 if (curwin->w_localdir != NULL)
4236 {
4237 /* Window has a local directory: Save current directory as global
4238 * directory (unless that was done already) and change to the local
4239 * directory. */
4240 if (globaldir == NULL)
4241 {
4242 char_u cwd[MAXPATHL];
4243
4244 if (mch_dirname(cwd, MAXPATHL) == OK)
4245 globaldir = vim_strsave(cwd);
4246 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004247 if (mch_chdir((char *)curwin->w_localdir) == 0)
4248 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 else if (globaldir != NULL)
4251 {
4252 /* Window doesn't have a local directory and we are not in the global
4253 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004254 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 vim_free(globaldir);
4256 globaldir = NULL;
4257 shorten_fnames(TRUE);
4258 }
4259
4260#ifdef FEAT_AUTOCMD
4261 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4262 if (other_buffer)
4263 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4264#endif
4265
4266#ifdef FEAT_TITLE
4267 maketitle();
4268#endif
4269 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004270 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (restart_edit)
4272 redraw_later(VALID); /* causes status line redraw */
4273
4274 /* set window height to desired minimal value */
4275 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4276 win_setheight((int)p_wh);
4277 else if (curwin->w_height == 0)
4278 win_setheight(1);
4279
4280#ifdef FEAT_VERTSPLIT
4281 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004282 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 win_setwidth((int)p_wiw);
4284#endif
4285
4286#ifdef FEAT_MOUSE
4287 setmouse(); /* in case jumped to/from help buffer */
4288#endif
4289
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004290 /* Change directories when the 'acd' option is set. */
4291 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292}
4293
4294#endif /* FEAT_WINDOWS */
4295
4296#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4297/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004298 * Jump to the first open window that contains buffer "buf", if one exists.
4299 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 */
4301 win_T *
4302buf_jump_open_win(buf)
4303 buf_T *buf;
4304{
4305# ifdef FEAT_WINDOWS
4306 win_T *wp;
4307
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004308 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if (wp->w_buffer == buf)
4310 break;
4311 if (wp != NULL)
4312 win_enter(wp, FALSE);
4313 return wp;
4314# else
4315 if (curwin->w_buffer == buf)
4316 return curwin;
4317 return NULL;
4318# endif
4319}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004320
4321/*
4322 * Jump to the first open window in any tab page that contains buffer "buf",
4323 * if one exists.
4324 * Returns a pointer to the window found, otherwise NULL.
4325 */
4326 win_T *
4327buf_jump_open_tab(buf)
4328 buf_T *buf;
4329{
4330# ifdef FEAT_WINDOWS
4331 win_T *wp;
4332 tabpage_T *tp;
4333
4334 /* First try the current tab page. */
4335 wp = buf_jump_open_win(buf);
4336 if (wp != NULL)
4337 return wp;
4338
4339 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4340 if (tp != curtab)
4341 {
4342 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4343 if (wp->w_buffer == buf)
4344 break;
4345 if (wp != NULL)
4346 {
4347 goto_tabpage_win(tp, wp);
4348 if (curwin != wp)
4349 wp = NULL; /* something went wrong */
4350 break;
4351 }
4352 }
4353
4354 return wp;
4355# else
4356 if (curwin->w_buffer == buf)
4357 return curwin;
4358 return NULL;
4359# endif
4360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361#endif
4362
4363/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004364 * Allocate a window structure and link it in the window list when "hidden" is
4365 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004368win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004369 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004370 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004372 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * allocate window structure and linesizes arrays
4376 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004377 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004378 if (new_wp == NULL)
4379 return NULL;
4380
4381 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004383 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004384 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386
Bram Moolenaar429fa852013-04-15 12:27:36 +02004387#ifdef FEAT_EVAL
4388 /* init w: variables */
4389 new_wp->w_vars = dict_alloc();
4390 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004392 win_free_lsize(new_wp);
4393 vim_free(new_wp);
4394 return NULL;
4395 }
4396 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004397#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004398
4399#ifdef FEAT_AUTOCMD
4400 /* Don't execute autocommands while the window is not properly
4401 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4402 * event. */
4403 block_autocmds();
4404#endif
4405 /*
4406 * link the window in the window list
4407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004409 if (!hidden)
4410 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411#endif
4412#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004413 new_wp->w_wincol = 0;
4414 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#endif
4416
Bram Moolenaar429fa852013-04-15 12:27:36 +02004417 /* position the display and the cursor at the top of the file. */
4418 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004420 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004422 new_wp->w_botline = 2;
4423 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004425 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#endif
4427
Bram Moolenaar429fa852013-04-15 12:27:36 +02004428 /* We won't calculate w_fraction until resizing the window */
4429 new_wp->w_fraction = 0;
4430 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004433 if (gui.in_use)
4434 {
4435 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4436 SBAR_LEFT, new_wp);
4437 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4438 SBAR_RIGHT, new_wp);
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#endif
4441#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004442 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004444#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004445 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004446#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004447#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004448 new_wp->w_match_head = NULL;
4449 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004450#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004451 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452}
4453
4454#if defined(FEAT_WINDOWS) || defined(PROTO)
4455
4456/*
4457 * remove window 'wp' from the window list and free the structure
4458 */
4459 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004460win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004462 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
4464 int i;
4465
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004466#ifdef FEAT_FOLDING
4467 clearFolding(wp);
4468#endif
4469
4470 /* reduce the reference count to the argument list. */
4471 alist_unlink(wp->w_alist);
4472
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004473#ifdef FEAT_AUTOCMD
4474 /* Don't execute autocommands while the window is halfway being deleted.
4475 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004476 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004477#endif
4478
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004479#ifdef FEAT_LUA
4480 lua_window_free(wp);
4481#endif
4482
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004483#ifdef FEAT_MZSCHEME
4484 mzscheme_window_free(wp);
4485#endif
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#ifdef FEAT_PERL
4488 perl_win_free(wp);
4489#endif
4490
4491#ifdef FEAT_PYTHON
4492 python_window_free(wp);
4493#endif
4494
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004495#ifdef FEAT_PYTHON3
4496 python3_window_free(wp);
4497#endif
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499#ifdef FEAT_TCL
4500 tcl_window_free(wp);
4501#endif
4502
4503#ifdef FEAT_RUBY
4504 ruby_window_free(wp);
4505#endif
4506
4507 clear_winopt(&wp->w_onebuf_opt);
4508 clear_winopt(&wp->w_allbuf_opt);
4509
4510#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004511 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4512 hash_init(&wp->w_vars->dv_hashtab);
4513 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#endif
4515
4516 if (prevwin == wp)
4517 prevwin = NULL;
4518 win_free_lsize(wp);
4519
4520 for (i = 0; i < wp->w_tagstacklen; ++i)
4521 vim_free(wp->w_tagstack[i].tagname);
4522
4523 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004526 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529#ifdef FEAT_JUMPLIST
4530 free_jumplist(wp);
4531#endif
4532
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004533#ifdef FEAT_QUICKFIX
4534 qf_free_all(wp);
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537#ifdef FEAT_GUI
4538 if (gui.in_use)
4539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4541 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4542 }
4543#endif /* FEAT_GUI */
4544
Bram Moolenaar860cae12010-06-05 23:22:07 +02004545#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004546 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004547#endif
4548
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004549#ifdef FEAT_AUTOCMD
4550 if (wp != aucmd_win)
4551#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004552 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004554
4555#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004556 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558}
4559
4560/*
4561 * Append window "wp" in the window list after window "after".
4562 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004563 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564win_append(after, wp)
4565 win_T *after, *wp;
4566{
4567 win_T *before;
4568
4569 if (after == NULL) /* after NULL is in front of the first */
4570 before = firstwin;
4571 else
4572 before = after->w_next;
4573
4574 wp->w_next = before;
4575 wp->w_prev = after;
4576 if (after == NULL)
4577 firstwin = wp;
4578 else
4579 after->w_next = wp;
4580 if (before == NULL)
4581 lastwin = wp;
4582 else
4583 before->w_prev = wp;
4584}
4585
4586/*
4587 * Remove a window from the window list.
4588 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004589 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004590win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004592 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593{
4594 if (wp->w_prev != NULL)
4595 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004596 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004598 else
4599 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 if (wp->w_next != NULL)
4601 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004602 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004604 else
4605 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606}
4607
4608/*
4609 * Append frame "frp" in a frame list after frame "after".
4610 */
4611 static void
4612frame_append(after, frp)
4613 frame_T *after, *frp;
4614{
4615 frp->fr_next = after->fr_next;
4616 after->fr_next = frp;
4617 if (frp->fr_next != NULL)
4618 frp->fr_next->fr_prev = frp;
4619 frp->fr_prev = after;
4620}
4621
4622/*
4623 * Insert frame "frp" in a frame list before frame "before".
4624 */
4625 static void
4626frame_insert(before, frp)
4627 frame_T *before, *frp;
4628{
4629 frp->fr_next = before;
4630 frp->fr_prev = before->fr_prev;
4631 before->fr_prev = frp;
4632 if (frp->fr_prev != NULL)
4633 frp->fr_prev->fr_next = frp;
4634 else
4635 frp->fr_parent->fr_child = frp;
4636}
4637
4638/*
4639 * Remove a frame from a frame list.
4640 */
4641 static void
4642frame_remove(frp)
4643 frame_T *frp;
4644{
4645 if (frp->fr_prev != NULL)
4646 frp->fr_prev->fr_next = frp->fr_next;
4647 else
4648 frp->fr_parent->fr_child = frp->fr_next;
4649 if (frp->fr_next != NULL)
4650 frp->fr_next->fr_prev = frp->fr_prev;
4651}
4652
4653#endif /* FEAT_WINDOWS */
4654
4655/*
4656 * Allocate w_lines[] for window "wp".
4657 * Return FAIL for failure, OK for success.
4658 */
4659 int
4660win_alloc_lines(wp)
4661 win_T *wp;
4662{
4663 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004664 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (wp->w_lines == NULL)
4666 return FAIL;
4667 return OK;
4668}
4669
4670/*
4671 * free lsize arrays for a window
4672 */
4673 void
4674win_free_lsize(wp)
4675 win_T *wp;
4676{
4677 vim_free(wp->w_lines);
4678 wp->w_lines = NULL;
4679}
4680
4681/*
4682 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004683 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 */
4685 void
4686shell_new_rows()
4687{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004688 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 if (firstwin == NULL) /* not initialized yet */
4691 return;
4692#ifdef FEAT_WINDOWS
4693 if (h < frame_minheight(topframe, NULL))
4694 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004695
4696 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * that doesn't result in the right height, forget about that option. */
4698 frame_new_height(topframe, h, FALSE, TRUE);
4699 if (topframe->fr_height != h)
4700 frame_new_height(topframe, h, FALSE, FALSE);
4701
4702 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4703#else
4704 if (h < 1)
4705 h = 1;
4706 win_new_height(firstwin, h);
4707#endif
4708 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004709#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004710 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004711#endif
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#if 0
4714 /* Disabled: don't want making the screen smaller make a window larger. */
4715 if (p_ea)
4716 win_equal(curwin, FALSE, 'v');
4717#endif
4718}
4719
4720#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4721/*
4722 * Called from win_new_shellsize() after Columns changed.
4723 */
4724 void
4725shell_new_columns()
4726{
4727 if (firstwin == NULL) /* not initialized yet */
4728 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004729
4730 /* First try setting the widths of windows with 'winfixwidth'. If that
4731 * doesn't result in the right width, forget about that option. */
4732 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4733 if (topframe->fr_width != Columns)
4734 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4737#if 0
4738 /* Disabled: don't want making the screen smaller make a window larger. */
4739 if (p_ea)
4740 win_equal(curwin, FALSE, 'h');
4741#endif
4742}
4743#endif
4744
4745#if defined(FEAT_CMDWIN) || defined(PROTO)
4746/*
4747 * Save the size of all windows in "gap".
4748 */
4749 void
4750win_size_save(gap)
4751 garray_T *gap;
4752
4753{
4754 win_T *wp;
4755
4756 ga_init2(gap, (int)sizeof(int), 1);
4757 if (ga_grow(gap, win_count() * 2) == OK)
4758 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4759 {
4760 ((int *)gap->ga_data)[gap->ga_len++] =
4761 wp->w_width + wp->w_vsep_width;
4762 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4763 }
4764}
4765
4766/*
4767 * Restore window sizes, but only if the number of windows is still the same.
4768 * Does not free the growarray.
4769 */
4770 void
4771win_size_restore(gap)
4772 garray_T *gap;
4773{
4774 win_T *wp;
4775 int i;
4776
4777 if (win_count() * 2 == gap->ga_len)
4778 {
4779 i = 0;
4780 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4781 {
4782 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4783 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4784 }
4785 /* recompute the window positions */
4786 (void)win_comp_pos();
4787 }
4788}
4789#endif /* FEAT_CMDWIN */
4790
4791#if defined(FEAT_WINDOWS) || defined(PROTO)
4792/*
4793 * Update the position for all windows, using the width and height of the
4794 * frames.
4795 * Returns the row just after the last window.
4796 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004797 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798win_comp_pos()
4799{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004800 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int col = 0;
4802
4803 frame_comp_pos(topframe, &row, &col);
4804 return row;
4805}
4806
4807/*
4808 * Update the position of the windows in frame "topfrp", using the width and
4809 * height of the frames.
4810 * "*row" and "*col" are the top-left position of the frame. They are updated
4811 * to the bottom-right position plus one.
4812 */
4813 static void
4814frame_comp_pos(topfrp, row, col)
4815 frame_T *topfrp;
4816 int *row;
4817 int *col;
4818{
4819 win_T *wp;
4820 frame_T *frp;
4821#ifdef FEAT_VERTSPLIT
4822 int startcol;
4823 int startrow;
4824#endif
4825
4826 wp = topfrp->fr_win;
4827 if (wp != NULL)
4828 {
4829 if (wp->w_winrow != *row
4830#ifdef FEAT_VERTSPLIT
4831 || wp->w_wincol != *col
4832#endif
4833 )
4834 {
4835 /* position changed, redraw */
4836 wp->w_winrow = *row;
4837#ifdef FEAT_VERTSPLIT
4838 wp->w_wincol = *col;
4839#endif
4840 redraw_win_later(wp, NOT_VALID);
4841 wp->w_redr_status = TRUE;
4842 }
4843 *row += wp->w_height + wp->w_status_height;
4844#ifdef FEAT_VERTSPLIT
4845 *col += wp->w_width + wp->w_vsep_width;
4846#endif
4847 }
4848 else
4849 {
4850#ifdef FEAT_VERTSPLIT
4851 startrow = *row;
4852 startcol = *col;
4853#endif
4854 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4855 {
4856#ifdef FEAT_VERTSPLIT
4857 if (topfrp->fr_layout == FR_ROW)
4858 *row = startrow; /* all frames are at the same row */
4859 else
4860 *col = startcol; /* all frames are at the same col */
4861#endif
4862 frame_comp_pos(frp, row, col);
4863 }
4864 }
4865}
4866
4867#endif /* FEAT_WINDOWS */
4868
4869/*
4870 * Set current window height and take care of repositioning other windows to
4871 * fit around it.
4872 */
4873 void
4874win_setheight(height)
4875 int height;
4876{
4877 win_setheight_win(height, curwin);
4878}
4879
4880/*
4881 * Set the window height of window "win" and take care of repositioning other
4882 * windows to fit around it.
4883 */
4884 void
4885win_setheight_win(height, win)
4886 int height;
4887 win_T *win;
4888{
4889 int row;
4890
4891 if (win == curwin)
4892 {
4893 /* Always keep current window at least one line high, even when
4894 * 'winminheight' is zero. */
4895#ifdef FEAT_WINDOWS
4896 if (height < p_wmh)
4897 height = p_wmh;
4898#endif
4899 if (height == 0)
4900 height = 1;
4901 }
4902
4903#ifdef FEAT_WINDOWS
4904 frame_setheight(win->w_frame, height + win->w_status_height);
4905
4906 /* recompute the window positions */
4907 row = win_comp_pos();
4908#else
4909 if (height > topframe->fr_height)
4910 height = topframe->fr_height;
4911 win->w_height = height;
4912 row = height;
4913#endif
4914
4915 /*
4916 * If there is extra space created between the last window and the command
4917 * line, clear it.
4918 */
4919 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4920 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4921 cmdline_row = row;
4922 msg_row = row;
4923 msg_col = 0;
4924
4925 redraw_all_later(NOT_VALID);
4926}
4927
4928#if defined(FEAT_WINDOWS) || defined(PROTO)
4929
4930/*
4931 * Set the height of a frame to "height" and take care that all frames and
4932 * windows inside it are resized. Also resize frames on the left and right if
4933 * the are in the same FR_ROW frame.
4934 *
4935 * Strategy:
4936 * If the frame is part of a FR_COL frame, try fitting the frame in that
4937 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4938 * go to containing frames to resize them and make room.
4939 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4940 * Check for the minimal height of the FR_ROW frame.
4941 * At the top level we can also use change the command line height.
4942 */
4943 static void
4944frame_setheight(curfrp, height)
4945 frame_T *curfrp;
4946 int height;
4947{
4948 int room; /* total number of lines available */
4949 int take; /* number of lines taken from other windows */
4950 int room_cmdline; /* lines available from cmdline */
4951 int run;
4952 frame_T *frp;
4953 int h;
4954 int room_reserved;
4955
4956 /* If the height already is the desired value, nothing to do. */
4957 if (curfrp->fr_height == height)
4958 return;
4959
4960 if (curfrp->fr_parent == NULL)
4961 {
4962 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004963 if (height > ROWS_AVAIL)
4964 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (height > 0)
4966 frame_new_height(curfrp, height, FALSE, FALSE);
4967 }
4968 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4969 {
4970 /* Row of frames: Also need to resize frames left and right of this
4971 * one. First check for the minimal height of these. */
4972 h = frame_minheight(curfrp->fr_parent, NULL);
4973 if (height < h)
4974 height = h;
4975 frame_setheight(curfrp->fr_parent, height);
4976 }
4977 else
4978 {
4979 /*
4980 * Column of frames: try to change only frames in this column.
4981 */
4982#ifdef FEAT_VERTSPLIT
4983 /*
4984 * Do this twice:
4985 * 1: compute room available, if it's not enough try resizing the
4986 * containing frame.
4987 * 2: compute the room available and adjust the height to it.
4988 * Try not to reduce the height of a window with 'winfixheight' set.
4989 */
4990 for (run = 1; run <= 2; ++run)
4991#else
4992 for (;;)
4993#endif
4994 {
4995 room = 0;
4996 room_reserved = 0;
4997 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4998 frp = frp->fr_next)
4999 {
5000 if (frp != curfrp
5001 && frp->fr_win != NULL
5002 && frp->fr_win->w_p_wfh)
5003 room_reserved += frp->fr_height;
5004 room += frp->fr_height;
5005 if (frp != curfrp)
5006 room -= frame_minheight(frp, NULL);
5007 }
5008#ifdef FEAT_VERTSPLIT
5009 if (curfrp->fr_width != Columns)
5010 room_cmdline = 0;
5011 else
5012#endif
5013 {
5014 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5015 + lastwin->w_height + lastwin->w_status_height);
5016 if (room_cmdline < 0)
5017 room_cmdline = 0;
5018 }
5019
5020 if (height <= room + room_cmdline)
5021 break;
5022#ifdef FEAT_VERTSPLIT
5023 if (run == 2 || curfrp->fr_width == Columns)
5024#endif
5025 {
5026 if (height > room + room_cmdline)
5027 height = room + room_cmdline;
5028 break;
5029 }
5030#ifdef FEAT_VERTSPLIT
5031 frame_setheight(curfrp->fr_parent, height
5032 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5033#endif
5034 /*NOTREACHED*/
5035 }
5036
5037 /*
5038 * Compute the number of lines we will take from others frames (can be
5039 * negative!).
5040 */
5041 take = height - curfrp->fr_height;
5042
5043 /* If there is not enough room, also reduce the height of a window
5044 * with 'winfixheight' set. */
5045 if (height > room + room_cmdline - room_reserved)
5046 room_reserved = room + room_cmdline - height;
5047 /* If there is only a 'winfixheight' window and making the
5048 * window smaller, need to make the other window taller. */
5049 if (take < 0 && room - curfrp->fr_height < room_reserved)
5050 room_reserved = 0;
5051
5052 if (take > 0 && room_cmdline > 0)
5053 {
5054 /* use lines from cmdline first */
5055 if (take < room_cmdline)
5056 room_cmdline = take;
5057 take -= room_cmdline;
5058 topframe->fr_height += room_cmdline;
5059 }
5060
5061 /*
5062 * set the current frame to the new height
5063 */
5064 frame_new_height(curfrp, height, FALSE, FALSE);
5065
5066 /*
5067 * First take lines from the frames after the current frame. If
5068 * that is not enough, takes lines from frames above the current
5069 * frame.
5070 */
5071 for (run = 0; run < 2; ++run)
5072 {
5073 if (run == 0)
5074 frp = curfrp->fr_next; /* 1st run: start with next window */
5075 else
5076 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5077 while (frp != NULL && take != 0)
5078 {
5079 h = frame_minheight(frp, NULL);
5080 if (room_reserved > 0
5081 && frp->fr_win != NULL
5082 && frp->fr_win->w_p_wfh)
5083 {
5084 if (room_reserved >= frp->fr_height)
5085 room_reserved -= frp->fr_height;
5086 else
5087 {
5088 if (frp->fr_height - room_reserved > take)
5089 room_reserved = frp->fr_height - take;
5090 take -= frp->fr_height - room_reserved;
5091 frame_new_height(frp, room_reserved, FALSE, FALSE);
5092 room_reserved = 0;
5093 }
5094 }
5095 else
5096 {
5097 if (frp->fr_height - take < h)
5098 {
5099 take -= frp->fr_height - h;
5100 frame_new_height(frp, h, FALSE, FALSE);
5101 }
5102 else
5103 {
5104 frame_new_height(frp, frp->fr_height - take,
5105 FALSE, FALSE);
5106 take = 0;
5107 }
5108 }
5109 if (run == 0)
5110 frp = frp->fr_next;
5111 else
5112 frp = frp->fr_prev;
5113 }
5114 }
5115 }
5116}
5117
5118#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5119/*
5120 * Set current window width and take care of repositioning other windows to
5121 * fit around it.
5122 */
5123 void
5124win_setwidth(width)
5125 int width;
5126{
5127 win_setwidth_win(width, curwin);
5128}
5129
5130 void
5131win_setwidth_win(width, wp)
5132 int width;
5133 win_T *wp;
5134{
5135 /* Always keep current window at least one column wide, even when
5136 * 'winminwidth' is zero. */
5137 if (wp == curwin)
5138 {
5139 if (width < p_wmw)
5140 width = p_wmw;
5141 if (width == 0)
5142 width = 1;
5143 }
5144
5145 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5146
5147 /* recompute the window positions */
5148 (void)win_comp_pos();
5149
5150 redraw_all_later(NOT_VALID);
5151}
5152
5153/*
5154 * Set the width of a frame to "width" and take care that all frames and
5155 * windows inside it are resized. Also resize frames above and below if the
5156 * are in the same FR_ROW frame.
5157 *
5158 * Strategy is similar to frame_setheight().
5159 */
5160 static void
5161frame_setwidth(curfrp, width)
5162 frame_T *curfrp;
5163 int width;
5164{
5165 int room; /* total number of lines available */
5166 int take; /* number of lines taken from other windows */
5167 int run;
5168 frame_T *frp;
5169 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005170 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
5172 /* If the width already is the desired value, nothing to do. */
5173 if (curfrp->fr_width == width)
5174 return;
5175
5176 if (curfrp->fr_parent == NULL)
5177 /* topframe: can't change width */
5178 return;
5179
5180 if (curfrp->fr_parent->fr_layout == FR_COL)
5181 {
5182 /* Column of frames: Also need to resize frames above and below of
5183 * this one. First check for the minimal width of these. */
5184 w = frame_minwidth(curfrp->fr_parent, NULL);
5185 if (width < w)
5186 width = w;
5187 frame_setwidth(curfrp->fr_parent, width);
5188 }
5189 else
5190 {
5191 /*
5192 * Row of frames: try to change only frames in this row.
5193 *
5194 * Do this twice:
5195 * 1: compute room available, if it's not enough try resizing the
5196 * containing frame.
5197 * 2: compute the room available and adjust the width to it.
5198 */
5199 for (run = 1; run <= 2; ++run)
5200 {
5201 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005202 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5204 frp = frp->fr_next)
5205 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005206 if (frp != curfrp
5207 && frp->fr_win != NULL
5208 && frp->fr_win->w_p_wfw)
5209 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 room += frp->fr_width;
5211 if (frp != curfrp)
5212 room -= frame_minwidth(frp, NULL);
5213 }
5214
5215 if (width <= room)
5216 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005217 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 if (width > room)
5220 width = room;
5221 break;
5222 }
5223 frame_setwidth(curfrp->fr_parent, width
5224 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5225 }
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 /*
5228 * Compute the number of lines we will take from others frames (can be
5229 * negative!).
5230 */
5231 take = width - curfrp->fr_width;
5232
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005233 /* If there is not enough room, also reduce the width of a window
5234 * with 'winfixwidth' set. */
5235 if (width > room - room_reserved)
5236 room_reserved = room - width;
5237 /* If there is only a 'winfixwidth' window and making the
5238 * window smaller, need to make the other window narrower. */
5239 if (take < 0 && room - curfrp->fr_width < room_reserved)
5240 room_reserved = 0;
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 /*
5243 * set the current frame to the new width
5244 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005245 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 /*
5248 * First take lines from the frames right of the current frame. If
5249 * that is not enough, takes lines from frames left of the current
5250 * frame.
5251 */
5252 for (run = 0; run < 2; ++run)
5253 {
5254 if (run == 0)
5255 frp = curfrp->fr_next; /* 1st run: start with next window */
5256 else
5257 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5258 while (frp != NULL && take != 0)
5259 {
5260 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005261 if (room_reserved > 0
5262 && frp->fr_win != NULL
5263 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005265 if (room_reserved >= frp->fr_width)
5266 room_reserved -= frp->fr_width;
5267 else
5268 {
5269 if (frp->fr_width - room_reserved > take)
5270 room_reserved = frp->fr_width - take;
5271 take -= frp->fr_width - room_reserved;
5272 frame_new_width(frp, room_reserved, FALSE, FALSE);
5273 room_reserved = 0;
5274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005278 if (frp->fr_width - take < w)
5279 {
5280 take -= frp->fr_width - w;
5281 frame_new_width(frp, w, FALSE, FALSE);
5282 }
5283 else
5284 {
5285 frame_new_width(frp, frp->fr_width - take,
5286 FALSE, FALSE);
5287 take = 0;
5288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290 if (run == 0)
5291 frp = frp->fr_next;
5292 else
5293 frp = frp->fr_prev;
5294 }
5295 }
5296 }
5297}
5298#endif /* FEAT_VERTSPLIT */
5299
5300/*
5301 * Check 'winminheight' for a valid value.
5302 */
5303 void
5304win_setminheight()
5305{
5306 int room;
5307 int first = TRUE;
5308 win_T *wp;
5309
5310 /* loop until there is a 'winminheight' that is possible */
5311 while (p_wmh > 0)
5312 {
5313 /* TODO: handle vertical splits */
5314 room = -p_wh;
5315 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5316 room += wp->w_height - p_wmh;
5317 if (room >= 0)
5318 break;
5319 --p_wmh;
5320 if (first)
5321 {
5322 EMSG(_(e_noroom));
5323 first = FALSE;
5324 }
5325 }
5326}
5327
5328#ifdef FEAT_MOUSE
5329
5330/*
5331 * Status line of dragwin is dragged "offset" lines down (negative is up).
5332 */
5333 void
5334win_drag_status_line(dragwin, offset)
5335 win_T *dragwin;
5336 int offset;
5337{
5338 frame_T *curfr;
5339 frame_T *fr;
5340 int room;
5341 int row;
5342 int up; /* if TRUE, drag status line up, otherwise down */
5343 int n;
5344
5345 fr = dragwin->w_frame;
5346 curfr = fr;
5347 if (fr != topframe) /* more than one window */
5348 {
5349 fr = fr->fr_parent;
5350 /* When the parent frame is not a column of frames, its parent should
5351 * be. */
5352 if (fr->fr_layout != FR_COL)
5353 {
5354 curfr = fr;
5355 if (fr != topframe) /* only a row of windows, may drag statusline */
5356 fr = fr->fr_parent;
5357 }
5358 }
5359
5360 /* If this is the last frame in a column, may want to resize the parent
5361 * frame instead (go two up to skip a row of frames). */
5362 while (curfr != topframe && curfr->fr_next == NULL)
5363 {
5364 if (fr != topframe)
5365 fr = fr->fr_parent;
5366 curfr = fr;
5367 if (fr != topframe)
5368 fr = fr->fr_parent;
5369 }
5370
5371 if (offset < 0) /* drag up */
5372 {
5373 up = TRUE;
5374 offset = -offset;
5375 /* sum up the room of the current frame and above it */
5376 if (fr == curfr)
5377 {
5378 /* only one window */
5379 room = fr->fr_height - frame_minheight(fr, NULL);
5380 }
5381 else
5382 {
5383 room = 0;
5384 for (fr = fr->fr_child; ; fr = fr->fr_next)
5385 {
5386 room += fr->fr_height - frame_minheight(fr, NULL);
5387 if (fr == curfr)
5388 break;
5389 }
5390 }
5391 fr = curfr->fr_next; /* put fr at frame that grows */
5392 }
5393 else /* drag down */
5394 {
5395 up = FALSE;
5396 /*
5397 * Only dragging the last status line can reduce p_ch.
5398 */
5399 room = Rows - cmdline_row;
5400 if (curfr->fr_next == NULL)
5401 room -= 1;
5402 else
5403 room -= p_ch;
5404 if (room < 0)
5405 room = 0;
5406 /* sum up the room of frames below of the current one */
5407 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5408 room += fr->fr_height - frame_minheight(fr, NULL);
5409 fr = curfr; /* put fr at window that grows */
5410 }
5411
5412 if (room < offset) /* Not enough room */
5413 offset = room; /* Move as far as we can */
5414 if (offset <= 0)
5415 return;
5416
5417 /*
5418 * Grow frame fr by "offset" lines.
5419 * Doesn't happen when dragging the last status line up.
5420 */
5421 if (fr != NULL)
5422 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5423
5424 if (up)
5425 fr = curfr; /* current frame gets smaller */
5426 else
5427 fr = curfr->fr_next; /* next frame gets smaller */
5428
5429 /*
5430 * Now make the other frames smaller.
5431 */
5432 while (fr != NULL && offset > 0)
5433 {
5434 n = frame_minheight(fr, NULL);
5435 if (fr->fr_height - offset <= n)
5436 {
5437 offset -= fr->fr_height - n;
5438 frame_new_height(fr, n, !up, FALSE);
5439 }
5440 else
5441 {
5442 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5443 break;
5444 }
5445 if (up)
5446 fr = fr->fr_prev;
5447 else
5448 fr = fr->fr_next;
5449 }
5450 row = win_comp_pos();
5451 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5452 cmdline_row = row;
5453 p_ch = Rows - cmdline_row;
5454 if (p_ch < 1)
5455 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005456 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005457 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 showmode();
5459}
5460
5461#ifdef FEAT_VERTSPLIT
5462/*
5463 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5464 */
5465 void
5466win_drag_vsep_line(dragwin, offset)
5467 win_T *dragwin;
5468 int offset;
5469{
5470 frame_T *curfr;
5471 frame_T *fr;
5472 int room;
5473 int left; /* if TRUE, drag separator line left, otherwise right */
5474 int n;
5475
5476 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005477 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return;
5479 curfr = fr;
5480 fr = fr->fr_parent;
5481 /* When the parent frame is not a row of frames, its parent should be. */
5482 if (fr->fr_layout != FR_ROW)
5483 {
5484 if (fr == topframe) /* only a column of windows (cannot happen?) */
5485 return;
5486 curfr = fr;
5487 fr = fr->fr_parent;
5488 }
5489
5490 /* If this is the last frame in a row, may want to resize a parent
5491 * frame instead. */
5492 while (curfr->fr_next == NULL)
5493 {
5494 if (fr == topframe)
5495 break;
5496 curfr = fr;
5497 fr = fr->fr_parent;
5498 if (fr != topframe)
5499 {
5500 curfr = fr;
5501 fr = fr->fr_parent;
5502 }
5503 }
5504
5505 if (offset < 0) /* drag left */
5506 {
5507 left = TRUE;
5508 offset = -offset;
5509 /* sum up the room of the current frame and left of it */
5510 room = 0;
5511 for (fr = fr->fr_child; ; fr = fr->fr_next)
5512 {
5513 room += fr->fr_width - frame_minwidth(fr, NULL);
5514 if (fr == curfr)
5515 break;
5516 }
5517 fr = curfr->fr_next; /* put fr at frame that grows */
5518 }
5519 else /* drag right */
5520 {
5521 left = FALSE;
5522 /* sum up the room of frames right of the current one */
5523 room = 0;
5524 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5525 room += fr->fr_width - frame_minwidth(fr, NULL);
5526 fr = curfr; /* put fr at window that grows */
5527 }
5528
5529 if (room < offset) /* Not enough room */
5530 offset = room; /* Move as far as we can */
5531 if (offset <= 0) /* No room at all, quit. */
5532 return;
5533
5534 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005535 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 /* shrink other frames: current and at the left or at the right */
5538 if (left)
5539 fr = curfr; /* current frame gets smaller */
5540 else
5541 fr = curfr->fr_next; /* next frame gets smaller */
5542
5543 while (fr != NULL && offset > 0)
5544 {
5545 n = frame_minwidth(fr, NULL);
5546 if (fr->fr_width - offset <= n)
5547 {
5548 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005549 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else
5552 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005553 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 break;
5555 }
5556 if (left)
5557 fr = fr->fr_prev;
5558 else
5559 fr = fr->fr_next;
5560 }
5561 (void)win_comp_pos();
5562 redraw_all_later(NOT_VALID);
5563}
5564#endif /* FEAT_VERTSPLIT */
5565#endif /* FEAT_MOUSE */
5566
5567#endif /* FEAT_WINDOWS */
5568
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005569#define FRACTION_MULT 16384L
5570
5571/*
5572 * Set wp->w_fraction for the current w_wrow and w_height.
5573 */
5574 static void
5575set_fraction(wp)
5576 win_T *wp;
5577{
5578 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5579 + FRACTION_MULT / 2) / (long)wp->w_height;
5580}
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582/*
5583 * Set the height of a window.
5584 * This takes care of the things inside the window, not what happens to the
5585 * window position, the frame or to other windows.
5586 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005587 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588win_new_height(wp, height)
5589 win_T *wp;
5590 int height;
5591{
5592 linenr_T lnum;
5593 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 /* Don't want a negative height. Happens when splitting a tiny window.
5596 * Will equalize heights soon to fix it. */
5597 if (height < 0)
5598 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005599 if (wp->w_height == height)
5600 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
5602 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005603 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605 wp->w_height = height;
5606 wp->w_skipcol = 0;
5607
5608 /* Don't change w_topline when height is zero. Don't set w_topline when
5609 * 'scrollbind' is set and this isn't the current window. */
5610 if (height > 0
5611#ifdef FEAT_SCROLLBIND
5612 && (!wp->w_p_scb || wp == curwin)
5613#endif
5614 )
5615 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005616 /*
5617 * Find a value for w_topline that shows the cursor at the same
5618 * relative position in the window as before (more or less).
5619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 lnum = wp->w_cursor.lnum;
5621 if (lnum < 1) /* can happen when starting up */
5622 lnum = 1;
5623 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5624 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5625 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005626
5627 if (sline >= 0)
5628 {
5629 /* Make sure the whole cursor line is visible, if possible. */
5630 int rows = plines_win(wp, lnum, FALSE);
5631
5632 if (sline > wp->w_height - rows)
5633 {
5634 sline = wp->w_height - rows;
5635 wp->w_wrow -= rows - line_size;
5636 }
5637 }
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (sline < 0)
5640 {
5641 /*
5642 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005643 * Make cursor line the first line in the window. If not enough
5644 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 */
5646 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005647 if (wp->w_wrow >= wp->w_height
5648 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5649 {
5650 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5651 --wp->w_wrow;
5652 while (wp->w_wrow >= wp->w_height)
5653 {
5654 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5655 + win_col_off2(wp);
5656 --wp->w_wrow;
5657 }
5658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else
5661 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005662 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
5664#ifdef FEAT_FOLDING
5665 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5666 if (lnum == 1)
5667 {
5668 /* first line in buffer is folded */
5669 line_size = 1;
5670 --sline;
5671 break;
5672 }
5673#endif
5674 --lnum;
5675#ifdef FEAT_DIFF
5676 if (lnum == wp->w_topline)
5677 line_size = plines_win_nofill(wp, lnum, TRUE)
5678 + wp->w_topfill;
5679 else
5680#endif
5681 line_size = plines_win(wp, lnum, TRUE);
5682 sline -= line_size;
5683 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (sline < 0)
5686 {
5687 /*
5688 * Line we want at top would go off top of screen. Use next
5689 * line instead.
5690 */
5691#ifdef FEAT_FOLDING
5692 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5693#endif
5694 lnum++;
5695 wp->w_wrow -= line_size + sline;
5696 }
5697 else if (sline > 0)
5698 {
5699 /* First line of file reached, use that as topline. */
5700 lnum = 1;
5701 wp->w_wrow -= sline;
5702 }
5703 }
5704 set_topline(wp, lnum);
5705 }
5706
5707 if (wp == curwin)
5708 {
5709 if (p_so)
5710 update_topline();
5711 curs_columns(FALSE); /* validate w_wrow */
5712 }
5713 wp->w_prev_fraction_row = wp->w_wrow;
5714
5715 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005716 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717#ifdef FEAT_WINDOWS
5718 wp->w_redr_status = TRUE;
5719#endif
5720 invalidate_botline_win(wp);
5721}
5722
5723#ifdef FEAT_VERTSPLIT
5724/*
5725 * Set the width of a window.
5726 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005727 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728win_new_width(wp, width)
5729 win_T *wp;
5730 int width;
5731{
5732 wp->w_width = width;
5733 wp->w_lines_valid = 0;
5734 changed_line_abv_curs_win(wp);
5735 invalidate_botline_win(wp);
5736 if (wp == curwin)
5737 {
5738 update_topline();
5739 curs_columns(TRUE); /* validate w_wrow */
5740 }
5741 redraw_win_later(wp, NOT_VALID);
5742 wp->w_redr_status = TRUE;
5743}
5744#endif
5745
5746 void
5747win_comp_scroll(wp)
5748 win_T *wp;
5749{
5750 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5751 if (wp->w_p_scr == 0)
5752 wp->w_p_scr = 1;
5753}
5754
5755/*
5756 * command_height: called whenever p_ch has been changed
5757 */
5758 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005759command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761#ifdef FEAT_WINDOWS
5762 int h;
5763 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005764 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005766 /* Use the value of p_ch that we remembered. This is needed for when the
5767 * GUI starts up, we can't be sure in what order things happen. And when
5768 * p_ch was changed in another tab page. */
5769 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 /* Find bottom frame with width of screen. */
5772 frp = lastwin->w_frame;
5773# ifdef FEAT_VERTSPLIT
5774 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5775 frp = frp->fr_parent;
5776# endif
5777
5778 /* Avoid changing the height of a window with 'winfixheight' set. */
5779 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5780 && frp->fr_win->w_p_wfh)
5781 frp = frp->fr_prev;
5782
5783 if (starting != NO_SCREEN)
5784 {
5785 cmdline_row = Rows - p_ch;
5786
5787 if (p_ch > old_p_ch) /* p_ch got bigger */
5788 {
5789 while (p_ch > old_p_ch)
5790 {
5791 if (frp == NULL)
5792 {
5793 EMSG(_(e_noroom));
5794 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005795 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 cmdline_row = Rows - p_ch;
5797 break;
5798 }
5799 h = frp->fr_height - frame_minheight(frp, NULL);
5800 if (h > p_ch - old_p_ch)
5801 h = p_ch - old_p_ch;
5802 old_p_ch += h;
5803 frame_add_height(frp, -h);
5804 frp = frp->fr_prev;
5805 }
5806
5807 /* Recompute window positions. */
5808 (void)win_comp_pos();
5809
5810 /* clear the lines added to cmdline */
5811 if (full_screen)
5812 screen_fill((int)(cmdline_row), (int)Rows, 0,
5813 (int)Columns, ' ', ' ', 0);
5814 msg_row = cmdline_row;
5815 redraw_cmdline = TRUE;
5816 return;
5817 }
5818
5819 if (msg_row < cmdline_row)
5820 msg_row = cmdline_row;
5821 redraw_cmdline = TRUE;
5822 }
5823 frame_add_height(frp, (int)(old_p_ch - p_ch));
5824
5825 /* Recompute window positions. */
5826 if (frp != lastwin->w_frame)
5827 (void)win_comp_pos();
5828#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005830 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#endif
5832}
5833
5834#if defined(FEAT_WINDOWS) || defined(PROTO)
5835/*
5836 * Resize frame "frp" to be "n" lines higher (negative for less high).
5837 * Also resize the frames it is contained in.
5838 */
5839 static void
5840frame_add_height(frp, n)
5841 frame_T *frp;
5842 int n;
5843{
5844 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5845 for (;;)
5846 {
5847 frp = frp->fr_parent;
5848 if (frp == NULL)
5849 break;
5850 frp->fr_height += n;
5851 }
5852}
5853
5854/*
5855 * Add or remove a status line for the bottom window(s), according to the
5856 * value of 'laststatus'.
5857 */
5858 void
5859last_status(morewin)
5860 int morewin; /* pretend there are two or more windows */
5861{
5862 /* Don't make a difference between horizontal or vertical split. */
5863 last_status_rec(topframe, (p_ls == 2
5864 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5865}
5866
5867 static void
5868last_status_rec(fr, statusline)
5869 frame_T *fr;
5870 int statusline;
5871{
5872 frame_T *fp;
5873 win_T *wp;
5874
5875 if (fr->fr_layout == FR_LEAF)
5876 {
5877 wp = fr->fr_win;
5878 if (wp->w_status_height != 0 && !statusline)
5879 {
5880 /* remove status line */
5881 win_new_height(wp, wp->w_height + 1);
5882 wp->w_status_height = 0;
5883 comp_col();
5884 }
5885 else if (wp->w_status_height == 0 && statusline)
5886 {
5887 /* Find a frame to take a line from. */
5888 fp = fr;
5889 while (fp->fr_height <= frame_minheight(fp, NULL))
5890 {
5891 if (fp == topframe)
5892 {
5893 EMSG(_(e_noroom));
5894 return;
5895 }
5896 /* In a column of frames: go to frame above. If already at
5897 * the top or in a row of frames: go to parent. */
5898 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5899 fp = fp->fr_prev;
5900 else
5901 fp = fp->fr_parent;
5902 }
5903 wp->w_status_height = 1;
5904 if (fp != fr)
5905 {
5906 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5907 frame_fix_height(wp);
5908 (void)win_comp_pos();
5909 }
5910 else
5911 win_new_height(wp, wp->w_height - 1);
5912 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005913 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915 }
5916#ifdef FEAT_VERTSPLIT
5917 else if (fr->fr_layout == FR_ROW)
5918 {
5919 /* vertically split windows, set status line for each one */
5920 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5921 last_status_rec(fp, statusline);
5922 }
5923#endif
5924 else
5925 {
5926 /* horizontally split window, set status line for last one */
5927 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5928 ;
5929 last_status_rec(fp, statusline);
5930 }
5931}
5932
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005933/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005934 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005935 */
5936 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005937tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005938{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005939#ifdef FEAT_GUI_TABLINE
5940 /* When the GUI has the tabline then this always returns zero. */
5941 if (gui_use_tabline())
5942 return 0;
5943#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005944 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005945 {
5946 case 0: return 0;
5947 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5948 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 return 1;
5950}
5951
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952#endif /* FEAT_WINDOWS */
5953
5954#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5955/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005956 * Get the file name at the cursor.
5957 * If Visual mode is active, use the selected text if it's in one line.
5958 * Returns the name in allocated memory, NULL for failure.
5959 */
5960 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005961grab_file_name(count, file_lnum)
5962 long count;
5963 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005964{
5965# ifdef FEAT_VISUAL
5966 if (VIsual_active)
5967 {
5968 int len;
5969 char_u *ptr;
5970
5971 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5972 return NULL;
5973 return find_file_name_in_path(ptr, len,
5974 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5975 }
5976# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005977 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5978 file_lnum);
5979
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005980}
5981
5982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 * Return the file name under or after the cursor.
5984 *
5985 * The 'path' option is searched if the file name is not absolute.
5986 * The string returned has been alloc'ed and should be freed by the caller.
5987 * NULL is returned if the file name or file is not found.
5988 *
5989 * options:
5990 * FNAME_MESS give error messages
5991 * FNAME_EXP expand to path
5992 * FNAME_HYP check for hypertext link
5993 * FNAME_INCL apply "includeexpr"
5994 */
5995 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005996file_name_at_cursor(options, count, file_lnum)
5997 int options;
5998 long count;
5999 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006002 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6003 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004}
6005
6006/*
6007 * Return the name of the file under or after ptr[col].
6008 * Otherwise like file_name_at_cursor().
6009 */
6010 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006011file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 char_u *line;
6013 int col;
6014 int options;
6015 long count;
6016 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006017 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 char_u *ptr;
6020 int len;
6021
6022 /*
6023 * search forward for what could be the start of a file name
6024 */
6025 ptr = line + col;
6026 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006027 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (*ptr == NUL) /* nothing found */
6029 {
6030 if (options & FNAME_MESS)
6031 EMSG(_("E446: No file name under cursor"));
6032 return NULL;
6033 }
6034
6035 /*
6036 * Search backward for first char of the file name.
6037 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6038 */
6039 while (ptr > line)
6040 {
6041#ifdef FEAT_MBYTE
6042 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6043 ptr -= len + 1;
6044 else
6045#endif
6046 if (vim_isfilec(ptr[-1])
6047 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6048 --ptr;
6049 else
6050 break;
6051 }
6052
6053 /*
6054 * Search forward for the last char of the file name.
6055 * Also allow "://" when ':' is not in 'isfname'.
6056 */
6057 len = 0;
6058 while (vim_isfilec(ptr[len])
6059 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6060#ifdef FEAT_MBYTE
6061 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006062 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 else
6064#endif
6065 ++len;
6066
6067 /*
6068 * If there is trailing punctuation, remove it.
6069 * But don't remove "..", could be a directory name.
6070 */
6071 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6072 && ptr[len - 2] != '.')
6073 --len;
6074
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006075 if (file_lnum != NULL)
6076 {
6077 char_u *p;
6078
6079 /* Get the number after the file name and a separator character */
6080 p = ptr + len;
6081 p = skipwhite(p);
6082 if (*p != NUL)
6083 {
6084 if (!isdigit(*p))
6085 ++p; /* skip the separator */
6086 p = skipwhite(p);
6087 if (isdigit(*p))
6088 *file_lnum = (int)getdigits(&p);
6089 }
6090 }
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6093}
6094
6095# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6096static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6097
6098 static char_u *
6099eval_includeexpr(ptr, len)
6100 char_u *ptr;
6101 int len;
6102{
6103 char_u *res;
6104
6105 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006106 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006107 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 set_vim_var_string(VV_FNAME, NULL, 0);
6109 return res;
6110}
6111#endif
6112
6113/*
6114 * Return the name of the file ptr[len] in 'path'.
6115 * Otherwise like file_name_at_cursor().
6116 */
6117 char_u *
6118find_file_name_in_path(ptr, len, options, count, rel_fname)
6119 char_u *ptr;
6120 int len;
6121 int options;
6122 long count;
6123 char_u *rel_fname; /* file we are searching relative to */
6124{
6125 char_u *file_name;
6126 int c;
6127# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6128 char_u *tofree = NULL;
6129
6130 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6131 {
6132 tofree = eval_includeexpr(ptr, len);
6133 if (tofree != NULL)
6134 {
6135 ptr = tofree;
6136 len = (int)STRLEN(ptr);
6137 }
6138 }
6139# endif
6140
6141 if (options & FNAME_EXP)
6142 {
6143 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6144 TRUE, rel_fname);
6145
6146# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6147 /*
6148 * If the file could not be found in a normal way, try applying
6149 * 'includeexpr' (unless done already).
6150 */
6151 if (file_name == NULL
6152 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6153 {
6154 tofree = eval_includeexpr(ptr, len);
6155 if (tofree != NULL)
6156 {
6157 ptr = tofree;
6158 len = (int)STRLEN(ptr);
6159 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6160 TRUE, rel_fname);
6161 }
6162 }
6163# endif
6164 if (file_name == NULL && (options & FNAME_MESS))
6165 {
6166 c = ptr[len];
6167 ptr[len] = NUL;
6168 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6169 ptr[len] = c;
6170 }
6171
6172 /* Repeat finding the file "count" times. This matters when it
6173 * appears several times in the path. */
6174 while (file_name != NULL && --count > 0)
6175 {
6176 vim_free(file_name);
6177 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6178 }
6179 }
6180 else
6181 file_name = vim_strnsave(ptr, len);
6182
6183# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6184 vim_free(tofree);
6185# endif
6186
6187 return file_name;
6188}
6189#endif /* FEAT_SEARCHPATH */
6190
6191/*
6192 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6193 * Also check for ":\\", which MS Internet Explorer accepts, return
6194 * URL_BACKSLASH.
6195 */
6196 static int
6197path_is_url(p)
6198 char_u *p;
6199{
6200 if (STRNCMP(p, "://", (size_t)3) == 0)
6201 return URL_SLASH;
6202 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6203 return URL_BACKSLASH;
6204 return 0;
6205}
6206
6207/*
6208 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6209 * Return URL_BACKSLASH for "name:\\".
6210 * Return zero otherwise.
6211 */
6212 int
6213path_with_url(fname)
6214 char_u *fname;
6215{
6216 char_u *p;
6217
6218 for (p = fname; isalpha(*p); ++p)
6219 ;
6220 return path_is_url(p);
6221}
6222
6223/*
6224 * Return TRUE if "name" is a full (absolute) path name or URL.
6225 */
6226 int
6227vim_isAbsName(name)
6228 char_u *name;
6229{
6230 return (path_with_url(name) != 0 || mch_isFullName(name));
6231}
6232
6233/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006234 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 *
6236 * return FAIL for failure, OK otherwise
6237 */
6238 int
6239vim_FullName(fname, buf, len, force)
6240 char_u *fname, *buf;
6241 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006242 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 int retval = OK;
6245 int url;
6246
6247 *buf = NUL;
6248 if (fname == NULL)
6249 return FAIL;
6250
6251 url = path_with_url(fname);
6252 if (!url)
6253 retval = mch_FullName(fname, buf, len, force);
6254 if (url || retval == FAIL)
6255 {
6256 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006257 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
6259#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6260 slash_adjust(buf);
6261#endif
6262 return retval;
6263}
6264
6265/*
6266 * Return the minimal number of rows that is needed on the screen to display
6267 * the current number of windows.
6268 */
6269 int
6270min_rows()
6271{
6272 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006273#ifdef FEAT_WINDOWS
6274 tabpage_T *tp;
6275 int n;
6276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
6278 if (firstwin == NULL) /* not initialized yet */
6279 return MIN_LINES;
6280
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006282 total = 0;
6283 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6284 {
6285 n = frame_minheight(tp->tp_topframe, NULL);
6286 if (total < n)
6287 total = n;
6288 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006289 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006291 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006293 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 return total;
6295}
6296
6297/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006298 * Return TRUE if there is only one window (in the current tab page), not
6299 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006300 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 */
6302 int
6303only_one_window()
6304{
6305#ifdef FEAT_WINDOWS
6306 int count = 0;
6307 win_T *wp;
6308
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 /* If there is another tab page there always is another window. */
6310 if (first_tabpage->tp_next != NULL)
6311 return FALSE;
6312
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006314 if (wp->w_buffer != NULL
6315 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316# ifdef FEAT_QUICKFIX
6317 || wp->w_p_pvw
6318# endif
6319 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006320# ifdef FEAT_AUTOCMD
6321 && wp != aucmd_win
6322# endif
6323 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 ++count;
6325 return (count <= 1);
6326#else
6327 return TRUE;
6328#endif
6329}
6330
6331#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6332/*
6333 * Correct the cursor line number in other windows. Used after changing the
6334 * current buffer, and before applying autocommands.
6335 * When "do_curwin" is TRUE, also check current window.
6336 */
6337 void
6338check_lnums(do_curwin)
6339 int do_curwin;
6340{
6341 win_T *wp;
6342
6343#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006344 tabpage_T *tp;
6345
6346 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6348#else
6349 wp = curwin;
6350 if (do_curwin)
6351#endif
6352 {
6353 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6354 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6355 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6356 wp->w_topline = curbuf->b_ml.ml_line_count;
6357 }
6358}
6359#endif
6360
6361#if defined(FEAT_WINDOWS) || defined(PROTO)
6362
6363/*
6364 * A snapshot of the window sizes, to restore them after closing the help
6365 * window.
6366 * Only these fields are used:
6367 * fr_layout
6368 * fr_width
6369 * fr_height
6370 * fr_next
6371 * fr_child
6372 * fr_win (only valid for the old curwin, NULL otherwise)
6373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375/*
6376 * Create a snapshot of the current frame sizes.
6377 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006378 void
6379make_snapshot(idx)
6380 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006382 clear_snapshot(curtab, idx);
6383 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384}
6385
6386 static void
6387make_snapshot_rec(fr, frp)
6388 frame_T *fr;
6389 frame_T **frp;
6390{
6391 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6392 if (*frp == NULL)
6393 return;
6394 (*frp)->fr_layout = fr->fr_layout;
6395# ifdef FEAT_VERTSPLIT
6396 (*frp)->fr_width = fr->fr_width;
6397# endif
6398 (*frp)->fr_height = fr->fr_height;
6399 if (fr->fr_next != NULL)
6400 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6401 if (fr->fr_child != NULL)
6402 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6403 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6404 (*frp)->fr_win = curwin;
6405}
6406
6407/*
6408 * Remove any existing snapshot.
6409 */
6410 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006411clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006412 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006413 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006415 clear_snapshot_rec(tp->tp_snapshot[idx]);
6416 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417}
6418
6419 static void
6420clear_snapshot_rec(fr)
6421 frame_T *fr;
6422{
6423 if (fr != NULL)
6424 {
6425 clear_snapshot_rec(fr->fr_next);
6426 clear_snapshot_rec(fr->fr_child);
6427 vim_free(fr);
6428 }
6429}
6430
6431/*
6432 * Restore a previously created snapshot, if there is any.
6433 * This is only done if the screen size didn't change and the window layout is
6434 * still the same.
6435 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006436 void
6437restore_snapshot(idx, close_curwin)
6438 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 int close_curwin; /* closing current window */
6440{
6441 win_T *wp;
6442
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006443 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006445 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006447 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6448 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006450 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 win_comp_pos();
6452 if (wp != NULL && close_curwin)
6453 win_goto(wp);
6454 redraw_all_later(CLEAR);
6455 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006456 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457}
6458
6459/*
6460 * Check if frames "sn" and "fr" have the same layout, same following frames
6461 * and same children.
6462 */
6463 static int
6464check_snapshot_rec(sn, fr)
6465 frame_T *sn;
6466 frame_T *fr;
6467{
6468 if (sn->fr_layout != fr->fr_layout
6469 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6470 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6471 || (sn->fr_next != NULL
6472 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6473 || (sn->fr_child != NULL
6474 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6475 return FAIL;
6476 return OK;
6477}
6478
6479/*
6480 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6481 * following frames and children.
6482 * Returns a pointer to the old current window, or NULL.
6483 */
6484 static win_T *
6485restore_snapshot_rec(sn, fr)
6486 frame_T *sn;
6487 frame_T *fr;
6488{
6489 win_T *wp = NULL;
6490 win_T *wp2;
6491
6492 fr->fr_height = sn->fr_height;
6493# ifdef FEAT_VERTSPLIT
6494 fr->fr_width = sn->fr_width;
6495# endif
6496 if (fr->fr_layout == FR_LEAF)
6497 {
6498 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6499# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006500 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501# endif
6502 wp = sn->fr_win;
6503 }
6504 if (sn->fr_next != NULL)
6505 {
6506 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6507 if (wp2 != NULL)
6508 wp = wp2;
6509 }
6510 if (sn->fr_child != NULL)
6511 {
6512 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6513 if (wp2 != NULL)
6514 wp = wp2;
6515 }
6516 return wp;
6517}
6518
6519#endif
6520
6521#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6522/*
6523 * Return TRUE if there is any vertically split window.
6524 */
6525 int
6526win_hasvertsplit()
6527{
6528 frame_T *fr;
6529
6530 if (topframe->fr_layout == FR_ROW)
6531 return TRUE;
6532
6533 if (topframe->fr_layout == FR_COL)
6534 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6535 if (fr->fr_layout == FR_ROW)
6536 return TRUE;
6537
6538 return FALSE;
6539}
6540#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006541
6542#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6543/*
6544 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006545 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006546 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6547 * If no particular ID is desired, -1 must be specified for 'id'.
6548 * Return ID of added match, -1 on failure.
6549 */
6550 int
6551match_add(wp, grp, pat, prio, id)
6552 win_T *wp;
6553 char_u *grp;
6554 char_u *pat;
6555 int prio;
6556 int id;
6557{
6558 matchitem_T *cur;
6559 matchitem_T *prev;
6560 matchitem_T *m;
6561 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006562 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006563
6564 if (*grp == NUL || *pat == NUL)
6565 return -1;
6566 if (id < -1 || id == 0)
6567 {
6568 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6569 return -1;
6570 }
6571 if (id != -1)
6572 {
6573 cur = wp->w_match_head;
6574 while (cur != NULL)
6575 {
6576 if (cur->id == id)
6577 {
6578 EMSGN("E801: ID already taken: %ld", id);
6579 return -1;
6580 }
6581 cur = cur->next;
6582 }
6583 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006584 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006585 {
6586 EMSG2(_(e_nogroup), grp);
6587 return -1;
6588 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006589 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006590 {
6591 EMSG2(_(e_invarg2), pat);
6592 return -1;
6593 }
6594
6595 /* Find available match ID. */
6596 while (id == -1)
6597 {
6598 cur = wp->w_match_head;
6599 while (cur != NULL && cur->id != wp->w_next_match_id)
6600 cur = cur->next;
6601 if (cur == NULL)
6602 id = wp->w_next_match_id;
6603 wp->w_next_match_id++;
6604 }
6605
6606 /* Build new match. */
6607 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6608 m->id = id;
6609 m->priority = prio;
6610 m->pattern = vim_strsave(pat);
6611 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006612 m->match.regprog = regprog;
6613 m->match.rmm_ic = FALSE;
6614 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006615
6616 /* Insert new match. The match list is in ascending order with regard to
6617 * the match priorities. */
6618 cur = wp->w_match_head;
6619 prev = cur;
6620 while (cur != NULL && prio >= cur->priority)
6621 {
6622 prev = cur;
6623 cur = cur->next;
6624 }
6625 if (cur == prev)
6626 wp->w_match_head = m;
6627 else
6628 prev->next = m;
6629 m->next = cur;
6630
6631 redraw_later(SOME_VALID);
6632 return id;
6633}
6634
6635/*
6636 * Delete match with ID 'id' in the match list of window 'wp'.
6637 * Print error messages if 'perr' is TRUE.
6638 */
6639 int
6640match_delete(wp, id, perr)
6641 win_T *wp;
6642 int id;
6643 int perr;
6644{
6645 matchitem_T *cur = wp->w_match_head;
6646 matchitem_T *prev = cur;
6647
6648 if (id < 1)
6649 {
6650 if (perr == TRUE)
6651 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6652 id);
6653 return -1;
6654 }
6655 while (cur != NULL && cur->id != id)
6656 {
6657 prev = cur;
6658 cur = cur->next;
6659 }
6660 if (cur == NULL)
6661 {
6662 if (perr == TRUE)
6663 EMSGN("E803: ID not found: %ld", id);
6664 return -1;
6665 }
6666 if (cur == prev)
6667 wp->w_match_head = cur->next;
6668 else
6669 prev->next = cur->next;
6670 vim_free(cur->match.regprog);
6671 vim_free(cur->pattern);
6672 vim_free(cur);
6673 redraw_later(SOME_VALID);
6674 return 0;
6675}
6676
6677/*
6678 * Delete all matches in the match list of window 'wp'.
6679 */
6680 void
6681clear_matches(wp)
6682 win_T *wp;
6683{
6684 matchitem_T *m;
6685
6686 while (wp->w_match_head != NULL)
6687 {
6688 m = wp->w_match_head->next;
6689 vim_free(wp->w_match_head->match.regprog);
6690 vim_free(wp->w_match_head->pattern);
6691 vim_free(wp->w_match_head);
6692 wp->w_match_head = m;
6693 }
6694 redraw_later(SOME_VALID);
6695}
6696
6697/*
6698 * Get match from ID 'id' in window 'wp'.
6699 * Return NULL if match not found.
6700 */
6701 matchitem_T *
6702get_match(wp, id)
6703 win_T *wp;
6704 int id;
6705{
6706 matchitem_T *cur = wp->w_match_head;
6707
6708 while (cur != NULL && cur->id != id)
6709 cur = cur->next;
6710 return cur;
6711}
6712#endif