blob: 7ba9e228f7a9c684c9e10ebfb963440508d7064d [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
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static 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 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
78static win_T *win_alloc __ARGS((win_T *after));
79static void win_new_height __ARGS((win_T *, int));
80
81#define URL_SLASH 1 /* path_is_url() has found "://" */
82#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
83
84#define NOWIN (win_T *)-1 /* non-exisiting window */
85
Bram Moolenaar05159a02005-02-26 23:04:13 +000086#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000087# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000088#else
89# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000090#endif
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000093
94static char *m_onlyone = N_("Already only one window");
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/*
97 * all CTRL-W window commands are handled here, called from normal_cmd().
98 */
99 void
100do_window(nchar, Prenum, xchar)
101 int nchar;
102 long Prenum;
103 int xchar; /* extra char from ":wincmd gx" or NUL */
104{
105 long Prenum1;
106 win_T *wp;
107#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000109 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_FIND_ID
112 int type = FIND_DEFINE;
113 int len;
114#endif
115 char_u cbuf[40];
116
117 if (Prenum == 0)
118 Prenum1 = 1;
119 else
120 Prenum1 = Prenum;
121
122#ifdef FEAT_CMDWIN
123# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124#else
125# define CHECK_CMDWIN
126#endif
127
128 switch (nchar)
129 {
130/* split current window in two parts, horizontally */
131 case 'S':
132 case Ctrl_S:
133 case 's':
134 CHECK_CMDWIN
135#ifdef FEAT_VISUAL
136 reset_VIsual_and_resel(); /* stop Visual mode */
137#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000138#ifdef FEAT_QUICKFIX
139 /* When splitting the quickfix window open a new buffer in it,
140 * don't replicate the quickfix buffer. */
141 if (bt_quickfix(curbuf))
142 goto newwindow;
143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_GUI
145 need_mouse_correct = TRUE;
146#endif
147 win_split((int)Prenum, 0);
148 break;
149
150#ifdef FEAT_VERTSPLIT
151/* split current window in two parts, vertically */
152 case Ctrl_V:
153 case 'v':
154 CHECK_CMDWIN
155#ifdef FEAT_VISUAL
156 reset_VIsual_and_resel(); /* stop Visual mode */
157#endif
158#ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160#endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
174 sprintf((char *)cbuf + 7, "%ld", Prenum);
175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
189 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 else
191 cbuf[0] = NUL;
192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
199#ifdef FEAT_VISUAL
200 reset_VIsual_and_resel(); /* stop Visual mode */
201#endif
202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"close");
212 break;
213
214#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215/* close preview window */
216 case Ctrl_Z:
217 case 'z':
218 CHECK_CMDWIN
219#ifdef FEAT_VISUAL
220 reset_VIsual_and_resel(); /* stop Visual mode */
221#endif
222 do_cmdline_cmd((char_u *)"pclose");
223 break;
224
225/* cursor to preview window */
226 case 'P':
227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 if (wp->w_p_pvw)
229 break;
230 if (wp == NULL)
231 EMSG(_("E441: There is no preview window"));
232 else
233 win_goto(wp);
234 break;
235#endif
236
237/* close all but current window */
238 case Ctrl_O:
239 case 'o':
240 CHECK_CMDWIN
241#ifdef FEAT_VISUAL
242 reset_VIsual_and_resel(); /* stop Visual mode */
243#endif
244 do_cmdline_cmd((char_u *)"only");
245 break;
246
247/* cursor to next window with wrap around */
248 case Ctrl_W:
249 case 'w':
250/* cursor to previous window with wrap around */
251 case 'W':
252 CHECK_CMDWIN
253 if (lastwin == firstwin && Prenum != 1) /* just one window */
254 beep_flush();
255 else
256 {
257 if (Prenum) /* go to specified window */
258 {
259 for (wp = firstwin; --Prenum > 0; )
260 {
261 if (wp->w_next == NULL)
262 break;
263 else
264 wp = wp->w_next;
265 }
266 }
267 else
268 {
269 if (nchar == 'W') /* go to previous window */
270 {
271 wp = curwin->w_prev;
272 if (wp == NULL)
273 wp = lastwin; /* wrap around */
274 }
275 else /* go to next window */
276 {
277 wp = curwin->w_next;
278 if (wp == NULL)
279 wp = firstwin; /* wrap around */
280 }
281 }
282 win_goto(wp);
283 }
284 break;
285
286/* cursor to window below */
287 case 'j':
288 case K_DOWN:
289 case Ctrl_J:
290 CHECK_CMDWIN
291#ifdef FEAT_VERTSPLIT
292 win_goto_ver(FALSE, Prenum1);
293#else
294 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 wp = wp->w_next)
296 ;
297 win_goto(wp);
298#endif
299 break;
300
301/* cursor to window above */
302 case 'k':
303 case K_UP:
304 case Ctrl_K:
305 CHECK_CMDWIN
306#ifdef FEAT_VERTSPLIT
307 win_goto_ver(TRUE, Prenum1);
308#else
309 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 wp = wp->w_prev)
311 ;
312 win_goto(wp);
313#endif
314 break;
315
316#ifdef FEAT_VERTSPLIT
317/* cursor to left window */
318 case 'h':
319 case K_LEFT:
320 case Ctrl_H:
321 case K_BS:
322 CHECK_CMDWIN
323 win_goto_hor(TRUE, Prenum1);
324 break;
325
326/* cursor to right window */
327 case 'l':
328 case K_RIGHT:
329 case Ctrl_L:
330 CHECK_CMDWIN
331 win_goto_hor(FALSE, Prenum1);
332 break;
333#endif
334
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000335/* move window to new tab page */
336 case 'T':
337 if (firstwin == lastwin)
338 MSG(_(m_onlyone));
339 else
340 {
341 tabpage_T *oldtab = curtab;
342 tabpage_T *newtab;
343 win_T *wp = curwin;
344
345 /* First create a new tab with the window, then go back to
346 * the old tab and close the window there. */
347 if (win_new_tabpage((int)Prenum) == OK
348 && valid_tabpage(oldtab))
349 {
350 newtab = curtab;
351 goto_tabpage_tp(oldtab);
352 if (curwin == wp)
353 win_close(curwin, FALSE);
354 if (valid_tabpage(newtab))
355 goto_tabpage_tp(newtab);
356 }
357 }
358 break;
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360/* cursor to top-left window */
361 case 't':
362 case Ctrl_T:
363 win_goto(firstwin);
364 break;
365
366/* cursor to bottom-right window */
367 case 'b':
368 case Ctrl_B:
369 win_goto(lastwin);
370 break;
371
372/* cursor to last accessed (previous) window */
373 case 'p':
374 case Ctrl_P:
375 if (prevwin == NULL)
376 beep_flush();
377 else
378 win_goto(prevwin);
379 break;
380
381/* exchange current and next window */
382 case 'x':
383 case Ctrl_X:
384 CHECK_CMDWIN
385 win_exchange(Prenum);
386 break;
387
388/* rotate windows downwards */
389 case Ctrl_R:
390 case 'r':
391 CHECK_CMDWIN
392#ifdef FEAT_VISUAL
393 reset_VIsual_and_resel(); /* stop Visual mode */
394#endif
395 win_rotate(FALSE, (int)Prenum1); /* downwards */
396 break;
397
398/* rotate windows upwards */
399 case 'R':
400 CHECK_CMDWIN
401#ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403#endif
404 win_rotate(TRUE, (int)Prenum1); /* upwards */
405 break;
406
407/* move window to the very top/bottom/left/right */
408 case 'K':
409 case 'J':
410#ifdef FEAT_VERTSPLIT
411 case 'H':
412 case 'L':
413#endif
414 CHECK_CMDWIN
415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
453#ifdef FEAT_VERTSPLIT
454/* increase current window width */
455 case '>':
456#ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458#endif
459 win_setwidth(curwin->w_width + (int)Prenum1);
460 break;
461
462/* decrease current window width */
463 case '<':
464#ifdef FEAT_GUI
465 need_mouse_correct = TRUE;
466#endif
467 win_setwidth(curwin->w_width - (int)Prenum1);
468 break;
469
470/* set current window width */
471 case '|':
472#ifdef FEAT_GUI
473 need_mouse_correct = TRUE;
474#endif
475 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
476 break;
477#endif
478
479/* jump to tag and split window if tag exists (in preview window) */
480#if defined(FEAT_QUICKFIX)
481 case '}':
482 CHECK_CMDWIN
483 if (Prenum)
484 g_do_tagpreview = Prenum;
485 else
486 g_do_tagpreview = p_pvh;
487 /*FALLTHROUGH*/
488#endif
489 case ']':
490 case Ctrl_RSB:
491 CHECK_CMDWIN
492#ifdef FEAT_VISUAL
493 reset_VIsual_and_resel(); /* stop Visual mode */
494#endif
495 if (Prenum)
496 postponed_split = Prenum;
497 else
498 postponed_split = -1;
499
500 /* Execute the command right here, required when
501 * "wincmd ]" was used in a function. */
502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 setpcmark();
520 if (win_split(0, 0) == OK)
521 {
522# ifdef FEAT_SCROLLBIND
523 curwin->w_p_scb = FALSE;
524# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000525 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
526 if (nchar == 'F' && lnum >= 0)
527 {
528 curwin->w_cursor.lnum = lnum;
529 check_cursor_lnum();
530 beginline(BL_SOL | BL_FIX);
531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 vim_free(ptr);
534 }
535 break;
536#endif
537
538#ifdef FEAT_FIND_ID
539/* Go to the first occurence of the identifier under cursor along path in a
540 * new window -- webb
541 */
542 case 'i': /* Go to any match */
543 case Ctrl_I:
544 type = FIND_ANY;
545 /* FALLTHROUGH */
546 case 'd': /* Go to definition, using 'define' */
547 case Ctrl_D:
548 CHECK_CMDWIN
549 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
550 break;
551 find_pattern_in_path(ptr, 0, len, TRUE,
552 Prenum == 0 ? TRUE : FALSE, type,
553 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
554 curwin->w_set_curswant = TRUE;
555 break;
556#endif
557
Bram Moolenaar05159a02005-02-26 23:04:13 +0000558 case K_KENTER:
559 case CAR:
560#if defined(FEAT_QUICKFIX)
561 /*
562 * In a quickfix window a <CR> jumps to the error under the
563 * cursor in a new window.
564 */
565 if (bt_quickfix(curbuf))
566 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000567 sprintf((char *)cbuf, "split +%ld%s",
568 (long)curwin->w_cursor.lnum,
569 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000570 do_cmdline_cmd(cbuf);
571 }
572#endif
573 break;
574
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/* CTRL-W g extended commands */
577 case 'g':
578 case Ctrl_G:
579 CHECK_CMDWIN
580#ifdef USE_ON_FLY_SCROLL
581 dont_scroll = TRUE; /* disallow scrolling here */
582#endif
583 ++no_mapping;
584 ++allow_keys; /* no mapping for xchar, but allow key codes */
585 if (xchar == NUL)
586 xchar = safe_vgetc();
587#ifdef FEAT_LANGMAP
588 LANGMAP_ADJUST(xchar, TRUE);
589#endif
590 --no_mapping;
591 --allow_keys;
592#ifdef FEAT_CMDL_INFO
593 (void)add_to_showcmd(xchar);
594#endif
595 switch (xchar)
596 {
597#if defined(FEAT_QUICKFIX)
598 case '}':
599 xchar = Ctrl_RSB;
600 if (Prenum)
601 g_do_tagpreview = Prenum;
602 else
603 g_do_tagpreview = p_pvh;
604 /*FALLTHROUGH*/
605#endif
606 case ']':
607 case Ctrl_RSB:
608#ifdef FEAT_VISUAL
609 reset_VIsual_and_resel(); /* stop Visual mode */
610#endif
611 if (Prenum)
612 postponed_split = Prenum;
613 else
614 postponed_split = -1;
615
616 /* Execute the command right here, required when
617 * "wincmd g}" was used in a function. */
618 do_nv_ident('g', xchar);
619 break;
620
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000621#ifdef FEAT_SEARCHPATH
622 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000623 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000624 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000626 goto wingotofile;
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 default:
629 beep_flush();
630 break;
631 }
632 break;
633
634 default: beep_flush();
635 break;
636 }
637}
638
639/*
640 * split the current window, implements CTRL-W s and :split
641 *
642 * "size" is the height or width for the new window, 0 to use half of current
643 * height or width.
644 *
645 * "flags":
646 * WSP_ROOM: require enough room for new window
647 * WSP_VERT: vertical split.
648 * WSP_TOP: open window at the top-left of the shell (help window).
649 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
650 * WSP_HELP: creating the help window, keep layout snapshot
651 *
652 * return FAIL for failure, OK otherwise
653 */
654 int
655win_split(size, flags)
656 int size;
657 int flags;
658{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000659 /* When the ":tab" modifier was used open a new tab page instead. */
660 if (may_open_tabpage() == OK)
661 return OK;
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /* Add flags from ":vertical", ":topleft" and ":botright". */
664 flags |= cmdmod.split;
665 if ((flags & WSP_TOP) && (flags & WSP_BOT))
666 {
667 EMSG(_("E442: Can't split topleft and botright at the same time"));
668 return FAIL;
669 }
670
671 /* When creating the help window make a snapshot of the window layout.
672 * Otherwise clear the snapshot, it's now invalid. */
673 if (flags & WSP_HELP)
674 make_snapshot();
675 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000676 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 return win_split_ins(size, flags, NULL, 0);
679}
680
681/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000682 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 * When "newwin" is not NULL: insert this window at the far
684 * top/left/right/bottom.
685 * return FAIL for failure, OK otherwise
686 */
687 static int
688win_split_ins(size, flags, newwin, dir)
689 int size;
690 int flags;
691 win_T *newwin;
692 int dir;
693{
694 win_T *wp = newwin;
695 win_T *oldwin;
696 int new_size = size;
697 int i;
698 int need_status = 0;
699 int do_equal = FALSE;
700 int needed;
701 int available;
702 int oldwin_height = 0;
703 int layout;
704 frame_T *frp, *curfrp;
705 int before;
706
707 if (flags & WSP_TOP)
708 oldwin = firstwin;
709 else if (flags & WSP_BOT)
710 oldwin = lastwin;
711 else
712 oldwin = curwin;
713
714 /* add a status line when p_ls == 1 and splitting the first window */
715 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
716 {
717 if (oldwin->w_height <= p_wmh && newwin == NULL)
718 {
719 EMSG(_(e_noroom));
720 return FAIL;
721 }
722 need_status = STATUS_HEIGHT;
723 }
724
725#ifdef FEAT_VERTSPLIT
726 if (flags & WSP_VERT)
727 {
728 layout = FR_ROW;
729 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
730
731 /*
732 * Check if we are able to split the current window and compute its
733 * width.
734 */
735 needed = p_wmw + 1;
736 if (flags & WSP_ROOM)
737 needed += p_wiw - p_wmw;
738 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
739 {
740 available = topframe->fr_width;
741 needed += frame_minwidth(topframe, NULL);
742 }
743 else
744 available = oldwin->w_width;
745 if (available < needed && newwin == NULL)
746 {
747 EMSG(_(e_noroom));
748 return FAIL;
749 }
750 if (new_size == 0)
751 new_size = oldwin->w_width / 2;
752 if (new_size > oldwin->w_width - p_wmw - 1)
753 new_size = oldwin->w_width - p_wmw - 1;
754 if (new_size < p_wmw)
755 new_size = p_wmw;
756
757 /* if it doesn't fit in the current window, need win_equal() */
758 if (oldwin->w_width - new_size - 1 < p_wmw)
759 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000760
761 /* We don't like to take lines for the new window from a
762 * 'winfixwidth' window. Take them from a window to the left or right
763 * instead, if possible. */
764 if (oldwin->w_p_wfw)
765 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767 else
768#endif
769 {
770 layout = FR_COL;
771 do_equal = (p_ea && new_size == 0
772#ifdef FEAT_VERTSPLIT
773 && *p_ead != 'h'
774#endif
775 );
776
777 /*
778 * Check if we are able to split the current window and compute its
779 * height.
780 */
781 needed = p_wmh + STATUS_HEIGHT + need_status;
782 if (flags & WSP_ROOM)
783 needed += p_wh - p_wmh;
784 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
785 {
786 available = topframe->fr_height;
787 needed += frame_minheight(topframe, NULL);
788 }
789 else
790 {
791 available = oldwin->w_height;
792 needed += p_wmh;
793 }
794 if (available < needed && newwin == NULL)
795 {
796 EMSG(_(e_noroom));
797 return FAIL;
798 }
799 oldwin_height = oldwin->w_height;
800 if (need_status)
801 {
802 oldwin->w_status_height = STATUS_HEIGHT;
803 oldwin_height -= STATUS_HEIGHT;
804 }
805 if (new_size == 0)
806 new_size = oldwin_height / 2;
807
808 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
809 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
810 if (new_size < p_wmh)
811 new_size = p_wmh;
812
813 /* if it doesn't fit in the current window, need win_equal() */
814 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
815 do_equal = TRUE;
816
817 /* We don't like to take lines for the new window from a
818 * 'winfixheight' window. Take them from a window above or below
819 * instead, if possible. */
820 if (oldwin->w_p_wfh)
821 {
822 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
823 oldwin);
824 oldwin_height = oldwin->w_height;
825 if (need_status)
826 oldwin_height -= STATUS_HEIGHT;
827 }
828 }
829
830 /*
831 * allocate new window structure and link it in the window list
832 */
833 if ((flags & WSP_TOP) == 0
834 && ((flags & WSP_BOT)
835 || (flags & WSP_BELOW)
836 || (!(flags & WSP_ABOVE)
837 && (
838#ifdef FEAT_VERTSPLIT
839 (flags & WSP_VERT) ? p_spr :
840#endif
841 p_sb))))
842 {
843 /* new window below/right of current one */
844 if (newwin == NULL)
845 wp = win_alloc(oldwin);
846 else
847 win_append(oldwin, wp);
848 }
849 else
850 {
851 if (newwin == NULL)
852 wp = win_alloc(oldwin->w_prev);
853 else
854 win_append(oldwin->w_prev, wp);
855 }
856
857 if (newwin == NULL)
858 {
859 if (wp == NULL)
860 return FAIL;
861
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000862 /* make the contents of the new window the same as the current one */
863 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865
866 /*
867 * Reorganise the tree of frames to insert the new window.
868 */
869 if (flags & (WSP_TOP | WSP_BOT))
870 {
871#ifdef FEAT_VERTSPLIT
872 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
873 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
874#else
875 if (topframe->fr_layout == FR_COL)
876#endif
877 {
878 curfrp = topframe->fr_child;
879 if (flags & WSP_BOT)
880 while (curfrp->fr_next != NULL)
881 curfrp = curfrp->fr_next;
882 }
883 else
884 curfrp = topframe;
885 before = (flags & WSP_TOP);
886 }
887 else
888 {
889 curfrp = oldwin->w_frame;
890 if (flags & WSP_BELOW)
891 before = FALSE;
892 else if (flags & WSP_ABOVE)
893 before = TRUE;
894 else
895#ifdef FEAT_VERTSPLIT
896 if (flags & WSP_VERT)
897 before = !p_spr;
898 else
899#endif
900 before = !p_sb;
901 }
902 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
903 {
904 /* Need to create a new frame in the tree to make a branch. */
905 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
906 *frp = *curfrp;
907 curfrp->fr_layout = layout;
908 frp->fr_parent = curfrp;
909 frp->fr_next = NULL;
910 frp->fr_prev = NULL;
911 curfrp->fr_child = frp;
912 curfrp->fr_win = NULL;
913 curfrp = frp;
914 if (frp->fr_win != NULL)
915 oldwin->w_frame = frp;
916 else
917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
918 frp->fr_parent = curfrp;
919 }
920
921 if (newwin == NULL)
922 {
923 /* Create a frame for the new window. */
924 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
925 frp->fr_layout = FR_LEAF;
926 frp->fr_win = wp;
927 wp->w_frame = frp;
928 }
929 else
930 frp = newwin->w_frame;
931 frp->fr_parent = curfrp->fr_parent;
932
933 /* Insert the new frame at the right place in the frame list. */
934 if (before)
935 frame_insert(curfrp, frp);
936 else
937 frame_append(curfrp, frp);
938
939#ifdef FEAT_VERTSPLIT
940 if (flags & WSP_VERT)
941 {
942 wp->w_p_scr = curwin->w_p_scr;
943 if (need_status)
944 {
945 --oldwin->w_height;
946 oldwin->w_status_height = need_status;
947 }
948 if (flags & (WSP_TOP | WSP_BOT))
949 {
950 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000951 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 wp->w_height = curfrp->fr_height - (p_ls > 0);
953 wp->w_status_height = (p_ls > 0);
954 }
955 else
956 {
957 /* height and row of new window is same as current window */
958 wp->w_winrow = oldwin->w_winrow;
959 wp->w_height = oldwin->w_height;
960 wp->w_status_height = oldwin->w_status_height;
961 }
962 frp->fr_height = curfrp->fr_height;
963
964 /* "new_size" of the current window goes to the new window, use
965 * one column for the vertical separator */
966 wp->w_width = new_size;
967 if (before)
968 wp->w_vsep_width = 1;
969 else
970 {
971 wp->w_vsep_width = oldwin->w_vsep_width;
972 oldwin->w_vsep_width = 1;
973 }
974 if (flags & (WSP_TOP | WSP_BOT))
975 {
976 if (flags & WSP_BOT)
977 frame_add_vsep(curfrp);
978 /* Set width of neighbor frame */
979 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000980 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
981 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 }
983 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000984 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 if (before) /* new window left of current one */
986 {
987 wp->w_wincol = oldwin->w_wincol;
988 oldwin->w_wincol += new_size + 1;
989 }
990 else /* new window right of current one */
991 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
992 frame_fix_width(oldwin);
993 frame_fix_width(wp);
994 }
995 else
996#endif
997 {
998 /* width and column of new window is same as current window */
999#ifdef FEAT_VERTSPLIT
1000 if (flags & (WSP_TOP | WSP_BOT))
1001 {
1002 wp->w_wincol = 0;
1003 wp->w_width = Columns;
1004 wp->w_vsep_width = 0;
1005 }
1006 else
1007 {
1008 wp->w_wincol = oldwin->w_wincol;
1009 wp->w_width = oldwin->w_width;
1010 wp->w_vsep_width = oldwin->w_vsep_width;
1011 }
1012 frp->fr_width = curfrp->fr_width;
1013#endif
1014
1015 /* "new_size" of the current window goes to the new window, use
1016 * one row for the status line */
1017 win_new_height(wp, new_size);
1018 if (flags & (WSP_TOP | WSP_BOT))
1019 frame_new_height(curfrp, curfrp->fr_height
1020 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1021 else
1022 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1023 if (before) /* new window above current one */
1024 {
1025 wp->w_winrow = oldwin->w_winrow;
1026 wp->w_status_height = STATUS_HEIGHT;
1027 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1028 }
1029 else /* new window below current one */
1030 {
1031 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1032 wp->w_status_height = oldwin->w_status_height;
1033 oldwin->w_status_height = STATUS_HEIGHT;
1034 }
1035#ifdef FEAT_VERTSPLIT
1036 if (flags & WSP_BOT)
1037 frame_add_statusline(curfrp);
1038#endif
1039 frame_fix_height(wp);
1040 frame_fix_height(oldwin);
1041 }
1042
1043 if (flags & (WSP_TOP | WSP_BOT))
1044 (void)win_comp_pos();
1045
1046 /*
1047 * Both windows need redrawing
1048 */
1049 redraw_win_later(wp, NOT_VALID);
1050 wp->w_redr_status = TRUE;
1051 redraw_win_later(oldwin, NOT_VALID);
1052 oldwin->w_redr_status = TRUE;
1053
1054 if (need_status)
1055 {
1056 msg_row = Rows - 1;
1057 msg_col = sc_col;
1058 msg_clr_eos_force(); /* Old command/ruler may still be there */
1059 comp_col();
1060 msg_row = Rows - 1;
1061 msg_col = 0; /* put position back at start of line */
1062 }
1063
1064 /*
1065 * make the new window the current window and redraw
1066 */
1067 if (do_equal || dir != 0)
1068 win_equal(wp, TRUE,
1069#ifdef FEAT_VERTSPLIT
1070 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1071 : dir == 'h' ? 'b' :
1072#endif
1073 'v');
1074
1075 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1076 * size was given. */
1077#ifdef FEAT_VERTSPLIT
1078 if (flags & WSP_VERT)
1079 {
1080 i = p_wiw;
1081 if (size != 0)
1082 p_wiw = size;
1083
1084# ifdef FEAT_GUI
1085 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1086 if (gui.in_use)
1087 gui_init_which_components(NULL);
1088# endif
1089 }
1090 else
1091#endif
1092 {
1093 i = p_wh;
1094 if (size != 0)
1095 p_wh = size;
1096 }
1097 win_enter(wp, FALSE);
1098#ifdef FEAT_VERTSPLIT
1099 if (flags & WSP_VERT)
1100 p_wiw = i;
1101 else
1102#endif
1103 p_wh = i;
1104
1105 return OK;
1106}
1107
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001108/*
1109 * Initialize window "newp" from window "oldp".
1110 * Used when splitting a window and when creating a new tab page.
1111 * The windows will both edit the same buffer.
1112 */
1113 static void
1114win_init(newp, oldp)
1115 win_T *newp;
1116 win_T *oldp;
1117{
1118 int i;
1119
1120 newp->w_buffer = oldp->w_buffer;
1121 oldp->w_buffer->b_nwindows++;
1122 newp->w_cursor = oldp->w_cursor;
1123 newp->w_valid = 0;
1124 newp->w_curswant = oldp->w_curswant;
1125 newp->w_set_curswant = oldp->w_set_curswant;
1126 newp->w_topline = oldp->w_topline;
1127#ifdef FEAT_DIFF
1128 newp->w_topfill = oldp->w_topfill;
1129#endif
1130 newp->w_leftcol = oldp->w_leftcol;
1131 newp->w_pcmark = oldp->w_pcmark;
1132 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1133 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001134 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001135 newp->w_fraction = oldp->w_fraction;
1136 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1137#ifdef FEAT_JUMPLIST
1138 copy_jumplist(oldp, newp);
1139#endif
1140#ifdef FEAT_QUICKFIX
1141 copy_loclist(oldp, newp);
1142#endif
1143 if (oldp->w_localdir != NULL)
1144 newp->w_localdir = vim_strsave(oldp->w_localdir);
1145
1146 /* Use the same argument list. */
1147 newp->w_alist = oldp->w_alist;
1148 ++newp->w_alist->al_refcount;
1149 newp->w_arg_idx = oldp->w_arg_idx;
1150
1151 /*
1152 * copy tagstack and options from existing window
1153 */
1154 for (i = 0; i < oldp->w_tagstacklen; i++)
1155 {
1156 newp->w_tagstack[i] = oldp->w_tagstack[i];
1157 if (newp->w_tagstack[i].tagname != NULL)
1158 newp->w_tagstack[i].tagname =
1159 vim_strsave(newp->w_tagstack[i].tagname);
1160 }
1161 newp->w_tagstackidx = oldp->w_tagstackidx;
1162 newp->w_tagstacklen = oldp->w_tagstacklen;
1163 win_copy_options(oldp, newp);
1164# ifdef FEAT_FOLDING
1165 copyFoldingState(oldp, newp);
1166# endif
1167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#endif /* FEAT_WINDOWS */
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#if defined(FEAT_WINDOWS) || defined(PROTO)
1172/*
1173 * Check if "win" is a pointer to an existing window.
1174 */
1175 int
1176win_valid(win)
1177 win_T *win;
1178{
1179 win_T *wp;
1180
1181 if (win == NULL)
1182 return FALSE;
1183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1184 if (wp == win)
1185 return TRUE;
1186 return FALSE;
1187}
1188
1189/*
1190 * Return the number of windows.
1191 */
1192 int
1193win_count()
1194{
1195 win_T *wp;
1196 int count = 0;
1197
1198 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1199 ++count;
1200 return count;
1201}
1202
1203/*
1204 * Make "count" windows on the screen.
1205 * Return actual number of windows on the screen.
1206 * Must be called when there is just one window, filling the whole screen
1207 * (excluding the command line).
1208 */
1209/*ARGSUSED*/
1210 int
1211make_windows(count, vertical)
1212 int count;
1213 int vertical; /* split windows vertically if TRUE */
1214{
1215 int maxcount;
1216 int todo;
1217
1218#ifdef FEAT_VERTSPLIT
1219 if (vertical)
1220 {
1221 /* Each windows needs at least 'winminwidth' lines and a separator
1222 * column. */
1223 maxcount = (curwin->w_width + curwin->w_vsep_width
1224 - (p_wiw - p_wmw)) / (p_wmw + 1);
1225 }
1226 else
1227#endif
1228 {
1229 /* Each window needs at least 'winminheight' lines and a status line. */
1230 maxcount = (curwin->w_height + curwin->w_status_height
1231 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1232 }
1233
1234 if (maxcount < 2)
1235 maxcount = 2;
1236 if (count > maxcount)
1237 count = maxcount;
1238
1239 /*
1240 * add status line now, otherwise first window will be too big
1241 */
1242 if (count > 1)
1243 last_status(TRUE);
1244
1245#ifdef FEAT_AUTOCMD
1246 /*
1247 * Don't execute autocommands while creating the windows. Must do that
1248 * when putting the buffers in the windows.
1249 */
1250 ++autocmd_block;
1251#endif
1252
1253 /* todo is number of windows left to create */
1254 for (todo = count - 1; todo > 0; --todo)
1255#ifdef FEAT_VERTSPLIT
1256 if (vertical)
1257 {
1258 if (win_split(curwin->w_width - (curwin->w_width - todo)
1259 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1260 break;
1261 }
1262 else
1263#endif
1264 {
1265 if (win_split(curwin->w_height - (curwin->w_height - todo
1266 * STATUS_HEIGHT) / (todo + 1)
1267 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1268 break;
1269 }
1270
1271#ifdef FEAT_AUTOCMD
1272 --autocmd_block;
1273#endif
1274
1275 /* return actual number of windows */
1276 return (count - todo);
1277}
1278
1279/*
1280 * Exchange current and next window
1281 */
1282 static void
1283win_exchange(Prenum)
1284 long Prenum;
1285{
1286 frame_T *frp;
1287 frame_T *frp2;
1288 win_T *wp;
1289 win_T *wp2;
1290 int temp;
1291
1292 if (lastwin == firstwin) /* just one window */
1293 {
1294 beep_flush();
1295 return;
1296 }
1297
1298#ifdef FEAT_GUI
1299 need_mouse_correct = TRUE;
1300#endif
1301
1302 /*
1303 * find window to exchange with
1304 */
1305 if (Prenum)
1306 {
1307 frp = curwin->w_frame->fr_parent->fr_child;
1308 while (frp != NULL && --Prenum > 0)
1309 frp = frp->fr_next;
1310 }
1311 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1312 frp = curwin->w_frame->fr_next;
1313 else /* Swap last window in row/col with previous */
1314 frp = curwin->w_frame->fr_prev;
1315
1316 /* We can only exchange a window with another window, not with a frame
1317 * containing windows. */
1318 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1319 return;
1320 wp = frp->fr_win;
1321
1322/*
1323 * 1. remove curwin from the list. Remember after which window it was in wp2
1324 * 2. insert curwin before wp in the list
1325 * if wp != wp2
1326 * 3. remove wp from the list
1327 * 4. insert wp after wp2
1328 * 5. exchange the status line height and vsep width.
1329 */
1330 wp2 = curwin->w_prev;
1331 frp2 = curwin->w_frame->fr_prev;
1332 if (wp->w_prev != curwin)
1333 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001334 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 frame_remove(curwin->w_frame);
1336 win_append(wp->w_prev, curwin);
1337 frame_insert(frp, curwin->w_frame);
1338 }
1339 if (wp != wp2)
1340 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 frame_remove(wp->w_frame);
1343 win_append(wp2, wp);
1344 if (frp2 == NULL)
1345 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1346 else
1347 frame_append(frp2, wp->w_frame);
1348 }
1349 temp = curwin->w_status_height;
1350 curwin->w_status_height = wp->w_status_height;
1351 wp->w_status_height = temp;
1352#ifdef FEAT_VERTSPLIT
1353 temp = curwin->w_vsep_width;
1354 curwin->w_vsep_width = wp->w_vsep_width;
1355 wp->w_vsep_width = temp;
1356
1357 /* If the windows are not in the same frame, exchange the sizes to avoid
1358 * messing up the window layout. Otherwise fix the frame sizes. */
1359 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1360 {
1361 temp = curwin->w_height;
1362 curwin->w_height = wp->w_height;
1363 wp->w_height = temp;
1364 temp = curwin->w_width;
1365 curwin->w_width = wp->w_width;
1366 wp->w_width = temp;
1367 }
1368 else
1369 {
1370 frame_fix_height(curwin);
1371 frame_fix_height(wp);
1372 frame_fix_width(curwin);
1373 frame_fix_width(wp);
1374 }
1375#endif
1376
1377 (void)win_comp_pos(); /* recompute window positions */
1378
1379 win_enter(wp, TRUE);
1380 redraw_later(CLEAR);
1381}
1382
1383/*
1384 * rotate windows: if upwards TRUE the second window becomes the first one
1385 * if upwards FALSE the first window becomes the second one
1386 */
1387 static void
1388win_rotate(upwards, count)
1389 int upwards;
1390 int count;
1391{
1392 win_T *wp1;
1393 win_T *wp2;
1394 frame_T *frp;
1395 int n;
1396
1397 if (firstwin == lastwin) /* nothing to do */
1398 {
1399 beep_flush();
1400 return;
1401 }
1402
1403#ifdef FEAT_GUI
1404 need_mouse_correct = TRUE;
1405#endif
1406
1407#ifdef FEAT_VERTSPLIT
1408 /* Check if all frames in this row/col have one window. */
1409 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1410 frp = frp->fr_next)
1411 if (frp->fr_win == NULL)
1412 {
1413 EMSG(_("E443: Cannot rotate when another window is split"));
1414 return;
1415 }
1416#endif
1417
1418 while (count--)
1419 {
1420 if (upwards) /* first window becomes last window */
1421 {
1422 /* remove first window/frame from the list */
1423 frp = curwin->w_frame->fr_parent->fr_child;
1424 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 frame_remove(frp);
1427
1428 /* find last frame and append removed window/frame after it */
1429 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1430 ;
1431 win_append(frp->fr_win, wp1);
1432 frame_append(frp, wp1->w_frame);
1433
1434 wp2 = frp->fr_win; /* previously last window */
1435 }
1436 else /* last window becomes first window */
1437 {
1438 /* find last window/frame in the list and remove it */
1439 for (frp = curwin->w_frame; frp->fr_next != NULL;
1440 frp = frp->fr_next)
1441 ;
1442 wp1 = frp->fr_win;
1443 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 frame_remove(frp);
1446
1447 /* append the removed window/frame before the first in the list */
1448 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1449 frame_insert(frp->fr_parent->fr_child, frp);
1450 }
1451
1452 /* exchange status height and vsep width of old and new last window */
1453 n = wp2->w_status_height;
1454 wp2->w_status_height = wp1->w_status_height;
1455 wp1->w_status_height = n;
1456 frame_fix_height(wp1);
1457 frame_fix_height(wp2);
1458#ifdef FEAT_VERTSPLIT
1459 n = wp2->w_vsep_width;
1460 wp2->w_vsep_width = wp1->w_vsep_width;
1461 wp1->w_vsep_width = n;
1462 frame_fix_width(wp1);
1463 frame_fix_width(wp2);
1464#endif
1465
1466 /* recompute w_winrow and w_wincol for all windows */
1467 (void)win_comp_pos();
1468 }
1469
1470 redraw_later(CLEAR);
1471}
1472
1473/*
1474 * Move the current window to the very top/bottom/left/right of the screen.
1475 */
1476 static void
1477win_totop(size, flags)
1478 int size;
1479 int flags;
1480{
1481 int dir;
1482 int height = curwin->w_height;
1483
1484 if (lastwin == firstwin)
1485 {
1486 beep_flush();
1487 return;
1488 }
1489
1490 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001491 (void)winframe_remove(curwin, &dir, NULL);
1492 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 last_status(FALSE); /* may need to remove last status line */
1494 (void)win_comp_pos(); /* recompute window positions */
1495
1496 /* Split a window on the desired side and put the window there. */
1497 (void)win_split_ins(size, flags, curwin, dir);
1498 if (!(flags & WSP_VERT))
1499 {
1500 win_setheight(height);
1501 if (p_ea)
1502 win_equal(curwin, TRUE, 'v');
1503 }
1504
1505#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1506 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1507 * scrollbars. Have to update them anyway. */
1508 if (gui.in_use)
1509 {
1510 out_flush();
1511 gui_init_which_components(NULL);
1512 gui_update_scrollbars(TRUE);
1513 }
1514 need_mouse_correct = TRUE;
1515#endif
1516
1517}
1518
1519/*
1520 * Move window "win1" to below/right of "win2" and make "win1" the current
1521 * window. Only works within the same frame!
1522 */
1523 void
1524win_move_after(win1, win2)
1525 win_T *win1, *win2;
1526{
1527 int height;
1528
1529 /* check if the arguments are reasonable */
1530 if (win1 == win2)
1531 return;
1532
1533 /* check if there is something to do */
1534 if (win2->w_next != win1)
1535 {
1536 /* may need move the status line/vertical separator of the last window
1537 * */
1538 if (win1 == lastwin)
1539 {
1540 height = win1->w_prev->w_status_height;
1541 win1->w_prev->w_status_height = win1->w_status_height;
1542 win1->w_status_height = height;
1543#ifdef FEAT_VERTSPLIT
1544 win1->w_prev->w_vsep_width = 0;
1545 win1->w_vsep_width = 1;
1546#endif
1547 }
1548 else if (win2 == lastwin)
1549 {
1550 height = win1->w_status_height;
1551 win1->w_status_height = win2->w_status_height;
1552 win2->w_status_height = height;
1553#ifdef FEAT_VERTSPLIT
1554 win2->w_vsep_width = 1;
1555 win1->w_vsep_width = 0;
1556#endif
1557 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001558 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 frame_remove(win1->w_frame);
1560 win_append(win2, win1);
1561 frame_append(win2->w_frame, win1->w_frame);
1562
1563 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1564 redraw_later(NOT_VALID);
1565 }
1566 win_enter(win1, FALSE);
1567}
1568
1569/*
1570 * Make all windows the same height.
1571 * 'next_curwin' will soon be the current window, make sure it has enough
1572 * rows.
1573 */
1574 void
1575win_equal(next_curwin, current, dir)
1576 win_T *next_curwin; /* pointer to current window to be or NULL */
1577 int current; /* do only frame with current window */
1578 int dir; /* 'v' for vertically, 'h' for horizontally,
1579 'b' for both, 0 for using p_ead */
1580{
1581 if (dir == 0)
1582#ifdef FEAT_VERTSPLIT
1583 dir = *p_ead;
1584#else
1585 dir = 'b';
1586#endif
1587 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001588 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001589 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590}
1591
1592/*
1593 * Set a frame to a new position and height, spreading the available room
1594 * equally over contained frames.
1595 * The window "next_curwin" (if not NULL) should at least get the size from
1596 * 'winheight' and 'winwidth' if possible.
1597 */
1598 static void
1599win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1600 win_T *next_curwin; /* pointer to current window to be or NULL */
1601 int current; /* do only frame with current window */
1602 frame_T *topfr; /* frame to set size off */
1603 int dir; /* 'v', 'h' or 'b', see win_equal() */
1604 int col; /* horizontal position for frame */
1605 int row; /* vertical position for frame */
1606 int width; /* new width of frame */
1607 int height; /* new height of frame */
1608{
1609 int n, m;
1610 int extra_sep = 0;
1611 int wincount, totwincount = 0;
1612 frame_T *fr;
1613 int next_curwin_size = 0;
1614 int room = 0;
1615 int new_size;
1616 int has_next_curwin = 0;
1617 int hnc;
1618
1619 if (topfr->fr_layout == FR_LEAF)
1620 {
1621 /* Set the width/height of this frame.
1622 * Redraw when size or position changes */
1623 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1624#ifdef FEAT_VERTSPLIT
1625 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1626#endif
1627 )
1628 {
1629 topfr->fr_win->w_winrow = row;
1630 frame_new_height(topfr, height, FALSE, FALSE);
1631#ifdef FEAT_VERTSPLIT
1632 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001633 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635 redraw_all_later(CLEAR);
1636 }
1637 }
1638#ifdef FEAT_VERTSPLIT
1639 else if (topfr->fr_layout == FR_ROW)
1640 {
1641 topfr->fr_width = width;
1642 topfr->fr_height = height;
1643
1644 if (dir != 'v') /* equalize frame widths */
1645 {
1646 /* Compute the maximum number of windows horizontally in this
1647 * frame. */
1648 n = frame_minwidth(topfr, NOWIN);
1649 /* add one for the rightmost window, it doesn't have a separator */
1650 if (col + width == Columns)
1651 extra_sep = 1;
1652 else
1653 extra_sep = 0;
1654 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001655 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001657 /*
1658 * Compute width for "next_curwin" window and room available for
1659 * other windows.
1660 * "m" is the minimal width when counting p_wiw for "next_curwin".
1661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 m = frame_minwidth(topfr, next_curwin);
1663 room = width - m;
1664 if (room < 0)
1665 {
1666 next_curwin_size = p_wiw + room;
1667 room = 0;
1668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 else
1670 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001671 next_curwin_size = -1;
1672 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1673 {
1674 /* If 'winfixwidth' set keep the window width if
1675 * possible.
1676 * Watch out for this window being the next_curwin. */
1677 if (frame_fixed_width(fr))
1678 {
1679 n = frame_minwidth(fr, NOWIN);
1680 new_size = fr->fr_width;
1681 if (frame_has_win(fr, next_curwin))
1682 {
1683 room += p_wiw - p_wmw;
1684 next_curwin_size = 0;
1685 if (new_size < p_wiw)
1686 new_size = p_wiw;
1687 }
1688 else
1689 /* These windows don't use up room. */
1690 totwincount -= (n + (fr->fr_next == NULL
1691 ? extra_sep : 0)) / (p_wmw + 1);
1692 room -= new_size - n;
1693 if (room < 0)
1694 {
1695 new_size += room;
1696 room = 0;
1697 }
1698 fr->fr_newwidth = new_size;
1699 }
1700 }
1701 if (next_curwin_size == -1)
1702 {
1703 if (!has_next_curwin)
1704 next_curwin_size = 0;
1705 else if (totwincount > 1
1706 && (room + (totwincount - 2))
1707 / (totwincount - 1) > p_wiw)
1708 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001709 /* Can make all windows wider than 'winwidth', spread
1710 * the room equally. */
1711 next_curwin_size = (room + p_wiw
1712 + (totwincount - 1) * p_wmw
1713 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001714 room -= next_curwin_size - p_wiw;
1715 }
1716 else
1717 next_curwin_size = p_wiw;
1718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720
1721 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 --totwincount; /* don't count curwin */
1723 }
1724
1725 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1726 {
1727 n = m = 0;
1728 wincount = 1;
1729 if (fr->fr_next == NULL)
1730 /* last frame gets all that remains (avoid roundoff error) */
1731 new_size = width;
1732 else if (dir == 'v')
1733 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001734 else if (frame_fixed_width(fr))
1735 {
1736 new_size = fr->fr_newwidth;
1737 wincount = 0; /* doesn't count as a sizeable window */
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 else
1740 {
1741 /* Compute the maximum number of windows horiz. in "fr". */
1742 n = frame_minwidth(fr, NOWIN);
1743 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1744 / (p_wmw + 1);
1745 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 if (has_next_curwin)
1747 hnc = frame_has_win(fr, next_curwin);
1748 else
1749 hnc = FALSE;
1750 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001752 if (totwincount == 0)
1753 new_size = room;
1754 else
1755 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001757 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 next_curwin_size -= p_wiw - (m - n);
1760 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001761 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001763 else
1764 room -= new_size;
1765 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001768 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * window, unless equalizing all frames. */
1770 if (!current || dir != 'v' || topfr->fr_parent != NULL
1771 || (new_size != fr->fr_width)
1772 || frame_has_win(fr, next_curwin))
1773 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001774 new_size, height);
1775 col += new_size;
1776 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 totwincount -= wincount;
1778 }
1779 }
1780#endif
1781 else /* topfr->fr_layout == FR_COL */
1782 {
1783#ifdef FEAT_VERTSPLIT
1784 topfr->fr_width = width;
1785#endif
1786 topfr->fr_height = height;
1787
1788 if (dir != 'h') /* equalize frame heights */
1789 {
1790 /* Compute maximum number of windows vertically in this frame. */
1791 n = frame_minheight(topfr, NOWIN);
1792 /* add one for the bottom window if it doesn't have a statusline */
1793 if (row + height == cmdline_row && p_ls == 0)
1794 extra_sep = 1;
1795 else
1796 extra_sep = 0;
1797 totwincount = (n + extra_sep) / (p_wmh + 1);
1798 has_next_curwin = frame_has_win(topfr, next_curwin);
1799
1800 /*
1801 * Compute height for "next_curwin" window and room available for
1802 * other windows.
1803 * "m" is the minimal height when counting p_wh for "next_curwin".
1804 */
1805 m = frame_minheight(topfr, next_curwin);
1806 room = height - m;
1807 if (room < 0)
1808 {
1809 /* The room is less then 'winheight', use all space for the
1810 * current window. */
1811 next_curwin_size = p_wh + room;
1812 room = 0;
1813 }
1814 else
1815 {
1816 next_curwin_size = -1;
1817 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1818 {
1819 /* If 'winfixheight' set keep the window height if
1820 * possible.
1821 * Watch out for this window being the next_curwin. */
1822 if (frame_fixed_height(fr))
1823 {
1824 n = frame_minheight(fr, NOWIN);
1825 new_size = fr->fr_height;
1826 if (frame_has_win(fr, next_curwin))
1827 {
1828 room += p_wh - p_wmh;
1829 next_curwin_size = 0;
1830 if (new_size < p_wh)
1831 new_size = p_wh;
1832 }
1833 else
1834 /* These windows don't use up room. */
1835 totwincount -= (n + (fr->fr_next == NULL
1836 ? extra_sep : 0)) / (p_wmh + 1);
1837 room -= new_size - n;
1838 if (room < 0)
1839 {
1840 new_size += room;
1841 room = 0;
1842 }
1843 fr->fr_newheight = new_size;
1844 }
1845 }
1846 if (next_curwin_size == -1)
1847 {
1848 if (!has_next_curwin)
1849 next_curwin_size = 0;
1850 else if (totwincount > 1
1851 && (room + (totwincount - 2))
1852 / (totwincount - 1) > p_wh)
1853 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001854 /* can make all windows higher than 'winheight',
1855 * spread the room equally. */
1856 next_curwin_size = (room + p_wh
1857 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 + (totwincount - 1)) / totwincount;
1859 room -= next_curwin_size - p_wh;
1860 }
1861 else
1862 next_curwin_size = p_wh;
1863 }
1864 }
1865
1866 if (has_next_curwin)
1867 --totwincount; /* don't count curwin */
1868 }
1869
1870 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1871 {
1872 n = m = 0;
1873 wincount = 1;
1874 if (fr->fr_next == NULL)
1875 /* last frame gets all that remains (avoid roundoff error) */
1876 new_size = height;
1877 else if (dir == 'h')
1878 new_size = fr->fr_height;
1879 else if (frame_fixed_height(fr))
1880 {
1881 new_size = fr->fr_newheight;
1882 wincount = 0; /* doesn't count as a sizeable window */
1883 }
1884 else
1885 {
1886 /* Compute the maximum number of windows vert. in "fr". */
1887 n = frame_minheight(fr, NOWIN);
1888 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1889 / (p_wmh + 1);
1890 m = frame_minheight(fr, next_curwin);
1891 if (has_next_curwin)
1892 hnc = frame_has_win(fr, next_curwin);
1893 else
1894 hnc = FALSE;
1895 if (hnc) /* don't count next_curwin */
1896 --wincount;
1897 if (totwincount == 0)
1898 new_size = room;
1899 else
1900 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1901 / totwincount;
1902 if (hnc) /* add next_curwin size */
1903 {
1904 next_curwin_size -= p_wh - (m - n);
1905 new_size += next_curwin_size;
1906 room -= new_size - next_curwin_size;
1907 }
1908 else
1909 room -= new_size;
1910 new_size += n;
1911 }
1912 /* Skip frame that is full width when splitting or closing a
1913 * window, unless equalizing all frames. */
1914 if (!current || dir != 'h' || topfr->fr_parent != NULL
1915 || (new_size != fr->fr_height)
1916 || frame_has_win(fr, next_curwin))
1917 win_equal_rec(next_curwin, current, fr, dir, col, row,
1918 width, new_size);
1919 row += new_size;
1920 height -= new_size;
1921 totwincount -= wincount;
1922 }
1923 }
1924}
1925
1926/*
1927 * close all windows for buffer 'buf'
1928 */
1929 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001930close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001932 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001934 win_T *wp;
1935 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001936 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001939
1940 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001942 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001944 win_close(wp, FALSE);
1945
1946 /* Start all over, autocommands may change the window layout. */
1947 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001950 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001952
1953 /* Also check windows in other tab pages. */
1954 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1955 {
1956 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001957 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001958 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1959 if (wp->w_buffer == buf)
1960 {
1961 win_close_othertab(wp, FALSE, tp);
1962
1963 /* Start all over, the tab page may be closed and
1964 * autocommands may change the window layout. */
1965 nexttp = first_tabpage;
1966 break;
1967 }
1968 }
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001971
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001972 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001973 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974}
1975
1976/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001977 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001978 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001979 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001980 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001981last_window()
1982{
1983 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1984}
1985
1986/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001987 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 * If "free_buf" is TRUE related buffer may be unloaded.
1989 *
1990 * called by :quit, :close, :xit, :wq and findtag()
1991 */
1992 void
1993win_close(win, free_buf)
1994 win_T *win;
1995 int free_buf;
1996{
1997 win_T *wp;
1998#ifdef FEAT_AUTOCMD
1999 int other_buffer = FALSE;
2000#endif
2001 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 int dir;
2003 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002004 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002006 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 EMSG(_("E444: Cannot close last window"));
2009 return;
2010 }
2011
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002012 /*
2013 * When closing the last window in a tab page first go to another tab
2014 * page and then close the window and the tab page. This avoids that
2015 * curwin and curtab are not invalid while we are freeing memory, they may
2016 * be used in GUI events.
2017 */
2018 if (firstwin == lastwin)
2019 {
2020 goto_tabpage_tp(alt_tabpage());
2021 redraw_tabline = TRUE;
2022
2023 /* Safety check: Autocommands may have closed the window when jumping
2024 * to the other tab page. */
2025 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2026 {
2027 int h = tabline_height();
2028
2029 win_close_othertab(win, free_buf, prev_curtab);
2030 if (h != tabline_height())
2031 shell_new_rows();
2032 }
2033 return;
2034 }
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /* When closing the help window, try restoring a snapshot after closing
2037 * the window. Otherwise clear the snapshot, it's now invalid. */
2038 if (win->w_buffer->b_help)
2039 help_window = TRUE;
2040 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002041 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043#ifdef FEAT_AUTOCMD
2044 if (win == curwin)
2045 {
2046 /*
2047 * Guess which window is going to be the new current window.
2048 * This may change because of the autocommands (sigh).
2049 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002050 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 /*
2053 * Be careful: If autocommands delete the window, return now.
2054 */
2055 if (wp->w_buffer != curbuf)
2056 {
2057 other_buffer = TRUE;
2058 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002059 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return;
2061 }
2062 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002063 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return;
2065# ifdef FEAT_EVAL
2066 /* autocmds may abort script processing */
2067 if (aborting())
2068 return;
2069# endif
2070 }
2071#endif
2072
2073 /*
2074 * Close the link to the buffer.
2075 */
2076 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002079 * other window or moved to another tab page. */
2080 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 return;
2082
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002083 /* Free the memory used for the window. */
2084 wp = win_free_mem(win, &dir, NULL);
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /* Make sure curwin isn't invalid. It can cause severe trouble when
2087 * printing an error message. For win_equal() curbuf needs to be valid
2088 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002089 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
2091 curwin = wp;
2092#ifdef FEAT_QUICKFIX
2093 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2094 {
2095 /*
2096 * The cursor goes to the preview or the quickfix window, try
2097 * finding another window to go to.
2098 */
2099 for (;;)
2100 {
2101 if (wp->w_next == NULL)
2102 wp = firstwin;
2103 else
2104 wp = wp->w_next;
2105 if (wp == curwin)
2106 break;
2107 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2108 {
2109 curwin = wp;
2110 break;
2111 }
2112 }
2113 }
2114#endif
2115 curbuf = curwin->w_buffer;
2116 close_curwin = TRUE;
2117 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002118 if (p_ea
2119#ifdef FEAT_VERTSPLIT
2120 && (*p_ead == 'b' || *p_ead == dir)
2121#endif
2122 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 win_equal(curwin, TRUE,
2124#ifdef FEAT_VERTSPLIT
2125 dir
2126#else
2127 0
2128#endif
2129 );
2130 else
2131 win_comp_pos();
2132 if (close_curwin)
2133 {
2134 win_enter_ext(wp, FALSE, TRUE);
2135#ifdef FEAT_AUTOCMD
2136 if (other_buffer)
2137 /* careful: after this wp and win may be invalid! */
2138 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2139#endif
2140 }
2141
2142 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002143 * If last window has a status line now and we don't want one,
2144 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 */
2146 last_status(FALSE);
2147
2148 /* After closing the help window, try restoring the window layout from
2149 * before it was opened. */
2150 if (help_window)
2151 restore_snapshot(close_curwin);
2152
2153#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2154 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2155 if (gui.in_use && !win_hasvertsplit())
2156 gui_init_which_components(NULL);
2157#endif
2158
2159 redraw_all_later(NOT_VALID);
2160}
2161
2162/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163 * Close window "win" in tab page "tp", which is not the current tab page.
2164 * This may be the last window ih that tab page and result in closing the tab,
2165 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002166 * Caller must check if buffer is hidden and whether the tabline needs to be
2167 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002168 */
2169 void
2170win_close_othertab(win, free_buf, tp)
2171 win_T *win;
2172 int free_buf;
2173 tabpage_T *tp;
2174{
2175 win_T *wp;
2176 int dir;
2177 tabpage_T *ptp = NULL;
2178
2179 /* Close the link to the buffer. */
2180 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2181
2182 /* Careful: Autocommands may have closed the tab page or made it the
2183 * current tab page. */
2184 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2185 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002186 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002187 return;
2188
2189 /* Autocommands may have closed the window already. */
2190 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2191 ;
2192 if (wp == NULL)
2193 return;
2194
2195 /* Free the memory used for the window. */
2196 wp = win_free_mem(win, &dir, tp);
2197
2198 /* When closing the last window in a tab page remove the tab page. */
2199 if (wp == NULL)
2200 {
2201 if (tp == first_tabpage)
2202 first_tabpage = tp->tp_next;
2203 else
2204 {
2205 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2206 ptp = ptp->tp_next)
2207 ;
2208 if (ptp == NULL)
2209 {
2210 EMSG2(_(e_intern2), "win_close_othertab()");
2211 return;
2212 }
2213 ptp->tp_next = tp->tp_next;
2214 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002215 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002216 }
2217}
2218
2219/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002220 * Free the memory used for a window.
2221 * Returns a pointer to the window that got the freed up space.
2222 */
2223 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002225 win_T *win;
2226 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002227 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002228{
2229 frame_T *frp;
2230 win_T *wp;
2231
Bram Moolenaarea408852005-06-25 22:49:46 +00002232#ifdef FEAT_FOLDING
2233 clearFolding(win);
2234#endif
2235
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002236 /* reduce the reference count to the argument list. */
2237 alist_unlink(win->w_alist);
2238
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002239 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002240 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002242 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002243 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245 /* When deleting the current window of another tab page select a new
2246 * current window. */
2247 if (tp != NULL && win == tp->tp_curwin)
2248 tp->tp_curwin = wp;
2249
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002250 return wp;
2251}
2252
2253#if defined(EXITFREE) || defined(PROTO)
2254 void
2255win_free_all()
2256{
2257 int dummy;
2258
Bram Moolenaarf740b292006-02-16 22:11:02 +00002259# ifdef FEAT_WINDOWS
2260 while (first_tabpage->tp_next != NULL)
2261 tabpage_close(TRUE);
2262# endif
2263
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002264 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002265 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002266}
2267#endif
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Remove a window and its frame from the tree of frames.
2271 * Returns a pointer to the window that got the freed up space.
2272 */
2273/*ARGSUSED*/
2274 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002275winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 win_T *win;
2277 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002278 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 frame_T *frp, *frp2, *frp3;
2281 frame_T *frp_close = win->w_frame;
2282 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002283 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 * If there is only one window there is nothing to remove.
2287 */
2288 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2289 return NULL;
2290
2291 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 * Remove the window from its frame.
2293 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002294 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 wp = frame2win(frp2);
2296
2297 /* Remove this frame from the list of frames. */
2298 frame_remove(frp_close);
2299
2300#ifdef FEAT_VERTSPLIT
2301 if (frp_close->fr_parent->fr_layout == FR_COL)
2302 {
2303#endif
2304 /* When 'winfixheight' is set, remember its old size and restore
2305 * it later (it's a simplistic solution...). Don't do this if the
2306 * window will occupy the full height of the screen. */
2307 if (frp2->fr_win != NULL
2308 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2309 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002310 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2312 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002313 if (old_size != 0)
2314 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_VERTSPLIT
2316 *dirp = 'v';
2317 }
2318 else
2319 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002320 /* When 'winfixwidth' is set, remember its old size and restore
2321 * it later (it's a simplistic solution...). Don't do this if the
2322 * window will occupy the full width of the screen. */
2323 if (frp2->fr_win != NULL
2324 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2325 && frp2->fr_win->w_p_wfw)
2326 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002328 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2329 if (old_size != 0)
2330 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 *dirp = 'h';
2332 }
2333#endif
2334
2335 /* If rows/columns go to a window below/right its positions need to be
2336 * updated. Can only be done after the sizes have been updated. */
2337 if (frp2 == frp_close->fr_next)
2338 {
2339 int row = win->w_winrow;
2340 int col = W_WINCOL(win);
2341
2342 frame_comp_pos(frp2, &row, &col);
2343 }
2344
2345 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2346 {
2347 /* There is no other frame in this list, move its info to the parent
2348 * and remove it. */
2349 frp2->fr_parent->fr_layout = frp2->fr_layout;
2350 frp2->fr_parent->fr_child = frp2->fr_child;
2351 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2352 frp->fr_parent = frp2->fr_parent;
2353 frp2->fr_parent->fr_win = frp2->fr_win;
2354 if (frp2->fr_win != NULL)
2355 frp2->fr_win->w_frame = frp2->fr_parent;
2356 frp = frp2->fr_parent;
2357 vim_free(frp2);
2358
2359 frp2 = frp->fr_parent;
2360 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2361 {
2362 /* The frame above the parent has the same layout, have to merge
2363 * the frames into this list. */
2364 if (frp2->fr_child == frp)
2365 frp2->fr_child = frp->fr_child;
2366 frp->fr_child->fr_prev = frp->fr_prev;
2367 if (frp->fr_prev != NULL)
2368 frp->fr_prev->fr_next = frp->fr_child;
2369 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2370 {
2371 frp3->fr_parent = frp2;
2372 if (frp3->fr_next == NULL)
2373 {
2374 frp3->fr_next = frp->fr_next;
2375 if (frp->fr_next != NULL)
2376 frp->fr_next->fr_prev = frp3;
2377 break;
2378 }
2379 }
2380 vim_free(frp);
2381 }
2382 }
2383
2384 return wp;
2385}
2386
2387/*
2388 * Find out which frame is going to get the freed up space when "win" is
2389 * closed.
2390 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2391 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2392 * This makes opening a window and closing it immediately keep the same window
2393 * layout.
2394 */
2395 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002396win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400 frame_T *frp;
2401 int b;
2402
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002404 /* Last window in this tab page, will go to next tab page. */
2405 return alt_tabpage()->tp_curwin->w_frame;
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 frp = win->w_frame;
2408#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002409 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 b = p_spr;
2411 else
2412#endif
2413 b = p_sb;
2414 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2415 return frp->fr_next;
2416 return frp->fr_prev;
2417}
2418
2419/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002420 * Return the tabpage that will be used if the current one is closed.
2421 */
2422 static tabpage_T *
2423alt_tabpage()
2424{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002425 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002426
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002427 /* Use the next tab page if possible. */
2428 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002429 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002430
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002431 /* Find the last but one tab page. */
2432 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2433 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002434 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002435}
2436
2437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 * Find the left-upper window in frame "frp".
2439 */
2440 static win_T *
2441frame2win(frp)
2442 frame_T *frp;
2443{
2444 while (frp->fr_win == NULL)
2445 frp = frp->fr_child;
2446 return frp->fr_win;
2447}
2448
2449/*
2450 * Return TRUE if frame "frp" contains window "wp".
2451 */
2452 static int
2453frame_has_win(frp, wp)
2454 frame_T *frp;
2455 win_T *wp;
2456{
2457 frame_T *p;
2458
2459 if (frp->fr_layout == FR_LEAF)
2460 return frp->fr_win == wp;
2461
2462 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2463 if (frame_has_win(p, wp))
2464 return TRUE;
2465 return FALSE;
2466}
2467
2468/*
2469 * Set a new height for a frame. Recursively sets the height for contained
2470 * frames and windows. Caller must take care of positions.
2471 */
2472 static void
2473frame_new_height(topfrp, height, topfirst, wfh)
2474 frame_T *topfrp;
2475 int height;
2476 int topfirst; /* resize topmost contained frame first */
2477 int wfh; /* obey 'winfixheight' when there is a choice;
2478 may cause the height not to be set */
2479{
2480 frame_T *frp;
2481 int extra_lines;
2482 int h;
2483
2484 if (topfrp->fr_win != NULL)
2485 {
2486 /* Simple case: just one window. */
2487 win_new_height(topfrp->fr_win,
2488 height - topfrp->fr_win->w_status_height);
2489 }
2490#ifdef FEAT_VERTSPLIT
2491 else if (topfrp->fr_layout == FR_ROW)
2492 {
2493 do
2494 {
2495 /* All frames in this row get the same new height. */
2496 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2497 {
2498 frame_new_height(frp, height, topfirst, wfh);
2499 if (frp->fr_height > height)
2500 {
2501 /* Could not fit the windows, make the whole row higher. */
2502 height = frp->fr_height;
2503 break;
2504 }
2505 }
2506 }
2507 while (frp != NULL);
2508 }
2509#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002510 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /* Complicated case: Resize a column of frames. Resize the bottom
2513 * frame first, frames above that when needed. */
2514
2515 frp = topfrp->fr_child;
2516 if (wfh)
2517 /* Advance past frames with one window with 'wfh' set. */
2518 while (frame_fixed_height(frp))
2519 {
2520 frp = frp->fr_next;
2521 if (frp == NULL)
2522 return; /* no frame without 'wfh', give up */
2523 }
2524 if (!topfirst)
2525 {
2526 /* Find the bottom frame of this column */
2527 while (frp->fr_next != NULL)
2528 frp = frp->fr_next;
2529 if (wfh)
2530 /* Advance back for frames with one window with 'wfh' set. */
2531 while (frame_fixed_height(frp))
2532 frp = frp->fr_prev;
2533 }
2534
2535 extra_lines = height - topfrp->fr_height;
2536 if (extra_lines < 0)
2537 {
2538 /* reduce height of contained frames, bottom or top frame first */
2539 while (frp != NULL)
2540 {
2541 h = frame_minheight(frp, NULL);
2542 if (frp->fr_height + extra_lines < h)
2543 {
2544 extra_lines += frp->fr_height - h;
2545 frame_new_height(frp, h, topfirst, wfh);
2546 }
2547 else
2548 {
2549 frame_new_height(frp, frp->fr_height + extra_lines,
2550 topfirst, wfh);
2551 break;
2552 }
2553 if (topfirst)
2554 {
2555 do
2556 frp = frp->fr_next;
2557 while (wfh && frp != NULL && frame_fixed_height(frp));
2558 }
2559 else
2560 {
2561 do
2562 frp = frp->fr_prev;
2563 while (wfh && frp != NULL && frame_fixed_height(frp));
2564 }
2565 /* Increase "height" if we could not reduce enough frames. */
2566 if (frp == NULL)
2567 height -= extra_lines;
2568 }
2569 }
2570 else if (extra_lines > 0)
2571 {
2572 /* increase height of bottom or top frame */
2573 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2574 }
2575 }
2576 topfrp->fr_height = height;
2577}
2578
2579/*
2580 * Return TRUE if height of frame "frp" should not be changed because of
2581 * the 'winfixheight' option.
2582 */
2583 static int
2584frame_fixed_height(frp)
2585 frame_T *frp;
2586{
2587 /* frame with one window: fixed height if 'winfixheight' set. */
2588 if (frp->fr_win != NULL)
2589 return frp->fr_win->w_p_wfh;
2590
2591 if (frp->fr_layout == FR_ROW)
2592 {
2593 /* The frame is fixed height if one of the frames in the row is fixed
2594 * height. */
2595 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2596 if (frame_fixed_height(frp))
2597 return TRUE;
2598 return FALSE;
2599 }
2600
2601 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2602 * frames in the row are fixed height. */
2603 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2604 if (!frame_fixed_height(frp))
2605 return FALSE;
2606 return TRUE;
2607}
2608
2609#ifdef FEAT_VERTSPLIT
2610/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002611 * Return TRUE if width of frame "frp" should not be changed because of
2612 * the 'winfixwidth' option.
2613 */
2614 static int
2615frame_fixed_width(frp)
2616 frame_T *frp;
2617{
2618 /* frame with one window: fixed width if 'winfixwidth' set. */
2619 if (frp->fr_win != NULL)
2620 return frp->fr_win->w_p_wfw;
2621
2622 if (frp->fr_layout == FR_COL)
2623 {
2624 /* The frame is fixed width if one of the frames in the row is fixed
2625 * width. */
2626 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2627 if (frame_fixed_width(frp))
2628 return TRUE;
2629 return FALSE;
2630 }
2631
2632 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2633 * frames in the row are fixed width. */
2634 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2635 if (!frame_fixed_width(frp))
2636 return FALSE;
2637 return TRUE;
2638}
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * Add a status line to windows at the bottom of "frp".
2642 * Note: Does not check if there is room!
2643 */
2644 static void
2645frame_add_statusline(frp)
2646 frame_T *frp;
2647{
2648 win_T *wp;
2649
2650 if (frp->fr_layout == FR_LEAF)
2651 {
2652 wp = frp->fr_win;
2653 if (wp->w_status_height == 0)
2654 {
2655 if (wp->w_height > 0) /* don't make it negative */
2656 --wp->w_height;
2657 wp->w_status_height = STATUS_HEIGHT;
2658 }
2659 }
2660 else if (frp->fr_layout == FR_ROW)
2661 {
2662 /* Handle all the frames in the row. */
2663 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2664 frame_add_statusline(frp);
2665 }
2666 else /* frp->fr_layout == FR_COL */
2667 {
2668 /* Only need to handle the last frame in the column. */
2669 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2670 ;
2671 frame_add_statusline(frp);
2672 }
2673}
2674
2675/*
2676 * Set width of a frame. Handles recursively going through contained frames.
2677 * May remove separator line for windows at the right side (for win_close()).
2678 */
2679 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002680frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 frame_T *topfrp;
2682 int width;
2683 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002684 int wfw; /* obey 'winfixwidth' when there is a choice;
2685 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 frame_T *frp;
2688 int extra_cols;
2689 int w;
2690 win_T *wp;
2691
2692 if (topfrp->fr_layout == FR_LEAF)
2693 {
2694 /* Simple case: just one window. */
2695 wp = topfrp->fr_win;
2696 /* Find out if there are any windows right of this one. */
2697 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2698 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2699 break;
2700 if (frp->fr_parent == NULL)
2701 wp->w_vsep_width = 0;
2702 win_new_width(wp, width - wp->w_vsep_width);
2703 }
2704 else if (topfrp->fr_layout == FR_COL)
2705 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 do
2707 {
2708 /* All frames in this column get the same new width. */
2709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2710 {
2711 frame_new_width(frp, width, leftfirst, wfw);
2712 if (frp->fr_width > width)
2713 {
2714 /* Could not fit the windows, make whole column wider. */
2715 width = frp->fr_width;
2716 break;
2717 }
2718 }
2719 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721 else /* fr_layout == FR_ROW */
2722 {
2723 /* Complicated case: Resize a row of frames. Resize the rightmost
2724 * frame first, frames left of it when needed. */
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002727 if (wfw)
2728 /* Advance past frames with one window with 'wfw' set. */
2729 while (frame_fixed_width(frp))
2730 {
2731 frp = frp->fr_next;
2732 if (frp == NULL)
2733 return; /* no frame without 'wfw', give up */
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002736 {
2737 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 while (frp->fr_next != NULL)
2739 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002740 if (wfw)
2741 /* Advance back for frames with one window with 'wfw' set. */
2742 while (frame_fixed_width(frp))
2743 frp = frp->fr_prev;
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746 extra_cols = width - topfrp->fr_width;
2747 if (extra_cols < 0)
2748 {
2749 /* reduce frame width, rightmost frame first */
2750 while (frp != NULL)
2751 {
2752 w = frame_minwidth(frp, NULL);
2753 if (frp->fr_width + extra_cols < w)
2754 {
2755 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002756 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else
2759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002760 frame_new_width(frp, frp->fr_width + extra_cols,
2761 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 break;
2763 }
2764 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002765 {
2766 do
2767 frp = frp->fr_next;
2768 while (wfw && frp != NULL && frame_fixed_width(frp));
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002771 {
2772 do
2773 frp = frp->fr_prev;
2774 while (wfw && frp != NULL && frame_fixed_width(frp));
2775 }
2776 /* Increase "width" if we could not reduce enough frames. */
2777 if (frp == NULL)
2778 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781 else if (extra_cols > 0)
2782 {
2783 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002784 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
2786 }
2787 topfrp->fr_width = width;
2788}
2789
2790/*
2791 * Add the vertical separator to windows at the right side of "frp".
2792 * Note: Does not check if there is room!
2793 */
2794 static void
2795frame_add_vsep(frp)
2796 frame_T *frp;
2797{
2798 win_T *wp;
2799
2800 if (frp->fr_layout == FR_LEAF)
2801 {
2802 wp = frp->fr_win;
2803 if (wp->w_vsep_width == 0)
2804 {
2805 if (wp->w_width > 0) /* don't make it negative */
2806 --wp->w_width;
2807 wp->w_vsep_width = 1;
2808 }
2809 }
2810 else if (frp->fr_layout == FR_COL)
2811 {
2812 /* Handle all the frames in the column. */
2813 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2814 frame_add_vsep(frp);
2815 }
2816 else /* frp->fr_layout == FR_ROW */
2817 {
2818 /* Only need to handle the last frame in the row. */
2819 frp = frp->fr_child;
2820 while (frp->fr_next != NULL)
2821 frp = frp->fr_next;
2822 frame_add_vsep(frp);
2823 }
2824}
2825
2826/*
2827 * Set frame width from the window it contains.
2828 */
2829 static void
2830frame_fix_width(wp)
2831 win_T *wp;
2832{
2833 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2834}
2835#endif
2836
2837/*
2838 * Set frame height from the window it contains.
2839 */
2840 static void
2841frame_fix_height(wp)
2842 win_T *wp;
2843{
2844 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2845}
2846
2847/*
2848 * Compute the minimal height for frame "topfrp".
2849 * Uses the 'winminheight' option.
2850 * When "next_curwin" isn't NULL, use p_wh for this window.
2851 * When "next_curwin" is NOWIN, don't use at least one line for the current
2852 * window.
2853 */
2854 static int
2855frame_minheight(topfrp, next_curwin)
2856 frame_T *topfrp;
2857 win_T *next_curwin;
2858{
2859 frame_T *frp;
2860 int m;
2861#ifdef FEAT_VERTSPLIT
2862 int n;
2863#endif
2864
2865 if (topfrp->fr_win != NULL)
2866 {
2867 if (topfrp->fr_win == next_curwin)
2868 m = p_wh + topfrp->fr_win->w_status_height;
2869 else
2870 {
2871 /* window: minimal height of the window plus status line */
2872 m = p_wmh + topfrp->fr_win->w_status_height;
2873 /* Current window is minimal one line high */
2874 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2875 ++m;
2876 }
2877 }
2878#ifdef FEAT_VERTSPLIT
2879 else if (topfrp->fr_layout == FR_ROW)
2880 {
2881 /* get the minimal height from each frame in this row */
2882 m = 0;
2883 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2884 {
2885 n = frame_minheight(frp, next_curwin);
2886 if (n > m)
2887 m = n;
2888 }
2889 }
2890#endif
2891 else
2892 {
2893 /* Add up the minimal heights for all frames in this column. */
2894 m = 0;
2895 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2896 m += frame_minheight(frp, next_curwin);
2897 }
2898
2899 return m;
2900}
2901
2902#ifdef FEAT_VERTSPLIT
2903/*
2904 * Compute the minimal width for frame "topfrp".
2905 * When "next_curwin" isn't NULL, use p_wiw for this window.
2906 * When "next_curwin" is NOWIN, don't use at least one column for the current
2907 * window.
2908 */
2909 static int
2910frame_minwidth(topfrp, next_curwin)
2911 frame_T *topfrp;
2912 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2913{
2914 frame_T *frp;
2915 int m, n;
2916
2917 if (topfrp->fr_win != NULL)
2918 {
2919 if (topfrp->fr_win == next_curwin)
2920 m = p_wiw + topfrp->fr_win->w_vsep_width;
2921 else
2922 {
2923 /* window: minimal width of the window plus separator column */
2924 m = p_wmw + topfrp->fr_win->w_vsep_width;
2925 /* Current window is minimal one column wide */
2926 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2927 ++m;
2928 }
2929 }
2930 else if (topfrp->fr_layout == FR_COL)
2931 {
2932 /* get the minimal width from each frame in this column */
2933 m = 0;
2934 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2935 {
2936 n = frame_minwidth(frp, next_curwin);
2937 if (n > m)
2938 m = n;
2939 }
2940 }
2941 else
2942 {
2943 /* Add up the minimal widths for all frames in this row. */
2944 m = 0;
2945 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2946 m += frame_minwidth(frp, next_curwin);
2947 }
2948
2949 return m;
2950}
2951#endif
2952
2953
2954/*
2955 * Try to close all windows except current one.
2956 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2957 * used and the buffer was modified.
2958 *
2959 * Used by ":bdel" and ":only".
2960 */
2961 void
2962close_others(message, forceit)
2963 int message;
2964 int forceit; /* always hide all other windows */
2965{
2966 win_T *wp;
2967 win_T *nextwp;
2968 int r;
2969
2970 if (lastwin == firstwin)
2971 {
2972 if (message
2973#ifdef FEAT_AUTOCMD
2974 && !autocmd_busy
2975#endif
2976 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002977 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 return;
2979 }
2980
2981 /* Be very careful here: autocommands may change the window layout. */
2982 for (wp = firstwin; win_valid(wp); wp = nextwp)
2983 {
2984 nextwp = wp->w_next;
2985 if (wp != curwin) /* don't close current window */
2986 {
2987
2988 /* Check if it's allowed to abandon this window */
2989 r = can_abandon(wp->w_buffer, forceit);
2990#ifdef FEAT_AUTOCMD
2991 if (!win_valid(wp)) /* autocommands messed wp up */
2992 {
2993 nextwp = firstwin;
2994 continue;
2995 }
2996#endif
2997 if (!r)
2998 {
2999#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3000 if (message && (p_confirm || cmdmod.confirm) && p_write)
3001 {
3002 dialog_changed(wp->w_buffer, FALSE);
3003# ifdef FEAT_AUTOCMD
3004 if (!win_valid(wp)) /* autocommands messed wp up */
3005 {
3006 nextwp = firstwin;
3007 continue;
3008 }
3009# endif
3010 }
3011 if (bufIsChanged(wp->w_buffer))
3012#endif
3013 continue;
3014 }
3015 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3016 }
3017 }
3018
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003019 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 EMSG(_("E445: Other window contains changes"));
3021}
3022
3023#endif /* FEAT_WINDOWS */
3024
3025/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003026 * Init the current window "curwin".
3027 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
3029 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003030curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003032 redraw_win_later(curwin, NOT_VALID);
3033 curwin->w_lines_valid = 0;
3034 curwin->w_cursor.lnum = 1;
3035 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003037 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003039 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3040 curwin->w_pcmark.col = 0;
3041 curwin->w_prev_pcmark.lnum = 0;
3042 curwin->w_prev_pcmark.col = 0;
3043 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003045 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003047 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef FEAT_FKMAP
3049 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003050 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003052 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#endif
3054}
3055
3056/*
3057 * Allocate the first window and put an empty buffer in it.
3058 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003059 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003061 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062win_alloc_first()
3063{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003064 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003065 return FAIL;
3066
3067#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003068 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003069 if (first_tabpage == NULL)
3070 return FAIL;
3071 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003072 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073#endif
3074 return OK;
3075}
3076
3077/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003078 * Allocate the first window or the first window in a new tab page.
3079 * When "oldwin" is NULL create an empty buffer for it.
3080 * When "oldwin" is not NULL copy info from it to the new window (only with
3081 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 * Return FAIL when something goes wrong (out of memory).
3083 */
3084 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003085win_alloc_firstwin(oldwin)
3086 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003089 if (oldwin == NULL)
3090 {
3091 /* Very first window, need to create an empty buffer for it and
3092 * initialize from scratch. */
3093 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3094 if (curwin == NULL || curbuf == NULL)
3095 return FAIL;
3096 curwin->w_buffer = curbuf;
3097 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003099 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003101 curwin_init(); /* init current window */
3102 }
3103#ifdef FEAT_WINDOWS
3104 else
3105 {
3106 /* First window in new tab page, initialize it from "oldwin". */
3107 win_init(curwin, oldwin);
3108
3109# ifdef FEAT_SCROLLBIND
3110 /* We don't want scroll-binding in the first window. */
3111 curwin->w_p_scb = FALSE;
3112# endif
3113 }
3114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3117 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003118 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 topframe->fr_layout = FR_LEAF;
3120#ifdef FEAT_VERTSPLIT
3121 topframe->fr_width = Columns;
3122#endif
3123 topframe->fr_height = Rows - p_ch;
3124 topframe->fr_win = curwin;
3125 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126
3127 return OK;
3128}
3129
3130/*
3131 * Initialize the window and frame size to the maximum.
3132 */
3133 void
3134win_init_size()
3135{
3136 firstwin->w_height = ROWS_AVAIL;
3137 topframe->fr_height = ROWS_AVAIL;
3138#ifdef FEAT_VERTSPLIT
3139 firstwin->w_width = Columns;
3140 topframe->fr_width = Columns;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003145
3146/*
3147 * Allocate a new tabpage_T and init the values.
3148 * Returns NULL when out of memory.
3149 */
3150 static tabpage_T *
3151alloc_tabpage()
3152{
3153 tabpage_T *tp;
3154
3155 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3156 if (tp != NULL)
3157 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003158# ifdef FEAT_GUI
3159 int i;
3160
3161 for (i = 0; i < 3; i++)
3162 tp->tp_prev_which_scrollbars[i] = -1;
3163# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003164# ifdef FEAT_DIFF
3165 tp->tp_diff_invalid = TRUE;
3166# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003167#ifdef FEAT_EVAL
3168 /* init t: variables */
3169 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3170#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003171 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003172 }
3173 return tp;
3174}
3175
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003176 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003177free_tabpage(tp)
3178 tabpage_T *tp;
3179{
3180# ifdef FEAT_DIFF
3181 diff_clear(tp);
3182# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003183 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003184#ifdef FEAT_EVAL
3185 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3186#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003187 vim_free(tp);
3188}
3189
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003190/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003191 * Create a new Tab page with one window.
3192 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003193 * When "after" is 0 put it just after the current Tab page.
3194 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 * Return FAIL or OK.
3196 */
3197 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003198win_new_tabpage(after)
3199 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003201 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003203 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003205 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 if (newtp == NULL)
3207 return FAIL;
3208
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003209 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003210 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003211 {
3212 vim_free(newtp);
3213 return FAIL;
3214 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003215 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003216
3217 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003218 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003219 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003221 if (after == 1)
3222 {
3223 /* New tab page becomes the first one. */
3224 newtp->tp_next = first_tabpage;
3225 first_tabpage = newtp;
3226 }
3227 else
3228 {
3229 if (after > 0)
3230 {
3231 /* Put new tab page before tab page "after". */
3232 n = 2;
3233 for (tp = first_tabpage; tp->tp_next != NULL
3234 && n < after; tp = tp->tp_next)
3235 ++n;
3236 }
3237 newtp->tp_next = tp->tp_next;
3238 tp->tp_next = newtp;
3239 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003240 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003241 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003243
3244 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003245 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003246
3247#if defined(FEAT_GUI)
3248 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3249 * scrollbars. Have to update them anyway. */
3250 if (gui.in_use && starting == 0)
3251 {
3252 gui_init_which_components(NULL);
3253 gui_update_scrollbars(TRUE);
3254 }
3255 need_mouse_correct = TRUE;
3256#endif
3257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003258 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003259#ifdef FEAT_AUTOCMD
3260 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3261 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3262#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263 return OK;
3264 }
3265
3266 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003267 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003268 return FAIL;
3269}
3270
3271/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003272 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3273 * like with ":split".
3274 * Returns OK if a new tab page was created, FAIL otherwise.
3275 */
3276 int
3277may_open_tabpage()
3278{
3279 int n = cmdmod.tab;
3280
3281 if (cmdmod.tab != 0)
3282 {
3283 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3284 return win_new_tabpage(n);
3285 }
3286 return FAIL;
3287}
3288
3289/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003290 * Create up to "maxcount" tabpages with empty windows.
3291 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003292 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003293 int
3294make_tabpages(maxcount)
3295 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003296{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003297 int count = maxcount;
3298 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003299
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003300 /* Limit to 'tabpagemax' tabs. */
3301 if (count > p_tpm)
3302 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003303
3304#ifdef FEAT_AUTOCMD
3305 /*
3306 * Don't execute autocommands while creating the tab pages. Must do that
3307 * when putting the buffers in the windows.
3308 */
3309 ++autocmd_block;
3310#endif
3311
3312 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003313 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003314 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003315
3316#ifdef FEAT_AUTOCMD
3317 --autocmd_block;
3318#endif
3319
3320 /* return actual number of tab pages */
3321 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003322}
3323
3324/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003325 * Return TRUE when "tpc" points to a valid tab page.
3326 */
3327 int
3328valid_tabpage(tpc)
3329 tabpage_T *tpc;
3330{
3331 tabpage_T *tp;
3332
3333 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3334 if (tp == tpc)
3335 return TRUE;
3336 return FALSE;
3337}
3338
3339/*
3340 * Find tab page "n" (first one is 1). Returns NULL when not found.
3341 */
3342 tabpage_T *
3343find_tabpage(n)
3344 int n;
3345{
3346 tabpage_T *tp;
3347 int i = 1;
3348
3349 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3350 ++i;
3351 return tp;
3352}
3353
3354/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003355 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003356 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003357 */
3358 int
3359tabpage_index(ftp)
3360 tabpage_T *ftp;
3361{
3362 int i = 1;
3363 tabpage_T *tp;
3364
3365 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3366 ++i;
3367 return i;
3368}
3369
3370/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003371 * Prepare for leaving the current tab page.
3372 * When autocomands change "curtab" we don't leave the tab page and return
3373 * FAIL.
3374 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003376/*ARGSUSED*/
3377 static int
3378leave_tabpage(new_curbuf)
3379 buf_T *new_curbuf; /* what is going to be the new curbuf,
3380 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003381{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 tabpage_T *tp = curtab;
3383
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003384#ifdef FEAT_VISUAL
3385 reset_VIsual_and_resel(); /* stop Visual mode */
3386#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003387#ifdef FEAT_AUTOCMD
3388 if (new_curbuf != curbuf)
3389 {
3390 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3391 if (curtab != tp)
3392 return FAIL;
3393 }
3394 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3395 if (curtab != tp)
3396 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398 if (curtab != tp)
3399 return FAIL;
3400#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003401#if defined(FEAT_GUI)
3402 /* Remove the scrollbars. They may be added back later. */
3403 if (gui.in_use)
3404 gui_remove_scrollbars();
3405#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003406 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003407 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 tp->tp_firstwin = firstwin;
3409 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003410 tp->tp_old_Rows = Rows;
3411 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412 firstwin = NULL;
3413 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003414 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415}
3416
3417/*
3418 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003419 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 */
3421/*ARGSUSED*/
3422 static void
3423enter_tabpage(tp, old_curbuf)
3424 tabpage_T *tp;
3425 buf_T *old_curbuf;
3426{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003427 int old_off = tp->tp_firstwin->w_winrow;
3428
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003429 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430 firstwin = tp->tp_firstwin;
3431 lastwin = tp->tp_lastwin;
3432 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433#ifdef FEAT_AUTOCMD
3434 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3435#endif
3436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003437 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003438 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003439
3440#ifdef FEAT_AUTOCMD
3441 if (old_curbuf != curbuf)
3442 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3443#endif
3444
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003445 last_status(FALSE); /* status line may appear or disappear */
3446 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 must_redraw = CLEAR; /* need to redraw everything */
3448#ifdef FEAT_DIFF
3449 diff_need_scrollbind = TRUE;
3450#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003452 /* The tabpage line may have appeared or disappeared, may need to resize
3453 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003454 * frame sizes too. Use the stored value of p_ch, so that it can be
3455 * different for each tab page. */
3456 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003457 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3458#ifdef FEAT_GUI_TABLINE
3459 && !gui_use_tabline()
3460#endif
3461 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003462 shell_new_rows();
3463#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003464 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003465 shell_new_columns(); /* update window widths */
3466#endif
3467
3468#if defined(FEAT_GUI)
3469 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3470 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003471 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003472 {
3473 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003474 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003475 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003476 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477#endif
3478
3479 redraw_all_later(CLEAR);
3480}
3481
3482/*
3483 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003485 */
3486 void
3487goto_tabpage(n)
3488 int n;
3489{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003491 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003492 int i;
3493
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003494 /* If there is only one it can't work. */
3495 if (first_tabpage->tp_next == NULL)
3496 {
3497 if (n > 1)
3498 beep_flush();
3499 return;
3500 }
3501
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 if (n == 0)
3503 {
3504 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003505 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 tp = first_tabpage;
3507 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003508 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003509 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003510 else if (n < 0)
3511 {
3512 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3513 * this N times. */
3514 ttp = curtab;
3515 for (i = n; i < 0; ++i)
3516 {
3517 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3518 tp = tp->tp_next)
3519 ;
3520 ttp = tp;
3521 }
3522 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003523 else if (n == 9999)
3524 {
3525 /* Go to last tab page. */
3526 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3527 ;
3528 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529 else
3530 {
3531 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003532 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003533 if (tp == NULL)
3534 {
3535 beep_flush();
3536 return;
3537 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 }
3539
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003540 goto_tabpage_tp(tp);
3541
3542#ifdef FEAT_GUI_TABLINE
3543 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003544 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003545#endif
3546}
3547
3548/*
3549 * Go to tabpage "tp".
3550 * Note: doesn't update the GUI tab.
3551 */
3552 void
3553goto_tabpage_tp(tp)
3554 tabpage_T *tp;
3555{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003556 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003557 {
3558 if (valid_tabpage(tp))
3559 enter_tabpage(tp, curbuf);
3560 else
3561 enter_tabpage(curtab, curbuf);
3562 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003566 * Enter window "wp" in tab page "tp".
3567 * Also updates the GUI tab.
3568 */
3569 void
3570goto_tabpage_win(tp, wp)
3571 tabpage_T *tp;
3572 win_T *wp;
3573{
3574 goto_tabpage_tp(tp);
3575 if (curtab == tp && win_valid(wp))
3576 {
3577 win_enter(wp, TRUE);
3578# ifdef FEAT_GUI_TABLINE
3579 if (gui_use_tabline())
3580 gui_mch_set_curtab(tabpage_index(curtab));
3581# endif
3582 }
3583}
3584
3585/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003586 * Move the current tab page to before tab page "nr".
3587 */
3588 void
3589tabpage_move(nr)
3590 int nr;
3591{
3592 int n = nr;
3593 tabpage_T *tp;
3594
3595 if (first_tabpage->tp_next == NULL)
3596 return;
3597
3598 /* Remove the current tab page from the list of tab pages. */
3599 if (curtab == first_tabpage)
3600 first_tabpage = curtab->tp_next;
3601 else
3602 {
3603 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3604 if (tp->tp_next == curtab)
3605 break;
3606 if (tp == NULL) /* "cannot happen" */
3607 return;
3608 tp->tp_next = curtab->tp_next;
3609 }
3610
3611 /* Re-insert it at the specified position. */
3612 if (n == 0)
3613 {
3614 curtab->tp_next = first_tabpage;
3615 first_tabpage = curtab;
3616 }
3617 else
3618 {
3619 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3620 --n;
3621 curtab->tp_next = tp->tp_next;
3622 tp->tp_next = curtab;
3623 }
3624
3625 /* Need to redraw the tabline. Tab page contents doesn't change. */
3626 redraw_tabline = TRUE;
3627}
3628
3629
3630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 * Go to another window.
3632 * When jumping to another buffer, stop Visual mode. Do this before
3633 * changing windows so we can yank the selection into the '*' register.
3634 * When jumping to another window on the same buffer, adjust its cursor
3635 * position to keep the same Visual area.
3636 */
3637 void
3638win_goto(wp)
3639 win_T *wp;
3640{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003641 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003644 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 return;
3646 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003647#ifdef FEAT_AUTOCMD
3648 if (curbuf_locked())
3649 return;
3650#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652#ifdef FEAT_VISUAL
3653 if (wp->w_buffer != curbuf)
3654 reset_VIsual_and_resel();
3655 else if (VIsual_active)
3656 wp->w_cursor = curwin->w_cursor;
3657#endif
3658
3659#ifdef FEAT_GUI
3660 need_mouse_correct = TRUE;
3661#endif
3662 win_enter(wp, TRUE);
3663}
3664
3665#if defined(FEAT_PERL) || defined(PROTO)
3666/*
3667 * Find window number "winnr" (counting top to bottom).
3668 */
3669 win_T *
3670win_find_nr(winnr)
3671 int winnr;
3672{
3673 win_T *wp;
3674
3675# ifdef FEAT_WINDOWS
3676 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3677 if (--winnr == 0)
3678 break;
3679 return wp;
3680# else
3681 return curwin;
3682# endif
3683}
3684#endif
3685
3686#ifdef FEAT_VERTSPLIT
3687/*
3688 * Move to window above or below "count" times.
3689 */
3690 static void
3691win_goto_ver(up, count)
3692 int up; /* TRUE to go to win above */
3693 long count;
3694{
3695 frame_T *fr;
3696 frame_T *nfr;
3697 frame_T *foundfr;
3698
3699 foundfr = curwin->w_frame;
3700 while (count--)
3701 {
3702 /*
3703 * First go upwards in the tree of frames until we find a upwards or
3704 * downwards neighbor.
3705 */
3706 fr = foundfr;
3707 for (;;)
3708 {
3709 if (fr == topframe)
3710 goto end;
3711 if (up)
3712 nfr = fr->fr_prev;
3713 else
3714 nfr = fr->fr_next;
3715 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3716 break;
3717 fr = fr->fr_parent;
3718 }
3719
3720 /*
3721 * Now go downwards to find the bottom or top frame in it.
3722 */
3723 for (;;)
3724 {
3725 if (nfr->fr_layout == FR_LEAF)
3726 {
3727 foundfr = nfr;
3728 break;
3729 }
3730 fr = nfr->fr_child;
3731 if (nfr->fr_layout == FR_ROW)
3732 {
3733 /* Find the frame at the cursor row. */
3734 while (fr->fr_next != NULL
3735 && frame2win(fr)->w_wincol + fr->fr_width
3736 <= curwin->w_wincol + curwin->w_wcol)
3737 fr = fr->fr_next;
3738 }
3739 if (nfr->fr_layout == FR_COL && up)
3740 while (fr->fr_next != NULL)
3741 fr = fr->fr_next;
3742 nfr = fr;
3743 }
3744 }
3745end:
3746 if (foundfr != NULL)
3747 win_goto(foundfr->fr_win);
3748}
3749
3750/*
3751 * Move to left or right window.
3752 */
3753 static void
3754win_goto_hor(left, count)
3755 int left; /* TRUE to go to left win */
3756 long count;
3757{
3758 frame_T *fr;
3759 frame_T *nfr;
3760 frame_T *foundfr;
3761
3762 foundfr = curwin->w_frame;
3763 while (count--)
3764 {
3765 /*
3766 * First go upwards in the tree of frames until we find a left or
3767 * right neighbor.
3768 */
3769 fr = foundfr;
3770 for (;;)
3771 {
3772 if (fr == topframe)
3773 goto end;
3774 if (left)
3775 nfr = fr->fr_prev;
3776 else
3777 nfr = fr->fr_next;
3778 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3779 break;
3780 fr = fr->fr_parent;
3781 }
3782
3783 /*
3784 * Now go downwards to find the leftmost or rightmost frame in it.
3785 */
3786 for (;;)
3787 {
3788 if (nfr->fr_layout == FR_LEAF)
3789 {
3790 foundfr = nfr;
3791 break;
3792 }
3793 fr = nfr->fr_child;
3794 if (nfr->fr_layout == FR_COL)
3795 {
3796 /* Find the frame at the cursor row. */
3797 while (fr->fr_next != NULL
3798 && frame2win(fr)->w_winrow + fr->fr_height
3799 <= curwin->w_winrow + curwin->w_wrow)
3800 fr = fr->fr_next;
3801 }
3802 if (nfr->fr_layout == FR_ROW && left)
3803 while (fr->fr_next != NULL)
3804 fr = fr->fr_next;
3805 nfr = fr;
3806 }
3807 }
3808end:
3809 if (foundfr != NULL)
3810 win_goto(foundfr->fr_win);
3811}
3812#endif
3813
3814/*
3815 * Make window "wp" the current window.
3816 */
3817 void
3818win_enter(wp, undo_sync)
3819 win_T *wp;
3820 int undo_sync;
3821{
3822 win_enter_ext(wp, undo_sync, FALSE);
3823}
3824
3825/*
3826 * Make window wp the current window.
3827 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3828 * been closed and isn't valid.
3829 */
3830 static void
3831win_enter_ext(wp, undo_sync, curwin_invalid)
3832 win_T *wp;
3833 int undo_sync;
3834 int curwin_invalid;
3835{
3836#ifdef FEAT_AUTOCMD
3837 int other_buffer = FALSE;
3838#endif
3839
3840 if (wp == curwin && !curwin_invalid) /* nothing to do */
3841 return;
3842
3843#ifdef FEAT_AUTOCMD
3844 if (!curwin_invalid)
3845 {
3846 /*
3847 * Be careful: If autocommands delete the window, return now.
3848 */
3849 if (wp->w_buffer != curbuf)
3850 {
3851 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3852 other_buffer = TRUE;
3853 if (!win_valid(wp))
3854 return;
3855 }
3856 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3857 if (!win_valid(wp))
3858 return;
3859# ifdef FEAT_EVAL
3860 /* autocmds may abort script processing */
3861 if (aborting())
3862 return;
3863# endif
3864 }
3865#endif
3866
3867 /* sync undo before leaving the current buffer */
3868 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003869 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 /* may have to copy the buffer options when 'cpo' contains 'S' */
3871 if (wp->w_buffer != curbuf)
3872 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3873 if (!curwin_invalid)
3874 {
3875 prevwin = curwin; /* remember for CTRL-W p */
3876 curwin->w_redr_status = TRUE;
3877 }
3878 curwin = wp;
3879 curbuf = wp->w_buffer;
3880 check_cursor();
3881#ifdef FEAT_VIRTUALEDIT
3882 if (!virtual_active())
3883 curwin->w_cursor.coladd = 0;
3884#endif
3885 changed_line_abv_curs(); /* assume cursor position needs updating */
3886
3887 if (curwin->w_localdir != NULL)
3888 {
3889 /* Window has a local directory: Save current directory as global
3890 * directory (unless that was done already) and change to the local
3891 * directory. */
3892 if (globaldir == NULL)
3893 {
3894 char_u cwd[MAXPATHL];
3895
3896 if (mch_dirname(cwd, MAXPATHL) == OK)
3897 globaldir = vim_strsave(cwd);
3898 }
3899 mch_chdir((char *)curwin->w_localdir);
3900 shorten_fnames(TRUE);
3901 }
3902 else if (globaldir != NULL)
3903 {
3904 /* Window doesn't have a local directory and we are not in the global
3905 * directory: Change to the global directory. */
3906 mch_chdir((char *)globaldir);
3907 vim_free(globaldir);
3908 globaldir = NULL;
3909 shorten_fnames(TRUE);
3910 }
3911
3912#ifdef FEAT_AUTOCMD
3913 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3914 if (other_buffer)
3915 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3916#endif
3917
3918#ifdef FEAT_TITLE
3919 maketitle();
3920#endif
3921 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003922 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if (restart_edit)
3924 redraw_later(VALID); /* causes status line redraw */
3925
3926 /* set window height to desired minimal value */
3927 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3928 win_setheight((int)p_wh);
3929 else if (curwin->w_height == 0)
3930 win_setheight(1);
3931
3932#ifdef FEAT_VERTSPLIT
3933 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003934 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 win_setwidth((int)p_wiw);
3936#endif
3937
3938#ifdef FEAT_MOUSE
3939 setmouse(); /* in case jumped to/from help buffer */
3940#endif
3941
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003942#ifdef FEAT_AUTOCHDIR
3943 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 * switching windows. */
3945 if (p_acd && curbuf->b_ffname != NULL
3946 && vim_chdirfile(curbuf->b_ffname) == OK)
3947 shorten_fnames(TRUE);
3948#endif
3949}
3950
3951#endif /* FEAT_WINDOWS */
3952
3953#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3954/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003955 * Jump to the first open window that contains buffer "buf", if one exists.
3956 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 */
3958 win_T *
3959buf_jump_open_win(buf)
3960 buf_T *buf;
3961{
3962# ifdef FEAT_WINDOWS
3963 win_T *wp;
3964
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003965 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 if (wp->w_buffer == buf)
3967 break;
3968 if (wp != NULL)
3969 win_enter(wp, FALSE);
3970 return wp;
3971# else
3972 if (curwin->w_buffer == buf)
3973 return curwin;
3974 return NULL;
3975# endif
3976}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003977
3978/*
3979 * Jump to the first open window in any tab page that contains buffer "buf",
3980 * if one exists.
3981 * Returns a pointer to the window found, otherwise NULL.
3982 */
3983 win_T *
3984buf_jump_open_tab(buf)
3985 buf_T *buf;
3986{
3987# ifdef FEAT_WINDOWS
3988 win_T *wp;
3989 tabpage_T *tp;
3990
3991 /* First try the current tab page. */
3992 wp = buf_jump_open_win(buf);
3993 if (wp != NULL)
3994 return wp;
3995
3996 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3997 if (tp != curtab)
3998 {
3999 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4000 if (wp->w_buffer == buf)
4001 break;
4002 if (wp != NULL)
4003 {
4004 goto_tabpage_win(tp, wp);
4005 if (curwin != wp)
4006 wp = NULL; /* something went wrong */
4007 break;
4008 }
4009 }
4010
4011 return wp;
4012# else
4013 if (curwin->w_buffer == buf)
4014 return curwin;
4015 return NULL;
4016# endif
4017}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018#endif
4019
4020/*
4021 * allocate a window structure and link it in the window list
4022 */
4023/*ARGSUSED*/
4024 static win_T *
4025win_alloc(after)
4026 win_T *after;
4027{
4028 win_T *newwin;
4029
4030 /*
4031 * allocate window structure and linesizes arrays
4032 */
4033 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4034 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4035 {
4036 vim_free(newwin);
4037 newwin = NULL;
4038 }
4039
4040 if (newwin != NULL)
4041 {
4042 /*
4043 * link the window in the window list
4044 */
4045#ifdef FEAT_WINDOWS
4046 win_append(after, newwin);
4047#endif
4048#ifdef FEAT_VERTSPLIT
4049 newwin->w_wincol = 0;
4050 newwin->w_width = Columns;
4051#endif
4052
4053 /* position the display and the cursor at the top of the file. */
4054 newwin->w_topline = 1;
4055#ifdef FEAT_DIFF
4056 newwin->w_topfill = 0;
4057#endif
4058 newwin->w_botline = 2;
4059 newwin->w_cursor.lnum = 1;
4060#ifdef FEAT_SCROLLBIND
4061 newwin->w_scbind_pos = 1;
4062#endif
4063
4064 /* We won't calculate w_fraction until resizing the window */
4065 newwin->w_fraction = 0;
4066 newwin->w_prev_fraction_row = -1;
4067
4068#ifdef FEAT_GUI
4069 if (gui.in_use)
4070 {
4071 out_flush();
4072 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4073 SBAR_LEFT, newwin);
4074 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4075 SBAR_RIGHT, newwin);
4076 }
4077#endif
4078#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004079 /* init w: variables */
4080 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#endif
4082#ifdef FEAT_FOLDING
4083 foldInitWin(newwin);
4084#endif
4085 }
4086 return newwin;
4087}
4088
4089#if defined(FEAT_WINDOWS) || defined(PROTO)
4090
4091/*
4092 * remove window 'wp' from the window list and free the structure
4093 */
4094 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004095win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004097 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 int i;
4100
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004101#ifdef FEAT_MZSCHEME
4102 mzscheme_window_free(wp);
4103#endif
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#ifdef FEAT_PERL
4106 perl_win_free(wp);
4107#endif
4108
4109#ifdef FEAT_PYTHON
4110 python_window_free(wp);
4111#endif
4112
4113#ifdef FEAT_TCL
4114 tcl_window_free(wp);
4115#endif
4116
4117#ifdef FEAT_RUBY
4118 ruby_window_free(wp);
4119#endif
4120
4121 clear_winopt(&wp->w_onebuf_opt);
4122 clear_winopt(&wp->w_allbuf_opt);
4123
4124#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004125 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#endif
4127
4128 if (prevwin == wp)
4129 prevwin = NULL;
4130 win_free_lsize(wp);
4131
4132 for (i = 0; i < wp->w_tagstacklen; ++i)
4133 vim_free(wp->w_tagstack[i].tagname);
4134
4135 vim_free(wp->w_localdir);
4136#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004137 vim_free(wp->w_match[0].regprog);
4138 vim_free(wp->w_match[1].regprog);
4139 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#endif
4141#ifdef FEAT_JUMPLIST
4142 free_jumplist(wp);
4143#endif
4144
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004145#ifdef FEAT_QUICKFIX
4146 qf_free_all(wp);
4147#endif
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#ifdef FEAT_GUI
4150 if (gui.in_use)
4151 {
4152 out_flush();
4153 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4154 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4155 }
4156#endif /* FEAT_GUI */
4157
Bram Moolenaarf740b292006-02-16 22:11:02 +00004158 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 vim_free(wp);
4160}
4161
4162/*
4163 * Append window "wp" in the window list after window "after".
4164 */
4165 static void
4166win_append(after, wp)
4167 win_T *after, *wp;
4168{
4169 win_T *before;
4170
4171 if (after == NULL) /* after NULL is in front of the first */
4172 before = firstwin;
4173 else
4174 before = after->w_next;
4175
4176 wp->w_next = before;
4177 wp->w_prev = after;
4178 if (after == NULL)
4179 firstwin = wp;
4180 else
4181 after->w_next = wp;
4182 if (before == NULL)
4183 lastwin = wp;
4184 else
4185 before->w_prev = wp;
4186}
4187
4188/*
4189 * Remove a window from the window list.
4190 */
4191 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004192win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004194 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196 if (wp->w_prev != NULL)
4197 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004198 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004200 else
4201 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (wp->w_next != NULL)
4203 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004204 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004206 else
4207 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208}
4209
4210/*
4211 * Append frame "frp" in a frame list after frame "after".
4212 */
4213 static void
4214frame_append(after, frp)
4215 frame_T *after, *frp;
4216{
4217 frp->fr_next = after->fr_next;
4218 after->fr_next = frp;
4219 if (frp->fr_next != NULL)
4220 frp->fr_next->fr_prev = frp;
4221 frp->fr_prev = after;
4222}
4223
4224/*
4225 * Insert frame "frp" in a frame list before frame "before".
4226 */
4227 static void
4228frame_insert(before, frp)
4229 frame_T *before, *frp;
4230{
4231 frp->fr_next = before;
4232 frp->fr_prev = before->fr_prev;
4233 before->fr_prev = frp;
4234 if (frp->fr_prev != NULL)
4235 frp->fr_prev->fr_next = frp;
4236 else
4237 frp->fr_parent->fr_child = frp;
4238}
4239
4240/*
4241 * Remove a frame from a frame list.
4242 */
4243 static void
4244frame_remove(frp)
4245 frame_T *frp;
4246{
4247 if (frp->fr_prev != NULL)
4248 frp->fr_prev->fr_next = frp->fr_next;
4249 else
4250 frp->fr_parent->fr_child = frp->fr_next;
4251 if (frp->fr_next != NULL)
4252 frp->fr_next->fr_prev = frp->fr_prev;
4253}
4254
4255#endif /* FEAT_WINDOWS */
4256
4257/*
4258 * Allocate w_lines[] for window "wp".
4259 * Return FAIL for failure, OK for success.
4260 */
4261 int
4262win_alloc_lines(wp)
4263 win_T *wp;
4264{
4265 wp->w_lines_valid = 0;
4266 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4267 if (wp->w_lines == NULL)
4268 return FAIL;
4269 return OK;
4270}
4271
4272/*
4273 * free lsize arrays for a window
4274 */
4275 void
4276win_free_lsize(wp)
4277 win_T *wp;
4278{
4279 vim_free(wp->w_lines);
4280 wp->w_lines = NULL;
4281}
4282
4283/*
4284 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004285 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
4287 void
4288shell_new_rows()
4289{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004290 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 if (firstwin == NULL) /* not initialized yet */
4293 return;
4294#ifdef FEAT_WINDOWS
4295 if (h < frame_minheight(topframe, NULL))
4296 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004297
4298 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 * that doesn't result in the right height, forget about that option. */
4300 frame_new_height(topframe, h, FALSE, TRUE);
4301 if (topframe->fr_height != h)
4302 frame_new_height(topframe, h, FALSE, FALSE);
4303
4304 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4305#else
4306 if (h < 1)
4307 h = 1;
4308 win_new_height(firstwin, h);
4309#endif
4310 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004311#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004312 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#if 0
4316 /* Disabled: don't want making the screen smaller make a window larger. */
4317 if (p_ea)
4318 win_equal(curwin, FALSE, 'v');
4319#endif
4320}
4321
4322#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4323/*
4324 * Called from win_new_shellsize() after Columns changed.
4325 */
4326 void
4327shell_new_columns()
4328{
4329 if (firstwin == NULL) /* not initialized yet */
4330 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004331
4332 /* First try setting the widths of windows with 'winfixwidth'. If that
4333 * doesn't result in the right width, forget about that option. */
4334 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4335 if (topframe->fr_width != Columns)
4336 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4337
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4339#if 0
4340 /* Disabled: don't want making the screen smaller make a window larger. */
4341 if (p_ea)
4342 win_equal(curwin, FALSE, 'h');
4343#endif
4344}
4345#endif
4346
4347#if defined(FEAT_CMDWIN) || defined(PROTO)
4348/*
4349 * Save the size of all windows in "gap".
4350 */
4351 void
4352win_size_save(gap)
4353 garray_T *gap;
4354
4355{
4356 win_T *wp;
4357
4358 ga_init2(gap, (int)sizeof(int), 1);
4359 if (ga_grow(gap, win_count() * 2) == OK)
4360 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4361 {
4362 ((int *)gap->ga_data)[gap->ga_len++] =
4363 wp->w_width + wp->w_vsep_width;
4364 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4365 }
4366}
4367
4368/*
4369 * Restore window sizes, but only if the number of windows is still the same.
4370 * Does not free the growarray.
4371 */
4372 void
4373win_size_restore(gap)
4374 garray_T *gap;
4375{
4376 win_T *wp;
4377 int i;
4378
4379 if (win_count() * 2 == gap->ga_len)
4380 {
4381 i = 0;
4382 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4383 {
4384 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4385 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4386 }
4387 /* recompute the window positions */
4388 (void)win_comp_pos();
4389 }
4390}
4391#endif /* FEAT_CMDWIN */
4392
4393#if defined(FEAT_WINDOWS) || defined(PROTO)
4394/*
4395 * Update the position for all windows, using the width and height of the
4396 * frames.
4397 * Returns the row just after the last window.
4398 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004399 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400win_comp_pos()
4401{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004402 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 int col = 0;
4404
4405 frame_comp_pos(topframe, &row, &col);
4406 return row;
4407}
4408
4409/*
4410 * Update the position of the windows in frame "topfrp", using the width and
4411 * height of the frames.
4412 * "*row" and "*col" are the top-left position of the frame. They are updated
4413 * to the bottom-right position plus one.
4414 */
4415 static void
4416frame_comp_pos(topfrp, row, col)
4417 frame_T *topfrp;
4418 int *row;
4419 int *col;
4420{
4421 win_T *wp;
4422 frame_T *frp;
4423#ifdef FEAT_VERTSPLIT
4424 int startcol;
4425 int startrow;
4426#endif
4427
4428 wp = topfrp->fr_win;
4429 if (wp != NULL)
4430 {
4431 if (wp->w_winrow != *row
4432#ifdef FEAT_VERTSPLIT
4433 || wp->w_wincol != *col
4434#endif
4435 )
4436 {
4437 /* position changed, redraw */
4438 wp->w_winrow = *row;
4439#ifdef FEAT_VERTSPLIT
4440 wp->w_wincol = *col;
4441#endif
4442 redraw_win_later(wp, NOT_VALID);
4443 wp->w_redr_status = TRUE;
4444 }
4445 *row += wp->w_height + wp->w_status_height;
4446#ifdef FEAT_VERTSPLIT
4447 *col += wp->w_width + wp->w_vsep_width;
4448#endif
4449 }
4450 else
4451 {
4452#ifdef FEAT_VERTSPLIT
4453 startrow = *row;
4454 startcol = *col;
4455#endif
4456 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4457 {
4458#ifdef FEAT_VERTSPLIT
4459 if (topfrp->fr_layout == FR_ROW)
4460 *row = startrow; /* all frames are at the same row */
4461 else
4462 *col = startcol; /* all frames are at the same col */
4463#endif
4464 frame_comp_pos(frp, row, col);
4465 }
4466 }
4467}
4468
4469#endif /* FEAT_WINDOWS */
4470
4471/*
4472 * Set current window height and take care of repositioning other windows to
4473 * fit around it.
4474 */
4475 void
4476win_setheight(height)
4477 int height;
4478{
4479 win_setheight_win(height, curwin);
4480}
4481
4482/*
4483 * Set the window height of window "win" and take care of repositioning other
4484 * windows to fit around it.
4485 */
4486 void
4487win_setheight_win(height, win)
4488 int height;
4489 win_T *win;
4490{
4491 int row;
4492
4493 if (win == curwin)
4494 {
4495 /* Always keep current window at least one line high, even when
4496 * 'winminheight' is zero. */
4497#ifdef FEAT_WINDOWS
4498 if (height < p_wmh)
4499 height = p_wmh;
4500#endif
4501 if (height == 0)
4502 height = 1;
4503 }
4504
4505#ifdef FEAT_WINDOWS
4506 frame_setheight(win->w_frame, height + win->w_status_height);
4507
4508 /* recompute the window positions */
4509 row = win_comp_pos();
4510#else
4511 if (height > topframe->fr_height)
4512 height = topframe->fr_height;
4513 win->w_height = height;
4514 row = height;
4515#endif
4516
4517 /*
4518 * If there is extra space created between the last window and the command
4519 * line, clear it.
4520 */
4521 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4522 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4523 cmdline_row = row;
4524 msg_row = row;
4525 msg_col = 0;
4526
4527 redraw_all_later(NOT_VALID);
4528}
4529
4530#if defined(FEAT_WINDOWS) || defined(PROTO)
4531
4532/*
4533 * Set the height of a frame to "height" and take care that all frames and
4534 * windows inside it are resized. Also resize frames on the left and right if
4535 * the are in the same FR_ROW frame.
4536 *
4537 * Strategy:
4538 * If the frame is part of a FR_COL frame, try fitting the frame in that
4539 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4540 * go to containing frames to resize them and make room.
4541 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4542 * Check for the minimal height of the FR_ROW frame.
4543 * At the top level we can also use change the command line height.
4544 */
4545 static void
4546frame_setheight(curfrp, height)
4547 frame_T *curfrp;
4548 int height;
4549{
4550 int room; /* total number of lines available */
4551 int take; /* number of lines taken from other windows */
4552 int room_cmdline; /* lines available from cmdline */
4553 int run;
4554 frame_T *frp;
4555 int h;
4556 int room_reserved;
4557
4558 /* If the height already is the desired value, nothing to do. */
4559 if (curfrp->fr_height == height)
4560 return;
4561
4562 if (curfrp->fr_parent == NULL)
4563 {
4564 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004565 if (height > ROWS_AVAIL)
4566 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (height > 0)
4568 frame_new_height(curfrp, height, FALSE, FALSE);
4569 }
4570 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4571 {
4572 /* Row of frames: Also need to resize frames left and right of this
4573 * one. First check for the minimal height of these. */
4574 h = frame_minheight(curfrp->fr_parent, NULL);
4575 if (height < h)
4576 height = h;
4577 frame_setheight(curfrp->fr_parent, height);
4578 }
4579 else
4580 {
4581 /*
4582 * Column of frames: try to change only frames in this column.
4583 */
4584#ifdef FEAT_VERTSPLIT
4585 /*
4586 * Do this twice:
4587 * 1: compute room available, if it's not enough try resizing the
4588 * containing frame.
4589 * 2: compute the room available and adjust the height to it.
4590 * Try not to reduce the height of a window with 'winfixheight' set.
4591 */
4592 for (run = 1; run <= 2; ++run)
4593#else
4594 for (;;)
4595#endif
4596 {
4597 room = 0;
4598 room_reserved = 0;
4599 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4600 frp = frp->fr_next)
4601 {
4602 if (frp != curfrp
4603 && frp->fr_win != NULL
4604 && frp->fr_win->w_p_wfh)
4605 room_reserved += frp->fr_height;
4606 room += frp->fr_height;
4607 if (frp != curfrp)
4608 room -= frame_minheight(frp, NULL);
4609 }
4610#ifdef FEAT_VERTSPLIT
4611 if (curfrp->fr_width != Columns)
4612 room_cmdline = 0;
4613 else
4614#endif
4615 {
4616 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4617 + lastwin->w_height + lastwin->w_status_height);
4618 if (room_cmdline < 0)
4619 room_cmdline = 0;
4620 }
4621
4622 if (height <= room + room_cmdline)
4623 break;
4624#ifdef FEAT_VERTSPLIT
4625 if (run == 2 || curfrp->fr_width == Columns)
4626#endif
4627 {
4628 if (height > room + room_cmdline)
4629 height = room + room_cmdline;
4630 break;
4631 }
4632#ifdef FEAT_VERTSPLIT
4633 frame_setheight(curfrp->fr_parent, height
4634 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4635#endif
4636 /*NOTREACHED*/
4637 }
4638
4639 /*
4640 * Compute the number of lines we will take from others frames (can be
4641 * negative!).
4642 */
4643 take = height - curfrp->fr_height;
4644
4645 /* If there is not enough room, also reduce the height of a window
4646 * with 'winfixheight' set. */
4647 if (height > room + room_cmdline - room_reserved)
4648 room_reserved = room + room_cmdline - height;
4649 /* If there is only a 'winfixheight' window and making the
4650 * window smaller, need to make the other window taller. */
4651 if (take < 0 && room - curfrp->fr_height < room_reserved)
4652 room_reserved = 0;
4653
4654 if (take > 0 && room_cmdline > 0)
4655 {
4656 /* use lines from cmdline first */
4657 if (take < room_cmdline)
4658 room_cmdline = take;
4659 take -= room_cmdline;
4660 topframe->fr_height += room_cmdline;
4661 }
4662
4663 /*
4664 * set the current frame to the new height
4665 */
4666 frame_new_height(curfrp, height, FALSE, FALSE);
4667
4668 /*
4669 * First take lines from the frames after the current frame. If
4670 * that is not enough, takes lines from frames above the current
4671 * frame.
4672 */
4673 for (run = 0; run < 2; ++run)
4674 {
4675 if (run == 0)
4676 frp = curfrp->fr_next; /* 1st run: start with next window */
4677 else
4678 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4679 while (frp != NULL && take != 0)
4680 {
4681 h = frame_minheight(frp, NULL);
4682 if (room_reserved > 0
4683 && frp->fr_win != NULL
4684 && frp->fr_win->w_p_wfh)
4685 {
4686 if (room_reserved >= frp->fr_height)
4687 room_reserved -= frp->fr_height;
4688 else
4689 {
4690 if (frp->fr_height - room_reserved > take)
4691 room_reserved = frp->fr_height - take;
4692 take -= frp->fr_height - room_reserved;
4693 frame_new_height(frp, room_reserved, FALSE, FALSE);
4694 room_reserved = 0;
4695 }
4696 }
4697 else
4698 {
4699 if (frp->fr_height - take < h)
4700 {
4701 take -= frp->fr_height - h;
4702 frame_new_height(frp, h, FALSE, FALSE);
4703 }
4704 else
4705 {
4706 frame_new_height(frp, frp->fr_height - take,
4707 FALSE, FALSE);
4708 take = 0;
4709 }
4710 }
4711 if (run == 0)
4712 frp = frp->fr_next;
4713 else
4714 frp = frp->fr_prev;
4715 }
4716 }
4717 }
4718}
4719
4720#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4721/*
4722 * Set current window width and take care of repositioning other windows to
4723 * fit around it.
4724 */
4725 void
4726win_setwidth(width)
4727 int width;
4728{
4729 win_setwidth_win(width, curwin);
4730}
4731
4732 void
4733win_setwidth_win(width, wp)
4734 int width;
4735 win_T *wp;
4736{
4737 /* Always keep current window at least one column wide, even when
4738 * 'winminwidth' is zero. */
4739 if (wp == curwin)
4740 {
4741 if (width < p_wmw)
4742 width = p_wmw;
4743 if (width == 0)
4744 width = 1;
4745 }
4746
4747 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4748
4749 /* recompute the window positions */
4750 (void)win_comp_pos();
4751
4752 redraw_all_later(NOT_VALID);
4753}
4754
4755/*
4756 * Set the width of a frame to "width" and take care that all frames and
4757 * windows inside it are resized. Also resize frames above and below if the
4758 * are in the same FR_ROW frame.
4759 *
4760 * Strategy is similar to frame_setheight().
4761 */
4762 static void
4763frame_setwidth(curfrp, width)
4764 frame_T *curfrp;
4765 int width;
4766{
4767 int room; /* total number of lines available */
4768 int take; /* number of lines taken from other windows */
4769 int run;
4770 frame_T *frp;
4771 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004772 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 /* If the width already is the desired value, nothing to do. */
4775 if (curfrp->fr_width == width)
4776 return;
4777
4778 if (curfrp->fr_parent == NULL)
4779 /* topframe: can't change width */
4780 return;
4781
4782 if (curfrp->fr_parent->fr_layout == FR_COL)
4783 {
4784 /* Column of frames: Also need to resize frames above and below of
4785 * this one. First check for the minimal width of these. */
4786 w = frame_minwidth(curfrp->fr_parent, NULL);
4787 if (width < w)
4788 width = w;
4789 frame_setwidth(curfrp->fr_parent, width);
4790 }
4791 else
4792 {
4793 /*
4794 * Row of frames: try to change only frames in this row.
4795 *
4796 * Do this twice:
4797 * 1: compute room available, if it's not enough try resizing the
4798 * containing frame.
4799 * 2: compute the room available and adjust the width to it.
4800 */
4801 for (run = 1; run <= 2; ++run)
4802 {
4803 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004804 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4806 frp = frp->fr_next)
4807 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004808 if (frp != curfrp
4809 && frp->fr_win != NULL
4810 && frp->fr_win->w_p_wfw)
4811 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 room += frp->fr_width;
4813 if (frp != curfrp)
4814 room -= frame_minwidth(frp, NULL);
4815 }
4816
4817 if (width <= room)
4818 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004819 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (width > room)
4822 width = room;
4823 break;
4824 }
4825 frame_setwidth(curfrp->fr_parent, width
4826 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4827 }
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 /*
4830 * Compute the number of lines we will take from others frames (can be
4831 * negative!).
4832 */
4833 take = width - curfrp->fr_width;
4834
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004835 /* If there is not enough room, also reduce the width of a window
4836 * with 'winfixwidth' set. */
4837 if (width > room - room_reserved)
4838 room_reserved = room - width;
4839 /* If there is only a 'winfixwidth' window and making the
4840 * window smaller, need to make the other window narrower. */
4841 if (take < 0 && room - curfrp->fr_width < room_reserved)
4842 room_reserved = 0;
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 /*
4845 * set the current frame to the new width
4846 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004847 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 /*
4850 * First take lines from the frames right of the current frame. If
4851 * that is not enough, takes lines from frames left of the current
4852 * frame.
4853 */
4854 for (run = 0; run < 2; ++run)
4855 {
4856 if (run == 0)
4857 frp = curfrp->fr_next; /* 1st run: start with next window */
4858 else
4859 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4860 while (frp != NULL && take != 0)
4861 {
4862 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004863 if (room_reserved > 0
4864 && frp->fr_win != NULL
4865 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004867 if (room_reserved >= frp->fr_width)
4868 room_reserved -= frp->fr_width;
4869 else
4870 {
4871 if (frp->fr_width - room_reserved > take)
4872 room_reserved = frp->fr_width - take;
4873 take -= frp->fr_width - room_reserved;
4874 frame_new_width(frp, room_reserved, FALSE, FALSE);
4875 room_reserved = 0;
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 else
4879 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004880 if (frp->fr_width - take < w)
4881 {
4882 take -= frp->fr_width - w;
4883 frame_new_width(frp, w, FALSE, FALSE);
4884 }
4885 else
4886 {
4887 frame_new_width(frp, frp->fr_width - take,
4888 FALSE, FALSE);
4889 take = 0;
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 if (run == 0)
4893 frp = frp->fr_next;
4894 else
4895 frp = frp->fr_prev;
4896 }
4897 }
4898 }
4899}
4900#endif /* FEAT_VERTSPLIT */
4901
4902/*
4903 * Check 'winminheight' for a valid value.
4904 */
4905 void
4906win_setminheight()
4907{
4908 int room;
4909 int first = TRUE;
4910 win_T *wp;
4911
4912 /* loop until there is a 'winminheight' that is possible */
4913 while (p_wmh > 0)
4914 {
4915 /* TODO: handle vertical splits */
4916 room = -p_wh;
4917 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4918 room += wp->w_height - p_wmh;
4919 if (room >= 0)
4920 break;
4921 --p_wmh;
4922 if (first)
4923 {
4924 EMSG(_(e_noroom));
4925 first = FALSE;
4926 }
4927 }
4928}
4929
4930#ifdef FEAT_MOUSE
4931
4932/*
4933 * Status line of dragwin is dragged "offset" lines down (negative is up).
4934 */
4935 void
4936win_drag_status_line(dragwin, offset)
4937 win_T *dragwin;
4938 int offset;
4939{
4940 frame_T *curfr;
4941 frame_T *fr;
4942 int room;
4943 int row;
4944 int up; /* if TRUE, drag status line up, otherwise down */
4945 int n;
4946
4947 fr = dragwin->w_frame;
4948 curfr = fr;
4949 if (fr != topframe) /* more than one window */
4950 {
4951 fr = fr->fr_parent;
4952 /* When the parent frame is not a column of frames, its parent should
4953 * be. */
4954 if (fr->fr_layout != FR_COL)
4955 {
4956 curfr = fr;
4957 if (fr != topframe) /* only a row of windows, may drag statusline */
4958 fr = fr->fr_parent;
4959 }
4960 }
4961
4962 /* If this is the last frame in a column, may want to resize the parent
4963 * frame instead (go two up to skip a row of frames). */
4964 while (curfr != topframe && curfr->fr_next == NULL)
4965 {
4966 if (fr != topframe)
4967 fr = fr->fr_parent;
4968 curfr = fr;
4969 if (fr != topframe)
4970 fr = fr->fr_parent;
4971 }
4972
4973 if (offset < 0) /* drag up */
4974 {
4975 up = TRUE;
4976 offset = -offset;
4977 /* sum up the room of the current frame and above it */
4978 if (fr == curfr)
4979 {
4980 /* only one window */
4981 room = fr->fr_height - frame_minheight(fr, NULL);
4982 }
4983 else
4984 {
4985 room = 0;
4986 for (fr = fr->fr_child; ; fr = fr->fr_next)
4987 {
4988 room += fr->fr_height - frame_minheight(fr, NULL);
4989 if (fr == curfr)
4990 break;
4991 }
4992 }
4993 fr = curfr->fr_next; /* put fr at frame that grows */
4994 }
4995 else /* drag down */
4996 {
4997 up = FALSE;
4998 /*
4999 * Only dragging the last status line can reduce p_ch.
5000 */
5001 room = Rows - cmdline_row;
5002 if (curfr->fr_next == NULL)
5003 room -= 1;
5004 else
5005 room -= p_ch;
5006 if (room < 0)
5007 room = 0;
5008 /* sum up the room of frames below of the current one */
5009 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5010 room += fr->fr_height - frame_minheight(fr, NULL);
5011 fr = curfr; /* put fr at window that grows */
5012 }
5013
5014 if (room < offset) /* Not enough room */
5015 offset = room; /* Move as far as we can */
5016 if (offset <= 0)
5017 return;
5018
5019 /*
5020 * Grow frame fr by "offset" lines.
5021 * Doesn't happen when dragging the last status line up.
5022 */
5023 if (fr != NULL)
5024 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5025
5026 if (up)
5027 fr = curfr; /* current frame gets smaller */
5028 else
5029 fr = curfr->fr_next; /* next frame gets smaller */
5030
5031 /*
5032 * Now make the other frames smaller.
5033 */
5034 while (fr != NULL && offset > 0)
5035 {
5036 n = frame_minheight(fr, NULL);
5037 if (fr->fr_height - offset <= n)
5038 {
5039 offset -= fr->fr_height - n;
5040 frame_new_height(fr, n, !up, FALSE);
5041 }
5042 else
5043 {
5044 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5045 break;
5046 }
5047 if (up)
5048 fr = fr->fr_prev;
5049 else
5050 fr = fr->fr_next;
5051 }
5052 row = win_comp_pos();
5053 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5054 cmdline_row = row;
5055 p_ch = Rows - cmdline_row;
5056 if (p_ch < 1)
5057 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005058 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005059 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 showmode();
5061}
5062
5063#ifdef FEAT_VERTSPLIT
5064/*
5065 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5066 */
5067 void
5068win_drag_vsep_line(dragwin, offset)
5069 win_T *dragwin;
5070 int offset;
5071{
5072 frame_T *curfr;
5073 frame_T *fr;
5074 int room;
5075 int left; /* if TRUE, drag separator line left, otherwise right */
5076 int n;
5077
5078 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005079 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 return;
5081 curfr = fr;
5082 fr = fr->fr_parent;
5083 /* When the parent frame is not a row of frames, its parent should be. */
5084 if (fr->fr_layout != FR_ROW)
5085 {
5086 if (fr == topframe) /* only a column of windows (cannot happen?) */
5087 return;
5088 curfr = fr;
5089 fr = fr->fr_parent;
5090 }
5091
5092 /* If this is the last frame in a row, may want to resize a parent
5093 * frame instead. */
5094 while (curfr->fr_next == NULL)
5095 {
5096 if (fr == topframe)
5097 break;
5098 curfr = fr;
5099 fr = fr->fr_parent;
5100 if (fr != topframe)
5101 {
5102 curfr = fr;
5103 fr = fr->fr_parent;
5104 }
5105 }
5106
5107 if (offset < 0) /* drag left */
5108 {
5109 left = TRUE;
5110 offset = -offset;
5111 /* sum up the room of the current frame and left of it */
5112 room = 0;
5113 for (fr = fr->fr_child; ; fr = fr->fr_next)
5114 {
5115 room += fr->fr_width - frame_minwidth(fr, NULL);
5116 if (fr == curfr)
5117 break;
5118 }
5119 fr = curfr->fr_next; /* put fr at frame that grows */
5120 }
5121 else /* drag right */
5122 {
5123 left = FALSE;
5124 /* sum up the room of frames right of the current one */
5125 room = 0;
5126 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5127 room += fr->fr_width - frame_minwidth(fr, NULL);
5128 fr = curfr; /* put fr at window that grows */
5129 }
5130
5131 if (room < offset) /* Not enough room */
5132 offset = room; /* Move as far as we can */
5133 if (offset <= 0) /* No room at all, quit. */
5134 return;
5135
5136 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005137 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 /* shrink other frames: current and at the left or at the right */
5140 if (left)
5141 fr = curfr; /* current frame gets smaller */
5142 else
5143 fr = curfr->fr_next; /* next frame gets smaller */
5144
5145 while (fr != NULL && offset > 0)
5146 {
5147 n = frame_minwidth(fr, NULL);
5148 if (fr->fr_width - offset <= n)
5149 {
5150 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005151 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 else
5154 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005155 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157 }
5158 if (left)
5159 fr = fr->fr_prev;
5160 else
5161 fr = fr->fr_next;
5162 }
5163 (void)win_comp_pos();
5164 redraw_all_later(NOT_VALID);
5165}
5166#endif /* FEAT_VERTSPLIT */
5167#endif /* FEAT_MOUSE */
5168
5169#endif /* FEAT_WINDOWS */
5170
5171/*
5172 * Set the height of a window.
5173 * This takes care of the things inside the window, not what happens to the
5174 * window position, the frame or to other windows.
5175 */
5176 static void
5177win_new_height(wp, height)
5178 win_T *wp;
5179 int height;
5180{
5181 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005182 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005184 int space;
5185 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#define FRACTION_MULT 16384L
5187
5188 /* Don't want a negative height. Happens when splitting a tiny window.
5189 * Will equalize heights soon to fix it. */
5190 if (height < 0)
5191 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005192 if (wp->w_height == height)
5193 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5196 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5197 + FRACTION_MULT / 2) / (long)wp->w_height;
5198
5199 wp->w_height = height;
5200 wp->w_skipcol = 0;
5201
5202 /* Don't change w_topline when height is zero. Don't set w_topline when
5203 * 'scrollbind' is set and this isn't the current window. */
5204 if (height > 0
5205#ifdef FEAT_SCROLLBIND
5206 && (!wp->w_p_scb || wp == curwin)
5207#endif
5208 )
5209 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005210 /*
5211 * Find a value for w_topline that shows the cursor at the same
5212 * relative position in the window as before (more or less).
5213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 lnum = wp->w_cursor.lnum;
5215 if (lnum < 1) /* can happen when starting up */
5216 lnum = 1;
5217 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5218 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5219 sline = wp->w_wrow - line_size;
5220 if (sline < 0)
5221 {
5222 /*
5223 * Cursor line would go off top of screen if w_wrow was this high.
5224 */
5225 wp->w_wrow = line_size;
5226 }
5227 else
5228 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005229 space = height;
5230 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005232 space -= line_size;
5233 if (space > 0 && sline <= 0 && !did_below)
5234 {
5235 /* Try to use "~" lines below the text to avoid that text
5236 * is above the window while there are empty lines.
5237 * Subtract the rows below the cursor from "space" and
5238 * give the rest to "sline". */
5239 did_below = TRUE;
5240 bot = wp->w_cursor.lnum;
5241 while (space > 0)
5242 {
5243 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5244 space = 0;
5245 else
5246 {
5247#ifdef FEAT_FOLDING
5248 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5249#endif
5250 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5251 break;
5252 ++bot;
5253 space -= plines_win(wp, bot, TRUE);
5254 }
5255 }
5256 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5257 sline += space;
5258 }
5259 if (sline <= 0)
5260 break;
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#ifdef FEAT_FOLDING
5263 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5264 if (lnum == 1)
5265 {
5266 /* first line in buffer is folded */
5267 line_size = 1;
5268 --sline;
5269 break;
5270 }
5271#endif
5272 --lnum;
5273#ifdef FEAT_DIFF
5274 if (lnum == wp->w_topline)
5275 line_size = plines_win_nofill(wp, lnum, TRUE)
5276 + wp->w_topfill;
5277 else
5278#endif
5279 line_size = plines_win(wp, lnum, TRUE);
5280 sline -= line_size;
5281 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (sline < 0)
5284 {
5285 /*
5286 * Line we want at top would go off top of screen. Use next
5287 * line instead.
5288 */
5289#ifdef FEAT_FOLDING
5290 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5291#endif
5292 lnum++;
5293 wp->w_wrow -= line_size + sline;
5294 }
5295 else if (sline > 0)
5296 {
5297 /* First line of file reached, use that as topline. */
5298 lnum = 1;
5299 wp->w_wrow -= sline;
5300 }
5301 }
5302 set_topline(wp, lnum);
5303 }
5304
5305 if (wp == curwin)
5306 {
5307 if (p_so)
5308 update_topline();
5309 curs_columns(FALSE); /* validate w_wrow */
5310 }
5311 wp->w_prev_fraction_row = wp->w_wrow;
5312
5313 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005314 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#ifdef FEAT_WINDOWS
5316 wp->w_redr_status = TRUE;
5317#endif
5318 invalidate_botline_win(wp);
5319}
5320
5321#ifdef FEAT_VERTSPLIT
5322/*
5323 * Set the width of a window.
5324 */
5325 static void
5326win_new_width(wp, width)
5327 win_T *wp;
5328 int width;
5329{
5330 wp->w_width = width;
5331 wp->w_lines_valid = 0;
5332 changed_line_abv_curs_win(wp);
5333 invalidate_botline_win(wp);
5334 if (wp == curwin)
5335 {
5336 update_topline();
5337 curs_columns(TRUE); /* validate w_wrow */
5338 }
5339 redraw_win_later(wp, NOT_VALID);
5340 wp->w_redr_status = TRUE;
5341}
5342#endif
5343
5344 void
5345win_comp_scroll(wp)
5346 win_T *wp;
5347{
5348 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5349 if (wp->w_p_scr == 0)
5350 wp->w_p_scr = 1;
5351}
5352
5353/*
5354 * command_height: called whenever p_ch has been changed
5355 */
5356 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005357command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359#ifdef FEAT_WINDOWS
5360 int h;
5361 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005362 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005364 /* Use the value of p_ch that we remembered. This is needed for when the
5365 * GUI starts up, we can't be sure in what order things happen. And when
5366 * p_ch was changed in another tab page. */
5367 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /* Find bottom frame with width of screen. */
5370 frp = lastwin->w_frame;
5371# ifdef FEAT_VERTSPLIT
5372 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5373 frp = frp->fr_parent;
5374# endif
5375
5376 /* Avoid changing the height of a window with 'winfixheight' set. */
5377 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5378 && frp->fr_win->w_p_wfh)
5379 frp = frp->fr_prev;
5380
5381 if (starting != NO_SCREEN)
5382 {
5383 cmdline_row = Rows - p_ch;
5384
5385 if (p_ch > old_p_ch) /* p_ch got bigger */
5386 {
5387 while (p_ch > old_p_ch)
5388 {
5389 if (frp == NULL)
5390 {
5391 EMSG(_(e_noroom));
5392 p_ch = old_p_ch;
5393 cmdline_row = Rows - p_ch;
5394 break;
5395 }
5396 h = frp->fr_height - frame_minheight(frp, NULL);
5397 if (h > p_ch - old_p_ch)
5398 h = p_ch - old_p_ch;
5399 old_p_ch += h;
5400 frame_add_height(frp, -h);
5401 frp = frp->fr_prev;
5402 }
5403
5404 /* Recompute window positions. */
5405 (void)win_comp_pos();
5406
5407 /* clear the lines added to cmdline */
5408 if (full_screen)
5409 screen_fill((int)(cmdline_row), (int)Rows, 0,
5410 (int)Columns, ' ', ' ', 0);
5411 msg_row = cmdline_row;
5412 redraw_cmdline = TRUE;
5413 return;
5414 }
5415
5416 if (msg_row < cmdline_row)
5417 msg_row = cmdline_row;
5418 redraw_cmdline = TRUE;
5419 }
5420 frame_add_height(frp, (int)(old_p_ch - p_ch));
5421
5422 /* Recompute window positions. */
5423 if (frp != lastwin->w_frame)
5424 (void)win_comp_pos();
5425#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005427 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#endif
5429}
5430
5431#if defined(FEAT_WINDOWS) || defined(PROTO)
5432/*
5433 * Resize frame "frp" to be "n" lines higher (negative for less high).
5434 * Also resize the frames it is contained in.
5435 */
5436 static void
5437frame_add_height(frp, n)
5438 frame_T *frp;
5439 int n;
5440{
5441 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5442 for (;;)
5443 {
5444 frp = frp->fr_parent;
5445 if (frp == NULL)
5446 break;
5447 frp->fr_height += n;
5448 }
5449}
5450
5451/*
5452 * Add or remove a status line for the bottom window(s), according to the
5453 * value of 'laststatus'.
5454 */
5455 void
5456last_status(morewin)
5457 int morewin; /* pretend there are two or more windows */
5458{
5459 /* Don't make a difference between horizontal or vertical split. */
5460 last_status_rec(topframe, (p_ls == 2
5461 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5462}
5463
5464 static void
5465last_status_rec(fr, statusline)
5466 frame_T *fr;
5467 int statusline;
5468{
5469 frame_T *fp;
5470 win_T *wp;
5471
5472 if (fr->fr_layout == FR_LEAF)
5473 {
5474 wp = fr->fr_win;
5475 if (wp->w_status_height != 0 && !statusline)
5476 {
5477 /* remove status line */
5478 win_new_height(wp, wp->w_height + 1);
5479 wp->w_status_height = 0;
5480 comp_col();
5481 }
5482 else if (wp->w_status_height == 0 && statusline)
5483 {
5484 /* Find a frame to take a line from. */
5485 fp = fr;
5486 while (fp->fr_height <= frame_minheight(fp, NULL))
5487 {
5488 if (fp == topframe)
5489 {
5490 EMSG(_(e_noroom));
5491 return;
5492 }
5493 /* In a column of frames: go to frame above. If already at
5494 * the top or in a row of frames: go to parent. */
5495 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5496 fp = fp->fr_prev;
5497 else
5498 fp = fp->fr_parent;
5499 }
5500 wp->w_status_height = 1;
5501 if (fp != fr)
5502 {
5503 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5504 frame_fix_height(wp);
5505 (void)win_comp_pos();
5506 }
5507 else
5508 win_new_height(wp, wp->w_height - 1);
5509 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005510 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512 }
5513#ifdef FEAT_VERTSPLIT
5514 else if (fr->fr_layout == FR_ROW)
5515 {
5516 /* vertically split windows, set status line for each one */
5517 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5518 last_status_rec(fp, statusline);
5519 }
5520#endif
5521 else
5522 {
5523 /* horizontally split window, set status line for last one */
5524 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5525 ;
5526 last_status_rec(fp, statusline);
5527 }
5528}
5529
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005530/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005531 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005532 */
5533 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005534tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005535{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005536#ifdef FEAT_GUI_TABLINE
5537 /* When the GUI has the tabline then this always returns zero. */
5538 if (gui_use_tabline())
5539 return 0;
5540#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005541 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005542 {
5543 case 0: return 0;
5544 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5545 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005546 return 1;
5547}
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#endif /* FEAT_WINDOWS */
5550
5551#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5552/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005553 * Get the file name at the cursor.
5554 * If Visual mode is active, use the selected text if it's in one line.
5555 * Returns the name in allocated memory, NULL for failure.
5556 */
5557 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005558grab_file_name(count, file_lnum)
5559 long count;
5560 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005561{
5562# ifdef FEAT_VISUAL
5563 if (VIsual_active)
5564 {
5565 int len;
5566 char_u *ptr;
5567
5568 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5569 return NULL;
5570 return find_file_name_in_path(ptr, len,
5571 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5572 }
5573# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005574 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5575 file_lnum);
5576
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005577}
5578
5579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 * Return the file name under or after the cursor.
5581 *
5582 * The 'path' option is searched if the file name is not absolute.
5583 * The string returned has been alloc'ed and should be freed by the caller.
5584 * NULL is returned if the file name or file is not found.
5585 *
5586 * options:
5587 * FNAME_MESS give error messages
5588 * FNAME_EXP expand to path
5589 * FNAME_HYP check for hypertext link
5590 * FNAME_INCL apply "includeexpr"
5591 */
5592 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005593file_name_at_cursor(options, count, file_lnum)
5594 int options;
5595 long count;
5596 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005599 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5600 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602
5603/*
5604 * Return the name of the file under or after ptr[col].
5605 * Otherwise like file_name_at_cursor().
5606 */
5607 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005608file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 char_u *line;
5610 int col;
5611 int options;
5612 long count;
5613 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005614 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 char_u *ptr;
5617 int len;
5618
5619 /*
5620 * search forward for what could be the start of a file name
5621 */
5622 ptr = line + col;
5623 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005624 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (*ptr == NUL) /* nothing found */
5626 {
5627 if (options & FNAME_MESS)
5628 EMSG(_("E446: No file name under cursor"));
5629 return NULL;
5630 }
5631
5632 /*
5633 * Search backward for first char of the file name.
5634 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5635 */
5636 while (ptr > line)
5637 {
5638#ifdef FEAT_MBYTE
5639 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5640 ptr -= len + 1;
5641 else
5642#endif
5643 if (vim_isfilec(ptr[-1])
5644 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5645 --ptr;
5646 else
5647 break;
5648 }
5649
5650 /*
5651 * Search forward for the last char of the file name.
5652 * Also allow "://" when ':' is not in 'isfname'.
5653 */
5654 len = 0;
5655 while (vim_isfilec(ptr[len])
5656 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5657#ifdef FEAT_MBYTE
5658 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005659 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
5661#endif
5662 ++len;
5663
5664 /*
5665 * If there is trailing punctuation, remove it.
5666 * But don't remove "..", could be a directory name.
5667 */
5668 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5669 && ptr[len - 2] != '.')
5670 --len;
5671
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005672 if (file_lnum != NULL)
5673 {
5674 char_u *p;
5675
5676 /* Get the number after the file name and a separator character */
5677 p = ptr + len;
5678 p = skipwhite(p);
5679 if (*p != NUL)
5680 {
5681 if (!isdigit(*p))
5682 ++p; /* skip the separator */
5683 p = skipwhite(p);
5684 if (isdigit(*p))
5685 *file_lnum = (int)getdigits(&p);
5686 }
5687 }
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5690}
5691
5692# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5693static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5694
5695 static char_u *
5696eval_includeexpr(ptr, len)
5697 char_u *ptr;
5698 int len;
5699{
5700 char_u *res;
5701
5702 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005703 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005704 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 set_vim_var_string(VV_FNAME, NULL, 0);
5706 return res;
5707}
5708#endif
5709
5710/*
5711 * Return the name of the file ptr[len] in 'path'.
5712 * Otherwise like file_name_at_cursor().
5713 */
5714 char_u *
5715find_file_name_in_path(ptr, len, options, count, rel_fname)
5716 char_u *ptr;
5717 int len;
5718 int options;
5719 long count;
5720 char_u *rel_fname; /* file we are searching relative to */
5721{
5722 char_u *file_name;
5723 int c;
5724# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5725 char_u *tofree = NULL;
5726
5727 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5728 {
5729 tofree = eval_includeexpr(ptr, len);
5730 if (tofree != NULL)
5731 {
5732 ptr = tofree;
5733 len = (int)STRLEN(ptr);
5734 }
5735 }
5736# endif
5737
5738 if (options & FNAME_EXP)
5739 {
5740 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5741 TRUE, rel_fname);
5742
5743# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5744 /*
5745 * If the file could not be found in a normal way, try applying
5746 * 'includeexpr' (unless done already).
5747 */
5748 if (file_name == NULL
5749 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5750 {
5751 tofree = eval_includeexpr(ptr, len);
5752 if (tofree != NULL)
5753 {
5754 ptr = tofree;
5755 len = (int)STRLEN(ptr);
5756 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5757 TRUE, rel_fname);
5758 }
5759 }
5760# endif
5761 if (file_name == NULL && (options & FNAME_MESS))
5762 {
5763 c = ptr[len];
5764 ptr[len] = NUL;
5765 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5766 ptr[len] = c;
5767 }
5768
5769 /* Repeat finding the file "count" times. This matters when it
5770 * appears several times in the path. */
5771 while (file_name != NULL && --count > 0)
5772 {
5773 vim_free(file_name);
5774 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5775 }
5776 }
5777 else
5778 file_name = vim_strnsave(ptr, len);
5779
5780# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5781 vim_free(tofree);
5782# endif
5783
5784 return file_name;
5785}
5786#endif /* FEAT_SEARCHPATH */
5787
5788/*
5789 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5790 * Also check for ":\\", which MS Internet Explorer accepts, return
5791 * URL_BACKSLASH.
5792 */
5793 static int
5794path_is_url(p)
5795 char_u *p;
5796{
5797 if (STRNCMP(p, "://", (size_t)3) == 0)
5798 return URL_SLASH;
5799 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5800 return URL_BACKSLASH;
5801 return 0;
5802}
5803
5804/*
5805 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5806 * Return URL_BACKSLASH for "name:\\".
5807 * Return zero otherwise.
5808 */
5809 int
5810path_with_url(fname)
5811 char_u *fname;
5812{
5813 char_u *p;
5814
5815 for (p = fname; isalpha(*p); ++p)
5816 ;
5817 return path_is_url(p);
5818}
5819
5820/*
5821 * Return TRUE if "name" is a full (absolute) path name or URL.
5822 */
5823 int
5824vim_isAbsName(name)
5825 char_u *name;
5826{
5827 return (path_with_url(name) != 0 || mch_isFullName(name));
5828}
5829
5830/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005831 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 *
5833 * return FAIL for failure, OK otherwise
5834 */
5835 int
5836vim_FullName(fname, buf, len, force)
5837 char_u *fname, *buf;
5838 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005839 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 int retval = OK;
5842 int url;
5843
5844 *buf = NUL;
5845 if (fname == NULL)
5846 return FAIL;
5847
5848 url = path_with_url(fname);
5849 if (!url)
5850 retval = mch_FullName(fname, buf, len, force);
5851 if (url || retval == FAIL)
5852 {
5853 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005854 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
5856#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5857 slash_adjust(buf);
5858#endif
5859 return retval;
5860}
5861
5862/*
5863 * Return the minimal number of rows that is needed on the screen to display
5864 * the current number of windows.
5865 */
5866 int
5867min_rows()
5868{
5869 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005870#ifdef FEAT_WINDOWS
5871 tabpage_T *tp;
5872 int n;
5873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 if (firstwin == NULL) /* not initialized yet */
5876 return MIN_LINES;
5877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005879 total = 0;
5880 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5881 {
5882 n = frame_minheight(tp->tp_topframe, NULL);
5883 if (total < n)
5884 total = n;
5885 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005886 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005888 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005890 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return total;
5892}
5893
5894/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005895 * Return TRUE if there is only one window (in the current tab page), not
5896 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 */
5898 int
5899only_one_window()
5900{
5901#ifdef FEAT_WINDOWS
5902 int count = 0;
5903 win_T *wp;
5904
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005905 /* If there is another tab page there always is another window. */
5906 if (first_tabpage->tp_next != NULL)
5907 return FALSE;
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005910 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911# ifdef FEAT_QUICKFIX
5912 || wp->w_p_pvw
5913# endif
5914 ) || wp == curwin)
5915 ++count;
5916 return (count <= 1);
5917#else
5918 return TRUE;
5919#endif
5920}
5921
5922#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5923/*
5924 * Correct the cursor line number in other windows. Used after changing the
5925 * current buffer, and before applying autocommands.
5926 * When "do_curwin" is TRUE, also check current window.
5927 */
5928 void
5929check_lnums(do_curwin)
5930 int do_curwin;
5931{
5932 win_T *wp;
5933
5934#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005935 tabpage_T *tp;
5936
5937 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5939#else
5940 wp = curwin;
5941 if (do_curwin)
5942#endif
5943 {
5944 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5945 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5946 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5947 wp->w_topline = curbuf->b_ml.ml_line_count;
5948 }
5949}
5950#endif
5951
5952#if defined(FEAT_WINDOWS) || defined(PROTO)
5953
5954/*
5955 * A snapshot of the window sizes, to restore them after closing the help
5956 * window.
5957 * Only these fields are used:
5958 * fr_layout
5959 * fr_width
5960 * fr_height
5961 * fr_next
5962 * fr_child
5963 * fr_win (only valid for the old curwin, NULL otherwise)
5964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
5966/*
5967 * Create a snapshot of the current frame sizes.
5968 */
5969 static void
5970make_snapshot()
5971{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005972 clear_snapshot(curtab);
5973 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974}
5975
5976 static void
5977make_snapshot_rec(fr, frp)
5978 frame_T *fr;
5979 frame_T **frp;
5980{
5981 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5982 if (*frp == NULL)
5983 return;
5984 (*frp)->fr_layout = fr->fr_layout;
5985# ifdef FEAT_VERTSPLIT
5986 (*frp)->fr_width = fr->fr_width;
5987# endif
5988 (*frp)->fr_height = fr->fr_height;
5989 if (fr->fr_next != NULL)
5990 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5991 if (fr->fr_child != NULL)
5992 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5993 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5994 (*frp)->fr_win = curwin;
5995}
5996
5997/*
5998 * Remove any existing snapshot.
5999 */
6000 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006001clear_snapshot(tp)
6002 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006004 clear_snapshot_rec(tp->tp_snapshot);
6005 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006}
6007
6008 static void
6009clear_snapshot_rec(fr)
6010 frame_T *fr;
6011{
6012 if (fr != NULL)
6013 {
6014 clear_snapshot_rec(fr->fr_next);
6015 clear_snapshot_rec(fr->fr_child);
6016 vim_free(fr);
6017 }
6018}
6019
6020/*
6021 * Restore a previously created snapshot, if there is any.
6022 * This is only done if the screen size didn't change and the window layout is
6023 * still the same.
6024 */
6025 static void
6026restore_snapshot(close_curwin)
6027 int close_curwin; /* closing current window */
6028{
6029 win_T *wp;
6030
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006031 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006033 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006035 && curtab->tp_snapshot->fr_height == topframe->fr_height
6036 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006038 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 win_comp_pos();
6040 if (wp != NULL && close_curwin)
6041 win_goto(wp);
6042 redraw_all_later(CLEAR);
6043 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006044 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045}
6046
6047/*
6048 * Check if frames "sn" and "fr" have the same layout, same following frames
6049 * and same children.
6050 */
6051 static int
6052check_snapshot_rec(sn, fr)
6053 frame_T *sn;
6054 frame_T *fr;
6055{
6056 if (sn->fr_layout != fr->fr_layout
6057 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6058 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6059 || (sn->fr_next != NULL
6060 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6061 || (sn->fr_child != NULL
6062 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6063 return FAIL;
6064 return OK;
6065}
6066
6067/*
6068 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6069 * following frames and children.
6070 * Returns a pointer to the old current window, or NULL.
6071 */
6072 static win_T *
6073restore_snapshot_rec(sn, fr)
6074 frame_T *sn;
6075 frame_T *fr;
6076{
6077 win_T *wp = NULL;
6078 win_T *wp2;
6079
6080 fr->fr_height = sn->fr_height;
6081# ifdef FEAT_VERTSPLIT
6082 fr->fr_width = sn->fr_width;
6083# endif
6084 if (fr->fr_layout == FR_LEAF)
6085 {
6086 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6087# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006088 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089# endif
6090 wp = sn->fr_win;
6091 }
6092 if (sn->fr_next != NULL)
6093 {
6094 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6095 if (wp2 != NULL)
6096 wp = wp2;
6097 }
6098 if (sn->fr_child != NULL)
6099 {
6100 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6101 if (wp2 != NULL)
6102 wp = wp2;
6103 }
6104 return wp;
6105}
6106
6107#endif
6108
6109#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6110/*
6111 * Return TRUE if there is any vertically split window.
6112 */
6113 int
6114win_hasvertsplit()
6115{
6116 frame_T *fr;
6117
6118 if (topframe->fr_layout == FR_ROW)
6119 return TRUE;
6120
6121 if (topframe->fr_layout == FR_COL)
6122 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6123 if (fr->fr_layout == FR_ROW)
6124 return TRUE;
6125
6126 return FALSE;
6127}
6128#endif