blob: a0f196bc458745cb58e4e7f8e8cf070b157ad71a [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
152#ifdef FEAT_VISUAL
153 reset_VIsual_and_resel(); /* stop Visual mode */
154#endif
155#ifdef FEAT_GUI
156 need_mouse_correct = TRUE;
157#endif
158 win_split((int)Prenum, WSP_VERT);
159 break;
160#endif
161
162/* split current window and edit alternate file */
163 case Ctrl_HAT:
164 case '^':
165 CHECK_CMDWIN
166#ifdef FEAT_VISUAL
167 reset_VIsual_and_resel(); /* stop Visual mode */
168#endif
169 STRCPY(cbuf, "split #");
170 if (Prenum)
171 sprintf((char *)cbuf + 7, "%ld", Prenum);
172 do_cmdline_cmd(cbuf);
173 break;
174
175/* open new window */
176 case Ctrl_N:
177 case 'n':
178 CHECK_CMDWIN
179#ifdef FEAT_VISUAL
180 reset_VIsual_and_resel(); /* stop Visual mode */
181#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000182#ifdef FEAT_QUICKFIX
183newwindow:
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (Prenum)
186 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
187 else
188 cbuf[0] = NUL;
189 STRCAT(cbuf, "new");
190 do_cmdline_cmd(cbuf);
191 break;
192
193/* quit current window */
194 case Ctrl_Q:
195 case 'q':
196#ifdef FEAT_VISUAL
197 reset_VIsual_and_resel(); /* stop Visual mode */
198#endif
199 do_cmdline_cmd((char_u *)"quit");
200 break;
201
202/* close current window */
203 case Ctrl_C:
204 case 'c':
205#ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207#endif
208 do_cmdline_cmd((char_u *)"close");
209 break;
210
211#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
212/* close preview window */
213 case Ctrl_Z:
214 case 'z':
215 CHECK_CMDWIN
216#ifdef FEAT_VISUAL
217 reset_VIsual_and_resel(); /* stop Visual mode */
218#endif
219 do_cmdline_cmd((char_u *)"pclose");
220 break;
221
222/* cursor to preview window */
223 case 'P':
224 for (wp = firstwin; wp != NULL; wp = wp->w_next)
225 if (wp->w_p_pvw)
226 break;
227 if (wp == NULL)
228 EMSG(_("E441: There is no preview window"));
229 else
230 win_goto(wp);
231 break;
232#endif
233
234/* close all but current window */
235 case Ctrl_O:
236 case 'o':
237 CHECK_CMDWIN
238#ifdef FEAT_VISUAL
239 reset_VIsual_and_resel(); /* stop Visual mode */
240#endif
241 do_cmdline_cmd((char_u *)"only");
242 break;
243
244/* cursor to next window with wrap around */
245 case Ctrl_W:
246 case 'w':
247/* cursor to previous window with wrap around */
248 case 'W':
249 CHECK_CMDWIN
250 if (lastwin == firstwin && Prenum != 1) /* just one window */
251 beep_flush();
252 else
253 {
254 if (Prenum) /* go to specified window */
255 {
256 for (wp = firstwin; --Prenum > 0; )
257 {
258 if (wp->w_next == NULL)
259 break;
260 else
261 wp = wp->w_next;
262 }
263 }
264 else
265 {
266 if (nchar == 'W') /* go to previous window */
267 {
268 wp = curwin->w_prev;
269 if (wp == NULL)
270 wp = lastwin; /* wrap around */
271 }
272 else /* go to next window */
273 {
274 wp = curwin->w_next;
275 if (wp == NULL)
276 wp = firstwin; /* wrap around */
277 }
278 }
279 win_goto(wp);
280 }
281 break;
282
283/* cursor to window below */
284 case 'j':
285 case K_DOWN:
286 case Ctrl_J:
287 CHECK_CMDWIN
288#ifdef FEAT_VERTSPLIT
289 win_goto_ver(FALSE, Prenum1);
290#else
291 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
292 wp = wp->w_next)
293 ;
294 win_goto(wp);
295#endif
296 break;
297
298/* cursor to window above */
299 case 'k':
300 case K_UP:
301 case Ctrl_K:
302 CHECK_CMDWIN
303#ifdef FEAT_VERTSPLIT
304 win_goto_ver(TRUE, Prenum1);
305#else
306 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
307 wp = wp->w_prev)
308 ;
309 win_goto(wp);
310#endif
311 break;
312
313#ifdef FEAT_VERTSPLIT
314/* cursor to left window */
315 case 'h':
316 case K_LEFT:
317 case Ctrl_H:
318 case K_BS:
319 CHECK_CMDWIN
320 win_goto_hor(TRUE, Prenum1);
321 break;
322
323/* cursor to right window */
324 case 'l':
325 case K_RIGHT:
326 case Ctrl_L:
327 CHECK_CMDWIN
328 win_goto_hor(FALSE, Prenum1);
329 break;
330#endif
331
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332/* move window to new tab page */
333 case 'T':
334 if (firstwin == lastwin)
335 MSG(_(m_onlyone));
336 else
337 {
338 tabpage_T *oldtab = curtab;
339 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000340
341 /* First create a new tab with the window, then go back to
342 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000343 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000344 if (win_new_tabpage((int)Prenum) == OK
345 && valid_tabpage(oldtab))
346 {
347 newtab = curtab;
348 goto_tabpage_tp(oldtab);
349 if (curwin == wp)
350 win_close(curwin, FALSE);
351 if (valid_tabpage(newtab))
352 goto_tabpage_tp(newtab);
353 }
354 }
355 break;
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357/* cursor to top-left window */
358 case 't':
359 case Ctrl_T:
360 win_goto(firstwin);
361 break;
362
363/* cursor to bottom-right window */
364 case 'b':
365 case Ctrl_B:
366 win_goto(lastwin);
367 break;
368
369/* cursor to last accessed (previous) window */
370 case 'p':
371 case Ctrl_P:
372 if (prevwin == NULL)
373 beep_flush();
374 else
375 win_goto(prevwin);
376 break;
377
378/* exchange current and next window */
379 case 'x':
380 case Ctrl_X:
381 CHECK_CMDWIN
382 win_exchange(Prenum);
383 break;
384
385/* rotate windows downwards */
386 case Ctrl_R:
387 case 'r':
388 CHECK_CMDWIN
389#ifdef FEAT_VISUAL
390 reset_VIsual_and_resel(); /* stop Visual mode */
391#endif
392 win_rotate(FALSE, (int)Prenum1); /* downwards */
393 break;
394
395/* rotate windows upwards */
396 case 'R':
397 CHECK_CMDWIN
398#ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400#endif
401 win_rotate(TRUE, (int)Prenum1); /* upwards */
402 break;
403
404/* move window to the very top/bottom/left/right */
405 case 'K':
406 case 'J':
407#ifdef FEAT_VERTSPLIT
408 case 'H':
409 case 'L':
410#endif
411 CHECK_CMDWIN
412 win_totop((int)Prenum,
413 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
414 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
415 break;
416
417/* make all windows the same height */
418 case '=':
419#ifdef FEAT_GUI
420 need_mouse_correct = TRUE;
421#endif
422 win_equal(NULL, FALSE, 'b');
423 break;
424
425/* increase current window height */
426 case '+':
427#ifdef FEAT_GUI
428 need_mouse_correct = TRUE;
429#endif
430 win_setheight(curwin->w_height + (int)Prenum1);
431 break;
432
433/* decrease current window height */
434 case '-':
435#ifdef FEAT_GUI
436 need_mouse_correct = TRUE;
437#endif
438 win_setheight(curwin->w_height - (int)Prenum1);
439 break;
440
441/* set current window height */
442 case Ctrl__:
443 case '_':
444#ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446#endif
447 win_setheight(Prenum ? (int)Prenum : 9999);
448 break;
449
450#ifdef FEAT_VERTSPLIT
451/* increase current window width */
452 case '>':
453#ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455#endif
456 win_setwidth(curwin->w_width + (int)Prenum1);
457 break;
458
459/* decrease current window width */
460 case '<':
461#ifdef FEAT_GUI
462 need_mouse_correct = TRUE;
463#endif
464 win_setwidth(curwin->w_width - (int)Prenum1);
465 break;
466
467/* set current window width */
468 case '|':
469#ifdef FEAT_GUI
470 need_mouse_correct = TRUE;
471#endif
472 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
473 break;
474#endif
475
476/* jump to tag and split window if tag exists (in preview window) */
477#if defined(FEAT_QUICKFIX)
478 case '}':
479 CHECK_CMDWIN
480 if (Prenum)
481 g_do_tagpreview = Prenum;
482 else
483 g_do_tagpreview = p_pvh;
484 /*FALLTHROUGH*/
485#endif
486 case ']':
487 case Ctrl_RSB:
488 CHECK_CMDWIN
489#ifdef FEAT_VISUAL
490 reset_VIsual_and_resel(); /* stop Visual mode */
491#endif
492 if (Prenum)
493 postponed_split = Prenum;
494 else
495 postponed_split = -1;
496
497 /* Execute the command right here, required when
498 * "wincmd ]" was used in a function. */
499 do_nv_ident(Ctrl_RSB, NUL);
500 break;
501
502#ifdef FEAT_SEARCHPATH
503/* edit file name under cursor in a new window */
504 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000505 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000507wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000509
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000510 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (ptr != NULL)
512 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000513# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000515# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 setpcmark();
517 if (win_split(0, 0) == OK)
518 {
519# ifdef FEAT_SCROLLBIND
520 curwin->w_p_scb = FALSE;
521# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000522 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
523 if (nchar == 'F' && lnum >= 0)
524 {
525 curwin->w_cursor.lnum = lnum;
526 check_cursor_lnum();
527 beginline(BL_SOL | BL_FIX);
528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530 vim_free(ptr);
531 }
532 break;
533#endif
534
535#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000536/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 * new window -- webb
538 */
539 case 'i': /* Go to any match */
540 case Ctrl_I:
541 type = FIND_ANY;
542 /* FALLTHROUGH */
543 case 'd': /* Go to definition, using 'define' */
544 case Ctrl_D:
545 CHECK_CMDWIN
546 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
547 break;
548 find_pattern_in_path(ptr, 0, len, TRUE,
549 Prenum == 0 ? TRUE : FALSE, type,
550 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
551 curwin->w_set_curswant = TRUE;
552 break;
553#endif
554
Bram Moolenaar05159a02005-02-26 23:04:13 +0000555 case K_KENTER:
556 case CAR:
557#if defined(FEAT_QUICKFIX)
558 /*
559 * In a quickfix window a <CR> jumps to the error under the
560 * cursor in a new window.
561 */
562 if (bt_quickfix(curbuf))
563 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000564 sprintf((char *)cbuf, "split +%ld%s",
565 (long)curwin->w_cursor.lnum,
566 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000567 do_cmdline_cmd(cbuf);
568 }
569#endif
570 break;
571
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573/* CTRL-W g extended commands */
574 case 'g':
575 case Ctrl_G:
576 CHECK_CMDWIN
577#ifdef USE_ON_FLY_SCROLL
578 dont_scroll = TRUE; /* disallow scrolling here */
579#endif
580 ++no_mapping;
581 ++allow_keys; /* no mapping for xchar, but allow key codes */
582 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000583 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584#ifdef FEAT_LANGMAP
585 LANGMAP_ADJUST(xchar, TRUE);
586#endif
587 --no_mapping;
588 --allow_keys;
589#ifdef FEAT_CMDL_INFO
590 (void)add_to_showcmd(xchar);
591#endif
592 switch (xchar)
593 {
594#if defined(FEAT_QUICKFIX)
595 case '}':
596 xchar = Ctrl_RSB;
597 if (Prenum)
598 g_do_tagpreview = Prenum;
599 else
600 g_do_tagpreview = p_pvh;
601 /*FALLTHROUGH*/
602#endif
603 case ']':
604 case Ctrl_RSB:
605#ifdef FEAT_VISUAL
606 reset_VIsual_and_resel(); /* stop Visual mode */
607#endif
608 if (Prenum)
609 postponed_split = Prenum;
610 else
611 postponed_split = -1;
612
613 /* Execute the command right here, required when
614 * "wincmd g}" was used in a function. */
615 do_nv_ident('g', xchar);
616 break;
617
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000618#ifdef FEAT_SEARCHPATH
619 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000620 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000621 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000622 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000623 goto wingotofile;
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 default:
626 beep_flush();
627 break;
628 }
629 break;
630
631 default: beep_flush();
632 break;
633 }
634}
635
636/*
637 * split the current window, implements CTRL-W s and :split
638 *
639 * "size" is the height or width for the new window, 0 to use half of current
640 * height or width.
641 *
642 * "flags":
643 * WSP_ROOM: require enough room for new window
644 * WSP_VERT: vertical split.
645 * WSP_TOP: open window at the top-left of the shell (help window).
646 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
647 * WSP_HELP: creating the help window, keep layout snapshot
648 *
649 * return FAIL for failure, OK otherwise
650 */
651 int
652win_split(size, flags)
653 int size;
654 int flags;
655{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000656 /* When the ":tab" modifier was used open a new tab page instead. */
657 if (may_open_tabpage() == OK)
658 return OK;
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /* Add flags from ":vertical", ":topleft" and ":botright". */
661 flags |= cmdmod.split;
662 if ((flags & WSP_TOP) && (flags & WSP_BOT))
663 {
664 EMSG(_("E442: Can't split topleft and botright at the same time"));
665 return FAIL;
666 }
667
668 /* When creating the help window make a snapshot of the window layout.
669 * Otherwise clear the snapshot, it's now invalid. */
670 if (flags & WSP_HELP)
671 make_snapshot();
672 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000673 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675 return win_split_ins(size, flags, NULL, 0);
676}
677
678/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000679 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 * When "newwin" is not NULL: insert this window at the far
681 * top/left/right/bottom.
682 * return FAIL for failure, OK otherwise
683 */
684 static int
685win_split_ins(size, flags, newwin, dir)
686 int size;
687 int flags;
688 win_T *newwin;
689 int dir;
690{
691 win_T *wp = newwin;
692 win_T *oldwin;
693 int new_size = size;
694 int i;
695 int need_status = 0;
696 int do_equal = FALSE;
697 int needed;
698 int available;
699 int oldwin_height = 0;
700 int layout;
701 frame_T *frp, *curfrp;
702 int before;
703
704 if (flags & WSP_TOP)
705 oldwin = firstwin;
706 else if (flags & WSP_BOT)
707 oldwin = lastwin;
708 else
709 oldwin = curwin;
710
711 /* add a status line when p_ls == 1 and splitting the first window */
712 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
713 {
714 if (oldwin->w_height <= p_wmh && newwin == NULL)
715 {
716 EMSG(_(e_noroom));
717 return FAIL;
718 }
719 need_status = STATUS_HEIGHT;
720 }
721
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000722#ifdef FEAT_GUI
723 /* May be needed for the scrollbars that are going to change. */
724 if (gui.in_use)
725 out_flush();
726#endif
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef FEAT_VERTSPLIT
729 if (flags & WSP_VERT)
730 {
731 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733 /*
734 * Check if we are able to split the current window and compute its
735 * width.
736 */
737 needed = p_wmw + 1;
738 if (flags & WSP_ROOM)
739 needed += p_wiw - p_wmw;
740 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
741 {
742 available = topframe->fr_width;
743 needed += frame_minwidth(topframe, NULL);
744 }
745 else
746 available = oldwin->w_width;
747 if (available < needed && newwin == NULL)
748 {
749 EMSG(_(e_noroom));
750 return FAIL;
751 }
752 if (new_size == 0)
753 new_size = oldwin->w_width / 2;
754 if (new_size > oldwin->w_width - p_wmw - 1)
755 new_size = oldwin->w_width - p_wmw - 1;
756 if (new_size < p_wmw)
757 new_size = p_wmw;
758
759 /* if it doesn't fit in the current window, need win_equal() */
760 if (oldwin->w_width - new_size - 1 < p_wmw)
761 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000762
763 /* We don't like to take lines for the new window from a
764 * 'winfixwidth' window. Take them from a window to the left or right
765 * instead, if possible. */
766 if (oldwin->w_p_wfw)
767 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000768
769 /* Only make all windows the same width if one of them (except oldwin)
770 * is wider than one of the split windows. */
771 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
772 && oldwin->w_frame->fr_parent != NULL)
773 {
774 frp = oldwin->w_frame->fr_parent->fr_child;
775 while (frp != NULL)
776 {
777 if (frp->fr_win != oldwin && frp->fr_win != NULL
778 && (frp->fr_win->w_width > new_size
779 || frp->fr_win->w_width > oldwin->w_width
780 - new_size - STATUS_HEIGHT))
781 {
782 do_equal = TRUE;
783 break;
784 }
785 frp = frp->fr_next;
786 }
787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789 else
790#endif
791 {
792 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 /*
795 * Check if we are able to split the current window and compute its
796 * height.
797 */
798 needed = p_wmh + STATUS_HEIGHT + need_status;
799 if (flags & WSP_ROOM)
800 needed += p_wh - p_wmh;
801 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
802 {
803 available = topframe->fr_height;
804 needed += frame_minheight(topframe, NULL);
805 }
806 else
807 {
808 available = oldwin->w_height;
809 needed += p_wmh;
810 }
811 if (available < needed && newwin == NULL)
812 {
813 EMSG(_(e_noroom));
814 return FAIL;
815 }
816 oldwin_height = oldwin->w_height;
817 if (need_status)
818 {
819 oldwin->w_status_height = STATUS_HEIGHT;
820 oldwin_height -= STATUS_HEIGHT;
821 }
822 if (new_size == 0)
823 new_size = oldwin_height / 2;
824
825 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
826 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
827 if (new_size < p_wmh)
828 new_size = p_wmh;
829
830 /* if it doesn't fit in the current window, need win_equal() */
831 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
832 do_equal = TRUE;
833
834 /* We don't like to take lines for the new window from a
835 * 'winfixheight' window. Take them from a window above or below
836 * instead, if possible. */
837 if (oldwin->w_p_wfh)
838 {
839 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
840 oldwin);
841 oldwin_height = oldwin->w_height;
842 if (need_status)
843 oldwin_height -= STATUS_HEIGHT;
844 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000845
846 /* Only make all windows the same height if one of them (except oldwin)
847 * is higher than one of the split windows. */
848 if (!do_equal && p_ea && size == 0
849#ifdef FEAT_VERTSPLIT
850 && *p_ead != 'h'
851#endif
852 && oldwin->w_frame->fr_parent != NULL)
853 {
854 frp = oldwin->w_frame->fr_parent->fr_child;
855 while (frp != NULL)
856 {
857 if (frp->fr_win != oldwin && frp->fr_win != NULL
858 && (frp->fr_win->w_height > new_size
859 || frp->fr_win->w_height > oldwin_height - new_size
860 - STATUS_HEIGHT))
861 {
862 do_equal = TRUE;
863 break;
864 }
865 frp = frp->fr_next;
866 }
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869
870 /*
871 * allocate new window structure and link it in the window list
872 */
873 if ((flags & WSP_TOP) == 0
874 && ((flags & WSP_BOT)
875 || (flags & WSP_BELOW)
876 || (!(flags & WSP_ABOVE)
877 && (
878#ifdef FEAT_VERTSPLIT
879 (flags & WSP_VERT) ? p_spr :
880#endif
881 p_sb))))
882 {
883 /* new window below/right of current one */
884 if (newwin == NULL)
885 wp = win_alloc(oldwin);
886 else
887 win_append(oldwin, wp);
888 }
889 else
890 {
891 if (newwin == NULL)
892 wp = win_alloc(oldwin->w_prev);
893 else
894 win_append(oldwin->w_prev, wp);
895 }
896
897 if (newwin == NULL)
898 {
899 if (wp == NULL)
900 return FAIL;
901
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000902 /* make the contents of the new window the same as the current one */
903 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905
906 /*
907 * Reorganise the tree of frames to insert the new window.
908 */
909 if (flags & (WSP_TOP | WSP_BOT))
910 {
911#ifdef FEAT_VERTSPLIT
912 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
913 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
914#else
915 if (topframe->fr_layout == FR_COL)
916#endif
917 {
918 curfrp = topframe->fr_child;
919 if (flags & WSP_BOT)
920 while (curfrp->fr_next != NULL)
921 curfrp = curfrp->fr_next;
922 }
923 else
924 curfrp = topframe;
925 before = (flags & WSP_TOP);
926 }
927 else
928 {
929 curfrp = oldwin->w_frame;
930 if (flags & WSP_BELOW)
931 before = FALSE;
932 else if (flags & WSP_ABOVE)
933 before = TRUE;
934 else
935#ifdef FEAT_VERTSPLIT
936 if (flags & WSP_VERT)
937 before = !p_spr;
938 else
939#endif
940 before = !p_sb;
941 }
942 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
943 {
944 /* Need to create a new frame in the tree to make a branch. */
945 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
946 *frp = *curfrp;
947 curfrp->fr_layout = layout;
948 frp->fr_parent = curfrp;
949 frp->fr_next = NULL;
950 frp->fr_prev = NULL;
951 curfrp->fr_child = frp;
952 curfrp->fr_win = NULL;
953 curfrp = frp;
954 if (frp->fr_win != NULL)
955 oldwin->w_frame = frp;
956 else
957 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
958 frp->fr_parent = curfrp;
959 }
960
961 if (newwin == NULL)
962 {
963 /* Create a frame for the new window. */
964 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
965 frp->fr_layout = FR_LEAF;
966 frp->fr_win = wp;
967 wp->w_frame = frp;
968 }
969 else
970 frp = newwin->w_frame;
971 frp->fr_parent = curfrp->fr_parent;
972
973 /* Insert the new frame at the right place in the frame list. */
974 if (before)
975 frame_insert(curfrp, frp);
976 else
977 frame_append(curfrp, frp);
978
979#ifdef FEAT_VERTSPLIT
980 if (flags & WSP_VERT)
981 {
982 wp->w_p_scr = curwin->w_p_scr;
983 if (need_status)
984 {
985 --oldwin->w_height;
986 oldwin->w_status_height = need_status;
987 }
988 if (flags & (WSP_TOP | WSP_BOT))
989 {
990 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000991 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 wp->w_height = curfrp->fr_height - (p_ls > 0);
993 wp->w_status_height = (p_ls > 0);
994 }
995 else
996 {
997 /* height and row of new window is same as current window */
998 wp->w_winrow = oldwin->w_winrow;
999 wp->w_height = oldwin->w_height;
1000 wp->w_status_height = oldwin->w_status_height;
1001 }
1002 frp->fr_height = curfrp->fr_height;
1003
1004 /* "new_size" of the current window goes to the new window, use
1005 * one column for the vertical separator */
1006 wp->w_width = new_size;
1007 if (before)
1008 wp->w_vsep_width = 1;
1009 else
1010 {
1011 wp->w_vsep_width = oldwin->w_vsep_width;
1012 oldwin->w_vsep_width = 1;
1013 }
1014 if (flags & (WSP_TOP | WSP_BOT))
1015 {
1016 if (flags & WSP_BOT)
1017 frame_add_vsep(curfrp);
1018 /* Set width of neighbor frame */
1019 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001020 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1021 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001024 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (before) /* new window left of current one */
1026 {
1027 wp->w_wincol = oldwin->w_wincol;
1028 oldwin->w_wincol += new_size + 1;
1029 }
1030 else /* new window right of current one */
1031 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1032 frame_fix_width(oldwin);
1033 frame_fix_width(wp);
1034 }
1035 else
1036#endif
1037 {
1038 /* width and column of new window is same as current window */
1039#ifdef FEAT_VERTSPLIT
1040 if (flags & (WSP_TOP | WSP_BOT))
1041 {
1042 wp->w_wincol = 0;
1043 wp->w_width = Columns;
1044 wp->w_vsep_width = 0;
1045 }
1046 else
1047 {
1048 wp->w_wincol = oldwin->w_wincol;
1049 wp->w_width = oldwin->w_width;
1050 wp->w_vsep_width = oldwin->w_vsep_width;
1051 }
1052 frp->fr_width = curfrp->fr_width;
1053#endif
1054
1055 /* "new_size" of the current window goes to the new window, use
1056 * one row for the status line */
1057 win_new_height(wp, new_size);
1058 if (flags & (WSP_TOP | WSP_BOT))
1059 frame_new_height(curfrp, curfrp->fr_height
1060 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1061 else
1062 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1063 if (before) /* new window above current one */
1064 {
1065 wp->w_winrow = oldwin->w_winrow;
1066 wp->w_status_height = STATUS_HEIGHT;
1067 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1068 }
1069 else /* new window below current one */
1070 {
1071 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1072 wp->w_status_height = oldwin->w_status_height;
1073 oldwin->w_status_height = STATUS_HEIGHT;
1074 }
1075#ifdef FEAT_VERTSPLIT
1076 if (flags & WSP_BOT)
1077 frame_add_statusline(curfrp);
1078#endif
1079 frame_fix_height(wp);
1080 frame_fix_height(oldwin);
1081 }
1082
1083 if (flags & (WSP_TOP | WSP_BOT))
1084 (void)win_comp_pos();
1085
1086 /*
1087 * Both windows need redrawing
1088 */
1089 redraw_win_later(wp, NOT_VALID);
1090 wp->w_redr_status = TRUE;
1091 redraw_win_later(oldwin, NOT_VALID);
1092 oldwin->w_redr_status = TRUE;
1093
1094 if (need_status)
1095 {
1096 msg_row = Rows - 1;
1097 msg_col = sc_col;
1098 msg_clr_eos_force(); /* Old command/ruler may still be there */
1099 comp_col();
1100 msg_row = Rows - 1;
1101 msg_col = 0; /* put position back at start of line */
1102 }
1103
1104 /*
1105 * make the new window the current window and redraw
1106 */
1107 if (do_equal || dir != 0)
1108 win_equal(wp, TRUE,
1109#ifdef FEAT_VERTSPLIT
1110 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1111 : dir == 'h' ? 'b' :
1112#endif
1113 'v');
1114
1115 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1116 * size was given. */
1117#ifdef FEAT_VERTSPLIT
1118 if (flags & WSP_VERT)
1119 {
1120 i = p_wiw;
1121 if (size != 0)
1122 p_wiw = size;
1123
1124# ifdef FEAT_GUI
1125 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1126 if (gui.in_use)
1127 gui_init_which_components(NULL);
1128# endif
1129 }
1130 else
1131#endif
1132 {
1133 i = p_wh;
1134 if (size != 0)
1135 p_wh = size;
1136 }
1137 win_enter(wp, FALSE);
1138#ifdef FEAT_VERTSPLIT
1139 if (flags & WSP_VERT)
1140 p_wiw = i;
1141 else
1142#endif
1143 p_wh = i;
1144
1145 return OK;
1146}
1147
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001148/*
1149 * Initialize window "newp" from window "oldp".
1150 * Used when splitting a window and when creating a new tab page.
1151 * The windows will both edit the same buffer.
1152 */
1153 static void
1154win_init(newp, oldp)
1155 win_T *newp;
1156 win_T *oldp;
1157{
1158 int i;
1159
1160 newp->w_buffer = oldp->w_buffer;
1161 oldp->w_buffer->b_nwindows++;
1162 newp->w_cursor = oldp->w_cursor;
1163 newp->w_valid = 0;
1164 newp->w_curswant = oldp->w_curswant;
1165 newp->w_set_curswant = oldp->w_set_curswant;
1166 newp->w_topline = oldp->w_topline;
1167#ifdef FEAT_DIFF
1168 newp->w_topfill = oldp->w_topfill;
1169#endif
1170 newp->w_leftcol = oldp->w_leftcol;
1171 newp->w_pcmark = oldp->w_pcmark;
1172 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1173 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001174 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001175 newp->w_fraction = oldp->w_fraction;
1176 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1177#ifdef FEAT_JUMPLIST
1178 copy_jumplist(oldp, newp);
1179#endif
1180#ifdef FEAT_QUICKFIX
1181 copy_loclist(oldp, newp);
1182#endif
1183 if (oldp->w_localdir != NULL)
1184 newp->w_localdir = vim_strsave(oldp->w_localdir);
1185
1186 /* Use the same argument list. */
1187 newp->w_alist = oldp->w_alist;
1188 ++newp->w_alist->al_refcount;
1189 newp->w_arg_idx = oldp->w_arg_idx;
1190
1191 /*
1192 * copy tagstack and options from existing window
1193 */
1194 for (i = 0; i < oldp->w_tagstacklen; i++)
1195 {
1196 newp->w_tagstack[i] = oldp->w_tagstack[i];
1197 if (newp->w_tagstack[i].tagname != NULL)
1198 newp->w_tagstack[i].tagname =
1199 vim_strsave(newp->w_tagstack[i].tagname);
1200 }
1201 newp->w_tagstackidx = oldp->w_tagstackidx;
1202 newp->w_tagstacklen = oldp->w_tagstacklen;
1203 win_copy_options(oldp, newp);
1204# ifdef FEAT_FOLDING
1205 copyFoldingState(oldp, newp);
1206# endif
1207}
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209#endif /* FEAT_WINDOWS */
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211#if defined(FEAT_WINDOWS) || defined(PROTO)
1212/*
1213 * Check if "win" is a pointer to an existing window.
1214 */
1215 int
1216win_valid(win)
1217 win_T *win;
1218{
1219 win_T *wp;
1220
1221 if (win == NULL)
1222 return FALSE;
1223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1224 if (wp == win)
1225 return TRUE;
1226 return FALSE;
1227}
1228
1229/*
1230 * Return the number of windows.
1231 */
1232 int
1233win_count()
1234{
1235 win_T *wp;
1236 int count = 0;
1237
1238 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1239 ++count;
1240 return count;
1241}
1242
1243/*
1244 * Make "count" windows on the screen.
1245 * Return actual number of windows on the screen.
1246 * Must be called when there is just one window, filling the whole screen
1247 * (excluding the command line).
1248 */
1249/*ARGSUSED*/
1250 int
1251make_windows(count, vertical)
1252 int count;
1253 int vertical; /* split windows vertically if TRUE */
1254{
1255 int maxcount;
1256 int todo;
1257
1258#ifdef FEAT_VERTSPLIT
1259 if (vertical)
1260 {
1261 /* Each windows needs at least 'winminwidth' lines and a separator
1262 * column. */
1263 maxcount = (curwin->w_width + curwin->w_vsep_width
1264 - (p_wiw - p_wmw)) / (p_wmw + 1);
1265 }
1266 else
1267#endif
1268 {
1269 /* Each window needs at least 'winminheight' lines and a status line. */
1270 maxcount = (curwin->w_height + curwin->w_status_height
1271 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1272 }
1273
1274 if (maxcount < 2)
1275 maxcount = 2;
1276 if (count > maxcount)
1277 count = maxcount;
1278
1279 /*
1280 * add status line now, otherwise first window will be too big
1281 */
1282 if (count > 1)
1283 last_status(TRUE);
1284
1285#ifdef FEAT_AUTOCMD
1286 /*
1287 * Don't execute autocommands while creating the windows. Must do that
1288 * when putting the buffers in the windows.
1289 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001290 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#endif
1292
1293 /* todo is number of windows left to create */
1294 for (todo = count - 1; todo > 0; --todo)
1295#ifdef FEAT_VERTSPLIT
1296 if (vertical)
1297 {
1298 if (win_split(curwin->w_width - (curwin->w_width - todo)
1299 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1300 break;
1301 }
1302 else
1303#endif
1304 {
1305 if (win_split(curwin->w_height - (curwin->w_height - todo
1306 * STATUS_HEIGHT) / (todo + 1)
1307 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1308 break;
1309 }
1310
1311#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001312 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#endif
1314
1315 /* return actual number of windows */
1316 return (count - todo);
1317}
1318
1319/*
1320 * Exchange current and next window
1321 */
1322 static void
1323win_exchange(Prenum)
1324 long Prenum;
1325{
1326 frame_T *frp;
1327 frame_T *frp2;
1328 win_T *wp;
1329 win_T *wp2;
1330 int temp;
1331
1332 if (lastwin == firstwin) /* just one window */
1333 {
1334 beep_flush();
1335 return;
1336 }
1337
1338#ifdef FEAT_GUI
1339 need_mouse_correct = TRUE;
1340#endif
1341
1342 /*
1343 * find window to exchange with
1344 */
1345 if (Prenum)
1346 {
1347 frp = curwin->w_frame->fr_parent->fr_child;
1348 while (frp != NULL && --Prenum > 0)
1349 frp = frp->fr_next;
1350 }
1351 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1352 frp = curwin->w_frame->fr_next;
1353 else /* Swap last window in row/col with previous */
1354 frp = curwin->w_frame->fr_prev;
1355
1356 /* We can only exchange a window with another window, not with a frame
1357 * containing windows. */
1358 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1359 return;
1360 wp = frp->fr_win;
1361
1362/*
1363 * 1. remove curwin from the list. Remember after which window it was in wp2
1364 * 2. insert curwin before wp in the list
1365 * if wp != wp2
1366 * 3. remove wp from the list
1367 * 4. insert wp after wp2
1368 * 5. exchange the status line height and vsep width.
1369 */
1370 wp2 = curwin->w_prev;
1371 frp2 = curwin->w_frame->fr_prev;
1372 if (wp->w_prev != curwin)
1373 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001374 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 frame_remove(curwin->w_frame);
1376 win_append(wp->w_prev, curwin);
1377 frame_insert(frp, curwin->w_frame);
1378 }
1379 if (wp != wp2)
1380 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001381 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 frame_remove(wp->w_frame);
1383 win_append(wp2, wp);
1384 if (frp2 == NULL)
1385 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1386 else
1387 frame_append(frp2, wp->w_frame);
1388 }
1389 temp = curwin->w_status_height;
1390 curwin->w_status_height = wp->w_status_height;
1391 wp->w_status_height = temp;
1392#ifdef FEAT_VERTSPLIT
1393 temp = curwin->w_vsep_width;
1394 curwin->w_vsep_width = wp->w_vsep_width;
1395 wp->w_vsep_width = temp;
1396
1397 /* If the windows are not in the same frame, exchange the sizes to avoid
1398 * messing up the window layout. Otherwise fix the frame sizes. */
1399 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1400 {
1401 temp = curwin->w_height;
1402 curwin->w_height = wp->w_height;
1403 wp->w_height = temp;
1404 temp = curwin->w_width;
1405 curwin->w_width = wp->w_width;
1406 wp->w_width = temp;
1407 }
1408 else
1409 {
1410 frame_fix_height(curwin);
1411 frame_fix_height(wp);
1412 frame_fix_width(curwin);
1413 frame_fix_width(wp);
1414 }
1415#endif
1416
1417 (void)win_comp_pos(); /* recompute window positions */
1418
1419 win_enter(wp, TRUE);
1420 redraw_later(CLEAR);
1421}
1422
1423/*
1424 * rotate windows: if upwards TRUE the second window becomes the first one
1425 * if upwards FALSE the first window becomes the second one
1426 */
1427 static void
1428win_rotate(upwards, count)
1429 int upwards;
1430 int count;
1431{
1432 win_T *wp1;
1433 win_T *wp2;
1434 frame_T *frp;
1435 int n;
1436
1437 if (firstwin == lastwin) /* nothing to do */
1438 {
1439 beep_flush();
1440 return;
1441 }
1442
1443#ifdef FEAT_GUI
1444 need_mouse_correct = TRUE;
1445#endif
1446
1447#ifdef FEAT_VERTSPLIT
1448 /* Check if all frames in this row/col have one window. */
1449 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1450 frp = frp->fr_next)
1451 if (frp->fr_win == NULL)
1452 {
1453 EMSG(_("E443: Cannot rotate when another window is split"));
1454 return;
1455 }
1456#endif
1457
1458 while (count--)
1459 {
1460 if (upwards) /* first window becomes last window */
1461 {
1462 /* remove first window/frame from the list */
1463 frp = curwin->w_frame->fr_parent->fr_child;
1464 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001465 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 frame_remove(frp);
1467
1468 /* find last frame and append removed window/frame after it */
1469 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1470 ;
1471 win_append(frp->fr_win, wp1);
1472 frame_append(frp, wp1->w_frame);
1473
1474 wp2 = frp->fr_win; /* previously last window */
1475 }
1476 else /* last window becomes first window */
1477 {
1478 /* find last window/frame in the list and remove it */
1479 for (frp = curwin->w_frame; frp->fr_next != NULL;
1480 frp = frp->fr_next)
1481 ;
1482 wp1 = frp->fr_win;
1483 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001484 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 frame_remove(frp);
1486
1487 /* append the removed window/frame before the first in the list */
1488 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1489 frame_insert(frp->fr_parent->fr_child, frp);
1490 }
1491
1492 /* exchange status height and vsep width of old and new last window */
1493 n = wp2->w_status_height;
1494 wp2->w_status_height = wp1->w_status_height;
1495 wp1->w_status_height = n;
1496 frame_fix_height(wp1);
1497 frame_fix_height(wp2);
1498#ifdef FEAT_VERTSPLIT
1499 n = wp2->w_vsep_width;
1500 wp2->w_vsep_width = wp1->w_vsep_width;
1501 wp1->w_vsep_width = n;
1502 frame_fix_width(wp1);
1503 frame_fix_width(wp2);
1504#endif
1505
1506 /* recompute w_winrow and w_wincol for all windows */
1507 (void)win_comp_pos();
1508 }
1509
1510 redraw_later(CLEAR);
1511}
1512
1513/*
1514 * Move the current window to the very top/bottom/left/right of the screen.
1515 */
1516 static void
1517win_totop(size, flags)
1518 int size;
1519 int flags;
1520{
1521 int dir;
1522 int height = curwin->w_height;
1523
1524 if (lastwin == firstwin)
1525 {
1526 beep_flush();
1527 return;
1528 }
1529
1530 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001531 (void)winframe_remove(curwin, &dir, NULL);
1532 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 last_status(FALSE); /* may need to remove last status line */
1534 (void)win_comp_pos(); /* recompute window positions */
1535
1536 /* Split a window on the desired side and put the window there. */
1537 (void)win_split_ins(size, flags, curwin, dir);
1538 if (!(flags & WSP_VERT))
1539 {
1540 win_setheight(height);
1541 if (p_ea)
1542 win_equal(curwin, TRUE, 'v');
1543 }
1544
1545#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1546 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1547 * scrollbars. Have to update them anyway. */
1548 if (gui.in_use)
1549 {
1550 out_flush();
1551 gui_init_which_components(NULL);
1552 gui_update_scrollbars(TRUE);
1553 }
1554 need_mouse_correct = TRUE;
1555#endif
1556
1557}
1558
1559/*
1560 * Move window "win1" to below/right of "win2" and make "win1" the current
1561 * window. Only works within the same frame!
1562 */
1563 void
1564win_move_after(win1, win2)
1565 win_T *win1, *win2;
1566{
1567 int height;
1568
1569 /* check if the arguments are reasonable */
1570 if (win1 == win2)
1571 return;
1572
1573 /* check if there is something to do */
1574 if (win2->w_next != win1)
1575 {
1576 /* may need move the status line/vertical separator of the last window
1577 * */
1578 if (win1 == lastwin)
1579 {
1580 height = win1->w_prev->w_status_height;
1581 win1->w_prev->w_status_height = win1->w_status_height;
1582 win1->w_status_height = height;
1583#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001584 if (win1->w_prev->w_vsep_width == 1)
1585 {
1586 /* Remove the vertical separator from the last-but-one window,
1587 * add it to the last window. Adjust the frame widths. */
1588 win1->w_prev->w_vsep_width = 0;
1589 win1->w_prev->w_frame->fr_width -= 1;
1590 win1->w_vsep_width = 1;
1591 win1->w_frame->fr_width += 1;
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594 }
1595 else if (win2 == lastwin)
1596 {
1597 height = win1->w_status_height;
1598 win1->w_status_height = win2->w_status_height;
1599 win2->w_status_height = height;
1600#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001601 if (win1->w_vsep_width == 1)
1602 {
1603 /* Remove the vertical separator from win1, add it to the last
1604 * window, win2. Adjust the frame widths. */
1605 win2->w_vsep_width = 1;
1606 win2->w_frame->fr_width += 1;
1607 win1->w_vsep_width = 0;
1608 win1->w_frame->fr_width -= 1;
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610#endif
1611 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001612 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 frame_remove(win1->w_frame);
1614 win_append(win2, win1);
1615 frame_append(win2->w_frame, win1->w_frame);
1616
1617 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1618 redraw_later(NOT_VALID);
1619 }
1620 win_enter(win1, FALSE);
1621}
1622
1623/*
1624 * Make all windows the same height.
1625 * 'next_curwin' will soon be the current window, make sure it has enough
1626 * rows.
1627 */
1628 void
1629win_equal(next_curwin, current, dir)
1630 win_T *next_curwin; /* pointer to current window to be or NULL */
1631 int current; /* do only frame with current window */
1632 int dir; /* 'v' for vertically, 'h' for horizontally,
1633 'b' for both, 0 for using p_ead */
1634{
1635 if (dir == 0)
1636#ifdef FEAT_VERTSPLIT
1637 dir = *p_ead;
1638#else
1639 dir = 'b';
1640#endif
1641 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001642 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001643 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644}
1645
1646/*
1647 * Set a frame to a new position and height, spreading the available room
1648 * equally over contained frames.
1649 * The window "next_curwin" (if not NULL) should at least get the size from
1650 * 'winheight' and 'winwidth' if possible.
1651 */
1652 static void
1653win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1654 win_T *next_curwin; /* pointer to current window to be or NULL */
1655 int current; /* do only frame with current window */
1656 frame_T *topfr; /* frame to set size off */
1657 int dir; /* 'v', 'h' or 'b', see win_equal() */
1658 int col; /* horizontal position for frame */
1659 int row; /* vertical position for frame */
1660 int width; /* new width of frame */
1661 int height; /* new height of frame */
1662{
1663 int n, m;
1664 int extra_sep = 0;
1665 int wincount, totwincount = 0;
1666 frame_T *fr;
1667 int next_curwin_size = 0;
1668 int room = 0;
1669 int new_size;
1670 int has_next_curwin = 0;
1671 int hnc;
1672
1673 if (topfr->fr_layout == FR_LEAF)
1674 {
1675 /* Set the width/height of this frame.
1676 * Redraw when size or position changes */
1677 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1678#ifdef FEAT_VERTSPLIT
1679 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1680#endif
1681 )
1682 {
1683 topfr->fr_win->w_winrow = row;
1684 frame_new_height(topfr, height, FALSE, FALSE);
1685#ifdef FEAT_VERTSPLIT
1686 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001687 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#endif
1689 redraw_all_later(CLEAR);
1690 }
1691 }
1692#ifdef FEAT_VERTSPLIT
1693 else if (topfr->fr_layout == FR_ROW)
1694 {
1695 topfr->fr_width = width;
1696 topfr->fr_height = height;
1697
1698 if (dir != 'v') /* equalize frame widths */
1699 {
1700 /* Compute the maximum number of windows horizontally in this
1701 * frame. */
1702 n = frame_minwidth(topfr, NOWIN);
1703 /* add one for the rightmost window, it doesn't have a separator */
1704 if (col + width == Columns)
1705 extra_sep = 1;
1706 else
1707 extra_sep = 0;
1708 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001709 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001711 /*
1712 * Compute width for "next_curwin" window and room available for
1713 * other windows.
1714 * "m" is the minimal width when counting p_wiw for "next_curwin".
1715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 m = frame_minwidth(topfr, next_curwin);
1717 room = width - m;
1718 if (room < 0)
1719 {
1720 next_curwin_size = p_wiw + room;
1721 room = 0;
1722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 else
1724 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001725 next_curwin_size = -1;
1726 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1727 {
1728 /* If 'winfixwidth' set keep the window width if
1729 * possible.
1730 * Watch out for this window being the next_curwin. */
1731 if (frame_fixed_width(fr))
1732 {
1733 n = frame_minwidth(fr, NOWIN);
1734 new_size = fr->fr_width;
1735 if (frame_has_win(fr, next_curwin))
1736 {
1737 room += p_wiw - p_wmw;
1738 next_curwin_size = 0;
1739 if (new_size < p_wiw)
1740 new_size = p_wiw;
1741 }
1742 else
1743 /* These windows don't use up room. */
1744 totwincount -= (n + (fr->fr_next == NULL
1745 ? extra_sep : 0)) / (p_wmw + 1);
1746 room -= new_size - n;
1747 if (room < 0)
1748 {
1749 new_size += room;
1750 room = 0;
1751 }
1752 fr->fr_newwidth = new_size;
1753 }
1754 }
1755 if (next_curwin_size == -1)
1756 {
1757 if (!has_next_curwin)
1758 next_curwin_size = 0;
1759 else if (totwincount > 1
1760 && (room + (totwincount - 2))
1761 / (totwincount - 1) > p_wiw)
1762 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001763 /* Can make all windows wider than 'winwidth', spread
1764 * the room equally. */
1765 next_curwin_size = (room + p_wiw
1766 + (totwincount - 1) * p_wmw
1767 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001768 room -= next_curwin_size - p_wiw;
1769 }
1770 else
1771 next_curwin_size = p_wiw;
1772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774
1775 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 --totwincount; /* don't count curwin */
1777 }
1778
1779 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1780 {
1781 n = m = 0;
1782 wincount = 1;
1783 if (fr->fr_next == NULL)
1784 /* last frame gets all that remains (avoid roundoff error) */
1785 new_size = width;
1786 else if (dir == 'v')
1787 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001788 else if (frame_fixed_width(fr))
1789 {
1790 new_size = fr->fr_newwidth;
1791 wincount = 0; /* doesn't count as a sizeable window */
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 else
1794 {
1795 /* Compute the maximum number of windows horiz. in "fr". */
1796 n = frame_minwidth(fr, NOWIN);
1797 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1798 / (p_wmw + 1);
1799 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001800 if (has_next_curwin)
1801 hnc = frame_has_win(fr, next_curwin);
1802 else
1803 hnc = FALSE;
1804 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001806 if (totwincount == 0)
1807 new_size = room;
1808 else
1809 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001811 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 next_curwin_size -= p_wiw - (m - n);
1814 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001815 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001817 else
1818 room -= new_size;
1819 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 * window, unless equalizing all frames. */
1824 if (!current || dir != 'v' || topfr->fr_parent != NULL
1825 || (new_size != fr->fr_width)
1826 || frame_has_win(fr, next_curwin))
1827 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 new_size, height);
1829 col += new_size;
1830 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 totwincount -= wincount;
1832 }
1833 }
1834#endif
1835 else /* topfr->fr_layout == FR_COL */
1836 {
1837#ifdef FEAT_VERTSPLIT
1838 topfr->fr_width = width;
1839#endif
1840 topfr->fr_height = height;
1841
1842 if (dir != 'h') /* equalize frame heights */
1843 {
1844 /* Compute maximum number of windows vertically in this frame. */
1845 n = frame_minheight(topfr, NOWIN);
1846 /* add one for the bottom window if it doesn't have a statusline */
1847 if (row + height == cmdline_row && p_ls == 0)
1848 extra_sep = 1;
1849 else
1850 extra_sep = 0;
1851 totwincount = (n + extra_sep) / (p_wmh + 1);
1852 has_next_curwin = frame_has_win(topfr, next_curwin);
1853
1854 /*
1855 * Compute height for "next_curwin" window and room available for
1856 * other windows.
1857 * "m" is the minimal height when counting p_wh for "next_curwin".
1858 */
1859 m = frame_minheight(topfr, next_curwin);
1860 room = height - m;
1861 if (room < 0)
1862 {
1863 /* The room is less then 'winheight', use all space for the
1864 * current window. */
1865 next_curwin_size = p_wh + room;
1866 room = 0;
1867 }
1868 else
1869 {
1870 next_curwin_size = -1;
1871 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1872 {
1873 /* If 'winfixheight' set keep the window height if
1874 * possible.
1875 * Watch out for this window being the next_curwin. */
1876 if (frame_fixed_height(fr))
1877 {
1878 n = frame_minheight(fr, NOWIN);
1879 new_size = fr->fr_height;
1880 if (frame_has_win(fr, next_curwin))
1881 {
1882 room += p_wh - p_wmh;
1883 next_curwin_size = 0;
1884 if (new_size < p_wh)
1885 new_size = p_wh;
1886 }
1887 else
1888 /* These windows don't use up room. */
1889 totwincount -= (n + (fr->fr_next == NULL
1890 ? extra_sep : 0)) / (p_wmh + 1);
1891 room -= new_size - n;
1892 if (room < 0)
1893 {
1894 new_size += room;
1895 room = 0;
1896 }
1897 fr->fr_newheight = new_size;
1898 }
1899 }
1900 if (next_curwin_size == -1)
1901 {
1902 if (!has_next_curwin)
1903 next_curwin_size = 0;
1904 else if (totwincount > 1
1905 && (room + (totwincount - 2))
1906 / (totwincount - 1) > p_wh)
1907 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001908 /* can make all windows higher than 'winheight',
1909 * spread the room equally. */
1910 next_curwin_size = (room + p_wh
1911 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 + (totwincount - 1)) / totwincount;
1913 room -= next_curwin_size - p_wh;
1914 }
1915 else
1916 next_curwin_size = p_wh;
1917 }
1918 }
1919
1920 if (has_next_curwin)
1921 --totwincount; /* don't count curwin */
1922 }
1923
1924 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1925 {
1926 n = m = 0;
1927 wincount = 1;
1928 if (fr->fr_next == NULL)
1929 /* last frame gets all that remains (avoid roundoff error) */
1930 new_size = height;
1931 else if (dir == 'h')
1932 new_size = fr->fr_height;
1933 else if (frame_fixed_height(fr))
1934 {
1935 new_size = fr->fr_newheight;
1936 wincount = 0; /* doesn't count as a sizeable window */
1937 }
1938 else
1939 {
1940 /* Compute the maximum number of windows vert. in "fr". */
1941 n = frame_minheight(fr, NOWIN);
1942 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1943 / (p_wmh + 1);
1944 m = frame_minheight(fr, next_curwin);
1945 if (has_next_curwin)
1946 hnc = frame_has_win(fr, next_curwin);
1947 else
1948 hnc = FALSE;
1949 if (hnc) /* don't count next_curwin */
1950 --wincount;
1951 if (totwincount == 0)
1952 new_size = room;
1953 else
1954 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1955 / totwincount;
1956 if (hnc) /* add next_curwin size */
1957 {
1958 next_curwin_size -= p_wh - (m - n);
1959 new_size += next_curwin_size;
1960 room -= new_size - next_curwin_size;
1961 }
1962 else
1963 room -= new_size;
1964 new_size += n;
1965 }
1966 /* Skip frame that is full width when splitting or closing a
1967 * window, unless equalizing all frames. */
1968 if (!current || dir != 'h' || topfr->fr_parent != NULL
1969 || (new_size != fr->fr_height)
1970 || frame_has_win(fr, next_curwin))
1971 win_equal_rec(next_curwin, current, fr, dir, col, row,
1972 width, new_size);
1973 row += new_size;
1974 height -= new_size;
1975 totwincount -= wincount;
1976 }
1977 }
1978}
1979
1980/*
1981 * close all windows for buffer 'buf'
1982 */
1983 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001984close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001986 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001988 win_T *wp;
1989 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001990 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001993
1994 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001996 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001998 win_close(wp, FALSE);
1999
2000 /* Start all over, autocommands may change the window layout. */
2001 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002004 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002006
2007 /* Also check windows in other tab pages. */
2008 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2009 {
2010 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002011 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002012 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2013 if (wp->w_buffer == buf)
2014 {
2015 win_close_othertab(wp, FALSE, tp);
2016
2017 /* Start all over, the tab page may be closed and
2018 * autocommands may change the window layout. */
2019 nexttp = first_tabpage;
2020 break;
2021 }
2022 }
2023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002026 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028}
2029
2030/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002031 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002032 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002033 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002034 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002035last_window()
2036{
2037 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2038}
2039
2040/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 * If "free_buf" is TRUE related buffer may be unloaded.
2043 *
2044 * called by :quit, :close, :xit, :wq and findtag()
2045 */
2046 void
2047win_close(win, free_buf)
2048 win_T *win;
2049 int free_buf;
2050{
2051 win_T *wp;
2052#ifdef FEAT_AUTOCMD
2053 int other_buffer = FALSE;
2054#endif
2055 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 int dir;
2057 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002058 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002060 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 EMSG(_("E444: Cannot close last window"));
2063 return;
2064 }
2065
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002066 /*
2067 * When closing the last window in a tab page first go to another tab
2068 * page and then close the window and the tab page. This avoids that
2069 * curwin and curtab are not invalid while we are freeing memory, they may
2070 * be used in GUI events.
2071 */
2072 if (firstwin == lastwin)
2073 {
2074 goto_tabpage_tp(alt_tabpage());
2075 redraw_tabline = TRUE;
2076
2077 /* Safety check: Autocommands may have closed the window when jumping
2078 * to the other tab page. */
2079 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2080 {
2081 int h = tabline_height();
2082
2083 win_close_othertab(win, free_buf, prev_curtab);
2084 if (h != tabline_height())
2085 shell_new_rows();
2086 }
2087 return;
2088 }
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 /* When closing the help window, try restoring a snapshot after closing
2091 * the window. Otherwise clear the snapshot, it's now invalid. */
2092 if (win->w_buffer->b_help)
2093 help_window = TRUE;
2094 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002095 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097#ifdef FEAT_AUTOCMD
2098 if (win == curwin)
2099 {
2100 /*
2101 * Guess which window is going to be the new current window.
2102 * This may change because of the autocommands (sigh).
2103 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002104 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 /*
2107 * Be careful: If autocommands delete the window, return now.
2108 */
2109 if (wp->w_buffer != curbuf)
2110 {
2111 other_buffer = TRUE;
2112 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002113 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 return;
2115 }
2116 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002117 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 return;
2119# ifdef FEAT_EVAL
2120 /* autocmds may abort script processing */
2121 if (aborting())
2122 return;
2123# endif
2124 }
2125#endif
2126
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002127#ifdef FEAT_GUI
2128 /* Avoid trouble with scrollbars that are going to be deleted in
2129 * win_free(). */
2130 if (gui.in_use)
2131 out_flush();
2132#endif
2133
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 /*
2135 * Close the link to the buffer.
2136 */
2137 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002138
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002140 * other window or moved to another tab page. */
2141 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 return;
2143
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002144 /* Free the memory used for the window. */
2145 wp = win_free_mem(win, &dir, NULL);
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 /* Make sure curwin isn't invalid. It can cause severe trouble when
2148 * printing an error message. For win_equal() curbuf needs to be valid
2149 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002150 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 curwin = wp;
2153#ifdef FEAT_QUICKFIX
2154 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2155 {
2156 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002157 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 * finding another window to go to.
2159 */
2160 for (;;)
2161 {
2162 if (wp->w_next == NULL)
2163 wp = firstwin;
2164 else
2165 wp = wp->w_next;
2166 if (wp == curwin)
2167 break;
2168 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2169 {
2170 curwin = wp;
2171 break;
2172 }
2173 }
2174 }
2175#endif
2176 curbuf = curwin->w_buffer;
2177 close_curwin = TRUE;
2178 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002179 if (p_ea
2180#ifdef FEAT_VERTSPLIT
2181 && (*p_ead == 'b' || *p_ead == dir)
2182#endif
2183 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 win_equal(curwin, TRUE,
2185#ifdef FEAT_VERTSPLIT
2186 dir
2187#else
2188 0
2189#endif
2190 );
2191 else
2192 win_comp_pos();
2193 if (close_curwin)
2194 {
2195 win_enter_ext(wp, FALSE, TRUE);
2196#ifdef FEAT_AUTOCMD
2197 if (other_buffer)
2198 /* careful: after this wp and win may be invalid! */
2199 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2200#endif
2201 }
2202
2203 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002204 * If last window has a status line now and we don't want one,
2205 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
2207 last_status(FALSE);
2208
2209 /* After closing the help window, try restoring the window layout from
2210 * before it was opened. */
2211 if (help_window)
2212 restore_snapshot(close_curwin);
2213
2214#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2215 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2216 if (gui.in_use && !win_hasvertsplit())
2217 gui_init_which_components(NULL);
2218#endif
2219
2220 redraw_all_later(NOT_VALID);
2221}
2222
2223/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224 * Close window "win" in tab page "tp", which is not the current tab page.
2225 * This may be the last window ih that tab page and result in closing the tab,
2226 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002227 * Caller must check if buffer is hidden and whether the tabline needs to be
2228 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002229 */
2230 void
2231win_close_othertab(win, free_buf, tp)
2232 win_T *win;
2233 int free_buf;
2234 tabpage_T *tp;
2235{
2236 win_T *wp;
2237 int dir;
2238 tabpage_T *ptp = NULL;
2239
2240 /* Close the link to the buffer. */
2241 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2242
2243 /* Careful: Autocommands may have closed the tab page or made it the
2244 * current tab page. */
2245 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2246 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002247 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002248 return;
2249
2250 /* Autocommands may have closed the window already. */
2251 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2252 ;
2253 if (wp == NULL)
2254 return;
2255
2256 /* Free the memory used for the window. */
2257 wp = win_free_mem(win, &dir, tp);
2258
2259 /* When closing the last window in a tab page remove the tab page. */
2260 if (wp == NULL)
2261 {
2262 if (tp == first_tabpage)
2263 first_tabpage = tp->tp_next;
2264 else
2265 {
2266 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2267 ptp = ptp->tp_next)
2268 ;
2269 if (ptp == NULL)
2270 {
2271 EMSG2(_(e_intern2), "win_close_othertab()");
2272 return;
2273 }
2274 ptp->tp_next = tp->tp_next;
2275 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002276 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002277 }
2278}
2279
2280/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002281 * Free the memory used for a window.
2282 * Returns a pointer to the window that got the freed up space.
2283 */
2284 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002285win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002286 win_T *win;
2287 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002288 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002289{
2290 frame_T *frp;
2291 win_T *wp;
2292
Bram Moolenaarea408852005-06-25 22:49:46 +00002293#ifdef FEAT_FOLDING
2294 clearFolding(win);
2295#endif
2296
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002297 /* reduce the reference count to the argument list. */
2298 alist_unlink(win->w_alist);
2299
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002300 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002301 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002302 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002303 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002304 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002305
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306 /* When deleting the current window of another tab page select a new
2307 * current window. */
2308 if (tp != NULL && win == tp->tp_curwin)
2309 tp->tp_curwin = wp;
2310
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002311 return wp;
2312}
2313
2314#if defined(EXITFREE) || defined(PROTO)
2315 void
2316win_free_all()
2317{
2318 int dummy;
2319
Bram Moolenaarf740b292006-02-16 22:11:02 +00002320# ifdef FEAT_WINDOWS
2321 while (first_tabpage->tp_next != NULL)
2322 tabpage_close(TRUE);
2323# endif
2324
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002325 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002326 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002327}
2328#endif
2329
2330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 * Remove a window and its frame from the tree of frames.
2332 * Returns a pointer to the window that got the freed up space.
2333 */
2334/*ARGSUSED*/
2335 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002336winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 win_T *win;
2338 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002339 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 frame_T *frp, *frp2, *frp3;
2342 frame_T *frp_close = win->w_frame;
2343 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002346 * If there is only one window there is nothing to remove.
2347 */
2348 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2349 return NULL;
2350
2351 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 * Remove the window from its frame.
2353 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002354 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 wp = frame2win(frp2);
2356
2357 /* Remove this frame from the list of frames. */
2358 frame_remove(frp_close);
2359
2360#ifdef FEAT_VERTSPLIT
2361 if (frp_close->fr_parent->fr_layout == FR_COL)
2362 {
2363#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002364 /* When 'winfixheight' is set, try to find another frame in the column
2365 * (as close to the closed frame as possible) to distribute the height
2366 * to. */
2367 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2368 {
2369 frp = frp_close->fr_prev;
2370 frp3 = frp_close->fr_next;
2371 while (frp != NULL || frp3 != NULL)
2372 {
2373 if (frp != NULL)
2374 {
2375 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2376 {
2377 frp2 = frp;
2378 wp = frp->fr_win;
2379 break;
2380 }
2381 frp = frp->fr_prev;
2382 }
2383 if (frp3 != NULL)
2384 {
2385 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2386 {
2387 frp2 = frp3;
2388 wp = frp3->fr_win;
2389 break;
2390 }
2391 frp3 = frp3->fr_next;
2392 }
2393 }
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2396 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#ifdef FEAT_VERTSPLIT
2398 *dirp = 'v';
2399 }
2400 else
2401 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002402 /* When 'winfixwidth' is set, try to find another frame in the column
2403 * (as close to the closed frame as possible) to distribute the width
2404 * to. */
2405 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2406 {
2407 frp = frp_close->fr_prev;
2408 frp3 = frp_close->fr_next;
2409 while (frp != NULL || frp3 != NULL)
2410 {
2411 if (frp != NULL)
2412 {
2413 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2414 {
2415 frp2 = frp;
2416 wp = frp->fr_win;
2417 break;
2418 }
2419 frp = frp->fr_prev;
2420 }
2421 if (frp3 != NULL)
2422 {
2423 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2424 {
2425 frp2 = frp3;
2426 wp = frp3->fr_win;
2427 break;
2428 }
2429 frp3 = frp3->fr_next;
2430 }
2431 }
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002434 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 *dirp = 'h';
2436 }
2437#endif
2438
2439 /* If rows/columns go to a window below/right its positions need to be
2440 * updated. Can only be done after the sizes have been updated. */
2441 if (frp2 == frp_close->fr_next)
2442 {
2443 int row = win->w_winrow;
2444 int col = W_WINCOL(win);
2445
2446 frame_comp_pos(frp2, &row, &col);
2447 }
2448
2449 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2450 {
2451 /* There is no other frame in this list, move its info to the parent
2452 * and remove it. */
2453 frp2->fr_parent->fr_layout = frp2->fr_layout;
2454 frp2->fr_parent->fr_child = frp2->fr_child;
2455 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2456 frp->fr_parent = frp2->fr_parent;
2457 frp2->fr_parent->fr_win = frp2->fr_win;
2458 if (frp2->fr_win != NULL)
2459 frp2->fr_win->w_frame = frp2->fr_parent;
2460 frp = frp2->fr_parent;
2461 vim_free(frp2);
2462
2463 frp2 = frp->fr_parent;
2464 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2465 {
2466 /* The frame above the parent has the same layout, have to merge
2467 * the frames into this list. */
2468 if (frp2->fr_child == frp)
2469 frp2->fr_child = frp->fr_child;
2470 frp->fr_child->fr_prev = frp->fr_prev;
2471 if (frp->fr_prev != NULL)
2472 frp->fr_prev->fr_next = frp->fr_child;
2473 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2474 {
2475 frp3->fr_parent = frp2;
2476 if (frp3->fr_next == NULL)
2477 {
2478 frp3->fr_next = frp->fr_next;
2479 if (frp->fr_next != NULL)
2480 frp->fr_next->fr_prev = frp3;
2481 break;
2482 }
2483 }
2484 vim_free(frp);
2485 }
2486 }
2487
2488 return wp;
2489}
2490
2491/*
2492 * Find out which frame is going to get the freed up space when "win" is
2493 * closed.
2494 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2495 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2496 * This makes opening a window and closing it immediately keep the same window
2497 * layout.
2498 */
2499 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002500win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002502 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 frame_T *frp;
2505 int b;
2506
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002508 /* Last window in this tab page, will go to next tab page. */
2509 return alt_tabpage()->tp_curwin->w_frame;
2510
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 frp = win->w_frame;
2512#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002513 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 b = p_spr;
2515 else
2516#endif
2517 b = p_sb;
2518 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2519 return frp->fr_next;
2520 return frp->fr_prev;
2521}
2522
2523/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002524 * Return the tabpage that will be used if the current one is closed.
2525 */
2526 static tabpage_T *
2527alt_tabpage()
2528{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002529 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002530
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002531 /* Use the next tab page if possible. */
2532 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002533 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002534
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002535 /* Find the last but one tab page. */
2536 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2537 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002538 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002539}
2540
2541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 * Find the left-upper window in frame "frp".
2543 */
2544 static win_T *
2545frame2win(frp)
2546 frame_T *frp;
2547{
2548 while (frp->fr_win == NULL)
2549 frp = frp->fr_child;
2550 return frp->fr_win;
2551}
2552
2553/*
2554 * Return TRUE if frame "frp" contains window "wp".
2555 */
2556 static int
2557frame_has_win(frp, wp)
2558 frame_T *frp;
2559 win_T *wp;
2560{
2561 frame_T *p;
2562
2563 if (frp->fr_layout == FR_LEAF)
2564 return frp->fr_win == wp;
2565
2566 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2567 if (frame_has_win(p, wp))
2568 return TRUE;
2569 return FALSE;
2570}
2571
2572/*
2573 * Set a new height for a frame. Recursively sets the height for contained
2574 * frames and windows. Caller must take care of positions.
2575 */
2576 static void
2577frame_new_height(topfrp, height, topfirst, wfh)
2578 frame_T *topfrp;
2579 int height;
2580 int topfirst; /* resize topmost contained frame first */
2581 int wfh; /* obey 'winfixheight' when there is a choice;
2582 may cause the height not to be set */
2583{
2584 frame_T *frp;
2585 int extra_lines;
2586 int h;
2587
2588 if (topfrp->fr_win != NULL)
2589 {
2590 /* Simple case: just one window. */
2591 win_new_height(topfrp->fr_win,
2592 height - topfrp->fr_win->w_status_height);
2593 }
2594#ifdef FEAT_VERTSPLIT
2595 else if (topfrp->fr_layout == FR_ROW)
2596 {
2597 do
2598 {
2599 /* All frames in this row get the same new height. */
2600 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2601 {
2602 frame_new_height(frp, height, topfirst, wfh);
2603 if (frp->fr_height > height)
2604 {
2605 /* Could not fit the windows, make the whole row higher. */
2606 height = frp->fr_height;
2607 break;
2608 }
2609 }
2610 }
2611 while (frp != NULL);
2612 }
2613#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002614 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 /* Complicated case: Resize a column of frames. Resize the bottom
2617 * frame first, frames above that when needed. */
2618
2619 frp = topfrp->fr_child;
2620 if (wfh)
2621 /* Advance past frames with one window with 'wfh' set. */
2622 while (frame_fixed_height(frp))
2623 {
2624 frp = frp->fr_next;
2625 if (frp == NULL)
2626 return; /* no frame without 'wfh', give up */
2627 }
2628 if (!topfirst)
2629 {
2630 /* Find the bottom frame of this column */
2631 while (frp->fr_next != NULL)
2632 frp = frp->fr_next;
2633 if (wfh)
2634 /* Advance back for frames with one window with 'wfh' set. */
2635 while (frame_fixed_height(frp))
2636 frp = frp->fr_prev;
2637 }
2638
2639 extra_lines = height - topfrp->fr_height;
2640 if (extra_lines < 0)
2641 {
2642 /* reduce height of contained frames, bottom or top frame first */
2643 while (frp != NULL)
2644 {
2645 h = frame_minheight(frp, NULL);
2646 if (frp->fr_height + extra_lines < h)
2647 {
2648 extra_lines += frp->fr_height - h;
2649 frame_new_height(frp, h, topfirst, wfh);
2650 }
2651 else
2652 {
2653 frame_new_height(frp, frp->fr_height + extra_lines,
2654 topfirst, wfh);
2655 break;
2656 }
2657 if (topfirst)
2658 {
2659 do
2660 frp = frp->fr_next;
2661 while (wfh && frp != NULL && frame_fixed_height(frp));
2662 }
2663 else
2664 {
2665 do
2666 frp = frp->fr_prev;
2667 while (wfh && frp != NULL && frame_fixed_height(frp));
2668 }
2669 /* Increase "height" if we could not reduce enough frames. */
2670 if (frp == NULL)
2671 height -= extra_lines;
2672 }
2673 }
2674 else if (extra_lines > 0)
2675 {
2676 /* increase height of bottom or top frame */
2677 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2678 }
2679 }
2680 topfrp->fr_height = height;
2681}
2682
2683/*
2684 * Return TRUE if height of frame "frp" should not be changed because of
2685 * the 'winfixheight' option.
2686 */
2687 static int
2688frame_fixed_height(frp)
2689 frame_T *frp;
2690{
2691 /* frame with one window: fixed height if 'winfixheight' set. */
2692 if (frp->fr_win != NULL)
2693 return frp->fr_win->w_p_wfh;
2694
2695 if (frp->fr_layout == FR_ROW)
2696 {
2697 /* The frame is fixed height if one of the frames in the row is fixed
2698 * height. */
2699 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2700 if (frame_fixed_height(frp))
2701 return TRUE;
2702 return FALSE;
2703 }
2704
2705 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2706 * frames in the row are fixed height. */
2707 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2708 if (!frame_fixed_height(frp))
2709 return FALSE;
2710 return TRUE;
2711}
2712
2713#ifdef FEAT_VERTSPLIT
2714/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002715 * Return TRUE if width of frame "frp" should not be changed because of
2716 * the 'winfixwidth' option.
2717 */
2718 static int
2719frame_fixed_width(frp)
2720 frame_T *frp;
2721{
2722 /* frame with one window: fixed width if 'winfixwidth' set. */
2723 if (frp->fr_win != NULL)
2724 return frp->fr_win->w_p_wfw;
2725
2726 if (frp->fr_layout == FR_COL)
2727 {
2728 /* The frame is fixed width if one of the frames in the row is fixed
2729 * width. */
2730 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2731 if (frame_fixed_width(frp))
2732 return TRUE;
2733 return FALSE;
2734 }
2735
2736 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2737 * frames in the row are fixed width. */
2738 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2739 if (!frame_fixed_width(frp))
2740 return FALSE;
2741 return TRUE;
2742}
2743
2744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 * Add a status line to windows at the bottom of "frp".
2746 * Note: Does not check if there is room!
2747 */
2748 static void
2749frame_add_statusline(frp)
2750 frame_T *frp;
2751{
2752 win_T *wp;
2753
2754 if (frp->fr_layout == FR_LEAF)
2755 {
2756 wp = frp->fr_win;
2757 if (wp->w_status_height == 0)
2758 {
2759 if (wp->w_height > 0) /* don't make it negative */
2760 --wp->w_height;
2761 wp->w_status_height = STATUS_HEIGHT;
2762 }
2763 }
2764 else if (frp->fr_layout == FR_ROW)
2765 {
2766 /* Handle all the frames in the row. */
2767 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2768 frame_add_statusline(frp);
2769 }
2770 else /* frp->fr_layout == FR_COL */
2771 {
2772 /* Only need to handle the last frame in the column. */
2773 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2774 ;
2775 frame_add_statusline(frp);
2776 }
2777}
2778
2779/*
2780 * Set width of a frame. Handles recursively going through contained frames.
2781 * May remove separator line for windows at the right side (for win_close()).
2782 */
2783 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002784frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 frame_T *topfrp;
2786 int width;
2787 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002788 int wfw; /* obey 'winfixwidth' when there is a choice;
2789 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
2791 frame_T *frp;
2792 int extra_cols;
2793 int w;
2794 win_T *wp;
2795
2796 if (topfrp->fr_layout == FR_LEAF)
2797 {
2798 /* Simple case: just one window. */
2799 wp = topfrp->fr_win;
2800 /* Find out if there are any windows right of this one. */
2801 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2802 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2803 break;
2804 if (frp->fr_parent == NULL)
2805 wp->w_vsep_width = 0;
2806 win_new_width(wp, width - wp->w_vsep_width);
2807 }
2808 else if (topfrp->fr_layout == FR_COL)
2809 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002810 do
2811 {
2812 /* All frames in this column get the same new width. */
2813 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2814 {
2815 frame_new_width(frp, width, leftfirst, wfw);
2816 if (frp->fr_width > width)
2817 {
2818 /* Could not fit the windows, make whole column wider. */
2819 width = frp->fr_width;
2820 break;
2821 }
2822 }
2823 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 else /* fr_layout == FR_ROW */
2826 {
2827 /* Complicated case: Resize a row of frames. Resize the rightmost
2828 * frame first, frames left of it when needed. */
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002831 if (wfw)
2832 /* Advance past frames with one window with 'wfw' set. */
2833 while (frame_fixed_width(frp))
2834 {
2835 frp = frp->fr_next;
2836 if (frp == NULL)
2837 return; /* no frame without 'wfw', give up */
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002840 {
2841 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 while (frp->fr_next != NULL)
2843 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002844 if (wfw)
2845 /* Advance back for frames with one window with 'wfw' set. */
2846 while (frame_fixed_width(frp))
2847 frp = frp->fr_prev;
2848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 extra_cols = width - topfrp->fr_width;
2851 if (extra_cols < 0)
2852 {
2853 /* reduce frame width, rightmost frame first */
2854 while (frp != NULL)
2855 {
2856 w = frame_minwidth(frp, NULL);
2857 if (frp->fr_width + extra_cols < w)
2858 {
2859 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002860 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 else
2863 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002864 frame_new_width(frp, frp->fr_width + extra_cols,
2865 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 break;
2867 }
2868 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002869 {
2870 do
2871 frp = frp->fr_next;
2872 while (wfw && frp != NULL && frame_fixed_width(frp));
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002875 {
2876 do
2877 frp = frp->fr_prev;
2878 while (wfw && frp != NULL && frame_fixed_width(frp));
2879 }
2880 /* Increase "width" if we could not reduce enough frames. */
2881 if (frp == NULL)
2882 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
2885 else if (extra_cols > 0)
2886 {
2887 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002888 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
2890 }
2891 topfrp->fr_width = width;
2892}
2893
2894/*
2895 * Add the vertical separator to windows at the right side of "frp".
2896 * Note: Does not check if there is room!
2897 */
2898 static void
2899frame_add_vsep(frp)
2900 frame_T *frp;
2901{
2902 win_T *wp;
2903
2904 if (frp->fr_layout == FR_LEAF)
2905 {
2906 wp = frp->fr_win;
2907 if (wp->w_vsep_width == 0)
2908 {
2909 if (wp->w_width > 0) /* don't make it negative */
2910 --wp->w_width;
2911 wp->w_vsep_width = 1;
2912 }
2913 }
2914 else if (frp->fr_layout == FR_COL)
2915 {
2916 /* Handle all the frames in the column. */
2917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2918 frame_add_vsep(frp);
2919 }
2920 else /* frp->fr_layout == FR_ROW */
2921 {
2922 /* Only need to handle the last frame in the row. */
2923 frp = frp->fr_child;
2924 while (frp->fr_next != NULL)
2925 frp = frp->fr_next;
2926 frame_add_vsep(frp);
2927 }
2928}
2929
2930/*
2931 * Set frame width from the window it contains.
2932 */
2933 static void
2934frame_fix_width(wp)
2935 win_T *wp;
2936{
2937 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2938}
2939#endif
2940
2941/*
2942 * Set frame height from the window it contains.
2943 */
2944 static void
2945frame_fix_height(wp)
2946 win_T *wp;
2947{
2948 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2949}
2950
2951/*
2952 * Compute the minimal height for frame "topfrp".
2953 * Uses the 'winminheight' option.
2954 * When "next_curwin" isn't NULL, use p_wh for this window.
2955 * When "next_curwin" is NOWIN, don't use at least one line for the current
2956 * window.
2957 */
2958 static int
2959frame_minheight(topfrp, next_curwin)
2960 frame_T *topfrp;
2961 win_T *next_curwin;
2962{
2963 frame_T *frp;
2964 int m;
2965#ifdef FEAT_VERTSPLIT
2966 int n;
2967#endif
2968
2969 if (topfrp->fr_win != NULL)
2970 {
2971 if (topfrp->fr_win == next_curwin)
2972 m = p_wh + topfrp->fr_win->w_status_height;
2973 else
2974 {
2975 /* window: minimal height of the window plus status line */
2976 m = p_wmh + topfrp->fr_win->w_status_height;
2977 /* Current window is minimal one line high */
2978 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2979 ++m;
2980 }
2981 }
2982#ifdef FEAT_VERTSPLIT
2983 else if (topfrp->fr_layout == FR_ROW)
2984 {
2985 /* get the minimal height from each frame in this row */
2986 m = 0;
2987 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2988 {
2989 n = frame_minheight(frp, next_curwin);
2990 if (n > m)
2991 m = n;
2992 }
2993 }
2994#endif
2995 else
2996 {
2997 /* Add up the minimal heights for all frames in this column. */
2998 m = 0;
2999 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3000 m += frame_minheight(frp, next_curwin);
3001 }
3002
3003 return m;
3004}
3005
3006#ifdef FEAT_VERTSPLIT
3007/*
3008 * Compute the minimal width for frame "topfrp".
3009 * When "next_curwin" isn't NULL, use p_wiw for this window.
3010 * When "next_curwin" is NOWIN, don't use at least one column for the current
3011 * window.
3012 */
3013 static int
3014frame_minwidth(topfrp, next_curwin)
3015 frame_T *topfrp;
3016 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3017{
3018 frame_T *frp;
3019 int m, n;
3020
3021 if (topfrp->fr_win != NULL)
3022 {
3023 if (topfrp->fr_win == next_curwin)
3024 m = p_wiw + topfrp->fr_win->w_vsep_width;
3025 else
3026 {
3027 /* window: minimal width of the window plus separator column */
3028 m = p_wmw + topfrp->fr_win->w_vsep_width;
3029 /* Current window is minimal one column wide */
3030 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3031 ++m;
3032 }
3033 }
3034 else if (topfrp->fr_layout == FR_COL)
3035 {
3036 /* get the minimal width from each frame in this column */
3037 m = 0;
3038 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3039 {
3040 n = frame_minwidth(frp, next_curwin);
3041 if (n > m)
3042 m = n;
3043 }
3044 }
3045 else
3046 {
3047 /* Add up the minimal widths for all frames in this row. */
3048 m = 0;
3049 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3050 m += frame_minwidth(frp, next_curwin);
3051 }
3052
3053 return m;
3054}
3055#endif
3056
3057
3058/*
3059 * Try to close all windows except current one.
3060 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3061 * used and the buffer was modified.
3062 *
3063 * Used by ":bdel" and ":only".
3064 */
3065 void
3066close_others(message, forceit)
3067 int message;
3068 int forceit; /* always hide all other windows */
3069{
3070 win_T *wp;
3071 win_T *nextwp;
3072 int r;
3073
3074 if (lastwin == firstwin)
3075 {
3076 if (message
3077#ifdef FEAT_AUTOCMD
3078 && !autocmd_busy
3079#endif
3080 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003081 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 return;
3083 }
3084
3085 /* Be very careful here: autocommands may change the window layout. */
3086 for (wp = firstwin; win_valid(wp); wp = nextwp)
3087 {
3088 nextwp = wp->w_next;
3089 if (wp != curwin) /* don't close current window */
3090 {
3091
3092 /* Check if it's allowed to abandon this window */
3093 r = can_abandon(wp->w_buffer, forceit);
3094#ifdef FEAT_AUTOCMD
3095 if (!win_valid(wp)) /* autocommands messed wp up */
3096 {
3097 nextwp = firstwin;
3098 continue;
3099 }
3100#endif
3101 if (!r)
3102 {
3103#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3104 if (message && (p_confirm || cmdmod.confirm) && p_write)
3105 {
3106 dialog_changed(wp->w_buffer, FALSE);
3107# ifdef FEAT_AUTOCMD
3108 if (!win_valid(wp)) /* autocommands messed wp up */
3109 {
3110 nextwp = firstwin;
3111 continue;
3112 }
3113# endif
3114 }
3115 if (bufIsChanged(wp->w_buffer))
3116#endif
3117 continue;
3118 }
3119 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3120 }
3121 }
3122
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003123 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 EMSG(_("E445: Other window contains changes"));
3125}
3126
3127#endif /* FEAT_WINDOWS */
3128
3129/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003130 * Init the current window "curwin".
3131 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 */
3133 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003134curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003136 redraw_win_later(curwin, NOT_VALID);
3137 curwin->w_lines_valid = 0;
3138 curwin->w_cursor.lnum = 1;
3139 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003143 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3144 curwin->w_pcmark.col = 0;
3145 curwin->w_prev_pcmark.lnum = 0;
3146 curwin->w_prev_pcmark.col = 0;
3147 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003149 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003151 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#ifdef FEAT_FKMAP
3153 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003154 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003156 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158}
3159
3160/*
3161 * Allocate the first window and put an empty buffer in it.
3162 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003163 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166win_alloc_first()
3167{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003168 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003169 return FAIL;
3170
3171#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003172 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003173 if (first_tabpage == NULL)
3174 return FAIL;
3175 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003176 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003177#endif
3178 return OK;
3179}
3180
3181/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003182 * Allocate the first window or the first window in a new tab page.
3183 * When "oldwin" is NULL create an empty buffer for it.
3184 * When "oldwin" is not NULL copy info from it to the new window (only with
3185 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003186 * Return FAIL when something goes wrong (out of memory).
3187 */
3188 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003189win_alloc_firstwin(oldwin)
3190 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003191{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003193 if (oldwin == NULL)
3194 {
3195 /* Very first window, need to create an empty buffer for it and
3196 * initialize from scratch. */
3197 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3198 if (curwin == NULL || curbuf == NULL)
3199 return FAIL;
3200 curwin->w_buffer = curbuf;
3201 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003203 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003205 curwin_init(); /* init current window */
3206 }
3207#ifdef FEAT_WINDOWS
3208 else
3209 {
3210 /* First window in new tab page, initialize it from "oldwin". */
3211 win_init(curwin, oldwin);
3212
3213# ifdef FEAT_SCROLLBIND
3214 /* We don't want scroll-binding in the first window. */
3215 curwin->w_p_scb = FALSE;
3216# endif
3217 }
3218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
3220 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3221 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003222 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 topframe->fr_layout = FR_LEAF;
3224#ifdef FEAT_VERTSPLIT
3225 topframe->fr_width = Columns;
3226#endif
3227 topframe->fr_height = Rows - p_ch;
3228 topframe->fr_win = curwin;
3229 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230
3231 return OK;
3232}
3233
3234/*
3235 * Initialize the window and frame size to the maximum.
3236 */
3237 void
3238win_init_size()
3239{
3240 firstwin->w_height = ROWS_AVAIL;
3241 topframe->fr_height = ROWS_AVAIL;
3242#ifdef FEAT_VERTSPLIT
3243 firstwin->w_width = Columns;
3244 topframe->fr_width = Columns;
3245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003249
3250/*
3251 * Allocate a new tabpage_T and init the values.
3252 * Returns NULL when out of memory.
3253 */
3254 static tabpage_T *
3255alloc_tabpage()
3256{
3257 tabpage_T *tp;
3258
3259 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3260 if (tp != NULL)
3261 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003262# ifdef FEAT_GUI
3263 int i;
3264
3265 for (i = 0; i < 3; i++)
3266 tp->tp_prev_which_scrollbars[i] = -1;
3267# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003268# ifdef FEAT_DIFF
3269 tp->tp_diff_invalid = TRUE;
3270# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003271#ifdef FEAT_EVAL
3272 /* init t: variables */
3273 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3274#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003275 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003276 }
3277 return tp;
3278}
3279
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003280 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003281free_tabpage(tp)
3282 tabpage_T *tp;
3283{
3284# ifdef FEAT_DIFF
3285 diff_clear(tp);
3286# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003287 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288#ifdef FEAT_EVAL
3289 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3290#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003291 vim_free(tp);
3292}
3293
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003294/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003295 * Create a new Tab page with one window.
3296 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003297 * When "after" is 0 put it just after the current Tab page.
3298 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299 * Return FAIL or OK.
3300 */
3301 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003302win_new_tabpage(after)
3303 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003305 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003306 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003307 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003308
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003309 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003310 if (newtp == NULL)
3311 return FAIL;
3312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003313 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003314 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003315 {
3316 vim_free(newtp);
3317 return FAIL;
3318 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003319 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320
3321 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003322 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003325 if (after == 1)
3326 {
3327 /* New tab page becomes the first one. */
3328 newtp->tp_next = first_tabpage;
3329 first_tabpage = newtp;
3330 }
3331 else
3332 {
3333 if (after > 0)
3334 {
3335 /* Put new tab page before tab page "after". */
3336 n = 2;
3337 for (tp = first_tabpage; tp->tp_next != NULL
3338 && n < after; tp = tp->tp_next)
3339 ++n;
3340 }
3341 newtp->tp_next = tp->tp_next;
3342 tp->tp_next = newtp;
3343 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003345 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003346 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003347
3348 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003349 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003350
3351#if defined(FEAT_GUI)
3352 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3353 * scrollbars. Have to update them anyway. */
3354 if (gui.in_use && starting == 0)
3355 {
3356 gui_init_which_components(NULL);
3357 gui_update_scrollbars(TRUE);
3358 }
3359 need_mouse_correct = TRUE;
3360#endif
3361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003362 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003363#ifdef FEAT_AUTOCMD
3364 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3365 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3366#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003367 return OK;
3368 }
3369
3370 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003371 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372 return FAIL;
3373}
3374
3375/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003376 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3377 * like with ":split".
3378 * Returns OK if a new tab page was created, FAIL otherwise.
3379 */
3380 int
3381may_open_tabpage()
3382{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003383 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003384
Bram Moolenaard326ce82007-03-11 14:48:29 +00003385 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003386 {
3387 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003388 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003389 return win_new_tabpage(n);
3390 }
3391 return FAIL;
3392}
3393
3394/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003395 * Create up to "maxcount" tabpages with empty windows.
3396 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003398 int
3399make_tabpages(maxcount)
3400 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003401{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003402 int count = maxcount;
3403 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003405 /* Limit to 'tabpagemax' tabs. */
3406 if (count > p_tpm)
3407 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003408
3409#ifdef FEAT_AUTOCMD
3410 /*
3411 * Don't execute autocommands while creating the tab pages. Must do that
3412 * when putting the buffers in the windows.
3413 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003414 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415#endif
3416
3417 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003418 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003420
3421#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003422 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003423#endif
3424
3425 /* return actual number of tab pages */
3426 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003427}
3428
3429/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003430 * Return TRUE when "tpc" points to a valid tab page.
3431 */
3432 int
3433valid_tabpage(tpc)
3434 tabpage_T *tpc;
3435{
3436 tabpage_T *tp;
3437
3438 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3439 if (tp == tpc)
3440 return TRUE;
3441 return FALSE;
3442}
3443
3444/*
3445 * Find tab page "n" (first one is 1). Returns NULL when not found.
3446 */
3447 tabpage_T *
3448find_tabpage(n)
3449 int n;
3450{
3451 tabpage_T *tp;
3452 int i = 1;
3453
3454 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3455 ++i;
3456 return tp;
3457}
3458
3459/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003460 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003461 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003462 */
3463 int
3464tabpage_index(ftp)
3465 tabpage_T *ftp;
3466{
3467 int i = 1;
3468 tabpage_T *tp;
3469
3470 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3471 ++i;
3472 return i;
3473}
3474
3475/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003476 * Prepare for leaving the current tab page.
3477 * When autocomands change "curtab" we don't leave the tab page and return
3478 * FAIL.
3479 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003481/*ARGSUSED*/
3482 static int
3483leave_tabpage(new_curbuf)
3484 buf_T *new_curbuf; /* what is going to be the new curbuf,
3485 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003486{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003487 tabpage_T *tp = curtab;
3488
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003489#ifdef FEAT_VISUAL
3490 reset_VIsual_and_resel(); /* stop Visual mode */
3491#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003492#ifdef FEAT_AUTOCMD
3493 if (new_curbuf != curbuf)
3494 {
3495 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3496 if (curtab != tp)
3497 return FAIL;
3498 }
3499 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3500 if (curtab != tp)
3501 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003502 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003503 if (curtab != tp)
3504 return FAIL;
3505#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003506#if defined(FEAT_GUI)
3507 /* Remove the scrollbars. They may be added back later. */
3508 if (gui.in_use)
3509 gui_remove_scrollbars();
3510#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003511 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003512 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003513 tp->tp_firstwin = firstwin;
3514 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003515 tp->tp_old_Rows = Rows;
3516 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 firstwin = NULL;
3518 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003519 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520}
3521
3522/*
3523 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003524 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525 */
3526/*ARGSUSED*/
3527 static void
3528enter_tabpage(tp, old_curbuf)
3529 tabpage_T *tp;
3530 buf_T *old_curbuf;
3531{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003532 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003533 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003534
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003535 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 firstwin = tp->tp_firstwin;
3537 lastwin = tp->tp_lastwin;
3538 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003539
3540 /* We would like doing the TabEnter event first, but we don't have a
3541 * valid current window yet, which may break some commands.
3542 * This triggers autocommands, thus may make "tp" invalid. */
3543 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3544 prevwin = next_prevwin;
3545
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003546#ifdef FEAT_AUTOCMD
3547 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003548
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549 if (old_curbuf != curbuf)
3550 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3551#endif
3552
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003553 last_status(FALSE); /* status line may appear or disappear */
3554 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003555 must_redraw = CLEAR; /* need to redraw everything */
3556#ifdef FEAT_DIFF
3557 diff_need_scrollbind = TRUE;
3558#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003560 /* The tabpage line may have appeared or disappeared, may need to resize
3561 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003562 * frame sizes too. Use the stored value of p_ch, so that it can be
3563 * different for each tab page. */
3564 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003565 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3566#ifdef FEAT_GUI_TABLINE
3567 && !gui_use_tabline()
3568#endif
3569 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003570 shell_new_rows();
3571#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003572 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003573 shell_new_columns(); /* update window widths */
3574#endif
3575
3576#if defined(FEAT_GUI)
3577 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3578 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003579 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003580 {
3581 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003582 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003583 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003584 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585#endif
3586
3587 redraw_all_later(CLEAR);
3588}
3589
3590/*
3591 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003592 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 */
3594 void
3595goto_tabpage(n)
3596 int n;
3597{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003599 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 int i;
3601
Bram Moolenaard68071d2006-05-02 22:08:30 +00003602 if (text_locked())
3603 {
3604 /* Not allowed when editing the command line. */
3605#ifdef FEAT_CMDWIN
3606 if (cmdwin_type != 0)
3607 EMSG(_(e_cmdwin));
3608 else
3609#endif
3610 EMSG(_(e_secure));
3611 return;
3612 }
3613
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003614 /* If there is only one it can't work. */
3615 if (first_tabpage->tp_next == NULL)
3616 {
3617 if (n > 1)
3618 beep_flush();
3619 return;
3620 }
3621
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 if (n == 0)
3623 {
3624 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003625 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003626 tp = first_tabpage;
3627 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003628 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003630 else if (n < 0)
3631 {
3632 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3633 * this N times. */
3634 ttp = curtab;
3635 for (i = n; i < 0; ++i)
3636 {
3637 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3638 tp = tp->tp_next)
3639 ;
3640 ttp = tp;
3641 }
3642 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003643 else if (n == 9999)
3644 {
3645 /* Go to last tab page. */
3646 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3647 ;
3648 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003649 else
3650 {
3651 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003652 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003653 if (tp == NULL)
3654 {
3655 beep_flush();
3656 return;
3657 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658 }
3659
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003660 goto_tabpage_tp(tp);
3661
3662#ifdef FEAT_GUI_TABLINE
3663 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003664 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003665#endif
3666}
3667
3668/*
3669 * Go to tabpage "tp".
3670 * Note: doesn't update the GUI tab.
3671 */
3672 void
3673goto_tabpage_tp(tp)
3674 tabpage_T *tp;
3675{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003676 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003677 {
3678 if (valid_tabpage(tp))
3679 enter_tabpage(tp, curbuf);
3680 else
3681 enter_tabpage(curtab, curbuf);
3682 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003686 * Enter window "wp" in tab page "tp".
3687 * Also updates the GUI tab.
3688 */
3689 void
3690goto_tabpage_win(tp, wp)
3691 tabpage_T *tp;
3692 win_T *wp;
3693{
3694 goto_tabpage_tp(tp);
3695 if (curtab == tp && win_valid(wp))
3696 {
3697 win_enter(wp, TRUE);
3698# ifdef FEAT_GUI_TABLINE
3699 if (gui_use_tabline())
3700 gui_mch_set_curtab(tabpage_index(curtab));
3701# endif
3702 }
3703}
3704
3705/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003706 * Move the current tab page to before tab page "nr".
3707 */
3708 void
3709tabpage_move(nr)
3710 int nr;
3711{
3712 int n = nr;
3713 tabpage_T *tp;
3714
3715 if (first_tabpage->tp_next == NULL)
3716 return;
3717
3718 /* Remove the current tab page from the list of tab pages. */
3719 if (curtab == first_tabpage)
3720 first_tabpage = curtab->tp_next;
3721 else
3722 {
3723 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3724 if (tp->tp_next == curtab)
3725 break;
3726 if (tp == NULL) /* "cannot happen" */
3727 return;
3728 tp->tp_next = curtab->tp_next;
3729 }
3730
3731 /* Re-insert it at the specified position. */
3732 if (n == 0)
3733 {
3734 curtab->tp_next = first_tabpage;
3735 first_tabpage = curtab;
3736 }
3737 else
3738 {
3739 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3740 --n;
3741 curtab->tp_next = tp->tp_next;
3742 tp->tp_next = curtab;
3743 }
3744
3745 /* Need to redraw the tabline. Tab page contents doesn't change. */
3746 redraw_tabline = TRUE;
3747}
3748
3749
3750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 * Go to another window.
3752 * When jumping to another buffer, stop Visual mode. Do this before
3753 * changing windows so we can yank the selection into the '*' register.
3754 * When jumping to another window on the same buffer, adjust its cursor
3755 * position to keep the same Visual area.
3756 */
3757 void
3758win_goto(wp)
3759 win_T *wp;
3760{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003761 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003764 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 return;
3766 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767#ifdef FEAT_AUTOCMD
3768 if (curbuf_locked())
3769 return;
3770#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#ifdef FEAT_VISUAL
3773 if (wp->w_buffer != curbuf)
3774 reset_VIsual_and_resel();
3775 else if (VIsual_active)
3776 wp->w_cursor = curwin->w_cursor;
3777#endif
3778
3779#ifdef FEAT_GUI
3780 need_mouse_correct = TRUE;
3781#endif
3782 win_enter(wp, TRUE);
3783}
3784
3785#if defined(FEAT_PERL) || defined(PROTO)
3786/*
3787 * Find window number "winnr" (counting top to bottom).
3788 */
3789 win_T *
3790win_find_nr(winnr)
3791 int winnr;
3792{
3793 win_T *wp;
3794
3795# ifdef FEAT_WINDOWS
3796 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3797 if (--winnr == 0)
3798 break;
3799 return wp;
3800# else
3801 return curwin;
3802# endif
3803}
3804#endif
3805
3806#ifdef FEAT_VERTSPLIT
3807/*
3808 * Move to window above or below "count" times.
3809 */
3810 static void
3811win_goto_ver(up, count)
3812 int up; /* TRUE to go to win above */
3813 long count;
3814{
3815 frame_T *fr;
3816 frame_T *nfr;
3817 frame_T *foundfr;
3818
3819 foundfr = curwin->w_frame;
3820 while (count--)
3821 {
3822 /*
3823 * First go upwards in the tree of frames until we find a upwards or
3824 * downwards neighbor.
3825 */
3826 fr = foundfr;
3827 for (;;)
3828 {
3829 if (fr == topframe)
3830 goto end;
3831 if (up)
3832 nfr = fr->fr_prev;
3833 else
3834 nfr = fr->fr_next;
3835 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3836 break;
3837 fr = fr->fr_parent;
3838 }
3839
3840 /*
3841 * Now go downwards to find the bottom or top frame in it.
3842 */
3843 for (;;)
3844 {
3845 if (nfr->fr_layout == FR_LEAF)
3846 {
3847 foundfr = nfr;
3848 break;
3849 }
3850 fr = nfr->fr_child;
3851 if (nfr->fr_layout == FR_ROW)
3852 {
3853 /* Find the frame at the cursor row. */
3854 while (fr->fr_next != NULL
3855 && frame2win(fr)->w_wincol + fr->fr_width
3856 <= curwin->w_wincol + curwin->w_wcol)
3857 fr = fr->fr_next;
3858 }
3859 if (nfr->fr_layout == FR_COL && up)
3860 while (fr->fr_next != NULL)
3861 fr = fr->fr_next;
3862 nfr = fr;
3863 }
3864 }
3865end:
3866 if (foundfr != NULL)
3867 win_goto(foundfr->fr_win);
3868}
3869
3870/*
3871 * Move to left or right window.
3872 */
3873 static void
3874win_goto_hor(left, count)
3875 int left; /* TRUE to go to left win */
3876 long count;
3877{
3878 frame_T *fr;
3879 frame_T *nfr;
3880 frame_T *foundfr;
3881
3882 foundfr = curwin->w_frame;
3883 while (count--)
3884 {
3885 /*
3886 * First go upwards in the tree of frames until we find a left or
3887 * right neighbor.
3888 */
3889 fr = foundfr;
3890 for (;;)
3891 {
3892 if (fr == topframe)
3893 goto end;
3894 if (left)
3895 nfr = fr->fr_prev;
3896 else
3897 nfr = fr->fr_next;
3898 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3899 break;
3900 fr = fr->fr_parent;
3901 }
3902
3903 /*
3904 * Now go downwards to find the leftmost or rightmost frame in it.
3905 */
3906 for (;;)
3907 {
3908 if (nfr->fr_layout == FR_LEAF)
3909 {
3910 foundfr = nfr;
3911 break;
3912 }
3913 fr = nfr->fr_child;
3914 if (nfr->fr_layout == FR_COL)
3915 {
3916 /* Find the frame at the cursor row. */
3917 while (fr->fr_next != NULL
3918 && frame2win(fr)->w_winrow + fr->fr_height
3919 <= curwin->w_winrow + curwin->w_wrow)
3920 fr = fr->fr_next;
3921 }
3922 if (nfr->fr_layout == FR_ROW && left)
3923 while (fr->fr_next != NULL)
3924 fr = fr->fr_next;
3925 nfr = fr;
3926 }
3927 }
3928end:
3929 if (foundfr != NULL)
3930 win_goto(foundfr->fr_win);
3931}
3932#endif
3933
3934/*
3935 * Make window "wp" the current window.
3936 */
3937 void
3938win_enter(wp, undo_sync)
3939 win_T *wp;
3940 int undo_sync;
3941{
3942 win_enter_ext(wp, undo_sync, FALSE);
3943}
3944
3945/*
3946 * Make window wp the current window.
3947 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3948 * been closed and isn't valid.
3949 */
3950 static void
3951win_enter_ext(wp, undo_sync, curwin_invalid)
3952 win_T *wp;
3953 int undo_sync;
3954 int curwin_invalid;
3955{
3956#ifdef FEAT_AUTOCMD
3957 int other_buffer = FALSE;
3958#endif
3959
3960 if (wp == curwin && !curwin_invalid) /* nothing to do */
3961 return;
3962
3963#ifdef FEAT_AUTOCMD
3964 if (!curwin_invalid)
3965 {
3966 /*
3967 * Be careful: If autocommands delete the window, return now.
3968 */
3969 if (wp->w_buffer != curbuf)
3970 {
3971 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3972 other_buffer = TRUE;
3973 if (!win_valid(wp))
3974 return;
3975 }
3976 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3977 if (!win_valid(wp))
3978 return;
3979# ifdef FEAT_EVAL
3980 /* autocmds may abort script processing */
3981 if (aborting())
3982 return;
3983# endif
3984 }
3985#endif
3986
3987 /* sync undo before leaving the current buffer */
3988 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003989 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 /* may have to copy the buffer options when 'cpo' contains 'S' */
3991 if (wp->w_buffer != curbuf)
3992 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3993 if (!curwin_invalid)
3994 {
3995 prevwin = curwin; /* remember for CTRL-W p */
3996 curwin->w_redr_status = TRUE;
3997 }
3998 curwin = wp;
3999 curbuf = wp->w_buffer;
4000 check_cursor();
4001#ifdef FEAT_VIRTUALEDIT
4002 if (!virtual_active())
4003 curwin->w_cursor.coladd = 0;
4004#endif
4005 changed_line_abv_curs(); /* assume cursor position needs updating */
4006
4007 if (curwin->w_localdir != NULL)
4008 {
4009 /* Window has a local directory: Save current directory as global
4010 * directory (unless that was done already) and change to the local
4011 * directory. */
4012 if (globaldir == NULL)
4013 {
4014 char_u cwd[MAXPATHL];
4015
4016 if (mch_dirname(cwd, MAXPATHL) == OK)
4017 globaldir = vim_strsave(cwd);
4018 }
4019 mch_chdir((char *)curwin->w_localdir);
4020 shorten_fnames(TRUE);
4021 }
4022 else if (globaldir != NULL)
4023 {
4024 /* Window doesn't have a local directory and we are not in the global
4025 * directory: Change to the global directory. */
4026 mch_chdir((char *)globaldir);
4027 vim_free(globaldir);
4028 globaldir = NULL;
4029 shorten_fnames(TRUE);
4030 }
4031
4032#ifdef FEAT_AUTOCMD
4033 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4034 if (other_buffer)
4035 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4036#endif
4037
4038#ifdef FEAT_TITLE
4039 maketitle();
4040#endif
4041 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004042 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (restart_edit)
4044 redraw_later(VALID); /* causes status line redraw */
4045
4046 /* set window height to desired minimal value */
4047 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4048 win_setheight((int)p_wh);
4049 else if (curwin->w_height == 0)
4050 win_setheight(1);
4051
4052#ifdef FEAT_VERTSPLIT
4053 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004054 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 win_setwidth((int)p_wiw);
4056#endif
4057
4058#ifdef FEAT_MOUSE
4059 setmouse(); /* in case jumped to/from help buffer */
4060#endif
4061
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004062 /* Change directories when the 'acd' option is set. */
4063 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064}
4065
4066#endif /* FEAT_WINDOWS */
4067
4068#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4069/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004070 * Jump to the first open window that contains buffer "buf", if one exists.
4071 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 */
4073 win_T *
4074buf_jump_open_win(buf)
4075 buf_T *buf;
4076{
4077# ifdef FEAT_WINDOWS
4078 win_T *wp;
4079
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004080 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (wp->w_buffer == buf)
4082 break;
4083 if (wp != NULL)
4084 win_enter(wp, FALSE);
4085 return wp;
4086# else
4087 if (curwin->w_buffer == buf)
4088 return curwin;
4089 return NULL;
4090# endif
4091}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004092
4093/*
4094 * Jump to the first open window in any tab page that contains buffer "buf",
4095 * if one exists.
4096 * Returns a pointer to the window found, otherwise NULL.
4097 */
4098 win_T *
4099buf_jump_open_tab(buf)
4100 buf_T *buf;
4101{
4102# ifdef FEAT_WINDOWS
4103 win_T *wp;
4104 tabpage_T *tp;
4105
4106 /* First try the current tab page. */
4107 wp = buf_jump_open_win(buf);
4108 if (wp != NULL)
4109 return wp;
4110
4111 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4112 if (tp != curtab)
4113 {
4114 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4115 if (wp->w_buffer == buf)
4116 break;
4117 if (wp != NULL)
4118 {
4119 goto_tabpage_win(tp, wp);
4120 if (curwin != wp)
4121 wp = NULL; /* something went wrong */
4122 break;
4123 }
4124 }
4125
4126 return wp;
4127# else
4128 if (curwin->w_buffer == buf)
4129 return curwin;
4130 return NULL;
4131# endif
4132}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#endif
4134
4135/*
4136 * allocate a window structure and link it in the window list
4137 */
4138/*ARGSUSED*/
4139 static win_T *
4140win_alloc(after)
4141 win_T *after;
4142{
4143 win_T *newwin;
4144
4145 /*
4146 * allocate window structure and linesizes arrays
4147 */
4148 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4149 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4150 {
4151 vim_free(newwin);
4152 newwin = NULL;
4153 }
4154
4155 if (newwin != NULL)
4156 {
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004157#ifdef FEAT_AUTOCMD
4158 /* Don't execute autocommands while the window is not properly
4159 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4160 * event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004161 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 /*
4164 * link the window in the window list
4165 */
4166#ifdef FEAT_WINDOWS
4167 win_append(after, newwin);
4168#endif
4169#ifdef FEAT_VERTSPLIT
4170 newwin->w_wincol = 0;
4171 newwin->w_width = Columns;
4172#endif
4173
4174 /* position the display and the cursor at the top of the file. */
4175 newwin->w_topline = 1;
4176#ifdef FEAT_DIFF
4177 newwin->w_topfill = 0;
4178#endif
4179 newwin->w_botline = 2;
4180 newwin->w_cursor.lnum = 1;
4181#ifdef FEAT_SCROLLBIND
4182 newwin->w_scbind_pos = 1;
4183#endif
4184
4185 /* We won't calculate w_fraction until resizing the window */
4186 newwin->w_fraction = 0;
4187 newwin->w_prev_fraction_row = -1;
4188
4189#ifdef FEAT_GUI
4190 if (gui.in_use)
4191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4193 SBAR_LEFT, newwin);
4194 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4195 SBAR_RIGHT, newwin);
4196 }
4197#endif
4198#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004199 /* init w: variables */
4200 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#endif
4202#ifdef FEAT_FOLDING
4203 foldInitWin(newwin);
4204#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004205#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004206 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004207#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004208#ifdef FEAT_SEARCH_EXTRA
4209 newwin->w_match_head = NULL;
4210 newwin->w_next_match_id = 4;
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 return newwin;
4214}
4215
4216#if defined(FEAT_WINDOWS) || defined(PROTO)
4217
4218/*
4219 * remove window 'wp' from the window list and free the structure
4220 */
4221 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004222win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004224 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int i;
4227
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004228#ifdef FEAT_AUTOCMD
4229 /* Don't execute autocommands while the window is halfway being deleted.
4230 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004231 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004232#endif
4233
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004234#ifdef FEAT_MZSCHEME
4235 mzscheme_window_free(wp);
4236#endif
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#ifdef FEAT_PERL
4239 perl_win_free(wp);
4240#endif
4241
4242#ifdef FEAT_PYTHON
4243 python_window_free(wp);
4244#endif
4245
4246#ifdef FEAT_TCL
4247 tcl_window_free(wp);
4248#endif
4249
4250#ifdef FEAT_RUBY
4251 ruby_window_free(wp);
4252#endif
4253
4254 clear_winopt(&wp->w_onebuf_opt);
4255 clear_winopt(&wp->w_allbuf_opt);
4256
4257#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004258 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#endif
4260
4261 if (prevwin == wp)
4262 prevwin = NULL;
4263 win_free_lsize(wp);
4264
4265 for (i = 0; i < wp->w_tagstacklen; ++i)
4266 vim_free(wp->w_tagstack[i].tagname);
4267
4268 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004271 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274#ifdef FEAT_JUMPLIST
4275 free_jumplist(wp);
4276#endif
4277
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004278#ifdef FEAT_QUICKFIX
4279 qf_free_all(wp);
4280#endif
4281
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282#ifdef FEAT_GUI
4283 if (gui.in_use)
4284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4286 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4287 }
4288#endif /* FEAT_GUI */
4289
Bram Moolenaarf740b292006-02-16 22:11:02 +00004290 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004292
4293#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004294 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296}
4297
4298/*
4299 * Append window "wp" in the window list after window "after".
4300 */
4301 static void
4302win_append(after, wp)
4303 win_T *after, *wp;
4304{
4305 win_T *before;
4306
4307 if (after == NULL) /* after NULL is in front of the first */
4308 before = firstwin;
4309 else
4310 before = after->w_next;
4311
4312 wp->w_next = before;
4313 wp->w_prev = after;
4314 if (after == NULL)
4315 firstwin = wp;
4316 else
4317 after->w_next = wp;
4318 if (before == NULL)
4319 lastwin = wp;
4320 else
4321 before->w_prev = wp;
4322}
4323
4324/*
4325 * Remove a window from the window list.
4326 */
4327 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004328win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004330 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331{
4332 if (wp->w_prev != NULL)
4333 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004334 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004336 else
4337 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 if (wp->w_next != NULL)
4339 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004340 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004342 else
4343 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344}
4345
4346/*
4347 * Append frame "frp" in a frame list after frame "after".
4348 */
4349 static void
4350frame_append(after, frp)
4351 frame_T *after, *frp;
4352{
4353 frp->fr_next = after->fr_next;
4354 after->fr_next = frp;
4355 if (frp->fr_next != NULL)
4356 frp->fr_next->fr_prev = frp;
4357 frp->fr_prev = after;
4358}
4359
4360/*
4361 * Insert frame "frp" in a frame list before frame "before".
4362 */
4363 static void
4364frame_insert(before, frp)
4365 frame_T *before, *frp;
4366{
4367 frp->fr_next = before;
4368 frp->fr_prev = before->fr_prev;
4369 before->fr_prev = frp;
4370 if (frp->fr_prev != NULL)
4371 frp->fr_prev->fr_next = frp;
4372 else
4373 frp->fr_parent->fr_child = frp;
4374}
4375
4376/*
4377 * Remove a frame from a frame list.
4378 */
4379 static void
4380frame_remove(frp)
4381 frame_T *frp;
4382{
4383 if (frp->fr_prev != NULL)
4384 frp->fr_prev->fr_next = frp->fr_next;
4385 else
4386 frp->fr_parent->fr_child = frp->fr_next;
4387 if (frp->fr_next != NULL)
4388 frp->fr_next->fr_prev = frp->fr_prev;
4389}
4390
4391#endif /* FEAT_WINDOWS */
4392
4393/*
4394 * Allocate w_lines[] for window "wp".
4395 * Return FAIL for failure, OK for success.
4396 */
4397 int
4398win_alloc_lines(wp)
4399 win_T *wp;
4400{
4401 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004402 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (wp->w_lines == NULL)
4404 return FAIL;
4405 return OK;
4406}
4407
4408/*
4409 * free lsize arrays for a window
4410 */
4411 void
4412win_free_lsize(wp)
4413 win_T *wp;
4414{
4415 vim_free(wp->w_lines);
4416 wp->w_lines = NULL;
4417}
4418
4419/*
4420 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004421 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 */
4423 void
4424shell_new_rows()
4425{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004426 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428 if (firstwin == NULL) /* not initialized yet */
4429 return;
4430#ifdef FEAT_WINDOWS
4431 if (h < frame_minheight(topframe, NULL))
4432 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004433
4434 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * that doesn't result in the right height, forget about that option. */
4436 frame_new_height(topframe, h, FALSE, TRUE);
4437 if (topframe->fr_height != h)
4438 frame_new_height(topframe, h, FALSE, FALSE);
4439
4440 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4441#else
4442 if (h < 1)
4443 h = 1;
4444 win_new_height(firstwin, h);
4445#endif
4446 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004447#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004448 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449#endif
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#if 0
4452 /* Disabled: don't want making the screen smaller make a window larger. */
4453 if (p_ea)
4454 win_equal(curwin, FALSE, 'v');
4455#endif
4456}
4457
4458#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4459/*
4460 * Called from win_new_shellsize() after Columns changed.
4461 */
4462 void
4463shell_new_columns()
4464{
4465 if (firstwin == NULL) /* not initialized yet */
4466 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004467
4468 /* First try setting the widths of windows with 'winfixwidth'. If that
4469 * doesn't result in the right width, forget about that option. */
4470 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4471 if (topframe->fr_width != Columns)
4472 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4475#if 0
4476 /* Disabled: don't want making the screen smaller make a window larger. */
4477 if (p_ea)
4478 win_equal(curwin, FALSE, 'h');
4479#endif
4480}
4481#endif
4482
4483#if defined(FEAT_CMDWIN) || defined(PROTO)
4484/*
4485 * Save the size of all windows in "gap".
4486 */
4487 void
4488win_size_save(gap)
4489 garray_T *gap;
4490
4491{
4492 win_T *wp;
4493
4494 ga_init2(gap, (int)sizeof(int), 1);
4495 if (ga_grow(gap, win_count() * 2) == OK)
4496 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4497 {
4498 ((int *)gap->ga_data)[gap->ga_len++] =
4499 wp->w_width + wp->w_vsep_width;
4500 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4501 }
4502}
4503
4504/*
4505 * Restore window sizes, but only if the number of windows is still the same.
4506 * Does not free the growarray.
4507 */
4508 void
4509win_size_restore(gap)
4510 garray_T *gap;
4511{
4512 win_T *wp;
4513 int i;
4514
4515 if (win_count() * 2 == gap->ga_len)
4516 {
4517 i = 0;
4518 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4519 {
4520 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4521 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4522 }
4523 /* recompute the window positions */
4524 (void)win_comp_pos();
4525 }
4526}
4527#endif /* FEAT_CMDWIN */
4528
4529#if defined(FEAT_WINDOWS) || defined(PROTO)
4530/*
4531 * Update the position for all windows, using the width and height of the
4532 * frames.
4533 * Returns the row just after the last window.
4534 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004535 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536win_comp_pos()
4537{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004538 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 int col = 0;
4540
4541 frame_comp_pos(topframe, &row, &col);
4542 return row;
4543}
4544
4545/*
4546 * Update the position of the windows in frame "topfrp", using the width and
4547 * height of the frames.
4548 * "*row" and "*col" are the top-left position of the frame. They are updated
4549 * to the bottom-right position plus one.
4550 */
4551 static void
4552frame_comp_pos(topfrp, row, col)
4553 frame_T *topfrp;
4554 int *row;
4555 int *col;
4556{
4557 win_T *wp;
4558 frame_T *frp;
4559#ifdef FEAT_VERTSPLIT
4560 int startcol;
4561 int startrow;
4562#endif
4563
4564 wp = topfrp->fr_win;
4565 if (wp != NULL)
4566 {
4567 if (wp->w_winrow != *row
4568#ifdef FEAT_VERTSPLIT
4569 || wp->w_wincol != *col
4570#endif
4571 )
4572 {
4573 /* position changed, redraw */
4574 wp->w_winrow = *row;
4575#ifdef FEAT_VERTSPLIT
4576 wp->w_wincol = *col;
4577#endif
4578 redraw_win_later(wp, NOT_VALID);
4579 wp->w_redr_status = TRUE;
4580 }
4581 *row += wp->w_height + wp->w_status_height;
4582#ifdef FEAT_VERTSPLIT
4583 *col += wp->w_width + wp->w_vsep_width;
4584#endif
4585 }
4586 else
4587 {
4588#ifdef FEAT_VERTSPLIT
4589 startrow = *row;
4590 startcol = *col;
4591#endif
4592 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4593 {
4594#ifdef FEAT_VERTSPLIT
4595 if (topfrp->fr_layout == FR_ROW)
4596 *row = startrow; /* all frames are at the same row */
4597 else
4598 *col = startcol; /* all frames are at the same col */
4599#endif
4600 frame_comp_pos(frp, row, col);
4601 }
4602 }
4603}
4604
4605#endif /* FEAT_WINDOWS */
4606
4607/*
4608 * Set current window height and take care of repositioning other windows to
4609 * fit around it.
4610 */
4611 void
4612win_setheight(height)
4613 int height;
4614{
4615 win_setheight_win(height, curwin);
4616}
4617
4618/*
4619 * Set the window height of window "win" and take care of repositioning other
4620 * windows to fit around it.
4621 */
4622 void
4623win_setheight_win(height, win)
4624 int height;
4625 win_T *win;
4626{
4627 int row;
4628
4629 if (win == curwin)
4630 {
4631 /* Always keep current window at least one line high, even when
4632 * 'winminheight' is zero. */
4633#ifdef FEAT_WINDOWS
4634 if (height < p_wmh)
4635 height = p_wmh;
4636#endif
4637 if (height == 0)
4638 height = 1;
4639 }
4640
4641#ifdef FEAT_WINDOWS
4642 frame_setheight(win->w_frame, height + win->w_status_height);
4643
4644 /* recompute the window positions */
4645 row = win_comp_pos();
4646#else
4647 if (height > topframe->fr_height)
4648 height = topframe->fr_height;
4649 win->w_height = height;
4650 row = height;
4651#endif
4652
4653 /*
4654 * If there is extra space created between the last window and the command
4655 * line, clear it.
4656 */
4657 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4658 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4659 cmdline_row = row;
4660 msg_row = row;
4661 msg_col = 0;
4662
4663 redraw_all_later(NOT_VALID);
4664}
4665
4666#if defined(FEAT_WINDOWS) || defined(PROTO)
4667
4668/*
4669 * Set the height of a frame to "height" and take care that all frames and
4670 * windows inside it are resized. Also resize frames on the left and right if
4671 * the are in the same FR_ROW frame.
4672 *
4673 * Strategy:
4674 * If the frame is part of a FR_COL frame, try fitting the frame in that
4675 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4676 * go to containing frames to resize them and make room.
4677 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4678 * Check for the minimal height of the FR_ROW frame.
4679 * At the top level we can also use change the command line height.
4680 */
4681 static void
4682frame_setheight(curfrp, height)
4683 frame_T *curfrp;
4684 int height;
4685{
4686 int room; /* total number of lines available */
4687 int take; /* number of lines taken from other windows */
4688 int room_cmdline; /* lines available from cmdline */
4689 int run;
4690 frame_T *frp;
4691 int h;
4692 int room_reserved;
4693
4694 /* If the height already is the desired value, nothing to do. */
4695 if (curfrp->fr_height == height)
4696 return;
4697
4698 if (curfrp->fr_parent == NULL)
4699 {
4700 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004701 if (height > ROWS_AVAIL)
4702 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (height > 0)
4704 frame_new_height(curfrp, height, FALSE, FALSE);
4705 }
4706 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4707 {
4708 /* Row of frames: Also need to resize frames left and right of this
4709 * one. First check for the minimal height of these. */
4710 h = frame_minheight(curfrp->fr_parent, NULL);
4711 if (height < h)
4712 height = h;
4713 frame_setheight(curfrp->fr_parent, height);
4714 }
4715 else
4716 {
4717 /*
4718 * Column of frames: try to change only frames in this column.
4719 */
4720#ifdef FEAT_VERTSPLIT
4721 /*
4722 * Do this twice:
4723 * 1: compute room available, if it's not enough try resizing the
4724 * containing frame.
4725 * 2: compute the room available and adjust the height to it.
4726 * Try not to reduce the height of a window with 'winfixheight' set.
4727 */
4728 for (run = 1; run <= 2; ++run)
4729#else
4730 for (;;)
4731#endif
4732 {
4733 room = 0;
4734 room_reserved = 0;
4735 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4736 frp = frp->fr_next)
4737 {
4738 if (frp != curfrp
4739 && frp->fr_win != NULL
4740 && frp->fr_win->w_p_wfh)
4741 room_reserved += frp->fr_height;
4742 room += frp->fr_height;
4743 if (frp != curfrp)
4744 room -= frame_minheight(frp, NULL);
4745 }
4746#ifdef FEAT_VERTSPLIT
4747 if (curfrp->fr_width != Columns)
4748 room_cmdline = 0;
4749 else
4750#endif
4751 {
4752 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4753 + lastwin->w_height + lastwin->w_status_height);
4754 if (room_cmdline < 0)
4755 room_cmdline = 0;
4756 }
4757
4758 if (height <= room + room_cmdline)
4759 break;
4760#ifdef FEAT_VERTSPLIT
4761 if (run == 2 || curfrp->fr_width == Columns)
4762#endif
4763 {
4764 if (height > room + room_cmdline)
4765 height = room + room_cmdline;
4766 break;
4767 }
4768#ifdef FEAT_VERTSPLIT
4769 frame_setheight(curfrp->fr_parent, height
4770 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4771#endif
4772 /*NOTREACHED*/
4773 }
4774
4775 /*
4776 * Compute the number of lines we will take from others frames (can be
4777 * negative!).
4778 */
4779 take = height - curfrp->fr_height;
4780
4781 /* If there is not enough room, also reduce the height of a window
4782 * with 'winfixheight' set. */
4783 if (height > room + room_cmdline - room_reserved)
4784 room_reserved = room + room_cmdline - height;
4785 /* If there is only a 'winfixheight' window and making the
4786 * window smaller, need to make the other window taller. */
4787 if (take < 0 && room - curfrp->fr_height < room_reserved)
4788 room_reserved = 0;
4789
4790 if (take > 0 && room_cmdline > 0)
4791 {
4792 /* use lines from cmdline first */
4793 if (take < room_cmdline)
4794 room_cmdline = take;
4795 take -= room_cmdline;
4796 topframe->fr_height += room_cmdline;
4797 }
4798
4799 /*
4800 * set the current frame to the new height
4801 */
4802 frame_new_height(curfrp, height, FALSE, FALSE);
4803
4804 /*
4805 * First take lines from the frames after the current frame. If
4806 * that is not enough, takes lines from frames above the current
4807 * frame.
4808 */
4809 for (run = 0; run < 2; ++run)
4810 {
4811 if (run == 0)
4812 frp = curfrp->fr_next; /* 1st run: start with next window */
4813 else
4814 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4815 while (frp != NULL && take != 0)
4816 {
4817 h = frame_minheight(frp, NULL);
4818 if (room_reserved > 0
4819 && frp->fr_win != NULL
4820 && frp->fr_win->w_p_wfh)
4821 {
4822 if (room_reserved >= frp->fr_height)
4823 room_reserved -= frp->fr_height;
4824 else
4825 {
4826 if (frp->fr_height - room_reserved > take)
4827 room_reserved = frp->fr_height - take;
4828 take -= frp->fr_height - room_reserved;
4829 frame_new_height(frp, room_reserved, FALSE, FALSE);
4830 room_reserved = 0;
4831 }
4832 }
4833 else
4834 {
4835 if (frp->fr_height - take < h)
4836 {
4837 take -= frp->fr_height - h;
4838 frame_new_height(frp, h, FALSE, FALSE);
4839 }
4840 else
4841 {
4842 frame_new_height(frp, frp->fr_height - take,
4843 FALSE, FALSE);
4844 take = 0;
4845 }
4846 }
4847 if (run == 0)
4848 frp = frp->fr_next;
4849 else
4850 frp = frp->fr_prev;
4851 }
4852 }
4853 }
4854}
4855
4856#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4857/*
4858 * Set current window width and take care of repositioning other windows to
4859 * fit around it.
4860 */
4861 void
4862win_setwidth(width)
4863 int width;
4864{
4865 win_setwidth_win(width, curwin);
4866}
4867
4868 void
4869win_setwidth_win(width, wp)
4870 int width;
4871 win_T *wp;
4872{
4873 /* Always keep current window at least one column wide, even when
4874 * 'winminwidth' is zero. */
4875 if (wp == curwin)
4876 {
4877 if (width < p_wmw)
4878 width = p_wmw;
4879 if (width == 0)
4880 width = 1;
4881 }
4882
4883 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4884
4885 /* recompute the window positions */
4886 (void)win_comp_pos();
4887
4888 redraw_all_later(NOT_VALID);
4889}
4890
4891/*
4892 * Set the width of a frame to "width" and take care that all frames and
4893 * windows inside it are resized. Also resize frames above and below if the
4894 * are in the same FR_ROW frame.
4895 *
4896 * Strategy is similar to frame_setheight().
4897 */
4898 static void
4899frame_setwidth(curfrp, width)
4900 frame_T *curfrp;
4901 int width;
4902{
4903 int room; /* total number of lines available */
4904 int take; /* number of lines taken from other windows */
4905 int run;
4906 frame_T *frp;
4907 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004908 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 /* If the width already is the desired value, nothing to do. */
4911 if (curfrp->fr_width == width)
4912 return;
4913
4914 if (curfrp->fr_parent == NULL)
4915 /* topframe: can't change width */
4916 return;
4917
4918 if (curfrp->fr_parent->fr_layout == FR_COL)
4919 {
4920 /* Column of frames: Also need to resize frames above and below of
4921 * this one. First check for the minimal width of these. */
4922 w = frame_minwidth(curfrp->fr_parent, NULL);
4923 if (width < w)
4924 width = w;
4925 frame_setwidth(curfrp->fr_parent, width);
4926 }
4927 else
4928 {
4929 /*
4930 * Row of frames: try to change only frames in this row.
4931 *
4932 * Do this twice:
4933 * 1: compute room available, if it's not enough try resizing the
4934 * containing frame.
4935 * 2: compute the room available and adjust the width to it.
4936 */
4937 for (run = 1; run <= 2; ++run)
4938 {
4939 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004940 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4942 frp = frp->fr_next)
4943 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004944 if (frp != curfrp
4945 && frp->fr_win != NULL
4946 && frp->fr_win->w_p_wfw)
4947 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 room += frp->fr_width;
4949 if (frp != curfrp)
4950 room -= frame_minwidth(frp, NULL);
4951 }
4952
4953 if (width <= room)
4954 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004955 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 if (width > room)
4958 width = room;
4959 break;
4960 }
4961 frame_setwidth(curfrp->fr_parent, width
4962 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4963 }
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 /*
4966 * Compute the number of lines we will take from others frames (can be
4967 * negative!).
4968 */
4969 take = width - curfrp->fr_width;
4970
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004971 /* If there is not enough room, also reduce the width of a window
4972 * with 'winfixwidth' set. */
4973 if (width > room - room_reserved)
4974 room_reserved = room - width;
4975 /* If there is only a 'winfixwidth' window and making the
4976 * window smaller, need to make the other window narrower. */
4977 if (take < 0 && room - curfrp->fr_width < room_reserved)
4978 room_reserved = 0;
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 /*
4981 * set the current frame to the new width
4982 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004983 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
4985 /*
4986 * First take lines from the frames right of the current frame. If
4987 * that is not enough, takes lines from frames left of the current
4988 * frame.
4989 */
4990 for (run = 0; run < 2; ++run)
4991 {
4992 if (run == 0)
4993 frp = curfrp->fr_next; /* 1st run: start with next window */
4994 else
4995 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4996 while (frp != NULL && take != 0)
4997 {
4998 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004999 if (room_reserved > 0
5000 && frp->fr_win != NULL
5001 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005003 if (room_reserved >= frp->fr_width)
5004 room_reserved -= frp->fr_width;
5005 else
5006 {
5007 if (frp->fr_width - room_reserved > take)
5008 room_reserved = frp->fr_width - take;
5009 take -= frp->fr_width - room_reserved;
5010 frame_new_width(frp, room_reserved, FALSE, FALSE);
5011 room_reserved = 0;
5012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else
5015 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005016 if (frp->fr_width - take < w)
5017 {
5018 take -= frp->fr_width - w;
5019 frame_new_width(frp, w, FALSE, FALSE);
5020 }
5021 else
5022 {
5023 frame_new_width(frp, frp->fr_width - take,
5024 FALSE, FALSE);
5025 take = 0;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 if (run == 0)
5029 frp = frp->fr_next;
5030 else
5031 frp = frp->fr_prev;
5032 }
5033 }
5034 }
5035}
5036#endif /* FEAT_VERTSPLIT */
5037
5038/*
5039 * Check 'winminheight' for a valid value.
5040 */
5041 void
5042win_setminheight()
5043{
5044 int room;
5045 int first = TRUE;
5046 win_T *wp;
5047
5048 /* loop until there is a 'winminheight' that is possible */
5049 while (p_wmh > 0)
5050 {
5051 /* TODO: handle vertical splits */
5052 room = -p_wh;
5053 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5054 room += wp->w_height - p_wmh;
5055 if (room >= 0)
5056 break;
5057 --p_wmh;
5058 if (first)
5059 {
5060 EMSG(_(e_noroom));
5061 first = FALSE;
5062 }
5063 }
5064}
5065
5066#ifdef FEAT_MOUSE
5067
5068/*
5069 * Status line of dragwin is dragged "offset" lines down (negative is up).
5070 */
5071 void
5072win_drag_status_line(dragwin, offset)
5073 win_T *dragwin;
5074 int offset;
5075{
5076 frame_T *curfr;
5077 frame_T *fr;
5078 int room;
5079 int row;
5080 int up; /* if TRUE, drag status line up, otherwise down */
5081 int n;
5082
5083 fr = dragwin->w_frame;
5084 curfr = fr;
5085 if (fr != topframe) /* more than one window */
5086 {
5087 fr = fr->fr_parent;
5088 /* When the parent frame is not a column of frames, its parent should
5089 * be. */
5090 if (fr->fr_layout != FR_COL)
5091 {
5092 curfr = fr;
5093 if (fr != topframe) /* only a row of windows, may drag statusline */
5094 fr = fr->fr_parent;
5095 }
5096 }
5097
5098 /* If this is the last frame in a column, may want to resize the parent
5099 * frame instead (go two up to skip a row of frames). */
5100 while (curfr != topframe && curfr->fr_next == NULL)
5101 {
5102 if (fr != topframe)
5103 fr = fr->fr_parent;
5104 curfr = fr;
5105 if (fr != topframe)
5106 fr = fr->fr_parent;
5107 }
5108
5109 if (offset < 0) /* drag up */
5110 {
5111 up = TRUE;
5112 offset = -offset;
5113 /* sum up the room of the current frame and above it */
5114 if (fr == curfr)
5115 {
5116 /* only one window */
5117 room = fr->fr_height - frame_minheight(fr, NULL);
5118 }
5119 else
5120 {
5121 room = 0;
5122 for (fr = fr->fr_child; ; fr = fr->fr_next)
5123 {
5124 room += fr->fr_height - frame_minheight(fr, NULL);
5125 if (fr == curfr)
5126 break;
5127 }
5128 }
5129 fr = curfr->fr_next; /* put fr at frame that grows */
5130 }
5131 else /* drag down */
5132 {
5133 up = FALSE;
5134 /*
5135 * Only dragging the last status line can reduce p_ch.
5136 */
5137 room = Rows - cmdline_row;
5138 if (curfr->fr_next == NULL)
5139 room -= 1;
5140 else
5141 room -= p_ch;
5142 if (room < 0)
5143 room = 0;
5144 /* sum up the room of frames below of the current one */
5145 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5146 room += fr->fr_height - frame_minheight(fr, NULL);
5147 fr = curfr; /* put fr at window that grows */
5148 }
5149
5150 if (room < offset) /* Not enough room */
5151 offset = room; /* Move as far as we can */
5152 if (offset <= 0)
5153 return;
5154
5155 /*
5156 * Grow frame fr by "offset" lines.
5157 * Doesn't happen when dragging the last status line up.
5158 */
5159 if (fr != NULL)
5160 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5161
5162 if (up)
5163 fr = curfr; /* current frame gets smaller */
5164 else
5165 fr = curfr->fr_next; /* next frame gets smaller */
5166
5167 /*
5168 * Now make the other frames smaller.
5169 */
5170 while (fr != NULL && offset > 0)
5171 {
5172 n = frame_minheight(fr, NULL);
5173 if (fr->fr_height - offset <= n)
5174 {
5175 offset -= fr->fr_height - n;
5176 frame_new_height(fr, n, !up, FALSE);
5177 }
5178 else
5179 {
5180 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5181 break;
5182 }
5183 if (up)
5184 fr = fr->fr_prev;
5185 else
5186 fr = fr->fr_next;
5187 }
5188 row = win_comp_pos();
5189 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5190 cmdline_row = row;
5191 p_ch = Rows - cmdline_row;
5192 if (p_ch < 1)
5193 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005194 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005195 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 showmode();
5197}
5198
5199#ifdef FEAT_VERTSPLIT
5200/*
5201 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5202 */
5203 void
5204win_drag_vsep_line(dragwin, offset)
5205 win_T *dragwin;
5206 int offset;
5207{
5208 frame_T *curfr;
5209 frame_T *fr;
5210 int room;
5211 int left; /* if TRUE, drag separator line left, otherwise right */
5212 int n;
5213
5214 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005215 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 return;
5217 curfr = fr;
5218 fr = fr->fr_parent;
5219 /* When the parent frame is not a row of frames, its parent should be. */
5220 if (fr->fr_layout != FR_ROW)
5221 {
5222 if (fr == topframe) /* only a column of windows (cannot happen?) */
5223 return;
5224 curfr = fr;
5225 fr = fr->fr_parent;
5226 }
5227
5228 /* If this is the last frame in a row, may want to resize a parent
5229 * frame instead. */
5230 while (curfr->fr_next == NULL)
5231 {
5232 if (fr == topframe)
5233 break;
5234 curfr = fr;
5235 fr = fr->fr_parent;
5236 if (fr != topframe)
5237 {
5238 curfr = fr;
5239 fr = fr->fr_parent;
5240 }
5241 }
5242
5243 if (offset < 0) /* drag left */
5244 {
5245 left = TRUE;
5246 offset = -offset;
5247 /* sum up the room of the current frame and left of it */
5248 room = 0;
5249 for (fr = fr->fr_child; ; fr = fr->fr_next)
5250 {
5251 room += fr->fr_width - frame_minwidth(fr, NULL);
5252 if (fr == curfr)
5253 break;
5254 }
5255 fr = curfr->fr_next; /* put fr at frame that grows */
5256 }
5257 else /* drag right */
5258 {
5259 left = FALSE;
5260 /* sum up the room of frames right of the current one */
5261 room = 0;
5262 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5263 room += fr->fr_width - frame_minwidth(fr, NULL);
5264 fr = curfr; /* put fr at window that grows */
5265 }
5266
5267 if (room < offset) /* Not enough room */
5268 offset = room; /* Move as far as we can */
5269 if (offset <= 0) /* No room at all, quit. */
5270 return;
5271
5272 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005273 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 /* shrink other frames: current and at the left or at the right */
5276 if (left)
5277 fr = curfr; /* current frame gets smaller */
5278 else
5279 fr = curfr->fr_next; /* next frame gets smaller */
5280
5281 while (fr != NULL && offset > 0)
5282 {
5283 n = frame_minwidth(fr, NULL);
5284 if (fr->fr_width - offset <= n)
5285 {
5286 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005287 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289 else
5290 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005291 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 break;
5293 }
5294 if (left)
5295 fr = fr->fr_prev;
5296 else
5297 fr = fr->fr_next;
5298 }
5299 (void)win_comp_pos();
5300 redraw_all_later(NOT_VALID);
5301}
5302#endif /* FEAT_VERTSPLIT */
5303#endif /* FEAT_MOUSE */
5304
5305#endif /* FEAT_WINDOWS */
5306
5307/*
5308 * Set the height of a window.
5309 * This takes care of the things inside the window, not what happens to the
5310 * window position, the frame or to other windows.
5311 */
5312 static void
5313win_new_height(wp, height)
5314 win_T *wp;
5315 int height;
5316{
5317 linenr_T lnum;
5318 int sline, line_size;
5319#define FRACTION_MULT 16384L
5320
5321 /* Don't want a negative height. Happens when splitting a tiny window.
5322 * Will equalize heights soon to fix it. */
5323 if (height < 0)
5324 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005325 if (wp->w_height == height)
5326 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5329 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5330 + FRACTION_MULT / 2) / (long)wp->w_height;
5331
5332 wp->w_height = height;
5333 wp->w_skipcol = 0;
5334
5335 /* Don't change w_topline when height is zero. Don't set w_topline when
5336 * 'scrollbind' is set and this isn't the current window. */
5337 if (height > 0
5338#ifdef FEAT_SCROLLBIND
5339 && (!wp->w_p_scb || wp == curwin)
5340#endif
5341 )
5342 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005343 /*
5344 * Find a value for w_topline that shows the cursor at the same
5345 * relative position in the window as before (more or less).
5346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 lnum = wp->w_cursor.lnum;
5348 if (lnum < 1) /* can happen when starting up */
5349 lnum = 1;
5350 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5351 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5352 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005353
5354 if (sline >= 0)
5355 {
5356 /* Make sure the whole cursor line is visible, if possible. */
5357 int rows = plines_win(wp, lnum, FALSE);
5358
5359 if (sline > wp->w_height - rows)
5360 {
5361 sline = wp->w_height - rows;
5362 wp->w_wrow -= rows - line_size;
5363 }
5364 }
5365
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (sline < 0)
5367 {
5368 /*
5369 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005370 * Make cursor line the first line in the window. If not enough
5371 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 */
5373 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005374 if (wp->w_wrow >= wp->w_height
5375 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5376 {
5377 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5378 --wp->w_wrow;
5379 while (wp->w_wrow >= wp->w_height)
5380 {
5381 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5382 + win_col_off2(wp);
5383 --wp->w_wrow;
5384 }
5385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387 else
5388 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005389 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391#ifdef FEAT_FOLDING
5392 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5393 if (lnum == 1)
5394 {
5395 /* first line in buffer is folded */
5396 line_size = 1;
5397 --sline;
5398 break;
5399 }
5400#endif
5401 --lnum;
5402#ifdef FEAT_DIFF
5403 if (lnum == wp->w_topline)
5404 line_size = plines_win_nofill(wp, lnum, TRUE)
5405 + wp->w_topfill;
5406 else
5407#endif
5408 line_size = plines_win(wp, lnum, TRUE);
5409 sline -= line_size;
5410 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (sline < 0)
5413 {
5414 /*
5415 * Line we want at top would go off top of screen. Use next
5416 * line instead.
5417 */
5418#ifdef FEAT_FOLDING
5419 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5420#endif
5421 lnum++;
5422 wp->w_wrow -= line_size + sline;
5423 }
5424 else if (sline > 0)
5425 {
5426 /* First line of file reached, use that as topline. */
5427 lnum = 1;
5428 wp->w_wrow -= sline;
5429 }
5430 }
5431 set_topline(wp, lnum);
5432 }
5433
5434 if (wp == curwin)
5435 {
5436 if (p_so)
5437 update_topline();
5438 curs_columns(FALSE); /* validate w_wrow */
5439 }
5440 wp->w_prev_fraction_row = wp->w_wrow;
5441
5442 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005443 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#ifdef FEAT_WINDOWS
5445 wp->w_redr_status = TRUE;
5446#endif
5447 invalidate_botline_win(wp);
5448}
5449
5450#ifdef FEAT_VERTSPLIT
5451/*
5452 * Set the width of a window.
5453 */
5454 static void
5455win_new_width(wp, width)
5456 win_T *wp;
5457 int width;
5458{
5459 wp->w_width = width;
5460 wp->w_lines_valid = 0;
5461 changed_line_abv_curs_win(wp);
5462 invalidate_botline_win(wp);
5463 if (wp == curwin)
5464 {
5465 update_topline();
5466 curs_columns(TRUE); /* validate w_wrow */
5467 }
5468 redraw_win_later(wp, NOT_VALID);
5469 wp->w_redr_status = TRUE;
5470}
5471#endif
5472
5473 void
5474win_comp_scroll(wp)
5475 win_T *wp;
5476{
5477 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5478 if (wp->w_p_scr == 0)
5479 wp->w_p_scr = 1;
5480}
5481
5482/*
5483 * command_height: called whenever p_ch has been changed
5484 */
5485 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005486command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
5488#ifdef FEAT_WINDOWS
5489 int h;
5490 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005491 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005493 /* Use the value of p_ch that we remembered. This is needed for when the
5494 * GUI starts up, we can't be sure in what order things happen. And when
5495 * p_ch was changed in another tab page. */
5496 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 /* Find bottom frame with width of screen. */
5499 frp = lastwin->w_frame;
5500# ifdef FEAT_VERTSPLIT
5501 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5502 frp = frp->fr_parent;
5503# endif
5504
5505 /* Avoid changing the height of a window with 'winfixheight' set. */
5506 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5507 && frp->fr_win->w_p_wfh)
5508 frp = frp->fr_prev;
5509
5510 if (starting != NO_SCREEN)
5511 {
5512 cmdline_row = Rows - p_ch;
5513
5514 if (p_ch > old_p_ch) /* p_ch got bigger */
5515 {
5516 while (p_ch > old_p_ch)
5517 {
5518 if (frp == NULL)
5519 {
5520 EMSG(_(e_noroom));
5521 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005522 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 cmdline_row = Rows - p_ch;
5524 break;
5525 }
5526 h = frp->fr_height - frame_minheight(frp, NULL);
5527 if (h > p_ch - old_p_ch)
5528 h = p_ch - old_p_ch;
5529 old_p_ch += h;
5530 frame_add_height(frp, -h);
5531 frp = frp->fr_prev;
5532 }
5533
5534 /* Recompute window positions. */
5535 (void)win_comp_pos();
5536
5537 /* clear the lines added to cmdline */
5538 if (full_screen)
5539 screen_fill((int)(cmdline_row), (int)Rows, 0,
5540 (int)Columns, ' ', ' ', 0);
5541 msg_row = cmdline_row;
5542 redraw_cmdline = TRUE;
5543 return;
5544 }
5545
5546 if (msg_row < cmdline_row)
5547 msg_row = cmdline_row;
5548 redraw_cmdline = TRUE;
5549 }
5550 frame_add_height(frp, (int)(old_p_ch - p_ch));
5551
5552 /* Recompute window positions. */
5553 if (frp != lastwin->w_frame)
5554 (void)win_comp_pos();
5555#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005557 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558#endif
5559}
5560
5561#if defined(FEAT_WINDOWS) || defined(PROTO)
5562/*
5563 * Resize frame "frp" to be "n" lines higher (negative for less high).
5564 * Also resize the frames it is contained in.
5565 */
5566 static void
5567frame_add_height(frp, n)
5568 frame_T *frp;
5569 int n;
5570{
5571 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5572 for (;;)
5573 {
5574 frp = frp->fr_parent;
5575 if (frp == NULL)
5576 break;
5577 frp->fr_height += n;
5578 }
5579}
5580
5581/*
5582 * Add or remove a status line for the bottom window(s), according to the
5583 * value of 'laststatus'.
5584 */
5585 void
5586last_status(morewin)
5587 int morewin; /* pretend there are two or more windows */
5588{
5589 /* Don't make a difference between horizontal or vertical split. */
5590 last_status_rec(topframe, (p_ls == 2
5591 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5592}
5593
5594 static void
5595last_status_rec(fr, statusline)
5596 frame_T *fr;
5597 int statusline;
5598{
5599 frame_T *fp;
5600 win_T *wp;
5601
5602 if (fr->fr_layout == FR_LEAF)
5603 {
5604 wp = fr->fr_win;
5605 if (wp->w_status_height != 0 && !statusline)
5606 {
5607 /* remove status line */
5608 win_new_height(wp, wp->w_height + 1);
5609 wp->w_status_height = 0;
5610 comp_col();
5611 }
5612 else if (wp->w_status_height == 0 && statusline)
5613 {
5614 /* Find a frame to take a line from. */
5615 fp = fr;
5616 while (fp->fr_height <= frame_minheight(fp, NULL))
5617 {
5618 if (fp == topframe)
5619 {
5620 EMSG(_(e_noroom));
5621 return;
5622 }
5623 /* In a column of frames: go to frame above. If already at
5624 * the top or in a row of frames: go to parent. */
5625 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5626 fp = fp->fr_prev;
5627 else
5628 fp = fp->fr_parent;
5629 }
5630 wp->w_status_height = 1;
5631 if (fp != fr)
5632 {
5633 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5634 frame_fix_height(wp);
5635 (void)win_comp_pos();
5636 }
5637 else
5638 win_new_height(wp, wp->w_height - 1);
5639 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005640 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 }
5643#ifdef FEAT_VERTSPLIT
5644 else if (fr->fr_layout == FR_ROW)
5645 {
5646 /* vertically split windows, set status line for each one */
5647 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5648 last_status_rec(fp, statusline);
5649 }
5650#endif
5651 else
5652 {
5653 /* horizontally split window, set status line for last one */
5654 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5655 ;
5656 last_status_rec(fp, statusline);
5657 }
5658}
5659
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005660/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005661 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005662 */
5663 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005664tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005665{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005666#ifdef FEAT_GUI_TABLINE
5667 /* When the GUI has the tabline then this always returns zero. */
5668 if (gui_use_tabline())
5669 return 0;
5670#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005671 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005672 {
5673 case 0: return 0;
5674 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5675 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005676 return 1;
5677}
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679#endif /* FEAT_WINDOWS */
5680
5681#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5682/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005683 * Get the file name at the cursor.
5684 * If Visual mode is active, use the selected text if it's in one line.
5685 * Returns the name in allocated memory, NULL for failure.
5686 */
5687 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005688grab_file_name(count, file_lnum)
5689 long count;
5690 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005691{
5692# ifdef FEAT_VISUAL
5693 if (VIsual_active)
5694 {
5695 int len;
5696 char_u *ptr;
5697
5698 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5699 return NULL;
5700 return find_file_name_in_path(ptr, len,
5701 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5702 }
5703# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005704 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5705 file_lnum);
5706
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005707}
5708
5709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 * Return the file name under or after the cursor.
5711 *
5712 * The 'path' option is searched if the file name is not absolute.
5713 * The string returned has been alloc'ed and should be freed by the caller.
5714 * NULL is returned if the file name or file is not found.
5715 *
5716 * options:
5717 * FNAME_MESS give error messages
5718 * FNAME_EXP expand to path
5719 * FNAME_HYP check for hypertext link
5720 * FNAME_INCL apply "includeexpr"
5721 */
5722 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005723file_name_at_cursor(options, count, file_lnum)
5724 int options;
5725 long count;
5726 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005729 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5730 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731}
5732
5733/*
5734 * Return the name of the file under or after ptr[col].
5735 * Otherwise like file_name_at_cursor().
5736 */
5737 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005738file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 char_u *line;
5740 int col;
5741 int options;
5742 long count;
5743 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005744 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 char_u *ptr;
5747 int len;
5748
5749 /*
5750 * search forward for what could be the start of a file name
5751 */
5752 ptr = line + col;
5753 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005754 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (*ptr == NUL) /* nothing found */
5756 {
5757 if (options & FNAME_MESS)
5758 EMSG(_("E446: No file name under cursor"));
5759 return NULL;
5760 }
5761
5762 /*
5763 * Search backward for first char of the file name.
5764 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5765 */
5766 while (ptr > line)
5767 {
5768#ifdef FEAT_MBYTE
5769 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5770 ptr -= len + 1;
5771 else
5772#endif
5773 if (vim_isfilec(ptr[-1])
5774 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5775 --ptr;
5776 else
5777 break;
5778 }
5779
5780 /*
5781 * Search forward for the last char of the file name.
5782 * Also allow "://" when ':' is not in 'isfname'.
5783 */
5784 len = 0;
5785 while (vim_isfilec(ptr[len])
5786 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5787#ifdef FEAT_MBYTE
5788 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005789 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
5791#endif
5792 ++len;
5793
5794 /*
5795 * If there is trailing punctuation, remove it.
5796 * But don't remove "..", could be a directory name.
5797 */
5798 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5799 && ptr[len - 2] != '.')
5800 --len;
5801
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005802 if (file_lnum != NULL)
5803 {
5804 char_u *p;
5805
5806 /* Get the number after the file name and a separator character */
5807 p = ptr + len;
5808 p = skipwhite(p);
5809 if (*p != NUL)
5810 {
5811 if (!isdigit(*p))
5812 ++p; /* skip the separator */
5813 p = skipwhite(p);
5814 if (isdigit(*p))
5815 *file_lnum = (int)getdigits(&p);
5816 }
5817 }
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5820}
5821
5822# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5823static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5824
5825 static char_u *
5826eval_includeexpr(ptr, len)
5827 char_u *ptr;
5828 int len;
5829{
5830 char_u *res;
5831
5832 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005833 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005834 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 set_vim_var_string(VV_FNAME, NULL, 0);
5836 return res;
5837}
5838#endif
5839
5840/*
5841 * Return the name of the file ptr[len] in 'path'.
5842 * Otherwise like file_name_at_cursor().
5843 */
5844 char_u *
5845find_file_name_in_path(ptr, len, options, count, rel_fname)
5846 char_u *ptr;
5847 int len;
5848 int options;
5849 long count;
5850 char_u *rel_fname; /* file we are searching relative to */
5851{
5852 char_u *file_name;
5853 int c;
5854# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5855 char_u *tofree = NULL;
5856
5857 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5858 {
5859 tofree = eval_includeexpr(ptr, len);
5860 if (tofree != NULL)
5861 {
5862 ptr = tofree;
5863 len = (int)STRLEN(ptr);
5864 }
5865 }
5866# endif
5867
5868 if (options & FNAME_EXP)
5869 {
5870 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5871 TRUE, rel_fname);
5872
5873# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5874 /*
5875 * If the file could not be found in a normal way, try applying
5876 * 'includeexpr' (unless done already).
5877 */
5878 if (file_name == NULL
5879 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5880 {
5881 tofree = eval_includeexpr(ptr, len);
5882 if (tofree != NULL)
5883 {
5884 ptr = tofree;
5885 len = (int)STRLEN(ptr);
5886 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5887 TRUE, rel_fname);
5888 }
5889 }
5890# endif
5891 if (file_name == NULL && (options & FNAME_MESS))
5892 {
5893 c = ptr[len];
5894 ptr[len] = NUL;
5895 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5896 ptr[len] = c;
5897 }
5898
5899 /* Repeat finding the file "count" times. This matters when it
5900 * appears several times in the path. */
5901 while (file_name != NULL && --count > 0)
5902 {
5903 vim_free(file_name);
5904 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5905 }
5906 }
5907 else
5908 file_name = vim_strnsave(ptr, len);
5909
5910# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5911 vim_free(tofree);
5912# endif
5913
5914 return file_name;
5915}
5916#endif /* FEAT_SEARCHPATH */
5917
5918/*
5919 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5920 * Also check for ":\\", which MS Internet Explorer accepts, return
5921 * URL_BACKSLASH.
5922 */
5923 static int
5924path_is_url(p)
5925 char_u *p;
5926{
5927 if (STRNCMP(p, "://", (size_t)3) == 0)
5928 return URL_SLASH;
5929 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5930 return URL_BACKSLASH;
5931 return 0;
5932}
5933
5934/*
5935 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5936 * Return URL_BACKSLASH for "name:\\".
5937 * Return zero otherwise.
5938 */
5939 int
5940path_with_url(fname)
5941 char_u *fname;
5942{
5943 char_u *p;
5944
5945 for (p = fname; isalpha(*p); ++p)
5946 ;
5947 return path_is_url(p);
5948}
5949
5950/*
5951 * Return TRUE if "name" is a full (absolute) path name or URL.
5952 */
5953 int
5954vim_isAbsName(name)
5955 char_u *name;
5956{
5957 return (path_with_url(name) != 0 || mch_isFullName(name));
5958}
5959
5960/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005961 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 *
5963 * return FAIL for failure, OK otherwise
5964 */
5965 int
5966vim_FullName(fname, buf, len, force)
5967 char_u *fname, *buf;
5968 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005969 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 int retval = OK;
5972 int url;
5973
5974 *buf = NUL;
5975 if (fname == NULL)
5976 return FAIL;
5977
5978 url = path_with_url(fname);
5979 if (!url)
5980 retval = mch_FullName(fname, buf, len, force);
5981 if (url || retval == FAIL)
5982 {
5983 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005984 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5987 slash_adjust(buf);
5988#endif
5989 return retval;
5990}
5991
5992/*
5993 * Return the minimal number of rows that is needed on the screen to display
5994 * the current number of windows.
5995 */
5996 int
5997min_rows()
5998{
5999 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006000#ifdef FEAT_WINDOWS
6001 tabpage_T *tp;
6002 int n;
6003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
6005 if (firstwin == NULL) /* not initialized yet */
6006 return MIN_LINES;
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006009 total = 0;
6010 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6011 {
6012 n = frame_minheight(tp->tp_topframe, NULL);
6013 if (total < n)
6014 total = n;
6015 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006016 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006018 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006020 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 return total;
6022}
6023
6024/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006025 * Return TRUE if there is only one window (in the current tab page), not
6026 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 */
6028 int
6029only_one_window()
6030{
6031#ifdef FEAT_WINDOWS
6032 int count = 0;
6033 win_T *wp;
6034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035 /* If there is another tab page there always is another window. */
6036 if (first_tabpage->tp_next != NULL)
6037 return FALSE;
6038
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00006040 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041# ifdef FEAT_QUICKFIX
6042 || wp->w_p_pvw
6043# endif
6044 ) || wp == curwin)
6045 ++count;
6046 return (count <= 1);
6047#else
6048 return TRUE;
6049#endif
6050}
6051
6052#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6053/*
6054 * Correct the cursor line number in other windows. Used after changing the
6055 * current buffer, and before applying autocommands.
6056 * When "do_curwin" is TRUE, also check current window.
6057 */
6058 void
6059check_lnums(do_curwin)
6060 int do_curwin;
6061{
6062 win_T *wp;
6063
6064#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006065 tabpage_T *tp;
6066
6067 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6069#else
6070 wp = curwin;
6071 if (do_curwin)
6072#endif
6073 {
6074 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6075 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6076 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6077 wp->w_topline = curbuf->b_ml.ml_line_count;
6078 }
6079}
6080#endif
6081
6082#if defined(FEAT_WINDOWS) || defined(PROTO)
6083
6084/*
6085 * A snapshot of the window sizes, to restore them after closing the help
6086 * window.
6087 * Only these fields are used:
6088 * fr_layout
6089 * fr_width
6090 * fr_height
6091 * fr_next
6092 * fr_child
6093 * fr_win (only valid for the old curwin, NULL otherwise)
6094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096/*
6097 * Create a snapshot of the current frame sizes.
6098 */
6099 static void
6100make_snapshot()
6101{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006102 clear_snapshot(curtab);
6103 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104}
6105
6106 static void
6107make_snapshot_rec(fr, frp)
6108 frame_T *fr;
6109 frame_T **frp;
6110{
6111 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6112 if (*frp == NULL)
6113 return;
6114 (*frp)->fr_layout = fr->fr_layout;
6115# ifdef FEAT_VERTSPLIT
6116 (*frp)->fr_width = fr->fr_width;
6117# endif
6118 (*frp)->fr_height = fr->fr_height;
6119 if (fr->fr_next != NULL)
6120 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6121 if (fr->fr_child != NULL)
6122 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6123 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6124 (*frp)->fr_win = curwin;
6125}
6126
6127/*
6128 * Remove any existing snapshot.
6129 */
6130 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006131clear_snapshot(tp)
6132 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006134 clear_snapshot_rec(tp->tp_snapshot);
6135 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136}
6137
6138 static void
6139clear_snapshot_rec(fr)
6140 frame_T *fr;
6141{
6142 if (fr != NULL)
6143 {
6144 clear_snapshot_rec(fr->fr_next);
6145 clear_snapshot_rec(fr->fr_child);
6146 vim_free(fr);
6147 }
6148}
6149
6150/*
6151 * Restore a previously created snapshot, if there is any.
6152 * This is only done if the screen size didn't change and the window layout is
6153 * still the same.
6154 */
6155 static void
6156restore_snapshot(close_curwin)
6157 int close_curwin; /* closing current window */
6158{
6159 win_T *wp;
6160
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006161 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006163 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006165 && curtab->tp_snapshot->fr_height == topframe->fr_height
6166 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006168 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 win_comp_pos();
6170 if (wp != NULL && close_curwin)
6171 win_goto(wp);
6172 redraw_all_later(CLEAR);
6173 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006174 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175}
6176
6177/*
6178 * Check if frames "sn" and "fr" have the same layout, same following frames
6179 * and same children.
6180 */
6181 static int
6182check_snapshot_rec(sn, fr)
6183 frame_T *sn;
6184 frame_T *fr;
6185{
6186 if (sn->fr_layout != fr->fr_layout
6187 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6188 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6189 || (sn->fr_next != NULL
6190 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6191 || (sn->fr_child != NULL
6192 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6193 return FAIL;
6194 return OK;
6195}
6196
6197/*
6198 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6199 * following frames and children.
6200 * Returns a pointer to the old current window, or NULL.
6201 */
6202 static win_T *
6203restore_snapshot_rec(sn, fr)
6204 frame_T *sn;
6205 frame_T *fr;
6206{
6207 win_T *wp = NULL;
6208 win_T *wp2;
6209
6210 fr->fr_height = sn->fr_height;
6211# ifdef FEAT_VERTSPLIT
6212 fr->fr_width = sn->fr_width;
6213# endif
6214 if (fr->fr_layout == FR_LEAF)
6215 {
6216 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6217# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006218 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219# endif
6220 wp = sn->fr_win;
6221 }
6222 if (sn->fr_next != NULL)
6223 {
6224 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6225 if (wp2 != NULL)
6226 wp = wp2;
6227 }
6228 if (sn->fr_child != NULL)
6229 {
6230 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6231 if (wp2 != NULL)
6232 wp = wp2;
6233 }
6234 return wp;
6235}
6236
6237#endif
6238
6239#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6240/*
6241 * Return TRUE if there is any vertically split window.
6242 */
6243 int
6244win_hasvertsplit()
6245{
6246 frame_T *fr;
6247
6248 if (topframe->fr_layout == FR_ROW)
6249 return TRUE;
6250
6251 if (topframe->fr_layout == FR_COL)
6252 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6253 if (fr->fr_layout == FR_ROW)
6254 return TRUE;
6255
6256 return FALSE;
6257}
6258#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006259
6260#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6261/*
6262 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6263 * highligted with the group 'grp' with priority 'prio'.
6264 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6265 * If no particular ID is desired, -1 must be specified for 'id'.
6266 * Return ID of added match, -1 on failure.
6267 */
6268 int
6269match_add(wp, grp, pat, prio, id)
6270 win_T *wp;
6271 char_u *grp;
6272 char_u *pat;
6273 int prio;
6274 int id;
6275{
6276 matchitem_T *cur;
6277 matchitem_T *prev;
6278 matchitem_T *m;
6279 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006280 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006281
6282 if (*grp == NUL || *pat == NUL)
6283 return -1;
6284 if (id < -1 || id == 0)
6285 {
6286 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6287 return -1;
6288 }
6289 if (id != -1)
6290 {
6291 cur = wp->w_match_head;
6292 while (cur != NULL)
6293 {
6294 if (cur->id == id)
6295 {
6296 EMSGN("E801: ID already taken: %ld", id);
6297 return -1;
6298 }
6299 cur = cur->next;
6300 }
6301 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006302 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006303 {
6304 EMSG2(_(e_nogroup), grp);
6305 return -1;
6306 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006307 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006308 {
6309 EMSG2(_(e_invarg2), pat);
6310 return -1;
6311 }
6312
6313 /* Find available match ID. */
6314 while (id == -1)
6315 {
6316 cur = wp->w_match_head;
6317 while (cur != NULL && cur->id != wp->w_next_match_id)
6318 cur = cur->next;
6319 if (cur == NULL)
6320 id = wp->w_next_match_id;
6321 wp->w_next_match_id++;
6322 }
6323
6324 /* Build new match. */
6325 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6326 m->id = id;
6327 m->priority = prio;
6328 m->pattern = vim_strsave(pat);
6329 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006330 m->match.regprog = regprog;
6331 m->match.rmm_ic = FALSE;
6332 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006333
6334 /* Insert new match. The match list is in ascending order with regard to
6335 * the match priorities. */
6336 cur = wp->w_match_head;
6337 prev = cur;
6338 while (cur != NULL && prio >= cur->priority)
6339 {
6340 prev = cur;
6341 cur = cur->next;
6342 }
6343 if (cur == prev)
6344 wp->w_match_head = m;
6345 else
6346 prev->next = m;
6347 m->next = cur;
6348
6349 redraw_later(SOME_VALID);
6350 return id;
6351}
6352
6353/*
6354 * Delete match with ID 'id' in the match list of window 'wp'.
6355 * Print error messages if 'perr' is TRUE.
6356 */
6357 int
6358match_delete(wp, id, perr)
6359 win_T *wp;
6360 int id;
6361 int perr;
6362{
6363 matchitem_T *cur = wp->w_match_head;
6364 matchitem_T *prev = cur;
6365
6366 if (id < 1)
6367 {
6368 if (perr == TRUE)
6369 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6370 id);
6371 return -1;
6372 }
6373 while (cur != NULL && cur->id != id)
6374 {
6375 prev = cur;
6376 cur = cur->next;
6377 }
6378 if (cur == NULL)
6379 {
6380 if (perr == TRUE)
6381 EMSGN("E803: ID not found: %ld", id);
6382 return -1;
6383 }
6384 if (cur == prev)
6385 wp->w_match_head = cur->next;
6386 else
6387 prev->next = cur->next;
6388 vim_free(cur->match.regprog);
6389 vim_free(cur->pattern);
6390 vim_free(cur);
6391 redraw_later(SOME_VALID);
6392 return 0;
6393}
6394
6395/*
6396 * Delete all matches in the match list of window 'wp'.
6397 */
6398 void
6399clear_matches(wp)
6400 win_T *wp;
6401{
6402 matchitem_T *m;
6403
6404 while (wp->w_match_head != NULL)
6405 {
6406 m = wp->w_match_head->next;
6407 vim_free(wp->w_match_head->match.regprog);
6408 vim_free(wp->w_match_head->pattern);
6409 vim_free(wp->w_match_head);
6410 wp->w_match_head = m;
6411 }
6412 redraw_later(SOME_VALID);
6413}
6414
6415/*
6416 * Get match from ID 'id' in window 'wp'.
6417 * Return NULL if match not found.
6418 */
6419 matchitem_T *
6420get_match(wp, id)
6421 win_T *wp;
6422 int id;
6423{
6424 matchitem_T *cur = wp->w_match_head;
6425
6426 while (cur != NULL && cur->id != id)
6427 cur = cur->next;
6428 return cur;
6429}
6430#endif