blob: 2c14e505315253c573c53e336dc377ba1e1d684a [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 Moolenaar2a0449d2006-02-20 21:27:21 +000015static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000025static int last_window __ARGS((void));
Bram 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 */
914 win_init(wp, curwin);
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.
1163 */
1164 static void
1165win_init(newp, oldp)
1166 win_T *newp;
1167 win_T *oldp;
1168{
1169 int i;
1170
1171 newp->w_buffer = oldp->w_buffer;
1172 oldp->w_buffer->b_nwindows++;
1173 newp->w_cursor = oldp->w_cursor;
1174 newp->w_valid = 0;
1175 newp->w_curswant = oldp->w_curswant;
1176 newp->w_set_curswant = oldp->w_set_curswant;
1177 newp->w_topline = oldp->w_topline;
1178#ifdef FEAT_DIFF
1179 newp->w_topfill = oldp->w_topfill;
1180#endif
1181 newp->w_leftcol = oldp->w_leftcol;
1182 newp->w_pcmark = oldp->w_pcmark;
1183 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1184 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001185 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001186 newp->w_fraction = oldp->w_fraction;
1187 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1188#ifdef FEAT_JUMPLIST
1189 copy_jumplist(oldp, newp);
1190#endif
1191#ifdef FEAT_QUICKFIX
1192 copy_loclist(oldp, newp);
1193#endif
1194 if (oldp->w_localdir != NULL)
1195 newp->w_localdir = vim_strsave(oldp->w_localdir);
1196
1197 /* Use the same argument list. */
1198 newp->w_alist = oldp->w_alist;
1199 ++newp->w_alist->al_refcount;
1200 newp->w_arg_idx = oldp->w_arg_idx;
1201
1202 /*
1203 * copy tagstack and options from existing window
1204 */
1205 for (i = 0; i < oldp->w_tagstacklen; i++)
1206 {
1207 newp->w_tagstack[i] = oldp->w_tagstack[i];
1208 if (newp->w_tagstack[i].tagname != NULL)
1209 newp->w_tagstack[i].tagname =
1210 vim_strsave(newp->w_tagstack[i].tagname);
1211 }
1212 newp->w_tagstackidx = oldp->w_tagstackidx;
1213 newp->w_tagstacklen = oldp->w_tagstacklen;
1214 win_copy_options(oldp, newp);
1215# ifdef FEAT_FOLDING
1216 copyFoldingState(oldp, newp);
1217# endif
1218}
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif /* FEAT_WINDOWS */
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#if defined(FEAT_WINDOWS) || defined(PROTO)
1223/*
1224 * Check if "win" is a pointer to an existing window.
1225 */
1226 int
1227win_valid(win)
1228 win_T *win;
1229{
1230 win_T *wp;
1231
1232 if (win == NULL)
1233 return FALSE;
1234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1235 if (wp == win)
1236 return TRUE;
1237 return FALSE;
1238}
1239
1240/*
1241 * Return the number of windows.
1242 */
1243 int
1244win_count()
1245{
1246 win_T *wp;
1247 int count = 0;
1248
1249 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1250 ++count;
1251 return count;
1252}
1253
1254/*
1255 * Make "count" windows on the screen.
1256 * Return actual number of windows on the screen.
1257 * Must be called when there is just one window, filling the whole screen
1258 * (excluding the command line).
1259 */
1260/*ARGSUSED*/
1261 int
1262make_windows(count, vertical)
1263 int count;
1264 int vertical; /* split windows vertically if TRUE */
1265{
1266 int maxcount;
1267 int todo;
1268
1269#ifdef FEAT_VERTSPLIT
1270 if (vertical)
1271 {
1272 /* Each windows needs at least 'winminwidth' lines and a separator
1273 * column. */
1274 maxcount = (curwin->w_width + curwin->w_vsep_width
1275 - (p_wiw - p_wmw)) / (p_wmw + 1);
1276 }
1277 else
1278#endif
1279 {
1280 /* Each window needs at least 'winminheight' lines and a status line. */
1281 maxcount = (curwin->w_height + curwin->w_status_height
1282 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1283 }
1284
1285 if (maxcount < 2)
1286 maxcount = 2;
1287 if (count > maxcount)
1288 count = maxcount;
1289
1290 /*
1291 * add status line now, otherwise first window will be too big
1292 */
1293 if (count > 1)
1294 last_status(TRUE);
1295
1296#ifdef FEAT_AUTOCMD
1297 /*
1298 * Don't execute autocommands while creating the windows. Must do that
1299 * when putting the buffers in the windows.
1300 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001301 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
1303
1304 /* todo is number of windows left to create */
1305 for (todo = count - 1; todo > 0; --todo)
1306#ifdef FEAT_VERTSPLIT
1307 if (vertical)
1308 {
1309 if (win_split(curwin->w_width - (curwin->w_width - todo)
1310 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1311 break;
1312 }
1313 else
1314#endif
1315 {
1316 if (win_split(curwin->w_height - (curwin->w_height - todo
1317 * STATUS_HEIGHT) / (todo + 1)
1318 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1319 break;
1320 }
1321
1322#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001323 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324#endif
1325
1326 /* return actual number of windows */
1327 return (count - todo);
1328}
1329
1330/*
1331 * Exchange current and next window
1332 */
1333 static void
1334win_exchange(Prenum)
1335 long Prenum;
1336{
1337 frame_T *frp;
1338 frame_T *frp2;
1339 win_T *wp;
1340 win_T *wp2;
1341 int temp;
1342
1343 if (lastwin == firstwin) /* just one window */
1344 {
1345 beep_flush();
1346 return;
1347 }
1348
1349#ifdef FEAT_GUI
1350 need_mouse_correct = TRUE;
1351#endif
1352
1353 /*
1354 * find window to exchange with
1355 */
1356 if (Prenum)
1357 {
1358 frp = curwin->w_frame->fr_parent->fr_child;
1359 while (frp != NULL && --Prenum > 0)
1360 frp = frp->fr_next;
1361 }
1362 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1363 frp = curwin->w_frame->fr_next;
1364 else /* Swap last window in row/col with previous */
1365 frp = curwin->w_frame->fr_prev;
1366
1367 /* We can only exchange a window with another window, not with a frame
1368 * containing windows. */
1369 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1370 return;
1371 wp = frp->fr_win;
1372
1373/*
1374 * 1. remove curwin from the list. Remember after which window it was in wp2
1375 * 2. insert curwin before wp in the list
1376 * if wp != wp2
1377 * 3. remove wp from the list
1378 * 4. insert wp after wp2
1379 * 5. exchange the status line height and vsep width.
1380 */
1381 wp2 = curwin->w_prev;
1382 frp2 = curwin->w_frame->fr_prev;
1383 if (wp->w_prev != curwin)
1384 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001385 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 frame_remove(curwin->w_frame);
1387 win_append(wp->w_prev, curwin);
1388 frame_insert(frp, curwin->w_frame);
1389 }
1390 if (wp != wp2)
1391 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001392 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 frame_remove(wp->w_frame);
1394 win_append(wp2, wp);
1395 if (frp2 == NULL)
1396 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1397 else
1398 frame_append(frp2, wp->w_frame);
1399 }
1400 temp = curwin->w_status_height;
1401 curwin->w_status_height = wp->w_status_height;
1402 wp->w_status_height = temp;
1403#ifdef FEAT_VERTSPLIT
1404 temp = curwin->w_vsep_width;
1405 curwin->w_vsep_width = wp->w_vsep_width;
1406 wp->w_vsep_width = temp;
1407
1408 /* If the windows are not in the same frame, exchange the sizes to avoid
1409 * messing up the window layout. Otherwise fix the frame sizes. */
1410 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1411 {
1412 temp = curwin->w_height;
1413 curwin->w_height = wp->w_height;
1414 wp->w_height = temp;
1415 temp = curwin->w_width;
1416 curwin->w_width = wp->w_width;
1417 wp->w_width = temp;
1418 }
1419 else
1420 {
1421 frame_fix_height(curwin);
1422 frame_fix_height(wp);
1423 frame_fix_width(curwin);
1424 frame_fix_width(wp);
1425 }
1426#endif
1427
1428 (void)win_comp_pos(); /* recompute window positions */
1429
1430 win_enter(wp, TRUE);
1431 redraw_later(CLEAR);
1432}
1433
1434/*
1435 * rotate windows: if upwards TRUE the second window becomes the first one
1436 * if upwards FALSE the first window becomes the second one
1437 */
1438 static void
1439win_rotate(upwards, count)
1440 int upwards;
1441 int count;
1442{
1443 win_T *wp1;
1444 win_T *wp2;
1445 frame_T *frp;
1446 int n;
1447
1448 if (firstwin == lastwin) /* nothing to do */
1449 {
1450 beep_flush();
1451 return;
1452 }
1453
1454#ifdef FEAT_GUI
1455 need_mouse_correct = TRUE;
1456#endif
1457
1458#ifdef FEAT_VERTSPLIT
1459 /* Check if all frames in this row/col have one window. */
1460 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1461 frp = frp->fr_next)
1462 if (frp->fr_win == NULL)
1463 {
1464 EMSG(_("E443: Cannot rotate when another window is split"));
1465 return;
1466 }
1467#endif
1468
1469 while (count--)
1470 {
1471 if (upwards) /* first window becomes last window */
1472 {
1473 /* remove first window/frame from the list */
1474 frp = curwin->w_frame->fr_parent->fr_child;
1475 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001476 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 frame_remove(frp);
1478
1479 /* find last frame and append removed window/frame after it */
1480 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1481 ;
1482 win_append(frp->fr_win, wp1);
1483 frame_append(frp, wp1->w_frame);
1484
1485 wp2 = frp->fr_win; /* previously last window */
1486 }
1487 else /* last window becomes first window */
1488 {
1489 /* find last window/frame in the list and remove it */
1490 for (frp = curwin->w_frame; frp->fr_next != NULL;
1491 frp = frp->fr_next)
1492 ;
1493 wp1 = frp->fr_win;
1494 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001495 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 frame_remove(frp);
1497
1498 /* append the removed window/frame before the first in the list */
1499 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1500 frame_insert(frp->fr_parent->fr_child, frp);
1501 }
1502
1503 /* exchange status height and vsep width of old and new last window */
1504 n = wp2->w_status_height;
1505 wp2->w_status_height = wp1->w_status_height;
1506 wp1->w_status_height = n;
1507 frame_fix_height(wp1);
1508 frame_fix_height(wp2);
1509#ifdef FEAT_VERTSPLIT
1510 n = wp2->w_vsep_width;
1511 wp2->w_vsep_width = wp1->w_vsep_width;
1512 wp1->w_vsep_width = n;
1513 frame_fix_width(wp1);
1514 frame_fix_width(wp2);
1515#endif
1516
1517 /* recompute w_winrow and w_wincol for all windows */
1518 (void)win_comp_pos();
1519 }
1520
1521 redraw_later(CLEAR);
1522}
1523
1524/*
1525 * Move the current window to the very top/bottom/left/right of the screen.
1526 */
1527 static void
1528win_totop(size, flags)
1529 int size;
1530 int flags;
1531{
1532 int dir;
1533 int height = curwin->w_height;
1534
1535 if (lastwin == firstwin)
1536 {
1537 beep_flush();
1538 return;
1539 }
1540
1541 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001542 (void)winframe_remove(curwin, &dir, NULL);
1543 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 last_status(FALSE); /* may need to remove last status line */
1545 (void)win_comp_pos(); /* recompute window positions */
1546
1547 /* Split a window on the desired side and put the window there. */
1548 (void)win_split_ins(size, flags, curwin, dir);
1549 if (!(flags & WSP_VERT))
1550 {
1551 win_setheight(height);
1552 if (p_ea)
1553 win_equal(curwin, TRUE, 'v');
1554 }
1555
1556#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1557 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1558 * scrollbars. Have to update them anyway. */
1559 if (gui.in_use)
1560 {
1561 out_flush();
1562 gui_init_which_components(NULL);
1563 gui_update_scrollbars(TRUE);
1564 }
1565 need_mouse_correct = TRUE;
1566#endif
1567
1568}
1569
1570/*
1571 * Move window "win1" to below/right of "win2" and make "win1" the current
1572 * window. Only works within the same frame!
1573 */
1574 void
1575win_move_after(win1, win2)
1576 win_T *win1, *win2;
1577{
1578 int height;
1579
1580 /* check if the arguments are reasonable */
1581 if (win1 == win2)
1582 return;
1583
1584 /* check if there is something to do */
1585 if (win2->w_next != win1)
1586 {
1587 /* may need move the status line/vertical separator of the last window
1588 * */
1589 if (win1 == lastwin)
1590 {
1591 height = win1->w_prev->w_status_height;
1592 win1->w_prev->w_status_height = win1->w_status_height;
1593 win1->w_status_height = height;
1594#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001595 if (win1->w_prev->w_vsep_width == 1)
1596 {
1597 /* Remove the vertical separator from the last-but-one window,
1598 * add it to the last window. Adjust the frame widths. */
1599 win1->w_prev->w_vsep_width = 0;
1600 win1->w_prev->w_frame->fr_width -= 1;
1601 win1->w_vsep_width = 1;
1602 win1->w_frame->fr_width += 1;
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#endif
1605 }
1606 else if (win2 == lastwin)
1607 {
1608 height = win1->w_status_height;
1609 win1->w_status_height = win2->w_status_height;
1610 win2->w_status_height = height;
1611#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001612 if (win1->w_vsep_width == 1)
1613 {
1614 /* Remove the vertical separator from win1, add it to the last
1615 * window, win2. Adjust the frame widths. */
1616 win2->w_vsep_width = 1;
1617 win2->w_frame->fr_width += 1;
1618 win1->w_vsep_width = 0;
1619 win1->w_frame->fr_width -= 1;
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#endif
1622 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001623 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 frame_remove(win1->w_frame);
1625 win_append(win2, win1);
1626 frame_append(win2->w_frame, win1->w_frame);
1627
1628 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1629 redraw_later(NOT_VALID);
1630 }
1631 win_enter(win1, FALSE);
1632}
1633
1634/*
1635 * Make all windows the same height.
1636 * 'next_curwin' will soon be the current window, make sure it has enough
1637 * rows.
1638 */
1639 void
1640win_equal(next_curwin, current, dir)
1641 win_T *next_curwin; /* pointer to current window to be or NULL */
1642 int current; /* do only frame with current window */
1643 int dir; /* 'v' for vertically, 'h' for horizontally,
1644 'b' for both, 0 for using p_ead */
1645{
1646 if (dir == 0)
1647#ifdef FEAT_VERTSPLIT
1648 dir = *p_ead;
1649#else
1650 dir = 'b';
1651#endif
1652 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001653 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001654 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656
1657/*
1658 * Set a frame to a new position and height, spreading the available room
1659 * equally over contained frames.
1660 * The window "next_curwin" (if not NULL) should at least get the size from
1661 * 'winheight' and 'winwidth' if possible.
1662 */
1663 static void
1664win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1665 win_T *next_curwin; /* pointer to current window to be or NULL */
1666 int current; /* do only frame with current window */
1667 frame_T *topfr; /* frame to set size off */
1668 int dir; /* 'v', 'h' or 'b', see win_equal() */
1669 int col; /* horizontal position for frame */
1670 int row; /* vertical position for frame */
1671 int width; /* new width of frame */
1672 int height; /* new height of frame */
1673{
1674 int n, m;
1675 int extra_sep = 0;
1676 int wincount, totwincount = 0;
1677 frame_T *fr;
1678 int next_curwin_size = 0;
1679 int room = 0;
1680 int new_size;
1681 int has_next_curwin = 0;
1682 int hnc;
1683
1684 if (topfr->fr_layout == FR_LEAF)
1685 {
1686 /* Set the width/height of this frame.
1687 * Redraw when size or position changes */
1688 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1689#ifdef FEAT_VERTSPLIT
1690 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1691#endif
1692 )
1693 {
1694 topfr->fr_win->w_winrow = row;
1695 frame_new_height(topfr, height, FALSE, FALSE);
1696#ifdef FEAT_VERTSPLIT
1697 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001698 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
1700 redraw_all_later(CLEAR);
1701 }
1702 }
1703#ifdef FEAT_VERTSPLIT
1704 else if (topfr->fr_layout == FR_ROW)
1705 {
1706 topfr->fr_width = width;
1707 topfr->fr_height = height;
1708
1709 if (dir != 'v') /* equalize frame widths */
1710 {
1711 /* Compute the maximum number of windows horizontally in this
1712 * frame. */
1713 n = frame_minwidth(topfr, NOWIN);
1714 /* add one for the rightmost window, it doesn't have a separator */
1715 if (col + width == Columns)
1716 extra_sep = 1;
1717 else
1718 extra_sep = 0;
1719 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001722 /*
1723 * Compute width for "next_curwin" window and room available for
1724 * other windows.
1725 * "m" is the minimal width when counting p_wiw for "next_curwin".
1726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 m = frame_minwidth(topfr, next_curwin);
1728 room = width - m;
1729 if (room < 0)
1730 {
1731 next_curwin_size = p_wiw + room;
1732 room = 0;
1733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 else
1735 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001736 next_curwin_size = -1;
1737 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1738 {
1739 /* If 'winfixwidth' set keep the window width if
1740 * possible.
1741 * Watch out for this window being the next_curwin. */
1742 if (frame_fixed_width(fr))
1743 {
1744 n = frame_minwidth(fr, NOWIN);
1745 new_size = fr->fr_width;
1746 if (frame_has_win(fr, next_curwin))
1747 {
1748 room += p_wiw - p_wmw;
1749 next_curwin_size = 0;
1750 if (new_size < p_wiw)
1751 new_size = p_wiw;
1752 }
1753 else
1754 /* These windows don't use up room. */
1755 totwincount -= (n + (fr->fr_next == NULL
1756 ? extra_sep : 0)) / (p_wmw + 1);
1757 room -= new_size - n;
1758 if (room < 0)
1759 {
1760 new_size += room;
1761 room = 0;
1762 }
1763 fr->fr_newwidth = new_size;
1764 }
1765 }
1766 if (next_curwin_size == -1)
1767 {
1768 if (!has_next_curwin)
1769 next_curwin_size = 0;
1770 else if (totwincount > 1
1771 && (room + (totwincount - 2))
1772 / (totwincount - 1) > p_wiw)
1773 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001774 /* Can make all windows wider than 'winwidth', spread
1775 * the room equally. */
1776 next_curwin_size = (room + p_wiw
1777 + (totwincount - 1) * p_wmw
1778 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001779 room -= next_curwin_size - p_wiw;
1780 }
1781 else
1782 next_curwin_size = p_wiw;
1783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001785
1786 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 --totwincount; /* don't count curwin */
1788 }
1789
1790 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1791 {
1792 n = m = 0;
1793 wincount = 1;
1794 if (fr->fr_next == NULL)
1795 /* last frame gets all that remains (avoid roundoff error) */
1796 new_size = width;
1797 else if (dir == 'v')
1798 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001799 else if (frame_fixed_width(fr))
1800 {
1801 new_size = fr->fr_newwidth;
1802 wincount = 0; /* doesn't count as a sizeable window */
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 else
1805 {
1806 /* Compute the maximum number of windows horiz. in "fr". */
1807 n = frame_minwidth(fr, NOWIN);
1808 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1809 / (p_wmw + 1);
1810 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001811 if (has_next_curwin)
1812 hnc = frame_has_win(fr, next_curwin);
1813 else
1814 hnc = FALSE;
1815 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001817 if (totwincount == 0)
1818 new_size = room;
1819 else
1820 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 next_curwin_size -= p_wiw - (m - n);
1825 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001826 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 else
1829 room -= new_size;
1830 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001833 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 * window, unless equalizing all frames. */
1835 if (!current || dir != 'v' || topfr->fr_parent != NULL
1836 || (new_size != fr->fr_width)
1837 || frame_has_win(fr, next_curwin))
1838 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 new_size, height);
1840 col += new_size;
1841 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 totwincount -= wincount;
1843 }
1844 }
1845#endif
1846 else /* topfr->fr_layout == FR_COL */
1847 {
1848#ifdef FEAT_VERTSPLIT
1849 topfr->fr_width = width;
1850#endif
1851 topfr->fr_height = height;
1852
1853 if (dir != 'h') /* equalize frame heights */
1854 {
1855 /* Compute maximum number of windows vertically in this frame. */
1856 n = frame_minheight(topfr, NOWIN);
1857 /* add one for the bottom window if it doesn't have a statusline */
1858 if (row + height == cmdline_row && p_ls == 0)
1859 extra_sep = 1;
1860 else
1861 extra_sep = 0;
1862 totwincount = (n + extra_sep) / (p_wmh + 1);
1863 has_next_curwin = frame_has_win(topfr, next_curwin);
1864
1865 /*
1866 * Compute height for "next_curwin" window and room available for
1867 * other windows.
1868 * "m" is the minimal height when counting p_wh for "next_curwin".
1869 */
1870 m = frame_minheight(topfr, next_curwin);
1871 room = height - m;
1872 if (room < 0)
1873 {
1874 /* The room is less then 'winheight', use all space for the
1875 * current window. */
1876 next_curwin_size = p_wh + room;
1877 room = 0;
1878 }
1879 else
1880 {
1881 next_curwin_size = -1;
1882 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1883 {
1884 /* If 'winfixheight' set keep the window height if
1885 * possible.
1886 * Watch out for this window being the next_curwin. */
1887 if (frame_fixed_height(fr))
1888 {
1889 n = frame_minheight(fr, NOWIN);
1890 new_size = fr->fr_height;
1891 if (frame_has_win(fr, next_curwin))
1892 {
1893 room += p_wh - p_wmh;
1894 next_curwin_size = 0;
1895 if (new_size < p_wh)
1896 new_size = p_wh;
1897 }
1898 else
1899 /* These windows don't use up room. */
1900 totwincount -= (n + (fr->fr_next == NULL
1901 ? extra_sep : 0)) / (p_wmh + 1);
1902 room -= new_size - n;
1903 if (room < 0)
1904 {
1905 new_size += room;
1906 room = 0;
1907 }
1908 fr->fr_newheight = new_size;
1909 }
1910 }
1911 if (next_curwin_size == -1)
1912 {
1913 if (!has_next_curwin)
1914 next_curwin_size = 0;
1915 else if (totwincount > 1
1916 && (room + (totwincount - 2))
1917 / (totwincount - 1) > p_wh)
1918 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001919 /* can make all windows higher than 'winheight',
1920 * spread the room equally. */
1921 next_curwin_size = (room + p_wh
1922 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 + (totwincount - 1)) / totwincount;
1924 room -= next_curwin_size - p_wh;
1925 }
1926 else
1927 next_curwin_size = p_wh;
1928 }
1929 }
1930
1931 if (has_next_curwin)
1932 --totwincount; /* don't count curwin */
1933 }
1934
1935 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1936 {
1937 n = m = 0;
1938 wincount = 1;
1939 if (fr->fr_next == NULL)
1940 /* last frame gets all that remains (avoid roundoff error) */
1941 new_size = height;
1942 else if (dir == 'h')
1943 new_size = fr->fr_height;
1944 else if (frame_fixed_height(fr))
1945 {
1946 new_size = fr->fr_newheight;
1947 wincount = 0; /* doesn't count as a sizeable window */
1948 }
1949 else
1950 {
1951 /* Compute the maximum number of windows vert. in "fr". */
1952 n = frame_minheight(fr, NOWIN);
1953 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1954 / (p_wmh + 1);
1955 m = frame_minheight(fr, next_curwin);
1956 if (has_next_curwin)
1957 hnc = frame_has_win(fr, next_curwin);
1958 else
1959 hnc = FALSE;
1960 if (hnc) /* don't count next_curwin */
1961 --wincount;
1962 if (totwincount == 0)
1963 new_size = room;
1964 else
1965 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1966 / totwincount;
1967 if (hnc) /* add next_curwin size */
1968 {
1969 next_curwin_size -= p_wh - (m - n);
1970 new_size += next_curwin_size;
1971 room -= new_size - next_curwin_size;
1972 }
1973 else
1974 room -= new_size;
1975 new_size += n;
1976 }
1977 /* Skip frame that is full width when splitting or closing a
1978 * window, unless equalizing all frames. */
1979 if (!current || dir != 'h' || topfr->fr_parent != NULL
1980 || (new_size != fr->fr_height)
1981 || frame_has_win(fr, next_curwin))
1982 win_equal_rec(next_curwin, current, fr, dir, col, row,
1983 width, new_size);
1984 row += new_size;
1985 height -= new_size;
1986 totwincount -= wincount;
1987 }
1988 }
1989}
1990
1991/*
1992 * close all windows for buffer 'buf'
1993 */
1994 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001995close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001997 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001999 win_T *wp;
2000 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002001 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002004
2005 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002007 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002009 win_close(wp, FALSE);
2010
2011 /* Start all over, autocommands may change the window layout. */
2012 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002015 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002017
2018 /* Also check windows in other tab pages. */
2019 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2020 {
2021 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002022 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2024 if (wp->w_buffer == buf)
2025 {
2026 win_close_othertab(wp, FALSE, tp);
2027
2028 /* Start all over, the tab page may be closed and
2029 * autocommands may change the window layout. */
2030 nexttp = first_tabpage;
2031 break;
2032 }
2033 }
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002036
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002037 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002038 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039}
2040
2041/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002042 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002043 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002044 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002045 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002046last_window()
2047{
2048 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2049}
2050
2051/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 * If "free_buf" is TRUE related buffer may be unloaded.
2054 *
2055 * called by :quit, :close, :xit, :wq and findtag()
2056 */
2057 void
2058win_close(win, free_buf)
2059 win_T *win;
2060 int free_buf;
2061{
2062 win_T *wp;
2063#ifdef FEAT_AUTOCMD
2064 int other_buffer = FALSE;
2065#endif
2066 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 int dir;
2068 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002069 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002071 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 EMSG(_("E444: Cannot close last window"));
2074 return;
2075 }
2076
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002077 /*
2078 * When closing the last window in a tab page first go to another tab
2079 * page and then close the window and the tab page. This avoids that
2080 * curwin and curtab are not invalid while we are freeing memory, they may
2081 * be used in GUI events.
2082 */
2083 if (firstwin == lastwin)
2084 {
2085 goto_tabpage_tp(alt_tabpage());
2086 redraw_tabline = TRUE;
2087
2088 /* Safety check: Autocommands may have closed the window when jumping
2089 * to the other tab page. */
2090 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2091 {
2092 int h = tabline_height();
2093
2094 win_close_othertab(win, free_buf, prev_curtab);
2095 if (h != tabline_height())
2096 shell_new_rows();
2097 }
2098 return;
2099 }
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 /* When closing the help window, try restoring a snapshot after closing
2102 * the window. Otherwise clear the snapshot, it's now invalid. */
2103 if (win->w_buffer->b_help)
2104 help_window = TRUE;
2105 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002106 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108#ifdef FEAT_AUTOCMD
2109 if (win == curwin)
2110 {
2111 /*
2112 * Guess which window is going to be the new current window.
2113 * This may change because of the autocommands (sigh).
2114 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002115 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
2117 /*
2118 * Be careful: If autocommands delete the window, return now.
2119 */
2120 if (wp->w_buffer != curbuf)
2121 {
2122 other_buffer = TRUE;
2123 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002124 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 return;
2126 }
2127 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002128 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return;
2130# ifdef FEAT_EVAL
2131 /* autocmds may abort script processing */
2132 if (aborting())
2133 return;
2134# endif
2135 }
2136#endif
2137
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002138#ifdef FEAT_GUI
2139 /* Avoid trouble with scrollbars that are going to be deleted in
2140 * win_free(). */
2141 if (gui.in_use)
2142 out_flush();
2143#endif
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 /*
2146 * Close the link to the buffer.
2147 */
2148 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002151 * other window or moved to another tab page. */
2152 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 return;
2154
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002155 /* Free the memory used for the window. */
2156 wp = win_free_mem(win, &dir, NULL);
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /* Make sure curwin isn't invalid. It can cause severe trouble when
2159 * printing an error message. For win_equal() curbuf needs to be valid
2160 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002161 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 curwin = wp;
2164#ifdef FEAT_QUICKFIX
2165 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2166 {
2167 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002168 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 * finding another window to go to.
2170 */
2171 for (;;)
2172 {
2173 if (wp->w_next == NULL)
2174 wp = firstwin;
2175 else
2176 wp = wp->w_next;
2177 if (wp == curwin)
2178 break;
2179 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2180 {
2181 curwin = wp;
2182 break;
2183 }
2184 }
2185 }
2186#endif
2187 curbuf = curwin->w_buffer;
2188 close_curwin = TRUE;
2189 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002190 if (p_ea
2191#ifdef FEAT_VERTSPLIT
2192 && (*p_ead == 'b' || *p_ead == dir)
2193#endif
2194 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 win_equal(curwin, TRUE,
2196#ifdef FEAT_VERTSPLIT
2197 dir
2198#else
2199 0
2200#endif
2201 );
2202 else
2203 win_comp_pos();
2204 if (close_curwin)
2205 {
2206 win_enter_ext(wp, FALSE, TRUE);
2207#ifdef FEAT_AUTOCMD
2208 if (other_buffer)
2209 /* careful: after this wp and win may be invalid! */
2210 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2211#endif
2212 }
2213
2214 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002215 * If last window has a status line now and we don't want one,
2216 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 */
2218 last_status(FALSE);
2219
2220 /* After closing the help window, try restoring the window layout from
2221 * before it was opened. */
2222 if (help_window)
2223 restore_snapshot(close_curwin);
2224
2225#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2226 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2227 if (gui.in_use && !win_hasvertsplit())
2228 gui_init_which_components(NULL);
2229#endif
2230
2231 redraw_all_later(NOT_VALID);
2232}
2233
2234/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002235 * Close window "win" in tab page "tp", which is not the current tab page.
2236 * This may be the last window ih that tab page and result in closing the tab,
2237 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002238 * Caller must check if buffer is hidden and whether the tabline needs to be
2239 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002240 */
2241 void
2242win_close_othertab(win, free_buf, tp)
2243 win_T *win;
2244 int free_buf;
2245 tabpage_T *tp;
2246{
2247 win_T *wp;
2248 int dir;
2249 tabpage_T *ptp = NULL;
2250
2251 /* Close the link to the buffer. */
2252 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2253
2254 /* Careful: Autocommands may have closed the tab page or made it the
2255 * current tab page. */
2256 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2257 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002258 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002259 return;
2260
2261 /* Autocommands may have closed the window already. */
2262 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2263 ;
2264 if (wp == NULL)
2265 return;
2266
2267 /* Free the memory used for the window. */
2268 wp = win_free_mem(win, &dir, tp);
2269
2270 /* When closing the last window in a tab page remove the tab page. */
2271 if (wp == NULL)
2272 {
2273 if (tp == first_tabpage)
2274 first_tabpage = tp->tp_next;
2275 else
2276 {
2277 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2278 ptp = ptp->tp_next)
2279 ;
2280 if (ptp == NULL)
2281 {
2282 EMSG2(_(e_intern2), "win_close_othertab()");
2283 return;
2284 }
2285 ptp->tp_next = tp->tp_next;
2286 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002287 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002288 }
2289}
2290
2291/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002292 * Free the memory used for a window.
2293 * Returns a pointer to the window that got the freed up space.
2294 */
2295 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002296win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002297 win_T *win;
2298 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002299 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002300{
2301 frame_T *frp;
2302 win_T *wp;
2303
Bram Moolenaarea408852005-06-25 22:49:46 +00002304#ifdef FEAT_FOLDING
2305 clearFolding(win);
2306#endif
2307
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002308 /* reduce the reference count to the argument list. */
2309 alist_unlink(win->w_alist);
2310
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002311 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002312 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002313 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002314 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002315 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002316
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002317 /* When deleting the current window of another tab page select a new
2318 * current window. */
2319 if (tp != NULL && win == tp->tp_curwin)
2320 tp->tp_curwin = wp;
2321
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002322 return wp;
2323}
2324
2325#if defined(EXITFREE) || defined(PROTO)
2326 void
2327win_free_all()
2328{
2329 int dummy;
2330
Bram Moolenaarf740b292006-02-16 22:11:02 +00002331# ifdef FEAT_WINDOWS
2332 while (first_tabpage->tp_next != NULL)
2333 tabpage_close(TRUE);
2334# endif
2335
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002336 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002337 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002338}
2339#endif
2340
2341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 * Remove a window and its frame from the tree of frames.
2343 * Returns a pointer to the window that got the freed up space.
2344 */
2345/*ARGSUSED*/
2346 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002347winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 win_T *win;
2349 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002350 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352 frame_T *frp, *frp2, *frp3;
2353 frame_T *frp_close = win->w_frame;
2354 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002357 * If there is only one window there is nothing to remove.
2358 */
2359 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2360 return NULL;
2361
2362 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * Remove the window from its frame.
2364 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002365 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 wp = frame2win(frp2);
2367
2368 /* Remove this frame from the list of frames. */
2369 frame_remove(frp_close);
2370
2371#ifdef FEAT_VERTSPLIT
2372 if (frp_close->fr_parent->fr_layout == FR_COL)
2373 {
2374#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002375 /* When 'winfixheight' is set, try to find another frame in the column
2376 * (as close to the closed frame as possible) to distribute the height
2377 * to. */
2378 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2379 {
2380 frp = frp_close->fr_prev;
2381 frp3 = frp_close->fr_next;
2382 while (frp != NULL || frp3 != NULL)
2383 {
2384 if (frp != NULL)
2385 {
2386 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2387 {
2388 frp2 = frp;
2389 wp = frp->fr_win;
2390 break;
2391 }
2392 frp = frp->fr_prev;
2393 }
2394 if (frp3 != NULL)
2395 {
2396 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2397 {
2398 frp2 = frp3;
2399 wp = frp3->fr_win;
2400 break;
2401 }
2402 frp3 = frp3->fr_next;
2403 }
2404 }
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2407 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#ifdef FEAT_VERTSPLIT
2409 *dirp = 'v';
2410 }
2411 else
2412 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002413 /* When 'winfixwidth' is set, try to find another frame in the column
2414 * (as close to the closed frame as possible) to distribute the width
2415 * to. */
2416 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2417 {
2418 frp = frp_close->fr_prev;
2419 frp3 = frp_close->fr_next;
2420 while (frp != NULL || frp3 != NULL)
2421 {
2422 if (frp != NULL)
2423 {
2424 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2425 {
2426 frp2 = frp;
2427 wp = frp->fr_win;
2428 break;
2429 }
2430 frp = frp->fr_prev;
2431 }
2432 if (frp3 != NULL)
2433 {
2434 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2435 {
2436 frp2 = frp3;
2437 wp = frp3->fr_win;
2438 break;
2439 }
2440 frp3 = frp3->fr_next;
2441 }
2442 }
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002445 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 *dirp = 'h';
2447 }
2448#endif
2449
2450 /* If rows/columns go to a window below/right its positions need to be
2451 * updated. Can only be done after the sizes have been updated. */
2452 if (frp2 == frp_close->fr_next)
2453 {
2454 int row = win->w_winrow;
2455 int col = W_WINCOL(win);
2456
2457 frame_comp_pos(frp2, &row, &col);
2458 }
2459
2460 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2461 {
2462 /* There is no other frame in this list, move its info to the parent
2463 * and remove it. */
2464 frp2->fr_parent->fr_layout = frp2->fr_layout;
2465 frp2->fr_parent->fr_child = frp2->fr_child;
2466 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2467 frp->fr_parent = frp2->fr_parent;
2468 frp2->fr_parent->fr_win = frp2->fr_win;
2469 if (frp2->fr_win != NULL)
2470 frp2->fr_win->w_frame = frp2->fr_parent;
2471 frp = frp2->fr_parent;
2472 vim_free(frp2);
2473
2474 frp2 = frp->fr_parent;
2475 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2476 {
2477 /* The frame above the parent has the same layout, have to merge
2478 * the frames into this list. */
2479 if (frp2->fr_child == frp)
2480 frp2->fr_child = frp->fr_child;
2481 frp->fr_child->fr_prev = frp->fr_prev;
2482 if (frp->fr_prev != NULL)
2483 frp->fr_prev->fr_next = frp->fr_child;
2484 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2485 {
2486 frp3->fr_parent = frp2;
2487 if (frp3->fr_next == NULL)
2488 {
2489 frp3->fr_next = frp->fr_next;
2490 if (frp->fr_next != NULL)
2491 frp->fr_next->fr_prev = frp3;
2492 break;
2493 }
2494 }
2495 vim_free(frp);
2496 }
2497 }
2498
2499 return wp;
2500}
2501
2502/*
2503 * Find out which frame is going to get the freed up space when "win" is
2504 * closed.
2505 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2506 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2507 * This makes opening a window and closing it immediately keep the same window
2508 * layout.
2509 */
2510 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002511win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 frame_T *frp;
2516 int b;
2517
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002519 /* Last window in this tab page, will go to next tab page. */
2520 return alt_tabpage()->tp_curwin->w_frame;
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 frp = win->w_frame;
2523#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002524 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 b = p_spr;
2526 else
2527#endif
2528 b = p_sb;
2529 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2530 return frp->fr_next;
2531 return frp->fr_prev;
2532}
2533
2534/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002535 * Return the tabpage that will be used if the current one is closed.
2536 */
2537 static tabpage_T *
2538alt_tabpage()
2539{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002540 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002541
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002542 /* Use the next tab page if possible. */
2543 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002544 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002545
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002546 /* Find the last but one tab page. */
2547 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2548 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002549 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002550}
2551
2552/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 * Find the left-upper window in frame "frp".
2554 */
2555 static win_T *
2556frame2win(frp)
2557 frame_T *frp;
2558{
2559 while (frp->fr_win == NULL)
2560 frp = frp->fr_child;
2561 return frp->fr_win;
2562}
2563
2564/*
2565 * Return TRUE if frame "frp" contains window "wp".
2566 */
2567 static int
2568frame_has_win(frp, wp)
2569 frame_T *frp;
2570 win_T *wp;
2571{
2572 frame_T *p;
2573
2574 if (frp->fr_layout == FR_LEAF)
2575 return frp->fr_win == wp;
2576
2577 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2578 if (frame_has_win(p, wp))
2579 return TRUE;
2580 return FALSE;
2581}
2582
2583/*
2584 * Set a new height for a frame. Recursively sets the height for contained
2585 * frames and windows. Caller must take care of positions.
2586 */
2587 static void
2588frame_new_height(topfrp, height, topfirst, wfh)
2589 frame_T *topfrp;
2590 int height;
2591 int topfirst; /* resize topmost contained frame first */
2592 int wfh; /* obey 'winfixheight' when there is a choice;
2593 may cause the height not to be set */
2594{
2595 frame_T *frp;
2596 int extra_lines;
2597 int h;
2598
2599 if (topfrp->fr_win != NULL)
2600 {
2601 /* Simple case: just one window. */
2602 win_new_height(topfrp->fr_win,
2603 height - topfrp->fr_win->w_status_height);
2604 }
2605#ifdef FEAT_VERTSPLIT
2606 else if (topfrp->fr_layout == FR_ROW)
2607 {
2608 do
2609 {
2610 /* All frames in this row get the same new height. */
2611 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2612 {
2613 frame_new_height(frp, height, topfirst, wfh);
2614 if (frp->fr_height > height)
2615 {
2616 /* Could not fit the windows, make the whole row higher. */
2617 height = frp->fr_height;
2618 break;
2619 }
2620 }
2621 }
2622 while (frp != NULL);
2623 }
2624#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002625 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
2627 /* Complicated case: Resize a column of frames. Resize the bottom
2628 * frame first, frames above that when needed. */
2629
2630 frp = topfrp->fr_child;
2631 if (wfh)
2632 /* Advance past frames with one window with 'wfh' set. */
2633 while (frame_fixed_height(frp))
2634 {
2635 frp = frp->fr_next;
2636 if (frp == NULL)
2637 return; /* no frame without 'wfh', give up */
2638 }
2639 if (!topfirst)
2640 {
2641 /* Find the bottom frame of this column */
2642 while (frp->fr_next != NULL)
2643 frp = frp->fr_next;
2644 if (wfh)
2645 /* Advance back for frames with one window with 'wfh' set. */
2646 while (frame_fixed_height(frp))
2647 frp = frp->fr_prev;
2648 }
2649
2650 extra_lines = height - topfrp->fr_height;
2651 if (extra_lines < 0)
2652 {
2653 /* reduce height of contained frames, bottom or top frame first */
2654 while (frp != NULL)
2655 {
2656 h = frame_minheight(frp, NULL);
2657 if (frp->fr_height + extra_lines < h)
2658 {
2659 extra_lines += frp->fr_height - h;
2660 frame_new_height(frp, h, topfirst, wfh);
2661 }
2662 else
2663 {
2664 frame_new_height(frp, frp->fr_height + extra_lines,
2665 topfirst, wfh);
2666 break;
2667 }
2668 if (topfirst)
2669 {
2670 do
2671 frp = frp->fr_next;
2672 while (wfh && frp != NULL && frame_fixed_height(frp));
2673 }
2674 else
2675 {
2676 do
2677 frp = frp->fr_prev;
2678 while (wfh && frp != NULL && frame_fixed_height(frp));
2679 }
2680 /* Increase "height" if we could not reduce enough frames. */
2681 if (frp == NULL)
2682 height -= extra_lines;
2683 }
2684 }
2685 else if (extra_lines > 0)
2686 {
2687 /* increase height of bottom or top frame */
2688 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2689 }
2690 }
2691 topfrp->fr_height = height;
2692}
2693
2694/*
2695 * Return TRUE if height of frame "frp" should not be changed because of
2696 * the 'winfixheight' option.
2697 */
2698 static int
2699frame_fixed_height(frp)
2700 frame_T *frp;
2701{
2702 /* frame with one window: fixed height if 'winfixheight' set. */
2703 if (frp->fr_win != NULL)
2704 return frp->fr_win->w_p_wfh;
2705
2706 if (frp->fr_layout == FR_ROW)
2707 {
2708 /* The frame is fixed height if one of the frames in the row is fixed
2709 * height. */
2710 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2711 if (frame_fixed_height(frp))
2712 return TRUE;
2713 return FALSE;
2714 }
2715
2716 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2717 * frames in the row are fixed height. */
2718 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2719 if (!frame_fixed_height(frp))
2720 return FALSE;
2721 return TRUE;
2722}
2723
2724#ifdef FEAT_VERTSPLIT
2725/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002726 * Return TRUE if width of frame "frp" should not be changed because of
2727 * the 'winfixwidth' option.
2728 */
2729 static int
2730frame_fixed_width(frp)
2731 frame_T *frp;
2732{
2733 /* frame with one window: fixed width if 'winfixwidth' set. */
2734 if (frp->fr_win != NULL)
2735 return frp->fr_win->w_p_wfw;
2736
2737 if (frp->fr_layout == FR_COL)
2738 {
2739 /* The frame is fixed width if one of the frames in the row is fixed
2740 * width. */
2741 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2742 if (frame_fixed_width(frp))
2743 return TRUE;
2744 return FALSE;
2745 }
2746
2747 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2748 * frames in the row are fixed width. */
2749 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2750 if (!frame_fixed_width(frp))
2751 return FALSE;
2752 return TRUE;
2753}
2754
2755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 * Add a status line to windows at the bottom of "frp".
2757 * Note: Does not check if there is room!
2758 */
2759 static void
2760frame_add_statusline(frp)
2761 frame_T *frp;
2762{
2763 win_T *wp;
2764
2765 if (frp->fr_layout == FR_LEAF)
2766 {
2767 wp = frp->fr_win;
2768 if (wp->w_status_height == 0)
2769 {
2770 if (wp->w_height > 0) /* don't make it negative */
2771 --wp->w_height;
2772 wp->w_status_height = STATUS_HEIGHT;
2773 }
2774 }
2775 else if (frp->fr_layout == FR_ROW)
2776 {
2777 /* Handle all the frames in the row. */
2778 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2779 frame_add_statusline(frp);
2780 }
2781 else /* frp->fr_layout == FR_COL */
2782 {
2783 /* Only need to handle the last frame in the column. */
2784 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2785 ;
2786 frame_add_statusline(frp);
2787 }
2788}
2789
2790/*
2791 * Set width of a frame. Handles recursively going through contained frames.
2792 * May remove separator line for windows at the right side (for win_close()).
2793 */
2794 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002795frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 frame_T *topfrp;
2797 int width;
2798 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002799 int wfw; /* obey 'winfixwidth' when there is a choice;
2800 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 frame_T *frp;
2803 int extra_cols;
2804 int w;
2805 win_T *wp;
2806
2807 if (topfrp->fr_layout == FR_LEAF)
2808 {
2809 /* Simple case: just one window. */
2810 wp = topfrp->fr_win;
2811 /* Find out if there are any windows right of this one. */
2812 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2813 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2814 break;
2815 if (frp->fr_parent == NULL)
2816 wp->w_vsep_width = 0;
2817 win_new_width(wp, width - wp->w_vsep_width);
2818 }
2819 else if (topfrp->fr_layout == FR_COL)
2820 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002821 do
2822 {
2823 /* All frames in this column get the same new width. */
2824 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2825 {
2826 frame_new_width(frp, width, leftfirst, wfw);
2827 if (frp->fr_width > width)
2828 {
2829 /* Could not fit the windows, make whole column wider. */
2830 width = frp->fr_width;
2831 break;
2832 }
2833 }
2834 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 }
2836 else /* fr_layout == FR_ROW */
2837 {
2838 /* Complicated case: Resize a row of frames. Resize the rightmost
2839 * frame first, frames left of it when needed. */
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002842 if (wfw)
2843 /* Advance past frames with one window with 'wfw' set. */
2844 while (frame_fixed_width(frp))
2845 {
2846 frp = frp->fr_next;
2847 if (frp == NULL)
2848 return; /* no frame without 'wfw', give up */
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002851 {
2852 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 while (frp->fr_next != NULL)
2854 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002855 if (wfw)
2856 /* Advance back for frames with one window with 'wfw' set. */
2857 while (frame_fixed_width(frp))
2858 frp = frp->fr_prev;
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 extra_cols = width - topfrp->fr_width;
2862 if (extra_cols < 0)
2863 {
2864 /* reduce frame width, rightmost frame first */
2865 while (frp != NULL)
2866 {
2867 w = frame_minwidth(frp, NULL);
2868 if (frp->fr_width + extra_cols < w)
2869 {
2870 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002871 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873 else
2874 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002875 frame_new_width(frp, frp->fr_width + extra_cols,
2876 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 break;
2878 }
2879 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002880 {
2881 do
2882 frp = frp->fr_next;
2883 while (wfw && frp != NULL && frame_fixed_width(frp));
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002886 {
2887 do
2888 frp = frp->fr_prev;
2889 while (wfw && frp != NULL && frame_fixed_width(frp));
2890 }
2891 /* Increase "width" if we could not reduce enough frames. */
2892 if (frp == NULL)
2893 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895 }
2896 else if (extra_cols > 0)
2897 {
2898 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002899 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901 }
2902 topfrp->fr_width = width;
2903}
2904
2905/*
2906 * Add the vertical separator to windows at the right side of "frp".
2907 * Note: Does not check if there is room!
2908 */
2909 static void
2910frame_add_vsep(frp)
2911 frame_T *frp;
2912{
2913 win_T *wp;
2914
2915 if (frp->fr_layout == FR_LEAF)
2916 {
2917 wp = frp->fr_win;
2918 if (wp->w_vsep_width == 0)
2919 {
2920 if (wp->w_width > 0) /* don't make it negative */
2921 --wp->w_width;
2922 wp->w_vsep_width = 1;
2923 }
2924 }
2925 else if (frp->fr_layout == FR_COL)
2926 {
2927 /* Handle all the frames in the column. */
2928 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2929 frame_add_vsep(frp);
2930 }
2931 else /* frp->fr_layout == FR_ROW */
2932 {
2933 /* Only need to handle the last frame in the row. */
2934 frp = frp->fr_child;
2935 while (frp->fr_next != NULL)
2936 frp = frp->fr_next;
2937 frame_add_vsep(frp);
2938 }
2939}
2940
2941/*
2942 * Set frame width from the window it contains.
2943 */
2944 static void
2945frame_fix_width(wp)
2946 win_T *wp;
2947{
2948 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2949}
2950#endif
2951
2952/*
2953 * Set frame height from the window it contains.
2954 */
2955 static void
2956frame_fix_height(wp)
2957 win_T *wp;
2958{
2959 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2960}
2961
2962/*
2963 * Compute the minimal height for frame "topfrp".
2964 * Uses the 'winminheight' option.
2965 * When "next_curwin" isn't NULL, use p_wh for this window.
2966 * When "next_curwin" is NOWIN, don't use at least one line for the current
2967 * window.
2968 */
2969 static int
2970frame_minheight(topfrp, next_curwin)
2971 frame_T *topfrp;
2972 win_T *next_curwin;
2973{
2974 frame_T *frp;
2975 int m;
2976#ifdef FEAT_VERTSPLIT
2977 int n;
2978#endif
2979
2980 if (topfrp->fr_win != NULL)
2981 {
2982 if (topfrp->fr_win == next_curwin)
2983 m = p_wh + topfrp->fr_win->w_status_height;
2984 else
2985 {
2986 /* window: minimal height of the window plus status line */
2987 m = p_wmh + topfrp->fr_win->w_status_height;
2988 /* Current window is minimal one line high */
2989 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2990 ++m;
2991 }
2992 }
2993#ifdef FEAT_VERTSPLIT
2994 else if (topfrp->fr_layout == FR_ROW)
2995 {
2996 /* get the minimal height from each frame in this row */
2997 m = 0;
2998 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2999 {
3000 n = frame_minheight(frp, next_curwin);
3001 if (n > m)
3002 m = n;
3003 }
3004 }
3005#endif
3006 else
3007 {
3008 /* Add up the minimal heights for all frames in this column. */
3009 m = 0;
3010 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3011 m += frame_minheight(frp, next_curwin);
3012 }
3013
3014 return m;
3015}
3016
3017#ifdef FEAT_VERTSPLIT
3018/*
3019 * Compute the minimal width for frame "topfrp".
3020 * When "next_curwin" isn't NULL, use p_wiw for this window.
3021 * When "next_curwin" is NOWIN, don't use at least one column for the current
3022 * window.
3023 */
3024 static int
3025frame_minwidth(topfrp, next_curwin)
3026 frame_T *topfrp;
3027 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3028{
3029 frame_T *frp;
3030 int m, n;
3031
3032 if (topfrp->fr_win != NULL)
3033 {
3034 if (topfrp->fr_win == next_curwin)
3035 m = p_wiw + topfrp->fr_win->w_vsep_width;
3036 else
3037 {
3038 /* window: minimal width of the window plus separator column */
3039 m = p_wmw + topfrp->fr_win->w_vsep_width;
3040 /* Current window is minimal one column wide */
3041 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3042 ++m;
3043 }
3044 }
3045 else if (topfrp->fr_layout == FR_COL)
3046 {
3047 /* get the minimal width from each frame in this column */
3048 m = 0;
3049 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3050 {
3051 n = frame_minwidth(frp, next_curwin);
3052 if (n > m)
3053 m = n;
3054 }
3055 }
3056 else
3057 {
3058 /* Add up the minimal widths for all frames in this row. */
3059 m = 0;
3060 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3061 m += frame_minwidth(frp, next_curwin);
3062 }
3063
3064 return m;
3065}
3066#endif
3067
3068
3069/*
3070 * Try to close all windows except current one.
3071 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3072 * used and the buffer was modified.
3073 *
3074 * Used by ":bdel" and ":only".
3075 */
3076 void
3077close_others(message, forceit)
3078 int message;
3079 int forceit; /* always hide all other windows */
3080{
3081 win_T *wp;
3082 win_T *nextwp;
3083 int r;
3084
3085 if (lastwin == firstwin)
3086 {
3087 if (message
3088#ifdef FEAT_AUTOCMD
3089 && !autocmd_busy
3090#endif
3091 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003092 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return;
3094 }
3095
3096 /* Be very careful here: autocommands may change the window layout. */
3097 for (wp = firstwin; win_valid(wp); wp = nextwp)
3098 {
3099 nextwp = wp->w_next;
3100 if (wp != curwin) /* don't close current window */
3101 {
3102
3103 /* Check if it's allowed to abandon this window */
3104 r = can_abandon(wp->w_buffer, forceit);
3105#ifdef FEAT_AUTOCMD
3106 if (!win_valid(wp)) /* autocommands messed wp up */
3107 {
3108 nextwp = firstwin;
3109 continue;
3110 }
3111#endif
3112 if (!r)
3113 {
3114#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3115 if (message && (p_confirm || cmdmod.confirm) && p_write)
3116 {
3117 dialog_changed(wp->w_buffer, FALSE);
3118# ifdef FEAT_AUTOCMD
3119 if (!win_valid(wp)) /* autocommands messed wp up */
3120 {
3121 nextwp = firstwin;
3122 continue;
3123 }
3124# endif
3125 }
3126 if (bufIsChanged(wp->w_buffer))
3127#endif
3128 continue;
3129 }
3130 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3131 }
3132 }
3133
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 EMSG(_("E445: Other window contains changes"));
3136}
3137
3138#endif /* FEAT_WINDOWS */
3139
3140/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141 * Init the current window "curwin".
3142 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003145curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003147 redraw_win_later(curwin, NOT_VALID);
3148 curwin->w_lines_valid = 0;
3149 curwin->w_cursor.lnum = 1;
3150 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003152 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003154 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3155 curwin->w_pcmark.col = 0;
3156 curwin->w_prev_pcmark.lnum = 0;
3157 curwin->w_prev_pcmark.col = 0;
3158 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003160 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#ifdef FEAT_FKMAP
3164 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003165 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003167 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
3169}
3170
3171/*
3172 * Allocate the first window and put an empty buffer in it.
3173 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003174 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003176 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177win_alloc_first()
3178{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003179 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003180 return FAIL;
3181
3182#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003183 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 if (first_tabpage == NULL)
3185 return FAIL;
3186 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003187 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003188#endif
3189 return OK;
3190}
3191
3192/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003193 * Allocate the first window or the first window in a new tab page.
3194 * When "oldwin" is NULL create an empty buffer for it.
3195 * When "oldwin" is not NULL copy info from it to the new window (only with
3196 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003197 * Return FAIL when something goes wrong (out of memory).
3198 */
3199 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003200win_alloc_firstwin(oldwin)
3201 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003204 if (oldwin == NULL)
3205 {
3206 /* Very first window, need to create an empty buffer for it and
3207 * initialize from scratch. */
3208 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3209 if (curwin == NULL || curbuf == NULL)
3210 return FAIL;
3211 curwin->w_buffer = curbuf;
3212 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003214 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003216 curwin_init(); /* init current window */
3217 }
3218#ifdef FEAT_WINDOWS
3219 else
3220 {
3221 /* First window in new tab page, initialize it from "oldwin". */
3222 win_init(curwin, oldwin);
3223
3224# ifdef FEAT_SCROLLBIND
3225 /* We don't want scroll-binding in the first window. */
3226 curwin->w_p_scb = FALSE;
3227# endif
3228 }
3229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3232 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003233 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 topframe->fr_layout = FR_LEAF;
3235#ifdef FEAT_VERTSPLIT
3236 topframe->fr_width = Columns;
3237#endif
3238 topframe->fr_height = Rows - p_ch;
3239 topframe->fr_win = curwin;
3240 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241
3242 return OK;
3243}
3244
3245/*
3246 * Initialize the window and frame size to the maximum.
3247 */
3248 void
3249win_init_size()
3250{
3251 firstwin->w_height = ROWS_AVAIL;
3252 topframe->fr_height = ROWS_AVAIL;
3253#ifdef FEAT_VERTSPLIT
3254 firstwin->w_width = Columns;
3255 topframe->fr_width = Columns;
3256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003260
3261/*
3262 * Allocate a new tabpage_T and init the values.
3263 * Returns NULL when out of memory.
3264 */
3265 static tabpage_T *
3266alloc_tabpage()
3267{
3268 tabpage_T *tp;
3269
3270 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3271 if (tp != NULL)
3272 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003273# ifdef FEAT_GUI
3274 int i;
3275
3276 for (i = 0; i < 3; i++)
3277 tp->tp_prev_which_scrollbars[i] = -1;
3278# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003279# ifdef FEAT_DIFF
3280 tp->tp_diff_invalid = TRUE;
3281# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003282#ifdef FEAT_EVAL
3283 /* init t: variables */
3284 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3285#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003286 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003287 }
3288 return tp;
3289}
3290
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003291 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003292free_tabpage(tp)
3293 tabpage_T *tp;
3294{
3295# ifdef FEAT_DIFF
3296 diff_clear(tp);
3297# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003298 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003299#ifdef FEAT_EVAL
3300 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3301#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003302 vim_free(tp);
3303}
3304
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003305/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003306 * Create a new Tab page with one window.
3307 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003308 * When "after" is 0 put it just after the current Tab page.
3309 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003310 * Return FAIL or OK.
3311 */
3312 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003313win_new_tabpage(after)
3314 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003315{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003316 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003317 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003318 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003319
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003320 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003321 if (newtp == NULL)
3322 return FAIL;
3323
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003325 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003326 {
3327 vim_free(newtp);
3328 return FAIL;
3329 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003330 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003331
3332 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003333 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003335 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003336 if (after == 1)
3337 {
3338 /* New tab page becomes the first one. */
3339 newtp->tp_next = first_tabpage;
3340 first_tabpage = newtp;
3341 }
3342 else
3343 {
3344 if (after > 0)
3345 {
3346 /* Put new tab page before tab page "after". */
3347 n = 2;
3348 for (tp = first_tabpage; tp->tp_next != NULL
3349 && n < after; tp = tp->tp_next)
3350 ++n;
3351 }
3352 newtp->tp_next = tp->tp_next;
3353 tp->tp_next = newtp;
3354 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003355 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003356 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003357 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358
3359 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003360 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003361
3362#if defined(FEAT_GUI)
3363 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3364 * scrollbars. Have to update them anyway. */
3365 if (gui.in_use && starting == 0)
3366 {
3367 gui_init_which_components(NULL);
3368 gui_update_scrollbars(TRUE);
3369 }
3370 need_mouse_correct = TRUE;
3371#endif
3372
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003373 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003374#ifdef FEAT_AUTOCMD
3375 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3376 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3377#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003378 return OK;
3379 }
3380
3381 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383 return FAIL;
3384}
3385
3386/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003387 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3388 * like with ":split".
3389 * Returns OK if a new tab page was created, FAIL otherwise.
3390 */
3391 int
3392may_open_tabpage()
3393{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003394 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003395
Bram Moolenaard326ce82007-03-11 14:48:29 +00003396 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003397 {
3398 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003399 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003400 return win_new_tabpage(n);
3401 }
3402 return FAIL;
3403}
3404
3405/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003406 * Create up to "maxcount" tabpages with empty windows.
3407 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003409 int
3410make_tabpages(maxcount)
3411 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003413 int count = maxcount;
3414 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003416 /* Limit to 'tabpagemax' tabs. */
3417 if (count > p_tpm)
3418 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003419
3420#ifdef FEAT_AUTOCMD
3421 /*
3422 * Don't execute autocommands while creating the tab pages. Must do that
3423 * when putting the buffers in the windows.
3424 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003425 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003426#endif
3427
3428 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003429 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003431
3432#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003433 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003434#endif
3435
3436 /* return actual number of tab pages */
3437 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438}
3439
3440/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003441 * Return TRUE when "tpc" points to a valid tab page.
3442 */
3443 int
3444valid_tabpage(tpc)
3445 tabpage_T *tpc;
3446{
3447 tabpage_T *tp;
3448
3449 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3450 if (tp == tpc)
3451 return TRUE;
3452 return FALSE;
3453}
3454
3455/*
3456 * Find tab page "n" (first one is 1). Returns NULL when not found.
3457 */
3458 tabpage_T *
3459find_tabpage(n)
3460 int n;
3461{
3462 tabpage_T *tp;
3463 int i = 1;
3464
3465 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3466 ++i;
3467 return tp;
3468}
3469
3470/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003472 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 */
3474 int
3475tabpage_index(ftp)
3476 tabpage_T *ftp;
3477{
3478 int i = 1;
3479 tabpage_T *tp;
3480
3481 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3482 ++i;
3483 return i;
3484}
3485
3486/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003487 * Prepare for leaving the current tab page.
3488 * When autocomands change "curtab" we don't leave the tab page and return
3489 * FAIL.
3490 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003492/*ARGSUSED*/
3493 static int
3494leave_tabpage(new_curbuf)
3495 buf_T *new_curbuf; /* what is going to be the new curbuf,
3496 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003497{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003498 tabpage_T *tp = curtab;
3499
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003500#ifdef FEAT_VISUAL
3501 reset_VIsual_and_resel(); /* stop Visual mode */
3502#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003503#ifdef FEAT_AUTOCMD
3504 if (new_curbuf != curbuf)
3505 {
3506 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3507 if (curtab != tp)
3508 return FAIL;
3509 }
3510 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3511 if (curtab != tp)
3512 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003513 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003514 if (curtab != tp)
3515 return FAIL;
3516#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003517#if defined(FEAT_GUI)
3518 /* Remove the scrollbars. They may be added back later. */
3519 if (gui.in_use)
3520 gui_remove_scrollbars();
3521#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003522 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003523 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 tp->tp_firstwin = firstwin;
3525 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003526 tp->tp_old_Rows = Rows;
3527 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003528 firstwin = NULL;
3529 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003530 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531}
3532
3533/*
3534 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003535 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 */
3537/*ARGSUSED*/
3538 static void
3539enter_tabpage(tp, old_curbuf)
3540 tabpage_T *tp;
3541 buf_T *old_curbuf;
3542{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003543 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003544 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003547 firstwin = tp->tp_firstwin;
3548 lastwin = tp->tp_lastwin;
3549 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003550
3551 /* We would like doing the TabEnter event first, but we don't have a
3552 * valid current window yet, which may break some commands.
3553 * This triggers autocommands, thus may make "tp" invalid. */
3554 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3555 prevwin = next_prevwin;
3556
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003557#ifdef FEAT_AUTOCMD
3558 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 if (old_curbuf != curbuf)
3561 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3562#endif
3563
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003564 last_status(FALSE); /* status line may appear or disappear */
3565 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003566 must_redraw = CLEAR; /* need to redraw everything */
3567#ifdef FEAT_DIFF
3568 diff_need_scrollbind = TRUE;
3569#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003570
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003571 /* The tabpage line may have appeared or disappeared, may need to resize
3572 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003573 * frame sizes too. Use the stored value of p_ch, so that it can be
3574 * different for each tab page. */
3575 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003576 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3577#ifdef FEAT_GUI_TABLINE
3578 && !gui_use_tabline()
3579#endif
3580 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003581 shell_new_rows();
3582#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003583 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003584 shell_new_columns(); /* update window widths */
3585#endif
3586
3587#if defined(FEAT_GUI)
3588 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3589 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003590 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003591 {
3592 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003593 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003594 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003595 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003596#endif
3597
3598 redraw_all_later(CLEAR);
3599}
3600
3601/*
3602 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604 */
3605 void
3606goto_tabpage(n)
3607 int n;
3608{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003609 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003610 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003611 int i;
3612
Bram Moolenaard68071d2006-05-02 22:08:30 +00003613 if (text_locked())
3614 {
3615 /* Not allowed when editing the command line. */
3616#ifdef FEAT_CMDWIN
3617 if (cmdwin_type != 0)
3618 EMSG(_(e_cmdwin));
3619 else
3620#endif
3621 EMSG(_(e_secure));
3622 return;
3623 }
3624
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003625 /* If there is only one it can't work. */
3626 if (first_tabpage->tp_next == NULL)
3627 {
3628 if (n > 1)
3629 beep_flush();
3630 return;
3631 }
3632
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003633 if (n == 0)
3634 {
3635 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003636 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003637 tp = first_tabpage;
3638 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003639 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003640 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003641 else if (n < 0)
3642 {
3643 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3644 * this N times. */
3645 ttp = curtab;
3646 for (i = n; i < 0; ++i)
3647 {
3648 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3649 tp = tp->tp_next)
3650 ;
3651 ttp = tp;
3652 }
3653 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003654 else if (n == 9999)
3655 {
3656 /* Go to last tab page. */
3657 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3658 ;
3659 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003660 else
3661 {
3662 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003663 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003664 if (tp == NULL)
3665 {
3666 beep_flush();
3667 return;
3668 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003669 }
3670
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003671 goto_tabpage_tp(tp);
3672
3673#ifdef FEAT_GUI_TABLINE
3674 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003675 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676#endif
3677}
3678
3679/*
3680 * Go to tabpage "tp".
3681 * Note: doesn't update the GUI tab.
3682 */
3683 void
3684goto_tabpage_tp(tp)
3685 tabpage_T *tp;
3686{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003687 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003688 {
3689 if (valid_tabpage(tp))
3690 enter_tabpage(tp, curbuf);
3691 else
3692 enter_tabpage(curtab, curbuf);
3693 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003697 * Enter window "wp" in tab page "tp".
3698 * Also updates the GUI tab.
3699 */
3700 void
3701goto_tabpage_win(tp, wp)
3702 tabpage_T *tp;
3703 win_T *wp;
3704{
3705 goto_tabpage_tp(tp);
3706 if (curtab == tp && win_valid(wp))
3707 {
3708 win_enter(wp, TRUE);
3709# ifdef FEAT_GUI_TABLINE
3710 if (gui_use_tabline())
3711 gui_mch_set_curtab(tabpage_index(curtab));
3712# endif
3713 }
3714}
3715
3716/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003717 * Move the current tab page to before tab page "nr".
3718 */
3719 void
3720tabpage_move(nr)
3721 int nr;
3722{
3723 int n = nr;
3724 tabpage_T *tp;
3725
3726 if (first_tabpage->tp_next == NULL)
3727 return;
3728
3729 /* Remove the current tab page from the list of tab pages. */
3730 if (curtab == first_tabpage)
3731 first_tabpage = curtab->tp_next;
3732 else
3733 {
3734 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3735 if (tp->tp_next == curtab)
3736 break;
3737 if (tp == NULL) /* "cannot happen" */
3738 return;
3739 tp->tp_next = curtab->tp_next;
3740 }
3741
3742 /* Re-insert it at the specified position. */
3743 if (n == 0)
3744 {
3745 curtab->tp_next = first_tabpage;
3746 first_tabpage = curtab;
3747 }
3748 else
3749 {
3750 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3751 --n;
3752 curtab->tp_next = tp->tp_next;
3753 tp->tp_next = curtab;
3754 }
3755
3756 /* Need to redraw the tabline. Tab page contents doesn't change. */
3757 redraw_tabline = TRUE;
3758}
3759
3760
3761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 * Go to another window.
3763 * When jumping to another buffer, stop Visual mode. Do this before
3764 * changing windows so we can yank the selection into the '*' register.
3765 * When jumping to another window on the same buffer, adjust its cursor
3766 * position to keep the same Visual area.
3767 */
3768 void
3769win_goto(wp)
3770 win_T *wp;
3771{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003772 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
3774 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003775 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return;
3777 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003778#ifdef FEAT_AUTOCMD
3779 if (curbuf_locked())
3780 return;
3781#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#ifdef FEAT_VISUAL
3784 if (wp->w_buffer != curbuf)
3785 reset_VIsual_and_resel();
3786 else if (VIsual_active)
3787 wp->w_cursor = curwin->w_cursor;
3788#endif
3789
3790#ifdef FEAT_GUI
3791 need_mouse_correct = TRUE;
3792#endif
3793 win_enter(wp, TRUE);
3794}
3795
3796#if defined(FEAT_PERL) || defined(PROTO)
3797/*
3798 * Find window number "winnr" (counting top to bottom).
3799 */
3800 win_T *
3801win_find_nr(winnr)
3802 int winnr;
3803{
3804 win_T *wp;
3805
3806# ifdef FEAT_WINDOWS
3807 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3808 if (--winnr == 0)
3809 break;
3810 return wp;
3811# else
3812 return curwin;
3813# endif
3814}
3815#endif
3816
3817#ifdef FEAT_VERTSPLIT
3818/*
3819 * Move to window above or below "count" times.
3820 */
3821 static void
3822win_goto_ver(up, count)
3823 int up; /* TRUE to go to win above */
3824 long count;
3825{
3826 frame_T *fr;
3827 frame_T *nfr;
3828 frame_T *foundfr;
3829
3830 foundfr = curwin->w_frame;
3831 while (count--)
3832 {
3833 /*
3834 * First go upwards in the tree of frames until we find a upwards or
3835 * downwards neighbor.
3836 */
3837 fr = foundfr;
3838 for (;;)
3839 {
3840 if (fr == topframe)
3841 goto end;
3842 if (up)
3843 nfr = fr->fr_prev;
3844 else
3845 nfr = fr->fr_next;
3846 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3847 break;
3848 fr = fr->fr_parent;
3849 }
3850
3851 /*
3852 * Now go downwards to find the bottom or top frame in it.
3853 */
3854 for (;;)
3855 {
3856 if (nfr->fr_layout == FR_LEAF)
3857 {
3858 foundfr = nfr;
3859 break;
3860 }
3861 fr = nfr->fr_child;
3862 if (nfr->fr_layout == FR_ROW)
3863 {
3864 /* Find the frame at the cursor row. */
3865 while (fr->fr_next != NULL
3866 && frame2win(fr)->w_wincol + fr->fr_width
3867 <= curwin->w_wincol + curwin->w_wcol)
3868 fr = fr->fr_next;
3869 }
3870 if (nfr->fr_layout == FR_COL && up)
3871 while (fr->fr_next != NULL)
3872 fr = fr->fr_next;
3873 nfr = fr;
3874 }
3875 }
3876end:
3877 if (foundfr != NULL)
3878 win_goto(foundfr->fr_win);
3879}
3880
3881/*
3882 * Move to left or right window.
3883 */
3884 static void
3885win_goto_hor(left, count)
3886 int left; /* TRUE to go to left win */
3887 long count;
3888{
3889 frame_T *fr;
3890 frame_T *nfr;
3891 frame_T *foundfr;
3892
3893 foundfr = curwin->w_frame;
3894 while (count--)
3895 {
3896 /*
3897 * First go upwards in the tree of frames until we find a left or
3898 * right neighbor.
3899 */
3900 fr = foundfr;
3901 for (;;)
3902 {
3903 if (fr == topframe)
3904 goto end;
3905 if (left)
3906 nfr = fr->fr_prev;
3907 else
3908 nfr = fr->fr_next;
3909 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3910 break;
3911 fr = fr->fr_parent;
3912 }
3913
3914 /*
3915 * Now go downwards to find the leftmost or rightmost frame in it.
3916 */
3917 for (;;)
3918 {
3919 if (nfr->fr_layout == FR_LEAF)
3920 {
3921 foundfr = nfr;
3922 break;
3923 }
3924 fr = nfr->fr_child;
3925 if (nfr->fr_layout == FR_COL)
3926 {
3927 /* Find the frame at the cursor row. */
3928 while (fr->fr_next != NULL
3929 && frame2win(fr)->w_winrow + fr->fr_height
3930 <= curwin->w_winrow + curwin->w_wrow)
3931 fr = fr->fr_next;
3932 }
3933 if (nfr->fr_layout == FR_ROW && left)
3934 while (fr->fr_next != NULL)
3935 fr = fr->fr_next;
3936 nfr = fr;
3937 }
3938 }
3939end:
3940 if (foundfr != NULL)
3941 win_goto(foundfr->fr_win);
3942}
3943#endif
3944
3945/*
3946 * Make window "wp" the current window.
3947 */
3948 void
3949win_enter(wp, undo_sync)
3950 win_T *wp;
3951 int undo_sync;
3952{
3953 win_enter_ext(wp, undo_sync, FALSE);
3954}
3955
3956/*
3957 * Make window wp the current window.
3958 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3959 * been closed and isn't valid.
3960 */
3961 static void
3962win_enter_ext(wp, undo_sync, curwin_invalid)
3963 win_T *wp;
3964 int undo_sync;
3965 int curwin_invalid;
3966{
3967#ifdef FEAT_AUTOCMD
3968 int other_buffer = FALSE;
3969#endif
3970
3971 if (wp == curwin && !curwin_invalid) /* nothing to do */
3972 return;
3973
3974#ifdef FEAT_AUTOCMD
3975 if (!curwin_invalid)
3976 {
3977 /*
3978 * Be careful: If autocommands delete the window, return now.
3979 */
3980 if (wp->w_buffer != curbuf)
3981 {
3982 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3983 other_buffer = TRUE;
3984 if (!win_valid(wp))
3985 return;
3986 }
3987 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3988 if (!win_valid(wp))
3989 return;
3990# ifdef FEAT_EVAL
3991 /* autocmds may abort script processing */
3992 if (aborting())
3993 return;
3994# endif
3995 }
3996#endif
3997
3998 /* sync undo before leaving the current buffer */
3999 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004000 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 /* may have to copy the buffer options when 'cpo' contains 'S' */
4002 if (wp->w_buffer != curbuf)
4003 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4004 if (!curwin_invalid)
4005 {
4006 prevwin = curwin; /* remember for CTRL-W p */
4007 curwin->w_redr_status = TRUE;
4008 }
4009 curwin = wp;
4010 curbuf = wp->w_buffer;
4011 check_cursor();
4012#ifdef FEAT_VIRTUALEDIT
4013 if (!virtual_active())
4014 curwin->w_cursor.coladd = 0;
4015#endif
4016 changed_line_abv_curs(); /* assume cursor position needs updating */
4017
4018 if (curwin->w_localdir != NULL)
4019 {
4020 /* Window has a local directory: Save current directory as global
4021 * directory (unless that was done already) and change to the local
4022 * directory. */
4023 if (globaldir == NULL)
4024 {
4025 char_u cwd[MAXPATHL];
4026
4027 if (mch_dirname(cwd, MAXPATHL) == OK)
4028 globaldir = vim_strsave(cwd);
4029 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004030 if (mch_chdir((char *)curwin->w_localdir) == 0)
4031 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033 else if (globaldir != NULL)
4034 {
4035 /* Window doesn't have a local directory and we are not in the global
4036 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004037 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 vim_free(globaldir);
4039 globaldir = NULL;
4040 shorten_fnames(TRUE);
4041 }
4042
4043#ifdef FEAT_AUTOCMD
4044 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4045 if (other_buffer)
4046 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4047#endif
4048
4049#ifdef FEAT_TITLE
4050 maketitle();
4051#endif
4052 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004053 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if (restart_edit)
4055 redraw_later(VALID); /* causes status line redraw */
4056
4057 /* set window height to desired minimal value */
4058 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4059 win_setheight((int)p_wh);
4060 else if (curwin->w_height == 0)
4061 win_setheight(1);
4062
4063#ifdef FEAT_VERTSPLIT
4064 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004065 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 win_setwidth((int)p_wiw);
4067#endif
4068
4069#ifdef FEAT_MOUSE
4070 setmouse(); /* in case jumped to/from help buffer */
4071#endif
4072
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004073 /* Change directories when the 'acd' option is set. */
4074 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075}
4076
4077#endif /* FEAT_WINDOWS */
4078
4079#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4080/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004081 * Jump to the first open window that contains buffer "buf", if one exists.
4082 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 */
4084 win_T *
4085buf_jump_open_win(buf)
4086 buf_T *buf;
4087{
4088# ifdef FEAT_WINDOWS
4089 win_T *wp;
4090
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004091 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 if (wp->w_buffer == buf)
4093 break;
4094 if (wp != NULL)
4095 win_enter(wp, FALSE);
4096 return wp;
4097# else
4098 if (curwin->w_buffer == buf)
4099 return curwin;
4100 return NULL;
4101# endif
4102}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004103
4104/*
4105 * Jump to the first open window in any tab page that contains buffer "buf",
4106 * if one exists.
4107 * Returns a pointer to the window found, otherwise NULL.
4108 */
4109 win_T *
4110buf_jump_open_tab(buf)
4111 buf_T *buf;
4112{
4113# ifdef FEAT_WINDOWS
4114 win_T *wp;
4115 tabpage_T *tp;
4116
4117 /* First try the current tab page. */
4118 wp = buf_jump_open_win(buf);
4119 if (wp != NULL)
4120 return wp;
4121
4122 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4123 if (tp != curtab)
4124 {
4125 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4126 if (wp->w_buffer == buf)
4127 break;
4128 if (wp != NULL)
4129 {
4130 goto_tabpage_win(tp, wp);
4131 if (curwin != wp)
4132 wp = NULL; /* something went wrong */
4133 break;
4134 }
4135 }
4136
4137 return wp;
4138# else
4139 if (curwin->w_buffer == buf)
4140 return curwin;
4141 return NULL;
4142# endif
4143}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#endif
4145
4146/*
4147 * allocate a window structure and link it in the window list
4148 */
4149/*ARGSUSED*/
4150 static win_T *
4151win_alloc(after)
4152 win_T *after;
4153{
4154 win_T *newwin;
4155
4156 /*
4157 * allocate window structure and linesizes arrays
4158 */
4159 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4160 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4161 {
4162 vim_free(newwin);
4163 newwin = NULL;
4164 }
4165
4166 if (newwin != NULL)
4167 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004168#ifdef FEAT_AUTOCMD
4169 /* Don't execute autocommands while the window is not properly
4170 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4171 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004172 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /*
4175 * link the window in the window list
4176 */
4177#ifdef FEAT_WINDOWS
4178 win_append(after, newwin);
4179#endif
4180#ifdef FEAT_VERTSPLIT
4181 newwin->w_wincol = 0;
4182 newwin->w_width = Columns;
4183#endif
4184
4185 /* position the display and the cursor at the top of the file. */
4186 newwin->w_topline = 1;
4187#ifdef FEAT_DIFF
4188 newwin->w_topfill = 0;
4189#endif
4190 newwin->w_botline = 2;
4191 newwin->w_cursor.lnum = 1;
4192#ifdef FEAT_SCROLLBIND
4193 newwin->w_scbind_pos = 1;
4194#endif
4195
4196 /* We won't calculate w_fraction until resizing the window */
4197 newwin->w_fraction = 0;
4198 newwin->w_prev_fraction_row = -1;
4199
4200#ifdef FEAT_GUI
4201 if (gui.in_use)
4202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4204 SBAR_LEFT, newwin);
4205 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4206 SBAR_RIGHT, newwin);
4207 }
4208#endif
4209#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004210 /* init w: variables */
4211 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212#endif
4213#ifdef FEAT_FOLDING
4214 foldInitWin(newwin);
4215#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004216#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004217 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004218#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004219#ifdef FEAT_SEARCH_EXTRA
4220 newwin->w_match_head = NULL;
4221 newwin->w_next_match_id = 4;
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 return newwin;
4225}
4226
4227#if defined(FEAT_WINDOWS) || defined(PROTO)
4228
4229/*
4230 * remove window 'wp' from the window list and free the structure
4231 */
4232 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004233win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004235 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 int i;
4238
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004239#ifdef FEAT_AUTOCMD
4240 /* Don't execute autocommands while the window is halfway being deleted.
4241 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004242 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004243#endif
4244
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004245#ifdef FEAT_MZSCHEME
4246 mzscheme_window_free(wp);
4247#endif
4248
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#ifdef FEAT_PERL
4250 perl_win_free(wp);
4251#endif
4252
4253#ifdef FEAT_PYTHON
4254 python_window_free(wp);
4255#endif
4256
4257#ifdef FEAT_TCL
4258 tcl_window_free(wp);
4259#endif
4260
4261#ifdef FEAT_RUBY
4262 ruby_window_free(wp);
4263#endif
4264
4265 clear_winopt(&wp->w_onebuf_opt);
4266 clear_winopt(&wp->w_allbuf_opt);
4267
4268#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004269 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#endif
4271
4272 if (prevwin == wp)
4273 prevwin = NULL;
4274 win_free_lsize(wp);
4275
4276 for (i = 0; i < wp->w_tagstacklen; ++i)
4277 vim_free(wp->w_tagstack[i].tagname);
4278
4279 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004282 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285#ifdef FEAT_JUMPLIST
4286 free_jumplist(wp);
4287#endif
4288
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004289#ifdef FEAT_QUICKFIX
4290 qf_free_all(wp);
4291#endif
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293#ifdef FEAT_GUI
4294 if (gui.in_use)
4295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4297 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4298 }
4299#endif /* FEAT_GUI */
4300
Bram Moolenaarf740b292006-02-16 22:11:02 +00004301 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004303
4304#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004305 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307}
4308
4309/*
4310 * Append window "wp" in the window list after window "after".
4311 */
4312 static void
4313win_append(after, wp)
4314 win_T *after, *wp;
4315{
4316 win_T *before;
4317
4318 if (after == NULL) /* after NULL is in front of the first */
4319 before = firstwin;
4320 else
4321 before = after->w_next;
4322
4323 wp->w_next = before;
4324 wp->w_prev = after;
4325 if (after == NULL)
4326 firstwin = wp;
4327 else
4328 after->w_next = wp;
4329 if (before == NULL)
4330 lastwin = wp;
4331 else
4332 before->w_prev = wp;
4333}
4334
4335/*
4336 * Remove a window from the window list.
4337 */
4338 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004339win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004341 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 if (wp->w_prev != NULL)
4344 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004345 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004347 else
4348 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (wp->w_next != NULL)
4350 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004351 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004353 else
4354 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355}
4356
4357/*
4358 * Append frame "frp" in a frame list after frame "after".
4359 */
4360 static void
4361frame_append(after, frp)
4362 frame_T *after, *frp;
4363{
4364 frp->fr_next = after->fr_next;
4365 after->fr_next = frp;
4366 if (frp->fr_next != NULL)
4367 frp->fr_next->fr_prev = frp;
4368 frp->fr_prev = after;
4369}
4370
4371/*
4372 * Insert frame "frp" in a frame list before frame "before".
4373 */
4374 static void
4375frame_insert(before, frp)
4376 frame_T *before, *frp;
4377{
4378 frp->fr_next = before;
4379 frp->fr_prev = before->fr_prev;
4380 before->fr_prev = frp;
4381 if (frp->fr_prev != NULL)
4382 frp->fr_prev->fr_next = frp;
4383 else
4384 frp->fr_parent->fr_child = frp;
4385}
4386
4387/*
4388 * Remove a frame from a frame list.
4389 */
4390 static void
4391frame_remove(frp)
4392 frame_T *frp;
4393{
4394 if (frp->fr_prev != NULL)
4395 frp->fr_prev->fr_next = frp->fr_next;
4396 else
4397 frp->fr_parent->fr_child = frp->fr_next;
4398 if (frp->fr_next != NULL)
4399 frp->fr_next->fr_prev = frp->fr_prev;
4400}
4401
4402#endif /* FEAT_WINDOWS */
4403
4404/*
4405 * Allocate w_lines[] for window "wp".
4406 * Return FAIL for failure, OK for success.
4407 */
4408 int
4409win_alloc_lines(wp)
4410 win_T *wp;
4411{
4412 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004413 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (wp->w_lines == NULL)
4415 return FAIL;
4416 return OK;
4417}
4418
4419/*
4420 * free lsize arrays for a window
4421 */
4422 void
4423win_free_lsize(wp)
4424 win_T *wp;
4425{
4426 vim_free(wp->w_lines);
4427 wp->w_lines = NULL;
4428}
4429
4430/*
4431 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004432 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
4434 void
4435shell_new_rows()
4436{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004437 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 if (firstwin == NULL) /* not initialized yet */
4440 return;
4441#ifdef FEAT_WINDOWS
4442 if (h < frame_minheight(topframe, NULL))
4443 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004444
4445 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 * that doesn't result in the right height, forget about that option. */
4447 frame_new_height(topframe, h, FALSE, TRUE);
4448 if (topframe->fr_height != h)
4449 frame_new_height(topframe, h, FALSE, FALSE);
4450
4451 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4452#else
4453 if (h < 1)
4454 h = 1;
4455 win_new_height(firstwin, h);
4456#endif
4457 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004458#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004459 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004460#endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#if 0
4463 /* Disabled: don't want making the screen smaller make a window larger. */
4464 if (p_ea)
4465 win_equal(curwin, FALSE, 'v');
4466#endif
4467}
4468
4469#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4470/*
4471 * Called from win_new_shellsize() after Columns changed.
4472 */
4473 void
4474shell_new_columns()
4475{
4476 if (firstwin == NULL) /* not initialized yet */
4477 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004478
4479 /* First try setting the widths of windows with 'winfixwidth'. If that
4480 * doesn't result in the right width, forget about that option. */
4481 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4482 if (topframe->fr_width != Columns)
4483 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4484
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4486#if 0
4487 /* Disabled: don't want making the screen smaller make a window larger. */
4488 if (p_ea)
4489 win_equal(curwin, FALSE, 'h');
4490#endif
4491}
4492#endif
4493
4494#if defined(FEAT_CMDWIN) || defined(PROTO)
4495/*
4496 * Save the size of all windows in "gap".
4497 */
4498 void
4499win_size_save(gap)
4500 garray_T *gap;
4501
4502{
4503 win_T *wp;
4504
4505 ga_init2(gap, (int)sizeof(int), 1);
4506 if (ga_grow(gap, win_count() * 2) == OK)
4507 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4508 {
4509 ((int *)gap->ga_data)[gap->ga_len++] =
4510 wp->w_width + wp->w_vsep_width;
4511 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4512 }
4513}
4514
4515/*
4516 * Restore window sizes, but only if the number of windows is still the same.
4517 * Does not free the growarray.
4518 */
4519 void
4520win_size_restore(gap)
4521 garray_T *gap;
4522{
4523 win_T *wp;
4524 int i;
4525
4526 if (win_count() * 2 == gap->ga_len)
4527 {
4528 i = 0;
4529 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4530 {
4531 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4532 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4533 }
4534 /* recompute the window positions */
4535 (void)win_comp_pos();
4536 }
4537}
4538#endif /* FEAT_CMDWIN */
4539
4540#if defined(FEAT_WINDOWS) || defined(PROTO)
4541/*
4542 * Update the position for all windows, using the width and height of the
4543 * frames.
4544 * Returns the row just after the last window.
4545 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004546 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547win_comp_pos()
4548{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004549 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 int col = 0;
4551
4552 frame_comp_pos(topframe, &row, &col);
4553 return row;
4554}
4555
4556/*
4557 * Update the position of the windows in frame "topfrp", using the width and
4558 * height of the frames.
4559 * "*row" and "*col" are the top-left position of the frame. They are updated
4560 * to the bottom-right position plus one.
4561 */
4562 static void
4563frame_comp_pos(topfrp, row, col)
4564 frame_T *topfrp;
4565 int *row;
4566 int *col;
4567{
4568 win_T *wp;
4569 frame_T *frp;
4570#ifdef FEAT_VERTSPLIT
4571 int startcol;
4572 int startrow;
4573#endif
4574
4575 wp = topfrp->fr_win;
4576 if (wp != NULL)
4577 {
4578 if (wp->w_winrow != *row
4579#ifdef FEAT_VERTSPLIT
4580 || wp->w_wincol != *col
4581#endif
4582 )
4583 {
4584 /* position changed, redraw */
4585 wp->w_winrow = *row;
4586#ifdef FEAT_VERTSPLIT
4587 wp->w_wincol = *col;
4588#endif
4589 redraw_win_later(wp, NOT_VALID);
4590 wp->w_redr_status = TRUE;
4591 }
4592 *row += wp->w_height + wp->w_status_height;
4593#ifdef FEAT_VERTSPLIT
4594 *col += wp->w_width + wp->w_vsep_width;
4595#endif
4596 }
4597 else
4598 {
4599#ifdef FEAT_VERTSPLIT
4600 startrow = *row;
4601 startcol = *col;
4602#endif
4603 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4604 {
4605#ifdef FEAT_VERTSPLIT
4606 if (topfrp->fr_layout == FR_ROW)
4607 *row = startrow; /* all frames are at the same row */
4608 else
4609 *col = startcol; /* all frames are at the same col */
4610#endif
4611 frame_comp_pos(frp, row, col);
4612 }
4613 }
4614}
4615
4616#endif /* FEAT_WINDOWS */
4617
4618/*
4619 * Set current window height and take care of repositioning other windows to
4620 * fit around it.
4621 */
4622 void
4623win_setheight(height)
4624 int height;
4625{
4626 win_setheight_win(height, curwin);
4627}
4628
4629/*
4630 * Set the window height of window "win" and take care of repositioning other
4631 * windows to fit around it.
4632 */
4633 void
4634win_setheight_win(height, win)
4635 int height;
4636 win_T *win;
4637{
4638 int row;
4639
4640 if (win == curwin)
4641 {
4642 /* Always keep current window at least one line high, even when
4643 * 'winminheight' is zero. */
4644#ifdef FEAT_WINDOWS
4645 if (height < p_wmh)
4646 height = p_wmh;
4647#endif
4648 if (height == 0)
4649 height = 1;
4650 }
4651
4652#ifdef FEAT_WINDOWS
4653 frame_setheight(win->w_frame, height + win->w_status_height);
4654
4655 /* recompute the window positions */
4656 row = win_comp_pos();
4657#else
4658 if (height > topframe->fr_height)
4659 height = topframe->fr_height;
4660 win->w_height = height;
4661 row = height;
4662#endif
4663
4664 /*
4665 * If there is extra space created between the last window and the command
4666 * line, clear it.
4667 */
4668 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4669 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4670 cmdline_row = row;
4671 msg_row = row;
4672 msg_col = 0;
4673
4674 redraw_all_later(NOT_VALID);
4675}
4676
4677#if defined(FEAT_WINDOWS) || defined(PROTO)
4678
4679/*
4680 * Set the height of a frame to "height" and take care that all frames and
4681 * windows inside it are resized. Also resize frames on the left and right if
4682 * the are in the same FR_ROW frame.
4683 *
4684 * Strategy:
4685 * If the frame is part of a FR_COL frame, try fitting the frame in that
4686 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4687 * go to containing frames to resize them and make room.
4688 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4689 * Check for the minimal height of the FR_ROW frame.
4690 * At the top level we can also use change the command line height.
4691 */
4692 static void
4693frame_setheight(curfrp, height)
4694 frame_T *curfrp;
4695 int height;
4696{
4697 int room; /* total number of lines available */
4698 int take; /* number of lines taken from other windows */
4699 int room_cmdline; /* lines available from cmdline */
4700 int run;
4701 frame_T *frp;
4702 int h;
4703 int room_reserved;
4704
4705 /* If the height already is the desired value, nothing to do. */
4706 if (curfrp->fr_height == height)
4707 return;
4708
4709 if (curfrp->fr_parent == NULL)
4710 {
4711 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004712 if (height > ROWS_AVAIL)
4713 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (height > 0)
4715 frame_new_height(curfrp, height, FALSE, FALSE);
4716 }
4717 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4718 {
4719 /* Row of frames: Also need to resize frames left and right of this
4720 * one. First check for the minimal height of these. */
4721 h = frame_minheight(curfrp->fr_parent, NULL);
4722 if (height < h)
4723 height = h;
4724 frame_setheight(curfrp->fr_parent, height);
4725 }
4726 else
4727 {
4728 /*
4729 * Column of frames: try to change only frames in this column.
4730 */
4731#ifdef FEAT_VERTSPLIT
4732 /*
4733 * Do this twice:
4734 * 1: compute room available, if it's not enough try resizing the
4735 * containing frame.
4736 * 2: compute the room available and adjust the height to it.
4737 * Try not to reduce the height of a window with 'winfixheight' set.
4738 */
4739 for (run = 1; run <= 2; ++run)
4740#else
4741 for (;;)
4742#endif
4743 {
4744 room = 0;
4745 room_reserved = 0;
4746 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4747 frp = frp->fr_next)
4748 {
4749 if (frp != curfrp
4750 && frp->fr_win != NULL
4751 && frp->fr_win->w_p_wfh)
4752 room_reserved += frp->fr_height;
4753 room += frp->fr_height;
4754 if (frp != curfrp)
4755 room -= frame_minheight(frp, NULL);
4756 }
4757#ifdef FEAT_VERTSPLIT
4758 if (curfrp->fr_width != Columns)
4759 room_cmdline = 0;
4760 else
4761#endif
4762 {
4763 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4764 + lastwin->w_height + lastwin->w_status_height);
4765 if (room_cmdline < 0)
4766 room_cmdline = 0;
4767 }
4768
4769 if (height <= room + room_cmdline)
4770 break;
4771#ifdef FEAT_VERTSPLIT
4772 if (run == 2 || curfrp->fr_width == Columns)
4773#endif
4774 {
4775 if (height > room + room_cmdline)
4776 height = room + room_cmdline;
4777 break;
4778 }
4779#ifdef FEAT_VERTSPLIT
4780 frame_setheight(curfrp->fr_parent, height
4781 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4782#endif
4783 /*NOTREACHED*/
4784 }
4785
4786 /*
4787 * Compute the number of lines we will take from others frames (can be
4788 * negative!).
4789 */
4790 take = height - curfrp->fr_height;
4791
4792 /* If there is not enough room, also reduce the height of a window
4793 * with 'winfixheight' set. */
4794 if (height > room + room_cmdline - room_reserved)
4795 room_reserved = room + room_cmdline - height;
4796 /* If there is only a 'winfixheight' window and making the
4797 * window smaller, need to make the other window taller. */
4798 if (take < 0 && room - curfrp->fr_height < room_reserved)
4799 room_reserved = 0;
4800
4801 if (take > 0 && room_cmdline > 0)
4802 {
4803 /* use lines from cmdline first */
4804 if (take < room_cmdline)
4805 room_cmdline = take;
4806 take -= room_cmdline;
4807 topframe->fr_height += room_cmdline;
4808 }
4809
4810 /*
4811 * set the current frame to the new height
4812 */
4813 frame_new_height(curfrp, height, FALSE, FALSE);
4814
4815 /*
4816 * First take lines from the frames after the current frame. If
4817 * that is not enough, takes lines from frames above the current
4818 * frame.
4819 */
4820 for (run = 0; run < 2; ++run)
4821 {
4822 if (run == 0)
4823 frp = curfrp->fr_next; /* 1st run: start with next window */
4824 else
4825 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4826 while (frp != NULL && take != 0)
4827 {
4828 h = frame_minheight(frp, NULL);
4829 if (room_reserved > 0
4830 && frp->fr_win != NULL
4831 && frp->fr_win->w_p_wfh)
4832 {
4833 if (room_reserved >= frp->fr_height)
4834 room_reserved -= frp->fr_height;
4835 else
4836 {
4837 if (frp->fr_height - room_reserved > take)
4838 room_reserved = frp->fr_height - take;
4839 take -= frp->fr_height - room_reserved;
4840 frame_new_height(frp, room_reserved, FALSE, FALSE);
4841 room_reserved = 0;
4842 }
4843 }
4844 else
4845 {
4846 if (frp->fr_height - take < h)
4847 {
4848 take -= frp->fr_height - h;
4849 frame_new_height(frp, h, FALSE, FALSE);
4850 }
4851 else
4852 {
4853 frame_new_height(frp, frp->fr_height - take,
4854 FALSE, FALSE);
4855 take = 0;
4856 }
4857 }
4858 if (run == 0)
4859 frp = frp->fr_next;
4860 else
4861 frp = frp->fr_prev;
4862 }
4863 }
4864 }
4865}
4866
4867#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4868/*
4869 * Set current window width and take care of repositioning other windows to
4870 * fit around it.
4871 */
4872 void
4873win_setwidth(width)
4874 int width;
4875{
4876 win_setwidth_win(width, curwin);
4877}
4878
4879 void
4880win_setwidth_win(width, wp)
4881 int width;
4882 win_T *wp;
4883{
4884 /* Always keep current window at least one column wide, even when
4885 * 'winminwidth' is zero. */
4886 if (wp == curwin)
4887 {
4888 if (width < p_wmw)
4889 width = p_wmw;
4890 if (width == 0)
4891 width = 1;
4892 }
4893
4894 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4895
4896 /* recompute the window positions */
4897 (void)win_comp_pos();
4898
4899 redraw_all_later(NOT_VALID);
4900}
4901
4902/*
4903 * Set the width of a frame to "width" and take care that all frames and
4904 * windows inside it are resized. Also resize frames above and below if the
4905 * are in the same FR_ROW frame.
4906 *
4907 * Strategy is similar to frame_setheight().
4908 */
4909 static void
4910frame_setwidth(curfrp, width)
4911 frame_T *curfrp;
4912 int width;
4913{
4914 int room; /* total number of lines available */
4915 int take; /* number of lines taken from other windows */
4916 int run;
4917 frame_T *frp;
4918 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004919 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 /* If the width already is the desired value, nothing to do. */
4922 if (curfrp->fr_width == width)
4923 return;
4924
4925 if (curfrp->fr_parent == NULL)
4926 /* topframe: can't change width */
4927 return;
4928
4929 if (curfrp->fr_parent->fr_layout == FR_COL)
4930 {
4931 /* Column of frames: Also need to resize frames above and below of
4932 * this one. First check for the minimal width of these. */
4933 w = frame_minwidth(curfrp->fr_parent, NULL);
4934 if (width < w)
4935 width = w;
4936 frame_setwidth(curfrp->fr_parent, width);
4937 }
4938 else
4939 {
4940 /*
4941 * Row of frames: try to change only frames in this row.
4942 *
4943 * Do this twice:
4944 * 1: compute room available, if it's not enough try resizing the
4945 * containing frame.
4946 * 2: compute the room available and adjust the width to it.
4947 */
4948 for (run = 1; run <= 2; ++run)
4949 {
4950 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004951 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4953 frp = frp->fr_next)
4954 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004955 if (frp != curfrp
4956 && frp->fr_win != NULL
4957 && frp->fr_win->w_p_wfw)
4958 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 room += frp->fr_width;
4960 if (frp != curfrp)
4961 room -= frame_minwidth(frp, NULL);
4962 }
4963
4964 if (width <= room)
4965 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004966 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 if (width > room)
4969 width = room;
4970 break;
4971 }
4972 frame_setwidth(curfrp->fr_parent, width
4973 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4974 }
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Compute the number of lines we will take from others frames (can be
4978 * negative!).
4979 */
4980 take = width - curfrp->fr_width;
4981
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004982 /* If there is not enough room, also reduce the width of a window
4983 * with 'winfixwidth' set. */
4984 if (width > room - room_reserved)
4985 room_reserved = room - width;
4986 /* If there is only a 'winfixwidth' window and making the
4987 * window smaller, need to make the other window narrower. */
4988 if (take < 0 && room - curfrp->fr_width < room_reserved)
4989 room_reserved = 0;
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * set the current frame to the new width
4993 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004994 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 /*
4997 * First take lines from the frames right of the current frame. If
4998 * that is not enough, takes lines from frames left of the current
4999 * frame.
5000 */
5001 for (run = 0; run < 2; ++run)
5002 {
5003 if (run == 0)
5004 frp = curfrp->fr_next; /* 1st run: start with next window */
5005 else
5006 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5007 while (frp != NULL && take != 0)
5008 {
5009 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005010 if (room_reserved > 0
5011 && frp->fr_win != NULL
5012 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005014 if (room_reserved >= frp->fr_width)
5015 room_reserved -= frp->fr_width;
5016 else
5017 {
5018 if (frp->fr_width - room_reserved > take)
5019 room_reserved = frp->fr_width - take;
5020 take -= frp->fr_width - room_reserved;
5021 frame_new_width(frp, room_reserved, FALSE, FALSE);
5022 room_reserved = 0;
5023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025 else
5026 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005027 if (frp->fr_width - take < w)
5028 {
5029 take -= frp->fr_width - w;
5030 frame_new_width(frp, w, FALSE, FALSE);
5031 }
5032 else
5033 {
5034 frame_new_width(frp, frp->fr_width - take,
5035 FALSE, FALSE);
5036 take = 0;
5037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 if (run == 0)
5040 frp = frp->fr_next;
5041 else
5042 frp = frp->fr_prev;
5043 }
5044 }
5045 }
5046}
5047#endif /* FEAT_VERTSPLIT */
5048
5049/*
5050 * Check 'winminheight' for a valid value.
5051 */
5052 void
5053win_setminheight()
5054{
5055 int room;
5056 int first = TRUE;
5057 win_T *wp;
5058
5059 /* loop until there is a 'winminheight' that is possible */
5060 while (p_wmh > 0)
5061 {
5062 /* TODO: handle vertical splits */
5063 room = -p_wh;
5064 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5065 room += wp->w_height - p_wmh;
5066 if (room >= 0)
5067 break;
5068 --p_wmh;
5069 if (first)
5070 {
5071 EMSG(_(e_noroom));
5072 first = FALSE;
5073 }
5074 }
5075}
5076
5077#ifdef FEAT_MOUSE
5078
5079/*
5080 * Status line of dragwin is dragged "offset" lines down (negative is up).
5081 */
5082 void
5083win_drag_status_line(dragwin, offset)
5084 win_T *dragwin;
5085 int offset;
5086{
5087 frame_T *curfr;
5088 frame_T *fr;
5089 int room;
5090 int row;
5091 int up; /* if TRUE, drag status line up, otherwise down */
5092 int n;
5093
5094 fr = dragwin->w_frame;
5095 curfr = fr;
5096 if (fr != topframe) /* more than one window */
5097 {
5098 fr = fr->fr_parent;
5099 /* When the parent frame is not a column of frames, its parent should
5100 * be. */
5101 if (fr->fr_layout != FR_COL)
5102 {
5103 curfr = fr;
5104 if (fr != topframe) /* only a row of windows, may drag statusline */
5105 fr = fr->fr_parent;
5106 }
5107 }
5108
5109 /* If this is the last frame in a column, may want to resize the parent
5110 * frame instead (go two up to skip a row of frames). */
5111 while (curfr != topframe && curfr->fr_next == NULL)
5112 {
5113 if (fr != topframe)
5114 fr = fr->fr_parent;
5115 curfr = fr;
5116 if (fr != topframe)
5117 fr = fr->fr_parent;
5118 }
5119
5120 if (offset < 0) /* drag up */
5121 {
5122 up = TRUE;
5123 offset = -offset;
5124 /* sum up the room of the current frame and above it */
5125 if (fr == curfr)
5126 {
5127 /* only one window */
5128 room = fr->fr_height - frame_minheight(fr, NULL);
5129 }
5130 else
5131 {
5132 room = 0;
5133 for (fr = fr->fr_child; ; fr = fr->fr_next)
5134 {
5135 room += fr->fr_height - frame_minheight(fr, NULL);
5136 if (fr == curfr)
5137 break;
5138 }
5139 }
5140 fr = curfr->fr_next; /* put fr at frame that grows */
5141 }
5142 else /* drag down */
5143 {
5144 up = FALSE;
5145 /*
5146 * Only dragging the last status line can reduce p_ch.
5147 */
5148 room = Rows - cmdline_row;
5149 if (curfr->fr_next == NULL)
5150 room -= 1;
5151 else
5152 room -= p_ch;
5153 if (room < 0)
5154 room = 0;
5155 /* sum up the room of frames below of the current one */
5156 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5157 room += fr->fr_height - frame_minheight(fr, NULL);
5158 fr = curfr; /* put fr at window that grows */
5159 }
5160
5161 if (room < offset) /* Not enough room */
5162 offset = room; /* Move as far as we can */
5163 if (offset <= 0)
5164 return;
5165
5166 /*
5167 * Grow frame fr by "offset" lines.
5168 * Doesn't happen when dragging the last status line up.
5169 */
5170 if (fr != NULL)
5171 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5172
5173 if (up)
5174 fr = curfr; /* current frame gets smaller */
5175 else
5176 fr = curfr->fr_next; /* next frame gets smaller */
5177
5178 /*
5179 * Now make the other frames smaller.
5180 */
5181 while (fr != NULL && offset > 0)
5182 {
5183 n = frame_minheight(fr, NULL);
5184 if (fr->fr_height - offset <= n)
5185 {
5186 offset -= fr->fr_height - n;
5187 frame_new_height(fr, n, !up, FALSE);
5188 }
5189 else
5190 {
5191 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5192 break;
5193 }
5194 if (up)
5195 fr = fr->fr_prev;
5196 else
5197 fr = fr->fr_next;
5198 }
5199 row = win_comp_pos();
5200 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5201 cmdline_row = row;
5202 p_ch = Rows - cmdline_row;
5203 if (p_ch < 1)
5204 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005205 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005206 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 showmode();
5208}
5209
5210#ifdef FEAT_VERTSPLIT
5211/*
5212 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5213 */
5214 void
5215win_drag_vsep_line(dragwin, offset)
5216 win_T *dragwin;
5217 int offset;
5218{
5219 frame_T *curfr;
5220 frame_T *fr;
5221 int room;
5222 int left; /* if TRUE, drag separator line left, otherwise right */
5223 int n;
5224
5225 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005226 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return;
5228 curfr = fr;
5229 fr = fr->fr_parent;
5230 /* When the parent frame is not a row of frames, its parent should be. */
5231 if (fr->fr_layout != FR_ROW)
5232 {
5233 if (fr == topframe) /* only a column of windows (cannot happen?) */
5234 return;
5235 curfr = fr;
5236 fr = fr->fr_parent;
5237 }
5238
5239 /* If this is the last frame in a row, may want to resize a parent
5240 * frame instead. */
5241 while (curfr->fr_next == NULL)
5242 {
5243 if (fr == topframe)
5244 break;
5245 curfr = fr;
5246 fr = fr->fr_parent;
5247 if (fr != topframe)
5248 {
5249 curfr = fr;
5250 fr = fr->fr_parent;
5251 }
5252 }
5253
5254 if (offset < 0) /* drag left */
5255 {
5256 left = TRUE;
5257 offset = -offset;
5258 /* sum up the room of the current frame and left of it */
5259 room = 0;
5260 for (fr = fr->fr_child; ; fr = fr->fr_next)
5261 {
5262 room += fr->fr_width - frame_minwidth(fr, NULL);
5263 if (fr == curfr)
5264 break;
5265 }
5266 fr = curfr->fr_next; /* put fr at frame that grows */
5267 }
5268 else /* drag right */
5269 {
5270 left = FALSE;
5271 /* sum up the room of frames right of the current one */
5272 room = 0;
5273 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5274 room += fr->fr_width - frame_minwidth(fr, NULL);
5275 fr = curfr; /* put fr at window that grows */
5276 }
5277
5278 if (room < offset) /* Not enough room */
5279 offset = room; /* Move as far as we can */
5280 if (offset <= 0) /* No room at all, quit. */
5281 return;
5282
5283 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005284 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /* shrink other frames: current and at the left or at the right */
5287 if (left)
5288 fr = curfr; /* current frame gets smaller */
5289 else
5290 fr = curfr->fr_next; /* next frame gets smaller */
5291
5292 while (fr != NULL && offset > 0)
5293 {
5294 n = frame_minwidth(fr, NULL);
5295 if (fr->fr_width - offset <= n)
5296 {
5297 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005298 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300 else
5301 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005302 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 break;
5304 }
5305 if (left)
5306 fr = fr->fr_prev;
5307 else
5308 fr = fr->fr_next;
5309 }
5310 (void)win_comp_pos();
5311 redraw_all_later(NOT_VALID);
5312}
5313#endif /* FEAT_VERTSPLIT */
5314#endif /* FEAT_MOUSE */
5315
5316#endif /* FEAT_WINDOWS */
5317
5318/*
5319 * Set the height of a window.
5320 * This takes care of the things inside the window, not what happens to the
5321 * window position, the frame or to other windows.
5322 */
5323 static void
5324win_new_height(wp, height)
5325 win_T *wp;
5326 int height;
5327{
5328 linenr_T lnum;
5329 int sline, line_size;
5330#define FRACTION_MULT 16384L
5331
5332 /* Don't want a negative height. Happens when splitting a tiny window.
5333 * Will equalize heights soon to fix it. */
5334 if (height < 0)
5335 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005336 if (wp->w_height == height)
5337 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5340 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5341 + FRACTION_MULT / 2) / (long)wp->w_height;
5342
5343 wp->w_height = height;
5344 wp->w_skipcol = 0;
5345
5346 /* Don't change w_topline when height is zero. Don't set w_topline when
5347 * 'scrollbind' is set and this isn't the current window. */
5348 if (height > 0
5349#ifdef FEAT_SCROLLBIND
5350 && (!wp->w_p_scb || wp == curwin)
5351#endif
5352 )
5353 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005354 /*
5355 * Find a value for w_topline that shows the cursor at the same
5356 * relative position in the window as before (more or less).
5357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 lnum = wp->w_cursor.lnum;
5359 if (lnum < 1) /* can happen when starting up */
5360 lnum = 1;
5361 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5362 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5363 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005364
5365 if (sline >= 0)
5366 {
5367 /* Make sure the whole cursor line is visible, if possible. */
5368 int rows = plines_win(wp, lnum, FALSE);
5369
5370 if (sline > wp->w_height - rows)
5371 {
5372 sline = wp->w_height - rows;
5373 wp->w_wrow -= rows - line_size;
5374 }
5375 }
5376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (sline < 0)
5378 {
5379 /*
5380 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005381 * Make cursor line the first line in the window. If not enough
5382 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 */
5384 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005385 if (wp->w_wrow >= wp->w_height
5386 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5387 {
5388 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5389 --wp->w_wrow;
5390 while (wp->w_wrow >= wp->w_height)
5391 {
5392 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5393 + win_col_off2(wp);
5394 --wp->w_wrow;
5395 }
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 else
5399 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005400 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
5402#ifdef FEAT_FOLDING
5403 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5404 if (lnum == 1)
5405 {
5406 /* first line in buffer is folded */
5407 line_size = 1;
5408 --sline;
5409 break;
5410 }
5411#endif
5412 --lnum;
5413#ifdef FEAT_DIFF
5414 if (lnum == wp->w_topline)
5415 line_size = plines_win_nofill(wp, lnum, TRUE)
5416 + wp->w_topfill;
5417 else
5418#endif
5419 line_size = plines_win(wp, lnum, TRUE);
5420 sline -= line_size;
5421 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 if (sline < 0)
5424 {
5425 /*
5426 * Line we want at top would go off top of screen. Use next
5427 * line instead.
5428 */
5429#ifdef FEAT_FOLDING
5430 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5431#endif
5432 lnum++;
5433 wp->w_wrow -= line_size + sline;
5434 }
5435 else if (sline > 0)
5436 {
5437 /* First line of file reached, use that as topline. */
5438 lnum = 1;
5439 wp->w_wrow -= sline;
5440 }
5441 }
5442 set_topline(wp, lnum);
5443 }
5444
5445 if (wp == curwin)
5446 {
5447 if (p_so)
5448 update_topline();
5449 curs_columns(FALSE); /* validate w_wrow */
5450 }
5451 wp->w_prev_fraction_row = wp->w_wrow;
5452
5453 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005454 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455#ifdef FEAT_WINDOWS
5456 wp->w_redr_status = TRUE;
5457#endif
5458 invalidate_botline_win(wp);
5459}
5460
5461#ifdef FEAT_VERTSPLIT
5462/*
5463 * Set the width of a window.
5464 */
5465 static void
5466win_new_width(wp, width)
5467 win_T *wp;
5468 int width;
5469{
5470 wp->w_width = width;
5471 wp->w_lines_valid = 0;
5472 changed_line_abv_curs_win(wp);
5473 invalidate_botline_win(wp);
5474 if (wp == curwin)
5475 {
5476 update_topline();
5477 curs_columns(TRUE); /* validate w_wrow */
5478 }
5479 redraw_win_later(wp, NOT_VALID);
5480 wp->w_redr_status = TRUE;
5481}
5482#endif
5483
5484 void
5485win_comp_scroll(wp)
5486 win_T *wp;
5487{
5488 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5489 if (wp->w_p_scr == 0)
5490 wp->w_p_scr = 1;
5491}
5492
5493/*
5494 * command_height: called whenever p_ch has been changed
5495 */
5496 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005497command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499#ifdef FEAT_WINDOWS
5500 int h;
5501 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005502 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005504 /* Use the value of p_ch that we remembered. This is needed for when the
5505 * GUI starts up, we can't be sure in what order things happen. And when
5506 * p_ch was changed in another tab page. */
5507 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005508
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 /* Find bottom frame with width of screen. */
5510 frp = lastwin->w_frame;
5511# ifdef FEAT_VERTSPLIT
5512 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5513 frp = frp->fr_parent;
5514# endif
5515
5516 /* Avoid changing the height of a window with 'winfixheight' set. */
5517 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5518 && frp->fr_win->w_p_wfh)
5519 frp = frp->fr_prev;
5520
5521 if (starting != NO_SCREEN)
5522 {
5523 cmdline_row = Rows - p_ch;
5524
5525 if (p_ch > old_p_ch) /* p_ch got bigger */
5526 {
5527 while (p_ch > old_p_ch)
5528 {
5529 if (frp == NULL)
5530 {
5531 EMSG(_(e_noroom));
5532 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005533 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 cmdline_row = Rows - p_ch;
5535 break;
5536 }
5537 h = frp->fr_height - frame_minheight(frp, NULL);
5538 if (h > p_ch - old_p_ch)
5539 h = p_ch - old_p_ch;
5540 old_p_ch += h;
5541 frame_add_height(frp, -h);
5542 frp = frp->fr_prev;
5543 }
5544
5545 /* Recompute window positions. */
5546 (void)win_comp_pos();
5547
5548 /* clear the lines added to cmdline */
5549 if (full_screen)
5550 screen_fill((int)(cmdline_row), (int)Rows, 0,
5551 (int)Columns, ' ', ' ', 0);
5552 msg_row = cmdline_row;
5553 redraw_cmdline = TRUE;
5554 return;
5555 }
5556
5557 if (msg_row < cmdline_row)
5558 msg_row = cmdline_row;
5559 redraw_cmdline = TRUE;
5560 }
5561 frame_add_height(frp, (int)(old_p_ch - p_ch));
5562
5563 /* Recompute window positions. */
5564 if (frp != lastwin->w_frame)
5565 (void)win_comp_pos();
5566#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005568 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#endif
5570}
5571
5572#if defined(FEAT_WINDOWS) || defined(PROTO)
5573/*
5574 * Resize frame "frp" to be "n" lines higher (negative for less high).
5575 * Also resize the frames it is contained in.
5576 */
5577 static void
5578frame_add_height(frp, n)
5579 frame_T *frp;
5580 int n;
5581{
5582 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5583 for (;;)
5584 {
5585 frp = frp->fr_parent;
5586 if (frp == NULL)
5587 break;
5588 frp->fr_height += n;
5589 }
5590}
5591
5592/*
5593 * Add or remove a status line for the bottom window(s), according to the
5594 * value of 'laststatus'.
5595 */
5596 void
5597last_status(morewin)
5598 int morewin; /* pretend there are two or more windows */
5599{
5600 /* Don't make a difference between horizontal or vertical split. */
5601 last_status_rec(topframe, (p_ls == 2
5602 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5603}
5604
5605 static void
5606last_status_rec(fr, statusline)
5607 frame_T *fr;
5608 int statusline;
5609{
5610 frame_T *fp;
5611 win_T *wp;
5612
5613 if (fr->fr_layout == FR_LEAF)
5614 {
5615 wp = fr->fr_win;
5616 if (wp->w_status_height != 0 && !statusline)
5617 {
5618 /* remove status line */
5619 win_new_height(wp, wp->w_height + 1);
5620 wp->w_status_height = 0;
5621 comp_col();
5622 }
5623 else if (wp->w_status_height == 0 && statusline)
5624 {
5625 /* Find a frame to take a line from. */
5626 fp = fr;
5627 while (fp->fr_height <= frame_minheight(fp, NULL))
5628 {
5629 if (fp == topframe)
5630 {
5631 EMSG(_(e_noroom));
5632 return;
5633 }
5634 /* In a column of frames: go to frame above. If already at
5635 * the top or in a row of frames: go to parent. */
5636 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5637 fp = fp->fr_prev;
5638 else
5639 fp = fp->fr_parent;
5640 }
5641 wp->w_status_height = 1;
5642 if (fp != fr)
5643 {
5644 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5645 frame_fix_height(wp);
5646 (void)win_comp_pos();
5647 }
5648 else
5649 win_new_height(wp, wp->w_height - 1);
5650 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005651 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 }
5654#ifdef FEAT_VERTSPLIT
5655 else if (fr->fr_layout == FR_ROW)
5656 {
5657 /* vertically split windows, set status line for each one */
5658 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5659 last_status_rec(fp, statusline);
5660 }
5661#endif
5662 else
5663 {
5664 /* horizontally split window, set status line for last one */
5665 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5666 ;
5667 last_status_rec(fp, statusline);
5668 }
5669}
5670
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005671/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005672 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005673 */
5674 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005675tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005676{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005677#ifdef FEAT_GUI_TABLINE
5678 /* When the GUI has the tabline then this always returns zero. */
5679 if (gui_use_tabline())
5680 return 0;
5681#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005682 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005683 {
5684 case 0: return 0;
5685 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5686 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005687 return 1;
5688}
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690#endif /* FEAT_WINDOWS */
5691
5692#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5693/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005694 * Get the file name at the cursor.
5695 * If Visual mode is active, use the selected text if it's in one line.
5696 * Returns the name in allocated memory, NULL for failure.
5697 */
5698 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005699grab_file_name(count, file_lnum)
5700 long count;
5701 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005702{
5703# ifdef FEAT_VISUAL
5704 if (VIsual_active)
5705 {
5706 int len;
5707 char_u *ptr;
5708
5709 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5710 return NULL;
5711 return find_file_name_in_path(ptr, len,
5712 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5713 }
5714# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005715 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5716 file_lnum);
5717
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005718}
5719
5720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 * Return the file name under or after the cursor.
5722 *
5723 * The 'path' option is searched if the file name is not absolute.
5724 * The string returned has been alloc'ed and should be freed by the caller.
5725 * NULL is returned if the file name or file is not found.
5726 *
5727 * options:
5728 * FNAME_MESS give error messages
5729 * FNAME_EXP expand to path
5730 * FNAME_HYP check for hypertext link
5731 * FNAME_INCL apply "includeexpr"
5732 */
5733 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005734file_name_at_cursor(options, count, file_lnum)
5735 int options;
5736 long count;
5737 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005740 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5741 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742}
5743
5744/*
5745 * Return the name of the file under or after ptr[col].
5746 * Otherwise like file_name_at_cursor().
5747 */
5748 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005749file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u *line;
5751 int col;
5752 int options;
5753 long count;
5754 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005755 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 char_u *ptr;
5758 int len;
5759
5760 /*
5761 * search forward for what could be the start of a file name
5762 */
5763 ptr = line + col;
5764 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005765 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (*ptr == NUL) /* nothing found */
5767 {
5768 if (options & FNAME_MESS)
5769 EMSG(_("E446: No file name under cursor"));
5770 return NULL;
5771 }
5772
5773 /*
5774 * Search backward for first char of the file name.
5775 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5776 */
5777 while (ptr > line)
5778 {
5779#ifdef FEAT_MBYTE
5780 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5781 ptr -= len + 1;
5782 else
5783#endif
5784 if (vim_isfilec(ptr[-1])
5785 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5786 --ptr;
5787 else
5788 break;
5789 }
5790
5791 /*
5792 * Search forward for the last char of the file name.
5793 * Also allow "://" when ':' is not in 'isfname'.
5794 */
5795 len = 0;
5796 while (vim_isfilec(ptr[len])
5797 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5798#ifdef FEAT_MBYTE
5799 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005800 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 else
5802#endif
5803 ++len;
5804
5805 /*
5806 * If there is trailing punctuation, remove it.
5807 * But don't remove "..", could be a directory name.
5808 */
5809 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5810 && ptr[len - 2] != '.')
5811 --len;
5812
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005813 if (file_lnum != NULL)
5814 {
5815 char_u *p;
5816
5817 /* Get the number after the file name and a separator character */
5818 p = ptr + len;
5819 p = skipwhite(p);
5820 if (*p != NUL)
5821 {
5822 if (!isdigit(*p))
5823 ++p; /* skip the separator */
5824 p = skipwhite(p);
5825 if (isdigit(*p))
5826 *file_lnum = (int)getdigits(&p);
5827 }
5828 }
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5831}
5832
5833# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5834static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5835
5836 static char_u *
5837eval_includeexpr(ptr, len)
5838 char_u *ptr;
5839 int len;
5840{
5841 char_u *res;
5842
5843 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005844 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005845 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 set_vim_var_string(VV_FNAME, NULL, 0);
5847 return res;
5848}
5849#endif
5850
5851/*
5852 * Return the name of the file ptr[len] in 'path'.
5853 * Otherwise like file_name_at_cursor().
5854 */
5855 char_u *
5856find_file_name_in_path(ptr, len, options, count, rel_fname)
5857 char_u *ptr;
5858 int len;
5859 int options;
5860 long count;
5861 char_u *rel_fname; /* file we are searching relative to */
5862{
5863 char_u *file_name;
5864 int c;
5865# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5866 char_u *tofree = NULL;
5867
5868 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5869 {
5870 tofree = eval_includeexpr(ptr, len);
5871 if (tofree != NULL)
5872 {
5873 ptr = tofree;
5874 len = (int)STRLEN(ptr);
5875 }
5876 }
5877# endif
5878
5879 if (options & FNAME_EXP)
5880 {
5881 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5882 TRUE, rel_fname);
5883
5884# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5885 /*
5886 * If the file could not be found in a normal way, try applying
5887 * 'includeexpr' (unless done already).
5888 */
5889 if (file_name == NULL
5890 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5891 {
5892 tofree = eval_includeexpr(ptr, len);
5893 if (tofree != NULL)
5894 {
5895 ptr = tofree;
5896 len = (int)STRLEN(ptr);
5897 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5898 TRUE, rel_fname);
5899 }
5900 }
5901# endif
5902 if (file_name == NULL && (options & FNAME_MESS))
5903 {
5904 c = ptr[len];
5905 ptr[len] = NUL;
5906 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5907 ptr[len] = c;
5908 }
5909
5910 /* Repeat finding the file "count" times. This matters when it
5911 * appears several times in the path. */
5912 while (file_name != NULL && --count > 0)
5913 {
5914 vim_free(file_name);
5915 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5916 }
5917 }
5918 else
5919 file_name = vim_strnsave(ptr, len);
5920
5921# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5922 vim_free(tofree);
5923# endif
5924
5925 return file_name;
5926}
5927#endif /* FEAT_SEARCHPATH */
5928
5929/*
5930 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5931 * Also check for ":\\", which MS Internet Explorer accepts, return
5932 * URL_BACKSLASH.
5933 */
5934 static int
5935path_is_url(p)
5936 char_u *p;
5937{
5938 if (STRNCMP(p, "://", (size_t)3) == 0)
5939 return URL_SLASH;
5940 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5941 return URL_BACKSLASH;
5942 return 0;
5943}
5944
5945/*
5946 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5947 * Return URL_BACKSLASH for "name:\\".
5948 * Return zero otherwise.
5949 */
5950 int
5951path_with_url(fname)
5952 char_u *fname;
5953{
5954 char_u *p;
5955
5956 for (p = fname; isalpha(*p); ++p)
5957 ;
5958 return path_is_url(p);
5959}
5960
5961/*
5962 * Return TRUE if "name" is a full (absolute) path name or URL.
5963 */
5964 int
5965vim_isAbsName(name)
5966 char_u *name;
5967{
5968 return (path_with_url(name) != 0 || mch_isFullName(name));
5969}
5970
5971/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005972 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 *
5974 * return FAIL for failure, OK otherwise
5975 */
5976 int
5977vim_FullName(fname, buf, len, force)
5978 char_u *fname, *buf;
5979 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005980 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981{
5982 int retval = OK;
5983 int url;
5984
5985 *buf = NUL;
5986 if (fname == NULL)
5987 return FAIL;
5988
5989 url = path_with_url(fname);
5990 if (!url)
5991 retval = mch_FullName(fname, buf, len, force);
5992 if (url || retval == FAIL)
5993 {
5994 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005995 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
5997#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5998 slash_adjust(buf);
5999#endif
6000 return retval;
6001}
6002
6003/*
6004 * Return the minimal number of rows that is needed on the screen to display
6005 * the current number of windows.
6006 */
6007 int
6008min_rows()
6009{
6010 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006011#ifdef FEAT_WINDOWS
6012 tabpage_T *tp;
6013 int n;
6014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 if (firstwin == NULL) /* not initialized yet */
6017 return MIN_LINES;
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006020 total = 0;
6021 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6022 {
6023 n = frame_minheight(tp->tp_topframe, NULL);
6024 if (total < n)
6025 total = n;
6026 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006027 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006029 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006031 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 return total;
6033}
6034
6035/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006036 * Return TRUE if there is only one window (in the current tab page), not
6037 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 */
6039 int
6040only_one_window()
6041{
6042#ifdef FEAT_WINDOWS
6043 int count = 0;
6044 win_T *wp;
6045
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006046 /* If there is another tab page there always is another window. */
6047 if (first_tabpage->tp_next != NULL)
6048 return FALSE;
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006051 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052# ifdef FEAT_QUICKFIX
6053 || wp->w_p_pvw
6054# endif
6055 ) || wp == curwin)
6056 ++count;
6057 return (count <= 1);
6058#else
6059 return TRUE;
6060#endif
6061}
6062
6063#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6064/*
6065 * Correct the cursor line number in other windows. Used after changing the
6066 * current buffer, and before applying autocommands.
6067 * When "do_curwin" is TRUE, also check current window.
6068 */
6069 void
6070check_lnums(do_curwin)
6071 int do_curwin;
6072{
6073 win_T *wp;
6074
6075#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006076 tabpage_T *tp;
6077
6078 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6080#else
6081 wp = curwin;
6082 if (do_curwin)
6083#endif
6084 {
6085 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6086 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6087 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6088 wp->w_topline = curbuf->b_ml.ml_line_count;
6089 }
6090}
6091#endif
6092
6093#if defined(FEAT_WINDOWS) || defined(PROTO)
6094
6095/*
6096 * A snapshot of the window sizes, to restore them after closing the help
6097 * window.
6098 * Only these fields are used:
6099 * fr_layout
6100 * fr_width
6101 * fr_height
6102 * fr_next
6103 * fr_child
6104 * fr_win (only valid for the old curwin, NULL otherwise)
6105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107/*
6108 * Create a snapshot of the current frame sizes.
6109 */
6110 static void
6111make_snapshot()
6112{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006113 clear_snapshot(curtab);
6114 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115}
6116
6117 static void
6118make_snapshot_rec(fr, frp)
6119 frame_T *fr;
6120 frame_T **frp;
6121{
6122 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6123 if (*frp == NULL)
6124 return;
6125 (*frp)->fr_layout = fr->fr_layout;
6126# ifdef FEAT_VERTSPLIT
6127 (*frp)->fr_width = fr->fr_width;
6128# endif
6129 (*frp)->fr_height = fr->fr_height;
6130 if (fr->fr_next != NULL)
6131 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6132 if (fr->fr_child != NULL)
6133 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6134 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6135 (*frp)->fr_win = curwin;
6136}
6137
6138/*
6139 * Remove any existing snapshot.
6140 */
6141 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006142clear_snapshot(tp)
6143 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006145 clear_snapshot_rec(tp->tp_snapshot);
6146 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147}
6148
6149 static void
6150clear_snapshot_rec(fr)
6151 frame_T *fr;
6152{
6153 if (fr != NULL)
6154 {
6155 clear_snapshot_rec(fr->fr_next);
6156 clear_snapshot_rec(fr->fr_child);
6157 vim_free(fr);
6158 }
6159}
6160
6161/*
6162 * Restore a previously created snapshot, if there is any.
6163 * This is only done if the screen size didn't change and the window layout is
6164 * still the same.
6165 */
6166 static void
6167restore_snapshot(close_curwin)
6168 int close_curwin; /* closing current window */
6169{
6170 win_T *wp;
6171
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006172 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006174 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006176 && curtab->tp_snapshot->fr_height == topframe->fr_height
6177 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006179 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 win_comp_pos();
6181 if (wp != NULL && close_curwin)
6182 win_goto(wp);
6183 redraw_all_later(CLEAR);
6184 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006185 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186}
6187
6188/*
6189 * Check if frames "sn" and "fr" have the same layout, same following frames
6190 * and same children.
6191 */
6192 static int
6193check_snapshot_rec(sn, fr)
6194 frame_T *sn;
6195 frame_T *fr;
6196{
6197 if (sn->fr_layout != fr->fr_layout
6198 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6199 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6200 || (sn->fr_next != NULL
6201 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6202 || (sn->fr_child != NULL
6203 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6204 return FAIL;
6205 return OK;
6206}
6207
6208/*
6209 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6210 * following frames and children.
6211 * Returns a pointer to the old current window, or NULL.
6212 */
6213 static win_T *
6214restore_snapshot_rec(sn, fr)
6215 frame_T *sn;
6216 frame_T *fr;
6217{
6218 win_T *wp = NULL;
6219 win_T *wp2;
6220
6221 fr->fr_height = sn->fr_height;
6222# ifdef FEAT_VERTSPLIT
6223 fr->fr_width = sn->fr_width;
6224# endif
6225 if (fr->fr_layout == FR_LEAF)
6226 {
6227 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6228# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006229 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230# endif
6231 wp = sn->fr_win;
6232 }
6233 if (sn->fr_next != NULL)
6234 {
6235 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6236 if (wp2 != NULL)
6237 wp = wp2;
6238 }
6239 if (sn->fr_child != NULL)
6240 {
6241 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6242 if (wp2 != NULL)
6243 wp = wp2;
6244 }
6245 return wp;
6246}
6247
6248#endif
6249
6250#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6251/*
6252 * Return TRUE if there is any vertically split window.
6253 */
6254 int
6255win_hasvertsplit()
6256{
6257 frame_T *fr;
6258
6259 if (topframe->fr_layout == FR_ROW)
6260 return TRUE;
6261
6262 if (topframe->fr_layout == FR_COL)
6263 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6264 if (fr->fr_layout == FR_ROW)
6265 return TRUE;
6266
6267 return FALSE;
6268}
6269#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006270
6271#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6272/*
6273 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006274 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006275 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6276 * If no particular ID is desired, -1 must be specified for 'id'.
6277 * Return ID of added match, -1 on failure.
6278 */
6279 int
6280match_add(wp, grp, pat, prio, id)
6281 win_T *wp;
6282 char_u *grp;
6283 char_u *pat;
6284 int prio;
6285 int id;
6286{
6287 matchitem_T *cur;
6288 matchitem_T *prev;
6289 matchitem_T *m;
6290 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006291 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006292
6293 if (*grp == NUL || *pat == NUL)
6294 return -1;
6295 if (id < -1 || id == 0)
6296 {
6297 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6298 return -1;
6299 }
6300 if (id != -1)
6301 {
6302 cur = wp->w_match_head;
6303 while (cur != NULL)
6304 {
6305 if (cur->id == id)
6306 {
6307 EMSGN("E801: ID already taken: %ld", id);
6308 return -1;
6309 }
6310 cur = cur->next;
6311 }
6312 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006313 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006314 {
6315 EMSG2(_(e_nogroup), grp);
6316 return -1;
6317 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006318 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006319 {
6320 EMSG2(_(e_invarg2), pat);
6321 return -1;
6322 }
6323
6324 /* Find available match ID. */
6325 while (id == -1)
6326 {
6327 cur = wp->w_match_head;
6328 while (cur != NULL && cur->id != wp->w_next_match_id)
6329 cur = cur->next;
6330 if (cur == NULL)
6331 id = wp->w_next_match_id;
6332 wp->w_next_match_id++;
6333 }
6334
6335 /* Build new match. */
6336 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6337 m->id = id;
6338 m->priority = prio;
6339 m->pattern = vim_strsave(pat);
6340 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006341 m->match.regprog = regprog;
6342 m->match.rmm_ic = FALSE;
6343 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006344
6345 /* Insert new match. The match list is in ascending order with regard to
6346 * the match priorities. */
6347 cur = wp->w_match_head;
6348 prev = cur;
6349 while (cur != NULL && prio >= cur->priority)
6350 {
6351 prev = cur;
6352 cur = cur->next;
6353 }
6354 if (cur == prev)
6355 wp->w_match_head = m;
6356 else
6357 prev->next = m;
6358 m->next = cur;
6359
6360 redraw_later(SOME_VALID);
6361 return id;
6362}
6363
6364/*
6365 * Delete match with ID 'id' in the match list of window 'wp'.
6366 * Print error messages if 'perr' is TRUE.
6367 */
6368 int
6369match_delete(wp, id, perr)
6370 win_T *wp;
6371 int id;
6372 int perr;
6373{
6374 matchitem_T *cur = wp->w_match_head;
6375 matchitem_T *prev = cur;
6376
6377 if (id < 1)
6378 {
6379 if (perr == TRUE)
6380 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6381 id);
6382 return -1;
6383 }
6384 while (cur != NULL && cur->id != id)
6385 {
6386 prev = cur;
6387 cur = cur->next;
6388 }
6389 if (cur == NULL)
6390 {
6391 if (perr == TRUE)
6392 EMSGN("E803: ID not found: %ld", id);
6393 return -1;
6394 }
6395 if (cur == prev)
6396 wp->w_match_head = cur->next;
6397 else
6398 prev->next = cur->next;
6399 vim_free(cur->match.regprog);
6400 vim_free(cur->pattern);
6401 vim_free(cur);
6402 redraw_later(SOME_VALID);
6403 return 0;
6404}
6405
6406/*
6407 * Delete all matches in the match list of window 'wp'.
6408 */
6409 void
6410clear_matches(wp)
6411 win_T *wp;
6412{
6413 matchitem_T *m;
6414
6415 while (wp->w_match_head != NULL)
6416 {
6417 m = wp->w_match_head->next;
6418 vim_free(wp->w_match_head->match.regprog);
6419 vim_free(wp->w_match_head->pattern);
6420 vim_free(wp->w_match_head);
6421 wp->w_match_head = m;
6422 }
6423 redraw_later(SOME_VALID);
6424}
6425
6426/*
6427 * Get match from ID 'id' in window 'wp'.
6428 * Return NULL if match not found.
6429 */
6430 matchitem_T *
6431get_match(wp, id)
6432 win_T *wp;
6433 int id;
6434{
6435 matchitem_T *cur = wp->w_match_head;
6436
6437 while (cur != NULL && cur->id != id)
6438 cur = cur->next;
6439 return cur;
6440}
6441#endif