blob: ecf2aa29ab8f53c0926ef4c22efff12cb6b6cb18 [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 Moolenaar1d2ba7f2006-02-14 22:29:30 +000048static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
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
57static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
60#endif
61static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
70#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000071
Bram Moolenaar746ebd32009-06-16 14:01:43 +000072static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010073static void set_fraction __ARGS((win_T *wp));
74static void win_new_height __ARGS((win_T *wp, int height));
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
76#define URL_SLASH 1 /* path_is_url() has found "://" */
77#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
78
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000079#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar05159a02005-02-26 23:04:13 +000081#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000082# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000083#else
84# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#endif
86
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000088
89static char *m_onlyone = N_("Already only one window");
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091/*
92 * all CTRL-W window commands are handled here, called from normal_cmd().
93 */
94 void
95do_window(nchar, Prenum, xchar)
96 int nchar;
97 long Prenum;
98 int xchar; /* extra char from ":wincmd gx" or NUL */
99{
100 long Prenum1;
101 win_T *wp;
102#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
103 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000104 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
106#ifdef FEAT_FIND_ID
107 int type = FIND_DEFINE;
108 int len;
109#endif
110 char_u cbuf[40];
111
112 if (Prenum == 0)
113 Prenum1 = 1;
114 else
115 Prenum1 = Prenum;
116
117#ifdef FEAT_CMDWIN
118# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
119#else
120# define CHECK_CMDWIN
121#endif
122
123 switch (nchar)
124 {
125/* split current window in two parts, horizontally */
126 case 'S':
127 case Ctrl_S:
128 case 's':
129 CHECK_CMDWIN
130#ifdef FEAT_VISUAL
131 reset_VIsual_and_resel(); /* stop Visual mode */
132#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000133#ifdef FEAT_QUICKFIX
134 /* When splitting the quickfix window open a new buffer in it,
135 * don't replicate the quickfix buffer. */
136 if (bt_quickfix(curbuf))
137 goto newwindow;
138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#ifdef FEAT_GUI
140 need_mouse_correct = TRUE;
141#endif
142 win_split((int)Prenum, 0);
143 break;
144
145#ifdef FEAT_VERTSPLIT
146/* split current window in two parts, vertically */
147 case Ctrl_V:
148 case 'v':
149 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000150# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# endif
153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170#ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172#endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000175 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
176 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 do_cmdline_cmd(cbuf);
178 break;
179
180/* open new window */
181 case Ctrl_N:
182 case 'n':
183 CHECK_CMDWIN
184#ifdef FEAT_VISUAL
185 reset_VIsual_and_resel(); /* stop Visual mode */
186#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000187#ifdef FEAT_QUICKFIX
188newwindow:
189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000191 /* window height */
192 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 else
194 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000195#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
196 if (nchar == 'v' || nchar == Ctrl_V)
197 STRCAT(cbuf, "v");
198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 STRCAT(cbuf, "new");
200 do_cmdline_cmd(cbuf);
201 break;
202
203/* quit current window */
204 case Ctrl_Q:
205 case 'q':
206#ifdef FEAT_VISUAL
207 reset_VIsual_and_resel(); /* stop Visual mode */
208#endif
209 do_cmdline_cmd((char_u *)"quit");
210 break;
211
212/* close current window */
213 case Ctrl_C:
214 case 'c':
215#ifdef FEAT_VISUAL
216 reset_VIsual_and_resel(); /* stop Visual mode */
217#endif
218 do_cmdline_cmd((char_u *)"close");
219 break;
220
221#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
222/* close preview window */
223 case Ctrl_Z:
224 case 'z':
225 CHECK_CMDWIN
226#ifdef FEAT_VISUAL
227 reset_VIsual_and_resel(); /* stop Visual mode */
228#endif
229 do_cmdline_cmd((char_u *)"pclose");
230 break;
231
232/* cursor to preview window */
233 case 'P':
234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
235 if (wp->w_p_pvw)
236 break;
237 if (wp == NULL)
238 EMSG(_("E441: There is no preview window"));
239 else
240 win_goto(wp);
241 break;
242#endif
243
244/* close all but current window */
245 case Ctrl_O:
246 case 'o':
247 CHECK_CMDWIN
248#ifdef FEAT_VISUAL
249 reset_VIsual_and_resel(); /* stop Visual mode */
250#endif
251 do_cmdline_cmd((char_u *)"only");
252 break;
253
254/* cursor to next window with wrap around */
255 case Ctrl_W:
256 case 'w':
257/* cursor to previous window with wrap around */
258 case 'W':
259 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000260 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 beep_flush();
262 else
263 {
264 if (Prenum) /* go to specified window */
265 {
266 for (wp = firstwin; --Prenum > 0; )
267 {
268 if (wp->w_next == NULL)
269 break;
270 else
271 wp = wp->w_next;
272 }
273 }
274 else
275 {
276 if (nchar == 'W') /* go to previous window */
277 {
278 wp = curwin->w_prev;
279 if (wp == NULL)
280 wp = lastwin; /* wrap around */
281 }
282 else /* go to next window */
283 {
284 wp = curwin->w_next;
285 if (wp == NULL)
286 wp = firstwin; /* wrap around */
287 }
288 }
289 win_goto(wp);
290 }
291 break;
292
293/* cursor to window below */
294 case 'j':
295 case K_DOWN:
296 case Ctrl_J:
297 CHECK_CMDWIN
298#ifdef FEAT_VERTSPLIT
299 win_goto_ver(FALSE, Prenum1);
300#else
301 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
302 wp = wp->w_next)
303 ;
304 win_goto(wp);
305#endif
306 break;
307
308/* cursor to window above */
309 case 'k':
310 case K_UP:
311 case Ctrl_K:
312 CHECK_CMDWIN
313#ifdef FEAT_VERTSPLIT
314 win_goto_ver(TRUE, Prenum1);
315#else
316 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
317 wp = wp->w_prev)
318 ;
319 win_goto(wp);
320#endif
321 break;
322
323#ifdef FEAT_VERTSPLIT
324/* cursor to left window */
325 case 'h':
326 case K_LEFT:
327 case Ctrl_H:
328 case K_BS:
329 CHECK_CMDWIN
330 win_goto_hor(TRUE, Prenum1);
331 break;
332
333/* cursor to right window */
334 case 'l':
335 case K_RIGHT:
336 case Ctrl_L:
337 CHECK_CMDWIN
338 win_goto_hor(FALSE, Prenum1);
339 break;
340#endif
341
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342/* move window to new tab page */
343 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000344 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 MSG(_(m_onlyone));
346 else
347 {
348 tabpage_T *oldtab = curtab;
349 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350
351 /* First create a new tab with the window, then go back to
352 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000353 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000354 if (win_new_tabpage((int)Prenum) == OK
355 && valid_tabpage(oldtab))
356 {
357 newtab = curtab;
358 goto_tabpage_tp(oldtab);
359 if (curwin == wp)
360 win_close(curwin, FALSE);
361 if (valid_tabpage(newtab))
362 goto_tabpage_tp(newtab);
363 }
364 }
365 break;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367/* cursor to top-left window */
368 case 't':
369 case Ctrl_T:
370 win_goto(firstwin);
371 break;
372
373/* cursor to bottom-right window */
374 case 'b':
375 case Ctrl_B:
376 win_goto(lastwin);
377 break;
378
379/* cursor to last accessed (previous) window */
380 case 'p':
381 case Ctrl_P:
382 if (prevwin == NULL)
383 beep_flush();
384 else
385 win_goto(prevwin);
386 break;
387
388/* exchange current and next window */
389 case 'x':
390 case Ctrl_X:
391 CHECK_CMDWIN
392 win_exchange(Prenum);
393 break;
394
395/* rotate windows downwards */
396 case Ctrl_R:
397 case 'r':
398 CHECK_CMDWIN
399#ifdef FEAT_VISUAL
400 reset_VIsual_and_resel(); /* stop Visual mode */
401#endif
402 win_rotate(FALSE, (int)Prenum1); /* downwards */
403 break;
404
405/* rotate windows upwards */
406 case 'R':
407 CHECK_CMDWIN
408#ifdef FEAT_VISUAL
409 reset_VIsual_and_resel(); /* stop Visual mode */
410#endif
411 win_rotate(TRUE, (int)Prenum1); /* upwards */
412 break;
413
414/* move window to the very top/bottom/left/right */
415 case 'K':
416 case 'J':
417#ifdef FEAT_VERTSPLIT
418 case 'H':
419 case 'L':
420#endif
421 CHECK_CMDWIN
422 win_totop((int)Prenum,
423 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
424 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
425 break;
426
427/* make all windows the same height */
428 case '=':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_equal(NULL, FALSE, 'b');
433 break;
434
435/* increase current window height */
436 case '+':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(curwin->w_height + (int)Prenum1);
441 break;
442
443/* decrease current window height */
444 case '-':
445#ifdef FEAT_GUI
446 need_mouse_correct = TRUE;
447#endif
448 win_setheight(curwin->w_height - (int)Prenum1);
449 break;
450
451/* set current window height */
452 case Ctrl__:
453 case '_':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setheight(Prenum ? (int)Prenum : 9999);
458 break;
459
460#ifdef FEAT_VERTSPLIT
461/* increase current window width */
462 case '>':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(curwin->w_width + (int)Prenum1);
467 break;
468
469/* decrease current window width */
470 case '<':
471#ifdef FEAT_GUI
472 need_mouse_correct = TRUE;
473#endif
474 win_setwidth(curwin->w_width - (int)Prenum1);
475 break;
476
477/* set current window width */
478 case '|':
479#ifdef FEAT_GUI
480 need_mouse_correct = TRUE;
481#endif
482 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
483 break;
484#endif
485
486/* jump to tag and split window if tag exists (in preview window) */
487#if defined(FEAT_QUICKFIX)
488 case '}':
489 CHECK_CMDWIN
490 if (Prenum)
491 g_do_tagpreview = Prenum;
492 else
493 g_do_tagpreview = p_pvh;
494 /*FALLTHROUGH*/
495#endif
496 case ']':
497 case Ctrl_RSB:
498 CHECK_CMDWIN
499#ifdef FEAT_VISUAL
500 reset_VIsual_and_resel(); /* stop Visual mode */
501#endif
502 if (Prenum)
503 postponed_split = Prenum;
504 else
505 postponed_split = -1;
506
507 /* Execute the command right here, required when
508 * "wincmd ]" was used in a function. */
509 do_nv_ident(Ctrl_RSB, NUL);
510 break;
511
512#ifdef FEAT_SEARCHPATH
513/* edit file name under cursor in a new window */
514 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000515 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000517wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000519
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000520 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (ptr != NULL)
522 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000523# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000525# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 setpcmark();
527 if (win_split(0, 0) == OK)
528 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200529 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000530 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
531 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000532 if (nchar == 'F' && lnum >= 0)
533 {
534 curwin->w_cursor.lnum = lnum;
535 check_cursor_lnum();
536 beginline(BL_SOL | BL_FIX);
537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
539 vim_free(ptr);
540 }
541 break;
542#endif
543
544#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000545/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 * new window -- webb
547 */
548 case 'i': /* Go to any match */
549 case Ctrl_I:
550 type = FIND_ANY;
551 /* FALLTHROUGH */
552 case 'd': /* Go to definition, using 'define' */
553 case Ctrl_D:
554 CHECK_CMDWIN
555 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
556 break;
557 find_pattern_in_path(ptr, 0, len, TRUE,
558 Prenum == 0 ? TRUE : FALSE, type,
559 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
560 curwin->w_set_curswant = TRUE;
561 break;
562#endif
563
Bram Moolenaar05159a02005-02-26 23:04:13 +0000564 case K_KENTER:
565 case CAR:
566#if defined(FEAT_QUICKFIX)
567 /*
568 * In a quickfix window a <CR> jumps to the error under the
569 * cursor in a new window.
570 */
571 if (bt_quickfix(curbuf))
572 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000573 sprintf((char *)cbuf, "split +%ld%s",
574 (long)curwin->w_cursor.lnum,
575 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000576 do_cmdline_cmd(cbuf);
577 }
578#endif
579 break;
580
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/* CTRL-W g extended commands */
583 case 'g':
584 case Ctrl_G:
585 CHECK_CMDWIN
586#ifdef USE_ON_FLY_SCROLL
587 dont_scroll = TRUE; /* disallow scrolling here */
588#endif
589 ++no_mapping;
590 ++allow_keys; /* no mapping for xchar, but allow key codes */
591 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000592 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 --no_mapping;
595 --allow_keys;
596#ifdef FEAT_CMDL_INFO
597 (void)add_to_showcmd(xchar);
598#endif
599 switch (xchar)
600 {
601#if defined(FEAT_QUICKFIX)
602 case '}':
603 xchar = Ctrl_RSB;
604 if (Prenum)
605 g_do_tagpreview = Prenum;
606 else
607 g_do_tagpreview = p_pvh;
608 /*FALLTHROUGH*/
609#endif
610 case ']':
611 case Ctrl_RSB:
612#ifdef FEAT_VISUAL
613 reset_VIsual_and_resel(); /* stop Visual mode */
614#endif
615 if (Prenum)
616 postponed_split = Prenum;
617 else
618 postponed_split = -1;
619
620 /* Execute the command right here, required when
621 * "wincmd g}" was used in a function. */
622 do_nv_ident('g', xchar);
623 break;
624
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000625#ifdef FEAT_SEARCHPATH
626 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000627 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100628 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000629 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000630 goto wingotofile;
631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 default:
633 beep_flush();
634 break;
635 }
636 break;
637
638 default: beep_flush();
639 break;
640 }
641}
642
643/*
644 * split the current window, implements CTRL-W s and :split
645 *
646 * "size" is the height or width for the new window, 0 to use half of current
647 * height or width.
648 *
649 * "flags":
650 * WSP_ROOM: require enough room for new window
651 * WSP_VERT: vertical split.
652 * WSP_TOP: open window at the top-left of the shell (help window).
653 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
654 * WSP_HELP: creating the help window, keep layout snapshot
655 *
656 * return FAIL for failure, OK otherwise
657 */
658 int
659win_split(size, flags)
660 int size;
661 int flags;
662{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000663 /* When the ":tab" modifier was used open a new tab page instead. */
664 if (may_open_tabpage() == OK)
665 return OK;
666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 /* Add flags from ":vertical", ":topleft" and ":botright". */
668 flags |= cmdmod.split;
669 if ((flags & WSP_TOP) && (flags & WSP_BOT))
670 {
671 EMSG(_("E442: Can't split topleft and botright at the same time"));
672 return FAIL;
673 }
674
675 /* When creating the help window make a snapshot of the window layout.
676 * Otherwise clear the snapshot, it's now invalid. */
677 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000680 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 return win_split_ins(size, flags, NULL, 0);
683}
684
685/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100686 * When "new_wp" is NULL: split the current window in two.
687 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 * top/left/right/bottom.
689 * return FAIL for failure, OK otherwise
690 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000691 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100692win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int size;
694 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100695 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 int dir;
697{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100698 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 win_T *oldwin;
700 int new_size = size;
701 int i;
702 int need_status = 0;
703 int do_equal = FALSE;
704 int needed;
705 int available;
706 int oldwin_height = 0;
707 int layout;
708 frame_T *frp, *curfrp;
709 int before;
710
711 if (flags & WSP_TOP)
712 oldwin = firstwin;
713 else if (flags & WSP_BOT)
714 oldwin = lastwin;
715 else
716 oldwin = curwin;
717
718 /* add a status line when p_ls == 1 and splitting the first window */
719 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
720 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100721 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 EMSG(_(e_noroom));
724 return FAIL;
725 }
726 need_status = STATUS_HEIGHT;
727 }
728
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000729#ifdef FEAT_GUI
730 /* May be needed for the scrollbars that are going to change. */
731 if (gui.in_use)
732 out_flush();
733#endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef FEAT_VERTSPLIT
736 if (flags & WSP_VERT)
737 {
738 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740 /*
741 * Check if we are able to split the current window and compute its
742 * width.
743 */
744 needed = p_wmw + 1;
745 if (flags & WSP_ROOM)
746 needed += p_wiw - p_wmw;
747 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
748 {
749 available = topframe->fr_width;
750 needed += frame_minwidth(topframe, NULL);
751 }
752 else
753 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100754 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
756 EMSG(_(e_noroom));
757 return FAIL;
758 }
759 if (new_size == 0)
760 new_size = oldwin->w_width / 2;
761 if (new_size > oldwin->w_width - p_wmw - 1)
762 new_size = oldwin->w_width - p_wmw - 1;
763 if (new_size < p_wmw)
764 new_size = p_wmw;
765
766 /* if it doesn't fit in the current window, need win_equal() */
767 if (oldwin->w_width - new_size - 1 < p_wmw)
768 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000769
770 /* We don't like to take lines for the new window from a
771 * 'winfixwidth' window. Take them from a window to the left or right
772 * instead, if possible. */
773 if (oldwin->w_p_wfw)
774 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000775
776 /* Only make all windows the same width if one of them (except oldwin)
777 * is wider than one of the split windows. */
778 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
779 && oldwin->w_frame->fr_parent != NULL)
780 {
781 frp = oldwin->w_frame->fr_parent->fr_child;
782 while (frp != NULL)
783 {
784 if (frp->fr_win != oldwin && frp->fr_win != NULL
785 && (frp->fr_win->w_width > new_size
786 || frp->fr_win->w_width > oldwin->w_width
787 - new_size - STATUS_HEIGHT))
788 {
789 do_equal = TRUE;
790 break;
791 }
792 frp = frp->fr_next;
793 }
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796 else
797#endif
798 {
799 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /*
802 * Check if we are able to split the current window and compute its
803 * height.
804 */
805 needed = p_wmh + STATUS_HEIGHT + need_status;
806 if (flags & WSP_ROOM)
807 needed += p_wh - p_wmh;
808 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
809 {
810 available = topframe->fr_height;
811 needed += frame_minheight(topframe, NULL);
812 }
813 else
814 {
815 available = oldwin->w_height;
816 needed += p_wmh;
817 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100818 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 EMSG(_(e_noroom));
821 return FAIL;
822 }
823 oldwin_height = oldwin->w_height;
824 if (need_status)
825 {
826 oldwin->w_status_height = STATUS_HEIGHT;
827 oldwin_height -= STATUS_HEIGHT;
828 }
829 if (new_size == 0)
830 new_size = oldwin_height / 2;
831
832 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
833 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
834 if (new_size < p_wmh)
835 new_size = p_wmh;
836
837 /* if it doesn't fit in the current window, need win_equal() */
838 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
839 do_equal = TRUE;
840
841 /* We don't like to take lines for the new window from a
842 * 'winfixheight' window. Take them from a window above or below
843 * instead, if possible. */
844 if (oldwin->w_p_wfh)
845 {
846 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
847 oldwin);
848 oldwin_height = oldwin->w_height;
849 if (need_status)
850 oldwin_height -= STATUS_HEIGHT;
851 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000852
853 /* Only make all windows the same height if one of them (except oldwin)
854 * is higher than one of the split windows. */
855 if (!do_equal && p_ea && size == 0
856#ifdef FEAT_VERTSPLIT
857 && *p_ead != 'h'
858#endif
859 && oldwin->w_frame->fr_parent != NULL)
860 {
861 frp = oldwin->w_frame->fr_parent->fr_child;
862 while (frp != NULL)
863 {
864 if (frp->fr_win != oldwin && frp->fr_win != NULL
865 && (frp->fr_win->w_height > new_size
866 || frp->fr_win->w_height > oldwin_height - new_size
867 - STATUS_HEIGHT))
868 {
869 do_equal = TRUE;
870 break;
871 }
872 frp = frp->fr_next;
873 }
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876
877 /*
878 * allocate new window structure and link it in the window list
879 */
880 if ((flags & WSP_TOP) == 0
881 && ((flags & WSP_BOT)
882 || (flags & WSP_BELOW)
883 || (!(flags & WSP_ABOVE)
884 && (
885#ifdef FEAT_VERTSPLIT
886 (flags & WSP_VERT) ? p_spr :
887#endif
888 p_sb))))
889 {
890 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100891 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000892 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
894 win_append(oldwin, wp);
895 }
896 else
897 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100898 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000899 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else
901 win_append(oldwin->w_prev, wp);
902 }
903
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100904 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
906 if (wp == NULL)
907 return FAIL;
908
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000909 new_frame(wp);
910 if (wp->w_frame == NULL)
911 {
912 win_free(wp, NULL);
913 return FAIL;
914 }
915
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000916 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000917 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919
920 /*
921 * Reorganise the tree of frames to insert the new window.
922 */
923 if (flags & (WSP_TOP | WSP_BOT))
924 {
925#ifdef FEAT_VERTSPLIT
926 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
927 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
928#else
929 if (topframe->fr_layout == FR_COL)
930#endif
931 {
932 curfrp = topframe->fr_child;
933 if (flags & WSP_BOT)
934 while (curfrp->fr_next != NULL)
935 curfrp = curfrp->fr_next;
936 }
937 else
938 curfrp = topframe;
939 before = (flags & WSP_TOP);
940 }
941 else
942 {
943 curfrp = oldwin->w_frame;
944 if (flags & WSP_BELOW)
945 before = FALSE;
946 else if (flags & WSP_ABOVE)
947 before = TRUE;
948 else
949#ifdef FEAT_VERTSPLIT
950 if (flags & WSP_VERT)
951 before = !p_spr;
952 else
953#endif
954 before = !p_sb;
955 }
956 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
957 {
958 /* Need to create a new frame in the tree to make a branch. */
959 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
960 *frp = *curfrp;
961 curfrp->fr_layout = layout;
962 frp->fr_parent = curfrp;
963 frp->fr_next = NULL;
964 frp->fr_prev = NULL;
965 curfrp->fr_child = frp;
966 curfrp->fr_win = NULL;
967 curfrp = frp;
968 if (frp->fr_win != NULL)
969 oldwin->w_frame = frp;
970 else
971 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
972 frp->fr_parent = curfrp;
973 }
974
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100975 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000976 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100978 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 frp->fr_parent = curfrp->fr_parent;
980
981 /* Insert the new frame at the right place in the frame list. */
982 if (before)
983 frame_insert(curfrp, frp);
984 else
985 frame_append(curfrp, frp);
986
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100987 /* Set w_fraction now so that the cursor keeps the same relative
988 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100989 if (oldwin->w_height > 0)
990 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100991 wp->w_fraction = oldwin->w_fraction;
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_VERTSPLIT
994 if (flags & WSP_VERT)
995 {
996 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (need_status)
999 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001000 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 oldwin->w_status_height = need_status;
1002 }
1003 if (flags & (WSP_TOP | WSP_BOT))
1004 {
1005 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001006 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001007 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 wp->w_status_height = (p_ls > 0);
1009 }
1010 else
1011 {
1012 /* height and row of new window is same as current window */
1013 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001014 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 wp->w_status_height = oldwin->w_status_height;
1016 }
1017 frp->fr_height = curfrp->fr_height;
1018
1019 /* "new_size" of the current window goes to the new window, use
1020 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001021 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (before)
1023 wp->w_vsep_width = 1;
1024 else
1025 {
1026 wp->w_vsep_width = oldwin->w_vsep_width;
1027 oldwin->w_vsep_width = 1;
1028 }
1029 if (flags & (WSP_TOP | WSP_BOT))
1030 {
1031 if (flags & WSP_BOT)
1032 frame_add_vsep(curfrp);
1033 /* Set width of neighbor frame */
1034 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1036 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001039 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (before) /* new window left of current one */
1041 {
1042 wp->w_wincol = oldwin->w_wincol;
1043 oldwin->w_wincol += new_size + 1;
1044 }
1045 else /* new window right of current one */
1046 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1047 frame_fix_width(oldwin);
1048 frame_fix_width(wp);
1049 }
1050 else
1051#endif
1052 {
1053 /* width and column of new window is same as current window */
1054#ifdef FEAT_VERTSPLIT
1055 if (flags & (WSP_TOP | WSP_BOT))
1056 {
1057 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = 0;
1060 }
1061 else
1062 {
1063 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001064 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 wp->w_vsep_width = oldwin->w_vsep_width;
1066 }
1067 frp->fr_width = curfrp->fr_width;
1068#endif
1069
1070 /* "new_size" of the current window goes to the new window, use
1071 * one row for the status line */
1072 win_new_height(wp, new_size);
1073 if (flags & (WSP_TOP | WSP_BOT))
1074 frame_new_height(curfrp, curfrp->fr_height
1075 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1076 else
1077 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1078 if (before) /* new window above current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow;
1081 wp->w_status_height = STATUS_HEIGHT;
1082 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1083 }
1084 else /* new window below current one */
1085 {
1086 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1087 wp->w_status_height = oldwin->w_status_height;
1088 oldwin->w_status_height = STATUS_HEIGHT;
1089 }
1090#ifdef FEAT_VERTSPLIT
1091 if (flags & WSP_BOT)
1092 frame_add_statusline(curfrp);
1093#endif
1094 frame_fix_height(wp);
1095 frame_fix_height(oldwin);
1096 }
1097
1098 if (flags & (WSP_TOP | WSP_BOT))
1099 (void)win_comp_pos();
1100
1101 /*
1102 * Both windows need redrawing
1103 */
1104 redraw_win_later(wp, NOT_VALID);
1105 wp->w_redr_status = TRUE;
1106 redraw_win_later(oldwin, NOT_VALID);
1107 oldwin->w_redr_status = TRUE;
1108
1109 if (need_status)
1110 {
1111 msg_row = Rows - 1;
1112 msg_col = sc_col;
1113 msg_clr_eos_force(); /* Old command/ruler may still be there */
1114 comp_col();
1115 msg_row = Rows - 1;
1116 msg_col = 0; /* put position back at start of line */
1117 }
1118
1119 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001120 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
1122 if (do_equal || dir != 0)
1123 win_equal(wp, TRUE,
1124#ifdef FEAT_VERTSPLIT
1125 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1126 : dir == 'h' ? 'b' :
1127#endif
1128 'v');
1129
1130 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1131 * size was given. */
1132#ifdef FEAT_VERTSPLIT
1133 if (flags & WSP_VERT)
1134 {
1135 i = p_wiw;
1136 if (size != 0)
1137 p_wiw = size;
1138
1139# ifdef FEAT_GUI
1140 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1141 if (gui.in_use)
1142 gui_init_which_components(NULL);
1143# endif
1144 }
1145 else
1146#endif
1147 {
1148 i = p_wh;
1149 if (size != 0)
1150 p_wh = size;
1151 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001152
1153 /*
1154 * make the new window the current window
1155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 win_enter(wp, FALSE);
1157#ifdef FEAT_VERTSPLIT
1158 if (flags & WSP_VERT)
1159 p_wiw = i;
1160 else
1161#endif
1162 p_wh = i;
1163
1164 return OK;
1165}
1166
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001167
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168/*
1169 * Initialize window "newp" from window "oldp".
1170 * Used when splitting a window and when creating a new tab page.
1171 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001172 * WSP_NEWLOC may be specified in flags to prevent the location list from
1173 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174 */
1175 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001176win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001177 win_T *newp;
1178 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001179 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180{
1181 int i;
1182
1183 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001185 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001186#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001187 oldp->w_buffer->b_nwindows++;
1188 newp->w_cursor = oldp->w_cursor;
1189 newp->w_valid = 0;
1190 newp->w_curswant = oldp->w_curswant;
1191 newp->w_set_curswant = oldp->w_set_curswant;
1192 newp->w_topline = oldp->w_topline;
1193#ifdef FEAT_DIFF
1194 newp->w_topfill = oldp->w_topfill;
1195#endif
1196 newp->w_leftcol = oldp->w_leftcol;
1197 newp->w_pcmark = oldp->w_pcmark;
1198 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1199 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001200 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001201 newp->w_fraction = oldp->w_fraction;
1202 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1203#ifdef FEAT_JUMPLIST
1204 copy_jumplist(oldp, newp);
1205#endif
1206#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001207 if (flags & WSP_NEWLOC)
1208 {
1209 /* Don't copy the location list. */
1210 newp->w_llist = NULL;
1211 newp->w_llist_ref = NULL;
1212 }
1213 else
1214 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001215#endif
1216 if (oldp->w_localdir != NULL)
1217 newp->w_localdir = vim_strsave(oldp->w_localdir);
1218
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001219 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220 for (i = 0; i < oldp->w_tagstacklen; i++)
1221 {
1222 newp->w_tagstack[i] = oldp->w_tagstack[i];
1223 if (newp->w_tagstack[i].tagname != NULL)
1224 newp->w_tagstack[i].tagname =
1225 vim_strsave(newp->w_tagstack[i].tagname);
1226 }
1227 newp->w_tagstackidx = oldp->w_tagstackidx;
1228 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001230 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001231#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232
1233 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001234
Bram Moolenaara971b822011-09-14 14:43:25 +02001235#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001236 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001237#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001238}
1239
1240/*
1241 * Initialize window "newp" from window"old".
1242 * Only the essential things are copied.
1243 */
1244 static void
1245win_init_some(newp, oldp)
1246 win_T *newp;
1247 win_T *oldp;
1248{
1249 /* Use the same argument list. */
1250 newp->w_alist = oldp->w_alist;
1251 ++newp->w_alist->al_refcount;
1252 newp->w_arg_idx = oldp->w_arg_idx;
1253
1254 /* copy options from existing window */
1255 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001256}
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif /* FEAT_WINDOWS */
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#if defined(FEAT_WINDOWS) || defined(PROTO)
1261/*
1262 * Check if "win" is a pointer to an existing window.
1263 */
1264 int
1265win_valid(win)
1266 win_T *win;
1267{
1268 win_T *wp;
1269
1270 if (win == NULL)
1271 return FALSE;
1272 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1273 if (wp == win)
1274 return TRUE;
1275 return FALSE;
1276}
1277
1278/*
1279 * Return the number of windows.
1280 */
1281 int
1282win_count()
1283{
1284 win_T *wp;
1285 int count = 0;
1286
1287 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1288 ++count;
1289 return count;
1290}
1291
1292/*
1293 * Make "count" windows on the screen.
1294 * Return actual number of windows on the screen.
1295 * Must be called when there is just one window, filling the whole screen
1296 * (excluding the command line).
1297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 int
1299make_windows(count, vertical)
1300 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001301 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int maxcount;
1304 int todo;
1305
1306#ifdef FEAT_VERTSPLIT
1307 if (vertical)
1308 {
1309 /* Each windows needs at least 'winminwidth' lines and a separator
1310 * column. */
1311 maxcount = (curwin->w_width + curwin->w_vsep_width
1312 - (p_wiw - p_wmw)) / (p_wmw + 1);
1313 }
1314 else
1315#endif
1316 {
1317 /* Each window needs at least 'winminheight' lines and a status line. */
1318 maxcount = (curwin->w_height + curwin->w_status_height
1319 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1320 }
1321
1322 if (maxcount < 2)
1323 maxcount = 2;
1324 if (count > maxcount)
1325 count = maxcount;
1326
1327 /*
1328 * add status line now, otherwise first window will be too big
1329 */
1330 if (count > 1)
1331 last_status(TRUE);
1332
1333#ifdef FEAT_AUTOCMD
1334 /*
1335 * Don't execute autocommands while creating the windows. Must do that
1336 * when putting the buffers in the windows.
1337 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001338 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#endif
1340
1341 /* todo is number of windows left to create */
1342 for (todo = count - 1; todo > 0; --todo)
1343#ifdef FEAT_VERTSPLIT
1344 if (vertical)
1345 {
1346 if (win_split(curwin->w_width - (curwin->w_width - todo)
1347 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1348 break;
1349 }
1350 else
1351#endif
1352 {
1353 if (win_split(curwin->w_height - (curwin->w_height - todo
1354 * STATUS_HEIGHT) / (todo + 1)
1355 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1356 break;
1357 }
1358
1359#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001360 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#endif
1362
1363 /* return actual number of windows */
1364 return (count - todo);
1365}
1366
1367/*
1368 * Exchange current and next window
1369 */
1370 static void
1371win_exchange(Prenum)
1372 long Prenum;
1373{
1374 frame_T *frp;
1375 frame_T *frp2;
1376 win_T *wp;
1377 win_T *wp2;
1378 int temp;
1379
1380 if (lastwin == firstwin) /* just one window */
1381 {
1382 beep_flush();
1383 return;
1384 }
1385
1386#ifdef FEAT_GUI
1387 need_mouse_correct = TRUE;
1388#endif
1389
1390 /*
1391 * find window to exchange with
1392 */
1393 if (Prenum)
1394 {
1395 frp = curwin->w_frame->fr_parent->fr_child;
1396 while (frp != NULL && --Prenum > 0)
1397 frp = frp->fr_next;
1398 }
1399 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1400 frp = curwin->w_frame->fr_next;
1401 else /* Swap last window in row/col with previous */
1402 frp = curwin->w_frame->fr_prev;
1403
1404 /* We can only exchange a window with another window, not with a frame
1405 * containing windows. */
1406 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1407 return;
1408 wp = frp->fr_win;
1409
1410/*
1411 * 1. remove curwin from the list. Remember after which window it was in wp2
1412 * 2. insert curwin before wp in the list
1413 * if wp != wp2
1414 * 3. remove wp from the list
1415 * 4. insert wp after wp2
1416 * 5. exchange the status line height and vsep width.
1417 */
1418 wp2 = curwin->w_prev;
1419 frp2 = curwin->w_frame->fr_prev;
1420 if (wp->w_prev != curwin)
1421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 frame_remove(curwin->w_frame);
1424 win_append(wp->w_prev, curwin);
1425 frame_insert(frp, curwin->w_frame);
1426 }
1427 if (wp != wp2)
1428 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001429 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 frame_remove(wp->w_frame);
1431 win_append(wp2, wp);
1432 if (frp2 == NULL)
1433 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1434 else
1435 frame_append(frp2, wp->w_frame);
1436 }
1437 temp = curwin->w_status_height;
1438 curwin->w_status_height = wp->w_status_height;
1439 wp->w_status_height = temp;
1440#ifdef FEAT_VERTSPLIT
1441 temp = curwin->w_vsep_width;
1442 curwin->w_vsep_width = wp->w_vsep_width;
1443 wp->w_vsep_width = temp;
1444
1445 /* If the windows are not in the same frame, exchange the sizes to avoid
1446 * messing up the window layout. Otherwise fix the frame sizes. */
1447 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1448 {
1449 temp = curwin->w_height;
1450 curwin->w_height = wp->w_height;
1451 wp->w_height = temp;
1452 temp = curwin->w_width;
1453 curwin->w_width = wp->w_width;
1454 wp->w_width = temp;
1455 }
1456 else
1457 {
1458 frame_fix_height(curwin);
1459 frame_fix_height(wp);
1460 frame_fix_width(curwin);
1461 frame_fix_width(wp);
1462 }
1463#endif
1464
1465 (void)win_comp_pos(); /* recompute window positions */
1466
1467 win_enter(wp, TRUE);
1468 redraw_later(CLEAR);
1469}
1470
1471/*
1472 * rotate windows: if upwards TRUE the second window becomes the first one
1473 * if upwards FALSE the first window becomes the second one
1474 */
1475 static void
1476win_rotate(upwards, count)
1477 int upwards;
1478 int count;
1479{
1480 win_T *wp1;
1481 win_T *wp2;
1482 frame_T *frp;
1483 int n;
1484
1485 if (firstwin == lastwin) /* nothing to do */
1486 {
1487 beep_flush();
1488 return;
1489 }
1490
1491#ifdef FEAT_GUI
1492 need_mouse_correct = TRUE;
1493#endif
1494
1495#ifdef FEAT_VERTSPLIT
1496 /* Check if all frames in this row/col have one window. */
1497 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1498 frp = frp->fr_next)
1499 if (frp->fr_win == NULL)
1500 {
1501 EMSG(_("E443: Cannot rotate when another window is split"));
1502 return;
1503 }
1504#endif
1505
1506 while (count--)
1507 {
1508 if (upwards) /* first window becomes last window */
1509 {
1510 /* remove first window/frame from the list */
1511 frp = curwin->w_frame->fr_parent->fr_child;
1512 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001513 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 frame_remove(frp);
1515
1516 /* find last frame and append removed window/frame after it */
1517 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1518 ;
1519 win_append(frp->fr_win, wp1);
1520 frame_append(frp, wp1->w_frame);
1521
1522 wp2 = frp->fr_win; /* previously last window */
1523 }
1524 else /* last window becomes first window */
1525 {
1526 /* find last window/frame in the list and remove it */
1527 for (frp = curwin->w_frame; frp->fr_next != NULL;
1528 frp = frp->fr_next)
1529 ;
1530 wp1 = frp->fr_win;
1531 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001532 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 frame_remove(frp);
1534
1535 /* append the removed window/frame before the first in the list */
1536 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1537 frame_insert(frp->fr_parent->fr_child, frp);
1538 }
1539
1540 /* exchange status height and vsep width of old and new last window */
1541 n = wp2->w_status_height;
1542 wp2->w_status_height = wp1->w_status_height;
1543 wp1->w_status_height = n;
1544 frame_fix_height(wp1);
1545 frame_fix_height(wp2);
1546#ifdef FEAT_VERTSPLIT
1547 n = wp2->w_vsep_width;
1548 wp2->w_vsep_width = wp1->w_vsep_width;
1549 wp1->w_vsep_width = n;
1550 frame_fix_width(wp1);
1551 frame_fix_width(wp2);
1552#endif
1553
1554 /* recompute w_winrow and w_wincol for all windows */
1555 (void)win_comp_pos();
1556 }
1557
1558 redraw_later(CLEAR);
1559}
1560
1561/*
1562 * Move the current window to the very top/bottom/left/right of the screen.
1563 */
1564 static void
1565win_totop(size, flags)
1566 int size;
1567 int flags;
1568{
1569 int dir;
1570 int height = curwin->w_height;
1571
1572 if (lastwin == firstwin)
1573 {
1574 beep_flush();
1575 return;
1576 }
1577
1578 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001579 (void)winframe_remove(curwin, &dir, NULL);
1580 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 last_status(FALSE); /* may need to remove last status line */
1582 (void)win_comp_pos(); /* recompute window positions */
1583
1584 /* Split a window on the desired side and put the window there. */
1585 (void)win_split_ins(size, flags, curwin, dir);
1586 if (!(flags & WSP_VERT))
1587 {
1588 win_setheight(height);
1589 if (p_ea)
1590 win_equal(curwin, TRUE, 'v');
1591 }
1592
1593#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1594 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1595 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001596 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598}
1599
1600/*
1601 * Move window "win1" to below/right of "win2" and make "win1" the current
1602 * window. Only works within the same frame!
1603 */
1604 void
1605win_move_after(win1, win2)
1606 win_T *win1, *win2;
1607{
1608 int height;
1609
1610 /* check if the arguments are reasonable */
1611 if (win1 == win2)
1612 return;
1613
1614 /* check if there is something to do */
1615 if (win2->w_next != win1)
1616 {
1617 /* may need move the status line/vertical separator of the last window
1618 * */
1619 if (win1 == lastwin)
1620 {
1621 height = win1->w_prev->w_status_height;
1622 win1->w_prev->w_status_height = win1->w_status_height;
1623 win1->w_status_height = height;
1624#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001625 if (win1->w_prev->w_vsep_width == 1)
1626 {
1627 /* Remove the vertical separator from the last-but-one window,
1628 * add it to the last window. Adjust the frame widths. */
1629 win1->w_prev->w_vsep_width = 0;
1630 win1->w_prev->w_frame->fr_width -= 1;
1631 win1->w_vsep_width = 1;
1632 win1->w_frame->fr_width += 1;
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 }
1636 else if (win2 == lastwin)
1637 {
1638 height = win1->w_status_height;
1639 win1->w_status_height = win2->w_status_height;
1640 win2->w_status_height = height;
1641#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001642 if (win1->w_vsep_width == 1)
1643 {
1644 /* Remove the vertical separator from win1, add it to the last
1645 * window, win2. Adjust the frame widths. */
1646 win2->w_vsep_width = 1;
1647 win2->w_frame->fr_width += 1;
1648 win1->w_vsep_width = 0;
1649 win1->w_frame->fr_width -= 1;
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
1652 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001653 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 frame_remove(win1->w_frame);
1655 win_append(win2, win1);
1656 frame_append(win2->w_frame, win1->w_frame);
1657
1658 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1659 redraw_later(NOT_VALID);
1660 }
1661 win_enter(win1, FALSE);
1662}
1663
1664/*
1665 * Make all windows the same height.
1666 * 'next_curwin' will soon be the current window, make sure it has enough
1667 * rows.
1668 */
1669 void
1670win_equal(next_curwin, current, dir)
1671 win_T *next_curwin; /* pointer to current window to be or NULL */
1672 int current; /* do only frame with current window */
1673 int dir; /* 'v' for vertically, 'h' for horizontally,
1674 'b' for both, 0 for using p_ead */
1675{
1676 if (dir == 0)
1677#ifdef FEAT_VERTSPLIT
1678 dir = *p_ead;
1679#else
1680 dir = 'b';
1681#endif
1682 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001683 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001684 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685}
1686
1687/*
1688 * Set a frame to a new position and height, spreading the available room
1689 * equally over contained frames.
1690 * The window "next_curwin" (if not NULL) should at least get the size from
1691 * 'winheight' and 'winwidth' if possible.
1692 */
1693 static void
1694win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1695 win_T *next_curwin; /* pointer to current window to be or NULL */
1696 int current; /* do only frame with current window */
1697 frame_T *topfr; /* frame to set size off */
1698 int dir; /* 'v', 'h' or 'b', see win_equal() */
1699 int col; /* horizontal position for frame */
1700 int row; /* vertical position for frame */
1701 int width; /* new width of frame */
1702 int height; /* new height of frame */
1703{
1704 int n, m;
1705 int extra_sep = 0;
1706 int wincount, totwincount = 0;
1707 frame_T *fr;
1708 int next_curwin_size = 0;
1709 int room = 0;
1710 int new_size;
1711 int has_next_curwin = 0;
1712 int hnc;
1713
1714 if (topfr->fr_layout == FR_LEAF)
1715 {
1716 /* Set the width/height of this frame.
1717 * Redraw when size or position changes */
1718 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1719#ifdef FEAT_VERTSPLIT
1720 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1721#endif
1722 )
1723 {
1724 topfr->fr_win->w_winrow = row;
1725 frame_new_height(topfr, height, FALSE, FALSE);
1726#ifdef FEAT_VERTSPLIT
1727 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#endif
1730 redraw_all_later(CLEAR);
1731 }
1732 }
1733#ifdef FEAT_VERTSPLIT
1734 else if (topfr->fr_layout == FR_ROW)
1735 {
1736 topfr->fr_width = width;
1737 topfr->fr_height = height;
1738
1739 if (dir != 'v') /* equalize frame widths */
1740 {
1741 /* Compute the maximum number of windows horizontally in this
1742 * frame. */
1743 n = frame_minwidth(topfr, NOWIN);
1744 /* add one for the rightmost window, it doesn't have a separator */
1745 if (col + width == Columns)
1746 extra_sep = 1;
1747 else
1748 extra_sep = 0;
1749 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001750 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 /*
1753 * Compute width for "next_curwin" window and room available for
1754 * other windows.
1755 * "m" is the minimal width when counting p_wiw for "next_curwin".
1756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 m = frame_minwidth(topfr, next_curwin);
1758 room = width - m;
1759 if (room < 0)
1760 {
1761 next_curwin_size = p_wiw + room;
1762 room = 0;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 else
1765 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001766 next_curwin_size = -1;
1767 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1768 {
1769 /* If 'winfixwidth' set keep the window width if
1770 * possible.
1771 * Watch out for this window being the next_curwin. */
1772 if (frame_fixed_width(fr))
1773 {
1774 n = frame_minwidth(fr, NOWIN);
1775 new_size = fr->fr_width;
1776 if (frame_has_win(fr, next_curwin))
1777 {
1778 room += p_wiw - p_wmw;
1779 next_curwin_size = 0;
1780 if (new_size < p_wiw)
1781 new_size = p_wiw;
1782 }
1783 else
1784 /* These windows don't use up room. */
1785 totwincount -= (n + (fr->fr_next == NULL
1786 ? extra_sep : 0)) / (p_wmw + 1);
1787 room -= new_size - n;
1788 if (room < 0)
1789 {
1790 new_size += room;
1791 room = 0;
1792 }
1793 fr->fr_newwidth = new_size;
1794 }
1795 }
1796 if (next_curwin_size == -1)
1797 {
1798 if (!has_next_curwin)
1799 next_curwin_size = 0;
1800 else if (totwincount > 1
1801 && (room + (totwincount - 2))
1802 / (totwincount - 1) > p_wiw)
1803 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001804 /* Can make all windows wider than 'winwidth', spread
1805 * the room equally. */
1806 next_curwin_size = (room + p_wiw
1807 + (totwincount - 1) * p_wmw
1808 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809 room -= next_curwin_size - p_wiw;
1810 }
1811 else
1812 next_curwin_size = p_wiw;
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001815
1816 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 --totwincount; /* don't count curwin */
1818 }
1819
1820 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1821 {
1822 n = m = 0;
1823 wincount = 1;
1824 if (fr->fr_next == NULL)
1825 /* last frame gets all that remains (avoid roundoff error) */
1826 new_size = width;
1827 else if (dir == 'v')
1828 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001829 else if (frame_fixed_width(fr))
1830 {
1831 new_size = fr->fr_newwidth;
1832 wincount = 0; /* doesn't count as a sizeable window */
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 else
1835 {
1836 /* Compute the maximum number of windows horiz. in "fr". */
1837 n = frame_minwidth(fr, NOWIN);
1838 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1839 / (p_wmw + 1);
1840 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (has_next_curwin)
1842 hnc = frame_has_win(fr, next_curwin);
1843 else
1844 hnc = FALSE;
1845 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001847 if (totwincount == 0)
1848 new_size = room;
1849 else
1850 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
1854 next_curwin_size -= p_wiw - (m - n);
1855 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001858 else
1859 room -= new_size;
1860 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 }
1862
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 * window, unless equalizing all frames. */
1865 if (!current || dir != 'v' || topfr->fr_parent != NULL
1866 || (new_size != fr->fr_width)
1867 || frame_has_win(fr, next_curwin))
1868 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001869 new_size, height);
1870 col += new_size;
1871 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 totwincount -= wincount;
1873 }
1874 }
1875#endif
1876 else /* topfr->fr_layout == FR_COL */
1877 {
1878#ifdef FEAT_VERTSPLIT
1879 topfr->fr_width = width;
1880#endif
1881 topfr->fr_height = height;
1882
1883 if (dir != 'h') /* equalize frame heights */
1884 {
1885 /* Compute maximum number of windows vertically in this frame. */
1886 n = frame_minheight(topfr, NOWIN);
1887 /* add one for the bottom window if it doesn't have a statusline */
1888 if (row + height == cmdline_row && p_ls == 0)
1889 extra_sep = 1;
1890 else
1891 extra_sep = 0;
1892 totwincount = (n + extra_sep) / (p_wmh + 1);
1893 has_next_curwin = frame_has_win(topfr, next_curwin);
1894
1895 /*
1896 * Compute height for "next_curwin" window and room available for
1897 * other windows.
1898 * "m" is the minimal height when counting p_wh for "next_curwin".
1899 */
1900 m = frame_minheight(topfr, next_curwin);
1901 room = height - m;
1902 if (room < 0)
1903 {
1904 /* The room is less then 'winheight', use all space for the
1905 * current window. */
1906 next_curwin_size = p_wh + room;
1907 room = 0;
1908 }
1909 else
1910 {
1911 next_curwin_size = -1;
1912 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1913 {
1914 /* If 'winfixheight' set keep the window height if
1915 * possible.
1916 * Watch out for this window being the next_curwin. */
1917 if (frame_fixed_height(fr))
1918 {
1919 n = frame_minheight(fr, NOWIN);
1920 new_size = fr->fr_height;
1921 if (frame_has_win(fr, next_curwin))
1922 {
1923 room += p_wh - p_wmh;
1924 next_curwin_size = 0;
1925 if (new_size < p_wh)
1926 new_size = p_wh;
1927 }
1928 else
1929 /* These windows don't use up room. */
1930 totwincount -= (n + (fr->fr_next == NULL
1931 ? extra_sep : 0)) / (p_wmh + 1);
1932 room -= new_size - n;
1933 if (room < 0)
1934 {
1935 new_size += room;
1936 room = 0;
1937 }
1938 fr->fr_newheight = new_size;
1939 }
1940 }
1941 if (next_curwin_size == -1)
1942 {
1943 if (!has_next_curwin)
1944 next_curwin_size = 0;
1945 else if (totwincount > 1
1946 && (room + (totwincount - 2))
1947 / (totwincount - 1) > p_wh)
1948 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001949 /* can make all windows higher than 'winheight',
1950 * spread the room equally. */
1951 next_curwin_size = (room + p_wh
1952 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 + (totwincount - 1)) / totwincount;
1954 room -= next_curwin_size - p_wh;
1955 }
1956 else
1957 next_curwin_size = p_wh;
1958 }
1959 }
1960
1961 if (has_next_curwin)
1962 --totwincount; /* don't count curwin */
1963 }
1964
1965 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1966 {
1967 n = m = 0;
1968 wincount = 1;
1969 if (fr->fr_next == NULL)
1970 /* last frame gets all that remains (avoid roundoff error) */
1971 new_size = height;
1972 else if (dir == 'h')
1973 new_size = fr->fr_height;
1974 else if (frame_fixed_height(fr))
1975 {
1976 new_size = fr->fr_newheight;
1977 wincount = 0; /* doesn't count as a sizeable window */
1978 }
1979 else
1980 {
1981 /* Compute the maximum number of windows vert. in "fr". */
1982 n = frame_minheight(fr, NOWIN);
1983 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1984 / (p_wmh + 1);
1985 m = frame_minheight(fr, next_curwin);
1986 if (has_next_curwin)
1987 hnc = frame_has_win(fr, next_curwin);
1988 else
1989 hnc = FALSE;
1990 if (hnc) /* don't count next_curwin */
1991 --wincount;
1992 if (totwincount == 0)
1993 new_size = room;
1994 else
1995 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1996 / totwincount;
1997 if (hnc) /* add next_curwin size */
1998 {
1999 next_curwin_size -= p_wh - (m - n);
2000 new_size += next_curwin_size;
2001 room -= new_size - next_curwin_size;
2002 }
2003 else
2004 room -= new_size;
2005 new_size += n;
2006 }
2007 /* Skip frame that is full width when splitting or closing a
2008 * window, unless equalizing all frames. */
2009 if (!current || dir != 'h' || topfr->fr_parent != NULL
2010 || (new_size != fr->fr_height)
2011 || frame_has_win(fr, next_curwin))
2012 win_equal_rec(next_curwin, current, fr, dir, col, row,
2013 width, new_size);
2014 row += new_size;
2015 height -= new_size;
2016 totwincount -= wincount;
2017 }
2018 }
2019}
2020
2021/*
2022 * close all windows for buffer 'buf'
2023 */
2024 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002029 win_T *wp;
2030 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002031 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034
2035 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002037 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2038#ifdef FEAT_AUTOCMD
2039 && !(wp->w_closing || wp->w_buffer->b_closing)
2040#endif
2041 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002043 win_close(wp, FALSE);
2044
2045 /* Start all over, autocommands may change the window layout. */
2046 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002051
2052 /* Also check windows in other tab pages. */
2053 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2054 {
2055 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002056 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002057 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002058 if (wp->w_buffer == buf
2059#ifdef FEAT_AUTOCMD
2060 && !(wp->w_closing || wp->w_buffer->b_closing)
2061#endif
2062 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002063 {
2064 win_close_othertab(wp, FALSE, tp);
2065
2066 /* Start all over, the tab page may be closed and
2067 * autocommands may change the window layout. */
2068 nexttp = first_tabpage;
2069 break;
2070 }
2071 }
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002074
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002075 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002076 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077}
2078
2079/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002080 * Return TRUE if the current window is the only window that exists (ignoring
2081 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002082 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002083 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002084 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002085last_window()
2086{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002087 return (one_window() && first_tabpage->tp_next == NULL);
2088}
2089
2090/*
2091 * Return TRUE if there is only one window other than "aucmd_win" in the
2092 * current tab page.
2093 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002094 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002095one_window()
2096{
2097#ifdef FEAT_AUTOCMD
2098 win_T *wp;
2099 int seen_one = FALSE;
2100
2101 FOR_ALL_WINDOWS(wp)
2102 {
2103 if (wp != aucmd_win)
2104 {
2105 if (seen_one)
2106 return FALSE;
2107 seen_one = TRUE;
2108 }
2109 }
2110 return TRUE;
2111#else
2112 return firstwin == lastwin;
2113#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002114}
2115
2116/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002117 * Close the possibly last window in a tab page.
2118 * Returns TRUE when the window was closed already.
2119 */
2120 static int
2121close_last_window_tabpage(win, free_buf, prev_curtab)
2122 win_T *win;
2123 int free_buf;
2124 tabpage_T *prev_curtab;
2125{
2126 if (firstwin == lastwin)
2127 {
2128 /*
2129 * Closing the last window in a tab page. First go to another tab
2130 * page and then close the window and the tab page. This avoids that
2131 * curwin and curtab are invalid while we are freeing memory, they may
2132 * be used in GUI events.
2133 */
2134 goto_tabpage_tp(alt_tabpage());
2135 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 }
2147 return TRUE;
2148 }
2149 return FALSE;
2150}
2151
2152/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * If "free_buf" is TRUE related buffer may be unloaded.
2155 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002156 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
2158 void
2159win_close(win, free_buf)
2160 win_T *win;
2161 int free_buf;
2162{
2163 win_T *wp;
2164#ifdef FEAT_AUTOCMD
2165 int other_buffer = FALSE;
2166#endif
2167 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 int dir;
2169 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002170 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002172 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 EMSG(_("E444: Cannot close last window"));
2175 return;
2176 }
2177
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002178#ifdef FEAT_AUTOCMD
Bram Moolenaar362ce482012-06-06 19:02:45 +02002179 if (win->w_closing || win->w_buffer->b_closing)
2180 return; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002181 if (win == aucmd_win)
2182 {
2183 EMSG(_("E813: Cannot close autocmd window"));
2184 return;
2185 }
2186 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2187 {
2188 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2189 return;
2190 }
2191#endif
2192
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002193 /* When closing the last window in a tab page first go to another tab page
2194 * and then close the window and the tab page to avoid that curwin and
2195 * curtab are invalid while we are freeing memory. */
2196 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2197 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 /* When closing the help window, try restoring a snapshot after closing
2200 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002201 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 help_window = TRUE;
2203 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002204 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
2206#ifdef FEAT_AUTOCMD
2207 if (win == curwin)
2208 {
2209 /*
2210 * Guess which window is going to be the new current window.
2211 * This may change because of the autocommands (sigh).
2212 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002213 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002216 * Be careful: If autocommands delete the window or cause this window
2217 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 */
2219 if (wp->w_buffer != curbuf)
2220 {
2221 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002222 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002224 if (!win_valid(win))
2225 return;
2226 win->w_closing = FALSE;
2227 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 return;
2229 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002230 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002232 if (!win_valid(win))
2233 return;
2234 win->w_closing = FALSE;
2235 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return;
2237# ifdef FEAT_EVAL
2238 /* autocmds may abort script processing */
2239 if (aborting())
2240 return;
2241# endif
2242 }
2243#endif
2244
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002245#ifdef FEAT_GUI
2246 /* Avoid trouble with scrollbars that are going to be deleted in
2247 * win_free(). */
2248 if (gui.in_use)
2249 out_flush();
2250#endif
2251
Bram Moolenaara971b822011-09-14 14:43:25 +02002252#ifdef FEAT_SYN_HL
2253 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002254 if (win->w_buffer != NULL)
2255 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002256#endif
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 /*
2259 * Close the link to the buffer.
2260 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002261 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002262 {
2263#ifdef FEAT_AUTOCMD
2264 win->w_closing = TRUE;
2265#endif
2266 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
2267#ifdef FEAT_AUTOCMD
2268 if (win_valid(win))
2269 win->w_closing = FALSE;
2270#endif
2271 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002274 * other window or moved to another tab page. */
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002275 if (!win_valid(win) || last_window() || curtab != prev_curtab
2276 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return;
2278
Bram Moolenaara971b822011-09-14 14:43:25 +02002279 /* Free the memory used for the window and get the window that received
2280 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002281 wp = win_free_mem(win, &dir, NULL);
2282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /* Make sure curwin isn't invalid. It can cause severe trouble when
2284 * printing an error message. For win_equal() curbuf needs to be valid
2285 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002286 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 curwin = wp;
2289#ifdef FEAT_QUICKFIX
2290 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2291 {
2292 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002293 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 * finding another window to go to.
2295 */
2296 for (;;)
2297 {
2298 if (wp->w_next == NULL)
2299 wp = firstwin;
2300 else
2301 wp = wp->w_next;
2302 if (wp == curwin)
2303 break;
2304 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2305 {
2306 curwin = wp;
2307 break;
2308 }
2309 }
2310 }
2311#endif
2312 curbuf = curwin->w_buffer;
2313 close_curwin = TRUE;
2314 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002315 if (p_ea
2316#ifdef FEAT_VERTSPLIT
2317 && (*p_ead == 'b' || *p_ead == dir)
2318#endif
2319 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 win_equal(curwin, TRUE,
2321#ifdef FEAT_VERTSPLIT
2322 dir
2323#else
2324 0
2325#endif
2326 );
2327 else
2328 win_comp_pos();
2329 if (close_curwin)
2330 {
2331 win_enter_ext(wp, FALSE, TRUE);
2332#ifdef FEAT_AUTOCMD
2333 if (other_buffer)
2334 /* careful: after this wp and win may be invalid! */
2335 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2336#endif
2337 }
2338
2339 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002340 * If last window has a status line now and we don't want one,
2341 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 */
2343 last_status(FALSE);
2344
2345 /* After closing the help window, try restoring the window layout from
2346 * before it was opened. */
2347 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002348 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2351 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2352 if (gui.in_use && !win_hasvertsplit())
2353 gui_init_which_components(NULL);
2354#endif
2355
2356 redraw_all_later(NOT_VALID);
2357}
2358
2359/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002360 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002361 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002362 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002363 * Caller must check if buffer is hidden and whether the tabline needs to be
2364 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 */
2366 void
2367win_close_othertab(win, free_buf, tp)
2368 win_T *win;
2369 int free_buf;
2370 tabpage_T *tp;
2371{
2372 win_T *wp;
2373 int dir;
2374 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002375 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002376
Bram Moolenaar362ce482012-06-06 19:02:45 +02002377#ifdef FEAT_AUTOCMD
2378 if (win->w_closing || win->w_buffer->b_closing)
2379 return; /* window is already being closed */
2380#endif
2381
Bram Moolenaarf740b292006-02-16 22:11:02 +00002382 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002383 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384
2385 /* Careful: Autocommands may have closed the tab page or made it the
2386 * current tab page. */
2387 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2388 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002389 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002390 return;
2391
2392 /* Autocommands may have closed the window already. */
2393 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2394 ;
2395 if (wp == NULL)
2396 return;
2397
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002399 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002400 {
2401 if (tp == first_tabpage)
2402 first_tabpage = tp->tp_next;
2403 else
2404 {
2405 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2406 ptp = ptp->tp_next)
2407 ;
2408 if (ptp == NULL)
2409 {
2410 EMSG2(_(e_intern2), "win_close_othertab()");
2411 return;
2412 }
2413 ptp->tp_next = tp->tp_next;
2414 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002415 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002416 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002417
2418 /* Free the memory used for the window. */
2419 win_free_mem(win, &dir, tp);
2420
2421 if (free_tp)
2422 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002423}
2424
2425/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002426 * Free the memory used for a window.
2427 * Returns a pointer to the window that got the freed up space.
2428 */
2429 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002430win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002431 win_T *win;
2432 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002433 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002434{
2435 frame_T *frp;
2436 win_T *wp;
2437
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002438 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002439 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002443
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002444 /* When deleting the current window of another tab page select a new
2445 * current window. */
2446 if (tp != NULL && win == tp->tp_curwin)
2447 tp->tp_curwin = wp;
2448
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002449 return wp;
2450}
2451
2452#if defined(EXITFREE) || defined(PROTO)
2453 void
2454win_free_all()
2455{
2456 int dummy;
2457
Bram Moolenaarf740b292006-02-16 22:11:02 +00002458# ifdef FEAT_WINDOWS
2459 while (first_tabpage->tp_next != NULL)
2460 tabpage_close(TRUE);
2461# endif
2462
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002463# ifdef FEAT_AUTOCMD
2464 if (aucmd_win != NULL)
2465 {
2466 (void)win_free_mem(aucmd_win, &dummy, NULL);
2467 aucmd_win = NULL;
2468 }
2469# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002470
2471 while (firstwin != NULL)
2472 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002473}
2474#endif
2475
2476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 * Remove a window and its frame from the tree of frames.
2478 * Returns a pointer to the window that got the freed up space.
2479 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002480 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002481winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002483 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002484 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 frame_T *frp, *frp2, *frp3;
2487 frame_T *frp_close = win->w_frame;
2488 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002491 * If there is only one window there is nothing to remove.
2492 */
2493 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2494 return NULL;
2495
2496 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 * Remove the window from its frame.
2498 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002499 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 wp = frame2win(frp2);
2501
2502 /* Remove this frame from the list of frames. */
2503 frame_remove(frp_close);
2504
2505#ifdef FEAT_VERTSPLIT
2506 if (frp_close->fr_parent->fr_layout == FR_COL)
2507 {
2508#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002509 /* When 'winfixheight' is set, try to find another frame in the column
2510 * (as close to the closed frame as possible) to distribute the height
2511 * to. */
2512 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2513 {
2514 frp = frp_close->fr_prev;
2515 frp3 = frp_close->fr_next;
2516 while (frp != NULL || frp3 != NULL)
2517 {
2518 if (frp != NULL)
2519 {
2520 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2521 {
2522 frp2 = frp;
2523 wp = frp->fr_win;
2524 break;
2525 }
2526 frp = frp->fr_prev;
2527 }
2528 if (frp3 != NULL)
2529 {
2530 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2531 {
2532 frp2 = frp3;
2533 wp = frp3->fr_win;
2534 break;
2535 }
2536 frp3 = frp3->fr_next;
2537 }
2538 }
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2541 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542#ifdef FEAT_VERTSPLIT
2543 *dirp = 'v';
2544 }
2545 else
2546 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002547 /* When 'winfixwidth' is set, try to find another frame in the column
2548 * (as close to the closed frame as possible) to distribute the width
2549 * to. */
2550 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2551 {
2552 frp = frp_close->fr_prev;
2553 frp3 = frp_close->fr_next;
2554 while (frp != NULL || frp3 != NULL)
2555 {
2556 if (frp != NULL)
2557 {
2558 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2559 {
2560 frp2 = frp;
2561 wp = frp->fr_win;
2562 break;
2563 }
2564 frp = frp->fr_prev;
2565 }
2566 if (frp3 != NULL)
2567 {
2568 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2569 {
2570 frp2 = frp3;
2571 wp = frp3->fr_win;
2572 break;
2573 }
2574 frp3 = frp3->fr_next;
2575 }
2576 }
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002579 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 *dirp = 'h';
2581 }
2582#endif
2583
2584 /* If rows/columns go to a window below/right its positions need to be
2585 * updated. Can only be done after the sizes have been updated. */
2586 if (frp2 == frp_close->fr_next)
2587 {
2588 int row = win->w_winrow;
2589 int col = W_WINCOL(win);
2590
2591 frame_comp_pos(frp2, &row, &col);
2592 }
2593
2594 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2595 {
2596 /* There is no other frame in this list, move its info to the parent
2597 * and remove it. */
2598 frp2->fr_parent->fr_layout = frp2->fr_layout;
2599 frp2->fr_parent->fr_child = frp2->fr_child;
2600 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2601 frp->fr_parent = frp2->fr_parent;
2602 frp2->fr_parent->fr_win = frp2->fr_win;
2603 if (frp2->fr_win != NULL)
2604 frp2->fr_win->w_frame = frp2->fr_parent;
2605 frp = frp2->fr_parent;
2606 vim_free(frp2);
2607
2608 frp2 = frp->fr_parent;
2609 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2610 {
2611 /* The frame above the parent has the same layout, have to merge
2612 * the frames into this list. */
2613 if (frp2->fr_child == frp)
2614 frp2->fr_child = frp->fr_child;
2615 frp->fr_child->fr_prev = frp->fr_prev;
2616 if (frp->fr_prev != NULL)
2617 frp->fr_prev->fr_next = frp->fr_child;
2618 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2619 {
2620 frp3->fr_parent = frp2;
2621 if (frp3->fr_next == NULL)
2622 {
2623 frp3->fr_next = frp->fr_next;
2624 if (frp->fr_next != NULL)
2625 frp->fr_next->fr_prev = frp3;
2626 break;
2627 }
2628 }
2629 vim_free(frp);
2630 }
2631 }
2632
2633 return wp;
2634}
2635
2636/*
2637 * Find out which frame is going to get the freed up space when "win" is
2638 * closed.
2639 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2640 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2641 * This makes opening a window and closing it immediately keep the same window
2642 * layout.
2643 */
2644 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002645win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 frame_T *frp;
2650 int b;
2651
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002653 /* Last window in this tab page, will go to next tab page. */
2654 return alt_tabpage()->tp_curwin->w_frame;
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 frp = win->w_frame;
2657#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002658 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 b = p_spr;
2660 else
2661#endif
2662 b = p_sb;
2663 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2664 return frp->fr_next;
2665 return frp->fr_prev;
2666}
2667
2668/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002669 * Return the tabpage that will be used if the current one is closed.
2670 */
2671 static tabpage_T *
2672alt_tabpage()
2673{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002674 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002675
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002676 /* Use the next tab page if possible. */
2677 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002678 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002679
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002680 /* Find the last but one tab page. */
2681 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2682 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002683 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002684}
2685
2686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 * Find the left-upper window in frame "frp".
2688 */
2689 static win_T *
2690frame2win(frp)
2691 frame_T *frp;
2692{
2693 while (frp->fr_win == NULL)
2694 frp = frp->fr_child;
2695 return frp->fr_win;
2696}
2697
2698/*
2699 * Return TRUE if frame "frp" contains window "wp".
2700 */
2701 static int
2702frame_has_win(frp, wp)
2703 frame_T *frp;
2704 win_T *wp;
2705{
2706 frame_T *p;
2707
2708 if (frp->fr_layout == FR_LEAF)
2709 return frp->fr_win == wp;
2710
2711 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2712 if (frame_has_win(p, wp))
2713 return TRUE;
2714 return FALSE;
2715}
2716
2717/*
2718 * Set a new height for a frame. Recursively sets the height for contained
2719 * frames and windows. Caller must take care of positions.
2720 */
2721 static void
2722frame_new_height(topfrp, height, topfirst, wfh)
2723 frame_T *topfrp;
2724 int height;
2725 int topfirst; /* resize topmost contained frame first */
2726 int wfh; /* obey 'winfixheight' when there is a choice;
2727 may cause the height not to be set */
2728{
2729 frame_T *frp;
2730 int extra_lines;
2731 int h;
2732
2733 if (topfrp->fr_win != NULL)
2734 {
2735 /* Simple case: just one window. */
2736 win_new_height(topfrp->fr_win,
2737 height - topfrp->fr_win->w_status_height);
2738 }
2739#ifdef FEAT_VERTSPLIT
2740 else if (topfrp->fr_layout == FR_ROW)
2741 {
2742 do
2743 {
2744 /* All frames in this row get the same new height. */
2745 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2746 {
2747 frame_new_height(frp, height, topfirst, wfh);
2748 if (frp->fr_height > height)
2749 {
2750 /* Could not fit the windows, make the whole row higher. */
2751 height = frp->fr_height;
2752 break;
2753 }
2754 }
2755 }
2756 while (frp != NULL);
2757 }
2758#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002759 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 /* Complicated case: Resize a column of frames. Resize the bottom
2762 * frame first, frames above that when needed. */
2763
2764 frp = topfrp->fr_child;
2765 if (wfh)
2766 /* Advance past frames with one window with 'wfh' set. */
2767 while (frame_fixed_height(frp))
2768 {
2769 frp = frp->fr_next;
2770 if (frp == NULL)
2771 return; /* no frame without 'wfh', give up */
2772 }
2773 if (!topfirst)
2774 {
2775 /* Find the bottom frame of this column */
2776 while (frp->fr_next != NULL)
2777 frp = frp->fr_next;
2778 if (wfh)
2779 /* Advance back for frames with one window with 'wfh' set. */
2780 while (frame_fixed_height(frp))
2781 frp = frp->fr_prev;
2782 }
2783
2784 extra_lines = height - topfrp->fr_height;
2785 if (extra_lines < 0)
2786 {
2787 /* reduce height of contained frames, bottom or top frame first */
2788 while (frp != NULL)
2789 {
2790 h = frame_minheight(frp, NULL);
2791 if (frp->fr_height + extra_lines < h)
2792 {
2793 extra_lines += frp->fr_height - h;
2794 frame_new_height(frp, h, topfirst, wfh);
2795 }
2796 else
2797 {
2798 frame_new_height(frp, frp->fr_height + extra_lines,
2799 topfirst, wfh);
2800 break;
2801 }
2802 if (topfirst)
2803 {
2804 do
2805 frp = frp->fr_next;
2806 while (wfh && frp != NULL && frame_fixed_height(frp));
2807 }
2808 else
2809 {
2810 do
2811 frp = frp->fr_prev;
2812 while (wfh && frp != NULL && frame_fixed_height(frp));
2813 }
2814 /* Increase "height" if we could not reduce enough frames. */
2815 if (frp == NULL)
2816 height -= extra_lines;
2817 }
2818 }
2819 else if (extra_lines > 0)
2820 {
2821 /* increase height of bottom or top frame */
2822 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2823 }
2824 }
2825 topfrp->fr_height = height;
2826}
2827
2828/*
2829 * Return TRUE if height of frame "frp" should not be changed because of
2830 * the 'winfixheight' option.
2831 */
2832 static int
2833frame_fixed_height(frp)
2834 frame_T *frp;
2835{
2836 /* frame with one window: fixed height if 'winfixheight' set. */
2837 if (frp->fr_win != NULL)
2838 return frp->fr_win->w_p_wfh;
2839
2840 if (frp->fr_layout == FR_ROW)
2841 {
2842 /* The frame is fixed height if one of the frames in the row is fixed
2843 * height. */
2844 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2845 if (frame_fixed_height(frp))
2846 return TRUE;
2847 return FALSE;
2848 }
2849
2850 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2851 * frames in the row are fixed height. */
2852 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2853 if (!frame_fixed_height(frp))
2854 return FALSE;
2855 return TRUE;
2856}
2857
2858#ifdef FEAT_VERTSPLIT
2859/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002860 * Return TRUE if width of frame "frp" should not be changed because of
2861 * the 'winfixwidth' option.
2862 */
2863 static int
2864frame_fixed_width(frp)
2865 frame_T *frp;
2866{
2867 /* frame with one window: fixed width if 'winfixwidth' set. */
2868 if (frp->fr_win != NULL)
2869 return frp->fr_win->w_p_wfw;
2870
2871 if (frp->fr_layout == FR_COL)
2872 {
2873 /* The frame is fixed width if one of the frames in the row is fixed
2874 * width. */
2875 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2876 if (frame_fixed_width(frp))
2877 return TRUE;
2878 return FALSE;
2879 }
2880
2881 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2882 * frames in the row are fixed width. */
2883 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2884 if (!frame_fixed_width(frp))
2885 return FALSE;
2886 return TRUE;
2887}
2888
2889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 * Add a status line to windows at the bottom of "frp".
2891 * Note: Does not check if there is room!
2892 */
2893 static void
2894frame_add_statusline(frp)
2895 frame_T *frp;
2896{
2897 win_T *wp;
2898
2899 if (frp->fr_layout == FR_LEAF)
2900 {
2901 wp = frp->fr_win;
2902 if (wp->w_status_height == 0)
2903 {
2904 if (wp->w_height > 0) /* don't make it negative */
2905 --wp->w_height;
2906 wp->w_status_height = STATUS_HEIGHT;
2907 }
2908 }
2909 else if (frp->fr_layout == FR_ROW)
2910 {
2911 /* Handle all the frames in the row. */
2912 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2913 frame_add_statusline(frp);
2914 }
2915 else /* frp->fr_layout == FR_COL */
2916 {
2917 /* Only need to handle the last frame in the column. */
2918 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2919 ;
2920 frame_add_statusline(frp);
2921 }
2922}
2923
2924/*
2925 * Set width of a frame. Handles recursively going through contained frames.
2926 * May remove separator line for windows at the right side (for win_close()).
2927 */
2928 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002929frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 frame_T *topfrp;
2931 int width;
2932 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002933 int wfw; /* obey 'winfixwidth' when there is a choice;
2934 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 frame_T *frp;
2937 int extra_cols;
2938 int w;
2939 win_T *wp;
2940
2941 if (topfrp->fr_layout == FR_LEAF)
2942 {
2943 /* Simple case: just one window. */
2944 wp = topfrp->fr_win;
2945 /* Find out if there are any windows right of this one. */
2946 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2947 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2948 break;
2949 if (frp->fr_parent == NULL)
2950 wp->w_vsep_width = 0;
2951 win_new_width(wp, width - wp->w_vsep_width);
2952 }
2953 else if (topfrp->fr_layout == FR_COL)
2954 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002955 do
2956 {
2957 /* All frames in this column get the same new width. */
2958 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2959 {
2960 frame_new_width(frp, width, leftfirst, wfw);
2961 if (frp->fr_width > width)
2962 {
2963 /* Could not fit the windows, make whole column wider. */
2964 width = frp->fr_width;
2965 break;
2966 }
2967 }
2968 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970 else /* fr_layout == FR_ROW */
2971 {
2972 /* Complicated case: Resize a row of frames. Resize the rightmost
2973 * frame first, frames left of it when needed. */
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002976 if (wfw)
2977 /* Advance past frames with one window with 'wfw' set. */
2978 while (frame_fixed_width(frp))
2979 {
2980 frp = frp->fr_next;
2981 if (frp == NULL)
2982 return; /* no frame without 'wfw', give up */
2983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002985 {
2986 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 while (frp->fr_next != NULL)
2988 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002989 if (wfw)
2990 /* Advance back for frames with one window with 'wfw' set. */
2991 while (frame_fixed_width(frp))
2992 frp = frp->fr_prev;
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 extra_cols = width - topfrp->fr_width;
2996 if (extra_cols < 0)
2997 {
2998 /* reduce frame width, rightmost frame first */
2999 while (frp != NULL)
3000 {
3001 w = frame_minwidth(frp, NULL);
3002 if (frp->fr_width + extra_cols < w)
3003 {
3004 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003005 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 else
3008 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003009 frame_new_width(frp, frp->fr_width + extra_cols,
3010 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 break;
3012 }
3013 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003014 {
3015 do
3016 frp = frp->fr_next;
3017 while (wfw && frp != NULL && frame_fixed_width(frp));
3018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003020 {
3021 do
3022 frp = frp->fr_prev;
3023 while (wfw && frp != NULL && frame_fixed_width(frp));
3024 }
3025 /* Increase "width" if we could not reduce enough frames. */
3026 if (frp == NULL)
3027 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
3029 }
3030 else if (extra_cols > 0)
3031 {
3032 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003033 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035 }
3036 topfrp->fr_width = width;
3037}
3038
3039/*
3040 * Add the vertical separator to windows at the right side of "frp".
3041 * Note: Does not check if there is room!
3042 */
3043 static void
3044frame_add_vsep(frp)
3045 frame_T *frp;
3046{
3047 win_T *wp;
3048
3049 if (frp->fr_layout == FR_LEAF)
3050 {
3051 wp = frp->fr_win;
3052 if (wp->w_vsep_width == 0)
3053 {
3054 if (wp->w_width > 0) /* don't make it negative */
3055 --wp->w_width;
3056 wp->w_vsep_width = 1;
3057 }
3058 }
3059 else if (frp->fr_layout == FR_COL)
3060 {
3061 /* Handle all the frames in the column. */
3062 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3063 frame_add_vsep(frp);
3064 }
3065 else /* frp->fr_layout == FR_ROW */
3066 {
3067 /* Only need to handle the last frame in the row. */
3068 frp = frp->fr_child;
3069 while (frp->fr_next != NULL)
3070 frp = frp->fr_next;
3071 frame_add_vsep(frp);
3072 }
3073}
3074
3075/*
3076 * Set frame width from the window it contains.
3077 */
3078 static void
3079frame_fix_width(wp)
3080 win_T *wp;
3081{
3082 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3083}
3084#endif
3085
3086/*
3087 * Set frame height from the window it contains.
3088 */
3089 static void
3090frame_fix_height(wp)
3091 win_T *wp;
3092{
3093 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3094}
3095
3096/*
3097 * Compute the minimal height for frame "topfrp".
3098 * Uses the 'winminheight' option.
3099 * When "next_curwin" isn't NULL, use p_wh for this window.
3100 * When "next_curwin" is NOWIN, don't use at least one line for the current
3101 * window.
3102 */
3103 static int
3104frame_minheight(topfrp, next_curwin)
3105 frame_T *topfrp;
3106 win_T *next_curwin;
3107{
3108 frame_T *frp;
3109 int m;
3110#ifdef FEAT_VERTSPLIT
3111 int n;
3112#endif
3113
3114 if (topfrp->fr_win != NULL)
3115 {
3116 if (topfrp->fr_win == next_curwin)
3117 m = p_wh + topfrp->fr_win->w_status_height;
3118 else
3119 {
3120 /* window: minimal height of the window plus status line */
3121 m = p_wmh + topfrp->fr_win->w_status_height;
3122 /* Current window is minimal one line high */
3123 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3124 ++m;
3125 }
3126 }
3127#ifdef FEAT_VERTSPLIT
3128 else if (topfrp->fr_layout == FR_ROW)
3129 {
3130 /* get the minimal height from each frame in this row */
3131 m = 0;
3132 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3133 {
3134 n = frame_minheight(frp, next_curwin);
3135 if (n > m)
3136 m = n;
3137 }
3138 }
3139#endif
3140 else
3141 {
3142 /* Add up the minimal heights for all frames in this column. */
3143 m = 0;
3144 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3145 m += frame_minheight(frp, next_curwin);
3146 }
3147
3148 return m;
3149}
3150
3151#ifdef FEAT_VERTSPLIT
3152/*
3153 * Compute the minimal width for frame "topfrp".
3154 * When "next_curwin" isn't NULL, use p_wiw for this window.
3155 * When "next_curwin" is NOWIN, don't use at least one column for the current
3156 * window.
3157 */
3158 static int
3159frame_minwidth(topfrp, next_curwin)
3160 frame_T *topfrp;
3161 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3162{
3163 frame_T *frp;
3164 int m, n;
3165
3166 if (topfrp->fr_win != NULL)
3167 {
3168 if (topfrp->fr_win == next_curwin)
3169 m = p_wiw + topfrp->fr_win->w_vsep_width;
3170 else
3171 {
3172 /* window: minimal width of the window plus separator column */
3173 m = p_wmw + topfrp->fr_win->w_vsep_width;
3174 /* Current window is minimal one column wide */
3175 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3176 ++m;
3177 }
3178 }
3179 else if (topfrp->fr_layout == FR_COL)
3180 {
3181 /* get the minimal width from each frame in this column */
3182 m = 0;
3183 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3184 {
3185 n = frame_minwidth(frp, next_curwin);
3186 if (n > m)
3187 m = n;
3188 }
3189 }
3190 else
3191 {
3192 /* Add up the minimal widths for all frames in this row. */
3193 m = 0;
3194 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3195 m += frame_minwidth(frp, next_curwin);
3196 }
3197
3198 return m;
3199}
3200#endif
3201
3202
3203/*
3204 * Try to close all windows except current one.
3205 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3206 * used and the buffer was modified.
3207 *
3208 * Used by ":bdel" and ":only".
3209 */
3210 void
3211close_others(message, forceit)
3212 int message;
3213 int forceit; /* always hide all other windows */
3214{
3215 win_T *wp;
3216 win_T *nextwp;
3217 int r;
3218
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003219 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 if (message
3222#ifdef FEAT_AUTOCMD
3223 && !autocmd_busy
3224#endif
3225 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003226 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 return;
3228 }
3229
3230 /* Be very careful here: autocommands may change the window layout. */
3231 for (wp = firstwin; win_valid(wp); wp = nextwp)
3232 {
3233 nextwp = wp->w_next;
3234 if (wp != curwin) /* don't close current window */
3235 {
3236
3237 /* Check if it's allowed to abandon this window */
3238 r = can_abandon(wp->w_buffer, forceit);
3239#ifdef FEAT_AUTOCMD
3240 if (!win_valid(wp)) /* autocommands messed wp up */
3241 {
3242 nextwp = firstwin;
3243 continue;
3244 }
3245#endif
3246 if (!r)
3247 {
3248#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3249 if (message && (p_confirm || cmdmod.confirm) && p_write)
3250 {
3251 dialog_changed(wp->w_buffer, FALSE);
3252# ifdef FEAT_AUTOCMD
3253 if (!win_valid(wp)) /* autocommands messed wp up */
3254 {
3255 nextwp = firstwin;
3256 continue;
3257 }
3258# endif
3259 }
3260 if (bufIsChanged(wp->w_buffer))
3261#endif
3262 continue;
3263 }
3264 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3265 }
3266 }
3267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 EMSG(_("E445: Other window contains changes"));
3270}
3271
3272#endif /* FEAT_WINDOWS */
3273
3274/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003275 * Init the current window "curwin".
3276 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
3278 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003279curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003281 win_init_empty(curwin);
3282}
3283
3284 void
3285win_init_empty(wp)
3286 win_T *wp;
3287{
3288 redraw_win_later(wp, NOT_VALID);
3289 wp->w_lines_valid = 0;
3290 wp->w_cursor.lnum = 1;
3291 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003293 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003295 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3296 wp->w_pcmark.col = 0;
3297 wp->w_prev_pcmark.lnum = 0;
3298 wp->w_prev_pcmark.col = 0;
3299 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003301 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003303 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003305 if (wp->w_p_rl)
3306 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003308 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003310#ifdef FEAT_SYN_HL
3311 wp->w_s = &wp->w_buffer->b_s;
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Allocate the first window and put an empty buffer in it.
3317 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321win_alloc_first()
3322{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003323 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 return FAIL;
3325
3326#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003327 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003328 if (first_tabpage == NULL)
3329 return FAIL;
3330 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003331 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 return OK;
3335}
3336
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003337#if defined(FEAT_AUTOCMD) || defined(PROTO)
3338/*
3339 * Init "aucmd_win". This can only be done after the first
3340 * window is fully initialized, thus it can't be in win_alloc_first().
3341 */
3342 void
3343win_alloc_aucmd_win()
3344{
3345 aucmd_win = win_alloc(NULL, TRUE);
3346 if (aucmd_win != NULL)
3347 {
3348 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003349 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003350 new_frame(aucmd_win);
3351 }
3352}
3353#endif
3354
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003355/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003356 * Allocate the first window or the first window in a new tab page.
3357 * When "oldwin" is NULL create an empty buffer for it.
3358 * When "oldwin" is not NULL copy info from it to the new window (only with
3359 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003360 * Return FAIL when something goes wrong (out of memory).
3361 */
3362 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003363win_alloc_firstwin(oldwin)
3364 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003365{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003366 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003367 if (oldwin == NULL)
3368 {
3369 /* Very first window, need to create an empty buffer for it and
3370 * initialize from scratch. */
3371 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3372 if (curwin == NULL || curbuf == NULL)
3373 return FAIL;
3374 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003375#ifdef FEAT_SYN_HL
3376 curwin->w_s = &(curbuf->b_s);
3377#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003378 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003380 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003382 curwin_init(); /* init current window */
3383 }
3384#ifdef FEAT_WINDOWS
3385 else
3386 {
3387 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003388 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003389
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003390 /* We don't want cursor- and scroll-binding in the first window. */
3391 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003392 }
3393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003395 new_frame(curwin);
3396 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003398 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399#ifdef FEAT_VERTSPLIT
3400 topframe->fr_width = Columns;
3401#endif
3402 topframe->fr_height = Rows - p_ch;
3403 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404
3405 return OK;
3406}
3407
3408/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003409 * Create a frame for window "wp".
3410 */
3411 static void
3412new_frame(win_T *wp)
3413{
3414 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3415
3416 wp->w_frame = frp;
3417 if (frp != NULL)
3418 {
3419 frp->fr_layout = FR_LEAF;
3420 frp->fr_win = wp;
3421 }
3422}
3423
3424/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425 * Initialize the window and frame size to the maximum.
3426 */
3427 void
3428win_init_size()
3429{
3430 firstwin->w_height = ROWS_AVAIL;
3431 topframe->fr_height = ROWS_AVAIL;
3432#ifdef FEAT_VERTSPLIT
3433 firstwin->w_width = Columns;
3434 topframe->fr_width = Columns;
3435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003439
3440/*
3441 * Allocate a new tabpage_T and init the values.
3442 * Returns NULL when out of memory.
3443 */
3444 static tabpage_T *
3445alloc_tabpage()
3446{
3447 tabpage_T *tp;
3448
3449 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3450 if (tp != NULL)
3451 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003452# ifdef FEAT_GUI
3453 int i;
3454
3455 for (i = 0; i < 3; i++)
3456 tp->tp_prev_which_scrollbars[i] = -1;
3457# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003458# ifdef FEAT_DIFF
3459 tp->tp_diff_invalid = TRUE;
3460# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003461#ifdef FEAT_EVAL
3462 /* init t: variables */
3463 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3464#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003465 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003466 }
3467 return tp;
3468}
3469
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003470 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003471free_tabpage(tp)
3472 tabpage_T *tp;
3473{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003474 int idx;
3475
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003476# ifdef FEAT_DIFF
3477 diff_clear(tp);
3478# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003479 for (idx = 0; idx < SNAP_COUNT; ++idx)
3480 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003481#ifdef FEAT_EVAL
3482 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3483#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003484 vim_free(tp);
3485}
3486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003487/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003488 * Create a new Tab page with one window.
3489 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003490 * When "after" is 0 put it just after the current Tab page.
3491 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003492 * Return FAIL or OK.
3493 */
3494 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003495win_new_tabpage(after)
3496 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003497{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003499 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003500 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003501
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003502 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 if (newtp == NULL)
3504 return FAIL;
3505
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003507 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003508 {
3509 vim_free(newtp);
3510 return FAIL;
3511 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003512 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003513
3514 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003515 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003518 if (after == 1)
3519 {
3520 /* New tab page becomes the first one. */
3521 newtp->tp_next = first_tabpage;
3522 first_tabpage = newtp;
3523 }
3524 else
3525 {
3526 if (after > 0)
3527 {
3528 /* Put new tab page before tab page "after". */
3529 n = 2;
3530 for (tp = first_tabpage; tp->tp_next != NULL
3531 && n < after; tp = tp->tp_next)
3532 ++n;
3533 }
3534 newtp->tp_next = tp->tp_next;
3535 tp->tp_next = newtp;
3536 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003538 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003539 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003540
3541 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003542 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003543
3544#if defined(FEAT_GUI)
3545 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3546 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003547 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003548#endif
3549
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003551#ifdef FEAT_AUTOCMD
3552 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3553 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3554#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003555 return OK;
3556 }
3557
3558 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003559 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 return FAIL;
3561}
3562
3563/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003564 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3565 * like with ":split".
3566 * Returns OK if a new tab page was created, FAIL otherwise.
3567 */
3568 int
3569may_open_tabpage()
3570{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003571 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003572
Bram Moolenaard326ce82007-03-11 14:48:29 +00003573 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003574 {
3575 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003576 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003577 return win_new_tabpage(n);
3578 }
3579 return FAIL;
3580}
3581
3582/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003583 * Create up to "maxcount" tabpages with empty windows.
3584 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003586 int
3587make_tabpages(maxcount)
3588 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003590 int count = maxcount;
3591 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003593 /* Limit to 'tabpagemax' tabs. */
3594 if (count > p_tpm)
3595 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003596
3597#ifdef FEAT_AUTOCMD
3598 /*
3599 * Don't execute autocommands while creating the tab pages. Must do that
3600 * when putting the buffers in the windows.
3601 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003602 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003603#endif
3604
3605 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003606 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003608
3609#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003610 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003611#endif
3612
3613 /* return actual number of tab pages */
3614 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615}
3616
3617/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003618 * Return TRUE when "tpc" points to a valid tab page.
3619 */
3620 int
3621valid_tabpage(tpc)
3622 tabpage_T *tpc;
3623{
3624 tabpage_T *tp;
3625
3626 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3627 if (tp == tpc)
3628 return TRUE;
3629 return FALSE;
3630}
3631
3632/*
3633 * Find tab page "n" (first one is 1). Returns NULL when not found.
3634 */
3635 tabpage_T *
3636find_tabpage(n)
3637 int n;
3638{
3639 tabpage_T *tp;
3640 int i = 1;
3641
3642 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3643 ++i;
3644 return tp;
3645}
3646
3647/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003649 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003650 */
3651 int
3652tabpage_index(ftp)
3653 tabpage_T *ftp;
3654{
3655 int i = 1;
3656 tabpage_T *tp;
3657
3658 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3659 ++i;
3660 return i;
3661}
3662
3663/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003664 * Prepare for leaving the current tab page.
3665 * When autocomands change "curtab" we don't leave the tab page and return
3666 * FAIL.
3667 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003668 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003669 static int
3670leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003671 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003672 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003673{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003674 tabpage_T *tp = curtab;
3675
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003676#ifdef FEAT_VISUAL
3677 reset_VIsual_and_resel(); /* stop Visual mode */
3678#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003679#ifdef FEAT_AUTOCMD
3680 if (new_curbuf != curbuf)
3681 {
3682 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3683 if (curtab != tp)
3684 return FAIL;
3685 }
3686 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3687 if (curtab != tp)
3688 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003689 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003690 if (curtab != tp)
3691 return FAIL;
3692#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003693#if defined(FEAT_GUI)
3694 /* Remove the scrollbars. They may be added back later. */
3695 if (gui.in_use)
3696 gui_remove_scrollbars();
3697#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003698 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003699 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003700 tp->tp_firstwin = firstwin;
3701 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003702 tp->tp_old_Rows = Rows;
3703 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003704 firstwin = NULL;
3705 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003706 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003707}
3708
3709/*
3710 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003711 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 static void
3714enter_tabpage(tp, old_curbuf)
3715 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003716 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003717{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003718 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003719 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003720
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003721 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722 firstwin = tp->tp_firstwin;
3723 lastwin = tp->tp_lastwin;
3724 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003725
3726 /* We would like doing the TabEnter event first, but we don't have a
3727 * valid current window yet, which may break some commands.
3728 * This triggers autocommands, thus may make "tp" invalid. */
3729 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3730 prevwin = next_prevwin;
3731
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003732 last_status(FALSE); /* status line may appear or disappear */
3733 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003734 must_redraw = CLEAR; /* need to redraw everything */
3735#ifdef FEAT_DIFF
3736 diff_need_scrollbind = TRUE;
3737#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003738
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003739 /* The tabpage line may have appeared or disappeared, may need to resize
3740 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003741 * frame sizes too. Use the stored value of p_ch, so that it can be
3742 * different for each tab page. */
3743 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003744 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3745#ifdef FEAT_GUI_TABLINE
3746 && !gui_use_tabline()
3747#endif
3748 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003749 shell_new_rows();
3750#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003751 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003752 shell_new_columns(); /* update window widths */
3753#endif
3754
3755#if defined(FEAT_GUI)
3756 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3757 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003758 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759#endif
3760
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003761#ifdef FEAT_AUTOCMD
3762 /* Apply autocommands after updating the display, when 'rows' and
3763 * 'columns' have been set correctly. */
3764 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3765 if (old_curbuf != curbuf)
3766 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3767#endif
3768
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003769 redraw_all_later(CLEAR);
3770}
3771
3772/*
3773 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003774 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775 */
3776 void
3777goto_tabpage(n)
3778 int n;
3779{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003780 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003781 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782 int i;
3783
Bram Moolenaard68071d2006-05-02 22:08:30 +00003784 if (text_locked())
3785 {
3786 /* Not allowed when editing the command line. */
3787#ifdef FEAT_CMDWIN
3788 if (cmdwin_type != 0)
3789 EMSG(_(e_cmdwin));
3790 else
3791#endif
3792 EMSG(_(e_secure));
3793 return;
3794 }
3795
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003796 /* If there is only one it can't work. */
3797 if (first_tabpage->tp_next == NULL)
3798 {
3799 if (n > 1)
3800 beep_flush();
3801 return;
3802 }
3803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 if (n == 0)
3805 {
3806 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003807 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003808 tp = first_tabpage;
3809 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003810 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003812 else if (n < 0)
3813 {
3814 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3815 * this N times. */
3816 ttp = curtab;
3817 for (i = n; i < 0; ++i)
3818 {
3819 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3820 tp = tp->tp_next)
3821 ;
3822 ttp = tp;
3823 }
3824 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003825 else if (n == 9999)
3826 {
3827 /* Go to last tab page. */
3828 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3829 ;
3830 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831 else
3832 {
3833 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003834 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003835 if (tp == NULL)
3836 {
3837 beep_flush();
3838 return;
3839 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840 }
3841
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003842 goto_tabpage_tp(tp);
3843
3844#ifdef FEAT_GUI_TABLINE
3845 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003846 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003847#endif
3848}
3849
3850/*
3851 * Go to tabpage "tp".
3852 * Note: doesn't update the GUI tab.
3853 */
3854 void
3855goto_tabpage_tp(tp)
3856 tabpage_T *tp;
3857{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003858 /* Don't repeat a message in another tab page. */
3859 set_keep_msg(NULL, 0);
3860
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003861 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003862 {
3863 if (valid_tabpage(tp))
3864 enter_tabpage(tp, curbuf);
3865 else
3866 enter_tabpage(curtab, curbuf);
3867 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003868}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
3870/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003871 * Enter window "wp" in tab page "tp".
3872 * Also updates the GUI tab.
3873 */
3874 void
3875goto_tabpage_win(tp, wp)
3876 tabpage_T *tp;
3877 win_T *wp;
3878{
3879 goto_tabpage_tp(tp);
3880 if (curtab == tp && win_valid(wp))
3881 {
3882 win_enter(wp, TRUE);
3883# ifdef FEAT_GUI_TABLINE
3884 if (gui_use_tabline())
3885 gui_mch_set_curtab(tabpage_index(curtab));
3886# endif
3887 }
3888}
3889
3890/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003891 * Move the current tab page to before tab page "nr".
3892 */
3893 void
3894tabpage_move(nr)
3895 int nr;
3896{
3897 int n = nr;
3898 tabpage_T *tp;
3899
3900 if (first_tabpage->tp_next == NULL)
3901 return;
3902
3903 /* Remove the current tab page from the list of tab pages. */
3904 if (curtab == first_tabpage)
3905 first_tabpage = curtab->tp_next;
3906 else
3907 {
3908 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3909 if (tp->tp_next == curtab)
3910 break;
3911 if (tp == NULL) /* "cannot happen" */
3912 return;
3913 tp->tp_next = curtab->tp_next;
3914 }
3915
3916 /* Re-insert it at the specified position. */
3917 if (n == 0)
3918 {
3919 curtab->tp_next = first_tabpage;
3920 first_tabpage = curtab;
3921 }
3922 else
3923 {
3924 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3925 --n;
3926 curtab->tp_next = tp->tp_next;
3927 tp->tp_next = curtab;
3928 }
3929
3930 /* Need to redraw the tabline. Tab page contents doesn't change. */
3931 redraw_tabline = TRUE;
3932}
3933
3934
3935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 * Go to another window.
3937 * When jumping to another buffer, stop Visual mode. Do this before
3938 * changing windows so we can yank the selection into the '*' register.
3939 * When jumping to another window on the same buffer, adjust its cursor
3940 * position to keep the same Visual area.
3941 */
3942 void
3943win_goto(wp)
3944 win_T *wp;
3945{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003946#ifdef FEAT_CONCEAL
3947 win_T *owp = curwin;
3948#endif
3949
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003950 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003953 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return;
3955 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003956#ifdef FEAT_AUTOCMD
3957 if (curbuf_locked())
3958 return;
3959#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961#ifdef FEAT_VISUAL
3962 if (wp->w_buffer != curbuf)
3963 reset_VIsual_and_resel();
3964 else if (VIsual_active)
3965 wp->w_cursor = curwin->w_cursor;
3966#endif
3967
3968#ifdef FEAT_GUI
3969 need_mouse_correct = TRUE;
3970#endif
3971 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003972
3973#ifdef FEAT_CONCEAL
3974 /* Conceal cursor line in previous window, unconceal in current window. */
3975 if (win_valid(owp))
3976 update_single_line(owp, owp->w_cursor.lnum);
3977 update_single_line(curwin, curwin->w_cursor.lnum);
3978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979}
3980
3981#if defined(FEAT_PERL) || defined(PROTO)
3982/*
3983 * Find window number "winnr" (counting top to bottom).
3984 */
3985 win_T *
3986win_find_nr(winnr)
3987 int winnr;
3988{
3989 win_T *wp;
3990
3991# ifdef FEAT_WINDOWS
3992 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3993 if (--winnr == 0)
3994 break;
3995 return wp;
3996# else
3997 return curwin;
3998# endif
3999}
4000#endif
4001
4002#ifdef FEAT_VERTSPLIT
4003/*
4004 * Move to window above or below "count" times.
4005 */
4006 static void
4007win_goto_ver(up, count)
4008 int up; /* TRUE to go to win above */
4009 long count;
4010{
4011 frame_T *fr;
4012 frame_T *nfr;
4013 frame_T *foundfr;
4014
4015 foundfr = curwin->w_frame;
4016 while (count--)
4017 {
4018 /*
4019 * First go upwards in the tree of frames until we find a upwards or
4020 * downwards neighbor.
4021 */
4022 fr = foundfr;
4023 for (;;)
4024 {
4025 if (fr == topframe)
4026 goto end;
4027 if (up)
4028 nfr = fr->fr_prev;
4029 else
4030 nfr = fr->fr_next;
4031 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4032 break;
4033 fr = fr->fr_parent;
4034 }
4035
4036 /*
4037 * Now go downwards to find the bottom or top frame in it.
4038 */
4039 for (;;)
4040 {
4041 if (nfr->fr_layout == FR_LEAF)
4042 {
4043 foundfr = nfr;
4044 break;
4045 }
4046 fr = nfr->fr_child;
4047 if (nfr->fr_layout == FR_ROW)
4048 {
4049 /* Find the frame at the cursor row. */
4050 while (fr->fr_next != NULL
4051 && frame2win(fr)->w_wincol + fr->fr_width
4052 <= curwin->w_wincol + curwin->w_wcol)
4053 fr = fr->fr_next;
4054 }
4055 if (nfr->fr_layout == FR_COL && up)
4056 while (fr->fr_next != NULL)
4057 fr = fr->fr_next;
4058 nfr = fr;
4059 }
4060 }
4061end:
4062 if (foundfr != NULL)
4063 win_goto(foundfr->fr_win);
4064}
4065
4066/*
4067 * Move to left or right window.
4068 */
4069 static void
4070win_goto_hor(left, count)
4071 int left; /* TRUE to go to left win */
4072 long count;
4073{
4074 frame_T *fr;
4075 frame_T *nfr;
4076 frame_T *foundfr;
4077
4078 foundfr = curwin->w_frame;
4079 while (count--)
4080 {
4081 /*
4082 * First go upwards in the tree of frames until we find a left or
4083 * right neighbor.
4084 */
4085 fr = foundfr;
4086 for (;;)
4087 {
4088 if (fr == topframe)
4089 goto end;
4090 if (left)
4091 nfr = fr->fr_prev;
4092 else
4093 nfr = fr->fr_next;
4094 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4095 break;
4096 fr = fr->fr_parent;
4097 }
4098
4099 /*
4100 * Now go downwards to find the leftmost or rightmost frame in it.
4101 */
4102 for (;;)
4103 {
4104 if (nfr->fr_layout == FR_LEAF)
4105 {
4106 foundfr = nfr;
4107 break;
4108 }
4109 fr = nfr->fr_child;
4110 if (nfr->fr_layout == FR_COL)
4111 {
4112 /* Find the frame at the cursor row. */
4113 while (fr->fr_next != NULL
4114 && frame2win(fr)->w_winrow + fr->fr_height
4115 <= curwin->w_winrow + curwin->w_wrow)
4116 fr = fr->fr_next;
4117 }
4118 if (nfr->fr_layout == FR_ROW && left)
4119 while (fr->fr_next != NULL)
4120 fr = fr->fr_next;
4121 nfr = fr;
4122 }
4123 }
4124end:
4125 if (foundfr != NULL)
4126 win_goto(foundfr->fr_win);
4127}
4128#endif
4129
4130/*
4131 * Make window "wp" the current window.
4132 */
4133 void
4134win_enter(wp, undo_sync)
4135 win_T *wp;
4136 int undo_sync;
4137{
4138 win_enter_ext(wp, undo_sync, FALSE);
4139}
4140
4141/*
4142 * Make window wp the current window.
4143 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4144 * been closed and isn't valid.
4145 */
4146 static void
4147win_enter_ext(wp, undo_sync, curwin_invalid)
4148 win_T *wp;
4149 int undo_sync;
4150 int curwin_invalid;
4151{
4152#ifdef FEAT_AUTOCMD
4153 int other_buffer = FALSE;
4154#endif
4155
4156 if (wp == curwin && !curwin_invalid) /* nothing to do */
4157 return;
4158
4159#ifdef FEAT_AUTOCMD
4160 if (!curwin_invalid)
4161 {
4162 /*
4163 * Be careful: If autocommands delete the window, return now.
4164 */
4165 if (wp->w_buffer != curbuf)
4166 {
4167 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4168 other_buffer = TRUE;
4169 if (!win_valid(wp))
4170 return;
4171 }
4172 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4173 if (!win_valid(wp))
4174 return;
4175# ifdef FEAT_EVAL
4176 /* autocmds may abort script processing */
4177 if (aborting())
4178 return;
4179# endif
4180 }
4181#endif
4182
4183 /* sync undo before leaving the current buffer */
4184 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004185 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 /* may have to copy the buffer options when 'cpo' contains 'S' */
4187 if (wp->w_buffer != curbuf)
4188 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4189 if (!curwin_invalid)
4190 {
4191 prevwin = curwin; /* remember for CTRL-W p */
4192 curwin->w_redr_status = TRUE;
4193 }
4194 curwin = wp;
4195 curbuf = wp->w_buffer;
4196 check_cursor();
4197#ifdef FEAT_VIRTUALEDIT
4198 if (!virtual_active())
4199 curwin->w_cursor.coladd = 0;
4200#endif
4201 changed_line_abv_curs(); /* assume cursor position needs updating */
4202
4203 if (curwin->w_localdir != NULL)
4204 {
4205 /* Window has a local directory: Save current directory as global
4206 * directory (unless that was done already) and change to the local
4207 * directory. */
4208 if (globaldir == NULL)
4209 {
4210 char_u cwd[MAXPATHL];
4211
4212 if (mch_dirname(cwd, MAXPATHL) == OK)
4213 globaldir = vim_strsave(cwd);
4214 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004215 if (mch_chdir((char *)curwin->w_localdir) == 0)
4216 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 else if (globaldir != NULL)
4219 {
4220 /* Window doesn't have a local directory and we are not in the global
4221 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004222 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 vim_free(globaldir);
4224 globaldir = NULL;
4225 shorten_fnames(TRUE);
4226 }
4227
4228#ifdef FEAT_AUTOCMD
4229 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4230 if (other_buffer)
4231 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4232#endif
4233
4234#ifdef FEAT_TITLE
4235 maketitle();
4236#endif
4237 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004238 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (restart_edit)
4240 redraw_later(VALID); /* causes status line redraw */
4241
4242 /* set window height to desired minimal value */
4243 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4244 win_setheight((int)p_wh);
4245 else if (curwin->w_height == 0)
4246 win_setheight(1);
4247
4248#ifdef FEAT_VERTSPLIT
4249 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004250 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 win_setwidth((int)p_wiw);
4252#endif
4253
4254#ifdef FEAT_MOUSE
4255 setmouse(); /* in case jumped to/from help buffer */
4256#endif
4257
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004258 /* Change directories when the 'acd' option is set. */
4259 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260}
4261
4262#endif /* FEAT_WINDOWS */
4263
4264#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4265/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004266 * Jump to the first open window that contains buffer "buf", if one exists.
4267 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
4269 win_T *
4270buf_jump_open_win(buf)
4271 buf_T *buf;
4272{
4273# ifdef FEAT_WINDOWS
4274 win_T *wp;
4275
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004276 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (wp->w_buffer == buf)
4278 break;
4279 if (wp != NULL)
4280 win_enter(wp, FALSE);
4281 return wp;
4282# else
4283 if (curwin->w_buffer == buf)
4284 return curwin;
4285 return NULL;
4286# endif
4287}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004288
4289/*
4290 * Jump to the first open window in any tab page that contains buffer "buf",
4291 * if one exists.
4292 * Returns a pointer to the window found, otherwise NULL.
4293 */
4294 win_T *
4295buf_jump_open_tab(buf)
4296 buf_T *buf;
4297{
4298# ifdef FEAT_WINDOWS
4299 win_T *wp;
4300 tabpage_T *tp;
4301
4302 /* First try the current tab page. */
4303 wp = buf_jump_open_win(buf);
4304 if (wp != NULL)
4305 return wp;
4306
4307 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4308 if (tp != curtab)
4309 {
4310 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4311 if (wp->w_buffer == buf)
4312 break;
4313 if (wp != NULL)
4314 {
4315 goto_tabpage_win(tp, wp);
4316 if (curwin != wp)
4317 wp = NULL; /* something went wrong */
4318 break;
4319 }
4320 }
4321
4322 return wp;
4323# else
4324 if (curwin->w_buffer == buf)
4325 return curwin;
4326 return NULL;
4327# endif
4328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329#endif
4330
4331/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004332 * Allocate a window structure and link it in the window list when "hidden" is
4333 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004336win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004337 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004338 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004340 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 /*
4343 * allocate window structure and linesizes arrays
4344 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004345 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4346 if (new_wp != NULL && win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004348 vim_free(new_wp);
4349 new_wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004352 if (new_wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004354#ifdef FEAT_AUTOCMD
4355 /* Don't execute autocommands while the window is not properly
4356 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4357 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004358 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /*
4361 * link the window in the window list
4362 */
4363#ifdef FEAT_WINDOWS
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004364 if (!hidden)
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004365 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#endif
4367#ifdef FEAT_VERTSPLIT
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004368 new_wp->w_wincol = 0;
4369 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#endif
4371
4372 /* position the display and the cursor at the top of the file. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004373 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374#ifdef FEAT_DIFF
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004375 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004377 new_wp->w_botline = 2;
4378 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_SCROLLBIND
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004380 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
4382
4383 /* We won't calculate w_fraction until resizing the window */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004384 new_wp->w_fraction = 0;
4385 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387#ifdef FEAT_GUI
4388 if (gui.in_use)
4389 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004390 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4391 SBAR_LEFT, new_wp);
4392 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4393 SBAR_RIGHT, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395#endif
4396#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004397 /* init w: variables */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004398 init_var_dict(&new_wp->w_vars, &new_wp->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#endif
4400#ifdef FEAT_FOLDING
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004401 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004403#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004404 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004405#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004406#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004407 new_wp->w_match_head = NULL;
4408 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004411 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412}
4413
4414#if defined(FEAT_WINDOWS) || defined(PROTO)
4415
4416/*
4417 * remove window 'wp' from the window list and free the structure
4418 */
4419 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004420win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004422 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 int i;
4425
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004426#ifdef FEAT_FOLDING
4427 clearFolding(wp);
4428#endif
4429
4430 /* reduce the reference count to the argument list. */
4431 alist_unlink(wp->w_alist);
4432
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004433#ifdef FEAT_AUTOCMD
4434 /* Don't execute autocommands while the window is halfway being deleted.
4435 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004436 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004437#endif
4438
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004439#ifdef FEAT_LUA
4440 lua_window_free(wp);
4441#endif
4442
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004443#ifdef FEAT_MZSCHEME
4444 mzscheme_window_free(wp);
4445#endif
4446
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_PERL
4448 perl_win_free(wp);
4449#endif
4450
4451#ifdef FEAT_PYTHON
4452 python_window_free(wp);
4453#endif
4454
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004455#ifdef FEAT_PYTHON3
4456 python3_window_free(wp);
4457#endif
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459#ifdef FEAT_TCL
4460 tcl_window_free(wp);
4461#endif
4462
4463#ifdef FEAT_RUBY
4464 ruby_window_free(wp);
4465#endif
4466
4467 clear_winopt(&wp->w_onebuf_opt);
4468 clear_winopt(&wp->w_allbuf_opt);
4469
4470#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004471 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#endif
4473
4474 if (prevwin == wp)
4475 prevwin = NULL;
4476 win_free_lsize(wp);
4477
4478 for (i = 0; i < wp->w_tagstacklen; ++i)
4479 vim_free(wp->w_tagstack[i].tagname);
4480
4481 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004484 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#ifdef FEAT_JUMPLIST
4488 free_jumplist(wp);
4489#endif
4490
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004491#ifdef FEAT_QUICKFIX
4492 qf_free_all(wp);
4493#endif
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#ifdef FEAT_GUI
4496 if (gui.in_use)
4497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4499 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4500 }
4501#endif /* FEAT_GUI */
4502
Bram Moolenaar860cae12010-06-05 23:22:07 +02004503#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004504 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004505#endif
4506
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004507#ifdef FEAT_AUTOCMD
4508 if (wp != aucmd_win)
4509#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004510 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004512
4513#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004514 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516}
4517
4518/*
4519 * Append window "wp" in the window list after window "after".
4520 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004521 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522win_append(after, wp)
4523 win_T *after, *wp;
4524{
4525 win_T *before;
4526
4527 if (after == NULL) /* after NULL is in front of the first */
4528 before = firstwin;
4529 else
4530 before = after->w_next;
4531
4532 wp->w_next = before;
4533 wp->w_prev = after;
4534 if (after == NULL)
4535 firstwin = wp;
4536 else
4537 after->w_next = wp;
4538 if (before == NULL)
4539 lastwin = wp;
4540 else
4541 before->w_prev = wp;
4542}
4543
4544/*
4545 * Remove a window from the window list.
4546 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004547 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004548win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004550 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551{
4552 if (wp->w_prev != NULL)
4553 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004554 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004556 else
4557 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (wp->w_next != NULL)
4559 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004560 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004562 else
4563 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564}
4565
4566/*
4567 * Append frame "frp" in a frame list after frame "after".
4568 */
4569 static void
4570frame_append(after, frp)
4571 frame_T *after, *frp;
4572{
4573 frp->fr_next = after->fr_next;
4574 after->fr_next = frp;
4575 if (frp->fr_next != NULL)
4576 frp->fr_next->fr_prev = frp;
4577 frp->fr_prev = after;
4578}
4579
4580/*
4581 * Insert frame "frp" in a frame list before frame "before".
4582 */
4583 static void
4584frame_insert(before, frp)
4585 frame_T *before, *frp;
4586{
4587 frp->fr_next = before;
4588 frp->fr_prev = before->fr_prev;
4589 before->fr_prev = frp;
4590 if (frp->fr_prev != NULL)
4591 frp->fr_prev->fr_next = frp;
4592 else
4593 frp->fr_parent->fr_child = frp;
4594}
4595
4596/*
4597 * Remove a frame from a frame list.
4598 */
4599 static void
4600frame_remove(frp)
4601 frame_T *frp;
4602{
4603 if (frp->fr_prev != NULL)
4604 frp->fr_prev->fr_next = frp->fr_next;
4605 else
4606 frp->fr_parent->fr_child = frp->fr_next;
4607 if (frp->fr_next != NULL)
4608 frp->fr_next->fr_prev = frp->fr_prev;
4609}
4610
4611#endif /* FEAT_WINDOWS */
4612
4613/*
4614 * Allocate w_lines[] for window "wp".
4615 * Return FAIL for failure, OK for success.
4616 */
4617 int
4618win_alloc_lines(wp)
4619 win_T *wp;
4620{
4621 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004622 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (wp->w_lines == NULL)
4624 return FAIL;
4625 return OK;
4626}
4627
4628/*
4629 * free lsize arrays for a window
4630 */
4631 void
4632win_free_lsize(wp)
4633 win_T *wp;
4634{
4635 vim_free(wp->w_lines);
4636 wp->w_lines = NULL;
4637}
4638
4639/*
4640 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004641 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 */
4643 void
4644shell_new_rows()
4645{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004646 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 if (firstwin == NULL) /* not initialized yet */
4649 return;
4650#ifdef FEAT_WINDOWS
4651 if (h < frame_minheight(topframe, NULL))
4652 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004653
4654 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 * that doesn't result in the right height, forget about that option. */
4656 frame_new_height(topframe, h, FALSE, TRUE);
4657 if (topframe->fr_height != h)
4658 frame_new_height(topframe, h, FALSE, FALSE);
4659
4660 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4661#else
4662 if (h < 1)
4663 h = 1;
4664 win_new_height(firstwin, h);
4665#endif
4666 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004667#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004668 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004669#endif
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#if 0
4672 /* Disabled: don't want making the screen smaller make a window larger. */
4673 if (p_ea)
4674 win_equal(curwin, FALSE, 'v');
4675#endif
4676}
4677
4678#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4679/*
4680 * Called from win_new_shellsize() after Columns changed.
4681 */
4682 void
4683shell_new_columns()
4684{
4685 if (firstwin == NULL) /* not initialized yet */
4686 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004687
4688 /* First try setting the widths of windows with 'winfixwidth'. If that
4689 * doesn't result in the right width, forget about that option. */
4690 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4691 if (topframe->fr_width != Columns)
4692 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4695#if 0
4696 /* Disabled: don't want making the screen smaller make a window larger. */
4697 if (p_ea)
4698 win_equal(curwin, FALSE, 'h');
4699#endif
4700}
4701#endif
4702
4703#if defined(FEAT_CMDWIN) || defined(PROTO)
4704/*
4705 * Save the size of all windows in "gap".
4706 */
4707 void
4708win_size_save(gap)
4709 garray_T *gap;
4710
4711{
4712 win_T *wp;
4713
4714 ga_init2(gap, (int)sizeof(int), 1);
4715 if (ga_grow(gap, win_count() * 2) == OK)
4716 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4717 {
4718 ((int *)gap->ga_data)[gap->ga_len++] =
4719 wp->w_width + wp->w_vsep_width;
4720 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4721 }
4722}
4723
4724/*
4725 * Restore window sizes, but only if the number of windows is still the same.
4726 * Does not free the growarray.
4727 */
4728 void
4729win_size_restore(gap)
4730 garray_T *gap;
4731{
4732 win_T *wp;
4733 int i;
4734
4735 if (win_count() * 2 == gap->ga_len)
4736 {
4737 i = 0;
4738 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4739 {
4740 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4741 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4742 }
4743 /* recompute the window positions */
4744 (void)win_comp_pos();
4745 }
4746}
4747#endif /* FEAT_CMDWIN */
4748
4749#if defined(FEAT_WINDOWS) || defined(PROTO)
4750/*
4751 * Update the position for all windows, using the width and height of the
4752 * frames.
4753 * Returns the row just after the last window.
4754 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004755 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756win_comp_pos()
4757{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004758 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 int col = 0;
4760
4761 frame_comp_pos(topframe, &row, &col);
4762 return row;
4763}
4764
4765/*
4766 * Update the position of the windows in frame "topfrp", using the width and
4767 * height of the frames.
4768 * "*row" and "*col" are the top-left position of the frame. They are updated
4769 * to the bottom-right position plus one.
4770 */
4771 static void
4772frame_comp_pos(topfrp, row, col)
4773 frame_T *topfrp;
4774 int *row;
4775 int *col;
4776{
4777 win_T *wp;
4778 frame_T *frp;
4779#ifdef FEAT_VERTSPLIT
4780 int startcol;
4781 int startrow;
4782#endif
4783
4784 wp = topfrp->fr_win;
4785 if (wp != NULL)
4786 {
4787 if (wp->w_winrow != *row
4788#ifdef FEAT_VERTSPLIT
4789 || wp->w_wincol != *col
4790#endif
4791 )
4792 {
4793 /* position changed, redraw */
4794 wp->w_winrow = *row;
4795#ifdef FEAT_VERTSPLIT
4796 wp->w_wincol = *col;
4797#endif
4798 redraw_win_later(wp, NOT_VALID);
4799 wp->w_redr_status = TRUE;
4800 }
4801 *row += wp->w_height + wp->w_status_height;
4802#ifdef FEAT_VERTSPLIT
4803 *col += wp->w_width + wp->w_vsep_width;
4804#endif
4805 }
4806 else
4807 {
4808#ifdef FEAT_VERTSPLIT
4809 startrow = *row;
4810 startcol = *col;
4811#endif
4812 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4813 {
4814#ifdef FEAT_VERTSPLIT
4815 if (topfrp->fr_layout == FR_ROW)
4816 *row = startrow; /* all frames are at the same row */
4817 else
4818 *col = startcol; /* all frames are at the same col */
4819#endif
4820 frame_comp_pos(frp, row, col);
4821 }
4822 }
4823}
4824
4825#endif /* FEAT_WINDOWS */
4826
4827/*
4828 * Set current window height and take care of repositioning other windows to
4829 * fit around it.
4830 */
4831 void
4832win_setheight(height)
4833 int height;
4834{
4835 win_setheight_win(height, curwin);
4836}
4837
4838/*
4839 * Set the window height of window "win" and take care of repositioning other
4840 * windows to fit around it.
4841 */
4842 void
4843win_setheight_win(height, win)
4844 int height;
4845 win_T *win;
4846{
4847 int row;
4848
4849 if (win == curwin)
4850 {
4851 /* Always keep current window at least one line high, even when
4852 * 'winminheight' is zero. */
4853#ifdef FEAT_WINDOWS
4854 if (height < p_wmh)
4855 height = p_wmh;
4856#endif
4857 if (height == 0)
4858 height = 1;
4859 }
4860
4861#ifdef FEAT_WINDOWS
4862 frame_setheight(win->w_frame, height + win->w_status_height);
4863
4864 /* recompute the window positions */
4865 row = win_comp_pos();
4866#else
4867 if (height > topframe->fr_height)
4868 height = topframe->fr_height;
4869 win->w_height = height;
4870 row = height;
4871#endif
4872
4873 /*
4874 * If there is extra space created between the last window and the command
4875 * line, clear it.
4876 */
4877 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4878 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4879 cmdline_row = row;
4880 msg_row = row;
4881 msg_col = 0;
4882
4883 redraw_all_later(NOT_VALID);
4884}
4885
4886#if defined(FEAT_WINDOWS) || defined(PROTO)
4887
4888/*
4889 * Set the height of a frame to "height" and take care that all frames and
4890 * windows inside it are resized. Also resize frames on the left and right if
4891 * the are in the same FR_ROW frame.
4892 *
4893 * Strategy:
4894 * If the frame is part of a FR_COL frame, try fitting the frame in that
4895 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4896 * go to containing frames to resize them and make room.
4897 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4898 * Check for the minimal height of the FR_ROW frame.
4899 * At the top level we can also use change the command line height.
4900 */
4901 static void
4902frame_setheight(curfrp, height)
4903 frame_T *curfrp;
4904 int height;
4905{
4906 int room; /* total number of lines available */
4907 int take; /* number of lines taken from other windows */
4908 int room_cmdline; /* lines available from cmdline */
4909 int run;
4910 frame_T *frp;
4911 int h;
4912 int room_reserved;
4913
4914 /* If the height already is the desired value, nothing to do. */
4915 if (curfrp->fr_height == height)
4916 return;
4917
4918 if (curfrp->fr_parent == NULL)
4919 {
4920 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004921 if (height > ROWS_AVAIL)
4922 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (height > 0)
4924 frame_new_height(curfrp, height, FALSE, FALSE);
4925 }
4926 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4927 {
4928 /* Row of frames: Also need to resize frames left and right of this
4929 * one. First check for the minimal height of these. */
4930 h = frame_minheight(curfrp->fr_parent, NULL);
4931 if (height < h)
4932 height = h;
4933 frame_setheight(curfrp->fr_parent, height);
4934 }
4935 else
4936 {
4937 /*
4938 * Column of frames: try to change only frames in this column.
4939 */
4940#ifdef FEAT_VERTSPLIT
4941 /*
4942 * Do this twice:
4943 * 1: compute room available, if it's not enough try resizing the
4944 * containing frame.
4945 * 2: compute the room available and adjust the height to it.
4946 * Try not to reduce the height of a window with 'winfixheight' set.
4947 */
4948 for (run = 1; run <= 2; ++run)
4949#else
4950 for (;;)
4951#endif
4952 {
4953 room = 0;
4954 room_reserved = 0;
4955 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4956 frp = frp->fr_next)
4957 {
4958 if (frp != curfrp
4959 && frp->fr_win != NULL
4960 && frp->fr_win->w_p_wfh)
4961 room_reserved += frp->fr_height;
4962 room += frp->fr_height;
4963 if (frp != curfrp)
4964 room -= frame_minheight(frp, NULL);
4965 }
4966#ifdef FEAT_VERTSPLIT
4967 if (curfrp->fr_width != Columns)
4968 room_cmdline = 0;
4969 else
4970#endif
4971 {
4972 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4973 + lastwin->w_height + lastwin->w_status_height);
4974 if (room_cmdline < 0)
4975 room_cmdline = 0;
4976 }
4977
4978 if (height <= room + room_cmdline)
4979 break;
4980#ifdef FEAT_VERTSPLIT
4981 if (run == 2 || curfrp->fr_width == Columns)
4982#endif
4983 {
4984 if (height > room + room_cmdline)
4985 height = room + room_cmdline;
4986 break;
4987 }
4988#ifdef FEAT_VERTSPLIT
4989 frame_setheight(curfrp->fr_parent, height
4990 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4991#endif
4992 /*NOTREACHED*/
4993 }
4994
4995 /*
4996 * Compute the number of lines we will take from others frames (can be
4997 * negative!).
4998 */
4999 take = height - curfrp->fr_height;
5000
5001 /* If there is not enough room, also reduce the height of a window
5002 * with 'winfixheight' set. */
5003 if (height > room + room_cmdline - room_reserved)
5004 room_reserved = room + room_cmdline - height;
5005 /* If there is only a 'winfixheight' window and making the
5006 * window smaller, need to make the other window taller. */
5007 if (take < 0 && room - curfrp->fr_height < room_reserved)
5008 room_reserved = 0;
5009
5010 if (take > 0 && room_cmdline > 0)
5011 {
5012 /* use lines from cmdline first */
5013 if (take < room_cmdline)
5014 room_cmdline = take;
5015 take -= room_cmdline;
5016 topframe->fr_height += room_cmdline;
5017 }
5018
5019 /*
5020 * set the current frame to the new height
5021 */
5022 frame_new_height(curfrp, height, FALSE, FALSE);
5023
5024 /*
5025 * First take lines from the frames after the current frame. If
5026 * that is not enough, takes lines from frames above the current
5027 * frame.
5028 */
5029 for (run = 0; run < 2; ++run)
5030 {
5031 if (run == 0)
5032 frp = curfrp->fr_next; /* 1st run: start with next window */
5033 else
5034 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5035 while (frp != NULL && take != 0)
5036 {
5037 h = frame_minheight(frp, NULL);
5038 if (room_reserved > 0
5039 && frp->fr_win != NULL
5040 && frp->fr_win->w_p_wfh)
5041 {
5042 if (room_reserved >= frp->fr_height)
5043 room_reserved -= frp->fr_height;
5044 else
5045 {
5046 if (frp->fr_height - room_reserved > take)
5047 room_reserved = frp->fr_height - take;
5048 take -= frp->fr_height - room_reserved;
5049 frame_new_height(frp, room_reserved, FALSE, FALSE);
5050 room_reserved = 0;
5051 }
5052 }
5053 else
5054 {
5055 if (frp->fr_height - take < h)
5056 {
5057 take -= frp->fr_height - h;
5058 frame_new_height(frp, h, FALSE, FALSE);
5059 }
5060 else
5061 {
5062 frame_new_height(frp, frp->fr_height - take,
5063 FALSE, FALSE);
5064 take = 0;
5065 }
5066 }
5067 if (run == 0)
5068 frp = frp->fr_next;
5069 else
5070 frp = frp->fr_prev;
5071 }
5072 }
5073 }
5074}
5075
5076#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5077/*
5078 * Set current window width and take care of repositioning other windows to
5079 * fit around it.
5080 */
5081 void
5082win_setwidth(width)
5083 int width;
5084{
5085 win_setwidth_win(width, curwin);
5086}
5087
5088 void
5089win_setwidth_win(width, wp)
5090 int width;
5091 win_T *wp;
5092{
5093 /* Always keep current window at least one column wide, even when
5094 * 'winminwidth' is zero. */
5095 if (wp == curwin)
5096 {
5097 if (width < p_wmw)
5098 width = p_wmw;
5099 if (width == 0)
5100 width = 1;
5101 }
5102
5103 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5104
5105 /* recompute the window positions */
5106 (void)win_comp_pos();
5107
5108 redraw_all_later(NOT_VALID);
5109}
5110
5111/*
5112 * Set the width of a frame to "width" and take care that all frames and
5113 * windows inside it are resized. Also resize frames above and below if the
5114 * are in the same FR_ROW frame.
5115 *
5116 * Strategy is similar to frame_setheight().
5117 */
5118 static void
5119frame_setwidth(curfrp, width)
5120 frame_T *curfrp;
5121 int width;
5122{
5123 int room; /* total number of lines available */
5124 int take; /* number of lines taken from other windows */
5125 int run;
5126 frame_T *frp;
5127 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005128 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
5130 /* If the width already is the desired value, nothing to do. */
5131 if (curfrp->fr_width == width)
5132 return;
5133
5134 if (curfrp->fr_parent == NULL)
5135 /* topframe: can't change width */
5136 return;
5137
5138 if (curfrp->fr_parent->fr_layout == FR_COL)
5139 {
5140 /* Column of frames: Also need to resize frames above and below of
5141 * this one. First check for the minimal width of these. */
5142 w = frame_minwidth(curfrp->fr_parent, NULL);
5143 if (width < w)
5144 width = w;
5145 frame_setwidth(curfrp->fr_parent, width);
5146 }
5147 else
5148 {
5149 /*
5150 * Row of frames: try to change only frames in this row.
5151 *
5152 * Do this twice:
5153 * 1: compute room available, if it's not enough try resizing the
5154 * containing frame.
5155 * 2: compute the room available and adjust the width to it.
5156 */
5157 for (run = 1; run <= 2; ++run)
5158 {
5159 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005160 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5162 frp = frp->fr_next)
5163 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005164 if (frp != curfrp
5165 && frp->fr_win != NULL
5166 && frp->fr_win->w_p_wfw)
5167 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 room += frp->fr_width;
5169 if (frp != curfrp)
5170 room -= frame_minwidth(frp, NULL);
5171 }
5172
5173 if (width <= room)
5174 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005175 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 if (width > room)
5178 width = room;
5179 break;
5180 }
5181 frame_setwidth(curfrp->fr_parent, width
5182 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5183 }
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 /*
5186 * Compute the number of lines we will take from others frames (can be
5187 * negative!).
5188 */
5189 take = width - curfrp->fr_width;
5190
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005191 /* If there is not enough room, also reduce the width of a window
5192 * with 'winfixwidth' set. */
5193 if (width > room - room_reserved)
5194 room_reserved = room - width;
5195 /* If there is only a 'winfixwidth' window and making the
5196 * window smaller, need to make the other window narrower. */
5197 if (take < 0 && room - curfrp->fr_width < room_reserved)
5198 room_reserved = 0;
5199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 /*
5201 * set the current frame to the new width
5202 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005203 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205 /*
5206 * First take lines from the frames right of the current frame. If
5207 * that is not enough, takes lines from frames left of the current
5208 * frame.
5209 */
5210 for (run = 0; run < 2; ++run)
5211 {
5212 if (run == 0)
5213 frp = curfrp->fr_next; /* 1st run: start with next window */
5214 else
5215 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5216 while (frp != NULL && take != 0)
5217 {
5218 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005219 if (room_reserved > 0
5220 && frp->fr_win != NULL
5221 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005223 if (room_reserved >= frp->fr_width)
5224 room_reserved -= frp->fr_width;
5225 else
5226 {
5227 if (frp->fr_width - room_reserved > take)
5228 room_reserved = frp->fr_width - take;
5229 take -= frp->fr_width - room_reserved;
5230 frame_new_width(frp, room_reserved, FALSE, FALSE);
5231 room_reserved = 0;
5232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234 else
5235 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005236 if (frp->fr_width - take < w)
5237 {
5238 take -= frp->fr_width - w;
5239 frame_new_width(frp, w, FALSE, FALSE);
5240 }
5241 else
5242 {
5243 frame_new_width(frp, frp->fr_width - take,
5244 FALSE, FALSE);
5245 take = 0;
5246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 if (run == 0)
5249 frp = frp->fr_next;
5250 else
5251 frp = frp->fr_prev;
5252 }
5253 }
5254 }
5255}
5256#endif /* FEAT_VERTSPLIT */
5257
5258/*
5259 * Check 'winminheight' for a valid value.
5260 */
5261 void
5262win_setminheight()
5263{
5264 int room;
5265 int first = TRUE;
5266 win_T *wp;
5267
5268 /* loop until there is a 'winminheight' that is possible */
5269 while (p_wmh > 0)
5270 {
5271 /* TODO: handle vertical splits */
5272 room = -p_wh;
5273 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5274 room += wp->w_height - p_wmh;
5275 if (room >= 0)
5276 break;
5277 --p_wmh;
5278 if (first)
5279 {
5280 EMSG(_(e_noroom));
5281 first = FALSE;
5282 }
5283 }
5284}
5285
5286#ifdef FEAT_MOUSE
5287
5288/*
5289 * Status line of dragwin is dragged "offset" lines down (negative is up).
5290 */
5291 void
5292win_drag_status_line(dragwin, offset)
5293 win_T *dragwin;
5294 int offset;
5295{
5296 frame_T *curfr;
5297 frame_T *fr;
5298 int room;
5299 int row;
5300 int up; /* if TRUE, drag status line up, otherwise down */
5301 int n;
5302
5303 fr = dragwin->w_frame;
5304 curfr = fr;
5305 if (fr != topframe) /* more than one window */
5306 {
5307 fr = fr->fr_parent;
5308 /* When the parent frame is not a column of frames, its parent should
5309 * be. */
5310 if (fr->fr_layout != FR_COL)
5311 {
5312 curfr = fr;
5313 if (fr != topframe) /* only a row of windows, may drag statusline */
5314 fr = fr->fr_parent;
5315 }
5316 }
5317
5318 /* If this is the last frame in a column, may want to resize the parent
5319 * frame instead (go two up to skip a row of frames). */
5320 while (curfr != topframe && curfr->fr_next == NULL)
5321 {
5322 if (fr != topframe)
5323 fr = fr->fr_parent;
5324 curfr = fr;
5325 if (fr != topframe)
5326 fr = fr->fr_parent;
5327 }
5328
5329 if (offset < 0) /* drag up */
5330 {
5331 up = TRUE;
5332 offset = -offset;
5333 /* sum up the room of the current frame and above it */
5334 if (fr == curfr)
5335 {
5336 /* only one window */
5337 room = fr->fr_height - frame_minheight(fr, NULL);
5338 }
5339 else
5340 {
5341 room = 0;
5342 for (fr = fr->fr_child; ; fr = fr->fr_next)
5343 {
5344 room += fr->fr_height - frame_minheight(fr, NULL);
5345 if (fr == curfr)
5346 break;
5347 }
5348 }
5349 fr = curfr->fr_next; /* put fr at frame that grows */
5350 }
5351 else /* drag down */
5352 {
5353 up = FALSE;
5354 /*
5355 * Only dragging the last status line can reduce p_ch.
5356 */
5357 room = Rows - cmdline_row;
5358 if (curfr->fr_next == NULL)
5359 room -= 1;
5360 else
5361 room -= p_ch;
5362 if (room < 0)
5363 room = 0;
5364 /* sum up the room of frames below of the current one */
5365 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5366 room += fr->fr_height - frame_minheight(fr, NULL);
5367 fr = curfr; /* put fr at window that grows */
5368 }
5369
5370 if (room < offset) /* Not enough room */
5371 offset = room; /* Move as far as we can */
5372 if (offset <= 0)
5373 return;
5374
5375 /*
5376 * Grow frame fr by "offset" lines.
5377 * Doesn't happen when dragging the last status line up.
5378 */
5379 if (fr != NULL)
5380 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5381
5382 if (up)
5383 fr = curfr; /* current frame gets smaller */
5384 else
5385 fr = curfr->fr_next; /* next frame gets smaller */
5386
5387 /*
5388 * Now make the other frames smaller.
5389 */
5390 while (fr != NULL && offset > 0)
5391 {
5392 n = frame_minheight(fr, NULL);
5393 if (fr->fr_height - offset <= n)
5394 {
5395 offset -= fr->fr_height - n;
5396 frame_new_height(fr, n, !up, FALSE);
5397 }
5398 else
5399 {
5400 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5401 break;
5402 }
5403 if (up)
5404 fr = fr->fr_prev;
5405 else
5406 fr = fr->fr_next;
5407 }
5408 row = win_comp_pos();
5409 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5410 cmdline_row = row;
5411 p_ch = Rows - cmdline_row;
5412 if (p_ch < 1)
5413 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005414 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005415 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 showmode();
5417}
5418
5419#ifdef FEAT_VERTSPLIT
5420/*
5421 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5422 */
5423 void
5424win_drag_vsep_line(dragwin, offset)
5425 win_T *dragwin;
5426 int offset;
5427{
5428 frame_T *curfr;
5429 frame_T *fr;
5430 int room;
5431 int left; /* if TRUE, drag separator line left, otherwise right */
5432 int n;
5433
5434 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005435 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 return;
5437 curfr = fr;
5438 fr = fr->fr_parent;
5439 /* When the parent frame is not a row of frames, its parent should be. */
5440 if (fr->fr_layout != FR_ROW)
5441 {
5442 if (fr == topframe) /* only a column of windows (cannot happen?) */
5443 return;
5444 curfr = fr;
5445 fr = fr->fr_parent;
5446 }
5447
5448 /* If this is the last frame in a row, may want to resize a parent
5449 * frame instead. */
5450 while (curfr->fr_next == NULL)
5451 {
5452 if (fr == topframe)
5453 break;
5454 curfr = fr;
5455 fr = fr->fr_parent;
5456 if (fr != topframe)
5457 {
5458 curfr = fr;
5459 fr = fr->fr_parent;
5460 }
5461 }
5462
5463 if (offset < 0) /* drag left */
5464 {
5465 left = TRUE;
5466 offset = -offset;
5467 /* sum up the room of the current frame and left of it */
5468 room = 0;
5469 for (fr = fr->fr_child; ; fr = fr->fr_next)
5470 {
5471 room += fr->fr_width - frame_minwidth(fr, NULL);
5472 if (fr == curfr)
5473 break;
5474 }
5475 fr = curfr->fr_next; /* put fr at frame that grows */
5476 }
5477 else /* drag right */
5478 {
5479 left = FALSE;
5480 /* sum up the room of frames right of the current one */
5481 room = 0;
5482 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5483 room += fr->fr_width - frame_minwidth(fr, NULL);
5484 fr = curfr; /* put fr at window that grows */
5485 }
5486
5487 if (room < offset) /* Not enough room */
5488 offset = room; /* Move as far as we can */
5489 if (offset <= 0) /* No room at all, quit. */
5490 return;
5491
5492 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005493 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 /* shrink other frames: current and at the left or at the right */
5496 if (left)
5497 fr = curfr; /* current frame gets smaller */
5498 else
5499 fr = curfr->fr_next; /* next frame gets smaller */
5500
5501 while (fr != NULL && offset > 0)
5502 {
5503 n = frame_minwidth(fr, NULL);
5504 if (fr->fr_width - offset <= n)
5505 {
5506 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005507 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 }
5509 else
5510 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005511 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 break;
5513 }
5514 if (left)
5515 fr = fr->fr_prev;
5516 else
5517 fr = fr->fr_next;
5518 }
5519 (void)win_comp_pos();
5520 redraw_all_later(NOT_VALID);
5521}
5522#endif /* FEAT_VERTSPLIT */
5523#endif /* FEAT_MOUSE */
5524
5525#endif /* FEAT_WINDOWS */
5526
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005527#define FRACTION_MULT 16384L
5528
5529/*
5530 * Set wp->w_fraction for the current w_wrow and w_height.
5531 */
5532 static void
5533set_fraction(wp)
5534 win_T *wp;
5535{
5536 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5537 + FRACTION_MULT / 2) / (long)wp->w_height;
5538}
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540/*
5541 * Set the height of a window.
5542 * This takes care of the things inside the window, not what happens to the
5543 * window position, the frame or to other windows.
5544 */
5545 static void
5546win_new_height(wp, height)
5547 win_T *wp;
5548 int height;
5549{
5550 linenr_T lnum;
5551 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 /* Don't want a negative height. Happens when splitting a tiny window.
5554 * Will equalize heights soon to fix it. */
5555 if (height < 0)
5556 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005557 if (wp->w_height == height)
5558 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005561 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 wp->w_height = height;
5564 wp->w_skipcol = 0;
5565
5566 /* Don't change w_topline when height is zero. Don't set w_topline when
5567 * 'scrollbind' is set and this isn't the current window. */
5568 if (height > 0
5569#ifdef FEAT_SCROLLBIND
5570 && (!wp->w_p_scb || wp == curwin)
5571#endif
5572 )
5573 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005574 /*
5575 * Find a value for w_topline that shows the cursor at the same
5576 * relative position in the window as before (more or less).
5577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 lnum = wp->w_cursor.lnum;
5579 if (lnum < 1) /* can happen when starting up */
5580 lnum = 1;
5581 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5582 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5583 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005584
5585 if (sline >= 0)
5586 {
5587 /* Make sure the whole cursor line is visible, if possible. */
5588 int rows = plines_win(wp, lnum, FALSE);
5589
5590 if (sline > wp->w_height - rows)
5591 {
5592 sline = wp->w_height - rows;
5593 wp->w_wrow -= rows - line_size;
5594 }
5595 }
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (sline < 0)
5598 {
5599 /*
5600 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005601 * Make cursor line the first line in the window. If not enough
5602 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 */
5604 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005605 if (wp->w_wrow >= wp->w_height
5606 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5607 {
5608 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5609 --wp->w_wrow;
5610 while (wp->w_wrow >= wp->w_height)
5611 {
5612 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5613 + win_col_off2(wp);
5614 --wp->w_wrow;
5615 }
5616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618 else
5619 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005620 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622#ifdef FEAT_FOLDING
5623 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5624 if (lnum == 1)
5625 {
5626 /* first line in buffer is folded */
5627 line_size = 1;
5628 --sline;
5629 break;
5630 }
5631#endif
5632 --lnum;
5633#ifdef FEAT_DIFF
5634 if (lnum == wp->w_topline)
5635 line_size = plines_win_nofill(wp, lnum, TRUE)
5636 + wp->w_topfill;
5637 else
5638#endif
5639 line_size = plines_win(wp, lnum, TRUE);
5640 sline -= line_size;
5641 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 if (sline < 0)
5644 {
5645 /*
5646 * Line we want at top would go off top of screen. Use next
5647 * line instead.
5648 */
5649#ifdef FEAT_FOLDING
5650 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5651#endif
5652 lnum++;
5653 wp->w_wrow -= line_size + sline;
5654 }
5655 else if (sline > 0)
5656 {
5657 /* First line of file reached, use that as topline. */
5658 lnum = 1;
5659 wp->w_wrow -= sline;
5660 }
5661 }
5662 set_topline(wp, lnum);
5663 }
5664
5665 if (wp == curwin)
5666 {
5667 if (p_so)
5668 update_topline();
5669 curs_columns(FALSE); /* validate w_wrow */
5670 }
5671 wp->w_prev_fraction_row = wp->w_wrow;
5672
5673 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005674 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#ifdef FEAT_WINDOWS
5676 wp->w_redr_status = TRUE;
5677#endif
5678 invalidate_botline_win(wp);
5679}
5680
5681#ifdef FEAT_VERTSPLIT
5682/*
5683 * Set the width of a window.
5684 */
5685 static void
5686win_new_width(wp, width)
5687 win_T *wp;
5688 int width;
5689{
5690 wp->w_width = width;
5691 wp->w_lines_valid = 0;
5692 changed_line_abv_curs_win(wp);
5693 invalidate_botline_win(wp);
5694 if (wp == curwin)
5695 {
5696 update_topline();
5697 curs_columns(TRUE); /* validate w_wrow */
5698 }
5699 redraw_win_later(wp, NOT_VALID);
5700 wp->w_redr_status = TRUE;
5701}
5702#endif
5703
5704 void
5705win_comp_scroll(wp)
5706 win_T *wp;
5707{
5708 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5709 if (wp->w_p_scr == 0)
5710 wp->w_p_scr = 1;
5711}
5712
5713/*
5714 * command_height: called whenever p_ch has been changed
5715 */
5716 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005717command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719#ifdef FEAT_WINDOWS
5720 int h;
5721 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005722 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005724 /* Use the value of p_ch that we remembered. This is needed for when the
5725 * GUI starts up, we can't be sure in what order things happen. And when
5726 * p_ch was changed in another tab page. */
5727 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 /* Find bottom frame with width of screen. */
5730 frp = lastwin->w_frame;
5731# ifdef FEAT_VERTSPLIT
5732 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5733 frp = frp->fr_parent;
5734# endif
5735
5736 /* Avoid changing the height of a window with 'winfixheight' set. */
5737 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5738 && frp->fr_win->w_p_wfh)
5739 frp = frp->fr_prev;
5740
5741 if (starting != NO_SCREEN)
5742 {
5743 cmdline_row = Rows - p_ch;
5744
5745 if (p_ch > old_p_ch) /* p_ch got bigger */
5746 {
5747 while (p_ch > old_p_ch)
5748 {
5749 if (frp == NULL)
5750 {
5751 EMSG(_(e_noroom));
5752 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005753 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 cmdline_row = Rows - p_ch;
5755 break;
5756 }
5757 h = frp->fr_height - frame_minheight(frp, NULL);
5758 if (h > p_ch - old_p_ch)
5759 h = p_ch - old_p_ch;
5760 old_p_ch += h;
5761 frame_add_height(frp, -h);
5762 frp = frp->fr_prev;
5763 }
5764
5765 /* Recompute window positions. */
5766 (void)win_comp_pos();
5767
5768 /* clear the lines added to cmdline */
5769 if (full_screen)
5770 screen_fill((int)(cmdline_row), (int)Rows, 0,
5771 (int)Columns, ' ', ' ', 0);
5772 msg_row = cmdline_row;
5773 redraw_cmdline = TRUE;
5774 return;
5775 }
5776
5777 if (msg_row < cmdline_row)
5778 msg_row = cmdline_row;
5779 redraw_cmdline = TRUE;
5780 }
5781 frame_add_height(frp, (int)(old_p_ch - p_ch));
5782
5783 /* Recompute window positions. */
5784 if (frp != lastwin->w_frame)
5785 (void)win_comp_pos();
5786#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005788 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#endif
5790}
5791
5792#if defined(FEAT_WINDOWS) || defined(PROTO)
5793/*
5794 * Resize frame "frp" to be "n" lines higher (negative for less high).
5795 * Also resize the frames it is contained in.
5796 */
5797 static void
5798frame_add_height(frp, n)
5799 frame_T *frp;
5800 int n;
5801{
5802 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5803 for (;;)
5804 {
5805 frp = frp->fr_parent;
5806 if (frp == NULL)
5807 break;
5808 frp->fr_height += n;
5809 }
5810}
5811
5812/*
5813 * Add or remove a status line for the bottom window(s), according to the
5814 * value of 'laststatus'.
5815 */
5816 void
5817last_status(morewin)
5818 int morewin; /* pretend there are two or more windows */
5819{
5820 /* Don't make a difference between horizontal or vertical split. */
5821 last_status_rec(topframe, (p_ls == 2
5822 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5823}
5824
5825 static void
5826last_status_rec(fr, statusline)
5827 frame_T *fr;
5828 int statusline;
5829{
5830 frame_T *fp;
5831 win_T *wp;
5832
5833 if (fr->fr_layout == FR_LEAF)
5834 {
5835 wp = fr->fr_win;
5836 if (wp->w_status_height != 0 && !statusline)
5837 {
5838 /* remove status line */
5839 win_new_height(wp, wp->w_height + 1);
5840 wp->w_status_height = 0;
5841 comp_col();
5842 }
5843 else if (wp->w_status_height == 0 && statusline)
5844 {
5845 /* Find a frame to take a line from. */
5846 fp = fr;
5847 while (fp->fr_height <= frame_minheight(fp, NULL))
5848 {
5849 if (fp == topframe)
5850 {
5851 EMSG(_(e_noroom));
5852 return;
5853 }
5854 /* In a column of frames: go to frame above. If already at
5855 * the top or in a row of frames: go to parent. */
5856 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5857 fp = fp->fr_prev;
5858 else
5859 fp = fp->fr_parent;
5860 }
5861 wp->w_status_height = 1;
5862 if (fp != fr)
5863 {
5864 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5865 frame_fix_height(wp);
5866 (void)win_comp_pos();
5867 }
5868 else
5869 win_new_height(wp, wp->w_height - 1);
5870 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005871 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
5873 }
5874#ifdef FEAT_VERTSPLIT
5875 else if (fr->fr_layout == FR_ROW)
5876 {
5877 /* vertically split windows, set status line for each one */
5878 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5879 last_status_rec(fp, statusline);
5880 }
5881#endif
5882 else
5883 {
5884 /* horizontally split window, set status line for last one */
5885 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5886 ;
5887 last_status_rec(fp, statusline);
5888 }
5889}
5890
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005892 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893 */
5894 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005895tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005896{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005897#ifdef FEAT_GUI_TABLINE
5898 /* When the GUI has the tabline then this always returns zero. */
5899 if (gui_use_tabline())
5900 return 0;
5901#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005903 {
5904 case 0: return 0;
5905 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5906 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 return 1;
5908}
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif /* FEAT_WINDOWS */
5911
5912#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5913/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005914 * Get the file name at the cursor.
5915 * If Visual mode is active, use the selected text if it's in one line.
5916 * Returns the name in allocated memory, NULL for failure.
5917 */
5918 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005919grab_file_name(count, file_lnum)
5920 long count;
5921 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005922{
5923# ifdef FEAT_VISUAL
5924 if (VIsual_active)
5925 {
5926 int len;
5927 char_u *ptr;
5928
5929 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5930 return NULL;
5931 return find_file_name_in_path(ptr, len,
5932 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5933 }
5934# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005935 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5936 file_lnum);
5937
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005938}
5939
5940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 * Return the file name under or after the cursor.
5942 *
5943 * The 'path' option is searched if the file name is not absolute.
5944 * The string returned has been alloc'ed and should be freed by the caller.
5945 * NULL is returned if the file name or file is not found.
5946 *
5947 * options:
5948 * FNAME_MESS give error messages
5949 * FNAME_EXP expand to path
5950 * FNAME_HYP check for hypertext link
5951 * FNAME_INCL apply "includeexpr"
5952 */
5953 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005954file_name_at_cursor(options, count, file_lnum)
5955 int options;
5956 long count;
5957 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
5959 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005960 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5961 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962}
5963
5964/*
5965 * Return the name of the file under or after ptr[col].
5966 * Otherwise like file_name_at_cursor().
5967 */
5968 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005969file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 char_u *line;
5971 int col;
5972 int options;
5973 long count;
5974 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005975 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 char_u *ptr;
5978 int len;
5979
5980 /*
5981 * search forward for what could be the start of a file name
5982 */
5983 ptr = line + col;
5984 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005985 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 if (*ptr == NUL) /* nothing found */
5987 {
5988 if (options & FNAME_MESS)
5989 EMSG(_("E446: No file name under cursor"));
5990 return NULL;
5991 }
5992
5993 /*
5994 * Search backward for first char of the file name.
5995 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5996 */
5997 while (ptr > line)
5998 {
5999#ifdef FEAT_MBYTE
6000 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6001 ptr -= len + 1;
6002 else
6003#endif
6004 if (vim_isfilec(ptr[-1])
6005 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6006 --ptr;
6007 else
6008 break;
6009 }
6010
6011 /*
6012 * Search forward for the last char of the file name.
6013 * Also allow "://" when ':' is not in 'isfname'.
6014 */
6015 len = 0;
6016 while (vim_isfilec(ptr[len])
6017 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6018#ifdef FEAT_MBYTE
6019 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006020 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 else
6022#endif
6023 ++len;
6024
6025 /*
6026 * If there is trailing punctuation, remove it.
6027 * But don't remove "..", could be a directory name.
6028 */
6029 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6030 && ptr[len - 2] != '.')
6031 --len;
6032
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006033 if (file_lnum != NULL)
6034 {
6035 char_u *p;
6036
6037 /* Get the number after the file name and a separator character */
6038 p = ptr + len;
6039 p = skipwhite(p);
6040 if (*p != NUL)
6041 {
6042 if (!isdigit(*p))
6043 ++p; /* skip the separator */
6044 p = skipwhite(p);
6045 if (isdigit(*p))
6046 *file_lnum = (int)getdigits(&p);
6047 }
6048 }
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6051}
6052
6053# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6054static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6055
6056 static char_u *
6057eval_includeexpr(ptr, len)
6058 char_u *ptr;
6059 int len;
6060{
6061 char_u *res;
6062
6063 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006064 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006065 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 set_vim_var_string(VV_FNAME, NULL, 0);
6067 return res;
6068}
6069#endif
6070
6071/*
6072 * Return the name of the file ptr[len] in 'path'.
6073 * Otherwise like file_name_at_cursor().
6074 */
6075 char_u *
6076find_file_name_in_path(ptr, len, options, count, rel_fname)
6077 char_u *ptr;
6078 int len;
6079 int options;
6080 long count;
6081 char_u *rel_fname; /* file we are searching relative to */
6082{
6083 char_u *file_name;
6084 int c;
6085# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6086 char_u *tofree = NULL;
6087
6088 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6089 {
6090 tofree = eval_includeexpr(ptr, len);
6091 if (tofree != NULL)
6092 {
6093 ptr = tofree;
6094 len = (int)STRLEN(ptr);
6095 }
6096 }
6097# endif
6098
6099 if (options & FNAME_EXP)
6100 {
6101 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6102 TRUE, rel_fname);
6103
6104# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6105 /*
6106 * If the file could not be found in a normal way, try applying
6107 * 'includeexpr' (unless done already).
6108 */
6109 if (file_name == NULL
6110 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6111 {
6112 tofree = eval_includeexpr(ptr, len);
6113 if (tofree != NULL)
6114 {
6115 ptr = tofree;
6116 len = (int)STRLEN(ptr);
6117 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6118 TRUE, rel_fname);
6119 }
6120 }
6121# endif
6122 if (file_name == NULL && (options & FNAME_MESS))
6123 {
6124 c = ptr[len];
6125 ptr[len] = NUL;
6126 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6127 ptr[len] = c;
6128 }
6129
6130 /* Repeat finding the file "count" times. This matters when it
6131 * appears several times in the path. */
6132 while (file_name != NULL && --count > 0)
6133 {
6134 vim_free(file_name);
6135 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6136 }
6137 }
6138 else
6139 file_name = vim_strnsave(ptr, len);
6140
6141# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6142 vim_free(tofree);
6143# endif
6144
6145 return file_name;
6146}
6147#endif /* FEAT_SEARCHPATH */
6148
6149/*
6150 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6151 * Also check for ":\\", which MS Internet Explorer accepts, return
6152 * URL_BACKSLASH.
6153 */
6154 static int
6155path_is_url(p)
6156 char_u *p;
6157{
6158 if (STRNCMP(p, "://", (size_t)3) == 0)
6159 return URL_SLASH;
6160 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6161 return URL_BACKSLASH;
6162 return 0;
6163}
6164
6165/*
6166 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6167 * Return URL_BACKSLASH for "name:\\".
6168 * Return zero otherwise.
6169 */
6170 int
6171path_with_url(fname)
6172 char_u *fname;
6173{
6174 char_u *p;
6175
6176 for (p = fname; isalpha(*p); ++p)
6177 ;
6178 return path_is_url(p);
6179}
6180
6181/*
6182 * Return TRUE if "name" is a full (absolute) path name or URL.
6183 */
6184 int
6185vim_isAbsName(name)
6186 char_u *name;
6187{
6188 return (path_with_url(name) != 0 || mch_isFullName(name));
6189}
6190
6191/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006192 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 *
6194 * return FAIL for failure, OK otherwise
6195 */
6196 int
6197vim_FullName(fname, buf, len, force)
6198 char_u *fname, *buf;
6199 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006200 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 int retval = OK;
6203 int url;
6204
6205 *buf = NUL;
6206 if (fname == NULL)
6207 return FAIL;
6208
6209 url = path_with_url(fname);
6210 if (!url)
6211 retval = mch_FullName(fname, buf, len, force);
6212 if (url || retval == FAIL)
6213 {
6214 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006215 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6218 slash_adjust(buf);
6219#endif
6220 return retval;
6221}
6222
6223/*
6224 * Return the minimal number of rows that is needed on the screen to display
6225 * the current number of windows.
6226 */
6227 int
6228min_rows()
6229{
6230 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006231#ifdef FEAT_WINDOWS
6232 tabpage_T *tp;
6233 int n;
6234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
6236 if (firstwin == NULL) /* not initialized yet */
6237 return MIN_LINES;
6238
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006240 total = 0;
6241 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6242 {
6243 n = frame_minheight(tp->tp_topframe, NULL);
6244 if (total < n)
6245 total = n;
6246 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006247 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006249 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006251 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 return total;
6253}
6254
6255/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006256 * Return TRUE if there is only one window (in the current tab page), not
6257 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006258 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 */
6260 int
6261only_one_window()
6262{
6263#ifdef FEAT_WINDOWS
6264 int count = 0;
6265 win_T *wp;
6266
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267 /* If there is another tab page there always is another window. */
6268 if (first_tabpage->tp_next != NULL)
6269 return FALSE;
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006272 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273# ifdef FEAT_QUICKFIX
6274 || wp->w_p_pvw
6275# endif
6276 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006277# ifdef FEAT_AUTOCMD
6278 && wp != aucmd_win
6279# endif
6280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ++count;
6282 return (count <= 1);
6283#else
6284 return TRUE;
6285#endif
6286}
6287
6288#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6289/*
6290 * Correct the cursor line number in other windows. Used after changing the
6291 * current buffer, and before applying autocommands.
6292 * When "do_curwin" is TRUE, also check current window.
6293 */
6294 void
6295check_lnums(do_curwin)
6296 int do_curwin;
6297{
6298 win_T *wp;
6299
6300#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301 tabpage_T *tp;
6302
6303 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6305#else
6306 wp = curwin;
6307 if (do_curwin)
6308#endif
6309 {
6310 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6311 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6312 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6313 wp->w_topline = curbuf->b_ml.ml_line_count;
6314 }
6315}
6316#endif
6317
6318#if defined(FEAT_WINDOWS) || defined(PROTO)
6319
6320/*
6321 * A snapshot of the window sizes, to restore them after closing the help
6322 * window.
6323 * Only these fields are used:
6324 * fr_layout
6325 * fr_width
6326 * fr_height
6327 * fr_next
6328 * fr_child
6329 * fr_win (only valid for the old curwin, NULL otherwise)
6330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332/*
6333 * Create a snapshot of the current frame sizes.
6334 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006335 void
6336make_snapshot(idx)
6337 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006339 clear_snapshot(curtab, idx);
6340 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341}
6342
6343 static void
6344make_snapshot_rec(fr, frp)
6345 frame_T *fr;
6346 frame_T **frp;
6347{
6348 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6349 if (*frp == NULL)
6350 return;
6351 (*frp)->fr_layout = fr->fr_layout;
6352# ifdef FEAT_VERTSPLIT
6353 (*frp)->fr_width = fr->fr_width;
6354# endif
6355 (*frp)->fr_height = fr->fr_height;
6356 if (fr->fr_next != NULL)
6357 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6358 if (fr->fr_child != NULL)
6359 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6360 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6361 (*frp)->fr_win = curwin;
6362}
6363
6364/*
6365 * Remove any existing snapshot.
6366 */
6367 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006368clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006369 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006370 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006372 clear_snapshot_rec(tp->tp_snapshot[idx]);
6373 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
6375
6376 static void
6377clear_snapshot_rec(fr)
6378 frame_T *fr;
6379{
6380 if (fr != NULL)
6381 {
6382 clear_snapshot_rec(fr->fr_next);
6383 clear_snapshot_rec(fr->fr_child);
6384 vim_free(fr);
6385 }
6386}
6387
6388/*
6389 * Restore a previously created snapshot, if there is any.
6390 * This is only done if the screen size didn't change and the window layout is
6391 * still the same.
6392 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006393 void
6394restore_snapshot(idx, close_curwin)
6395 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 int close_curwin; /* closing current window */
6397{
6398 win_T *wp;
6399
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006400 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006402 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006404 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6405 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006407 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 win_comp_pos();
6409 if (wp != NULL && close_curwin)
6410 win_goto(wp);
6411 redraw_all_later(CLEAR);
6412 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006413 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416/*
6417 * Check if frames "sn" and "fr" have the same layout, same following frames
6418 * and same children.
6419 */
6420 static int
6421check_snapshot_rec(sn, fr)
6422 frame_T *sn;
6423 frame_T *fr;
6424{
6425 if (sn->fr_layout != fr->fr_layout
6426 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6427 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6428 || (sn->fr_next != NULL
6429 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6430 || (sn->fr_child != NULL
6431 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6432 return FAIL;
6433 return OK;
6434}
6435
6436/*
6437 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6438 * following frames and children.
6439 * Returns a pointer to the old current window, or NULL.
6440 */
6441 static win_T *
6442restore_snapshot_rec(sn, fr)
6443 frame_T *sn;
6444 frame_T *fr;
6445{
6446 win_T *wp = NULL;
6447 win_T *wp2;
6448
6449 fr->fr_height = sn->fr_height;
6450# ifdef FEAT_VERTSPLIT
6451 fr->fr_width = sn->fr_width;
6452# endif
6453 if (fr->fr_layout == FR_LEAF)
6454 {
6455 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6456# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006457 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458# endif
6459 wp = sn->fr_win;
6460 }
6461 if (sn->fr_next != NULL)
6462 {
6463 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6464 if (wp2 != NULL)
6465 wp = wp2;
6466 }
6467 if (sn->fr_child != NULL)
6468 {
6469 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6470 if (wp2 != NULL)
6471 wp = wp2;
6472 }
6473 return wp;
6474}
6475
6476#endif
6477
6478#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6479/*
6480 * Return TRUE if there is any vertically split window.
6481 */
6482 int
6483win_hasvertsplit()
6484{
6485 frame_T *fr;
6486
6487 if (topframe->fr_layout == FR_ROW)
6488 return TRUE;
6489
6490 if (topframe->fr_layout == FR_COL)
6491 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6492 if (fr->fr_layout == FR_ROW)
6493 return TRUE;
6494
6495 return FALSE;
6496}
6497#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006498
6499#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6500/*
6501 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006502 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006503 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6504 * If no particular ID is desired, -1 must be specified for 'id'.
6505 * Return ID of added match, -1 on failure.
6506 */
6507 int
6508match_add(wp, grp, pat, prio, id)
6509 win_T *wp;
6510 char_u *grp;
6511 char_u *pat;
6512 int prio;
6513 int id;
6514{
6515 matchitem_T *cur;
6516 matchitem_T *prev;
6517 matchitem_T *m;
6518 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006519 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006520
6521 if (*grp == NUL || *pat == NUL)
6522 return -1;
6523 if (id < -1 || id == 0)
6524 {
6525 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6526 return -1;
6527 }
6528 if (id != -1)
6529 {
6530 cur = wp->w_match_head;
6531 while (cur != NULL)
6532 {
6533 if (cur->id == id)
6534 {
6535 EMSGN("E801: ID already taken: %ld", id);
6536 return -1;
6537 }
6538 cur = cur->next;
6539 }
6540 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006541 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006542 {
6543 EMSG2(_(e_nogroup), grp);
6544 return -1;
6545 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006546 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006547 {
6548 EMSG2(_(e_invarg2), pat);
6549 return -1;
6550 }
6551
6552 /* Find available match ID. */
6553 while (id == -1)
6554 {
6555 cur = wp->w_match_head;
6556 while (cur != NULL && cur->id != wp->w_next_match_id)
6557 cur = cur->next;
6558 if (cur == NULL)
6559 id = wp->w_next_match_id;
6560 wp->w_next_match_id++;
6561 }
6562
6563 /* Build new match. */
6564 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6565 m->id = id;
6566 m->priority = prio;
6567 m->pattern = vim_strsave(pat);
6568 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006569 m->match.regprog = regprog;
6570 m->match.rmm_ic = FALSE;
6571 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006572
6573 /* Insert new match. The match list is in ascending order with regard to
6574 * the match priorities. */
6575 cur = wp->w_match_head;
6576 prev = cur;
6577 while (cur != NULL && prio >= cur->priority)
6578 {
6579 prev = cur;
6580 cur = cur->next;
6581 }
6582 if (cur == prev)
6583 wp->w_match_head = m;
6584 else
6585 prev->next = m;
6586 m->next = cur;
6587
6588 redraw_later(SOME_VALID);
6589 return id;
6590}
6591
6592/*
6593 * Delete match with ID 'id' in the match list of window 'wp'.
6594 * Print error messages if 'perr' is TRUE.
6595 */
6596 int
6597match_delete(wp, id, perr)
6598 win_T *wp;
6599 int id;
6600 int perr;
6601{
6602 matchitem_T *cur = wp->w_match_head;
6603 matchitem_T *prev = cur;
6604
6605 if (id < 1)
6606 {
6607 if (perr == TRUE)
6608 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6609 id);
6610 return -1;
6611 }
6612 while (cur != NULL && cur->id != id)
6613 {
6614 prev = cur;
6615 cur = cur->next;
6616 }
6617 if (cur == NULL)
6618 {
6619 if (perr == TRUE)
6620 EMSGN("E803: ID not found: %ld", id);
6621 return -1;
6622 }
6623 if (cur == prev)
6624 wp->w_match_head = cur->next;
6625 else
6626 prev->next = cur->next;
6627 vim_free(cur->match.regprog);
6628 vim_free(cur->pattern);
6629 vim_free(cur);
6630 redraw_later(SOME_VALID);
6631 return 0;
6632}
6633
6634/*
6635 * Delete all matches in the match list of window 'wp'.
6636 */
6637 void
6638clear_matches(wp)
6639 win_T *wp;
6640{
6641 matchitem_T *m;
6642
6643 while (wp->w_match_head != NULL)
6644 {
6645 m = wp->w_match_head->next;
6646 vim_free(wp->w_match_head->match.regprog);
6647 vim_free(wp->w_match_head->pattern);
6648 vim_free(wp->w_match_head);
6649 wp->w_match_head = m;
6650 }
6651 redraw_later(SOME_VALID);
6652}
6653
6654/*
6655 * Get match from ID 'id' in window 'wp'.
6656 * Return NULL if match not found.
6657 */
6658 matchitem_T *
6659get_match(wp, id)
6660 win_T *wp;
6661 int id;
6662{
6663 matchitem_T *cur = wp->w_match_head;
6664
6665 while (cur != NULL && cur->id != id)
6666 cur = cur->next;
6667 return cur;
6668}
6669#endif