blob: 427fd1f354ff5fcf2d6846995d64d006efad7040 [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)
14static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar884ae642009-02-22 01:37:59 +000015static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
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 Moolenaarf740b292006-02-16 22:11:02 +000026static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
27static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
28static 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 Moolenaar1d2ba7f2006-02-14 22:29:30 +000044#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000045static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000046static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000047static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void frame_fix_height __ARGS((win_T *wp));
49static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
50static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000051static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000053static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static void frame_append __ARGS((frame_T *after, frame_T *frp));
55static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56static void frame_remove __ARGS((frame_T *frp));
57#ifdef FEAT_VERTSPLIT
58static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static void win_goto_ver __ARGS((int up, long count));
60static void win_goto_hor __ARGS((int left, long count));
61#endif
62static void frame_add_height __ARGS((frame_T *frp, int n));
63static void last_status_rec __ARGS((frame_T *fr, int statusline));
64
65static void make_snapshot __ARGS((void));
66static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000067static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static void clear_snapshot_rec __ARGS((frame_T *fr));
69static void restore_snapshot __ARGS((int close_curwin));
70static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
71static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
72
73#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000074
Bram Moolenaar071d4272004-06-13 20:20:40 +000075static win_T *win_alloc __ARGS((win_T *after));
76static void win_new_height __ARGS((win_T *, int));
77
78#define URL_SLASH 1 /* path_is_url() has found "://" */
79#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
80
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000081#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
Bram Moolenaar05159a02005-02-26 23:04:13 +000083#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000084# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000085#else
86# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000087#endif
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000090
91static char *m_onlyone = N_("Already only one window");
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/*
94 * all CTRL-W window commands are handled here, called from normal_cmd().
95 */
96 void
97do_window(nchar, Prenum, xchar)
98 int nchar;
99 long Prenum;
100 int xchar; /* extra char from ":wincmd gx" or NUL */
101{
102 long Prenum1;
103 win_T *wp;
104#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
105 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000106 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
108#ifdef FEAT_FIND_ID
109 int type = FIND_DEFINE;
110 int len;
111#endif
112 char_u cbuf[40];
113
114 if (Prenum == 0)
115 Prenum1 = 1;
116 else
117 Prenum1 = Prenum;
118
119#ifdef FEAT_CMDWIN
120# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
121#else
122# define CHECK_CMDWIN
123#endif
124
125 switch (nchar)
126 {
127/* split current window in two parts, horizontally */
128 case 'S':
129 case Ctrl_S:
130 case 's':
131 CHECK_CMDWIN
132#ifdef FEAT_VISUAL
133 reset_VIsual_and_resel(); /* stop Visual mode */
134#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000135#ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143#endif
144 win_split((int)Prenum, 0);
145 break;
146
147#ifdef FEAT_VERTSPLIT
148/* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000154# endif
155# ifdef FEAT_QUICKFIX
156 /* When splitting the quickfix window open a new buffer in it,
157 * don't replicate the quickfix buffer. */
158 if (bt_quickfix(curbuf))
159 goto newwindow;
160# endif
161# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 win_split((int)Prenum, WSP_VERT);
165 break;
166#endif
167
168/* split current window and edit alternate file */
169 case Ctrl_HAT:
170 case '^':
171 CHECK_CMDWIN
172#ifdef FEAT_VISUAL
173 reset_VIsual_and_resel(); /* stop Visual mode */
174#endif
175 STRCPY(cbuf, "split #");
176 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000177 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
178 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 do_cmdline_cmd(cbuf);
180 break;
181
182/* open new window */
183 case Ctrl_N:
184 case 'n':
185 CHECK_CMDWIN
186#ifdef FEAT_VISUAL
187 reset_VIsual_and_resel(); /* stop Visual mode */
188#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000189#ifdef FEAT_QUICKFIX
190newwindow:
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000193 /* window height */
194 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 else
196 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000197#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
198 if (nchar == 'v' || nchar == Ctrl_V)
199 STRCAT(cbuf, "v");
200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 STRCAT(cbuf, "new");
202 do_cmdline_cmd(cbuf);
203 break;
204
205/* quit current window */
206 case Ctrl_Q:
207 case 'q':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"quit");
212 break;
213
214/* close current window */
215 case Ctrl_C:
216 case 'c':
217#ifdef FEAT_VISUAL
218 reset_VIsual_and_resel(); /* stop Visual mode */
219#endif
220 do_cmdline_cmd((char_u *)"close");
221 break;
222
223#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
224/* close preview window */
225 case Ctrl_Z:
226 case 'z':
227 CHECK_CMDWIN
228#ifdef FEAT_VISUAL
229 reset_VIsual_and_resel(); /* stop Visual mode */
230#endif
231 do_cmdline_cmd((char_u *)"pclose");
232 break;
233
234/* cursor to preview window */
235 case 'P':
236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
237 if (wp->w_p_pvw)
238 break;
239 if (wp == NULL)
240 EMSG(_("E441: There is no preview window"));
241 else
242 win_goto(wp);
243 break;
244#endif
245
246/* close all but current window */
247 case Ctrl_O:
248 case 'o':
249 CHECK_CMDWIN
250#ifdef FEAT_VISUAL
251 reset_VIsual_and_resel(); /* stop Visual mode */
252#endif
253 do_cmdline_cmd((char_u *)"only");
254 break;
255
256/* cursor to next window with wrap around */
257 case Ctrl_W:
258 case 'w':
259/* cursor to previous window with wrap around */
260 case 'W':
261 CHECK_CMDWIN
262 if (lastwin == firstwin && Prenum != 1) /* just one window */
263 beep_flush();
264 else
265 {
266 if (Prenum) /* go to specified window */
267 {
268 for (wp = firstwin; --Prenum > 0; )
269 {
270 if (wp->w_next == NULL)
271 break;
272 else
273 wp = wp->w_next;
274 }
275 }
276 else
277 {
278 if (nchar == 'W') /* go to previous window */
279 {
280 wp = curwin->w_prev;
281 if (wp == NULL)
282 wp = lastwin; /* wrap around */
283 }
284 else /* go to next window */
285 {
286 wp = curwin->w_next;
287 if (wp == NULL)
288 wp = firstwin; /* wrap around */
289 }
290 }
291 win_goto(wp);
292 }
293 break;
294
295/* cursor to window below */
296 case 'j':
297 case K_DOWN:
298 case Ctrl_J:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(FALSE, Prenum1);
302#else
303 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
304 wp = wp->w_next)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310/* cursor to window above */
311 case 'k':
312 case K_UP:
313 case Ctrl_K:
314 CHECK_CMDWIN
315#ifdef FEAT_VERTSPLIT
316 win_goto_ver(TRUE, Prenum1);
317#else
318 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
319 wp = wp->w_prev)
320 ;
321 win_goto(wp);
322#endif
323 break;
324
325#ifdef FEAT_VERTSPLIT
326/* cursor to left window */
327 case 'h':
328 case K_LEFT:
329 case Ctrl_H:
330 case K_BS:
331 CHECK_CMDWIN
332 win_goto_hor(TRUE, Prenum1);
333 break;
334
335/* cursor to right window */
336 case 'l':
337 case K_RIGHT:
338 case Ctrl_L:
339 CHECK_CMDWIN
340 win_goto_hor(FALSE, Prenum1);
341 break;
342#endif
343
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344/* move window to new tab page */
345 case 'T':
346 if (firstwin == lastwin)
347 MSG(_(m_onlyone));
348 else
349 {
350 tabpage_T *oldtab = curtab;
351 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000352
353 /* First create a new tab with the window, then go back to
354 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000355 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000356 if (win_new_tabpage((int)Prenum) == OK
357 && valid_tabpage(oldtab))
358 {
359 newtab = curtab;
360 goto_tabpage_tp(oldtab);
361 if (curwin == wp)
362 win_close(curwin, FALSE);
363 if (valid_tabpage(newtab))
364 goto_tabpage_tp(newtab);
365 }
366 }
367 break;
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369/* cursor to top-left window */
370 case 't':
371 case Ctrl_T:
372 win_goto(firstwin);
373 break;
374
375/* cursor to bottom-right window */
376 case 'b':
377 case Ctrl_B:
378 win_goto(lastwin);
379 break;
380
381/* cursor to last accessed (previous) window */
382 case 'p':
383 case Ctrl_P:
384 if (prevwin == NULL)
385 beep_flush();
386 else
387 win_goto(prevwin);
388 break;
389
390/* exchange current and next window */
391 case 'x':
392 case Ctrl_X:
393 CHECK_CMDWIN
394 win_exchange(Prenum);
395 break;
396
397/* rotate windows downwards */
398 case Ctrl_R:
399 case 'r':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(FALSE, (int)Prenum1); /* downwards */
405 break;
406
407/* rotate windows upwards */
408 case 'R':
409 CHECK_CMDWIN
410#ifdef FEAT_VISUAL
411 reset_VIsual_and_resel(); /* stop Visual mode */
412#endif
413 win_rotate(TRUE, (int)Prenum1); /* upwards */
414 break;
415
416/* move window to the very top/bottom/left/right */
417 case 'K':
418 case 'J':
419#ifdef FEAT_VERTSPLIT
420 case 'H':
421 case 'L':
422#endif
423 CHECK_CMDWIN
424 win_totop((int)Prenum,
425 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
426 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
427 break;
428
429/* make all windows the same height */
430 case '=':
431#ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433#endif
434 win_equal(NULL, FALSE, 'b');
435 break;
436
437/* increase current window height */
438 case '+':
439#ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441#endif
442 win_setheight(curwin->w_height + (int)Prenum1);
443 break;
444
445/* decrease current window height */
446 case '-':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(curwin->w_height - (int)Prenum1);
451 break;
452
453/* set current window height */
454 case Ctrl__:
455 case '_':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setheight(Prenum ? (int)Prenum : 9999);
460 break;
461
462#ifdef FEAT_VERTSPLIT
463/* increase current window width */
464 case '>':
465#ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467#endif
468 win_setwidth(curwin->w_width + (int)Prenum1);
469 break;
470
471/* decrease current window width */
472 case '<':
473#ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475#endif
476 win_setwidth(curwin->w_width - (int)Prenum1);
477 break;
478
479/* set current window width */
480 case '|':
481#ifdef FEAT_GUI
482 need_mouse_correct = TRUE;
483#endif
484 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
485 break;
486#endif
487
488/* jump to tag and split window if tag exists (in preview window) */
489#if defined(FEAT_QUICKFIX)
490 case '}':
491 CHECK_CMDWIN
492 if (Prenum)
493 g_do_tagpreview = Prenum;
494 else
495 g_do_tagpreview = p_pvh;
496 /*FALLTHROUGH*/
497#endif
498 case ']':
499 case Ctrl_RSB:
500 CHECK_CMDWIN
501#ifdef FEAT_VISUAL
502 reset_VIsual_and_resel(); /* stop Visual mode */
503#endif
504 if (Prenum)
505 postponed_split = Prenum;
506 else
507 postponed_split = -1;
508
509 /* Execute the command right here, required when
510 * "wincmd ]" was used in a function. */
511 do_nv_ident(Ctrl_RSB, NUL);
512 break;
513
514#ifdef FEAT_SEARCHPATH
515/* edit file name under cursor in a new window */
516 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000517 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000519wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000521
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000522 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 if (ptr != NULL)
524 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000525# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 setpcmark();
529 if (win_split(0, 0) == OK)
530 {
531# ifdef FEAT_SCROLLBIND
532 curwin->w_p_scb = FALSE;
533# endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000534 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
535 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000536 if (nchar == 'F' && lnum >= 0)
537 {
538 curwin->w_cursor.lnum = lnum;
539 check_cursor_lnum();
540 beginline(BL_SOL | BL_FIX);
541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
543 vim_free(ptr);
544 }
545 break;
546#endif
547
548#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000549/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 * new window -- webb
551 */
552 case 'i': /* Go to any match */
553 case Ctrl_I:
554 type = FIND_ANY;
555 /* FALLTHROUGH */
556 case 'd': /* Go to definition, using 'define' */
557 case Ctrl_D:
558 CHECK_CMDWIN
559 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
560 break;
561 find_pattern_in_path(ptr, 0, len, TRUE,
562 Prenum == 0 ? TRUE : FALSE, type,
563 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
564 curwin->w_set_curswant = TRUE;
565 break;
566#endif
567
Bram Moolenaar05159a02005-02-26 23:04:13 +0000568 case K_KENTER:
569 case CAR:
570#if defined(FEAT_QUICKFIX)
571 /*
572 * In a quickfix window a <CR> jumps to the error under the
573 * cursor in a new window.
574 */
575 if (bt_quickfix(curbuf))
576 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000577 sprintf((char *)cbuf, "split +%ld%s",
578 (long)curwin->w_cursor.lnum,
579 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000580 do_cmdline_cmd(cbuf);
581 }
582#endif
583 break;
584
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586/* CTRL-W g extended commands */
587 case 'g':
588 case Ctrl_G:
589 CHECK_CMDWIN
590#ifdef USE_ON_FLY_SCROLL
591 dont_scroll = TRUE; /* disallow scrolling here */
592#endif
593 ++no_mapping;
594 ++allow_keys; /* no mapping for xchar, but allow key codes */
595 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000596 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 --no_mapping;
599 --allow_keys;
600#ifdef FEAT_CMDL_INFO
601 (void)add_to_showcmd(xchar);
602#endif
603 switch (xchar)
604 {
605#if defined(FEAT_QUICKFIX)
606 case '}':
607 xchar = Ctrl_RSB;
608 if (Prenum)
609 g_do_tagpreview = Prenum;
610 else
611 g_do_tagpreview = p_pvh;
612 /*FALLTHROUGH*/
613#endif
614 case ']':
615 case Ctrl_RSB:
616#ifdef FEAT_VISUAL
617 reset_VIsual_and_resel(); /* stop Visual mode */
618#endif
619 if (Prenum)
620 postponed_split = Prenum;
621 else
622 postponed_split = -1;
623
624 /* Execute the command right here, required when
625 * "wincmd g}" was used in a function. */
626 do_nv_ident('g', xchar);
627 break;
628
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000629#ifdef FEAT_SEARCHPATH
630 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000631 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000632 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000633 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000634 goto wingotofile;
635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 default:
637 beep_flush();
638 break;
639 }
640 break;
641
642 default: beep_flush();
643 break;
644 }
645}
646
647/*
648 * split the current window, implements CTRL-W s and :split
649 *
650 * "size" is the height or width for the new window, 0 to use half of current
651 * height or width.
652 *
653 * "flags":
654 * WSP_ROOM: require enough room for new window
655 * WSP_VERT: vertical split.
656 * WSP_TOP: open window at the top-left of the shell (help window).
657 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
658 * WSP_HELP: creating the help window, keep layout snapshot
659 *
660 * return FAIL for failure, OK otherwise
661 */
662 int
663win_split(size, flags)
664 int size;
665 int flags;
666{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000667 /* When the ":tab" modifier was used open a new tab page instead. */
668 if (may_open_tabpage() == OK)
669 return OK;
670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /* Add flags from ":vertical", ":topleft" and ":botright". */
672 flags |= cmdmod.split;
673 if ((flags & WSP_TOP) && (flags & WSP_BOT))
674 {
675 EMSG(_("E442: Can't split topleft and botright at the same time"));
676 return FAIL;
677 }
678
679 /* When creating the help window make a snapshot of the window layout.
680 * Otherwise clear the snapshot, it's now invalid. */
681 if (flags & WSP_HELP)
682 make_snapshot();
683 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000684 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 return win_split_ins(size, flags, NULL, 0);
687}
688
689/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000690 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 * When "newwin" is not NULL: insert this window at the far
692 * top/left/right/bottom.
693 * return FAIL for failure, OK otherwise
694 */
695 static int
696win_split_ins(size, flags, newwin, dir)
697 int size;
698 int flags;
699 win_T *newwin;
700 int dir;
701{
702 win_T *wp = newwin;
703 win_T *oldwin;
704 int new_size = size;
705 int i;
706 int need_status = 0;
707 int do_equal = FALSE;
708 int needed;
709 int available;
710 int oldwin_height = 0;
711 int layout;
712 frame_T *frp, *curfrp;
713 int before;
714
715 if (flags & WSP_TOP)
716 oldwin = firstwin;
717 else if (flags & WSP_BOT)
718 oldwin = lastwin;
719 else
720 oldwin = curwin;
721
722 /* add a status line when p_ls == 1 and splitting the first window */
723 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
724 {
725 if (oldwin->w_height <= p_wmh && newwin == NULL)
726 {
727 EMSG(_(e_noroom));
728 return FAIL;
729 }
730 need_status = STATUS_HEIGHT;
731 }
732
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000733#ifdef FEAT_GUI
734 /* May be needed for the scrollbars that are going to change. */
735 if (gui.in_use)
736 out_flush();
737#endif
738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#ifdef FEAT_VERTSPLIT
740 if (flags & WSP_VERT)
741 {
742 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 /*
745 * Check if we are able to split the current window and compute its
746 * width.
747 */
748 needed = p_wmw + 1;
749 if (flags & WSP_ROOM)
750 needed += p_wiw - p_wmw;
751 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
752 {
753 available = topframe->fr_width;
754 needed += frame_minwidth(topframe, NULL);
755 }
756 else
757 available = oldwin->w_width;
758 if (available < needed && newwin == NULL)
759 {
760 EMSG(_(e_noroom));
761 return FAIL;
762 }
763 if (new_size == 0)
764 new_size = oldwin->w_width / 2;
765 if (new_size > oldwin->w_width - p_wmw - 1)
766 new_size = oldwin->w_width - p_wmw - 1;
767 if (new_size < p_wmw)
768 new_size = p_wmw;
769
770 /* if it doesn't fit in the current window, need win_equal() */
771 if (oldwin->w_width - new_size - 1 < p_wmw)
772 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000773
774 /* We don't like to take lines for the new window from a
775 * 'winfixwidth' window. Take them from a window to the left or right
776 * instead, if possible. */
777 if (oldwin->w_p_wfw)
778 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000779
780 /* Only make all windows the same width if one of them (except oldwin)
781 * is wider than one of the split windows. */
782 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
783 && oldwin->w_frame->fr_parent != NULL)
784 {
785 frp = oldwin->w_frame->fr_parent->fr_child;
786 while (frp != NULL)
787 {
788 if (frp->fr_win != oldwin && frp->fr_win != NULL
789 && (frp->fr_win->w_width > new_size
790 || frp->fr_win->w_width > oldwin->w_width
791 - new_size - STATUS_HEIGHT))
792 {
793 do_equal = TRUE;
794 break;
795 }
796 frp = frp->fr_next;
797 }
798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
800 else
801#endif
802 {
803 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /*
806 * Check if we are able to split the current window and compute its
807 * height.
808 */
809 needed = p_wmh + STATUS_HEIGHT + need_status;
810 if (flags & WSP_ROOM)
811 needed += p_wh - p_wmh;
812 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
813 {
814 available = topframe->fr_height;
815 needed += frame_minheight(topframe, NULL);
816 }
817 else
818 {
819 available = oldwin->w_height;
820 needed += p_wmh;
821 }
822 if (available < needed && newwin == NULL)
823 {
824 EMSG(_(e_noroom));
825 return FAIL;
826 }
827 oldwin_height = oldwin->w_height;
828 if (need_status)
829 {
830 oldwin->w_status_height = STATUS_HEIGHT;
831 oldwin_height -= STATUS_HEIGHT;
832 }
833 if (new_size == 0)
834 new_size = oldwin_height / 2;
835
836 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
837 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
838 if (new_size < p_wmh)
839 new_size = p_wmh;
840
841 /* if it doesn't fit in the current window, need win_equal() */
842 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
843 do_equal = TRUE;
844
845 /* We don't like to take lines for the new window from a
846 * 'winfixheight' window. Take them from a window above or below
847 * instead, if possible. */
848 if (oldwin->w_p_wfh)
849 {
850 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
851 oldwin);
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 oldwin_height -= STATUS_HEIGHT;
855 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000856
857 /* Only make all windows the same height if one of them (except oldwin)
858 * is higher than one of the split windows. */
859 if (!do_equal && p_ea && size == 0
860#ifdef FEAT_VERTSPLIT
861 && *p_ead != 'h'
862#endif
863 && oldwin->w_frame->fr_parent != NULL)
864 {
865 frp = oldwin->w_frame->fr_parent->fr_child;
866 while (frp != NULL)
867 {
868 if (frp->fr_win != oldwin && frp->fr_win != NULL
869 && (frp->fr_win->w_height > new_size
870 || frp->fr_win->w_height > oldwin_height - new_size
871 - STATUS_HEIGHT))
872 {
873 do_equal = TRUE;
874 break;
875 }
876 frp = frp->fr_next;
877 }
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880
881 /*
882 * allocate new window structure and link it in the window list
883 */
884 if ((flags & WSP_TOP) == 0
885 && ((flags & WSP_BOT)
886 || (flags & WSP_BELOW)
887 || (!(flags & WSP_ABOVE)
888 && (
889#ifdef FEAT_VERTSPLIT
890 (flags & WSP_VERT) ? p_spr :
891#endif
892 p_sb))))
893 {
894 /* new window below/right of current one */
895 if (newwin == NULL)
896 wp = win_alloc(oldwin);
897 else
898 win_append(oldwin, wp);
899 }
900 else
901 {
902 if (newwin == NULL)
903 wp = win_alloc(oldwin->w_prev);
904 else
905 win_append(oldwin->w_prev, wp);
906 }
907
908 if (newwin == NULL)
909 {
910 if (wp == NULL)
911 return FAIL;
912
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000913 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000914 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
917 /*
918 * Reorganise the tree of frames to insert the new window.
919 */
920 if (flags & (WSP_TOP | WSP_BOT))
921 {
922#ifdef FEAT_VERTSPLIT
923 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
924 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
925#else
926 if (topframe->fr_layout == FR_COL)
927#endif
928 {
929 curfrp = topframe->fr_child;
930 if (flags & WSP_BOT)
931 while (curfrp->fr_next != NULL)
932 curfrp = curfrp->fr_next;
933 }
934 else
935 curfrp = topframe;
936 before = (flags & WSP_TOP);
937 }
938 else
939 {
940 curfrp = oldwin->w_frame;
941 if (flags & WSP_BELOW)
942 before = FALSE;
943 else if (flags & WSP_ABOVE)
944 before = TRUE;
945 else
946#ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
948 before = !p_spr;
949 else
950#endif
951 before = !p_sb;
952 }
953 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
954 {
955 /* Need to create a new frame in the tree to make a branch. */
956 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
957 *frp = *curfrp;
958 curfrp->fr_layout = layout;
959 frp->fr_parent = curfrp;
960 frp->fr_next = NULL;
961 frp->fr_prev = NULL;
962 curfrp->fr_child = frp;
963 curfrp->fr_win = NULL;
964 curfrp = frp;
965 if (frp->fr_win != NULL)
966 oldwin->w_frame = frp;
967 else
968 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
969 frp->fr_parent = curfrp;
970 }
971
972 if (newwin == NULL)
973 {
974 /* Create a frame for the new window. */
975 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
976 frp->fr_layout = FR_LEAF;
977 frp->fr_win = wp;
978 wp->w_frame = frp;
979 }
980 else
981 frp = newwin->w_frame;
982 frp->fr_parent = curfrp->fr_parent;
983
984 /* Insert the new frame at the right place in the frame list. */
985 if (before)
986 frame_insert(curfrp, frp);
987 else
988 frame_append(curfrp, frp);
989
990#ifdef FEAT_VERTSPLIT
991 if (flags & WSP_VERT)
992 {
993 wp->w_p_scr = curwin->w_p_scr;
994 if (need_status)
995 {
996 --oldwin->w_height;
997 oldwin->w_status_height = need_status;
998 }
999 if (flags & (WSP_TOP | WSP_BOT))
1000 {
1001 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001002 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 wp->w_height = curfrp->fr_height - (p_ls > 0);
1004 wp->w_status_height = (p_ls > 0);
1005 }
1006 else
1007 {
1008 /* height and row of new window is same as current window */
1009 wp->w_winrow = oldwin->w_winrow;
1010 wp->w_height = oldwin->w_height;
1011 wp->w_status_height = oldwin->w_status_height;
1012 }
1013 frp->fr_height = curfrp->fr_height;
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one column for the vertical separator */
1017 wp->w_width = new_size;
1018 if (before)
1019 wp->w_vsep_width = 1;
1020 else
1021 {
1022 wp->w_vsep_width = oldwin->w_vsep_width;
1023 oldwin->w_vsep_width = 1;
1024 }
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 {
1027 if (flags & WSP_BOT)
1028 frame_add_vsep(curfrp);
1029 /* Set width of neighbor frame */
1030 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001031 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1032 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
1034 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001035 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (before) /* new window left of current one */
1037 {
1038 wp->w_wincol = oldwin->w_wincol;
1039 oldwin->w_wincol += new_size + 1;
1040 }
1041 else /* new window right of current one */
1042 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1043 frame_fix_width(oldwin);
1044 frame_fix_width(wp);
1045 }
1046 else
1047#endif
1048 {
1049 /* width and column of new window is same as current window */
1050#ifdef FEAT_VERTSPLIT
1051 if (flags & (WSP_TOP | WSP_BOT))
1052 {
1053 wp->w_wincol = 0;
1054 wp->w_width = Columns;
1055 wp->w_vsep_width = 0;
1056 }
1057 else
1058 {
1059 wp->w_wincol = oldwin->w_wincol;
1060 wp->w_width = oldwin->w_width;
1061 wp->w_vsep_width = oldwin->w_vsep_width;
1062 }
1063 frp->fr_width = curfrp->fr_width;
1064#endif
1065
1066 /* "new_size" of the current window goes to the new window, use
1067 * one row for the status line */
1068 win_new_height(wp, new_size);
1069 if (flags & (WSP_TOP | WSP_BOT))
1070 frame_new_height(curfrp, curfrp->fr_height
1071 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1072 else
1073 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1074 if (before) /* new window above current one */
1075 {
1076 wp->w_winrow = oldwin->w_winrow;
1077 wp->w_status_height = STATUS_HEIGHT;
1078 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1079 }
1080 else /* new window below current one */
1081 {
1082 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1083 wp->w_status_height = oldwin->w_status_height;
1084 oldwin->w_status_height = STATUS_HEIGHT;
1085 }
1086#ifdef FEAT_VERTSPLIT
1087 if (flags & WSP_BOT)
1088 frame_add_statusline(curfrp);
1089#endif
1090 frame_fix_height(wp);
1091 frame_fix_height(oldwin);
1092 }
1093
1094 if (flags & (WSP_TOP | WSP_BOT))
1095 (void)win_comp_pos();
1096
1097 /*
1098 * Both windows need redrawing
1099 */
1100 redraw_win_later(wp, NOT_VALID);
1101 wp->w_redr_status = TRUE;
1102 redraw_win_later(oldwin, NOT_VALID);
1103 oldwin->w_redr_status = TRUE;
1104
1105 if (need_status)
1106 {
1107 msg_row = Rows - 1;
1108 msg_col = sc_col;
1109 msg_clr_eos_force(); /* Old command/ruler may still be there */
1110 comp_col();
1111 msg_row = Rows - 1;
1112 msg_col = 0; /* put position back at start of line */
1113 }
1114
1115 /*
1116 * make the new window the current window and redraw
1117 */
1118 if (do_equal || dir != 0)
1119 win_equal(wp, TRUE,
1120#ifdef FEAT_VERTSPLIT
1121 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1122 : dir == 'h' ? 'b' :
1123#endif
1124 'v');
1125
1126 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1127 * size was given. */
1128#ifdef FEAT_VERTSPLIT
1129 if (flags & WSP_VERT)
1130 {
1131 i = p_wiw;
1132 if (size != 0)
1133 p_wiw = size;
1134
1135# ifdef FEAT_GUI
1136 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1137 if (gui.in_use)
1138 gui_init_which_components(NULL);
1139# endif
1140 }
1141 else
1142#endif
1143 {
1144 i = p_wh;
1145 if (size != 0)
1146 p_wh = size;
1147 }
1148 win_enter(wp, FALSE);
1149#ifdef FEAT_VERTSPLIT
1150 if (flags & WSP_VERT)
1151 p_wiw = i;
1152 else
1153#endif
1154 p_wh = i;
1155
1156 return OK;
1157}
1158
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001159/*
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001165 */
1166 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001167win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 win_T *newp;
1169 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001170 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171{
1172 int i;
1173
1174 newp->w_buffer = oldp->w_buffer;
1175 oldp->w_buffer->b_nwindows++;
1176 newp->w_cursor = oldp->w_cursor;
1177 newp->w_valid = 0;
1178 newp->w_curswant = oldp->w_curswant;
1179 newp->w_set_curswant = oldp->w_set_curswant;
1180 newp->w_topline = oldp->w_topline;
1181#ifdef FEAT_DIFF
1182 newp->w_topfill = oldp->w_topfill;
1183#endif
1184 newp->w_leftcol = oldp->w_leftcol;
1185 newp->w_pcmark = oldp->w_pcmark;
1186 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1187 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001188 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001189 newp->w_fraction = oldp->w_fraction;
1190 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1191#ifdef FEAT_JUMPLIST
1192 copy_jumplist(oldp, newp);
1193#endif
1194#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001195 if (flags & WSP_NEWLOC)
1196 {
1197 /* Don't copy the location list. */
1198 newp->w_llist = NULL;
1199 newp->w_llist_ref = NULL;
1200 }
1201 else
1202 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001203#endif
1204 if (oldp->w_localdir != NULL)
1205 newp->w_localdir = vim_strsave(oldp->w_localdir);
1206
1207 /* Use the same argument list. */
1208 newp->w_alist = oldp->w_alist;
1209 ++newp->w_alist->al_refcount;
1210 newp->w_arg_idx = oldp->w_arg_idx;
1211
1212 /*
1213 * copy tagstack and options from existing window
1214 */
1215 for (i = 0; i < oldp->w_tagstacklen; i++)
1216 {
1217 newp->w_tagstack[i] = oldp->w_tagstack[i];
1218 if (newp->w_tagstack[i].tagname != NULL)
1219 newp->w_tagstack[i].tagname =
1220 vim_strsave(newp->w_tagstack[i].tagname);
1221 }
1222 newp->w_tagstackidx = oldp->w_tagstackidx;
1223 newp->w_tagstacklen = oldp->w_tagstacklen;
1224 win_copy_options(oldp, newp);
1225# ifdef FEAT_FOLDING
1226 copyFoldingState(oldp, newp);
1227# endif
1228}
1229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif /* FEAT_WINDOWS */
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#if defined(FEAT_WINDOWS) || defined(PROTO)
1233/*
1234 * Check if "win" is a pointer to an existing window.
1235 */
1236 int
1237win_valid(win)
1238 win_T *win;
1239{
1240 win_T *wp;
1241
1242 if (win == NULL)
1243 return FALSE;
1244 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1245 if (wp == win)
1246 return TRUE;
1247 return FALSE;
1248}
1249
1250/*
1251 * Return the number of windows.
1252 */
1253 int
1254win_count()
1255{
1256 win_T *wp;
1257 int count = 0;
1258
1259 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1260 ++count;
1261 return count;
1262}
1263
1264/*
1265 * Make "count" windows on the screen.
1266 * Return actual number of windows on the screen.
1267 * Must be called when there is just one window, filling the whole screen
1268 * (excluding the command line).
1269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 int
1271make_windows(count, vertical)
1272 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001273 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int maxcount;
1276 int todo;
1277
1278#ifdef FEAT_VERTSPLIT
1279 if (vertical)
1280 {
1281 /* Each windows needs at least 'winminwidth' lines and a separator
1282 * column. */
1283 maxcount = (curwin->w_width + curwin->w_vsep_width
1284 - (p_wiw - p_wmw)) / (p_wmw + 1);
1285 }
1286 else
1287#endif
1288 {
1289 /* Each window needs at least 'winminheight' lines and a status line. */
1290 maxcount = (curwin->w_height + curwin->w_status_height
1291 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1292 }
1293
1294 if (maxcount < 2)
1295 maxcount = 2;
1296 if (count > maxcount)
1297 count = maxcount;
1298
1299 /*
1300 * add status line now, otherwise first window will be too big
1301 */
1302 if (count > 1)
1303 last_status(TRUE);
1304
1305#ifdef FEAT_AUTOCMD
1306 /*
1307 * Don't execute autocommands while creating the windows. Must do that
1308 * when putting the buffers in the windows.
1309 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001310 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#endif
1312
1313 /* todo is number of windows left to create */
1314 for (todo = count - 1; todo > 0; --todo)
1315#ifdef FEAT_VERTSPLIT
1316 if (vertical)
1317 {
1318 if (win_split(curwin->w_width - (curwin->w_width - todo)
1319 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1320 break;
1321 }
1322 else
1323#endif
1324 {
1325 if (win_split(curwin->w_height - (curwin->w_height - todo
1326 * STATUS_HEIGHT) / (todo + 1)
1327 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1328 break;
1329 }
1330
1331#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001332 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335 /* return actual number of windows */
1336 return (count - todo);
1337}
1338
1339/*
1340 * Exchange current and next window
1341 */
1342 static void
1343win_exchange(Prenum)
1344 long Prenum;
1345{
1346 frame_T *frp;
1347 frame_T *frp2;
1348 win_T *wp;
1349 win_T *wp2;
1350 int temp;
1351
1352 if (lastwin == firstwin) /* just one window */
1353 {
1354 beep_flush();
1355 return;
1356 }
1357
1358#ifdef FEAT_GUI
1359 need_mouse_correct = TRUE;
1360#endif
1361
1362 /*
1363 * find window to exchange with
1364 */
1365 if (Prenum)
1366 {
1367 frp = curwin->w_frame->fr_parent->fr_child;
1368 while (frp != NULL && --Prenum > 0)
1369 frp = frp->fr_next;
1370 }
1371 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1372 frp = curwin->w_frame->fr_next;
1373 else /* Swap last window in row/col with previous */
1374 frp = curwin->w_frame->fr_prev;
1375
1376 /* We can only exchange a window with another window, not with a frame
1377 * containing windows. */
1378 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1379 return;
1380 wp = frp->fr_win;
1381
1382/*
1383 * 1. remove curwin from the list. Remember after which window it was in wp2
1384 * 2. insert curwin before wp in the list
1385 * if wp != wp2
1386 * 3. remove wp from the list
1387 * 4. insert wp after wp2
1388 * 5. exchange the status line height and vsep width.
1389 */
1390 wp2 = curwin->w_prev;
1391 frp2 = curwin->w_frame->fr_prev;
1392 if (wp->w_prev != curwin)
1393 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 frame_remove(curwin->w_frame);
1396 win_append(wp->w_prev, curwin);
1397 frame_insert(frp, curwin->w_frame);
1398 }
1399 if (wp != wp2)
1400 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001401 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 frame_remove(wp->w_frame);
1403 win_append(wp2, wp);
1404 if (frp2 == NULL)
1405 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1406 else
1407 frame_append(frp2, wp->w_frame);
1408 }
1409 temp = curwin->w_status_height;
1410 curwin->w_status_height = wp->w_status_height;
1411 wp->w_status_height = temp;
1412#ifdef FEAT_VERTSPLIT
1413 temp = curwin->w_vsep_width;
1414 curwin->w_vsep_width = wp->w_vsep_width;
1415 wp->w_vsep_width = temp;
1416
1417 /* If the windows are not in the same frame, exchange the sizes to avoid
1418 * messing up the window layout. Otherwise fix the frame sizes. */
1419 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1420 {
1421 temp = curwin->w_height;
1422 curwin->w_height = wp->w_height;
1423 wp->w_height = temp;
1424 temp = curwin->w_width;
1425 curwin->w_width = wp->w_width;
1426 wp->w_width = temp;
1427 }
1428 else
1429 {
1430 frame_fix_height(curwin);
1431 frame_fix_height(wp);
1432 frame_fix_width(curwin);
1433 frame_fix_width(wp);
1434 }
1435#endif
1436
1437 (void)win_comp_pos(); /* recompute window positions */
1438
1439 win_enter(wp, TRUE);
1440 redraw_later(CLEAR);
1441}
1442
1443/*
1444 * rotate windows: if upwards TRUE the second window becomes the first one
1445 * if upwards FALSE the first window becomes the second one
1446 */
1447 static void
1448win_rotate(upwards, count)
1449 int upwards;
1450 int count;
1451{
1452 win_T *wp1;
1453 win_T *wp2;
1454 frame_T *frp;
1455 int n;
1456
1457 if (firstwin == lastwin) /* nothing to do */
1458 {
1459 beep_flush();
1460 return;
1461 }
1462
1463#ifdef FEAT_GUI
1464 need_mouse_correct = TRUE;
1465#endif
1466
1467#ifdef FEAT_VERTSPLIT
1468 /* Check if all frames in this row/col have one window. */
1469 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1470 frp = frp->fr_next)
1471 if (frp->fr_win == NULL)
1472 {
1473 EMSG(_("E443: Cannot rotate when another window is split"));
1474 return;
1475 }
1476#endif
1477
1478 while (count--)
1479 {
1480 if (upwards) /* first window becomes last window */
1481 {
1482 /* remove first window/frame from the list */
1483 frp = curwin->w_frame->fr_parent->fr_child;
1484 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001485 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 frame_remove(frp);
1487
1488 /* find last frame and append removed window/frame after it */
1489 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1490 ;
1491 win_append(frp->fr_win, wp1);
1492 frame_append(frp, wp1->w_frame);
1493
1494 wp2 = frp->fr_win; /* previously last window */
1495 }
1496 else /* last window becomes first window */
1497 {
1498 /* find last window/frame in the list and remove it */
1499 for (frp = curwin->w_frame; frp->fr_next != NULL;
1500 frp = frp->fr_next)
1501 ;
1502 wp1 = frp->fr_win;
1503 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001504 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 frame_remove(frp);
1506
1507 /* append the removed window/frame before the first in the list */
1508 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1509 frame_insert(frp->fr_parent->fr_child, frp);
1510 }
1511
1512 /* exchange status height and vsep width of old and new last window */
1513 n = wp2->w_status_height;
1514 wp2->w_status_height = wp1->w_status_height;
1515 wp1->w_status_height = n;
1516 frame_fix_height(wp1);
1517 frame_fix_height(wp2);
1518#ifdef FEAT_VERTSPLIT
1519 n = wp2->w_vsep_width;
1520 wp2->w_vsep_width = wp1->w_vsep_width;
1521 wp1->w_vsep_width = n;
1522 frame_fix_width(wp1);
1523 frame_fix_width(wp2);
1524#endif
1525
1526 /* recompute w_winrow and w_wincol for all windows */
1527 (void)win_comp_pos();
1528 }
1529
1530 redraw_later(CLEAR);
1531}
1532
1533/*
1534 * Move the current window to the very top/bottom/left/right of the screen.
1535 */
1536 static void
1537win_totop(size, flags)
1538 int size;
1539 int flags;
1540{
1541 int dir;
1542 int height = curwin->w_height;
1543
1544 if (lastwin == firstwin)
1545 {
1546 beep_flush();
1547 return;
1548 }
1549
1550 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001551 (void)winframe_remove(curwin, &dir, NULL);
1552 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 last_status(FALSE); /* may need to remove last status line */
1554 (void)win_comp_pos(); /* recompute window positions */
1555
1556 /* Split a window on the desired side and put the window there. */
1557 (void)win_split_ins(size, flags, curwin, dir);
1558 if (!(flags & WSP_VERT))
1559 {
1560 win_setheight(height);
1561 if (p_ea)
1562 win_equal(curwin, TRUE, 'v');
1563 }
1564
1565#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1566 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1567 * scrollbars. Have to update them anyway. */
1568 if (gui.in_use)
1569 {
1570 out_flush();
1571 gui_init_which_components(NULL);
1572 gui_update_scrollbars(TRUE);
1573 }
1574 need_mouse_correct = TRUE;
1575#endif
1576
1577}
1578
1579/*
1580 * Move window "win1" to below/right of "win2" and make "win1" the current
1581 * window. Only works within the same frame!
1582 */
1583 void
1584win_move_after(win1, win2)
1585 win_T *win1, *win2;
1586{
1587 int height;
1588
1589 /* check if the arguments are reasonable */
1590 if (win1 == win2)
1591 return;
1592
1593 /* check if there is something to do */
1594 if (win2->w_next != win1)
1595 {
1596 /* may need move the status line/vertical separator of the last window
1597 * */
1598 if (win1 == lastwin)
1599 {
1600 height = win1->w_prev->w_status_height;
1601 win1->w_prev->w_status_height = win1->w_status_height;
1602 win1->w_status_height = height;
1603#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001604 if (win1->w_prev->w_vsep_width == 1)
1605 {
1606 /* Remove the vertical separator from the last-but-one window,
1607 * add it to the last window. Adjust the frame widths. */
1608 win1->w_prev->w_vsep_width = 0;
1609 win1->w_prev->w_frame->fr_width -= 1;
1610 win1->w_vsep_width = 1;
1611 win1->w_frame->fr_width += 1;
1612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
1614 }
1615 else if (win2 == lastwin)
1616 {
1617 height = win1->w_status_height;
1618 win1->w_status_height = win2->w_status_height;
1619 win2->w_status_height = height;
1620#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001621 if (win1->w_vsep_width == 1)
1622 {
1623 /* Remove the vertical separator from win1, add it to the last
1624 * window, win2. Adjust the frame widths. */
1625 win2->w_vsep_width = 1;
1626 win2->w_frame->fr_width += 1;
1627 win1->w_vsep_width = 0;
1628 win1->w_frame->fr_width -= 1;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001632 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 frame_remove(win1->w_frame);
1634 win_append(win2, win1);
1635 frame_append(win2->w_frame, win1->w_frame);
1636
1637 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1638 redraw_later(NOT_VALID);
1639 }
1640 win_enter(win1, FALSE);
1641}
1642
1643/*
1644 * Make all windows the same height.
1645 * 'next_curwin' will soon be the current window, make sure it has enough
1646 * rows.
1647 */
1648 void
1649win_equal(next_curwin, current, dir)
1650 win_T *next_curwin; /* pointer to current window to be or NULL */
1651 int current; /* do only frame with current window */
1652 int dir; /* 'v' for vertically, 'h' for horizontally,
1653 'b' for both, 0 for using p_ead */
1654{
1655 if (dir == 0)
1656#ifdef FEAT_VERTSPLIT
1657 dir = *p_ead;
1658#else
1659 dir = 'b';
1660#endif
1661 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001662 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001663 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664}
1665
1666/*
1667 * Set a frame to a new position and height, spreading the available room
1668 * equally over contained frames.
1669 * The window "next_curwin" (if not NULL) should at least get the size from
1670 * 'winheight' and 'winwidth' if possible.
1671 */
1672 static void
1673win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1674 win_T *next_curwin; /* pointer to current window to be or NULL */
1675 int current; /* do only frame with current window */
1676 frame_T *topfr; /* frame to set size off */
1677 int dir; /* 'v', 'h' or 'b', see win_equal() */
1678 int col; /* horizontal position for frame */
1679 int row; /* vertical position for frame */
1680 int width; /* new width of frame */
1681 int height; /* new height of frame */
1682{
1683 int n, m;
1684 int extra_sep = 0;
1685 int wincount, totwincount = 0;
1686 frame_T *fr;
1687 int next_curwin_size = 0;
1688 int room = 0;
1689 int new_size;
1690 int has_next_curwin = 0;
1691 int hnc;
1692
1693 if (topfr->fr_layout == FR_LEAF)
1694 {
1695 /* Set the width/height of this frame.
1696 * Redraw when size or position changes */
1697 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1698#ifdef FEAT_VERTSPLIT
1699 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1700#endif
1701 )
1702 {
1703 topfr->fr_win->w_winrow = row;
1704 frame_new_height(topfr, height, FALSE, FALSE);
1705#ifdef FEAT_VERTSPLIT
1706 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001707 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709 redraw_all_later(CLEAR);
1710 }
1711 }
1712#ifdef FEAT_VERTSPLIT
1713 else if (topfr->fr_layout == FR_ROW)
1714 {
1715 topfr->fr_width = width;
1716 topfr->fr_height = height;
1717
1718 if (dir != 'v') /* equalize frame widths */
1719 {
1720 /* Compute the maximum number of windows horizontally in this
1721 * frame. */
1722 n = frame_minwidth(topfr, NOWIN);
1723 /* add one for the rightmost window, it doesn't have a separator */
1724 if (col + width == Columns)
1725 extra_sep = 1;
1726 else
1727 extra_sep = 0;
1728 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001729 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001731 /*
1732 * Compute width for "next_curwin" window and room available for
1733 * other windows.
1734 * "m" is the minimal width when counting p_wiw for "next_curwin".
1735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 m = frame_minwidth(topfr, next_curwin);
1737 room = width - m;
1738 if (room < 0)
1739 {
1740 next_curwin_size = p_wiw + room;
1741 room = 0;
1742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 else
1744 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001745 next_curwin_size = -1;
1746 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1747 {
1748 /* If 'winfixwidth' set keep the window width if
1749 * possible.
1750 * Watch out for this window being the next_curwin. */
1751 if (frame_fixed_width(fr))
1752 {
1753 n = frame_minwidth(fr, NOWIN);
1754 new_size = fr->fr_width;
1755 if (frame_has_win(fr, next_curwin))
1756 {
1757 room += p_wiw - p_wmw;
1758 next_curwin_size = 0;
1759 if (new_size < p_wiw)
1760 new_size = p_wiw;
1761 }
1762 else
1763 /* These windows don't use up room. */
1764 totwincount -= (n + (fr->fr_next == NULL
1765 ? extra_sep : 0)) / (p_wmw + 1);
1766 room -= new_size - n;
1767 if (room < 0)
1768 {
1769 new_size += room;
1770 room = 0;
1771 }
1772 fr->fr_newwidth = new_size;
1773 }
1774 }
1775 if (next_curwin_size == -1)
1776 {
1777 if (!has_next_curwin)
1778 next_curwin_size = 0;
1779 else if (totwincount > 1
1780 && (room + (totwincount - 2))
1781 / (totwincount - 1) > p_wiw)
1782 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001783 /* Can make all windows wider than 'winwidth', spread
1784 * the room equally. */
1785 next_curwin_size = (room + p_wiw
1786 + (totwincount - 1) * p_wmw
1787 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001788 room -= next_curwin_size - p_wiw;
1789 }
1790 else
1791 next_curwin_size = p_wiw;
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001794
1795 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 --totwincount; /* don't count curwin */
1797 }
1798
1799 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1800 {
1801 n = m = 0;
1802 wincount = 1;
1803 if (fr->fr_next == NULL)
1804 /* last frame gets all that remains (avoid roundoff error) */
1805 new_size = width;
1806 else if (dir == 'v')
1807 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001808 else if (frame_fixed_width(fr))
1809 {
1810 new_size = fr->fr_newwidth;
1811 wincount = 0; /* doesn't count as a sizeable window */
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 {
1815 /* Compute the maximum number of windows horiz. in "fr". */
1816 n = frame_minwidth(fr, NOWIN);
1817 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1818 / (p_wmw + 1);
1819 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001820 if (has_next_curwin)
1821 hnc = frame_has_win(fr, next_curwin);
1822 else
1823 hnc = FALSE;
1824 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001826 if (totwincount == 0)
1827 new_size = room;
1828 else
1829 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001831 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 next_curwin_size -= p_wiw - (m - n);
1834 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001837 else
1838 room -= new_size;
1839 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
1841
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * window, unless equalizing all frames. */
1844 if (!current || dir != 'v' || topfr->fr_parent != NULL
1845 || (new_size != fr->fr_width)
1846 || frame_has_win(fr, next_curwin))
1847 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001848 new_size, height);
1849 col += new_size;
1850 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 totwincount -= wincount;
1852 }
1853 }
1854#endif
1855 else /* topfr->fr_layout == FR_COL */
1856 {
1857#ifdef FEAT_VERTSPLIT
1858 topfr->fr_width = width;
1859#endif
1860 topfr->fr_height = height;
1861
1862 if (dir != 'h') /* equalize frame heights */
1863 {
1864 /* Compute maximum number of windows vertically in this frame. */
1865 n = frame_minheight(topfr, NOWIN);
1866 /* add one for the bottom window if it doesn't have a statusline */
1867 if (row + height == cmdline_row && p_ls == 0)
1868 extra_sep = 1;
1869 else
1870 extra_sep = 0;
1871 totwincount = (n + extra_sep) / (p_wmh + 1);
1872 has_next_curwin = frame_has_win(topfr, next_curwin);
1873
1874 /*
1875 * Compute height for "next_curwin" window and room available for
1876 * other windows.
1877 * "m" is the minimal height when counting p_wh for "next_curwin".
1878 */
1879 m = frame_minheight(topfr, next_curwin);
1880 room = height - m;
1881 if (room < 0)
1882 {
1883 /* The room is less then 'winheight', use all space for the
1884 * current window. */
1885 next_curwin_size = p_wh + room;
1886 room = 0;
1887 }
1888 else
1889 {
1890 next_curwin_size = -1;
1891 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1892 {
1893 /* If 'winfixheight' set keep the window height if
1894 * possible.
1895 * Watch out for this window being the next_curwin. */
1896 if (frame_fixed_height(fr))
1897 {
1898 n = frame_minheight(fr, NOWIN);
1899 new_size = fr->fr_height;
1900 if (frame_has_win(fr, next_curwin))
1901 {
1902 room += p_wh - p_wmh;
1903 next_curwin_size = 0;
1904 if (new_size < p_wh)
1905 new_size = p_wh;
1906 }
1907 else
1908 /* These windows don't use up room. */
1909 totwincount -= (n + (fr->fr_next == NULL
1910 ? extra_sep : 0)) / (p_wmh + 1);
1911 room -= new_size - n;
1912 if (room < 0)
1913 {
1914 new_size += room;
1915 room = 0;
1916 }
1917 fr->fr_newheight = new_size;
1918 }
1919 }
1920 if (next_curwin_size == -1)
1921 {
1922 if (!has_next_curwin)
1923 next_curwin_size = 0;
1924 else if (totwincount > 1
1925 && (room + (totwincount - 2))
1926 / (totwincount - 1) > p_wh)
1927 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001928 /* can make all windows higher than 'winheight',
1929 * spread the room equally. */
1930 next_curwin_size = (room + p_wh
1931 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 + (totwincount - 1)) / totwincount;
1933 room -= next_curwin_size - p_wh;
1934 }
1935 else
1936 next_curwin_size = p_wh;
1937 }
1938 }
1939
1940 if (has_next_curwin)
1941 --totwincount; /* don't count curwin */
1942 }
1943
1944 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1945 {
1946 n = m = 0;
1947 wincount = 1;
1948 if (fr->fr_next == NULL)
1949 /* last frame gets all that remains (avoid roundoff error) */
1950 new_size = height;
1951 else if (dir == 'h')
1952 new_size = fr->fr_height;
1953 else if (frame_fixed_height(fr))
1954 {
1955 new_size = fr->fr_newheight;
1956 wincount = 0; /* doesn't count as a sizeable window */
1957 }
1958 else
1959 {
1960 /* Compute the maximum number of windows vert. in "fr". */
1961 n = frame_minheight(fr, NOWIN);
1962 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1963 / (p_wmh + 1);
1964 m = frame_minheight(fr, next_curwin);
1965 if (has_next_curwin)
1966 hnc = frame_has_win(fr, next_curwin);
1967 else
1968 hnc = FALSE;
1969 if (hnc) /* don't count next_curwin */
1970 --wincount;
1971 if (totwincount == 0)
1972 new_size = room;
1973 else
1974 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1975 / totwincount;
1976 if (hnc) /* add next_curwin size */
1977 {
1978 next_curwin_size -= p_wh - (m - n);
1979 new_size += next_curwin_size;
1980 room -= new_size - next_curwin_size;
1981 }
1982 else
1983 room -= new_size;
1984 new_size += n;
1985 }
1986 /* Skip frame that is full width when splitting or closing a
1987 * window, unless equalizing all frames. */
1988 if (!current || dir != 'h' || topfr->fr_parent != NULL
1989 || (new_size != fr->fr_height)
1990 || frame_has_win(fr, next_curwin))
1991 win_equal_rec(next_curwin, current, fr, dir, col, row,
1992 width, new_size);
1993 row += new_size;
1994 height -= new_size;
1995 totwincount -= wincount;
1996 }
1997 }
1998}
1999
2000/*
2001 * close all windows for buffer 'buf'
2002 */
2003 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002004close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002006 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002008 win_T *wp;
2009 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002010 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002013
2014 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002016 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002018 win_close(wp, FALSE);
2019
2020 /* Start all over, autocommands may change the window layout. */
2021 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002024 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002026
2027 /* Also check windows in other tab pages. */
2028 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2029 {
2030 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002031 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002032 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2033 if (wp->w_buffer == buf)
2034 {
2035 win_close_othertab(wp, FALSE, tp);
2036
2037 /* Start all over, the tab page may be closed and
2038 * autocommands may change the window layout. */
2039 nexttp = first_tabpage;
2040 break;
2041 }
2042 }
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002045
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002046 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048}
2049
2050/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002051 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002052 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002053 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002054 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002055last_window()
2056{
2057 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2058}
2059
2060/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002061 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 * If "free_buf" is TRUE related buffer may be unloaded.
2063 *
2064 * called by :quit, :close, :xit, :wq and findtag()
2065 */
2066 void
2067win_close(win, free_buf)
2068 win_T *win;
2069 int free_buf;
2070{
2071 win_T *wp;
2072#ifdef FEAT_AUTOCMD
2073 int other_buffer = FALSE;
2074#endif
2075 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 int dir;
2077 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002078 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002080 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 EMSG(_("E444: Cannot close last window"));
2083 return;
2084 }
2085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002086 /*
2087 * When closing the last window in a tab page first go to another tab
2088 * page and then close the window and the tab page. This avoids that
2089 * curwin and curtab are not invalid while we are freeing memory, they may
2090 * be used in GUI events.
2091 */
2092 if (firstwin == lastwin)
2093 {
2094 goto_tabpage_tp(alt_tabpage());
2095 redraw_tabline = TRUE;
2096
2097 /* Safety check: Autocommands may have closed the window when jumping
2098 * to the other tab page. */
2099 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2100 {
2101 int h = tabline_height();
2102
2103 win_close_othertab(win, free_buf, prev_curtab);
2104 if (h != tabline_height())
2105 shell_new_rows();
2106 }
2107 return;
2108 }
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 /* When closing the help window, try restoring a snapshot after closing
2111 * the window. Otherwise clear the snapshot, it's now invalid. */
2112 if (win->w_buffer->b_help)
2113 help_window = TRUE;
2114 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002115 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117#ifdef FEAT_AUTOCMD
2118 if (win == curwin)
2119 {
2120 /*
2121 * Guess which window is going to be the new current window.
2122 * This may change because of the autocommands (sigh).
2123 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
2126 /*
2127 * Be careful: If autocommands delete the window, return now.
2128 */
2129 if (wp->w_buffer != curbuf)
2130 {
2131 other_buffer = TRUE;
2132 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002133 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 return;
2135 }
2136 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002137 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 return;
2139# ifdef FEAT_EVAL
2140 /* autocmds may abort script processing */
2141 if (aborting())
2142 return;
2143# endif
2144 }
2145#endif
2146
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002147#ifdef FEAT_GUI
2148 /* Avoid trouble with scrollbars that are going to be deleted in
2149 * win_free(). */
2150 if (gui.in_use)
2151 out_flush();
2152#endif
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 /*
2155 * Close the link to the buffer.
2156 */
2157 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002160 * other window or moved to another tab page. */
2161 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 return;
2163
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002164 /* Free the memory used for the window. */
2165 wp = win_free_mem(win, &dir, NULL);
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 /* Make sure curwin isn't invalid. It can cause severe trouble when
2168 * printing an error message. For win_equal() curbuf needs to be valid
2169 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002170 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 curwin = wp;
2173#ifdef FEAT_QUICKFIX
2174 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2175 {
2176 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002177 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 * finding another window to go to.
2179 */
2180 for (;;)
2181 {
2182 if (wp->w_next == NULL)
2183 wp = firstwin;
2184 else
2185 wp = wp->w_next;
2186 if (wp == curwin)
2187 break;
2188 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2189 {
2190 curwin = wp;
2191 break;
2192 }
2193 }
2194 }
2195#endif
2196 curbuf = curwin->w_buffer;
2197 close_curwin = TRUE;
2198 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002199 if (p_ea
2200#ifdef FEAT_VERTSPLIT
2201 && (*p_ead == 'b' || *p_ead == dir)
2202#endif
2203 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 win_equal(curwin, TRUE,
2205#ifdef FEAT_VERTSPLIT
2206 dir
2207#else
2208 0
2209#endif
2210 );
2211 else
2212 win_comp_pos();
2213 if (close_curwin)
2214 {
2215 win_enter_ext(wp, FALSE, TRUE);
2216#ifdef FEAT_AUTOCMD
2217 if (other_buffer)
2218 /* careful: after this wp and win may be invalid! */
2219 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2220#endif
2221 }
2222
2223 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002224 * If last window has a status line now and we don't want one,
2225 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
2227 last_status(FALSE);
2228
2229 /* After closing the help window, try restoring the window layout from
2230 * before it was opened. */
2231 if (help_window)
2232 restore_snapshot(close_curwin);
2233
2234#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2235 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2236 if (gui.in_use && !win_hasvertsplit())
2237 gui_init_which_components(NULL);
2238#endif
2239
2240 redraw_all_later(NOT_VALID);
2241}
2242
2243/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002244 * Close window "win" in tab page "tp", which is not the current tab page.
2245 * This may be the last window ih that tab page and result in closing the tab,
2246 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002247 * Caller must check if buffer is hidden and whether the tabline needs to be
2248 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002249 */
2250 void
2251win_close_othertab(win, free_buf, tp)
2252 win_T *win;
2253 int free_buf;
2254 tabpage_T *tp;
2255{
2256 win_T *wp;
2257 int dir;
2258 tabpage_T *ptp = NULL;
2259
2260 /* Close the link to the buffer. */
2261 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2262
2263 /* Careful: Autocommands may have closed the tab page or made it the
2264 * current tab page. */
2265 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2266 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002267 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002268 return;
2269
2270 /* Autocommands may have closed the window already. */
2271 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2272 ;
2273 if (wp == NULL)
2274 return;
2275
2276 /* Free the memory used for the window. */
2277 wp = win_free_mem(win, &dir, tp);
2278
2279 /* When closing the last window in a tab page remove the tab page. */
2280 if (wp == NULL)
2281 {
2282 if (tp == first_tabpage)
2283 first_tabpage = tp->tp_next;
2284 else
2285 {
2286 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2287 ptp = ptp->tp_next)
2288 ;
2289 if (ptp == NULL)
2290 {
2291 EMSG2(_(e_intern2), "win_close_othertab()");
2292 return;
2293 }
2294 ptp->tp_next = tp->tp_next;
2295 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002296 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002297 }
2298}
2299
2300/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002301 * Free the memory used for a window.
2302 * Returns a pointer to the window that got the freed up space.
2303 */
2304 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002305win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002306 win_T *win;
2307 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002308 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002309{
2310 frame_T *frp;
2311 win_T *wp;
2312
Bram Moolenaarea408852005-06-25 22:49:46 +00002313#ifdef FEAT_FOLDING
2314 clearFolding(win);
2315#endif
2316
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002317 /* reduce the reference count to the argument list. */
2318 alist_unlink(win->w_alist);
2319
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002320 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002321 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002322 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002323 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002324 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002325
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002326 /* When deleting the current window of another tab page select a new
2327 * current window. */
2328 if (tp != NULL && win == tp->tp_curwin)
2329 tp->tp_curwin = wp;
2330
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002331 return wp;
2332}
2333
2334#if defined(EXITFREE) || defined(PROTO)
2335 void
2336win_free_all()
2337{
2338 int dummy;
2339
Bram Moolenaarf740b292006-02-16 22:11:02 +00002340# ifdef FEAT_WINDOWS
2341 while (first_tabpage->tp_next != NULL)
2342 tabpage_close(TRUE);
2343# endif
2344
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002345 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002346 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002347}
2348#endif
2349
2350/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 * Remove a window and its frame from the tree of frames.
2352 * Returns a pointer to the window that got the freed up space.
2353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002355winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002357 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002358 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359{
2360 frame_T *frp, *frp2, *frp3;
2361 frame_T *frp_close = win->w_frame;
2362 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 * If there is only one window there is nothing to remove.
2366 */
2367 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2368 return NULL;
2369
2370 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * Remove the window from its frame.
2372 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002373 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 wp = frame2win(frp2);
2375
2376 /* Remove this frame from the list of frames. */
2377 frame_remove(frp_close);
2378
2379#ifdef FEAT_VERTSPLIT
2380 if (frp_close->fr_parent->fr_layout == FR_COL)
2381 {
2382#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002383 /* When 'winfixheight' is set, try to find another frame in the column
2384 * (as close to the closed frame as possible) to distribute the height
2385 * to. */
2386 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2387 {
2388 frp = frp_close->fr_prev;
2389 frp3 = frp_close->fr_next;
2390 while (frp != NULL || frp3 != NULL)
2391 {
2392 if (frp != NULL)
2393 {
2394 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2395 {
2396 frp2 = frp;
2397 wp = frp->fr_win;
2398 break;
2399 }
2400 frp = frp->fr_prev;
2401 }
2402 if (frp3 != NULL)
2403 {
2404 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2405 {
2406 frp2 = frp3;
2407 wp = frp3->fr_win;
2408 break;
2409 }
2410 frp3 = frp3->fr_next;
2411 }
2412 }
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2415 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#ifdef FEAT_VERTSPLIT
2417 *dirp = 'v';
2418 }
2419 else
2420 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002421 /* When 'winfixwidth' is set, try to find another frame in the column
2422 * (as close to the closed frame as possible) to distribute the width
2423 * to. */
2424 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2425 {
2426 frp = frp_close->fr_prev;
2427 frp3 = frp_close->fr_next;
2428 while (frp != NULL || frp3 != NULL)
2429 {
2430 if (frp != NULL)
2431 {
2432 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2433 {
2434 frp2 = frp;
2435 wp = frp->fr_win;
2436 break;
2437 }
2438 frp = frp->fr_prev;
2439 }
2440 if (frp3 != NULL)
2441 {
2442 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2443 {
2444 frp2 = frp3;
2445 wp = frp3->fr_win;
2446 break;
2447 }
2448 frp3 = frp3->fr_next;
2449 }
2450 }
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002453 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 *dirp = 'h';
2455 }
2456#endif
2457
2458 /* If rows/columns go to a window below/right its positions need to be
2459 * updated. Can only be done after the sizes have been updated. */
2460 if (frp2 == frp_close->fr_next)
2461 {
2462 int row = win->w_winrow;
2463 int col = W_WINCOL(win);
2464
2465 frame_comp_pos(frp2, &row, &col);
2466 }
2467
2468 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2469 {
2470 /* There is no other frame in this list, move its info to the parent
2471 * and remove it. */
2472 frp2->fr_parent->fr_layout = frp2->fr_layout;
2473 frp2->fr_parent->fr_child = frp2->fr_child;
2474 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2475 frp->fr_parent = frp2->fr_parent;
2476 frp2->fr_parent->fr_win = frp2->fr_win;
2477 if (frp2->fr_win != NULL)
2478 frp2->fr_win->w_frame = frp2->fr_parent;
2479 frp = frp2->fr_parent;
2480 vim_free(frp2);
2481
2482 frp2 = frp->fr_parent;
2483 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2484 {
2485 /* The frame above the parent has the same layout, have to merge
2486 * the frames into this list. */
2487 if (frp2->fr_child == frp)
2488 frp2->fr_child = frp->fr_child;
2489 frp->fr_child->fr_prev = frp->fr_prev;
2490 if (frp->fr_prev != NULL)
2491 frp->fr_prev->fr_next = frp->fr_child;
2492 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2493 {
2494 frp3->fr_parent = frp2;
2495 if (frp3->fr_next == NULL)
2496 {
2497 frp3->fr_next = frp->fr_next;
2498 if (frp->fr_next != NULL)
2499 frp->fr_next->fr_prev = frp3;
2500 break;
2501 }
2502 }
2503 vim_free(frp);
2504 }
2505 }
2506
2507 return wp;
2508}
2509
2510/*
2511 * Find out which frame is going to get the freed up space when "win" is
2512 * closed.
2513 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2514 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2515 * This makes opening a window and closing it immediately keep the same window
2516 * layout.
2517 */
2518 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002521 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 frame_T *frp;
2524 int b;
2525
Bram Moolenaarf740b292006-02-16 22:11:02 +00002526 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002527 /* Last window in this tab page, will go to next tab page. */
2528 return alt_tabpage()->tp_curwin->w_frame;
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 frp = win->w_frame;
2531#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002532 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 b = p_spr;
2534 else
2535#endif
2536 b = p_sb;
2537 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2538 return frp->fr_next;
2539 return frp->fr_prev;
2540}
2541
2542/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002543 * Return the tabpage that will be used if the current one is closed.
2544 */
2545 static tabpage_T *
2546alt_tabpage()
2547{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002548 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002549
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002550 /* Use the next tab page if possible. */
2551 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002552 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002553
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002554 /* Find the last but one tab page. */
2555 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2556 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002557 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002558}
2559
2560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 * Find the left-upper window in frame "frp".
2562 */
2563 static win_T *
2564frame2win(frp)
2565 frame_T *frp;
2566{
2567 while (frp->fr_win == NULL)
2568 frp = frp->fr_child;
2569 return frp->fr_win;
2570}
2571
2572/*
2573 * Return TRUE if frame "frp" contains window "wp".
2574 */
2575 static int
2576frame_has_win(frp, wp)
2577 frame_T *frp;
2578 win_T *wp;
2579{
2580 frame_T *p;
2581
2582 if (frp->fr_layout == FR_LEAF)
2583 return frp->fr_win == wp;
2584
2585 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2586 if (frame_has_win(p, wp))
2587 return TRUE;
2588 return FALSE;
2589}
2590
2591/*
2592 * Set a new height for a frame. Recursively sets the height for contained
2593 * frames and windows. Caller must take care of positions.
2594 */
2595 static void
2596frame_new_height(topfrp, height, topfirst, wfh)
2597 frame_T *topfrp;
2598 int height;
2599 int topfirst; /* resize topmost contained frame first */
2600 int wfh; /* obey 'winfixheight' when there is a choice;
2601 may cause the height not to be set */
2602{
2603 frame_T *frp;
2604 int extra_lines;
2605 int h;
2606
2607 if (topfrp->fr_win != NULL)
2608 {
2609 /* Simple case: just one window. */
2610 win_new_height(topfrp->fr_win,
2611 height - topfrp->fr_win->w_status_height);
2612 }
2613#ifdef FEAT_VERTSPLIT
2614 else if (topfrp->fr_layout == FR_ROW)
2615 {
2616 do
2617 {
2618 /* All frames in this row get the same new height. */
2619 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2620 {
2621 frame_new_height(frp, height, topfirst, wfh);
2622 if (frp->fr_height > height)
2623 {
2624 /* Could not fit the windows, make the whole row higher. */
2625 height = frp->fr_height;
2626 break;
2627 }
2628 }
2629 }
2630 while (frp != NULL);
2631 }
2632#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002633 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 /* Complicated case: Resize a column of frames. Resize the bottom
2636 * frame first, frames above that when needed. */
2637
2638 frp = topfrp->fr_child;
2639 if (wfh)
2640 /* Advance past frames with one window with 'wfh' set. */
2641 while (frame_fixed_height(frp))
2642 {
2643 frp = frp->fr_next;
2644 if (frp == NULL)
2645 return; /* no frame without 'wfh', give up */
2646 }
2647 if (!topfirst)
2648 {
2649 /* Find the bottom frame of this column */
2650 while (frp->fr_next != NULL)
2651 frp = frp->fr_next;
2652 if (wfh)
2653 /* Advance back for frames with one window with 'wfh' set. */
2654 while (frame_fixed_height(frp))
2655 frp = frp->fr_prev;
2656 }
2657
2658 extra_lines = height - topfrp->fr_height;
2659 if (extra_lines < 0)
2660 {
2661 /* reduce height of contained frames, bottom or top frame first */
2662 while (frp != NULL)
2663 {
2664 h = frame_minheight(frp, NULL);
2665 if (frp->fr_height + extra_lines < h)
2666 {
2667 extra_lines += frp->fr_height - h;
2668 frame_new_height(frp, h, topfirst, wfh);
2669 }
2670 else
2671 {
2672 frame_new_height(frp, frp->fr_height + extra_lines,
2673 topfirst, wfh);
2674 break;
2675 }
2676 if (topfirst)
2677 {
2678 do
2679 frp = frp->fr_next;
2680 while (wfh && frp != NULL && frame_fixed_height(frp));
2681 }
2682 else
2683 {
2684 do
2685 frp = frp->fr_prev;
2686 while (wfh && frp != NULL && frame_fixed_height(frp));
2687 }
2688 /* Increase "height" if we could not reduce enough frames. */
2689 if (frp == NULL)
2690 height -= extra_lines;
2691 }
2692 }
2693 else if (extra_lines > 0)
2694 {
2695 /* increase height of bottom or top frame */
2696 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2697 }
2698 }
2699 topfrp->fr_height = height;
2700}
2701
2702/*
2703 * Return TRUE if height of frame "frp" should not be changed because of
2704 * the 'winfixheight' option.
2705 */
2706 static int
2707frame_fixed_height(frp)
2708 frame_T *frp;
2709{
2710 /* frame with one window: fixed height if 'winfixheight' set. */
2711 if (frp->fr_win != NULL)
2712 return frp->fr_win->w_p_wfh;
2713
2714 if (frp->fr_layout == FR_ROW)
2715 {
2716 /* The frame is fixed height if one of the frames in the row is fixed
2717 * height. */
2718 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2719 if (frame_fixed_height(frp))
2720 return TRUE;
2721 return FALSE;
2722 }
2723
2724 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2725 * frames in the row are fixed height. */
2726 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2727 if (!frame_fixed_height(frp))
2728 return FALSE;
2729 return TRUE;
2730}
2731
2732#ifdef FEAT_VERTSPLIT
2733/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002734 * Return TRUE if width of frame "frp" should not be changed because of
2735 * the 'winfixwidth' option.
2736 */
2737 static int
2738frame_fixed_width(frp)
2739 frame_T *frp;
2740{
2741 /* frame with one window: fixed width if 'winfixwidth' set. */
2742 if (frp->fr_win != NULL)
2743 return frp->fr_win->w_p_wfw;
2744
2745 if (frp->fr_layout == FR_COL)
2746 {
2747 /* The frame is fixed width if one of the frames in the row is fixed
2748 * width. */
2749 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2750 if (frame_fixed_width(frp))
2751 return TRUE;
2752 return FALSE;
2753 }
2754
2755 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2756 * frames in the row are fixed width. */
2757 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2758 if (!frame_fixed_width(frp))
2759 return FALSE;
2760 return TRUE;
2761}
2762
2763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 * Add a status line to windows at the bottom of "frp".
2765 * Note: Does not check if there is room!
2766 */
2767 static void
2768frame_add_statusline(frp)
2769 frame_T *frp;
2770{
2771 win_T *wp;
2772
2773 if (frp->fr_layout == FR_LEAF)
2774 {
2775 wp = frp->fr_win;
2776 if (wp->w_status_height == 0)
2777 {
2778 if (wp->w_height > 0) /* don't make it negative */
2779 --wp->w_height;
2780 wp->w_status_height = STATUS_HEIGHT;
2781 }
2782 }
2783 else if (frp->fr_layout == FR_ROW)
2784 {
2785 /* Handle all the frames in the row. */
2786 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2787 frame_add_statusline(frp);
2788 }
2789 else /* frp->fr_layout == FR_COL */
2790 {
2791 /* Only need to handle the last frame in the column. */
2792 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2793 ;
2794 frame_add_statusline(frp);
2795 }
2796}
2797
2798/*
2799 * Set width of a frame. Handles recursively going through contained frames.
2800 * May remove separator line for windows at the right side (for win_close()).
2801 */
2802 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002803frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 frame_T *topfrp;
2805 int width;
2806 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002807 int wfw; /* obey 'winfixwidth' when there is a choice;
2808 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 frame_T *frp;
2811 int extra_cols;
2812 int w;
2813 win_T *wp;
2814
2815 if (topfrp->fr_layout == FR_LEAF)
2816 {
2817 /* Simple case: just one window. */
2818 wp = topfrp->fr_win;
2819 /* Find out if there are any windows right of this one. */
2820 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2821 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2822 break;
2823 if (frp->fr_parent == NULL)
2824 wp->w_vsep_width = 0;
2825 win_new_width(wp, width - wp->w_vsep_width);
2826 }
2827 else if (topfrp->fr_layout == FR_COL)
2828 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002829 do
2830 {
2831 /* All frames in this column get the same new width. */
2832 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2833 {
2834 frame_new_width(frp, width, leftfirst, wfw);
2835 if (frp->fr_width > width)
2836 {
2837 /* Could not fit the windows, make whole column wider. */
2838 width = frp->fr_width;
2839 break;
2840 }
2841 }
2842 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844 else /* fr_layout == FR_ROW */
2845 {
2846 /* Complicated case: Resize a row of frames. Resize the rightmost
2847 * frame first, frames left of it when needed. */
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002850 if (wfw)
2851 /* Advance past frames with one window with 'wfw' set. */
2852 while (frame_fixed_width(frp))
2853 {
2854 frp = frp->fr_next;
2855 if (frp == NULL)
2856 return; /* no frame without 'wfw', give up */
2857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002859 {
2860 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 while (frp->fr_next != NULL)
2862 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002863 if (wfw)
2864 /* Advance back for frames with one window with 'wfw' set. */
2865 while (frame_fixed_width(frp))
2866 frp = frp->fr_prev;
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868
2869 extra_cols = width - topfrp->fr_width;
2870 if (extra_cols < 0)
2871 {
2872 /* reduce frame width, rightmost frame first */
2873 while (frp != NULL)
2874 {
2875 w = frame_minwidth(frp, NULL);
2876 if (frp->fr_width + extra_cols < w)
2877 {
2878 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002879 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881 else
2882 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002883 frame_new_width(frp, frp->fr_width + extra_cols,
2884 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
2886 }
2887 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002888 {
2889 do
2890 frp = frp->fr_next;
2891 while (wfw && frp != NULL && frame_fixed_width(frp));
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002894 {
2895 do
2896 frp = frp->fr_prev;
2897 while (wfw && frp != NULL && frame_fixed_width(frp));
2898 }
2899 /* Increase "width" if we could not reduce enough frames. */
2900 if (frp == NULL)
2901 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
2904 else if (extra_cols > 0)
2905 {
2906 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002907 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 }
2910 topfrp->fr_width = width;
2911}
2912
2913/*
2914 * Add the vertical separator to windows at the right side of "frp".
2915 * Note: Does not check if there is room!
2916 */
2917 static void
2918frame_add_vsep(frp)
2919 frame_T *frp;
2920{
2921 win_T *wp;
2922
2923 if (frp->fr_layout == FR_LEAF)
2924 {
2925 wp = frp->fr_win;
2926 if (wp->w_vsep_width == 0)
2927 {
2928 if (wp->w_width > 0) /* don't make it negative */
2929 --wp->w_width;
2930 wp->w_vsep_width = 1;
2931 }
2932 }
2933 else if (frp->fr_layout == FR_COL)
2934 {
2935 /* Handle all the frames in the column. */
2936 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2937 frame_add_vsep(frp);
2938 }
2939 else /* frp->fr_layout == FR_ROW */
2940 {
2941 /* Only need to handle the last frame in the row. */
2942 frp = frp->fr_child;
2943 while (frp->fr_next != NULL)
2944 frp = frp->fr_next;
2945 frame_add_vsep(frp);
2946 }
2947}
2948
2949/*
2950 * Set frame width from the window it contains.
2951 */
2952 static void
2953frame_fix_width(wp)
2954 win_T *wp;
2955{
2956 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2957}
2958#endif
2959
2960/*
2961 * Set frame height from the window it contains.
2962 */
2963 static void
2964frame_fix_height(wp)
2965 win_T *wp;
2966{
2967 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2968}
2969
2970/*
2971 * Compute the minimal height for frame "topfrp".
2972 * Uses the 'winminheight' option.
2973 * When "next_curwin" isn't NULL, use p_wh for this window.
2974 * When "next_curwin" is NOWIN, don't use at least one line for the current
2975 * window.
2976 */
2977 static int
2978frame_minheight(topfrp, next_curwin)
2979 frame_T *topfrp;
2980 win_T *next_curwin;
2981{
2982 frame_T *frp;
2983 int m;
2984#ifdef FEAT_VERTSPLIT
2985 int n;
2986#endif
2987
2988 if (topfrp->fr_win != NULL)
2989 {
2990 if (topfrp->fr_win == next_curwin)
2991 m = p_wh + topfrp->fr_win->w_status_height;
2992 else
2993 {
2994 /* window: minimal height of the window plus status line */
2995 m = p_wmh + topfrp->fr_win->w_status_height;
2996 /* Current window is minimal one line high */
2997 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2998 ++m;
2999 }
3000 }
3001#ifdef FEAT_VERTSPLIT
3002 else if (topfrp->fr_layout == FR_ROW)
3003 {
3004 /* get the minimal height from each frame in this row */
3005 m = 0;
3006 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3007 {
3008 n = frame_minheight(frp, next_curwin);
3009 if (n > m)
3010 m = n;
3011 }
3012 }
3013#endif
3014 else
3015 {
3016 /* Add up the minimal heights for all frames in this column. */
3017 m = 0;
3018 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3019 m += frame_minheight(frp, next_curwin);
3020 }
3021
3022 return m;
3023}
3024
3025#ifdef FEAT_VERTSPLIT
3026/*
3027 * Compute the minimal width for frame "topfrp".
3028 * When "next_curwin" isn't NULL, use p_wiw for this window.
3029 * When "next_curwin" is NOWIN, don't use at least one column for the current
3030 * window.
3031 */
3032 static int
3033frame_minwidth(topfrp, next_curwin)
3034 frame_T *topfrp;
3035 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3036{
3037 frame_T *frp;
3038 int m, n;
3039
3040 if (topfrp->fr_win != NULL)
3041 {
3042 if (topfrp->fr_win == next_curwin)
3043 m = p_wiw + topfrp->fr_win->w_vsep_width;
3044 else
3045 {
3046 /* window: minimal width of the window plus separator column */
3047 m = p_wmw + topfrp->fr_win->w_vsep_width;
3048 /* Current window is minimal one column wide */
3049 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3050 ++m;
3051 }
3052 }
3053 else if (topfrp->fr_layout == FR_COL)
3054 {
3055 /* get the minimal width from each frame in this column */
3056 m = 0;
3057 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3058 {
3059 n = frame_minwidth(frp, next_curwin);
3060 if (n > m)
3061 m = n;
3062 }
3063 }
3064 else
3065 {
3066 /* Add up the minimal widths for all frames in this row. */
3067 m = 0;
3068 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3069 m += frame_minwidth(frp, next_curwin);
3070 }
3071
3072 return m;
3073}
3074#endif
3075
3076
3077/*
3078 * Try to close all windows except current one.
3079 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3080 * used and the buffer was modified.
3081 *
3082 * Used by ":bdel" and ":only".
3083 */
3084 void
3085close_others(message, forceit)
3086 int message;
3087 int forceit; /* always hide all other windows */
3088{
3089 win_T *wp;
3090 win_T *nextwp;
3091 int r;
3092
3093 if (lastwin == firstwin)
3094 {
3095 if (message
3096#ifdef FEAT_AUTOCMD
3097 && !autocmd_busy
3098#endif
3099 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003100 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 return;
3102 }
3103
3104 /* Be very careful here: autocommands may change the window layout. */
3105 for (wp = firstwin; win_valid(wp); wp = nextwp)
3106 {
3107 nextwp = wp->w_next;
3108 if (wp != curwin) /* don't close current window */
3109 {
3110
3111 /* Check if it's allowed to abandon this window */
3112 r = can_abandon(wp->w_buffer, forceit);
3113#ifdef FEAT_AUTOCMD
3114 if (!win_valid(wp)) /* autocommands messed wp up */
3115 {
3116 nextwp = firstwin;
3117 continue;
3118 }
3119#endif
3120 if (!r)
3121 {
3122#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3123 if (message && (p_confirm || cmdmod.confirm) && p_write)
3124 {
3125 dialog_changed(wp->w_buffer, FALSE);
3126# ifdef FEAT_AUTOCMD
3127 if (!win_valid(wp)) /* autocommands messed wp up */
3128 {
3129 nextwp = firstwin;
3130 continue;
3131 }
3132# endif
3133 }
3134 if (bufIsChanged(wp->w_buffer))
3135#endif
3136 continue;
3137 }
3138 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3139 }
3140 }
3141
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003142 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 EMSG(_("E445: Other window contains changes"));
3144}
3145
3146#endif /* FEAT_WINDOWS */
3147
3148/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149 * Init the current window "curwin".
3150 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 */
3152 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003153curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003155 redraw_win_later(curwin, NOT_VALID);
3156 curwin->w_lines_valid = 0;
3157 curwin->w_cursor.lnum = 1;
3158 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003160 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3163 curwin->w_pcmark.col = 0;
3164 curwin->w_prev_pcmark.lnum = 0;
3165 curwin->w_prev_pcmark.col = 0;
3166 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003168 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003170 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#ifdef FEAT_FKMAP
3172 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003173 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003175 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#endif
3177}
3178
3179/*
3180 * Allocate the first window and put an empty buffer in it.
3181 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003182 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185win_alloc_first()
3186{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003187 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003188 return FAIL;
3189
3190#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003191 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003192 if (first_tabpage == NULL)
3193 return FAIL;
3194 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003195 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003196#endif
3197 return OK;
3198}
3199
3200/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003201 * Allocate the first window or the first window in a new tab page.
3202 * When "oldwin" is NULL create an empty buffer for it.
3203 * When "oldwin" is not NULL copy info from it to the new window (only with
3204 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003205 * Return FAIL when something goes wrong (out of memory).
3206 */
3207 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003208win_alloc_firstwin(oldwin)
3209 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003210{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003212 if (oldwin == NULL)
3213 {
3214 /* Very first window, need to create an empty buffer for it and
3215 * initialize from scratch. */
3216 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3217 if (curwin == NULL || curbuf == NULL)
3218 return FAIL;
3219 curwin->w_buffer = curbuf;
3220 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003222 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003224 curwin_init(); /* init current window */
3225 }
3226#ifdef FEAT_WINDOWS
3227 else
3228 {
3229 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003230 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003231
3232# ifdef FEAT_SCROLLBIND
3233 /* We don't want scroll-binding in the first window. */
3234 curwin->w_p_scb = FALSE;
3235# endif
3236 }
3237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
3239 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3240 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 topframe->fr_layout = FR_LEAF;
3243#ifdef FEAT_VERTSPLIT
3244 topframe->fr_width = Columns;
3245#endif
3246 topframe->fr_height = Rows - p_ch;
3247 topframe->fr_win = curwin;
3248 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003249
3250 return OK;
3251}
3252
3253/*
3254 * Initialize the window and frame size to the maximum.
3255 */
3256 void
3257win_init_size()
3258{
3259 firstwin->w_height = ROWS_AVAIL;
3260 topframe->fr_height = ROWS_AVAIL;
3261#ifdef FEAT_VERTSPLIT
3262 firstwin->w_width = Columns;
3263 topframe->fr_width = Columns;
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265}
3266
3267#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003268
3269/*
3270 * Allocate a new tabpage_T and init the values.
3271 * Returns NULL when out of memory.
3272 */
3273 static tabpage_T *
3274alloc_tabpage()
3275{
3276 tabpage_T *tp;
3277
3278 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3279 if (tp != NULL)
3280 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003281# ifdef FEAT_GUI
3282 int i;
3283
3284 for (i = 0; i < 3; i++)
3285 tp->tp_prev_which_scrollbars[i] = -1;
3286# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003287# ifdef FEAT_DIFF
3288 tp->tp_diff_invalid = TRUE;
3289# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290#ifdef FEAT_EVAL
3291 /* init t: variables */
3292 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3293#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003294 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003295 }
3296 return tp;
3297}
3298
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003299 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003300free_tabpage(tp)
3301 tabpage_T *tp;
3302{
3303# ifdef FEAT_DIFF
3304 diff_clear(tp);
3305# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003306 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003307#ifdef FEAT_EVAL
3308 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3309#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003310 vim_free(tp);
3311}
3312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003313/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003314 * Create a new Tab page with one window.
3315 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003316 * When "after" is 0 put it just after the current Tab page.
3317 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318 * Return FAIL or OK.
3319 */
3320 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003321win_new_tabpage(after)
3322 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003324 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003325 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003326 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003327
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003328 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003329 if (newtp == NULL)
3330 return FAIL;
3331
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003332 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003333 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003334 {
3335 vim_free(newtp);
3336 return FAIL;
3337 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003338 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339
3340 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003344 if (after == 1)
3345 {
3346 /* New tab page becomes the first one. */
3347 newtp->tp_next = first_tabpage;
3348 first_tabpage = newtp;
3349 }
3350 else
3351 {
3352 if (after > 0)
3353 {
3354 /* Put new tab page before tab page "after". */
3355 n = 2;
3356 for (tp = first_tabpage; tp->tp_next != NULL
3357 && n < after; tp = tp->tp_next)
3358 ++n;
3359 }
3360 newtp->tp_next = tp->tp_next;
3361 tp->tp_next = newtp;
3362 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003363 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003364 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003365 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003366
3367 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003368 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003369
3370#if defined(FEAT_GUI)
3371 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3372 * scrollbars. Have to update them anyway. */
3373 if (gui.in_use && starting == 0)
3374 {
3375 gui_init_which_components(NULL);
3376 gui_update_scrollbars(TRUE);
3377 }
3378 need_mouse_correct = TRUE;
3379#endif
3380
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003381 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003382#ifdef FEAT_AUTOCMD
3383 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3384 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3385#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 return OK;
3387 }
3388
3389 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003390 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003391 return FAIL;
3392}
3393
3394/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003395 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3396 * like with ":split".
3397 * Returns OK if a new tab page was created, FAIL otherwise.
3398 */
3399 int
3400may_open_tabpage()
3401{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003402 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003403
Bram Moolenaard326ce82007-03-11 14:48:29 +00003404 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003405 {
3406 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003407 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003408 return win_new_tabpage(n);
3409 }
3410 return FAIL;
3411}
3412
3413/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003414 * Create up to "maxcount" tabpages with empty windows.
3415 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003417 int
3418make_tabpages(maxcount)
3419 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003421 int count = maxcount;
3422 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003424 /* Limit to 'tabpagemax' tabs. */
3425 if (count > p_tpm)
3426 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003427
3428#ifdef FEAT_AUTOCMD
3429 /*
3430 * Don't execute autocommands while creating the tab pages. Must do that
3431 * when putting the buffers in the windows.
3432 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003433 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003434#endif
3435
3436 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003437 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003439
3440#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003441 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003442#endif
3443
3444 /* return actual number of tab pages */
3445 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003446}
3447
3448/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003449 * Return TRUE when "tpc" points to a valid tab page.
3450 */
3451 int
3452valid_tabpage(tpc)
3453 tabpage_T *tpc;
3454{
3455 tabpage_T *tp;
3456
3457 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3458 if (tp == tpc)
3459 return TRUE;
3460 return FALSE;
3461}
3462
3463/*
3464 * Find tab page "n" (first one is 1). Returns NULL when not found.
3465 */
3466 tabpage_T *
3467find_tabpage(n)
3468 int n;
3469{
3470 tabpage_T *tp;
3471 int i = 1;
3472
3473 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3474 ++i;
3475 return tp;
3476}
3477
3478/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003479 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003480 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003481 */
3482 int
3483tabpage_index(ftp)
3484 tabpage_T *ftp;
3485{
3486 int i = 1;
3487 tabpage_T *tp;
3488
3489 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3490 ++i;
3491 return i;
3492}
3493
3494/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003495 * Prepare for leaving the current tab page.
3496 * When autocomands change "curtab" we don't leave the tab page and return
3497 * FAIL.
3498 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003499 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003500 static int
3501leave_tabpage(new_curbuf)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003502 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003503 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003504{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003505 tabpage_T *tp = curtab;
3506
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003507#ifdef FEAT_VISUAL
3508 reset_VIsual_and_resel(); /* stop Visual mode */
3509#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003510#ifdef FEAT_AUTOCMD
3511 if (new_curbuf != curbuf)
3512 {
3513 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3514 if (curtab != tp)
3515 return FAIL;
3516 }
3517 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3518 if (curtab != tp)
3519 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003520 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003521 if (curtab != tp)
3522 return FAIL;
3523#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003524#if defined(FEAT_GUI)
3525 /* Remove the scrollbars. They may be added back later. */
3526 if (gui.in_use)
3527 gui_remove_scrollbars();
3528#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003530 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531 tp->tp_firstwin = firstwin;
3532 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003533 tp->tp_old_Rows = Rows;
3534 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535 firstwin = NULL;
3536 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003537 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538}
3539
3540/*
3541 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003542 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544 static void
3545enter_tabpage(tp, old_curbuf)
3546 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003547 buf_T *old_curbuf UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003549 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003550 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553 firstwin = tp->tp_firstwin;
3554 lastwin = tp->tp_lastwin;
3555 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003556
3557 /* We would like doing the TabEnter event first, but we don't have a
3558 * valid current window yet, which may break some commands.
3559 * This triggers autocommands, thus may make "tp" invalid. */
3560 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3561 prevwin = next_prevwin;
3562
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003563#ifdef FEAT_AUTOCMD
3564 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003565
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566 if (old_curbuf != curbuf)
3567 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3568#endif
3569
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003570 last_status(FALSE); /* status line may appear or disappear */
3571 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003572 must_redraw = CLEAR; /* need to redraw everything */
3573#ifdef FEAT_DIFF
3574 diff_need_scrollbind = TRUE;
3575#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003576
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003577 /* The tabpage line may have appeared or disappeared, may need to resize
3578 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003579 * frame sizes too. Use the stored value of p_ch, so that it can be
3580 * different for each tab page. */
3581 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003582 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3583#ifdef FEAT_GUI_TABLINE
3584 && !gui_use_tabline()
3585#endif
3586 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003587 shell_new_rows();
3588#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003589 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003590 shell_new_columns(); /* update window widths */
3591#endif
3592
3593#if defined(FEAT_GUI)
3594 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3595 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003596 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003597 {
3598 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003599 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003600 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003601 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602#endif
3603
3604 redraw_all_later(CLEAR);
3605}
3606
3607/*
3608 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003609 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003610 */
3611 void
3612goto_tabpage(n)
3613 int n;
3614{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003616 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003617 int i;
3618
Bram Moolenaard68071d2006-05-02 22:08:30 +00003619 if (text_locked())
3620 {
3621 /* Not allowed when editing the command line. */
3622#ifdef FEAT_CMDWIN
3623 if (cmdwin_type != 0)
3624 EMSG(_(e_cmdwin));
3625 else
3626#endif
3627 EMSG(_(e_secure));
3628 return;
3629 }
3630
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003631 /* If there is only one it can't work. */
3632 if (first_tabpage->tp_next == NULL)
3633 {
3634 if (n > 1)
3635 beep_flush();
3636 return;
3637 }
3638
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 if (n == 0)
3640 {
3641 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003642 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643 tp = first_tabpage;
3644 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003645 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003647 else if (n < 0)
3648 {
3649 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3650 * this N times. */
3651 ttp = curtab;
3652 for (i = n; i < 0; ++i)
3653 {
3654 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3655 tp = tp->tp_next)
3656 ;
3657 ttp = tp;
3658 }
3659 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660 else if (n == 9999)
3661 {
3662 /* Go to last tab page. */
3663 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3664 ;
3665 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003666 else
3667 {
3668 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003669 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003670 if (tp == NULL)
3671 {
3672 beep_flush();
3673 return;
3674 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675 }
3676
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003677 goto_tabpage_tp(tp);
3678
3679#ifdef FEAT_GUI_TABLINE
3680 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003681 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003682#endif
3683}
3684
3685/*
3686 * Go to tabpage "tp".
3687 * Note: doesn't update the GUI tab.
3688 */
3689 void
3690goto_tabpage_tp(tp)
3691 tabpage_T *tp;
3692{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003693 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003694 {
3695 if (valid_tabpage(tp))
3696 enter_tabpage(tp, curbuf);
3697 else
3698 enter_tabpage(curtab, curbuf);
3699 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003700}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
3702/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003703 * Enter window "wp" in tab page "tp".
3704 * Also updates the GUI tab.
3705 */
3706 void
3707goto_tabpage_win(tp, wp)
3708 tabpage_T *tp;
3709 win_T *wp;
3710{
3711 goto_tabpage_tp(tp);
3712 if (curtab == tp && win_valid(wp))
3713 {
3714 win_enter(wp, TRUE);
3715# ifdef FEAT_GUI_TABLINE
3716 if (gui_use_tabline())
3717 gui_mch_set_curtab(tabpage_index(curtab));
3718# endif
3719 }
3720}
3721
3722/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003723 * Move the current tab page to before tab page "nr".
3724 */
3725 void
3726tabpage_move(nr)
3727 int nr;
3728{
3729 int n = nr;
3730 tabpage_T *tp;
3731
3732 if (first_tabpage->tp_next == NULL)
3733 return;
3734
3735 /* Remove the current tab page from the list of tab pages. */
3736 if (curtab == first_tabpage)
3737 first_tabpage = curtab->tp_next;
3738 else
3739 {
3740 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3741 if (tp->tp_next == curtab)
3742 break;
3743 if (tp == NULL) /* "cannot happen" */
3744 return;
3745 tp->tp_next = curtab->tp_next;
3746 }
3747
3748 /* Re-insert it at the specified position. */
3749 if (n == 0)
3750 {
3751 curtab->tp_next = first_tabpage;
3752 first_tabpage = curtab;
3753 }
3754 else
3755 {
3756 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3757 --n;
3758 curtab->tp_next = tp->tp_next;
3759 tp->tp_next = curtab;
3760 }
3761
3762 /* Need to redraw the tabline. Tab page contents doesn't change. */
3763 redraw_tabline = TRUE;
3764}
3765
3766
3767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 * Go to another window.
3769 * When jumping to another buffer, stop Visual mode. Do this before
3770 * changing windows so we can yank the selection into the '*' register.
3771 * When jumping to another window on the same buffer, adjust its cursor
3772 * position to keep the same Visual area.
3773 */
3774 void
3775win_goto(wp)
3776 win_T *wp;
3777{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003778 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 {
3780 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003781 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return;
3783 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003784#ifdef FEAT_AUTOCMD
3785 if (curbuf_locked())
3786 return;
3787#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789#ifdef FEAT_VISUAL
3790 if (wp->w_buffer != curbuf)
3791 reset_VIsual_and_resel();
3792 else if (VIsual_active)
3793 wp->w_cursor = curwin->w_cursor;
3794#endif
3795
3796#ifdef FEAT_GUI
3797 need_mouse_correct = TRUE;
3798#endif
3799 win_enter(wp, TRUE);
3800}
3801
3802#if defined(FEAT_PERL) || defined(PROTO)
3803/*
3804 * Find window number "winnr" (counting top to bottom).
3805 */
3806 win_T *
3807win_find_nr(winnr)
3808 int winnr;
3809{
3810 win_T *wp;
3811
3812# ifdef FEAT_WINDOWS
3813 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3814 if (--winnr == 0)
3815 break;
3816 return wp;
3817# else
3818 return curwin;
3819# endif
3820}
3821#endif
3822
3823#ifdef FEAT_VERTSPLIT
3824/*
3825 * Move to window above or below "count" times.
3826 */
3827 static void
3828win_goto_ver(up, count)
3829 int up; /* TRUE to go to win above */
3830 long count;
3831{
3832 frame_T *fr;
3833 frame_T *nfr;
3834 frame_T *foundfr;
3835
3836 foundfr = curwin->w_frame;
3837 while (count--)
3838 {
3839 /*
3840 * First go upwards in the tree of frames until we find a upwards or
3841 * downwards neighbor.
3842 */
3843 fr = foundfr;
3844 for (;;)
3845 {
3846 if (fr == topframe)
3847 goto end;
3848 if (up)
3849 nfr = fr->fr_prev;
3850 else
3851 nfr = fr->fr_next;
3852 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3853 break;
3854 fr = fr->fr_parent;
3855 }
3856
3857 /*
3858 * Now go downwards to find the bottom or top frame in it.
3859 */
3860 for (;;)
3861 {
3862 if (nfr->fr_layout == FR_LEAF)
3863 {
3864 foundfr = nfr;
3865 break;
3866 }
3867 fr = nfr->fr_child;
3868 if (nfr->fr_layout == FR_ROW)
3869 {
3870 /* Find the frame at the cursor row. */
3871 while (fr->fr_next != NULL
3872 && frame2win(fr)->w_wincol + fr->fr_width
3873 <= curwin->w_wincol + curwin->w_wcol)
3874 fr = fr->fr_next;
3875 }
3876 if (nfr->fr_layout == FR_COL && up)
3877 while (fr->fr_next != NULL)
3878 fr = fr->fr_next;
3879 nfr = fr;
3880 }
3881 }
3882end:
3883 if (foundfr != NULL)
3884 win_goto(foundfr->fr_win);
3885}
3886
3887/*
3888 * Move to left or right window.
3889 */
3890 static void
3891win_goto_hor(left, count)
3892 int left; /* TRUE to go to left win */
3893 long count;
3894{
3895 frame_T *fr;
3896 frame_T *nfr;
3897 frame_T *foundfr;
3898
3899 foundfr = curwin->w_frame;
3900 while (count--)
3901 {
3902 /*
3903 * First go upwards in the tree of frames until we find a left or
3904 * right neighbor.
3905 */
3906 fr = foundfr;
3907 for (;;)
3908 {
3909 if (fr == topframe)
3910 goto end;
3911 if (left)
3912 nfr = fr->fr_prev;
3913 else
3914 nfr = fr->fr_next;
3915 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3916 break;
3917 fr = fr->fr_parent;
3918 }
3919
3920 /*
3921 * Now go downwards to find the leftmost or rightmost frame in it.
3922 */
3923 for (;;)
3924 {
3925 if (nfr->fr_layout == FR_LEAF)
3926 {
3927 foundfr = nfr;
3928 break;
3929 }
3930 fr = nfr->fr_child;
3931 if (nfr->fr_layout == FR_COL)
3932 {
3933 /* Find the frame at the cursor row. */
3934 while (fr->fr_next != NULL
3935 && frame2win(fr)->w_winrow + fr->fr_height
3936 <= curwin->w_winrow + curwin->w_wrow)
3937 fr = fr->fr_next;
3938 }
3939 if (nfr->fr_layout == FR_ROW && left)
3940 while (fr->fr_next != NULL)
3941 fr = fr->fr_next;
3942 nfr = fr;
3943 }
3944 }
3945end:
3946 if (foundfr != NULL)
3947 win_goto(foundfr->fr_win);
3948}
3949#endif
3950
3951/*
3952 * Make window "wp" the current window.
3953 */
3954 void
3955win_enter(wp, undo_sync)
3956 win_T *wp;
3957 int undo_sync;
3958{
3959 win_enter_ext(wp, undo_sync, FALSE);
3960}
3961
3962/*
3963 * Make window wp the current window.
3964 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3965 * been closed and isn't valid.
3966 */
3967 static void
3968win_enter_ext(wp, undo_sync, curwin_invalid)
3969 win_T *wp;
3970 int undo_sync;
3971 int curwin_invalid;
3972{
3973#ifdef FEAT_AUTOCMD
3974 int other_buffer = FALSE;
3975#endif
3976
3977 if (wp == curwin && !curwin_invalid) /* nothing to do */
3978 return;
3979
3980#ifdef FEAT_AUTOCMD
3981 if (!curwin_invalid)
3982 {
3983 /*
3984 * Be careful: If autocommands delete the window, return now.
3985 */
3986 if (wp->w_buffer != curbuf)
3987 {
3988 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3989 other_buffer = TRUE;
3990 if (!win_valid(wp))
3991 return;
3992 }
3993 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3994 if (!win_valid(wp))
3995 return;
3996# ifdef FEAT_EVAL
3997 /* autocmds may abort script processing */
3998 if (aborting())
3999 return;
4000# endif
4001 }
4002#endif
4003
4004 /* sync undo before leaving the current buffer */
4005 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004006 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 /* may have to copy the buffer options when 'cpo' contains 'S' */
4008 if (wp->w_buffer != curbuf)
4009 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4010 if (!curwin_invalid)
4011 {
4012 prevwin = curwin; /* remember for CTRL-W p */
4013 curwin->w_redr_status = TRUE;
4014 }
4015 curwin = wp;
4016 curbuf = wp->w_buffer;
4017 check_cursor();
4018#ifdef FEAT_VIRTUALEDIT
4019 if (!virtual_active())
4020 curwin->w_cursor.coladd = 0;
4021#endif
4022 changed_line_abv_curs(); /* assume cursor position needs updating */
4023
4024 if (curwin->w_localdir != NULL)
4025 {
4026 /* Window has a local directory: Save current directory as global
4027 * directory (unless that was done already) and change to the local
4028 * directory. */
4029 if (globaldir == NULL)
4030 {
4031 char_u cwd[MAXPATHL];
4032
4033 if (mch_dirname(cwd, MAXPATHL) == OK)
4034 globaldir = vim_strsave(cwd);
4035 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004036 if (mch_chdir((char *)curwin->w_localdir) == 0)
4037 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 else if (globaldir != NULL)
4040 {
4041 /* Window doesn't have a local directory and we are not in the global
4042 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004043 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 vim_free(globaldir);
4045 globaldir = NULL;
4046 shorten_fnames(TRUE);
4047 }
4048
4049#ifdef FEAT_AUTOCMD
4050 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4051 if (other_buffer)
4052 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4053#endif
4054
4055#ifdef FEAT_TITLE
4056 maketitle();
4057#endif
4058 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004059 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (restart_edit)
4061 redraw_later(VALID); /* causes status line redraw */
4062
4063 /* set window height to desired minimal value */
4064 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4065 win_setheight((int)p_wh);
4066 else if (curwin->w_height == 0)
4067 win_setheight(1);
4068
4069#ifdef FEAT_VERTSPLIT
4070 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004071 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 win_setwidth((int)p_wiw);
4073#endif
4074
4075#ifdef FEAT_MOUSE
4076 setmouse(); /* in case jumped to/from help buffer */
4077#endif
4078
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004079 /* Change directories when the 'acd' option is set. */
4080 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081}
4082
4083#endif /* FEAT_WINDOWS */
4084
4085#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4086/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004087 * Jump to the first open window that contains buffer "buf", if one exists.
4088 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 */
4090 win_T *
4091buf_jump_open_win(buf)
4092 buf_T *buf;
4093{
4094# ifdef FEAT_WINDOWS
4095 win_T *wp;
4096
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004097 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (wp->w_buffer == buf)
4099 break;
4100 if (wp != NULL)
4101 win_enter(wp, FALSE);
4102 return wp;
4103# else
4104 if (curwin->w_buffer == buf)
4105 return curwin;
4106 return NULL;
4107# endif
4108}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004109
4110/*
4111 * Jump to the first open window in any tab page that contains buffer "buf",
4112 * if one exists.
4113 * Returns a pointer to the window found, otherwise NULL.
4114 */
4115 win_T *
4116buf_jump_open_tab(buf)
4117 buf_T *buf;
4118{
4119# ifdef FEAT_WINDOWS
4120 win_T *wp;
4121 tabpage_T *tp;
4122
4123 /* First try the current tab page. */
4124 wp = buf_jump_open_win(buf);
4125 if (wp != NULL)
4126 return wp;
4127
4128 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4129 if (tp != curtab)
4130 {
4131 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4132 if (wp->w_buffer == buf)
4133 break;
4134 if (wp != NULL)
4135 {
4136 goto_tabpage_win(tp, wp);
4137 if (curwin != wp)
4138 wp = NULL; /* something went wrong */
4139 break;
4140 }
4141 }
4142
4143 return wp;
4144# else
4145 if (curwin->w_buffer == buf)
4146 return curwin;
4147 return NULL;
4148# endif
4149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#endif
4151
4152/*
4153 * allocate a window structure and link it in the window list
4154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 static win_T *
4156win_alloc(after)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004157 win_T *after UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 win_T *newwin;
4160
4161 /*
4162 * allocate window structure and linesizes arrays
4163 */
4164 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4165 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4166 {
4167 vim_free(newwin);
4168 newwin = NULL;
4169 }
4170
4171 if (newwin != NULL)
4172 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004173#ifdef FEAT_AUTOCMD
4174 /* Don't execute autocommands while the window is not properly
4175 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4176 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004177 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * link the window in the window list
4181 */
4182#ifdef FEAT_WINDOWS
4183 win_append(after, newwin);
4184#endif
4185#ifdef FEAT_VERTSPLIT
4186 newwin->w_wincol = 0;
4187 newwin->w_width = Columns;
4188#endif
4189
4190 /* position the display and the cursor at the top of the file. */
4191 newwin->w_topline = 1;
4192#ifdef FEAT_DIFF
4193 newwin->w_topfill = 0;
4194#endif
4195 newwin->w_botline = 2;
4196 newwin->w_cursor.lnum = 1;
4197#ifdef FEAT_SCROLLBIND
4198 newwin->w_scbind_pos = 1;
4199#endif
4200
4201 /* We won't calculate w_fraction until resizing the window */
4202 newwin->w_fraction = 0;
4203 newwin->w_prev_fraction_row = -1;
4204
4205#ifdef FEAT_GUI
4206 if (gui.in_use)
4207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4209 SBAR_LEFT, newwin);
4210 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4211 SBAR_RIGHT, newwin);
4212 }
4213#endif
4214#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004215 /* init w: variables */
4216 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#endif
4218#ifdef FEAT_FOLDING
4219 foldInitWin(newwin);
4220#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004221#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004222 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004223#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004224#ifdef FEAT_SEARCH_EXTRA
4225 newwin->w_match_head = NULL;
4226 newwin->w_next_match_id = 4;
4227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 return newwin;
4230}
4231
4232#if defined(FEAT_WINDOWS) || defined(PROTO)
4233
4234/*
4235 * remove window 'wp' from the window list and free the structure
4236 */
4237 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004238win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004240 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 int i;
4243
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004244#ifdef FEAT_AUTOCMD
4245 /* Don't execute autocommands while the window is halfway being deleted.
4246 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004247 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004248#endif
4249
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004250#ifdef FEAT_MZSCHEME
4251 mzscheme_window_free(wp);
4252#endif
4253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254#ifdef FEAT_PERL
4255 perl_win_free(wp);
4256#endif
4257
4258#ifdef FEAT_PYTHON
4259 python_window_free(wp);
4260#endif
4261
4262#ifdef FEAT_TCL
4263 tcl_window_free(wp);
4264#endif
4265
4266#ifdef FEAT_RUBY
4267 ruby_window_free(wp);
4268#endif
4269
4270 clear_winopt(&wp->w_onebuf_opt);
4271 clear_winopt(&wp->w_allbuf_opt);
4272
4273#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004274 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275#endif
4276
4277 if (prevwin == wp)
4278 prevwin = NULL;
4279 win_free_lsize(wp);
4280
4281 for (i = 0; i < wp->w_tagstacklen; ++i)
4282 vim_free(wp->w_tagstack[i].tagname);
4283
4284 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004287 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004289
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290#ifdef FEAT_JUMPLIST
4291 free_jumplist(wp);
4292#endif
4293
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004294#ifdef FEAT_QUICKFIX
4295 qf_free_all(wp);
4296#endif
4297
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298#ifdef FEAT_GUI
4299 if (gui.in_use)
4300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4302 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4303 }
4304#endif /* FEAT_GUI */
4305
Bram Moolenaarf740b292006-02-16 22:11:02 +00004306 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004308
4309#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004310 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312}
4313
4314/*
4315 * Append window "wp" in the window list after window "after".
4316 */
4317 static void
4318win_append(after, wp)
4319 win_T *after, *wp;
4320{
4321 win_T *before;
4322
4323 if (after == NULL) /* after NULL is in front of the first */
4324 before = firstwin;
4325 else
4326 before = after->w_next;
4327
4328 wp->w_next = before;
4329 wp->w_prev = after;
4330 if (after == NULL)
4331 firstwin = wp;
4332 else
4333 after->w_next = wp;
4334 if (before == NULL)
4335 lastwin = wp;
4336 else
4337 before->w_prev = wp;
4338}
4339
4340/*
4341 * Remove a window from the window list.
4342 */
4343 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004344win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004346 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
4348 if (wp->w_prev != NULL)
4349 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004350 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004352 else
4353 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (wp->w_next != NULL)
4355 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004356 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004358 else
4359 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362/*
4363 * Append frame "frp" in a frame list after frame "after".
4364 */
4365 static void
4366frame_append(after, frp)
4367 frame_T *after, *frp;
4368{
4369 frp->fr_next = after->fr_next;
4370 after->fr_next = frp;
4371 if (frp->fr_next != NULL)
4372 frp->fr_next->fr_prev = frp;
4373 frp->fr_prev = after;
4374}
4375
4376/*
4377 * Insert frame "frp" in a frame list before frame "before".
4378 */
4379 static void
4380frame_insert(before, frp)
4381 frame_T *before, *frp;
4382{
4383 frp->fr_next = before;
4384 frp->fr_prev = before->fr_prev;
4385 before->fr_prev = frp;
4386 if (frp->fr_prev != NULL)
4387 frp->fr_prev->fr_next = frp;
4388 else
4389 frp->fr_parent->fr_child = frp;
4390}
4391
4392/*
4393 * Remove a frame from a frame list.
4394 */
4395 static void
4396frame_remove(frp)
4397 frame_T *frp;
4398{
4399 if (frp->fr_prev != NULL)
4400 frp->fr_prev->fr_next = frp->fr_next;
4401 else
4402 frp->fr_parent->fr_child = frp->fr_next;
4403 if (frp->fr_next != NULL)
4404 frp->fr_next->fr_prev = frp->fr_prev;
4405}
4406
4407#endif /* FEAT_WINDOWS */
4408
4409/*
4410 * Allocate w_lines[] for window "wp".
4411 * Return FAIL for failure, OK for success.
4412 */
4413 int
4414win_alloc_lines(wp)
4415 win_T *wp;
4416{
4417 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004418 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (wp->w_lines == NULL)
4420 return FAIL;
4421 return OK;
4422}
4423
4424/*
4425 * free lsize arrays for a window
4426 */
4427 void
4428win_free_lsize(wp)
4429 win_T *wp;
4430{
4431 vim_free(wp->w_lines);
4432 wp->w_lines = NULL;
4433}
4434
4435/*
4436 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004437 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 */
4439 void
4440shell_new_rows()
4441{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004442 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 if (firstwin == NULL) /* not initialized yet */
4445 return;
4446#ifdef FEAT_WINDOWS
4447 if (h < frame_minheight(topframe, NULL))
4448 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004449
4450 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * that doesn't result in the right height, forget about that option. */
4452 frame_new_height(topframe, h, FALSE, TRUE);
4453 if (topframe->fr_height != h)
4454 frame_new_height(topframe, h, FALSE, FALSE);
4455
4456 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4457#else
4458 if (h < 1)
4459 h = 1;
4460 win_new_height(firstwin, h);
4461#endif
4462 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004463#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004464 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004465#endif
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#if 0
4468 /* Disabled: don't want making the screen smaller make a window larger. */
4469 if (p_ea)
4470 win_equal(curwin, FALSE, 'v');
4471#endif
4472}
4473
4474#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4475/*
4476 * Called from win_new_shellsize() after Columns changed.
4477 */
4478 void
4479shell_new_columns()
4480{
4481 if (firstwin == NULL) /* not initialized yet */
4482 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004483
4484 /* First try setting the widths of windows with 'winfixwidth'. If that
4485 * doesn't result in the right width, forget about that option. */
4486 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4487 if (topframe->fr_width != Columns)
4488 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4491#if 0
4492 /* Disabled: don't want making the screen smaller make a window larger. */
4493 if (p_ea)
4494 win_equal(curwin, FALSE, 'h');
4495#endif
4496}
4497#endif
4498
4499#if defined(FEAT_CMDWIN) || defined(PROTO)
4500/*
4501 * Save the size of all windows in "gap".
4502 */
4503 void
4504win_size_save(gap)
4505 garray_T *gap;
4506
4507{
4508 win_T *wp;
4509
4510 ga_init2(gap, (int)sizeof(int), 1);
4511 if (ga_grow(gap, win_count() * 2) == OK)
4512 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4513 {
4514 ((int *)gap->ga_data)[gap->ga_len++] =
4515 wp->w_width + wp->w_vsep_width;
4516 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4517 }
4518}
4519
4520/*
4521 * Restore window sizes, but only if the number of windows is still the same.
4522 * Does not free the growarray.
4523 */
4524 void
4525win_size_restore(gap)
4526 garray_T *gap;
4527{
4528 win_T *wp;
4529 int i;
4530
4531 if (win_count() * 2 == gap->ga_len)
4532 {
4533 i = 0;
4534 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4535 {
4536 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4537 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4538 }
4539 /* recompute the window positions */
4540 (void)win_comp_pos();
4541 }
4542}
4543#endif /* FEAT_CMDWIN */
4544
4545#if defined(FEAT_WINDOWS) || defined(PROTO)
4546/*
4547 * Update the position for all windows, using the width and height of the
4548 * frames.
4549 * Returns the row just after the last window.
4550 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004551 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552win_comp_pos()
4553{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004554 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 int col = 0;
4556
4557 frame_comp_pos(topframe, &row, &col);
4558 return row;
4559}
4560
4561/*
4562 * Update the position of the windows in frame "topfrp", using the width and
4563 * height of the frames.
4564 * "*row" and "*col" are the top-left position of the frame. They are updated
4565 * to the bottom-right position plus one.
4566 */
4567 static void
4568frame_comp_pos(topfrp, row, col)
4569 frame_T *topfrp;
4570 int *row;
4571 int *col;
4572{
4573 win_T *wp;
4574 frame_T *frp;
4575#ifdef FEAT_VERTSPLIT
4576 int startcol;
4577 int startrow;
4578#endif
4579
4580 wp = topfrp->fr_win;
4581 if (wp != NULL)
4582 {
4583 if (wp->w_winrow != *row
4584#ifdef FEAT_VERTSPLIT
4585 || wp->w_wincol != *col
4586#endif
4587 )
4588 {
4589 /* position changed, redraw */
4590 wp->w_winrow = *row;
4591#ifdef FEAT_VERTSPLIT
4592 wp->w_wincol = *col;
4593#endif
4594 redraw_win_later(wp, NOT_VALID);
4595 wp->w_redr_status = TRUE;
4596 }
4597 *row += wp->w_height + wp->w_status_height;
4598#ifdef FEAT_VERTSPLIT
4599 *col += wp->w_width + wp->w_vsep_width;
4600#endif
4601 }
4602 else
4603 {
4604#ifdef FEAT_VERTSPLIT
4605 startrow = *row;
4606 startcol = *col;
4607#endif
4608 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4609 {
4610#ifdef FEAT_VERTSPLIT
4611 if (topfrp->fr_layout == FR_ROW)
4612 *row = startrow; /* all frames are at the same row */
4613 else
4614 *col = startcol; /* all frames are at the same col */
4615#endif
4616 frame_comp_pos(frp, row, col);
4617 }
4618 }
4619}
4620
4621#endif /* FEAT_WINDOWS */
4622
4623/*
4624 * Set current window height and take care of repositioning other windows to
4625 * fit around it.
4626 */
4627 void
4628win_setheight(height)
4629 int height;
4630{
4631 win_setheight_win(height, curwin);
4632}
4633
4634/*
4635 * Set the window height of window "win" and take care of repositioning other
4636 * windows to fit around it.
4637 */
4638 void
4639win_setheight_win(height, win)
4640 int height;
4641 win_T *win;
4642{
4643 int row;
4644
4645 if (win == curwin)
4646 {
4647 /* Always keep current window at least one line high, even when
4648 * 'winminheight' is zero. */
4649#ifdef FEAT_WINDOWS
4650 if (height < p_wmh)
4651 height = p_wmh;
4652#endif
4653 if (height == 0)
4654 height = 1;
4655 }
4656
4657#ifdef FEAT_WINDOWS
4658 frame_setheight(win->w_frame, height + win->w_status_height);
4659
4660 /* recompute the window positions */
4661 row = win_comp_pos();
4662#else
4663 if (height > topframe->fr_height)
4664 height = topframe->fr_height;
4665 win->w_height = height;
4666 row = height;
4667#endif
4668
4669 /*
4670 * If there is extra space created between the last window and the command
4671 * line, clear it.
4672 */
4673 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4674 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4675 cmdline_row = row;
4676 msg_row = row;
4677 msg_col = 0;
4678
4679 redraw_all_later(NOT_VALID);
4680}
4681
4682#if defined(FEAT_WINDOWS) || defined(PROTO)
4683
4684/*
4685 * Set the height of a frame to "height" and take care that all frames and
4686 * windows inside it are resized. Also resize frames on the left and right if
4687 * the are in the same FR_ROW frame.
4688 *
4689 * Strategy:
4690 * If the frame is part of a FR_COL frame, try fitting the frame in that
4691 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4692 * go to containing frames to resize them and make room.
4693 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4694 * Check for the minimal height of the FR_ROW frame.
4695 * At the top level we can also use change the command line height.
4696 */
4697 static void
4698frame_setheight(curfrp, height)
4699 frame_T *curfrp;
4700 int height;
4701{
4702 int room; /* total number of lines available */
4703 int take; /* number of lines taken from other windows */
4704 int room_cmdline; /* lines available from cmdline */
4705 int run;
4706 frame_T *frp;
4707 int h;
4708 int room_reserved;
4709
4710 /* If the height already is the desired value, nothing to do. */
4711 if (curfrp->fr_height == height)
4712 return;
4713
4714 if (curfrp->fr_parent == NULL)
4715 {
4716 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004717 if (height > ROWS_AVAIL)
4718 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (height > 0)
4720 frame_new_height(curfrp, height, FALSE, FALSE);
4721 }
4722 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4723 {
4724 /* Row of frames: Also need to resize frames left and right of this
4725 * one. First check for the minimal height of these. */
4726 h = frame_minheight(curfrp->fr_parent, NULL);
4727 if (height < h)
4728 height = h;
4729 frame_setheight(curfrp->fr_parent, height);
4730 }
4731 else
4732 {
4733 /*
4734 * Column of frames: try to change only frames in this column.
4735 */
4736#ifdef FEAT_VERTSPLIT
4737 /*
4738 * Do this twice:
4739 * 1: compute room available, if it's not enough try resizing the
4740 * containing frame.
4741 * 2: compute the room available and adjust the height to it.
4742 * Try not to reduce the height of a window with 'winfixheight' set.
4743 */
4744 for (run = 1; run <= 2; ++run)
4745#else
4746 for (;;)
4747#endif
4748 {
4749 room = 0;
4750 room_reserved = 0;
4751 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4752 frp = frp->fr_next)
4753 {
4754 if (frp != curfrp
4755 && frp->fr_win != NULL
4756 && frp->fr_win->w_p_wfh)
4757 room_reserved += frp->fr_height;
4758 room += frp->fr_height;
4759 if (frp != curfrp)
4760 room -= frame_minheight(frp, NULL);
4761 }
4762#ifdef FEAT_VERTSPLIT
4763 if (curfrp->fr_width != Columns)
4764 room_cmdline = 0;
4765 else
4766#endif
4767 {
4768 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4769 + lastwin->w_height + lastwin->w_status_height);
4770 if (room_cmdline < 0)
4771 room_cmdline = 0;
4772 }
4773
4774 if (height <= room + room_cmdline)
4775 break;
4776#ifdef FEAT_VERTSPLIT
4777 if (run == 2 || curfrp->fr_width == Columns)
4778#endif
4779 {
4780 if (height > room + room_cmdline)
4781 height = room + room_cmdline;
4782 break;
4783 }
4784#ifdef FEAT_VERTSPLIT
4785 frame_setheight(curfrp->fr_parent, height
4786 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4787#endif
4788 /*NOTREACHED*/
4789 }
4790
4791 /*
4792 * Compute the number of lines we will take from others frames (can be
4793 * negative!).
4794 */
4795 take = height - curfrp->fr_height;
4796
4797 /* If there is not enough room, also reduce the height of a window
4798 * with 'winfixheight' set. */
4799 if (height > room + room_cmdline - room_reserved)
4800 room_reserved = room + room_cmdline - height;
4801 /* If there is only a 'winfixheight' window and making the
4802 * window smaller, need to make the other window taller. */
4803 if (take < 0 && room - curfrp->fr_height < room_reserved)
4804 room_reserved = 0;
4805
4806 if (take > 0 && room_cmdline > 0)
4807 {
4808 /* use lines from cmdline first */
4809 if (take < room_cmdline)
4810 room_cmdline = take;
4811 take -= room_cmdline;
4812 topframe->fr_height += room_cmdline;
4813 }
4814
4815 /*
4816 * set the current frame to the new height
4817 */
4818 frame_new_height(curfrp, height, FALSE, FALSE);
4819
4820 /*
4821 * First take lines from the frames after the current frame. If
4822 * that is not enough, takes lines from frames above the current
4823 * frame.
4824 */
4825 for (run = 0; run < 2; ++run)
4826 {
4827 if (run == 0)
4828 frp = curfrp->fr_next; /* 1st run: start with next window */
4829 else
4830 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4831 while (frp != NULL && take != 0)
4832 {
4833 h = frame_minheight(frp, NULL);
4834 if (room_reserved > 0
4835 && frp->fr_win != NULL
4836 && frp->fr_win->w_p_wfh)
4837 {
4838 if (room_reserved >= frp->fr_height)
4839 room_reserved -= frp->fr_height;
4840 else
4841 {
4842 if (frp->fr_height - room_reserved > take)
4843 room_reserved = frp->fr_height - take;
4844 take -= frp->fr_height - room_reserved;
4845 frame_new_height(frp, room_reserved, FALSE, FALSE);
4846 room_reserved = 0;
4847 }
4848 }
4849 else
4850 {
4851 if (frp->fr_height - take < h)
4852 {
4853 take -= frp->fr_height - h;
4854 frame_new_height(frp, h, FALSE, FALSE);
4855 }
4856 else
4857 {
4858 frame_new_height(frp, frp->fr_height - take,
4859 FALSE, FALSE);
4860 take = 0;
4861 }
4862 }
4863 if (run == 0)
4864 frp = frp->fr_next;
4865 else
4866 frp = frp->fr_prev;
4867 }
4868 }
4869 }
4870}
4871
4872#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4873/*
4874 * Set current window width and take care of repositioning other windows to
4875 * fit around it.
4876 */
4877 void
4878win_setwidth(width)
4879 int width;
4880{
4881 win_setwidth_win(width, curwin);
4882}
4883
4884 void
4885win_setwidth_win(width, wp)
4886 int width;
4887 win_T *wp;
4888{
4889 /* Always keep current window at least one column wide, even when
4890 * 'winminwidth' is zero. */
4891 if (wp == curwin)
4892 {
4893 if (width < p_wmw)
4894 width = p_wmw;
4895 if (width == 0)
4896 width = 1;
4897 }
4898
4899 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4900
4901 /* recompute the window positions */
4902 (void)win_comp_pos();
4903
4904 redraw_all_later(NOT_VALID);
4905}
4906
4907/*
4908 * Set the width of a frame to "width" and take care that all frames and
4909 * windows inside it are resized. Also resize frames above and below if the
4910 * are in the same FR_ROW frame.
4911 *
4912 * Strategy is similar to frame_setheight().
4913 */
4914 static void
4915frame_setwidth(curfrp, width)
4916 frame_T *curfrp;
4917 int width;
4918{
4919 int room; /* total number of lines available */
4920 int take; /* number of lines taken from other windows */
4921 int run;
4922 frame_T *frp;
4923 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004924 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 /* If the width already is the desired value, nothing to do. */
4927 if (curfrp->fr_width == width)
4928 return;
4929
4930 if (curfrp->fr_parent == NULL)
4931 /* topframe: can't change width */
4932 return;
4933
4934 if (curfrp->fr_parent->fr_layout == FR_COL)
4935 {
4936 /* Column of frames: Also need to resize frames above and below of
4937 * this one. First check for the minimal width of these. */
4938 w = frame_minwidth(curfrp->fr_parent, NULL);
4939 if (width < w)
4940 width = w;
4941 frame_setwidth(curfrp->fr_parent, width);
4942 }
4943 else
4944 {
4945 /*
4946 * Row of frames: try to change only frames in this row.
4947 *
4948 * Do this twice:
4949 * 1: compute room available, if it's not enough try resizing the
4950 * containing frame.
4951 * 2: compute the room available and adjust the width to it.
4952 */
4953 for (run = 1; run <= 2; ++run)
4954 {
4955 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004956 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4958 frp = frp->fr_next)
4959 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004960 if (frp != curfrp
4961 && frp->fr_win != NULL
4962 && frp->fr_win->w_p_wfw)
4963 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 room += frp->fr_width;
4965 if (frp != curfrp)
4966 room -= frame_minwidth(frp, NULL);
4967 }
4968
4969 if (width <= room)
4970 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004971 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 if (width > room)
4974 width = room;
4975 break;
4976 }
4977 frame_setwidth(curfrp->fr_parent, width
4978 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4979 }
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /*
4982 * Compute the number of lines we will take from others frames (can be
4983 * negative!).
4984 */
4985 take = width - curfrp->fr_width;
4986
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004987 /* If there is not enough room, also reduce the width of a window
4988 * with 'winfixwidth' set. */
4989 if (width > room - room_reserved)
4990 room_reserved = room - width;
4991 /* If there is only a 'winfixwidth' window and making the
4992 * window smaller, need to make the other window narrower. */
4993 if (take < 0 && room - curfrp->fr_width < room_reserved)
4994 room_reserved = 0;
4995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 /*
4997 * set the current frame to the new width
4998 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004999 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000
5001 /*
5002 * First take lines from the frames right of the current frame. If
5003 * that is not enough, takes lines from frames left of the current
5004 * frame.
5005 */
5006 for (run = 0; run < 2; ++run)
5007 {
5008 if (run == 0)
5009 frp = curfrp->fr_next; /* 1st run: start with next window */
5010 else
5011 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5012 while (frp != NULL && take != 0)
5013 {
5014 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005015 if (room_reserved > 0
5016 && frp->fr_win != NULL
5017 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005019 if (room_reserved >= frp->fr_width)
5020 room_reserved -= frp->fr_width;
5021 else
5022 {
5023 if (frp->fr_width - room_reserved > take)
5024 room_reserved = frp->fr_width - take;
5025 take -= frp->fr_width - room_reserved;
5026 frame_new_width(frp, room_reserved, FALSE, FALSE);
5027 room_reserved = 0;
5028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 else
5031 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005032 if (frp->fr_width - take < w)
5033 {
5034 take -= frp->fr_width - w;
5035 frame_new_width(frp, w, FALSE, FALSE);
5036 }
5037 else
5038 {
5039 frame_new_width(frp, frp->fr_width - take,
5040 FALSE, FALSE);
5041 take = 0;
5042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 if (run == 0)
5045 frp = frp->fr_next;
5046 else
5047 frp = frp->fr_prev;
5048 }
5049 }
5050 }
5051}
5052#endif /* FEAT_VERTSPLIT */
5053
5054/*
5055 * Check 'winminheight' for a valid value.
5056 */
5057 void
5058win_setminheight()
5059{
5060 int room;
5061 int first = TRUE;
5062 win_T *wp;
5063
5064 /* loop until there is a 'winminheight' that is possible */
5065 while (p_wmh > 0)
5066 {
5067 /* TODO: handle vertical splits */
5068 room = -p_wh;
5069 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5070 room += wp->w_height - p_wmh;
5071 if (room >= 0)
5072 break;
5073 --p_wmh;
5074 if (first)
5075 {
5076 EMSG(_(e_noroom));
5077 first = FALSE;
5078 }
5079 }
5080}
5081
5082#ifdef FEAT_MOUSE
5083
5084/*
5085 * Status line of dragwin is dragged "offset" lines down (negative is up).
5086 */
5087 void
5088win_drag_status_line(dragwin, offset)
5089 win_T *dragwin;
5090 int offset;
5091{
5092 frame_T *curfr;
5093 frame_T *fr;
5094 int room;
5095 int row;
5096 int up; /* if TRUE, drag status line up, otherwise down */
5097 int n;
5098
5099 fr = dragwin->w_frame;
5100 curfr = fr;
5101 if (fr != topframe) /* more than one window */
5102 {
5103 fr = fr->fr_parent;
5104 /* When the parent frame is not a column of frames, its parent should
5105 * be. */
5106 if (fr->fr_layout != FR_COL)
5107 {
5108 curfr = fr;
5109 if (fr != topframe) /* only a row of windows, may drag statusline */
5110 fr = fr->fr_parent;
5111 }
5112 }
5113
5114 /* If this is the last frame in a column, may want to resize the parent
5115 * frame instead (go two up to skip a row of frames). */
5116 while (curfr != topframe && curfr->fr_next == NULL)
5117 {
5118 if (fr != topframe)
5119 fr = fr->fr_parent;
5120 curfr = fr;
5121 if (fr != topframe)
5122 fr = fr->fr_parent;
5123 }
5124
5125 if (offset < 0) /* drag up */
5126 {
5127 up = TRUE;
5128 offset = -offset;
5129 /* sum up the room of the current frame and above it */
5130 if (fr == curfr)
5131 {
5132 /* only one window */
5133 room = fr->fr_height - frame_minheight(fr, NULL);
5134 }
5135 else
5136 {
5137 room = 0;
5138 for (fr = fr->fr_child; ; fr = fr->fr_next)
5139 {
5140 room += fr->fr_height - frame_minheight(fr, NULL);
5141 if (fr == curfr)
5142 break;
5143 }
5144 }
5145 fr = curfr->fr_next; /* put fr at frame that grows */
5146 }
5147 else /* drag down */
5148 {
5149 up = FALSE;
5150 /*
5151 * Only dragging the last status line can reduce p_ch.
5152 */
5153 room = Rows - cmdline_row;
5154 if (curfr->fr_next == NULL)
5155 room -= 1;
5156 else
5157 room -= p_ch;
5158 if (room < 0)
5159 room = 0;
5160 /* sum up the room of frames below of the current one */
5161 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5162 room += fr->fr_height - frame_minheight(fr, NULL);
5163 fr = curfr; /* put fr at window that grows */
5164 }
5165
5166 if (room < offset) /* Not enough room */
5167 offset = room; /* Move as far as we can */
5168 if (offset <= 0)
5169 return;
5170
5171 /*
5172 * Grow frame fr by "offset" lines.
5173 * Doesn't happen when dragging the last status line up.
5174 */
5175 if (fr != NULL)
5176 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5177
5178 if (up)
5179 fr = curfr; /* current frame gets smaller */
5180 else
5181 fr = curfr->fr_next; /* next frame gets smaller */
5182
5183 /*
5184 * Now make the other frames smaller.
5185 */
5186 while (fr != NULL && offset > 0)
5187 {
5188 n = frame_minheight(fr, NULL);
5189 if (fr->fr_height - offset <= n)
5190 {
5191 offset -= fr->fr_height - n;
5192 frame_new_height(fr, n, !up, FALSE);
5193 }
5194 else
5195 {
5196 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5197 break;
5198 }
5199 if (up)
5200 fr = fr->fr_prev;
5201 else
5202 fr = fr->fr_next;
5203 }
5204 row = win_comp_pos();
5205 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5206 cmdline_row = row;
5207 p_ch = Rows - cmdline_row;
5208 if (p_ch < 1)
5209 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005210 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005211 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 showmode();
5213}
5214
5215#ifdef FEAT_VERTSPLIT
5216/*
5217 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5218 */
5219 void
5220win_drag_vsep_line(dragwin, offset)
5221 win_T *dragwin;
5222 int offset;
5223{
5224 frame_T *curfr;
5225 frame_T *fr;
5226 int room;
5227 int left; /* if TRUE, drag separator line left, otherwise right */
5228 int n;
5229
5230 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005231 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 return;
5233 curfr = fr;
5234 fr = fr->fr_parent;
5235 /* When the parent frame is not a row of frames, its parent should be. */
5236 if (fr->fr_layout != FR_ROW)
5237 {
5238 if (fr == topframe) /* only a column of windows (cannot happen?) */
5239 return;
5240 curfr = fr;
5241 fr = fr->fr_parent;
5242 }
5243
5244 /* If this is the last frame in a row, may want to resize a parent
5245 * frame instead. */
5246 while (curfr->fr_next == NULL)
5247 {
5248 if (fr == topframe)
5249 break;
5250 curfr = fr;
5251 fr = fr->fr_parent;
5252 if (fr != topframe)
5253 {
5254 curfr = fr;
5255 fr = fr->fr_parent;
5256 }
5257 }
5258
5259 if (offset < 0) /* drag left */
5260 {
5261 left = TRUE;
5262 offset = -offset;
5263 /* sum up the room of the current frame and left of it */
5264 room = 0;
5265 for (fr = fr->fr_child; ; fr = fr->fr_next)
5266 {
5267 room += fr->fr_width - frame_minwidth(fr, NULL);
5268 if (fr == curfr)
5269 break;
5270 }
5271 fr = curfr->fr_next; /* put fr at frame that grows */
5272 }
5273 else /* drag right */
5274 {
5275 left = FALSE;
5276 /* sum up the room of frames right of the current one */
5277 room = 0;
5278 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5279 room += fr->fr_width - frame_minwidth(fr, NULL);
5280 fr = curfr; /* put fr at window that grows */
5281 }
5282
5283 if (room < offset) /* Not enough room */
5284 offset = room; /* Move as far as we can */
5285 if (offset <= 0) /* No room at all, quit. */
5286 return;
5287
5288 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005289 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /* shrink other frames: current and at the left or at the right */
5292 if (left)
5293 fr = curfr; /* current frame gets smaller */
5294 else
5295 fr = curfr->fr_next; /* next frame gets smaller */
5296
5297 while (fr != NULL && offset > 0)
5298 {
5299 n = frame_minwidth(fr, NULL);
5300 if (fr->fr_width - offset <= n)
5301 {
5302 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005303 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 else
5306 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005307 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 break;
5309 }
5310 if (left)
5311 fr = fr->fr_prev;
5312 else
5313 fr = fr->fr_next;
5314 }
5315 (void)win_comp_pos();
5316 redraw_all_later(NOT_VALID);
5317}
5318#endif /* FEAT_VERTSPLIT */
5319#endif /* FEAT_MOUSE */
5320
5321#endif /* FEAT_WINDOWS */
5322
5323/*
5324 * Set the height of a window.
5325 * This takes care of the things inside the window, not what happens to the
5326 * window position, the frame or to other windows.
5327 */
5328 static void
5329win_new_height(wp, height)
5330 win_T *wp;
5331 int height;
5332{
5333 linenr_T lnum;
5334 int sline, line_size;
5335#define FRACTION_MULT 16384L
5336
5337 /* Don't want a negative height. Happens when splitting a tiny window.
5338 * Will equalize heights soon to fix it. */
5339 if (height < 0)
5340 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005341 if (wp->w_height == height)
5342 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5345 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5346 + FRACTION_MULT / 2) / (long)wp->w_height;
5347
5348 wp->w_height = height;
5349 wp->w_skipcol = 0;
5350
5351 /* Don't change w_topline when height is zero. Don't set w_topline when
5352 * 'scrollbind' is set and this isn't the current window. */
5353 if (height > 0
5354#ifdef FEAT_SCROLLBIND
5355 && (!wp->w_p_scb || wp == curwin)
5356#endif
5357 )
5358 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005359 /*
5360 * Find a value for w_topline that shows the cursor at the same
5361 * relative position in the window as before (more or less).
5362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 lnum = wp->w_cursor.lnum;
5364 if (lnum < 1) /* can happen when starting up */
5365 lnum = 1;
5366 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5367 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5368 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005369
5370 if (sline >= 0)
5371 {
5372 /* Make sure the whole cursor line is visible, if possible. */
5373 int rows = plines_win(wp, lnum, FALSE);
5374
5375 if (sline > wp->w_height - rows)
5376 {
5377 sline = wp->w_height - rows;
5378 wp->w_wrow -= rows - line_size;
5379 }
5380 }
5381
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 if (sline < 0)
5383 {
5384 /*
5385 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005386 * Make cursor line the first line in the window. If not enough
5387 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 */
5389 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005390 if (wp->w_wrow >= wp->w_height
5391 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5392 {
5393 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5394 --wp->w_wrow;
5395 while (wp->w_wrow >= wp->w_height)
5396 {
5397 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5398 + win_col_off2(wp);
5399 --wp->w_wrow;
5400 }
5401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 else
5404 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005405 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407#ifdef FEAT_FOLDING
5408 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5409 if (lnum == 1)
5410 {
5411 /* first line in buffer is folded */
5412 line_size = 1;
5413 --sline;
5414 break;
5415 }
5416#endif
5417 --lnum;
5418#ifdef FEAT_DIFF
5419 if (lnum == wp->w_topline)
5420 line_size = plines_win_nofill(wp, lnum, TRUE)
5421 + wp->w_topfill;
5422 else
5423#endif
5424 line_size = plines_win(wp, lnum, TRUE);
5425 sline -= line_size;
5426 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 if (sline < 0)
5429 {
5430 /*
5431 * Line we want at top would go off top of screen. Use next
5432 * line instead.
5433 */
5434#ifdef FEAT_FOLDING
5435 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5436#endif
5437 lnum++;
5438 wp->w_wrow -= line_size + sline;
5439 }
5440 else if (sline > 0)
5441 {
5442 /* First line of file reached, use that as topline. */
5443 lnum = 1;
5444 wp->w_wrow -= sline;
5445 }
5446 }
5447 set_topline(wp, lnum);
5448 }
5449
5450 if (wp == curwin)
5451 {
5452 if (p_so)
5453 update_topline();
5454 curs_columns(FALSE); /* validate w_wrow */
5455 }
5456 wp->w_prev_fraction_row = wp->w_wrow;
5457
5458 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005459 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460#ifdef FEAT_WINDOWS
5461 wp->w_redr_status = TRUE;
5462#endif
5463 invalidate_botline_win(wp);
5464}
5465
5466#ifdef FEAT_VERTSPLIT
5467/*
5468 * Set the width of a window.
5469 */
5470 static void
5471win_new_width(wp, width)
5472 win_T *wp;
5473 int width;
5474{
5475 wp->w_width = width;
5476 wp->w_lines_valid = 0;
5477 changed_line_abv_curs_win(wp);
5478 invalidate_botline_win(wp);
5479 if (wp == curwin)
5480 {
5481 update_topline();
5482 curs_columns(TRUE); /* validate w_wrow */
5483 }
5484 redraw_win_later(wp, NOT_VALID);
5485 wp->w_redr_status = TRUE;
5486}
5487#endif
5488
5489 void
5490win_comp_scroll(wp)
5491 win_T *wp;
5492{
5493 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5494 if (wp->w_p_scr == 0)
5495 wp->w_p_scr = 1;
5496}
5497
5498/*
5499 * command_height: called whenever p_ch has been changed
5500 */
5501 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005502command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504#ifdef FEAT_WINDOWS
5505 int h;
5506 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005507 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005509 /* Use the value of p_ch that we remembered. This is needed for when the
5510 * GUI starts up, we can't be sure in what order things happen. And when
5511 * p_ch was changed in another tab page. */
5512 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005513
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 /* Find bottom frame with width of screen. */
5515 frp = lastwin->w_frame;
5516# ifdef FEAT_VERTSPLIT
5517 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5518 frp = frp->fr_parent;
5519# endif
5520
5521 /* Avoid changing the height of a window with 'winfixheight' set. */
5522 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5523 && frp->fr_win->w_p_wfh)
5524 frp = frp->fr_prev;
5525
5526 if (starting != NO_SCREEN)
5527 {
5528 cmdline_row = Rows - p_ch;
5529
5530 if (p_ch > old_p_ch) /* p_ch got bigger */
5531 {
5532 while (p_ch > old_p_ch)
5533 {
5534 if (frp == NULL)
5535 {
5536 EMSG(_(e_noroom));
5537 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005538 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 cmdline_row = Rows - p_ch;
5540 break;
5541 }
5542 h = frp->fr_height - frame_minheight(frp, NULL);
5543 if (h > p_ch - old_p_ch)
5544 h = p_ch - old_p_ch;
5545 old_p_ch += h;
5546 frame_add_height(frp, -h);
5547 frp = frp->fr_prev;
5548 }
5549
5550 /* Recompute window positions. */
5551 (void)win_comp_pos();
5552
5553 /* clear the lines added to cmdline */
5554 if (full_screen)
5555 screen_fill((int)(cmdline_row), (int)Rows, 0,
5556 (int)Columns, ' ', ' ', 0);
5557 msg_row = cmdline_row;
5558 redraw_cmdline = TRUE;
5559 return;
5560 }
5561
5562 if (msg_row < cmdline_row)
5563 msg_row = cmdline_row;
5564 redraw_cmdline = TRUE;
5565 }
5566 frame_add_height(frp, (int)(old_p_ch - p_ch));
5567
5568 /* Recompute window positions. */
5569 if (frp != lastwin->w_frame)
5570 (void)win_comp_pos();
5571#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005573 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574#endif
5575}
5576
5577#if defined(FEAT_WINDOWS) || defined(PROTO)
5578/*
5579 * Resize frame "frp" to be "n" lines higher (negative for less high).
5580 * Also resize the frames it is contained in.
5581 */
5582 static void
5583frame_add_height(frp, n)
5584 frame_T *frp;
5585 int n;
5586{
5587 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5588 for (;;)
5589 {
5590 frp = frp->fr_parent;
5591 if (frp == NULL)
5592 break;
5593 frp->fr_height += n;
5594 }
5595}
5596
5597/*
5598 * Add or remove a status line for the bottom window(s), according to the
5599 * value of 'laststatus'.
5600 */
5601 void
5602last_status(morewin)
5603 int morewin; /* pretend there are two or more windows */
5604{
5605 /* Don't make a difference between horizontal or vertical split. */
5606 last_status_rec(topframe, (p_ls == 2
5607 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5608}
5609
5610 static void
5611last_status_rec(fr, statusline)
5612 frame_T *fr;
5613 int statusline;
5614{
5615 frame_T *fp;
5616 win_T *wp;
5617
5618 if (fr->fr_layout == FR_LEAF)
5619 {
5620 wp = fr->fr_win;
5621 if (wp->w_status_height != 0 && !statusline)
5622 {
5623 /* remove status line */
5624 win_new_height(wp, wp->w_height + 1);
5625 wp->w_status_height = 0;
5626 comp_col();
5627 }
5628 else if (wp->w_status_height == 0 && statusline)
5629 {
5630 /* Find a frame to take a line from. */
5631 fp = fr;
5632 while (fp->fr_height <= frame_minheight(fp, NULL))
5633 {
5634 if (fp == topframe)
5635 {
5636 EMSG(_(e_noroom));
5637 return;
5638 }
5639 /* In a column of frames: go to frame above. If already at
5640 * the top or in a row of frames: go to parent. */
5641 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5642 fp = fp->fr_prev;
5643 else
5644 fp = fp->fr_parent;
5645 }
5646 wp->w_status_height = 1;
5647 if (fp != fr)
5648 {
5649 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5650 frame_fix_height(wp);
5651 (void)win_comp_pos();
5652 }
5653 else
5654 win_new_height(wp, wp->w_height - 1);
5655 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005656 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
5658 }
5659#ifdef FEAT_VERTSPLIT
5660 else if (fr->fr_layout == FR_ROW)
5661 {
5662 /* vertically split windows, set status line for each one */
5663 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5664 last_status_rec(fp, statusline);
5665 }
5666#endif
5667 else
5668 {
5669 /* horizontally split window, set status line for last one */
5670 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5671 ;
5672 last_status_rec(fp, statusline);
5673 }
5674}
5675
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005676/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005677 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005678 */
5679 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005680tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005681{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005682#ifdef FEAT_GUI_TABLINE
5683 /* When the GUI has the tabline then this always returns zero. */
5684 if (gui_use_tabline())
5685 return 0;
5686#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005687 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005688 {
5689 case 0: return 0;
5690 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5691 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005692 return 1;
5693}
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695#endif /* FEAT_WINDOWS */
5696
5697#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5698/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005699 * Get the file name at the cursor.
5700 * If Visual mode is active, use the selected text if it's in one line.
5701 * Returns the name in allocated memory, NULL for failure.
5702 */
5703 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005704grab_file_name(count, file_lnum)
5705 long count;
5706 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005707{
5708# ifdef FEAT_VISUAL
5709 if (VIsual_active)
5710 {
5711 int len;
5712 char_u *ptr;
5713
5714 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5715 return NULL;
5716 return find_file_name_in_path(ptr, len,
5717 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5718 }
5719# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005720 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5721 file_lnum);
5722
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005723}
5724
5725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 * Return the file name under or after the cursor.
5727 *
5728 * The 'path' option is searched if the file name is not absolute.
5729 * The string returned has been alloc'ed and should be freed by the caller.
5730 * NULL is returned if the file name or file is not found.
5731 *
5732 * options:
5733 * FNAME_MESS give error messages
5734 * FNAME_EXP expand to path
5735 * FNAME_HYP check for hypertext link
5736 * FNAME_INCL apply "includeexpr"
5737 */
5738 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005739file_name_at_cursor(options, count, file_lnum)
5740 int options;
5741 long count;
5742 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005745 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5746 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
5748
5749/*
5750 * Return the name of the file under or after ptr[col].
5751 * Otherwise like file_name_at_cursor().
5752 */
5753 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005754file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 char_u *line;
5756 int col;
5757 int options;
5758 long count;
5759 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005760 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 char_u *ptr;
5763 int len;
5764
5765 /*
5766 * search forward for what could be the start of a file name
5767 */
5768 ptr = line + col;
5769 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005770 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (*ptr == NUL) /* nothing found */
5772 {
5773 if (options & FNAME_MESS)
5774 EMSG(_("E446: No file name under cursor"));
5775 return NULL;
5776 }
5777
5778 /*
5779 * Search backward for first char of the file name.
5780 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5781 */
5782 while (ptr > line)
5783 {
5784#ifdef FEAT_MBYTE
5785 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5786 ptr -= len + 1;
5787 else
5788#endif
5789 if (vim_isfilec(ptr[-1])
5790 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5791 --ptr;
5792 else
5793 break;
5794 }
5795
5796 /*
5797 * Search forward for the last char of the file name.
5798 * Also allow "://" when ':' is not in 'isfname'.
5799 */
5800 len = 0;
5801 while (vim_isfilec(ptr[len])
5802 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5803#ifdef FEAT_MBYTE
5804 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005805 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 else
5807#endif
5808 ++len;
5809
5810 /*
5811 * If there is trailing punctuation, remove it.
5812 * But don't remove "..", could be a directory name.
5813 */
5814 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5815 && ptr[len - 2] != '.')
5816 --len;
5817
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005818 if (file_lnum != NULL)
5819 {
5820 char_u *p;
5821
5822 /* Get the number after the file name and a separator character */
5823 p = ptr + len;
5824 p = skipwhite(p);
5825 if (*p != NUL)
5826 {
5827 if (!isdigit(*p))
5828 ++p; /* skip the separator */
5829 p = skipwhite(p);
5830 if (isdigit(*p))
5831 *file_lnum = (int)getdigits(&p);
5832 }
5833 }
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5836}
5837
5838# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5839static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5840
5841 static char_u *
5842eval_includeexpr(ptr, len)
5843 char_u *ptr;
5844 int len;
5845{
5846 char_u *res;
5847
5848 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005849 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005850 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 set_vim_var_string(VV_FNAME, NULL, 0);
5852 return res;
5853}
5854#endif
5855
5856/*
5857 * Return the name of the file ptr[len] in 'path'.
5858 * Otherwise like file_name_at_cursor().
5859 */
5860 char_u *
5861find_file_name_in_path(ptr, len, options, count, rel_fname)
5862 char_u *ptr;
5863 int len;
5864 int options;
5865 long count;
5866 char_u *rel_fname; /* file we are searching relative to */
5867{
5868 char_u *file_name;
5869 int c;
5870# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5871 char_u *tofree = NULL;
5872
5873 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5874 {
5875 tofree = eval_includeexpr(ptr, len);
5876 if (tofree != NULL)
5877 {
5878 ptr = tofree;
5879 len = (int)STRLEN(ptr);
5880 }
5881 }
5882# endif
5883
5884 if (options & FNAME_EXP)
5885 {
5886 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5887 TRUE, rel_fname);
5888
5889# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5890 /*
5891 * If the file could not be found in a normal way, try applying
5892 * 'includeexpr' (unless done already).
5893 */
5894 if (file_name == NULL
5895 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5896 {
5897 tofree = eval_includeexpr(ptr, len);
5898 if (tofree != NULL)
5899 {
5900 ptr = tofree;
5901 len = (int)STRLEN(ptr);
5902 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5903 TRUE, rel_fname);
5904 }
5905 }
5906# endif
5907 if (file_name == NULL && (options & FNAME_MESS))
5908 {
5909 c = ptr[len];
5910 ptr[len] = NUL;
5911 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5912 ptr[len] = c;
5913 }
5914
5915 /* Repeat finding the file "count" times. This matters when it
5916 * appears several times in the path. */
5917 while (file_name != NULL && --count > 0)
5918 {
5919 vim_free(file_name);
5920 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5921 }
5922 }
5923 else
5924 file_name = vim_strnsave(ptr, len);
5925
5926# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5927 vim_free(tofree);
5928# endif
5929
5930 return file_name;
5931}
5932#endif /* FEAT_SEARCHPATH */
5933
5934/*
5935 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5936 * Also check for ":\\", which MS Internet Explorer accepts, return
5937 * URL_BACKSLASH.
5938 */
5939 static int
5940path_is_url(p)
5941 char_u *p;
5942{
5943 if (STRNCMP(p, "://", (size_t)3) == 0)
5944 return URL_SLASH;
5945 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5946 return URL_BACKSLASH;
5947 return 0;
5948}
5949
5950/*
5951 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5952 * Return URL_BACKSLASH for "name:\\".
5953 * Return zero otherwise.
5954 */
5955 int
5956path_with_url(fname)
5957 char_u *fname;
5958{
5959 char_u *p;
5960
5961 for (p = fname; isalpha(*p); ++p)
5962 ;
5963 return path_is_url(p);
5964}
5965
5966/*
5967 * Return TRUE if "name" is a full (absolute) path name or URL.
5968 */
5969 int
5970vim_isAbsName(name)
5971 char_u *name;
5972{
5973 return (path_with_url(name) != 0 || mch_isFullName(name));
5974}
5975
5976/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005977 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 *
5979 * return FAIL for failure, OK otherwise
5980 */
5981 int
5982vim_FullName(fname, buf, len, force)
5983 char_u *fname, *buf;
5984 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005985 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 int retval = OK;
5988 int url;
5989
5990 *buf = NUL;
5991 if (fname == NULL)
5992 return FAIL;
5993
5994 url = path_with_url(fname);
5995 if (!url)
5996 retval = mch_FullName(fname, buf, len, force);
5997 if (url || retval == FAIL)
5998 {
5999 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006000 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6003 slash_adjust(buf);
6004#endif
6005 return retval;
6006}
6007
6008/*
6009 * Return the minimal number of rows that is needed on the screen to display
6010 * the current number of windows.
6011 */
6012 int
6013min_rows()
6014{
6015 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006016#ifdef FEAT_WINDOWS
6017 tabpage_T *tp;
6018 int n;
6019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021 if (firstwin == NULL) /* not initialized yet */
6022 return MIN_LINES;
6023
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006025 total = 0;
6026 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6027 {
6028 n = frame_minheight(tp->tp_topframe, NULL);
6029 if (total < n)
6030 total = n;
6031 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006032 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006034 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006036 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 return total;
6038}
6039
6040/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006041 * Return TRUE if there is only one window (in the current tab page), not
6042 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
6044 int
6045only_one_window()
6046{
6047#ifdef FEAT_WINDOWS
6048 int count = 0;
6049 win_T *wp;
6050
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006051 /* If there is another tab page there always is another window. */
6052 if (first_tabpage->tp_next != NULL)
6053 return FALSE;
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006056 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057# ifdef FEAT_QUICKFIX
6058 || wp->w_p_pvw
6059# endif
6060 ) || wp == curwin)
6061 ++count;
6062 return (count <= 1);
6063#else
6064 return TRUE;
6065#endif
6066}
6067
6068#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6069/*
6070 * Correct the cursor line number in other windows. Used after changing the
6071 * current buffer, and before applying autocommands.
6072 * When "do_curwin" is TRUE, also check current window.
6073 */
6074 void
6075check_lnums(do_curwin)
6076 int do_curwin;
6077{
6078 win_T *wp;
6079
6080#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006081 tabpage_T *tp;
6082
6083 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6085#else
6086 wp = curwin;
6087 if (do_curwin)
6088#endif
6089 {
6090 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6091 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6092 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6093 wp->w_topline = curbuf->b_ml.ml_line_count;
6094 }
6095}
6096#endif
6097
6098#if defined(FEAT_WINDOWS) || defined(PROTO)
6099
6100/*
6101 * A snapshot of the window sizes, to restore them after closing the help
6102 * window.
6103 * Only these fields are used:
6104 * fr_layout
6105 * fr_width
6106 * fr_height
6107 * fr_next
6108 * fr_child
6109 * fr_win (only valid for the old curwin, NULL otherwise)
6110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111
6112/*
6113 * Create a snapshot of the current frame sizes.
6114 */
6115 static void
6116make_snapshot()
6117{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006118 clear_snapshot(curtab);
6119 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120}
6121
6122 static void
6123make_snapshot_rec(fr, frp)
6124 frame_T *fr;
6125 frame_T **frp;
6126{
6127 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6128 if (*frp == NULL)
6129 return;
6130 (*frp)->fr_layout = fr->fr_layout;
6131# ifdef FEAT_VERTSPLIT
6132 (*frp)->fr_width = fr->fr_width;
6133# endif
6134 (*frp)->fr_height = fr->fr_height;
6135 if (fr->fr_next != NULL)
6136 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6137 if (fr->fr_child != NULL)
6138 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6139 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6140 (*frp)->fr_win = curwin;
6141}
6142
6143/*
6144 * Remove any existing snapshot.
6145 */
6146 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147clear_snapshot(tp)
6148 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006150 clear_snapshot_rec(tp->tp_snapshot);
6151 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152}
6153
6154 static void
6155clear_snapshot_rec(fr)
6156 frame_T *fr;
6157{
6158 if (fr != NULL)
6159 {
6160 clear_snapshot_rec(fr->fr_next);
6161 clear_snapshot_rec(fr->fr_child);
6162 vim_free(fr);
6163 }
6164}
6165
6166/*
6167 * Restore a previously created snapshot, if there is any.
6168 * This is only done if the screen size didn't change and the window layout is
6169 * still the same.
6170 */
6171 static void
6172restore_snapshot(close_curwin)
6173 int close_curwin; /* closing current window */
6174{
6175 win_T *wp;
6176
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006177 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006179 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006181 && curtab->tp_snapshot->fr_height == topframe->fr_height
6182 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006184 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 win_comp_pos();
6186 if (wp != NULL && close_curwin)
6187 win_goto(wp);
6188 redraw_all_later(CLEAR);
6189 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006190 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191}
6192
6193/*
6194 * Check if frames "sn" and "fr" have the same layout, same following frames
6195 * and same children.
6196 */
6197 static int
6198check_snapshot_rec(sn, fr)
6199 frame_T *sn;
6200 frame_T *fr;
6201{
6202 if (sn->fr_layout != fr->fr_layout
6203 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6204 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6205 || (sn->fr_next != NULL
6206 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6207 || (sn->fr_child != NULL
6208 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6209 return FAIL;
6210 return OK;
6211}
6212
6213/*
6214 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6215 * following frames and children.
6216 * Returns a pointer to the old current window, or NULL.
6217 */
6218 static win_T *
6219restore_snapshot_rec(sn, fr)
6220 frame_T *sn;
6221 frame_T *fr;
6222{
6223 win_T *wp = NULL;
6224 win_T *wp2;
6225
6226 fr->fr_height = sn->fr_height;
6227# ifdef FEAT_VERTSPLIT
6228 fr->fr_width = sn->fr_width;
6229# endif
6230 if (fr->fr_layout == FR_LEAF)
6231 {
6232 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6233# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006234 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235# endif
6236 wp = sn->fr_win;
6237 }
6238 if (sn->fr_next != NULL)
6239 {
6240 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6241 if (wp2 != NULL)
6242 wp = wp2;
6243 }
6244 if (sn->fr_child != NULL)
6245 {
6246 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6247 if (wp2 != NULL)
6248 wp = wp2;
6249 }
6250 return wp;
6251}
6252
6253#endif
6254
6255#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6256/*
6257 * Return TRUE if there is any vertically split window.
6258 */
6259 int
6260win_hasvertsplit()
6261{
6262 frame_T *fr;
6263
6264 if (topframe->fr_layout == FR_ROW)
6265 return TRUE;
6266
6267 if (topframe->fr_layout == FR_COL)
6268 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6269 if (fr->fr_layout == FR_ROW)
6270 return TRUE;
6271
6272 return FALSE;
6273}
6274#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006275
6276#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6277/*
6278 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006279 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006280 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6281 * If no particular ID is desired, -1 must be specified for 'id'.
6282 * Return ID of added match, -1 on failure.
6283 */
6284 int
6285match_add(wp, grp, pat, prio, id)
6286 win_T *wp;
6287 char_u *grp;
6288 char_u *pat;
6289 int prio;
6290 int id;
6291{
6292 matchitem_T *cur;
6293 matchitem_T *prev;
6294 matchitem_T *m;
6295 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006296 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006297
6298 if (*grp == NUL || *pat == NUL)
6299 return -1;
6300 if (id < -1 || id == 0)
6301 {
6302 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6303 return -1;
6304 }
6305 if (id != -1)
6306 {
6307 cur = wp->w_match_head;
6308 while (cur != NULL)
6309 {
6310 if (cur->id == id)
6311 {
6312 EMSGN("E801: ID already taken: %ld", id);
6313 return -1;
6314 }
6315 cur = cur->next;
6316 }
6317 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006318 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006319 {
6320 EMSG2(_(e_nogroup), grp);
6321 return -1;
6322 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006323 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006324 {
6325 EMSG2(_(e_invarg2), pat);
6326 return -1;
6327 }
6328
6329 /* Find available match ID. */
6330 while (id == -1)
6331 {
6332 cur = wp->w_match_head;
6333 while (cur != NULL && cur->id != wp->w_next_match_id)
6334 cur = cur->next;
6335 if (cur == NULL)
6336 id = wp->w_next_match_id;
6337 wp->w_next_match_id++;
6338 }
6339
6340 /* Build new match. */
6341 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6342 m->id = id;
6343 m->priority = prio;
6344 m->pattern = vim_strsave(pat);
6345 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006346 m->match.regprog = regprog;
6347 m->match.rmm_ic = FALSE;
6348 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006349
6350 /* Insert new match. The match list is in ascending order with regard to
6351 * the match priorities. */
6352 cur = wp->w_match_head;
6353 prev = cur;
6354 while (cur != NULL && prio >= cur->priority)
6355 {
6356 prev = cur;
6357 cur = cur->next;
6358 }
6359 if (cur == prev)
6360 wp->w_match_head = m;
6361 else
6362 prev->next = m;
6363 m->next = cur;
6364
6365 redraw_later(SOME_VALID);
6366 return id;
6367}
6368
6369/*
6370 * Delete match with ID 'id' in the match list of window 'wp'.
6371 * Print error messages if 'perr' is TRUE.
6372 */
6373 int
6374match_delete(wp, id, perr)
6375 win_T *wp;
6376 int id;
6377 int perr;
6378{
6379 matchitem_T *cur = wp->w_match_head;
6380 matchitem_T *prev = cur;
6381
6382 if (id < 1)
6383 {
6384 if (perr == TRUE)
6385 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6386 id);
6387 return -1;
6388 }
6389 while (cur != NULL && cur->id != id)
6390 {
6391 prev = cur;
6392 cur = cur->next;
6393 }
6394 if (cur == NULL)
6395 {
6396 if (perr == TRUE)
6397 EMSGN("E803: ID not found: %ld", id);
6398 return -1;
6399 }
6400 if (cur == prev)
6401 wp->w_match_head = cur->next;
6402 else
6403 prev->next = cur->next;
6404 vim_free(cur->match.regprog);
6405 vim_free(cur->pattern);
6406 vim_free(cur);
6407 redraw_later(SOME_VALID);
6408 return 0;
6409}
6410
6411/*
6412 * Delete all matches in the match list of window 'wp'.
6413 */
6414 void
6415clear_matches(wp)
6416 win_T *wp;
6417{
6418 matchitem_T *m;
6419
6420 while (wp->w_match_head != NULL)
6421 {
6422 m = wp->w_match_head->next;
6423 vim_free(wp->w_match_head->match.regprog);
6424 vim_free(wp->w_match_head->pattern);
6425 vim_free(wp->w_match_head);
6426 wp->w_match_head = m;
6427 }
6428 redraw_later(SOME_VALID);
6429}
6430
6431/*
6432 * Get match from ID 'id' in window 'wp'.
6433 * Return NULL if match not found.
6434 */
6435 matchitem_T *
6436get_match(wp, id)
6437 win_T *wp;
6438 int id;
6439{
6440 matchitem_T *cur = wp->w_match_head;
6441
6442 while (cur != NULL && cur->id != id)
6443 cur = cur->next;
6444 return cur;
6445}
6446#endif