Merge branch 'vim' into feat/float-point-ext
[vim_extended.git] / src / window.c
blob0c3a7f3837e0aaecfec27812d380240e4f6516f5
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
10 #include "vim.h"
12 static int path_is_url __ARGS((char_u *p));
13 #if defined(FEAT_WINDOWS) || defined(PROTO)
14 static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
15 static void win_init_some __ARGS((win_T *newp, win_T *oldp));
16 static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17 static void frame_setheight __ARGS((frame_T *curfrp, int height));
18 #ifdef FEAT_VERTSPLIT
19 static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20 #endif
21 static void win_exchange __ARGS((long));
22 static void win_rotate __ARGS((int, int));
23 static void win_totop __ARGS((int size, int flags));
24 static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
25 static int last_window __ARGS((void));
26 static int one_window __ARGS((void));
27 static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
28 static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
29 static tabpage_T *alt_tabpage __ARGS((void));
30 static win_T *frame2win __ARGS((frame_T *frp));
31 static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32 static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33 static int frame_fixed_height __ARGS((frame_T *frp));
34 #ifdef FEAT_VERTSPLIT
35 static int frame_fixed_width __ARGS((frame_T *frp));
36 static void frame_add_statusline __ARGS((frame_T *frp));
37 static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
38 static void frame_add_vsep __ARGS((frame_T *frp));
39 static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40 static void frame_fix_width __ARGS((win_T *wp));
41 #endif
42 #endif
43 static int win_alloc_firstwin __ARGS((win_T *oldwin));
44 static void new_frame __ARGS((win_T *wp));
45 #if defined(FEAT_WINDOWS) || defined(PROTO)
46 static tabpage_T *alloc_tabpage __ARGS((void));
47 static int leave_tabpage __ARGS((buf_T *new_curbuf));
48 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
49 static void frame_fix_height __ARGS((win_T *wp));
50 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
52 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
53 static void frame_append __ARGS((frame_T *after, frame_T *frp));
54 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55 static void frame_remove __ARGS((frame_T *frp));
56 #ifdef FEAT_VERTSPLIT
57 static void win_new_width __ARGS((win_T *wp, int width));
58 static void win_goto_ver __ARGS((int up, long count));
59 static void win_goto_hor __ARGS((int left, long count));
60 #endif
61 static void frame_add_height __ARGS((frame_T *frp, int n));
62 static void last_status_rec __ARGS((frame_T *fr, int statusline));
64 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
65 static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
66 static void clear_snapshot_rec __ARGS((frame_T *fr));
67 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
70 #endif /* FEAT_WINDOWS */
72 static win_T *win_alloc __ARGS((win_T *after, int hidden));
73 static void win_new_height __ARGS((win_T *, int));
75 #define URL_SLASH 1 /* path_is_url() has found "://" */
76 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
78 #define NOWIN (win_T *)-1 /* non-existing window */
80 #ifdef FEAT_WINDOWS
81 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
82 #else
83 # define ROWS_AVAIL (Rows - p_ch)
84 #endif
86 #if defined(FEAT_WINDOWS) || defined(PROTO)
88 static char *m_onlyone = N_("Already only one window");
91 * all CTRL-W window commands are handled here, called from normal_cmd().
93 void
94 do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
99 long Prenum1;
100 win_T *wp;
101 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
103 linenr_T lnum = -1;
104 #endif
105 #ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108 #endif
109 char_u cbuf[40];
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
116 #ifdef FEAT_CMDWIN
117 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118 #else
119 # define CHECK_CMDWIN
120 #endif
122 switch (nchar)
124 /* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129 #ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131 #endif
132 #ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137 #endif
138 #ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140 #endif
141 win_split((int)Prenum, 0);
142 break;
144 #ifdef FEAT_VERTSPLIT
145 /* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
149 # ifdef FEAT_VISUAL
150 reset_VIsual_and_resel(); /* stop Visual mode */
151 # endif
152 # ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157 # endif
158 # ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160 # endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163 #endif
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 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
176 do_cmdline_cmd(cbuf);
177 break;
179 /* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183 #ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185 #endif
186 #ifdef FEAT_QUICKFIX
187 newwindow:
188 #endif
189 if (Prenum)
190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
192 else
193 cbuf[0] = NUL;
194 #if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197 #endif
198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
202 /* quit current window */
203 case Ctrl_Q:
204 case 'q':
205 #ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207 #endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
211 /* close current window */
212 case Ctrl_C:
213 case 'c':
214 #ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216 #endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
220 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221 /* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225 #ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227 #endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
231 /* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241 #endif
243 /* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247 #ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249 #endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
253 /* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256 /* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
259 if (firstwin == lastwin && Prenum != 1) /* just one window */
260 beep_flush();
261 else
263 if (Prenum) /* go to specified window */
265 for (wp = firstwin; --Prenum > 0; )
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
273 else
275 if (nchar == 'W') /* go to previous window */
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
281 else /* go to next window */
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
288 win_goto(wp);
290 break;
292 /* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297 #ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299 #else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
303 win_goto(wp);
304 #endif
305 break;
307 /* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312 #ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314 #else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
318 win_goto(wp);
319 #endif
320 break;
322 #ifdef FEAT_VERTSPLIT
323 /* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
332 /* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339 #endif
341 /* move window to new tab page */
342 case 'T':
343 if (one_window())
344 MSG(_(m_onlyone));
345 else
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
352 wp = curwin;
353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
364 break;
366 /* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
372 /* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
378 /* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
387 /* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
394 /* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398 #ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400 #endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
404 /* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407 #ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409 #endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
413 /* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416 #ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419 #endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
426 /* make all windows the same height */
427 case '=':
428 #ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430 #endif
431 win_equal(NULL, FALSE, 'b');
432 break;
434 /* increase current window height */
435 case '+':
436 #ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438 #endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
442 /* decrease current window height */
443 case '-':
444 #ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446 #endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
450 /* set current window height */
451 case Ctrl__:
452 case '_':
453 #ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455 #endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
459 #ifdef FEAT_VERTSPLIT
460 /* increase current window width */
461 case '>':
462 #ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464 #endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
468 /* decrease current window width */
469 case '<':
470 #ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472 #endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
476 /* set current window width */
477 case '|':
478 #ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480 #endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483 #endif
485 /* jump to tag and split window if tag exists (in preview window) */
486 #if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494 #endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498 #ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500 #endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
511 #ifdef FEAT_SEARCHPATH
512 /* edit file name under cursor in a new window */
513 case 'f':
514 case 'F':
515 case Ctrl_F:
516 wingotofile:
517 CHECK_CMDWIN
519 ptr = grab_file_name(Prenum1, &lnum);
520 if (ptr != NULL)
522 # ifdef FEAT_GUI
523 need_mouse_correct = TRUE;
524 # endif
525 setpcmark();
526 if (win_split(0, 0) == OK)
528 # ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530 # endif
531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
533 if (nchar == 'F' && lnum >= 0)
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
540 vim_free(ptr);
542 break;
543 #endif
545 #ifdef FEAT_FIND_ID
546 /* Go to the first occurrence of the identifier under cursor along path in a
547 * new window -- webb
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563 #endif
565 case K_KENTER:
566 case CAR:
567 #if defined(FEAT_QUICKFIX)
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
572 if (bt_quickfix(curbuf))
574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
577 do_cmdline_cmd(cbuf);
579 #endif
580 break;
583 /* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587 #ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589 #endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
593 xchar = plain_vgetc();
594 LANGMAP_ADJUST(xchar, TRUE);
595 --no_mapping;
596 --allow_keys;
597 #ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599 #endif
600 switch (xchar)
602 #if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610 #endif
611 case ']':
612 case Ctrl_RSB:
613 #ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615 #endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
626 #ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
629 cmdmod.tab = TRUE;
630 nchar = xchar;
631 goto wingotofile;
632 #endif
633 default:
634 beep_flush();
635 break;
637 break;
639 default: beep_flush();
640 break;
645 * split the current window, implements CTRL-W s and :split
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
657 * return FAIL for failure, OK otherwise
660 win_split(size, flags)
661 int size;
662 int flags;
664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
679 make_snapshot(SNAP_HELP_IDX);
680 else
681 clear_snapshot(curtab, SNAP_HELP_IDX);
683 return win_split_ins(size, flags, NULL, 0);
687 * When "newwin" is NULL: split the current window in two.
688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
693 win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
724 EMSG(_(e_noroom));
725 return FAIL;
727 need_status = STATUS_HEIGHT;
730 #ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734 #endif
736 #ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
739 layout = FR_ROW;
742 * Check if we are able to split the current window and compute its
743 * width.
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
757 EMSG(_(e_noroom));
758 return FAIL;
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
790 do_equal = TRUE;
791 break;
793 frp = frp->fr_next;
797 else
798 #endif
800 layout = FR_COL;
803 * Check if we are able to split the current window and compute its
804 * height.
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
814 else
816 available = oldwin->w_height;
817 needed += p_wmh;
819 if (available < needed && newwin == NULL)
821 EMSG(_(e_noroom));
822 return FAIL;
824 oldwin_height = oldwin->w_height;
825 if (need_status)
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857 #ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859 #endif
860 && oldwin->w_frame->fr_parent != NULL)
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
870 do_equal = TRUE;
871 break;
873 frp = frp->fr_next;
879 * allocate new window structure and link it in the window list
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886 #ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888 #endif
889 p_sb))))
891 /* new window below/right of current one */
892 if (newwin == NULL)
893 wp = win_alloc(oldwin, FALSE);
894 else
895 win_append(oldwin, wp);
897 else
899 if (newwin == NULL)
900 wp = win_alloc(oldwin->w_prev, FALSE);
901 else
902 win_append(oldwin->w_prev, wp);
905 if (newwin == NULL)
907 if (wp == NULL)
908 return FAIL;
910 new_frame(wp);
911 if (wp->w_frame == NULL)
913 win_free(wp, NULL);
914 return FAIL;
917 /* make the contents of the new window the same as the current one */
918 win_init(wp, curwin, flags);
922 * Reorganise the tree of frames to insert the new window.
924 if (flags & (WSP_TOP | WSP_BOT))
926 #ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929 #else
930 if (topframe->fr_layout == FR_COL)
931 #endif
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
942 else
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950 #ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954 #endif
955 before = !p_sb;
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
976 if (newwin == NULL)
977 frp = wp->w_frame;
978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
988 #ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
994 --oldwin->w_height;
995 oldwin->w_status_height = need_status;
997 if (flags & (WSP_TOP | WSP_BOT))
999 /* set height and row of new window to full height */
1000 wp->w_winrow = tabline_height();
1001 wp->w_height = curfrp->fr_height - (p_ls > 0);
1002 wp->w_status_height = (p_ls > 0);
1004 else
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
1008 wp->w_height = oldwin->w_height;
1009 wp->w_status_height = oldwin->w_status_height;
1011 frp->fr_height = curfrp->fr_height;
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
1015 wp->w_width = new_size;
1016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1023 if (flags & (WSP_TOP | WSP_BOT))
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
1029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
1032 else
1033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1034 if (before) /* new window left of current one */
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1044 else
1045 #endif
1047 /* width and column of new window is same as current window */
1048 #ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1051 wp->w_wincol = 0;
1052 wp->w_width = Columns;
1053 wp->w_vsep_width = 0;
1055 else
1057 wp->w_wincol = oldwin->w_wincol;
1058 wp->w_width = oldwin->w_width;
1059 wp->w_vsep_width = oldwin->w_vsep_width;
1061 frp->fr_width = curfrp->fr_width;
1062 #endif
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1078 else /* new window below current one */
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1084 #ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087 #endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1096 * Both windows need redrawing
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1103 if (need_status)
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1114 * make the new window the current window and redraw
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118 #ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121 #endif
1122 'v');
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126 #ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1133 # ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137 # endif
1139 else
1140 #endif
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1146 win_enter(wp, FALSE);
1147 #ifdef FEAT_VERTSPLIT
1148 if (flags & WSP_VERT)
1149 p_wiw = i;
1150 else
1151 #endif
1152 p_wh = i;
1154 return OK;
1159 * Initialize window "newp" from window "oldp".
1160 * Used when splitting a window and when creating a new tab page.
1161 * The windows will both edit the same buffer.
1162 * WSP_NEWLOC may be specified in flags to prevent the location list from
1163 * being copied.
1165 static void
1166 win_init(newp, oldp, flags)
1167 win_T *newp;
1168 win_T *oldp;
1169 int flags UNUSED;
1171 int i;
1173 newp->w_buffer = oldp->w_buffer;
1174 oldp->w_buffer->b_nwindows++;
1175 newp->w_cursor = oldp->w_cursor;
1176 newp->w_valid = 0;
1177 newp->w_curswant = oldp->w_curswant;
1178 newp->w_set_curswant = oldp->w_set_curswant;
1179 newp->w_topline = oldp->w_topline;
1180 #ifdef FEAT_DIFF
1181 newp->w_topfill = oldp->w_topfill;
1182 #endif
1183 newp->w_leftcol = oldp->w_leftcol;
1184 newp->w_pcmark = oldp->w_pcmark;
1185 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1186 newp->w_alt_fnum = oldp->w_alt_fnum;
1187 newp->w_wrow = oldp->w_wrow;
1188 newp->w_fraction = oldp->w_fraction;
1189 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1190 #ifdef FEAT_JUMPLIST
1191 copy_jumplist(oldp, newp);
1192 #endif
1193 #ifdef FEAT_QUICKFIX
1194 if (flags & WSP_NEWLOC)
1196 /* Don't copy the location list. */
1197 newp->w_llist = NULL;
1198 newp->w_llist_ref = NULL;
1200 else
1201 copy_loclist(oldp, newp);
1202 #endif
1203 if (oldp->w_localdir != NULL)
1204 newp->w_localdir = vim_strsave(oldp->w_localdir);
1206 /* copy tagstack and folds */
1207 for (i = 0; i < oldp->w_tagstacklen; i++)
1209 newp->w_tagstack[i] = oldp->w_tagstack[i];
1210 if (newp->w_tagstack[i].tagname != NULL)
1211 newp->w_tagstack[i].tagname =
1212 vim_strsave(newp->w_tagstack[i].tagname);
1214 newp->w_tagstackidx = oldp->w_tagstackidx;
1215 newp->w_tagstacklen = oldp->w_tagstacklen;
1216 # ifdef FEAT_FOLDING
1217 copyFoldingState(oldp, newp);
1218 # endif
1220 win_init_some(newp, oldp);
1224 * Initialize window "newp" from window"old".
1225 * Only the essential things are copied.
1227 static void
1228 win_init_some(newp, oldp)
1229 win_T *newp;
1230 win_T *oldp;
1232 /* Use the same argument list. */
1233 newp->w_alist = oldp->w_alist;
1234 ++newp->w_alist->al_refcount;
1235 newp->w_arg_idx = oldp->w_arg_idx;
1237 /* copy options from existing window */
1238 win_copy_options(oldp, newp);
1241 #endif /* FEAT_WINDOWS */
1243 #if defined(FEAT_WINDOWS) || defined(PROTO)
1245 * Check if "win" is a pointer to an existing window.
1248 win_valid(win)
1249 win_T *win;
1251 win_T *wp;
1253 if (win == NULL)
1254 return FALSE;
1255 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1256 if (wp == win)
1257 return TRUE;
1258 return FALSE;
1262 * Return the number of windows.
1265 win_count()
1267 win_T *wp;
1268 int count = 0;
1270 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1271 ++count;
1272 return count;
1276 * Make "count" windows on the screen.
1277 * Return actual number of windows on the screen.
1278 * Must be called when there is just one window, filling the whole screen
1279 * (excluding the command line).
1282 make_windows(count, vertical)
1283 int count;
1284 int vertical UNUSED; /* split windows vertically if TRUE */
1286 int maxcount;
1287 int todo;
1289 #ifdef FEAT_VERTSPLIT
1290 if (vertical)
1292 /* Each windows needs at least 'winminwidth' lines and a separator
1293 * column. */
1294 maxcount = (curwin->w_width + curwin->w_vsep_width
1295 - (p_wiw - p_wmw)) / (p_wmw + 1);
1297 else
1298 #endif
1300 /* Each window needs at least 'winminheight' lines and a status line. */
1301 maxcount = (curwin->w_height + curwin->w_status_height
1302 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1305 if (maxcount < 2)
1306 maxcount = 2;
1307 if (count > maxcount)
1308 count = maxcount;
1311 * add status line now, otherwise first window will be too big
1313 if (count > 1)
1314 last_status(TRUE);
1316 #ifdef FEAT_AUTOCMD
1318 * Don't execute autocommands while creating the windows. Must do that
1319 * when putting the buffers in the windows.
1321 block_autocmds();
1322 #endif
1324 /* todo is number of windows left to create */
1325 for (todo = count - 1; todo > 0; --todo)
1326 #ifdef FEAT_VERTSPLIT
1327 if (vertical)
1329 if (win_split(curwin->w_width - (curwin->w_width - todo)
1330 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1331 break;
1333 else
1334 #endif
1336 if (win_split(curwin->w_height - (curwin->w_height - todo
1337 * STATUS_HEIGHT) / (todo + 1)
1338 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1339 break;
1342 #ifdef FEAT_AUTOCMD
1343 unblock_autocmds();
1344 #endif
1346 /* return actual number of windows */
1347 return (count - todo);
1351 * Exchange current and next window
1353 static void
1354 win_exchange(Prenum)
1355 long Prenum;
1357 frame_T *frp;
1358 frame_T *frp2;
1359 win_T *wp;
1360 win_T *wp2;
1361 int temp;
1363 if (lastwin == firstwin) /* just one window */
1365 beep_flush();
1366 return;
1369 #ifdef FEAT_GUI
1370 need_mouse_correct = TRUE;
1371 #endif
1374 * find window to exchange with
1376 if (Prenum)
1378 frp = curwin->w_frame->fr_parent->fr_child;
1379 while (frp != NULL && --Prenum > 0)
1380 frp = frp->fr_next;
1382 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1383 frp = curwin->w_frame->fr_next;
1384 else /* Swap last window in row/col with previous */
1385 frp = curwin->w_frame->fr_prev;
1387 /* We can only exchange a window with another window, not with a frame
1388 * containing windows. */
1389 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1390 return;
1391 wp = frp->fr_win;
1394 * 1. remove curwin from the list. Remember after which window it was in wp2
1395 * 2. insert curwin before wp in the list
1396 * if wp != wp2
1397 * 3. remove wp from the list
1398 * 4. insert wp after wp2
1399 * 5. exchange the status line height and vsep width.
1401 wp2 = curwin->w_prev;
1402 frp2 = curwin->w_frame->fr_prev;
1403 if (wp->w_prev != curwin)
1405 win_remove(curwin, NULL);
1406 frame_remove(curwin->w_frame);
1407 win_append(wp->w_prev, curwin);
1408 frame_insert(frp, curwin->w_frame);
1410 if (wp != wp2)
1412 win_remove(wp, NULL);
1413 frame_remove(wp->w_frame);
1414 win_append(wp2, wp);
1415 if (frp2 == NULL)
1416 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1417 else
1418 frame_append(frp2, wp->w_frame);
1420 temp = curwin->w_status_height;
1421 curwin->w_status_height = wp->w_status_height;
1422 wp->w_status_height = temp;
1423 #ifdef FEAT_VERTSPLIT
1424 temp = curwin->w_vsep_width;
1425 curwin->w_vsep_width = wp->w_vsep_width;
1426 wp->w_vsep_width = temp;
1428 /* If the windows are not in the same frame, exchange the sizes to avoid
1429 * messing up the window layout. Otherwise fix the frame sizes. */
1430 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1432 temp = curwin->w_height;
1433 curwin->w_height = wp->w_height;
1434 wp->w_height = temp;
1435 temp = curwin->w_width;
1436 curwin->w_width = wp->w_width;
1437 wp->w_width = temp;
1439 else
1441 frame_fix_height(curwin);
1442 frame_fix_height(wp);
1443 frame_fix_width(curwin);
1444 frame_fix_width(wp);
1446 #endif
1448 (void)win_comp_pos(); /* recompute window positions */
1450 win_enter(wp, TRUE);
1451 redraw_later(CLEAR);
1455 * rotate windows: if upwards TRUE the second window becomes the first one
1456 * if upwards FALSE the first window becomes the second one
1458 static void
1459 win_rotate(upwards, count)
1460 int upwards;
1461 int count;
1463 win_T *wp1;
1464 win_T *wp2;
1465 frame_T *frp;
1466 int n;
1468 if (firstwin == lastwin) /* nothing to do */
1470 beep_flush();
1471 return;
1474 #ifdef FEAT_GUI
1475 need_mouse_correct = TRUE;
1476 #endif
1478 #ifdef FEAT_VERTSPLIT
1479 /* Check if all frames in this row/col have one window. */
1480 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1481 frp = frp->fr_next)
1482 if (frp->fr_win == NULL)
1484 EMSG(_("E443: Cannot rotate when another window is split"));
1485 return;
1487 #endif
1489 while (count--)
1491 if (upwards) /* first window becomes last window */
1493 /* remove first window/frame from the list */
1494 frp = curwin->w_frame->fr_parent->fr_child;
1495 wp1 = frp->fr_win;
1496 win_remove(wp1, NULL);
1497 frame_remove(frp);
1499 /* find last frame and append removed window/frame after it */
1500 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1502 win_append(frp->fr_win, wp1);
1503 frame_append(frp, wp1->w_frame);
1505 wp2 = frp->fr_win; /* previously last window */
1507 else /* last window becomes first window */
1509 /* find last window/frame in the list and remove it */
1510 for (frp = curwin->w_frame; frp->fr_next != NULL;
1511 frp = frp->fr_next)
1513 wp1 = frp->fr_win;
1514 wp2 = wp1->w_prev; /* will become last window */
1515 win_remove(wp1, NULL);
1516 frame_remove(frp);
1518 /* append the removed window/frame before the first in the list */
1519 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1520 frame_insert(frp->fr_parent->fr_child, frp);
1523 /* exchange status height and vsep width of old and new last window */
1524 n = wp2->w_status_height;
1525 wp2->w_status_height = wp1->w_status_height;
1526 wp1->w_status_height = n;
1527 frame_fix_height(wp1);
1528 frame_fix_height(wp2);
1529 #ifdef FEAT_VERTSPLIT
1530 n = wp2->w_vsep_width;
1531 wp2->w_vsep_width = wp1->w_vsep_width;
1532 wp1->w_vsep_width = n;
1533 frame_fix_width(wp1);
1534 frame_fix_width(wp2);
1535 #endif
1537 /* recompute w_winrow and w_wincol for all windows */
1538 (void)win_comp_pos();
1541 redraw_later(CLEAR);
1545 * Move the current window to the very top/bottom/left/right of the screen.
1547 static void
1548 win_totop(size, flags)
1549 int size;
1550 int flags;
1552 int dir;
1553 int height = curwin->w_height;
1555 if (lastwin == firstwin)
1557 beep_flush();
1558 return;
1561 /* Remove the window and frame from the tree of frames. */
1562 (void)winframe_remove(curwin, &dir, NULL);
1563 win_remove(curwin, NULL);
1564 last_status(FALSE); /* may need to remove last status line */
1565 (void)win_comp_pos(); /* recompute window positions */
1567 /* Split a window on the desired side and put the window there. */
1568 (void)win_split_ins(size, flags, curwin, dir);
1569 if (!(flags & WSP_VERT))
1571 win_setheight(height);
1572 if (p_ea)
1573 win_equal(curwin, TRUE, 'v');
1576 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1577 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1578 * scrollbars. Have to update them anyway. */
1579 gui_may_update_scrollbars();
1580 #endif
1584 * Move window "win1" to below/right of "win2" and make "win1" the current
1585 * window. Only works within the same frame!
1587 void
1588 win_move_after(win1, win2)
1589 win_T *win1, *win2;
1591 int height;
1593 /* check if the arguments are reasonable */
1594 if (win1 == win2)
1595 return;
1597 /* check if there is something to do */
1598 if (win2->w_next != win1)
1600 /* may need move the status line/vertical separator of the last window
1601 * */
1602 if (win1 == lastwin)
1604 height = win1->w_prev->w_status_height;
1605 win1->w_prev->w_status_height = win1->w_status_height;
1606 win1->w_status_height = height;
1607 #ifdef FEAT_VERTSPLIT
1608 if (win1->w_prev->w_vsep_width == 1)
1610 /* Remove the vertical separator from the last-but-one window,
1611 * add it to the last window. Adjust the frame widths. */
1612 win1->w_prev->w_vsep_width = 0;
1613 win1->w_prev->w_frame->fr_width -= 1;
1614 win1->w_vsep_width = 1;
1615 win1->w_frame->fr_width += 1;
1617 #endif
1619 else if (win2 == lastwin)
1621 height = win1->w_status_height;
1622 win1->w_status_height = win2->w_status_height;
1623 win2->w_status_height = height;
1624 #ifdef FEAT_VERTSPLIT
1625 if (win1->w_vsep_width == 1)
1627 /* Remove the vertical separator from win1, add it to the last
1628 * window, win2. Adjust the frame widths. */
1629 win2->w_vsep_width = 1;
1630 win2->w_frame->fr_width += 1;
1631 win1->w_vsep_width = 0;
1632 win1->w_frame->fr_width -= 1;
1634 #endif
1636 win_remove(win1, NULL);
1637 frame_remove(win1->w_frame);
1638 win_append(win2, win1);
1639 frame_append(win2->w_frame, win1->w_frame);
1641 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1642 redraw_later(NOT_VALID);
1644 win_enter(win1, FALSE);
1648 * Make all windows the same height.
1649 * 'next_curwin' will soon be the current window, make sure it has enough
1650 * rows.
1652 void
1653 win_equal(next_curwin, current, dir)
1654 win_T *next_curwin; /* pointer to current window to be or NULL */
1655 int current; /* do only frame with current window */
1656 int dir; /* 'v' for vertically, 'h' for horizontally,
1657 'b' for both, 0 for using p_ead */
1659 if (dir == 0)
1660 #ifdef FEAT_VERTSPLIT
1661 dir = *p_ead;
1662 #else
1663 dir = 'b';
1664 #endif
1665 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1666 topframe, dir, 0, tabline_height(),
1667 (int)Columns, topframe->fr_height);
1671 * Set a frame to a new position and height, spreading the available room
1672 * equally over contained frames.
1673 * The window "next_curwin" (if not NULL) should at least get the size from
1674 * 'winheight' and 'winwidth' if possible.
1676 static void
1677 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1678 win_T *next_curwin; /* pointer to current window to be or NULL */
1679 int current; /* do only frame with current window */
1680 frame_T *topfr; /* frame to set size off */
1681 int dir; /* 'v', 'h' or 'b', see win_equal() */
1682 int col; /* horizontal position for frame */
1683 int row; /* vertical position for frame */
1684 int width; /* new width of frame */
1685 int height; /* new height of frame */
1687 int n, m;
1688 int extra_sep = 0;
1689 int wincount, totwincount = 0;
1690 frame_T *fr;
1691 int next_curwin_size = 0;
1692 int room = 0;
1693 int new_size;
1694 int has_next_curwin = 0;
1695 int hnc;
1697 if (topfr->fr_layout == FR_LEAF)
1699 /* Set the width/height of this frame.
1700 * Redraw when size or position changes */
1701 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1702 #ifdef FEAT_VERTSPLIT
1703 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1704 #endif
1707 topfr->fr_win->w_winrow = row;
1708 frame_new_height(topfr, height, FALSE, FALSE);
1709 #ifdef FEAT_VERTSPLIT
1710 topfr->fr_win->w_wincol = col;
1711 frame_new_width(topfr, width, FALSE, FALSE);
1712 #endif
1713 redraw_all_later(CLEAR);
1716 #ifdef FEAT_VERTSPLIT
1717 else if (topfr->fr_layout == FR_ROW)
1719 topfr->fr_width = width;
1720 topfr->fr_height = height;
1722 if (dir != 'v') /* equalize frame widths */
1724 /* Compute the maximum number of windows horizontally in this
1725 * frame. */
1726 n = frame_minwidth(topfr, NOWIN);
1727 /* add one for the rightmost window, it doesn't have a separator */
1728 if (col + width == Columns)
1729 extra_sep = 1;
1730 else
1731 extra_sep = 0;
1732 totwincount = (n + extra_sep) / (p_wmw + 1);
1733 has_next_curwin = frame_has_win(topfr, next_curwin);
1736 * Compute width for "next_curwin" window and room available for
1737 * other windows.
1738 * "m" is the minimal width when counting p_wiw for "next_curwin".
1740 m = frame_minwidth(topfr, next_curwin);
1741 room = width - m;
1742 if (room < 0)
1744 next_curwin_size = p_wiw + room;
1745 room = 0;
1747 else
1749 next_curwin_size = -1;
1750 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1752 /* If 'winfixwidth' set keep the window width if
1753 * possible.
1754 * Watch out for this window being the next_curwin. */
1755 if (frame_fixed_width(fr))
1757 n = frame_minwidth(fr, NOWIN);
1758 new_size = fr->fr_width;
1759 if (frame_has_win(fr, next_curwin))
1761 room += p_wiw - p_wmw;
1762 next_curwin_size = 0;
1763 if (new_size < p_wiw)
1764 new_size = p_wiw;
1766 else
1767 /* These windows don't use up room. */
1768 totwincount -= (n + (fr->fr_next == NULL
1769 ? extra_sep : 0)) / (p_wmw + 1);
1770 room -= new_size - n;
1771 if (room < 0)
1773 new_size += room;
1774 room = 0;
1776 fr->fr_newwidth = new_size;
1779 if (next_curwin_size == -1)
1781 if (!has_next_curwin)
1782 next_curwin_size = 0;
1783 else if (totwincount > 1
1784 && (room + (totwincount - 2))
1785 / (totwincount - 1) > p_wiw)
1787 /* Can make all windows wider than 'winwidth', spread
1788 * the room equally. */
1789 next_curwin_size = (room + p_wiw
1790 + (totwincount - 1) * p_wmw
1791 + (totwincount - 1)) / totwincount;
1792 room -= next_curwin_size - p_wiw;
1794 else
1795 next_curwin_size = p_wiw;
1799 if (has_next_curwin)
1800 --totwincount; /* don't count curwin */
1803 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1805 n = m = 0;
1806 wincount = 1;
1807 if (fr->fr_next == NULL)
1808 /* last frame gets all that remains (avoid roundoff error) */
1809 new_size = width;
1810 else if (dir == 'v')
1811 new_size = fr->fr_width;
1812 else if (frame_fixed_width(fr))
1814 new_size = fr->fr_newwidth;
1815 wincount = 0; /* doesn't count as a sizeable window */
1817 else
1819 /* Compute the maximum number of windows horiz. in "fr". */
1820 n = frame_minwidth(fr, NOWIN);
1821 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1822 / (p_wmw + 1);
1823 m = frame_minwidth(fr, next_curwin);
1824 if (has_next_curwin)
1825 hnc = frame_has_win(fr, next_curwin);
1826 else
1827 hnc = FALSE;
1828 if (hnc) /* don't count next_curwin */
1829 --wincount;
1830 if (totwincount == 0)
1831 new_size = room;
1832 else
1833 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1834 / totwincount;
1835 if (hnc) /* add next_curwin size */
1837 next_curwin_size -= p_wiw - (m - n);
1838 new_size += next_curwin_size;
1839 room -= new_size - next_curwin_size;
1841 else
1842 room -= new_size;
1843 new_size += n;
1846 /* Skip frame that is full width when splitting or closing a
1847 * window, unless equalizing all frames. */
1848 if (!current || dir != 'v' || topfr->fr_parent != NULL
1849 || (new_size != fr->fr_width)
1850 || frame_has_win(fr, next_curwin))
1851 win_equal_rec(next_curwin, current, fr, dir, col, row,
1852 new_size, height);
1853 col += new_size;
1854 width -= new_size;
1855 totwincount -= wincount;
1858 #endif
1859 else /* topfr->fr_layout == FR_COL */
1861 #ifdef FEAT_VERTSPLIT
1862 topfr->fr_width = width;
1863 #endif
1864 topfr->fr_height = height;
1866 if (dir != 'h') /* equalize frame heights */
1868 /* Compute maximum number of windows vertically in this frame. */
1869 n = frame_minheight(topfr, NOWIN);
1870 /* add one for the bottom window if it doesn't have a statusline */
1871 if (row + height == cmdline_row && p_ls == 0)
1872 extra_sep = 1;
1873 else
1874 extra_sep = 0;
1875 totwincount = (n + extra_sep) / (p_wmh + 1);
1876 has_next_curwin = frame_has_win(topfr, next_curwin);
1879 * Compute height for "next_curwin" window and room available for
1880 * other windows.
1881 * "m" is the minimal height when counting p_wh for "next_curwin".
1883 m = frame_minheight(topfr, next_curwin);
1884 room = height - m;
1885 if (room < 0)
1887 /* The room is less then 'winheight', use all space for the
1888 * current window. */
1889 next_curwin_size = p_wh + room;
1890 room = 0;
1892 else
1894 next_curwin_size = -1;
1895 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1897 /* If 'winfixheight' set keep the window height if
1898 * possible.
1899 * Watch out for this window being the next_curwin. */
1900 if (frame_fixed_height(fr))
1902 n = frame_minheight(fr, NOWIN);
1903 new_size = fr->fr_height;
1904 if (frame_has_win(fr, next_curwin))
1906 room += p_wh - p_wmh;
1907 next_curwin_size = 0;
1908 if (new_size < p_wh)
1909 new_size = p_wh;
1911 else
1912 /* These windows don't use up room. */
1913 totwincount -= (n + (fr->fr_next == NULL
1914 ? extra_sep : 0)) / (p_wmh + 1);
1915 room -= new_size - n;
1916 if (room < 0)
1918 new_size += room;
1919 room = 0;
1921 fr->fr_newheight = new_size;
1924 if (next_curwin_size == -1)
1926 if (!has_next_curwin)
1927 next_curwin_size = 0;
1928 else if (totwincount > 1
1929 && (room + (totwincount - 2))
1930 / (totwincount - 1) > p_wh)
1932 /* can make all windows higher than 'winheight',
1933 * spread the room equally. */
1934 next_curwin_size = (room + p_wh
1935 + (totwincount - 1) * p_wmh
1936 + (totwincount - 1)) / totwincount;
1937 room -= next_curwin_size - p_wh;
1939 else
1940 next_curwin_size = p_wh;
1944 if (has_next_curwin)
1945 --totwincount; /* don't count curwin */
1948 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1950 n = m = 0;
1951 wincount = 1;
1952 if (fr->fr_next == NULL)
1953 /* last frame gets all that remains (avoid roundoff error) */
1954 new_size = height;
1955 else if (dir == 'h')
1956 new_size = fr->fr_height;
1957 else if (frame_fixed_height(fr))
1959 new_size = fr->fr_newheight;
1960 wincount = 0; /* doesn't count as a sizeable window */
1962 else
1964 /* Compute the maximum number of windows vert. in "fr". */
1965 n = frame_minheight(fr, NOWIN);
1966 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1967 / (p_wmh + 1);
1968 m = frame_minheight(fr, next_curwin);
1969 if (has_next_curwin)
1970 hnc = frame_has_win(fr, next_curwin);
1971 else
1972 hnc = FALSE;
1973 if (hnc) /* don't count next_curwin */
1974 --wincount;
1975 if (totwincount == 0)
1976 new_size = room;
1977 else
1978 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1979 / totwincount;
1980 if (hnc) /* add next_curwin size */
1982 next_curwin_size -= p_wh - (m - n);
1983 new_size += next_curwin_size;
1984 room -= new_size - next_curwin_size;
1986 else
1987 room -= new_size;
1988 new_size += n;
1990 /* Skip frame that is full width when splitting or closing a
1991 * window, unless equalizing all frames. */
1992 if (!current || dir != 'h' || topfr->fr_parent != NULL
1993 || (new_size != fr->fr_height)
1994 || frame_has_win(fr, next_curwin))
1995 win_equal_rec(next_curwin, current, fr, dir, col, row,
1996 width, new_size);
1997 row += new_size;
1998 height -= new_size;
1999 totwincount -= wincount;
2005 * close all windows for buffer 'buf'
2007 void
2008 close_windows(buf, keep_curwin)
2009 buf_T *buf;
2010 int keep_curwin; /* don't close "curwin" */
2012 win_T *wp;
2013 tabpage_T *tp, *nexttp;
2014 int h = tabline_height();
2016 ++RedrawingDisabled;
2018 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2020 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2022 win_close(wp, FALSE);
2024 /* Start all over, autocommands may change the window layout. */
2025 wp = firstwin;
2027 else
2028 wp = wp->w_next;
2031 /* Also check windows in other tab pages. */
2032 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2034 nexttp = tp->tp_next;
2035 if (tp != curtab)
2036 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2037 if (wp->w_buffer == buf)
2039 win_close_othertab(wp, FALSE, tp);
2041 /* Start all over, the tab page may be closed and
2042 * autocommands may change the window layout. */
2043 nexttp = first_tabpage;
2044 break;
2048 --RedrawingDisabled;
2050 if (h != tabline_height())
2051 shell_new_rows();
2055 * Return TRUE if the current window is the only window that exists (ignoring
2056 * "aucmd_win").
2057 * Returns FALSE if there is a window, possibly in another tab page.
2059 static int
2060 last_window()
2062 return (one_window() && first_tabpage->tp_next == NULL);
2066 * Return TRUE if there is only one window other than "aucmd_win" in the
2067 * current tab page.
2069 static int
2070 one_window()
2072 #ifdef FEAT_AUTOCMD
2073 win_T *wp;
2074 int seen_one = FALSE;
2076 FOR_ALL_WINDOWS(wp)
2078 if (wp != aucmd_win)
2080 if (seen_one)
2081 return FALSE;
2082 seen_one = TRUE;
2085 return TRUE;
2086 #else
2087 return firstwin == lastwin;
2088 #endif
2092 * Close window "win". Only works for the current tab page.
2093 * If "free_buf" is TRUE related buffer may be unloaded.
2095 * called by :quit, :close, :xit, :wq and findtag()
2097 void
2098 win_close(win, free_buf)
2099 win_T *win;
2100 int free_buf;
2102 win_T *wp;
2103 #ifdef FEAT_AUTOCMD
2104 int other_buffer = FALSE;
2105 #endif
2106 int close_curwin = FALSE;
2107 int dir;
2108 int help_window = FALSE;
2109 tabpage_T *prev_curtab = curtab;
2111 if (last_window())
2113 EMSG(_("E444: Cannot close last window"));
2114 return;
2117 #ifdef FEAT_AUTOCMD
2118 if (win == aucmd_win)
2120 EMSG(_("E813: Cannot close autocmd window"));
2121 return;
2123 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2125 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2126 return;
2128 #endif
2131 * When closing the last window in a tab page first go to another tab
2132 * page and then close the window and the tab page. This avoids that
2133 * curwin and curtab are not invalid while we are freeing memory, they may
2134 * be used in GUI events.
2136 if (firstwin == lastwin)
2138 goto_tabpage_tp(alt_tabpage());
2139 redraw_tabline = TRUE;
2141 /* Safety check: Autocommands may have closed the window when jumping
2142 * to the other tab page. */
2143 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2145 int h = tabline_height();
2147 win_close_othertab(win, free_buf, prev_curtab);
2148 if (h != tabline_height())
2149 shell_new_rows();
2151 return;
2154 /* When closing the help window, try restoring a snapshot after closing
2155 * the window. Otherwise clear the snapshot, it's now invalid. */
2156 if (win->w_buffer->b_help)
2157 help_window = TRUE;
2158 else
2159 clear_snapshot(curtab, SNAP_HELP_IDX);
2161 #ifdef FEAT_AUTOCMD
2162 if (win == curwin)
2165 * Guess which window is going to be the new current window.
2166 * This may change because of the autocommands (sigh).
2168 wp = frame2win(win_altframe(win, NULL));
2171 * Be careful: If autocommands delete the window, return now.
2173 if (wp->w_buffer != curbuf)
2175 other_buffer = TRUE;
2176 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2177 if (!win_valid(win) || last_window())
2178 return;
2180 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2181 if (!win_valid(win) || last_window())
2182 return;
2183 # ifdef FEAT_EVAL
2184 /* autocmds may abort script processing */
2185 if (aborting())
2186 return;
2187 # endif
2189 #endif
2191 #ifdef FEAT_GUI
2192 /* Avoid trouble with scrollbars that are going to be deleted in
2193 * win_free(). */
2194 if (gui.in_use)
2195 out_flush();
2196 #endif
2199 * Close the link to the buffer.
2201 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2203 /* Autocommands may have closed the window already, or closed the only
2204 * other window or moved to another tab page. */
2205 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2206 return;
2208 /* Free the memory used for the window. */
2209 wp = win_free_mem(win, &dir, NULL);
2211 /* Make sure curwin isn't invalid. It can cause severe trouble when
2212 * printing an error message. For win_equal() curbuf needs to be valid
2213 * too. */
2214 if (win == curwin)
2216 curwin = wp;
2217 #ifdef FEAT_QUICKFIX
2218 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2221 * If the cursor goes to the preview or the quickfix window, try
2222 * finding another window to go to.
2224 for (;;)
2226 if (wp->w_next == NULL)
2227 wp = firstwin;
2228 else
2229 wp = wp->w_next;
2230 if (wp == curwin)
2231 break;
2232 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2234 curwin = wp;
2235 break;
2239 #endif
2240 curbuf = curwin->w_buffer;
2241 close_curwin = TRUE;
2243 if (p_ea
2244 #ifdef FEAT_VERTSPLIT
2245 && (*p_ead == 'b' || *p_ead == dir)
2246 #endif
2248 win_equal(curwin, TRUE,
2249 #ifdef FEAT_VERTSPLIT
2251 #else
2253 #endif
2255 else
2256 win_comp_pos();
2257 if (close_curwin)
2259 win_enter_ext(wp, FALSE, TRUE);
2260 #ifdef FEAT_AUTOCMD
2261 if (other_buffer)
2262 /* careful: after this wp and win may be invalid! */
2263 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2264 #endif
2268 * If last window has a status line now and we don't want one,
2269 * remove the status line.
2271 last_status(FALSE);
2273 /* After closing the help window, try restoring the window layout from
2274 * before it was opened. */
2275 if (help_window)
2276 restore_snapshot(SNAP_HELP_IDX, close_curwin);
2278 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2279 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2280 if (gui.in_use && !win_hasvertsplit())
2281 gui_init_which_components(NULL);
2282 #endif
2284 redraw_all_later(NOT_VALID);
2288 * Close window "win" in tab page "tp", which is not the current tab page.
2289 * This may be the last window ih that tab page and result in closing the tab,
2290 * thus "tp" may become invalid!
2291 * Caller must check if buffer is hidden and whether the tabline needs to be
2292 * updated.
2294 void
2295 win_close_othertab(win, free_buf, tp)
2296 win_T *win;
2297 int free_buf;
2298 tabpage_T *tp;
2300 win_T *wp;
2301 int dir;
2302 tabpage_T *ptp = NULL;
2304 /* Close the link to the buffer. */
2305 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2307 /* Careful: Autocommands may have closed the tab page or made it the
2308 * current tab page. */
2309 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2311 if (ptp == NULL || tp == curtab)
2312 return;
2314 /* Autocommands may have closed the window already. */
2315 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2317 if (wp == NULL)
2318 return;
2320 /* Free the memory used for the window. */
2321 wp = win_free_mem(win, &dir, tp);
2323 /* When closing the last window in a tab page remove the tab page. */
2324 if (wp == NULL)
2326 if (tp == first_tabpage)
2327 first_tabpage = tp->tp_next;
2328 else
2330 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2331 ptp = ptp->tp_next)
2333 if (ptp == NULL)
2335 EMSG2(_(e_intern2), "win_close_othertab()");
2336 return;
2338 ptp->tp_next = tp->tp_next;
2340 free_tabpage(tp);
2345 * Free the memory used for a window.
2346 * Returns a pointer to the window that got the freed up space.
2348 static win_T *
2349 win_free_mem(win, dirp, tp)
2350 win_T *win;
2351 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2354 frame_T *frp;
2355 win_T *wp;
2357 #ifdef FEAT_FOLDING
2358 clearFolding(win);
2359 #endif
2361 /* reduce the reference count to the argument list. */
2362 alist_unlink(win->w_alist);
2364 /* Remove the window and its frame from the tree of frames. */
2365 frp = win->w_frame;
2366 wp = winframe_remove(win, dirp, tp);
2367 vim_free(frp);
2368 win_free(win, tp);
2370 /* When deleting the current window of another tab page select a new
2371 * current window. */
2372 if (tp != NULL && win == tp->tp_curwin)
2373 tp->tp_curwin = wp;
2375 return wp;
2378 #if defined(EXITFREE) || defined(PROTO)
2379 void
2380 win_free_all()
2382 int dummy;
2384 # ifdef FEAT_WINDOWS
2385 while (first_tabpage->tp_next != NULL)
2386 tabpage_close(TRUE);
2387 # endif
2389 while (firstwin != NULL)
2390 (void)win_free_mem(firstwin, &dummy, NULL);
2392 # ifdef FEAT_AUTOCMD
2393 if (aucmd_win != NULL)
2395 (void)win_free_mem(aucmd_win, &dummy, NULL);
2396 aucmd_win = NULL;
2398 # endif
2400 #endif
2403 * Remove a window and its frame from the tree of frames.
2404 * Returns a pointer to the window that got the freed up space.
2406 win_T *
2407 winframe_remove(win, dirp, tp)
2408 win_T *win;
2409 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
2410 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2412 frame_T *frp, *frp2, *frp3;
2413 frame_T *frp_close = win->w_frame;
2414 win_T *wp;
2417 * If there is only one window there is nothing to remove.
2419 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2420 return NULL;
2423 * Remove the window from its frame.
2425 frp2 = win_altframe(win, tp);
2426 wp = frame2win(frp2);
2428 /* Remove this frame from the list of frames. */
2429 frame_remove(frp_close);
2431 #ifdef FEAT_VERTSPLIT
2432 if (frp_close->fr_parent->fr_layout == FR_COL)
2434 #endif
2435 /* When 'winfixheight' is set, try to find another frame in the column
2436 * (as close to the closed frame as possible) to distribute the height
2437 * to. */
2438 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2440 frp = frp_close->fr_prev;
2441 frp3 = frp_close->fr_next;
2442 while (frp != NULL || frp3 != NULL)
2444 if (frp != NULL)
2446 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2448 frp2 = frp;
2449 wp = frp->fr_win;
2450 break;
2452 frp = frp->fr_prev;
2454 if (frp3 != NULL)
2456 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2458 frp2 = frp3;
2459 wp = frp3->fr_win;
2460 break;
2462 frp3 = frp3->fr_next;
2466 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2467 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2468 #ifdef FEAT_VERTSPLIT
2469 *dirp = 'v';
2471 else
2473 /* When 'winfixwidth' is set, try to find another frame in the column
2474 * (as close to the closed frame as possible) to distribute the width
2475 * to. */
2476 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2478 frp = frp_close->fr_prev;
2479 frp3 = frp_close->fr_next;
2480 while (frp != NULL || frp3 != NULL)
2482 if (frp != NULL)
2484 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2486 frp2 = frp;
2487 wp = frp->fr_win;
2488 break;
2490 frp = frp->fr_prev;
2492 if (frp3 != NULL)
2494 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2496 frp2 = frp3;
2497 wp = frp3->fr_win;
2498 break;
2500 frp3 = frp3->fr_next;
2504 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2505 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2506 *dirp = 'h';
2508 #endif
2510 /* If rows/columns go to a window below/right its positions need to be
2511 * updated. Can only be done after the sizes have been updated. */
2512 if (frp2 == frp_close->fr_next)
2514 int row = win->w_winrow;
2515 int col = W_WINCOL(win);
2517 frame_comp_pos(frp2, &row, &col);
2520 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2522 /* There is no other frame in this list, move its info to the parent
2523 * and remove it. */
2524 frp2->fr_parent->fr_layout = frp2->fr_layout;
2525 frp2->fr_parent->fr_child = frp2->fr_child;
2526 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2527 frp->fr_parent = frp2->fr_parent;
2528 frp2->fr_parent->fr_win = frp2->fr_win;
2529 if (frp2->fr_win != NULL)
2530 frp2->fr_win->w_frame = frp2->fr_parent;
2531 frp = frp2->fr_parent;
2532 vim_free(frp2);
2534 frp2 = frp->fr_parent;
2535 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2537 /* The frame above the parent has the same layout, have to merge
2538 * the frames into this list. */
2539 if (frp2->fr_child == frp)
2540 frp2->fr_child = frp->fr_child;
2541 frp->fr_child->fr_prev = frp->fr_prev;
2542 if (frp->fr_prev != NULL)
2543 frp->fr_prev->fr_next = frp->fr_child;
2544 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2546 frp3->fr_parent = frp2;
2547 if (frp3->fr_next == NULL)
2549 frp3->fr_next = frp->fr_next;
2550 if (frp->fr_next != NULL)
2551 frp->fr_next->fr_prev = frp3;
2552 break;
2555 vim_free(frp);
2559 return wp;
2563 * Find out which frame is going to get the freed up space when "win" is
2564 * closed.
2565 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2566 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2567 * This makes opening a window and closing it immediately keep the same window
2568 * layout.
2570 static frame_T *
2571 win_altframe(win, tp)
2572 win_T *win;
2573 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2575 frame_T *frp;
2576 int b;
2578 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2579 /* Last window in this tab page, will go to next tab page. */
2580 return alt_tabpage()->tp_curwin->w_frame;
2582 frp = win->w_frame;
2583 #ifdef FEAT_VERTSPLIT
2584 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2585 b = p_spr;
2586 else
2587 #endif
2588 b = p_sb;
2589 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2590 return frp->fr_next;
2591 return frp->fr_prev;
2595 * Return the tabpage that will be used if the current one is closed.
2597 static tabpage_T *
2598 alt_tabpage()
2600 tabpage_T *tp;
2602 /* Use the next tab page if possible. */
2603 if (curtab->tp_next != NULL)
2604 return curtab->tp_next;
2606 /* Find the last but one tab page. */
2607 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2609 return tp;
2613 * Find the left-upper window in frame "frp".
2615 static win_T *
2616 frame2win(frp)
2617 frame_T *frp;
2619 while (frp->fr_win == NULL)
2620 frp = frp->fr_child;
2621 return frp->fr_win;
2625 * Return TRUE if frame "frp" contains window "wp".
2627 static int
2628 frame_has_win(frp, wp)
2629 frame_T *frp;
2630 win_T *wp;
2632 frame_T *p;
2634 if (frp->fr_layout == FR_LEAF)
2635 return frp->fr_win == wp;
2637 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2638 if (frame_has_win(p, wp))
2639 return TRUE;
2640 return FALSE;
2644 * Set a new height for a frame. Recursively sets the height for contained
2645 * frames and windows. Caller must take care of positions.
2647 static void
2648 frame_new_height(topfrp, height, topfirst, wfh)
2649 frame_T *topfrp;
2650 int height;
2651 int topfirst; /* resize topmost contained frame first */
2652 int wfh; /* obey 'winfixheight' when there is a choice;
2653 may cause the height not to be set */
2655 frame_T *frp;
2656 int extra_lines;
2657 int h;
2659 if (topfrp->fr_win != NULL)
2661 /* Simple case: just one window. */
2662 win_new_height(topfrp->fr_win,
2663 height - topfrp->fr_win->w_status_height);
2665 #ifdef FEAT_VERTSPLIT
2666 else if (topfrp->fr_layout == FR_ROW)
2670 /* All frames in this row get the same new height. */
2671 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2673 frame_new_height(frp, height, topfirst, wfh);
2674 if (frp->fr_height > height)
2676 /* Could not fit the windows, make the whole row higher. */
2677 height = frp->fr_height;
2678 break;
2682 while (frp != NULL);
2684 #endif
2685 else /* fr_layout == FR_COL */
2687 /* Complicated case: Resize a column of frames. Resize the bottom
2688 * frame first, frames above that when needed. */
2690 frp = topfrp->fr_child;
2691 if (wfh)
2692 /* Advance past frames with one window with 'wfh' set. */
2693 while (frame_fixed_height(frp))
2695 frp = frp->fr_next;
2696 if (frp == NULL)
2697 return; /* no frame without 'wfh', give up */
2699 if (!topfirst)
2701 /* Find the bottom frame of this column */
2702 while (frp->fr_next != NULL)
2703 frp = frp->fr_next;
2704 if (wfh)
2705 /* Advance back for frames with one window with 'wfh' set. */
2706 while (frame_fixed_height(frp))
2707 frp = frp->fr_prev;
2710 extra_lines = height - topfrp->fr_height;
2711 if (extra_lines < 0)
2713 /* reduce height of contained frames, bottom or top frame first */
2714 while (frp != NULL)
2716 h = frame_minheight(frp, NULL);
2717 if (frp->fr_height + extra_lines < h)
2719 extra_lines += frp->fr_height - h;
2720 frame_new_height(frp, h, topfirst, wfh);
2722 else
2724 frame_new_height(frp, frp->fr_height + extra_lines,
2725 topfirst, wfh);
2726 break;
2728 if (topfirst)
2731 frp = frp->fr_next;
2732 while (wfh && frp != NULL && frame_fixed_height(frp));
2734 else
2737 frp = frp->fr_prev;
2738 while (wfh && frp != NULL && frame_fixed_height(frp));
2740 /* Increase "height" if we could not reduce enough frames. */
2741 if (frp == NULL)
2742 height -= extra_lines;
2745 else if (extra_lines > 0)
2747 /* increase height of bottom or top frame */
2748 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2751 topfrp->fr_height = height;
2755 * Return TRUE if height of frame "frp" should not be changed because of
2756 * the 'winfixheight' option.
2758 static int
2759 frame_fixed_height(frp)
2760 frame_T *frp;
2762 /* frame with one window: fixed height if 'winfixheight' set. */
2763 if (frp->fr_win != NULL)
2764 return frp->fr_win->w_p_wfh;
2766 if (frp->fr_layout == FR_ROW)
2768 /* The frame is fixed height if one of the frames in the row is fixed
2769 * height. */
2770 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2771 if (frame_fixed_height(frp))
2772 return TRUE;
2773 return FALSE;
2776 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2777 * frames in the row are fixed height. */
2778 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2779 if (!frame_fixed_height(frp))
2780 return FALSE;
2781 return TRUE;
2784 #ifdef FEAT_VERTSPLIT
2786 * Return TRUE if width of frame "frp" should not be changed because of
2787 * the 'winfixwidth' option.
2789 static int
2790 frame_fixed_width(frp)
2791 frame_T *frp;
2793 /* frame with one window: fixed width if 'winfixwidth' set. */
2794 if (frp->fr_win != NULL)
2795 return frp->fr_win->w_p_wfw;
2797 if (frp->fr_layout == FR_COL)
2799 /* The frame is fixed width if one of the frames in the row is fixed
2800 * width. */
2801 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2802 if (frame_fixed_width(frp))
2803 return TRUE;
2804 return FALSE;
2807 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2808 * frames in the row are fixed width. */
2809 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2810 if (!frame_fixed_width(frp))
2811 return FALSE;
2812 return TRUE;
2816 * Add a status line to windows at the bottom of "frp".
2817 * Note: Does not check if there is room!
2819 static void
2820 frame_add_statusline(frp)
2821 frame_T *frp;
2823 win_T *wp;
2825 if (frp->fr_layout == FR_LEAF)
2827 wp = frp->fr_win;
2828 if (wp->w_status_height == 0)
2830 if (wp->w_height > 0) /* don't make it negative */
2831 --wp->w_height;
2832 wp->w_status_height = STATUS_HEIGHT;
2835 else if (frp->fr_layout == FR_ROW)
2837 /* Handle all the frames in the row. */
2838 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2839 frame_add_statusline(frp);
2841 else /* frp->fr_layout == FR_COL */
2843 /* Only need to handle the last frame in the column. */
2844 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2846 frame_add_statusline(frp);
2851 * Set width of a frame. Handles recursively going through contained frames.
2852 * May remove separator line for windows at the right side (for win_close()).
2854 static void
2855 frame_new_width(topfrp, width, leftfirst, wfw)
2856 frame_T *topfrp;
2857 int width;
2858 int leftfirst; /* resize leftmost contained frame first */
2859 int wfw; /* obey 'winfixwidth' when there is a choice;
2860 may cause the width not to be set */
2862 frame_T *frp;
2863 int extra_cols;
2864 int w;
2865 win_T *wp;
2867 if (topfrp->fr_layout == FR_LEAF)
2869 /* Simple case: just one window. */
2870 wp = topfrp->fr_win;
2871 /* Find out if there are any windows right of this one. */
2872 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2873 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2874 break;
2875 if (frp->fr_parent == NULL)
2876 wp->w_vsep_width = 0;
2877 win_new_width(wp, width - wp->w_vsep_width);
2879 else if (topfrp->fr_layout == FR_COL)
2883 /* All frames in this column get the same new width. */
2884 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2886 frame_new_width(frp, width, leftfirst, wfw);
2887 if (frp->fr_width > width)
2889 /* Could not fit the windows, make whole column wider. */
2890 width = frp->fr_width;
2891 break;
2894 } while (frp != NULL);
2896 else /* fr_layout == FR_ROW */
2898 /* Complicated case: Resize a row of frames. Resize the rightmost
2899 * frame first, frames left of it when needed. */
2901 frp = topfrp->fr_child;
2902 if (wfw)
2903 /* Advance past frames with one window with 'wfw' set. */
2904 while (frame_fixed_width(frp))
2906 frp = frp->fr_next;
2907 if (frp == NULL)
2908 return; /* no frame without 'wfw', give up */
2910 if (!leftfirst)
2912 /* Find the rightmost frame of this row */
2913 while (frp->fr_next != NULL)
2914 frp = frp->fr_next;
2915 if (wfw)
2916 /* Advance back for frames with one window with 'wfw' set. */
2917 while (frame_fixed_width(frp))
2918 frp = frp->fr_prev;
2921 extra_cols = width - topfrp->fr_width;
2922 if (extra_cols < 0)
2924 /* reduce frame width, rightmost frame first */
2925 while (frp != NULL)
2927 w = frame_minwidth(frp, NULL);
2928 if (frp->fr_width + extra_cols < w)
2930 extra_cols += frp->fr_width - w;
2931 frame_new_width(frp, w, leftfirst, wfw);
2933 else
2935 frame_new_width(frp, frp->fr_width + extra_cols,
2936 leftfirst, wfw);
2937 break;
2939 if (leftfirst)
2942 frp = frp->fr_next;
2943 while (wfw && frp != NULL && frame_fixed_width(frp));
2945 else
2948 frp = frp->fr_prev;
2949 while (wfw && frp != NULL && frame_fixed_width(frp));
2951 /* Increase "width" if we could not reduce enough frames. */
2952 if (frp == NULL)
2953 width -= extra_cols;
2956 else if (extra_cols > 0)
2958 /* increase width of rightmost frame */
2959 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2962 topfrp->fr_width = width;
2966 * Add the vertical separator to windows at the right side of "frp".
2967 * Note: Does not check if there is room!
2969 static void
2970 frame_add_vsep(frp)
2971 frame_T *frp;
2973 win_T *wp;
2975 if (frp->fr_layout == FR_LEAF)
2977 wp = frp->fr_win;
2978 if (wp->w_vsep_width == 0)
2980 if (wp->w_width > 0) /* don't make it negative */
2981 --wp->w_width;
2982 wp->w_vsep_width = 1;
2985 else if (frp->fr_layout == FR_COL)
2987 /* Handle all the frames in the column. */
2988 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2989 frame_add_vsep(frp);
2991 else /* frp->fr_layout == FR_ROW */
2993 /* Only need to handle the last frame in the row. */
2994 frp = frp->fr_child;
2995 while (frp->fr_next != NULL)
2996 frp = frp->fr_next;
2997 frame_add_vsep(frp);
3002 * Set frame width from the window it contains.
3004 static void
3005 frame_fix_width(wp)
3006 win_T *wp;
3008 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3010 #endif
3013 * Set frame height from the window it contains.
3015 static void
3016 frame_fix_height(wp)
3017 win_T *wp;
3019 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3023 * Compute the minimal height for frame "topfrp".
3024 * Uses the 'winminheight' option.
3025 * When "next_curwin" isn't NULL, use p_wh for this window.
3026 * When "next_curwin" is NOWIN, don't use at least one line for the current
3027 * window.
3029 static int
3030 frame_minheight(topfrp, next_curwin)
3031 frame_T *topfrp;
3032 win_T *next_curwin;
3034 frame_T *frp;
3035 int m;
3036 #ifdef FEAT_VERTSPLIT
3037 int n;
3038 #endif
3040 if (topfrp->fr_win != NULL)
3042 if (topfrp->fr_win == next_curwin)
3043 m = p_wh + topfrp->fr_win->w_status_height;
3044 else
3046 /* window: minimal height of the window plus status line */
3047 m = p_wmh + topfrp->fr_win->w_status_height;
3048 /* Current window is minimal one line high */
3049 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3050 ++m;
3053 #ifdef FEAT_VERTSPLIT
3054 else if (topfrp->fr_layout == FR_ROW)
3056 /* get the minimal height from each frame in this row */
3057 m = 0;
3058 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3060 n = frame_minheight(frp, next_curwin);
3061 if (n > m)
3062 m = n;
3065 #endif
3066 else
3068 /* Add up the minimal heights for all frames in this column. */
3069 m = 0;
3070 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3071 m += frame_minheight(frp, next_curwin);
3074 return m;
3077 #ifdef FEAT_VERTSPLIT
3079 * Compute the minimal width for frame "topfrp".
3080 * When "next_curwin" isn't NULL, use p_wiw for this window.
3081 * When "next_curwin" is NOWIN, don't use at least one column for the current
3082 * window.
3084 static int
3085 frame_minwidth(topfrp, next_curwin)
3086 frame_T *topfrp;
3087 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3089 frame_T *frp;
3090 int m, n;
3092 if (topfrp->fr_win != NULL)
3094 if (topfrp->fr_win == next_curwin)
3095 m = p_wiw + topfrp->fr_win->w_vsep_width;
3096 else
3098 /* window: minimal width of the window plus separator column */
3099 m = p_wmw + topfrp->fr_win->w_vsep_width;
3100 /* Current window is minimal one column wide */
3101 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3102 ++m;
3105 else if (topfrp->fr_layout == FR_COL)
3107 /* get the minimal width from each frame in this column */
3108 m = 0;
3109 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3111 n = frame_minwidth(frp, next_curwin);
3112 if (n > m)
3113 m = n;
3116 else
3118 /* Add up the minimal widths for all frames in this row. */
3119 m = 0;
3120 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3121 m += frame_minwidth(frp, next_curwin);
3124 return m;
3126 #endif
3130 * Try to close all windows except current one.
3131 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3132 * used and the buffer was modified.
3134 * Used by ":bdel" and ":only".
3136 void
3137 close_others(message, forceit)
3138 int message;
3139 int forceit; /* always hide all other windows */
3141 win_T *wp;
3142 win_T *nextwp;
3143 int r;
3145 if (one_window())
3147 if (message
3148 #ifdef FEAT_AUTOCMD
3149 && !autocmd_busy
3150 #endif
3152 MSG(_(m_onlyone));
3153 return;
3156 /* Be very careful here: autocommands may change the window layout. */
3157 for (wp = firstwin; win_valid(wp); wp = nextwp)
3159 nextwp = wp->w_next;
3160 if (wp != curwin) /* don't close current window */
3163 /* Check if it's allowed to abandon this window */
3164 r = can_abandon(wp->w_buffer, forceit);
3165 #ifdef FEAT_AUTOCMD
3166 if (!win_valid(wp)) /* autocommands messed wp up */
3168 nextwp = firstwin;
3169 continue;
3171 #endif
3172 if (!r)
3174 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3175 if (message && (p_confirm || cmdmod.confirm) && p_write)
3177 dialog_changed(wp->w_buffer, FALSE);
3178 # ifdef FEAT_AUTOCMD
3179 if (!win_valid(wp)) /* autocommands messed wp up */
3181 nextwp = firstwin;
3182 continue;
3184 # endif
3186 if (bufIsChanged(wp->w_buffer))
3187 #endif
3188 continue;
3190 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3194 if (message && lastwin != firstwin)
3195 EMSG(_("E445: Other window contains changes"));
3198 #endif /* FEAT_WINDOWS */
3201 * Init the current window "curwin".
3202 * Called when a new file is being edited.
3204 void
3205 curwin_init()
3207 redraw_win_later(curwin, NOT_VALID);
3208 curwin->w_lines_valid = 0;
3209 curwin->w_cursor.lnum = 1;
3210 curwin->w_curswant = curwin->w_cursor.col = 0;
3211 #ifdef FEAT_VIRTUALEDIT
3212 curwin->w_cursor.coladd = 0;
3213 #endif
3214 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3215 curwin->w_pcmark.col = 0;
3216 curwin->w_prev_pcmark.lnum = 0;
3217 curwin->w_prev_pcmark.col = 0;
3218 curwin->w_topline = 1;
3219 #ifdef FEAT_DIFF
3220 curwin->w_topfill = 0;
3221 #endif
3222 curwin->w_botline = 2;
3223 #ifdef FEAT_FKMAP
3224 if (curwin->w_p_rl)
3225 curwin->w_farsi = W_CONV + W_R_L;
3226 else
3227 curwin->w_farsi = W_CONV;
3228 #endif
3232 * Allocate the first window and put an empty buffer in it.
3233 * Called from main().
3234 * Return FAIL when something goes wrong (out of memory).
3237 win_alloc_first()
3239 if (win_alloc_firstwin(NULL) == FAIL)
3240 return FAIL;
3242 #ifdef FEAT_WINDOWS
3243 first_tabpage = alloc_tabpage();
3244 if (first_tabpage == NULL)
3245 return FAIL;
3246 first_tabpage->tp_topframe = topframe;
3247 curtab = first_tabpage;
3248 #endif
3250 return OK;
3253 #if defined(FEAT_AUTOCMD) || defined(PROTO)
3255 * Init "aucmd_win". This can only be done after the first
3256 * window is fully initialized, thus it can't be in win_alloc_first().
3258 void
3259 win_alloc_aucmd_win()
3261 aucmd_win = win_alloc(NULL, TRUE);
3262 if (aucmd_win != NULL)
3264 win_init_some(aucmd_win, curwin);
3265 # ifdef FEAT_SCROLLBIND
3266 aucmd_win->w_p_scb = FALSE;
3267 # endif
3268 new_frame(aucmd_win);
3271 #endif
3274 * Allocate the first window or the first window in a new tab page.
3275 * When "oldwin" is NULL create an empty buffer for it.
3276 * When "oldwin" is not NULL copy info from it to the new window (only with
3277 * FEAT_WINDOWS).
3278 * Return FAIL when something goes wrong (out of memory).
3280 static int
3281 win_alloc_firstwin(oldwin)
3282 win_T *oldwin;
3284 curwin = win_alloc(NULL, FALSE);
3285 if (oldwin == NULL)
3287 /* Very first window, need to create an empty buffer for it and
3288 * initialize from scratch. */
3289 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3290 if (curwin == NULL || curbuf == NULL)
3291 return FAIL;
3292 curwin->w_buffer = curbuf;
3293 curbuf->b_nwindows = 1; /* there is one window */
3294 #ifdef FEAT_WINDOWS
3295 curwin->w_alist = &global_alist;
3296 #endif
3297 curwin_init(); /* init current window */
3299 #ifdef FEAT_WINDOWS
3300 else
3302 /* First window in new tab page, initialize it from "oldwin". */
3303 win_init(curwin, oldwin, 0);
3305 # ifdef FEAT_SCROLLBIND
3306 /* We don't want scroll-binding in the first window. */
3307 curwin->w_p_scb = FALSE;
3308 # endif
3310 #endif
3312 new_frame(curwin);
3313 if (curwin->w_frame == NULL)
3314 return FAIL;
3315 topframe = curwin->w_frame;
3316 #ifdef FEAT_VERTSPLIT
3317 topframe->fr_width = Columns;
3318 #endif
3319 topframe->fr_height = Rows - p_ch;
3320 topframe->fr_win = curwin;
3322 return OK;
3326 * Create a frame for window "wp".
3328 static void
3329 new_frame(win_T *wp)
3331 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3333 wp->w_frame = frp;
3334 if (frp != NULL)
3336 frp->fr_layout = FR_LEAF;
3337 frp->fr_win = wp;
3342 * Initialize the window and frame size to the maximum.
3344 void
3345 win_init_size()
3347 firstwin->w_height = ROWS_AVAIL;
3348 topframe->fr_height = ROWS_AVAIL;
3349 #ifdef FEAT_VERTSPLIT
3350 firstwin->w_width = Columns;
3351 topframe->fr_width = Columns;
3352 #endif
3355 #if defined(FEAT_WINDOWS) || defined(PROTO)
3358 * Allocate a new tabpage_T and init the values.
3359 * Returns NULL when out of memory.
3361 static tabpage_T *
3362 alloc_tabpage()
3364 tabpage_T *tp;
3366 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3367 if (tp != NULL)
3369 # ifdef FEAT_GUI
3370 int i;
3372 for (i = 0; i < 3; i++)
3373 tp->tp_prev_which_scrollbars[i] = -1;
3374 # endif
3375 # ifdef FEAT_DIFF
3376 tp->tp_diff_invalid = TRUE;
3377 # endif
3378 #ifdef FEAT_EVAL
3379 /* init t: variables */
3380 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3381 #endif
3382 tp->tp_ch_used = p_ch;
3384 return tp;
3387 void
3388 free_tabpage(tp)
3389 tabpage_T *tp;
3391 int idx;
3393 # ifdef FEAT_DIFF
3394 diff_clear(tp);
3395 # endif
3396 for (idx = 0; idx < SNAP_COUNT; ++idx)
3397 clear_snapshot(tp, idx);
3398 #ifdef FEAT_EVAL
3399 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3400 #endif
3401 vim_free(tp);
3405 * Create a new Tab page with one window.
3406 * It will edit the current buffer, like after ":split".
3407 * When "after" is 0 put it just after the current Tab page.
3408 * Otherwise put it just before tab page "after".
3409 * Return FAIL or OK.
3412 win_new_tabpage(after)
3413 int after;
3415 tabpage_T *tp = curtab;
3416 tabpage_T *newtp;
3417 int n;
3419 newtp = alloc_tabpage();
3420 if (newtp == NULL)
3421 return FAIL;
3423 /* Remember the current windows in this Tab page. */
3424 if (leave_tabpage(curbuf) == FAIL)
3426 vim_free(newtp);
3427 return FAIL;
3429 curtab = newtp;
3431 /* Create a new empty window. */
3432 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3434 /* Make the new Tab page the new topframe. */
3435 if (after == 1)
3437 /* New tab page becomes the first one. */
3438 newtp->tp_next = first_tabpage;
3439 first_tabpage = newtp;
3441 else
3443 if (after > 0)
3445 /* Put new tab page before tab page "after". */
3446 n = 2;
3447 for (tp = first_tabpage; tp->tp_next != NULL
3448 && n < after; tp = tp->tp_next)
3449 ++n;
3451 newtp->tp_next = tp->tp_next;
3452 tp->tp_next = newtp;
3454 win_init_size();
3455 firstwin->w_winrow = tabline_height();
3456 win_comp_scroll(curwin);
3458 newtp->tp_topframe = topframe;
3459 last_status(FALSE);
3461 #if defined(FEAT_GUI)
3462 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3463 * scrollbars. Have to update them anyway. */
3464 gui_may_update_scrollbars();
3465 #endif
3467 redraw_all_later(CLEAR);
3468 #ifdef FEAT_AUTOCMD
3469 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3470 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3471 #endif
3472 return OK;
3475 /* Failed, get back the previous Tab page */
3476 enter_tabpage(curtab, curbuf);
3477 return FAIL;
3481 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3482 * like with ":split".
3483 * Returns OK if a new tab page was created, FAIL otherwise.
3486 may_open_tabpage()
3488 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3490 if (n != 0)
3492 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3493 postponed_split_tab = 0;
3494 return win_new_tabpage(n);
3496 return FAIL;
3500 * Create up to "maxcount" tabpages with empty windows.
3501 * Returns the number of resulting tab pages.
3504 make_tabpages(maxcount)
3505 int maxcount;
3507 int count = maxcount;
3508 int todo;
3510 /* Limit to 'tabpagemax' tabs. */
3511 if (count > p_tpm)
3512 count = p_tpm;
3514 #ifdef FEAT_AUTOCMD
3516 * Don't execute autocommands while creating the tab pages. Must do that
3517 * when putting the buffers in the windows.
3519 block_autocmds();
3520 #endif
3522 for (todo = count - 1; todo > 0; --todo)
3523 if (win_new_tabpage(0) == FAIL)
3524 break;
3526 #ifdef FEAT_AUTOCMD
3527 unblock_autocmds();
3528 #endif
3530 /* return actual number of tab pages */
3531 return (count - todo);
3535 * Return TRUE when "tpc" points to a valid tab page.
3538 valid_tabpage(tpc)
3539 tabpage_T *tpc;
3541 tabpage_T *tp;
3543 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3544 if (tp == tpc)
3545 return TRUE;
3546 return FALSE;
3550 * Find tab page "n" (first one is 1). Returns NULL when not found.
3552 tabpage_T *
3553 find_tabpage(n)
3554 int n;
3556 tabpage_T *tp;
3557 int i = 1;
3559 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3560 ++i;
3561 return tp;
3565 * Get index of tab page "tp". First one has index 1.
3566 * When not found returns number of tab pages plus one.
3569 tabpage_index(ftp)
3570 tabpage_T *ftp;
3572 int i = 1;
3573 tabpage_T *tp;
3575 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3576 ++i;
3577 return i;
3581 * Prepare for leaving the current tab page.
3582 * When autocomands change "curtab" we don't leave the tab page and return
3583 * FAIL.
3584 * Careful: When OK is returned need to get a new tab page very very soon!
3586 static int
3587 leave_tabpage(new_curbuf)
3588 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
3589 NULL if unknown */
3591 tabpage_T *tp = curtab;
3593 #ifdef FEAT_VISUAL
3594 reset_VIsual_and_resel(); /* stop Visual mode */
3595 #endif
3596 #ifdef FEAT_AUTOCMD
3597 if (new_curbuf != curbuf)
3599 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3600 if (curtab != tp)
3601 return FAIL;
3603 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3604 if (curtab != tp)
3605 return FAIL;
3606 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3607 if (curtab != tp)
3608 return FAIL;
3609 #endif
3610 #if defined(FEAT_GUI)
3611 /* Remove the scrollbars. They may be added back later. */
3612 if (gui.in_use)
3613 gui_remove_scrollbars();
3614 #endif
3615 tp->tp_curwin = curwin;
3616 tp->tp_prevwin = prevwin;
3617 tp->tp_firstwin = firstwin;
3618 tp->tp_lastwin = lastwin;
3619 tp->tp_old_Rows = Rows;
3620 tp->tp_old_Columns = Columns;
3621 firstwin = NULL;
3622 lastwin = NULL;
3623 return OK;
3627 * Start using tab page "tp".
3628 * Only to be used after leave_tabpage() or freeing the current tab page.
3630 static void
3631 enter_tabpage(tp, old_curbuf)
3632 tabpage_T *tp;
3633 buf_T *old_curbuf UNUSED;
3635 int old_off = tp->tp_firstwin->w_winrow;
3636 win_T *next_prevwin = tp->tp_prevwin;
3638 curtab = tp;
3639 firstwin = tp->tp_firstwin;
3640 lastwin = tp->tp_lastwin;
3641 topframe = tp->tp_topframe;
3643 /* We would like doing the TabEnter event first, but we don't have a
3644 * valid current window yet, which may break some commands.
3645 * This triggers autocommands, thus may make "tp" invalid. */
3646 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3647 prevwin = next_prevwin;
3649 #ifdef FEAT_AUTOCMD
3650 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3652 if (old_curbuf != curbuf)
3653 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3654 #endif
3656 last_status(FALSE); /* status line may appear or disappear */
3657 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3658 must_redraw = CLEAR; /* need to redraw everything */
3659 #ifdef FEAT_DIFF
3660 diff_need_scrollbind = TRUE;
3661 #endif
3663 /* The tabpage line may have appeared or disappeared, may need to resize
3664 * the frames for that. When the Vim window was resized need to update
3665 * frame sizes too. Use the stored value of p_ch, so that it can be
3666 * different for each tab page. */
3667 p_ch = curtab->tp_ch_used;
3668 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3669 #ifdef FEAT_GUI_TABLINE
3670 && !gui_use_tabline()
3671 #endif
3673 shell_new_rows();
3674 #ifdef FEAT_VERTSPLIT
3675 if (curtab->tp_old_Columns != Columns && starting == 0)
3676 shell_new_columns(); /* update window widths */
3677 #endif
3679 #if defined(FEAT_GUI)
3680 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3681 * scrollbars. Have to update them anyway. */
3682 gui_may_update_scrollbars();
3683 #endif
3685 redraw_all_later(CLEAR);
3689 * Go to tab page "n". For ":tab N" and "Ngt".
3690 * When "n" is 9999 go to the last tab page.
3692 void
3693 goto_tabpage(n)
3694 int n;
3696 tabpage_T *tp;
3697 tabpage_T *ttp;
3698 int i;
3700 if (text_locked())
3702 /* Not allowed when editing the command line. */
3703 #ifdef FEAT_CMDWIN
3704 if (cmdwin_type != 0)
3705 EMSG(_(e_cmdwin));
3706 else
3707 #endif
3708 EMSG(_(e_secure));
3709 return;
3712 /* If there is only one it can't work. */
3713 if (first_tabpage->tp_next == NULL)
3715 if (n > 1)
3716 beep_flush();
3717 return;
3720 if (n == 0)
3722 /* No count, go to next tab page, wrap around end. */
3723 if (curtab->tp_next == NULL)
3724 tp = first_tabpage;
3725 else
3726 tp = curtab->tp_next;
3728 else if (n < 0)
3730 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3731 * this N times. */
3732 ttp = curtab;
3733 for (i = n; i < 0; ++i)
3735 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3736 tp = tp->tp_next)
3738 ttp = tp;
3741 else if (n == 9999)
3743 /* Go to last tab page. */
3744 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3747 else
3749 /* Go to tab page "n". */
3750 tp = find_tabpage(n);
3751 if (tp == NULL)
3753 beep_flush();
3754 return;
3758 goto_tabpage_tp(tp);
3760 #ifdef FEAT_GUI_TABLINE
3761 if (gui_use_tabline())
3762 gui_mch_set_curtab(tabpage_index(curtab));
3763 #endif
3767 * Go to tabpage "tp".
3768 * Note: doesn't update the GUI tab.
3770 void
3771 goto_tabpage_tp(tp)
3772 tabpage_T *tp;
3774 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3776 if (valid_tabpage(tp))
3777 enter_tabpage(tp, curbuf);
3778 else
3779 enter_tabpage(curtab, curbuf);
3784 * Enter window "wp" in tab page "tp".
3785 * Also updates the GUI tab.
3787 void
3788 goto_tabpage_win(tp, wp)
3789 tabpage_T *tp;
3790 win_T *wp;
3792 goto_tabpage_tp(tp);
3793 if (curtab == tp && win_valid(wp))
3795 win_enter(wp, TRUE);
3796 # ifdef FEAT_GUI_TABLINE
3797 if (gui_use_tabline())
3798 gui_mch_set_curtab(tabpage_index(curtab));
3799 # endif
3804 * Move the current tab page to before tab page "nr".
3806 void
3807 tabpage_move(nr)
3808 int nr;
3810 int n = nr;
3811 tabpage_T *tp;
3813 if (first_tabpage->tp_next == NULL)
3814 return;
3816 /* Remove the current tab page from the list of tab pages. */
3817 if (curtab == first_tabpage)
3818 first_tabpage = curtab->tp_next;
3819 else
3821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3822 if (tp->tp_next == curtab)
3823 break;
3824 if (tp == NULL) /* "cannot happen" */
3825 return;
3826 tp->tp_next = curtab->tp_next;
3829 /* Re-insert it at the specified position. */
3830 if (n == 0)
3832 curtab->tp_next = first_tabpage;
3833 first_tabpage = curtab;
3835 else
3837 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3838 --n;
3839 curtab->tp_next = tp->tp_next;
3840 tp->tp_next = curtab;
3843 /* Need to redraw the tabline. Tab page contents doesn't change. */
3844 redraw_tabline = TRUE;
3849 * Go to another window.
3850 * When jumping to another buffer, stop Visual mode. Do this before
3851 * changing windows so we can yank the selection into the '*' register.
3852 * When jumping to another window on the same buffer, adjust its cursor
3853 * position to keep the same Visual area.
3855 void
3856 win_goto(wp)
3857 win_T *wp;
3859 if (text_locked())
3861 beep_flush();
3862 text_locked_msg();
3863 return;
3865 #ifdef FEAT_AUTOCMD
3866 if (curbuf_locked())
3867 return;
3868 #endif
3870 #ifdef FEAT_VISUAL
3871 if (wp->w_buffer != curbuf)
3872 reset_VIsual_and_resel();
3873 else if (VIsual_active)
3874 wp->w_cursor = curwin->w_cursor;
3875 #endif
3877 #ifdef FEAT_GUI
3878 need_mouse_correct = TRUE;
3879 #endif
3880 win_enter(wp, TRUE);
3883 #if defined(FEAT_PERL) || defined(PROTO)
3885 * Find window number "winnr" (counting top to bottom).
3887 win_T *
3888 win_find_nr(winnr)
3889 int winnr;
3891 win_T *wp;
3893 # ifdef FEAT_WINDOWS
3894 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3895 if (--winnr == 0)
3896 break;
3897 return wp;
3898 # else
3899 return curwin;
3900 # endif
3902 #endif
3904 #ifdef FEAT_VERTSPLIT
3906 * Move to window above or below "count" times.
3908 static void
3909 win_goto_ver(up, count)
3910 int up; /* TRUE to go to win above */
3911 long count;
3913 frame_T *fr;
3914 frame_T *nfr;
3915 frame_T *foundfr;
3917 foundfr = curwin->w_frame;
3918 while (count--)
3921 * First go upwards in the tree of frames until we find a upwards or
3922 * downwards neighbor.
3924 fr = foundfr;
3925 for (;;)
3927 if (fr == topframe)
3928 goto end;
3929 if (up)
3930 nfr = fr->fr_prev;
3931 else
3932 nfr = fr->fr_next;
3933 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3934 break;
3935 fr = fr->fr_parent;
3939 * Now go downwards to find the bottom or top frame in it.
3941 for (;;)
3943 if (nfr->fr_layout == FR_LEAF)
3945 foundfr = nfr;
3946 break;
3948 fr = nfr->fr_child;
3949 if (nfr->fr_layout == FR_ROW)
3951 /* Find the frame at the cursor row. */
3952 while (fr->fr_next != NULL
3953 && frame2win(fr)->w_wincol + fr->fr_width
3954 <= curwin->w_wincol + curwin->w_wcol)
3955 fr = fr->fr_next;
3957 if (nfr->fr_layout == FR_COL && up)
3958 while (fr->fr_next != NULL)
3959 fr = fr->fr_next;
3960 nfr = fr;
3963 end:
3964 if (foundfr != NULL)
3965 win_goto(foundfr->fr_win);
3969 * Move to left or right window.
3971 static void
3972 win_goto_hor(left, count)
3973 int left; /* TRUE to go to left win */
3974 long count;
3976 frame_T *fr;
3977 frame_T *nfr;
3978 frame_T *foundfr;
3980 foundfr = curwin->w_frame;
3981 while (count--)
3984 * First go upwards in the tree of frames until we find a left or
3985 * right neighbor.
3987 fr = foundfr;
3988 for (;;)
3990 if (fr == topframe)
3991 goto end;
3992 if (left)
3993 nfr = fr->fr_prev;
3994 else
3995 nfr = fr->fr_next;
3996 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3997 break;
3998 fr = fr->fr_parent;
4002 * Now go downwards to find the leftmost or rightmost frame in it.
4004 for (;;)
4006 if (nfr->fr_layout == FR_LEAF)
4008 foundfr = nfr;
4009 break;
4011 fr = nfr->fr_child;
4012 if (nfr->fr_layout == FR_COL)
4014 /* Find the frame at the cursor row. */
4015 while (fr->fr_next != NULL
4016 && frame2win(fr)->w_winrow + fr->fr_height
4017 <= curwin->w_winrow + curwin->w_wrow)
4018 fr = fr->fr_next;
4020 if (nfr->fr_layout == FR_ROW && left)
4021 while (fr->fr_next != NULL)
4022 fr = fr->fr_next;
4023 nfr = fr;
4026 end:
4027 if (foundfr != NULL)
4028 win_goto(foundfr->fr_win);
4030 #endif
4033 * Make window "wp" the current window.
4035 void
4036 win_enter(wp, undo_sync)
4037 win_T *wp;
4038 int undo_sync;
4040 win_enter_ext(wp, undo_sync, FALSE);
4044 * Make window wp the current window.
4045 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4046 * been closed and isn't valid.
4048 static void
4049 win_enter_ext(wp, undo_sync, curwin_invalid)
4050 win_T *wp;
4051 int undo_sync;
4052 int curwin_invalid;
4054 #ifdef FEAT_AUTOCMD
4055 int other_buffer = FALSE;
4056 #endif
4058 if (wp == curwin && !curwin_invalid) /* nothing to do */
4059 return;
4061 #ifdef FEAT_AUTOCMD
4062 if (!curwin_invalid)
4065 * Be careful: If autocommands delete the window, return now.
4067 if (wp->w_buffer != curbuf)
4069 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4070 other_buffer = TRUE;
4071 if (!win_valid(wp))
4072 return;
4074 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4075 if (!win_valid(wp))
4076 return;
4077 # ifdef FEAT_EVAL
4078 /* autocmds may abort script processing */
4079 if (aborting())
4080 return;
4081 # endif
4083 #endif
4085 /* sync undo before leaving the current buffer */
4086 if (undo_sync && curbuf != wp->w_buffer)
4087 u_sync(FALSE);
4088 /* may have to copy the buffer options when 'cpo' contains 'S' */
4089 if (wp->w_buffer != curbuf)
4090 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4091 if (!curwin_invalid)
4093 prevwin = curwin; /* remember for CTRL-W p */
4094 curwin->w_redr_status = TRUE;
4096 curwin = wp;
4097 curbuf = wp->w_buffer;
4098 check_cursor();
4099 #ifdef FEAT_VIRTUALEDIT
4100 if (!virtual_active())
4101 curwin->w_cursor.coladd = 0;
4102 #endif
4103 changed_line_abv_curs(); /* assume cursor position needs updating */
4105 if (curwin->w_localdir != NULL)
4107 /* Window has a local directory: Save current directory as global
4108 * directory (unless that was done already) and change to the local
4109 * directory. */
4110 if (globaldir == NULL)
4112 char_u cwd[MAXPATHL];
4114 if (mch_dirname(cwd, MAXPATHL) == OK)
4115 globaldir = vim_strsave(cwd);
4117 if (mch_chdir((char *)curwin->w_localdir) == 0)
4118 shorten_fnames(TRUE);
4120 else if (globaldir != NULL)
4122 /* Window doesn't have a local directory and we are not in the global
4123 * directory: Change to the global directory. */
4124 ignored = mch_chdir((char *)globaldir);
4125 vim_free(globaldir);
4126 globaldir = NULL;
4127 shorten_fnames(TRUE);
4130 #ifdef FEAT_AUTOCMD
4131 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4132 if (other_buffer)
4133 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4134 #endif
4136 #ifdef FEAT_TITLE
4137 maketitle();
4138 #endif
4139 curwin->w_redr_status = TRUE;
4140 redraw_tabline = TRUE;
4141 if (restart_edit)
4142 redraw_later(VALID); /* causes status line redraw */
4144 /* set window height to desired minimal value */
4145 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4146 win_setheight((int)p_wh);
4147 else if (curwin->w_height == 0)
4148 win_setheight(1);
4150 #ifdef FEAT_VERTSPLIT
4151 /* set window width to desired minimal value */
4152 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4153 win_setwidth((int)p_wiw);
4154 #endif
4156 #ifdef FEAT_MOUSE
4157 setmouse(); /* in case jumped to/from help buffer */
4158 #endif
4160 /* Change directories when the 'acd' option is set. */
4161 DO_AUTOCHDIR
4164 #endif /* FEAT_WINDOWS */
4166 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4168 * Jump to the first open window that contains buffer "buf", if one exists.
4169 * Returns a pointer to the window found, otherwise NULL.
4171 win_T *
4172 buf_jump_open_win(buf)
4173 buf_T *buf;
4175 # ifdef FEAT_WINDOWS
4176 win_T *wp;
4178 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4179 if (wp->w_buffer == buf)
4180 break;
4181 if (wp != NULL)
4182 win_enter(wp, FALSE);
4183 return wp;
4184 # else
4185 if (curwin->w_buffer == buf)
4186 return curwin;
4187 return NULL;
4188 # endif
4192 * Jump to the first open window in any tab page that contains buffer "buf",
4193 * if one exists.
4194 * Returns a pointer to the window found, otherwise NULL.
4196 win_T *
4197 buf_jump_open_tab(buf)
4198 buf_T *buf;
4200 # ifdef FEAT_WINDOWS
4201 win_T *wp;
4202 tabpage_T *tp;
4204 /* First try the current tab page. */
4205 wp = buf_jump_open_win(buf);
4206 if (wp != NULL)
4207 return wp;
4209 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4210 if (tp != curtab)
4212 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4213 if (wp->w_buffer == buf)
4214 break;
4215 if (wp != NULL)
4217 goto_tabpage_win(tp, wp);
4218 if (curwin != wp)
4219 wp = NULL; /* something went wrong */
4220 break;
4224 return wp;
4225 # else
4226 if (curwin->w_buffer == buf)
4227 return curwin;
4228 return NULL;
4229 # endif
4231 #endif
4234 * Allocate a window structure and link it in the window list when "hidden" is
4235 * FALSE.
4237 static win_T *
4238 win_alloc(after, hidden)
4239 win_T *after UNUSED;
4240 int hidden UNUSED;
4242 win_T *newwin;
4245 * allocate window structure and linesizes arrays
4247 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4248 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4250 vim_free(newwin);
4251 newwin = NULL;
4254 if (newwin != NULL)
4256 #ifdef FEAT_AUTOCMD
4257 /* Don't execute autocommands while the window is not properly
4258 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4259 * event. */
4260 block_autocmds();
4261 #endif
4263 * link the window in the window list
4265 #ifdef FEAT_WINDOWS
4266 if (!hidden)
4267 win_append(after, newwin);
4268 #endif
4269 #ifdef FEAT_VERTSPLIT
4270 newwin->w_wincol = 0;
4271 newwin->w_width = Columns;
4272 #endif
4274 /* position the display and the cursor at the top of the file. */
4275 newwin->w_topline = 1;
4276 #ifdef FEAT_DIFF
4277 newwin->w_topfill = 0;
4278 #endif
4279 newwin->w_botline = 2;
4280 newwin->w_cursor.lnum = 1;
4281 #ifdef FEAT_SCROLLBIND
4282 newwin->w_scbind_pos = 1;
4283 #endif
4285 /* We won't calculate w_fraction until resizing the window */
4286 newwin->w_fraction = 0;
4287 newwin->w_prev_fraction_row = -1;
4289 #ifdef FEAT_GUI
4290 if (gui.in_use)
4292 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4293 SBAR_LEFT, newwin);
4294 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4295 SBAR_RIGHT, newwin);
4297 #endif
4298 #ifdef FEAT_EVAL
4299 /* init w: variables */
4300 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4301 #endif
4302 #ifdef FEAT_FOLDING
4303 foldInitWin(newwin);
4304 #endif
4305 #ifdef FEAT_AUTOCMD
4306 unblock_autocmds();
4307 #endif
4308 #ifdef FEAT_SEARCH_EXTRA
4309 newwin->w_match_head = NULL;
4310 newwin->w_next_match_id = 4;
4311 #endif
4313 return newwin;
4316 #if defined(FEAT_WINDOWS) || defined(PROTO)
4319 * remove window 'wp' from the window list and free the structure
4321 static void
4322 win_free(wp, tp)
4323 win_T *wp;
4324 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4326 int i;
4328 #ifdef FEAT_AUTOCMD
4329 /* Don't execute autocommands while the window is halfway being deleted.
4330 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4331 block_autocmds();
4332 #endif
4334 #ifdef FEAT_MZSCHEME
4335 mzscheme_window_free(wp);
4336 #endif
4338 #ifdef FEAT_PERL
4339 perl_win_free(wp);
4340 #endif
4342 #ifdef FEAT_PYTHON
4343 python_window_free(wp);
4344 #endif
4346 #ifdef FEAT_TCL
4347 tcl_window_free(wp);
4348 #endif
4350 #ifdef FEAT_RUBY
4351 ruby_window_free(wp);
4352 #endif
4354 clear_winopt(&wp->w_onebuf_opt);
4355 clear_winopt(&wp->w_allbuf_opt);
4357 #ifdef FEAT_EVAL
4358 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4359 #endif
4361 if (prevwin == wp)
4362 prevwin = NULL;
4363 win_free_lsize(wp);
4365 for (i = 0; i < wp->w_tagstacklen; ++i)
4366 vim_free(wp->w_tagstack[i].tagname);
4368 vim_free(wp->w_localdir);
4370 #ifdef FEAT_SEARCH_EXTRA
4371 clear_matches(wp);
4372 #endif
4374 #ifdef FEAT_JUMPLIST
4375 free_jumplist(wp);
4376 #endif
4378 #ifdef FEAT_QUICKFIX
4379 qf_free_all(wp);
4380 #endif
4382 #ifdef FEAT_GUI
4383 if (gui.in_use)
4385 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4386 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4388 #endif /* FEAT_GUI */
4390 win_remove(wp, tp);
4391 vim_free(wp);
4393 #ifdef FEAT_AUTOCMD
4394 unblock_autocmds();
4395 #endif
4399 * Append window "wp" in the window list after window "after".
4401 void
4402 win_append(after, wp)
4403 win_T *after, *wp;
4405 win_T *before;
4407 if (after == NULL) /* after NULL is in front of the first */
4408 before = firstwin;
4409 else
4410 before = after->w_next;
4412 wp->w_next = before;
4413 wp->w_prev = after;
4414 if (after == NULL)
4415 firstwin = wp;
4416 else
4417 after->w_next = wp;
4418 if (before == NULL)
4419 lastwin = wp;
4420 else
4421 before->w_prev = wp;
4425 * Remove a window from the window list.
4427 void
4428 win_remove(wp, tp)
4429 win_T *wp;
4430 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4432 if (wp->w_prev != NULL)
4433 wp->w_prev->w_next = wp->w_next;
4434 else if (tp == NULL)
4435 firstwin = wp->w_next;
4436 else
4437 tp->tp_firstwin = wp->w_next;
4438 if (wp->w_next != NULL)
4439 wp->w_next->w_prev = wp->w_prev;
4440 else if (tp == NULL)
4441 lastwin = wp->w_prev;
4442 else
4443 tp->tp_lastwin = wp->w_prev;
4447 * Append frame "frp" in a frame list after frame "after".
4449 static void
4450 frame_append(after, frp)
4451 frame_T *after, *frp;
4453 frp->fr_next = after->fr_next;
4454 after->fr_next = frp;
4455 if (frp->fr_next != NULL)
4456 frp->fr_next->fr_prev = frp;
4457 frp->fr_prev = after;
4461 * Insert frame "frp" in a frame list before frame "before".
4463 static void
4464 frame_insert(before, frp)
4465 frame_T *before, *frp;
4467 frp->fr_next = before;
4468 frp->fr_prev = before->fr_prev;
4469 before->fr_prev = frp;
4470 if (frp->fr_prev != NULL)
4471 frp->fr_prev->fr_next = frp;
4472 else
4473 frp->fr_parent->fr_child = frp;
4477 * Remove a frame from a frame list.
4479 static void
4480 frame_remove(frp)
4481 frame_T *frp;
4483 if (frp->fr_prev != NULL)
4484 frp->fr_prev->fr_next = frp->fr_next;
4485 else
4486 frp->fr_parent->fr_child = frp->fr_next;
4487 if (frp->fr_next != NULL)
4488 frp->fr_next->fr_prev = frp->fr_prev;
4491 #endif /* FEAT_WINDOWS */
4494 * Allocate w_lines[] for window "wp".
4495 * Return FAIL for failure, OK for success.
4498 win_alloc_lines(wp)
4499 win_T *wp;
4501 wp->w_lines_valid = 0;
4502 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4503 if (wp->w_lines == NULL)
4504 return FAIL;
4505 return OK;
4509 * free lsize arrays for a window
4511 void
4512 win_free_lsize(wp)
4513 win_T *wp;
4515 vim_free(wp->w_lines);
4516 wp->w_lines = NULL;
4520 * Called from win_new_shellsize() after Rows changed.
4521 * This only does the current tab page, others must be done when made active.
4523 void
4524 shell_new_rows()
4526 int h = (int)ROWS_AVAIL;
4528 if (firstwin == NULL) /* not initialized yet */
4529 return;
4530 #ifdef FEAT_WINDOWS
4531 if (h < frame_minheight(topframe, NULL))
4532 h = frame_minheight(topframe, NULL);
4534 /* First try setting the heights of windows with 'winfixheight'. If
4535 * that doesn't result in the right height, forget about that option. */
4536 frame_new_height(topframe, h, FALSE, TRUE);
4537 if (topframe->fr_height != h)
4538 frame_new_height(topframe, h, FALSE, FALSE);
4540 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4541 #else
4542 if (h < 1)
4543 h = 1;
4544 win_new_height(firstwin, h);
4545 #endif
4546 compute_cmdrow();
4547 #ifdef FEAT_WINDOWS
4548 curtab->tp_ch_used = p_ch;
4549 #endif
4551 #if 0
4552 /* Disabled: don't want making the screen smaller make a window larger. */
4553 if (p_ea)
4554 win_equal(curwin, FALSE, 'v');
4555 #endif
4558 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4560 * Called from win_new_shellsize() after Columns changed.
4562 void
4563 shell_new_columns()
4565 if (firstwin == NULL) /* not initialized yet */
4566 return;
4568 /* First try setting the widths of windows with 'winfixwidth'. If that
4569 * doesn't result in the right width, forget about that option. */
4570 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4571 if (topframe->fr_width != Columns)
4572 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4574 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4575 #if 0
4576 /* Disabled: don't want making the screen smaller make a window larger. */
4577 if (p_ea)
4578 win_equal(curwin, FALSE, 'h');
4579 #endif
4581 #endif
4583 #if defined(FEAT_CMDWIN) || defined(PROTO)
4585 * Save the size of all windows in "gap".
4587 void
4588 win_size_save(gap)
4589 garray_T *gap;
4592 win_T *wp;
4594 ga_init2(gap, (int)sizeof(int), 1);
4595 if (ga_grow(gap, win_count() * 2) == OK)
4596 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4598 ((int *)gap->ga_data)[gap->ga_len++] =
4599 wp->w_width + wp->w_vsep_width;
4600 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4605 * Restore window sizes, but only if the number of windows is still the same.
4606 * Does not free the growarray.
4608 void
4609 win_size_restore(gap)
4610 garray_T *gap;
4612 win_T *wp;
4613 int i;
4615 if (win_count() * 2 == gap->ga_len)
4617 i = 0;
4618 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4620 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4621 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4623 /* recompute the window positions */
4624 (void)win_comp_pos();
4627 #endif /* FEAT_CMDWIN */
4629 #if defined(FEAT_WINDOWS) || defined(PROTO)
4631 * Update the position for all windows, using the width and height of the
4632 * frames.
4633 * Returns the row just after the last window.
4636 win_comp_pos()
4638 int row = tabline_height();
4639 int col = 0;
4641 frame_comp_pos(topframe, &row, &col);
4642 return row;
4646 * Update the position of the windows in frame "topfrp", using the width and
4647 * height of the frames.
4648 * "*row" and "*col" are the top-left position of the frame. They are updated
4649 * to the bottom-right position plus one.
4651 static void
4652 frame_comp_pos(topfrp, row, col)
4653 frame_T *topfrp;
4654 int *row;
4655 int *col;
4657 win_T *wp;
4658 frame_T *frp;
4659 #ifdef FEAT_VERTSPLIT
4660 int startcol;
4661 int startrow;
4662 #endif
4664 wp = topfrp->fr_win;
4665 if (wp != NULL)
4667 if (wp->w_winrow != *row
4668 #ifdef FEAT_VERTSPLIT
4669 || wp->w_wincol != *col
4670 #endif
4673 /* position changed, redraw */
4674 wp->w_winrow = *row;
4675 #ifdef FEAT_VERTSPLIT
4676 wp->w_wincol = *col;
4677 #endif
4678 redraw_win_later(wp, NOT_VALID);
4679 wp->w_redr_status = TRUE;
4681 *row += wp->w_height + wp->w_status_height;
4682 #ifdef FEAT_VERTSPLIT
4683 *col += wp->w_width + wp->w_vsep_width;
4684 #endif
4686 else
4688 #ifdef FEAT_VERTSPLIT
4689 startrow = *row;
4690 startcol = *col;
4691 #endif
4692 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4694 #ifdef FEAT_VERTSPLIT
4695 if (topfrp->fr_layout == FR_ROW)
4696 *row = startrow; /* all frames are at the same row */
4697 else
4698 *col = startcol; /* all frames are at the same col */
4699 #endif
4700 frame_comp_pos(frp, row, col);
4705 #endif /* FEAT_WINDOWS */
4708 * Set current window height and take care of repositioning other windows to
4709 * fit around it.
4711 void
4712 win_setheight(height)
4713 int height;
4715 win_setheight_win(height, curwin);
4719 * Set the window height of window "win" and take care of repositioning other
4720 * windows to fit around it.
4722 void
4723 win_setheight_win(height, win)
4724 int height;
4725 win_T *win;
4727 int row;
4729 if (win == curwin)
4731 /* Always keep current window at least one line high, even when
4732 * 'winminheight' is zero. */
4733 #ifdef FEAT_WINDOWS
4734 if (height < p_wmh)
4735 height = p_wmh;
4736 #endif
4737 if (height == 0)
4738 height = 1;
4741 #ifdef FEAT_WINDOWS
4742 frame_setheight(win->w_frame, height + win->w_status_height);
4744 /* recompute the window positions */
4745 row = win_comp_pos();
4746 #else
4747 if (height > topframe->fr_height)
4748 height = topframe->fr_height;
4749 win->w_height = height;
4750 row = height;
4751 #endif
4754 * If there is extra space created between the last window and the command
4755 * line, clear it.
4757 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4758 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4759 cmdline_row = row;
4760 msg_row = row;
4761 msg_col = 0;
4763 redraw_all_later(NOT_VALID);
4766 #if defined(FEAT_WINDOWS) || defined(PROTO)
4769 * Set the height of a frame to "height" and take care that all frames and
4770 * windows inside it are resized. Also resize frames on the left and right if
4771 * the are in the same FR_ROW frame.
4773 * Strategy:
4774 * If the frame is part of a FR_COL frame, try fitting the frame in that
4775 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4776 * go to containing frames to resize them and make room.
4777 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4778 * Check for the minimal height of the FR_ROW frame.
4779 * At the top level we can also use change the command line height.
4781 static void
4782 frame_setheight(curfrp, height)
4783 frame_T *curfrp;
4784 int height;
4786 int room; /* total number of lines available */
4787 int take; /* number of lines taken from other windows */
4788 int room_cmdline; /* lines available from cmdline */
4789 int run;
4790 frame_T *frp;
4791 int h;
4792 int room_reserved;
4794 /* If the height already is the desired value, nothing to do. */
4795 if (curfrp->fr_height == height)
4796 return;
4798 if (curfrp->fr_parent == NULL)
4800 /* topframe: can only change the command line */
4801 if (height > ROWS_AVAIL)
4802 height = ROWS_AVAIL;
4803 if (height > 0)
4804 frame_new_height(curfrp, height, FALSE, FALSE);
4806 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4808 /* Row of frames: Also need to resize frames left and right of this
4809 * one. First check for the minimal height of these. */
4810 h = frame_minheight(curfrp->fr_parent, NULL);
4811 if (height < h)
4812 height = h;
4813 frame_setheight(curfrp->fr_parent, height);
4815 else
4818 * Column of frames: try to change only frames in this column.
4820 #ifdef FEAT_VERTSPLIT
4822 * Do this twice:
4823 * 1: compute room available, if it's not enough try resizing the
4824 * containing frame.
4825 * 2: compute the room available and adjust the height to it.
4826 * Try not to reduce the height of a window with 'winfixheight' set.
4828 for (run = 1; run <= 2; ++run)
4829 #else
4830 for (;;)
4831 #endif
4833 room = 0;
4834 room_reserved = 0;
4835 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4836 frp = frp->fr_next)
4838 if (frp != curfrp
4839 && frp->fr_win != NULL
4840 && frp->fr_win->w_p_wfh)
4841 room_reserved += frp->fr_height;
4842 room += frp->fr_height;
4843 if (frp != curfrp)
4844 room -= frame_minheight(frp, NULL);
4846 #ifdef FEAT_VERTSPLIT
4847 if (curfrp->fr_width != Columns)
4848 room_cmdline = 0;
4849 else
4850 #endif
4852 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4853 + lastwin->w_height + lastwin->w_status_height);
4854 if (room_cmdline < 0)
4855 room_cmdline = 0;
4858 if (height <= room + room_cmdline)
4859 break;
4860 #ifdef FEAT_VERTSPLIT
4861 if (run == 2 || curfrp->fr_width == Columns)
4862 #endif
4864 if (height > room + room_cmdline)
4865 height = room + room_cmdline;
4866 break;
4868 #ifdef FEAT_VERTSPLIT
4869 frame_setheight(curfrp->fr_parent, height
4870 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4871 #endif
4872 /*NOTREACHED*/
4876 * Compute the number of lines we will take from others frames (can be
4877 * negative!).
4879 take = height - curfrp->fr_height;
4881 /* If there is not enough room, also reduce the height of a window
4882 * with 'winfixheight' set. */
4883 if (height > room + room_cmdline - room_reserved)
4884 room_reserved = room + room_cmdline - height;
4885 /* If there is only a 'winfixheight' window and making the
4886 * window smaller, need to make the other window taller. */
4887 if (take < 0 && room - curfrp->fr_height < room_reserved)
4888 room_reserved = 0;
4890 if (take > 0 && room_cmdline > 0)
4892 /* use lines from cmdline first */
4893 if (take < room_cmdline)
4894 room_cmdline = take;
4895 take -= room_cmdline;
4896 topframe->fr_height += room_cmdline;
4900 * set the current frame to the new height
4902 frame_new_height(curfrp, height, FALSE, FALSE);
4905 * First take lines from the frames after the current frame. If
4906 * that is not enough, takes lines from frames above the current
4907 * frame.
4909 for (run = 0; run < 2; ++run)
4911 if (run == 0)
4912 frp = curfrp->fr_next; /* 1st run: start with next window */
4913 else
4914 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4915 while (frp != NULL && take != 0)
4917 h = frame_minheight(frp, NULL);
4918 if (room_reserved > 0
4919 && frp->fr_win != NULL
4920 && frp->fr_win->w_p_wfh)
4922 if (room_reserved >= frp->fr_height)
4923 room_reserved -= frp->fr_height;
4924 else
4926 if (frp->fr_height - room_reserved > take)
4927 room_reserved = frp->fr_height - take;
4928 take -= frp->fr_height - room_reserved;
4929 frame_new_height(frp, room_reserved, FALSE, FALSE);
4930 room_reserved = 0;
4933 else
4935 if (frp->fr_height - take < h)
4937 take -= frp->fr_height - h;
4938 frame_new_height(frp, h, FALSE, FALSE);
4940 else
4942 frame_new_height(frp, frp->fr_height - take,
4943 FALSE, FALSE);
4944 take = 0;
4947 if (run == 0)
4948 frp = frp->fr_next;
4949 else
4950 frp = frp->fr_prev;
4956 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4958 * Set current window width and take care of repositioning other windows to
4959 * fit around it.
4961 void
4962 win_setwidth(width)
4963 int width;
4965 win_setwidth_win(width, curwin);
4968 void
4969 win_setwidth_win(width, wp)
4970 int width;
4971 win_T *wp;
4973 /* Always keep current window at least one column wide, even when
4974 * 'winminwidth' is zero. */
4975 if (wp == curwin)
4977 if (width < p_wmw)
4978 width = p_wmw;
4979 if (width == 0)
4980 width = 1;
4983 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4985 /* recompute the window positions */
4986 (void)win_comp_pos();
4988 redraw_all_later(NOT_VALID);
4992 * Set the width of a frame to "width" and take care that all frames and
4993 * windows inside it are resized. Also resize frames above and below if the
4994 * are in the same FR_ROW frame.
4996 * Strategy is similar to frame_setheight().
4998 static void
4999 frame_setwidth(curfrp, width)
5000 frame_T *curfrp;
5001 int width;
5003 int room; /* total number of lines available */
5004 int take; /* number of lines taken from other windows */
5005 int run;
5006 frame_T *frp;
5007 int w;
5008 int room_reserved;
5010 /* If the width already is the desired value, nothing to do. */
5011 if (curfrp->fr_width == width)
5012 return;
5014 if (curfrp->fr_parent == NULL)
5015 /* topframe: can't change width */
5016 return;
5018 if (curfrp->fr_parent->fr_layout == FR_COL)
5020 /* Column of frames: Also need to resize frames above and below of
5021 * this one. First check for the minimal width of these. */
5022 w = frame_minwidth(curfrp->fr_parent, NULL);
5023 if (width < w)
5024 width = w;
5025 frame_setwidth(curfrp->fr_parent, width);
5027 else
5030 * Row of frames: try to change only frames in this row.
5032 * Do this twice:
5033 * 1: compute room available, if it's not enough try resizing the
5034 * containing frame.
5035 * 2: compute the room available and adjust the width to it.
5037 for (run = 1; run <= 2; ++run)
5039 room = 0;
5040 room_reserved = 0;
5041 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5042 frp = frp->fr_next)
5044 if (frp != curfrp
5045 && frp->fr_win != NULL
5046 && frp->fr_win->w_p_wfw)
5047 room_reserved += frp->fr_width;
5048 room += frp->fr_width;
5049 if (frp != curfrp)
5050 room -= frame_minwidth(frp, NULL);
5053 if (width <= room)
5054 break;
5055 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5057 if (width > room)
5058 width = room;
5059 break;
5061 frame_setwidth(curfrp->fr_parent, width
5062 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5066 * Compute the number of lines we will take from others frames (can be
5067 * negative!).
5069 take = width - curfrp->fr_width;
5071 /* If there is not enough room, also reduce the width of a window
5072 * with 'winfixwidth' set. */
5073 if (width > room - room_reserved)
5074 room_reserved = room - width;
5075 /* If there is only a 'winfixwidth' window and making the
5076 * window smaller, need to make the other window narrower. */
5077 if (take < 0 && room - curfrp->fr_width < room_reserved)
5078 room_reserved = 0;
5081 * set the current frame to the new width
5083 frame_new_width(curfrp, width, FALSE, FALSE);
5086 * First take lines from the frames right of the current frame. If
5087 * that is not enough, takes lines from frames left of the current
5088 * frame.
5090 for (run = 0; run < 2; ++run)
5092 if (run == 0)
5093 frp = curfrp->fr_next; /* 1st run: start with next window */
5094 else
5095 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5096 while (frp != NULL && take != 0)
5098 w = frame_minwidth(frp, NULL);
5099 if (room_reserved > 0
5100 && frp->fr_win != NULL
5101 && frp->fr_win->w_p_wfw)
5103 if (room_reserved >= frp->fr_width)
5104 room_reserved -= frp->fr_width;
5105 else
5107 if (frp->fr_width - room_reserved > take)
5108 room_reserved = frp->fr_width - take;
5109 take -= frp->fr_width - room_reserved;
5110 frame_new_width(frp, room_reserved, FALSE, FALSE);
5111 room_reserved = 0;
5114 else
5116 if (frp->fr_width - take < w)
5118 take -= frp->fr_width - w;
5119 frame_new_width(frp, w, FALSE, FALSE);
5121 else
5123 frame_new_width(frp, frp->fr_width - take,
5124 FALSE, FALSE);
5125 take = 0;
5128 if (run == 0)
5129 frp = frp->fr_next;
5130 else
5131 frp = frp->fr_prev;
5136 #endif /* FEAT_VERTSPLIT */
5139 * Check 'winminheight' for a valid value.
5141 void
5142 win_setminheight()
5144 int room;
5145 int first = TRUE;
5146 win_T *wp;
5148 /* loop until there is a 'winminheight' that is possible */
5149 while (p_wmh > 0)
5151 /* TODO: handle vertical splits */
5152 room = -p_wh;
5153 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5154 room += wp->w_height - p_wmh;
5155 if (room >= 0)
5156 break;
5157 --p_wmh;
5158 if (first)
5160 EMSG(_(e_noroom));
5161 first = FALSE;
5166 #ifdef FEAT_MOUSE
5169 * Status line of dragwin is dragged "offset" lines down (negative is up).
5171 void
5172 win_drag_status_line(dragwin, offset)
5173 win_T *dragwin;
5174 int offset;
5176 frame_T *curfr;
5177 frame_T *fr;
5178 int room;
5179 int row;
5180 int up; /* if TRUE, drag status line up, otherwise down */
5181 int n;
5183 fr = dragwin->w_frame;
5184 curfr = fr;
5185 if (fr != topframe) /* more than one window */
5187 fr = fr->fr_parent;
5188 /* When the parent frame is not a column of frames, its parent should
5189 * be. */
5190 if (fr->fr_layout != FR_COL)
5192 curfr = fr;
5193 if (fr != topframe) /* only a row of windows, may drag statusline */
5194 fr = fr->fr_parent;
5198 /* If this is the last frame in a column, may want to resize the parent
5199 * frame instead (go two up to skip a row of frames). */
5200 while (curfr != topframe && curfr->fr_next == NULL)
5202 if (fr != topframe)
5203 fr = fr->fr_parent;
5204 curfr = fr;
5205 if (fr != topframe)
5206 fr = fr->fr_parent;
5209 if (offset < 0) /* drag up */
5211 up = TRUE;
5212 offset = -offset;
5213 /* sum up the room of the current frame and above it */
5214 if (fr == curfr)
5216 /* only one window */
5217 room = fr->fr_height - frame_minheight(fr, NULL);
5219 else
5221 room = 0;
5222 for (fr = fr->fr_child; ; fr = fr->fr_next)
5224 room += fr->fr_height - frame_minheight(fr, NULL);
5225 if (fr == curfr)
5226 break;
5229 fr = curfr->fr_next; /* put fr at frame that grows */
5231 else /* drag down */
5233 up = FALSE;
5235 * Only dragging the last status line can reduce p_ch.
5237 room = Rows - cmdline_row;
5238 if (curfr->fr_next == NULL)
5239 room -= 1;
5240 else
5241 room -= p_ch;
5242 if (room < 0)
5243 room = 0;
5244 /* sum up the room of frames below of the current one */
5245 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5246 room += fr->fr_height - frame_minheight(fr, NULL);
5247 fr = curfr; /* put fr at window that grows */
5250 if (room < offset) /* Not enough room */
5251 offset = room; /* Move as far as we can */
5252 if (offset <= 0)
5253 return;
5256 * Grow frame fr by "offset" lines.
5257 * Doesn't happen when dragging the last status line up.
5259 if (fr != NULL)
5260 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5262 if (up)
5263 fr = curfr; /* current frame gets smaller */
5264 else
5265 fr = curfr->fr_next; /* next frame gets smaller */
5268 * Now make the other frames smaller.
5270 while (fr != NULL && offset > 0)
5272 n = frame_minheight(fr, NULL);
5273 if (fr->fr_height - offset <= n)
5275 offset -= fr->fr_height - n;
5276 frame_new_height(fr, n, !up, FALSE);
5278 else
5280 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5281 break;
5283 if (up)
5284 fr = fr->fr_prev;
5285 else
5286 fr = fr->fr_next;
5288 row = win_comp_pos();
5289 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5290 cmdline_row = row;
5291 p_ch = Rows - cmdline_row;
5292 if (p_ch < 1)
5293 p_ch = 1;
5294 curtab->tp_ch_used = p_ch;
5295 redraw_all_later(SOME_VALID);
5296 showmode();
5299 #ifdef FEAT_VERTSPLIT
5301 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5303 void
5304 win_drag_vsep_line(dragwin, offset)
5305 win_T *dragwin;
5306 int offset;
5308 frame_T *curfr;
5309 frame_T *fr;
5310 int room;
5311 int left; /* if TRUE, drag separator line left, otherwise right */
5312 int n;
5314 fr = dragwin->w_frame;
5315 if (fr == topframe) /* only one window (cannot happen?) */
5316 return;
5317 curfr = fr;
5318 fr = fr->fr_parent;
5319 /* When the parent frame is not a row of frames, its parent should be. */
5320 if (fr->fr_layout != FR_ROW)
5322 if (fr == topframe) /* only a column of windows (cannot happen?) */
5323 return;
5324 curfr = fr;
5325 fr = fr->fr_parent;
5328 /* If this is the last frame in a row, may want to resize a parent
5329 * frame instead. */
5330 while (curfr->fr_next == NULL)
5332 if (fr == topframe)
5333 break;
5334 curfr = fr;
5335 fr = fr->fr_parent;
5336 if (fr != topframe)
5338 curfr = fr;
5339 fr = fr->fr_parent;
5343 if (offset < 0) /* drag left */
5345 left = TRUE;
5346 offset = -offset;
5347 /* sum up the room of the current frame and left of it */
5348 room = 0;
5349 for (fr = fr->fr_child; ; fr = fr->fr_next)
5351 room += fr->fr_width - frame_minwidth(fr, NULL);
5352 if (fr == curfr)
5353 break;
5355 fr = curfr->fr_next; /* put fr at frame that grows */
5357 else /* drag right */
5359 left = FALSE;
5360 /* sum up the room of frames right of the current one */
5361 room = 0;
5362 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5363 room += fr->fr_width - frame_minwidth(fr, NULL);
5364 fr = curfr; /* put fr at window that grows */
5367 if (room < offset) /* Not enough room */
5368 offset = room; /* Move as far as we can */
5369 if (offset <= 0) /* No room at all, quit. */
5370 return;
5372 /* grow frame fr by offset lines */
5373 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5375 /* shrink other frames: current and at the left or at the right */
5376 if (left)
5377 fr = curfr; /* current frame gets smaller */
5378 else
5379 fr = curfr->fr_next; /* next frame gets smaller */
5381 while (fr != NULL && offset > 0)
5383 n = frame_minwidth(fr, NULL);
5384 if (fr->fr_width - offset <= n)
5386 offset -= fr->fr_width - n;
5387 frame_new_width(fr, n, !left, FALSE);
5389 else
5391 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5392 break;
5394 if (left)
5395 fr = fr->fr_prev;
5396 else
5397 fr = fr->fr_next;
5399 (void)win_comp_pos();
5400 redraw_all_later(NOT_VALID);
5402 #endif /* FEAT_VERTSPLIT */
5403 #endif /* FEAT_MOUSE */
5405 #endif /* FEAT_WINDOWS */
5408 * Set the height of a window.
5409 * This takes care of the things inside the window, not what happens to the
5410 * window position, the frame or to other windows.
5412 static void
5413 win_new_height(wp, height)
5414 win_T *wp;
5415 int height;
5417 linenr_T lnum;
5418 int sline, line_size;
5419 #define FRACTION_MULT 16384L
5421 /* Don't want a negative height. Happens when splitting a tiny window.
5422 * Will equalize heights soon to fix it. */
5423 if (height < 0)
5424 height = 0;
5425 if (wp->w_height == height)
5426 return; /* nothing to do */
5428 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5429 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5430 + FRACTION_MULT / 2) / (long)wp->w_height;
5432 wp->w_height = height;
5433 wp->w_skipcol = 0;
5435 /* Don't change w_topline when height is zero. Don't set w_topline when
5436 * 'scrollbind' is set and this isn't the current window. */
5437 if (height > 0
5438 #ifdef FEAT_SCROLLBIND
5439 && (!wp->w_p_scb || wp == curwin)
5440 #endif
5444 * Find a value for w_topline that shows the cursor at the same
5445 * relative position in the window as before (more or less).
5447 lnum = wp->w_cursor.lnum;
5448 if (lnum < 1) /* can happen when starting up */
5449 lnum = 1;
5450 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5451 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5452 sline = wp->w_wrow - line_size;
5454 if (sline >= 0)
5456 /* Make sure the whole cursor line is visible, if possible. */
5457 int rows = plines_win(wp, lnum, FALSE);
5459 if (sline > wp->w_height - rows)
5461 sline = wp->w_height - rows;
5462 wp->w_wrow -= rows - line_size;
5466 if (sline < 0)
5469 * Cursor line would go off top of screen if w_wrow was this high.
5470 * Make cursor line the first line in the window. If not enough
5471 * room use w_skipcol;
5473 wp->w_wrow = line_size;
5474 if (wp->w_wrow >= wp->w_height
5475 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5477 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5478 --wp->w_wrow;
5479 while (wp->w_wrow >= wp->w_height)
5481 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5482 + win_col_off2(wp);
5483 --wp->w_wrow;
5487 else
5489 while (sline > 0 && lnum > 1)
5491 #ifdef FEAT_FOLDING
5492 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5493 if (lnum == 1)
5495 /* first line in buffer is folded */
5496 line_size = 1;
5497 --sline;
5498 break;
5500 #endif
5501 --lnum;
5502 #ifdef FEAT_DIFF
5503 if (lnum == wp->w_topline)
5504 line_size = plines_win_nofill(wp, lnum, TRUE)
5505 + wp->w_topfill;
5506 else
5507 #endif
5508 line_size = plines_win(wp, lnum, TRUE);
5509 sline -= line_size;
5512 if (sline < 0)
5515 * Line we want at top would go off top of screen. Use next
5516 * line instead.
5518 #ifdef FEAT_FOLDING
5519 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5520 #endif
5521 lnum++;
5522 wp->w_wrow -= line_size + sline;
5524 else if (sline > 0)
5526 /* First line of file reached, use that as topline. */
5527 lnum = 1;
5528 wp->w_wrow -= sline;
5531 set_topline(wp, lnum);
5534 if (wp == curwin)
5536 if (p_so)
5537 update_topline();
5538 curs_columns(FALSE); /* validate w_wrow */
5540 wp->w_prev_fraction_row = wp->w_wrow;
5542 win_comp_scroll(wp);
5543 redraw_win_later(wp, SOME_VALID);
5544 #ifdef FEAT_WINDOWS
5545 wp->w_redr_status = TRUE;
5546 #endif
5547 invalidate_botline_win(wp);
5550 #ifdef FEAT_VERTSPLIT
5552 * Set the width of a window.
5554 static void
5555 win_new_width(wp, width)
5556 win_T *wp;
5557 int width;
5559 wp->w_width = width;
5560 wp->w_lines_valid = 0;
5561 changed_line_abv_curs_win(wp);
5562 invalidate_botline_win(wp);
5563 if (wp == curwin)
5565 update_topline();
5566 curs_columns(TRUE); /* validate w_wrow */
5568 redraw_win_later(wp, NOT_VALID);
5569 wp->w_redr_status = TRUE;
5571 #endif
5573 void
5574 win_comp_scroll(wp)
5575 win_T *wp;
5577 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5578 if (wp->w_p_scr == 0)
5579 wp->w_p_scr = 1;
5583 * command_height: called whenever p_ch has been changed
5585 void
5586 command_height()
5588 #ifdef FEAT_WINDOWS
5589 int h;
5590 frame_T *frp;
5591 int old_p_ch = curtab->tp_ch_used;
5593 /* Use the value of p_ch that we remembered. This is needed for when the
5594 * GUI starts up, we can't be sure in what order things happen. And when
5595 * p_ch was changed in another tab page. */
5596 curtab->tp_ch_used = p_ch;
5598 /* Find bottom frame with width of screen. */
5599 frp = lastwin->w_frame;
5600 # ifdef FEAT_VERTSPLIT
5601 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5602 frp = frp->fr_parent;
5603 # endif
5605 /* Avoid changing the height of a window with 'winfixheight' set. */
5606 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5607 && frp->fr_win->w_p_wfh)
5608 frp = frp->fr_prev;
5610 if (starting != NO_SCREEN)
5612 cmdline_row = Rows - p_ch;
5614 if (p_ch > old_p_ch) /* p_ch got bigger */
5616 while (p_ch > old_p_ch)
5618 if (frp == NULL)
5620 EMSG(_(e_noroom));
5621 p_ch = old_p_ch;
5622 curtab->tp_ch_used = p_ch;
5623 cmdline_row = Rows - p_ch;
5624 break;
5626 h = frp->fr_height - frame_minheight(frp, NULL);
5627 if (h > p_ch - old_p_ch)
5628 h = p_ch - old_p_ch;
5629 old_p_ch += h;
5630 frame_add_height(frp, -h);
5631 frp = frp->fr_prev;
5634 /* Recompute window positions. */
5635 (void)win_comp_pos();
5637 /* clear the lines added to cmdline */
5638 if (full_screen)
5639 screen_fill((int)(cmdline_row), (int)Rows, 0,
5640 (int)Columns, ' ', ' ', 0);
5641 msg_row = cmdline_row;
5642 redraw_cmdline = TRUE;
5643 return;
5646 if (msg_row < cmdline_row)
5647 msg_row = cmdline_row;
5648 redraw_cmdline = TRUE;
5650 frame_add_height(frp, (int)(old_p_ch - p_ch));
5652 /* Recompute window positions. */
5653 if (frp != lastwin->w_frame)
5654 (void)win_comp_pos();
5655 #else
5656 cmdline_row = Rows - p_ch;
5657 win_setheight(cmdline_row);
5658 #endif
5661 #if defined(FEAT_WINDOWS) || defined(PROTO)
5663 * Resize frame "frp" to be "n" lines higher (negative for less high).
5664 * Also resize the frames it is contained in.
5666 static void
5667 frame_add_height(frp, n)
5668 frame_T *frp;
5669 int n;
5671 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5672 for (;;)
5674 frp = frp->fr_parent;
5675 if (frp == NULL)
5676 break;
5677 frp->fr_height += n;
5682 * Add or remove a status line for the bottom window(s), according to the
5683 * value of 'laststatus'.
5685 void
5686 last_status(morewin)
5687 int morewin; /* pretend there are two or more windows */
5689 /* Don't make a difference between horizontal or vertical split. */
5690 last_status_rec(topframe, (p_ls == 2
5691 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5694 static void
5695 last_status_rec(fr, statusline)
5696 frame_T *fr;
5697 int statusline;
5699 frame_T *fp;
5700 win_T *wp;
5702 if (fr->fr_layout == FR_LEAF)
5704 wp = fr->fr_win;
5705 if (wp->w_status_height != 0 && !statusline)
5707 /* remove status line */
5708 win_new_height(wp, wp->w_height + 1);
5709 wp->w_status_height = 0;
5710 comp_col();
5712 else if (wp->w_status_height == 0 && statusline)
5714 /* Find a frame to take a line from. */
5715 fp = fr;
5716 while (fp->fr_height <= frame_minheight(fp, NULL))
5718 if (fp == topframe)
5720 EMSG(_(e_noroom));
5721 return;
5723 /* In a column of frames: go to frame above. If already at
5724 * the top or in a row of frames: go to parent. */
5725 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5726 fp = fp->fr_prev;
5727 else
5728 fp = fp->fr_parent;
5730 wp->w_status_height = 1;
5731 if (fp != fr)
5733 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5734 frame_fix_height(wp);
5735 (void)win_comp_pos();
5737 else
5738 win_new_height(wp, wp->w_height - 1);
5739 comp_col();
5740 redraw_all_later(SOME_VALID);
5743 #ifdef FEAT_VERTSPLIT
5744 else if (fr->fr_layout == FR_ROW)
5746 /* vertically split windows, set status line for each one */
5747 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5748 last_status_rec(fp, statusline);
5750 #endif
5751 else
5753 /* horizontally split window, set status line for last one */
5754 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5756 last_status_rec(fp, statusline);
5761 * Return the number of lines used by the tab page line.
5764 tabline_height()
5766 #ifdef FEAT_GUI_TABLINE
5767 /* When the GUI has the tabline then this always returns zero. */
5768 if (gui_use_tabline())
5769 return 0;
5770 #endif
5771 switch (p_stal)
5773 case 0: return 0;
5774 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5776 return 1;
5779 #endif /* FEAT_WINDOWS */
5781 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5783 * Get the file name at the cursor.
5784 * If Visual mode is active, use the selected text if it's in one line.
5785 * Returns the name in allocated memory, NULL for failure.
5787 char_u *
5788 grab_file_name(count, file_lnum)
5789 long count;
5790 linenr_T *file_lnum;
5792 # ifdef FEAT_VISUAL
5793 if (VIsual_active)
5795 int len;
5796 char_u *ptr;
5798 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5799 return NULL;
5800 return find_file_name_in_path(ptr, len,
5801 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5803 # endif
5804 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5805 file_lnum);
5810 * Return the file name under or after the cursor.
5812 * The 'path' option is searched if the file name is not absolute.
5813 * The string returned has been alloc'ed and should be freed by the caller.
5814 * NULL is returned if the file name or file is not found.
5816 * options:
5817 * FNAME_MESS give error messages
5818 * FNAME_EXP expand to path
5819 * FNAME_HYP check for hypertext link
5820 * FNAME_INCL apply "includeexpr"
5822 char_u *
5823 file_name_at_cursor(options, count, file_lnum)
5824 int options;
5825 long count;
5826 linenr_T *file_lnum;
5828 return file_name_in_line(ml_get_curline(),
5829 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5830 file_lnum);
5834 * Return the name of the file under or after ptr[col].
5835 * Otherwise like file_name_at_cursor().
5837 char_u *
5838 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5839 char_u *line;
5840 int col;
5841 int options;
5842 long count;
5843 char_u *rel_fname; /* file we are searching relative to */
5844 linenr_T *file_lnum; /* line number after the file name */
5846 char_u *ptr;
5847 int len;
5850 * search forward for what could be the start of a file name
5852 ptr = line + col;
5853 while (*ptr != NUL && !vim_isfilec(*ptr))
5854 mb_ptr_adv(ptr);
5855 if (*ptr == NUL) /* nothing found */
5857 if (options & FNAME_MESS)
5858 EMSG(_("E446: No file name under cursor"));
5859 return NULL;
5863 * Search backward for first char of the file name.
5864 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5866 while (ptr > line)
5868 #ifdef FEAT_MBYTE
5869 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5870 ptr -= len + 1;
5871 else
5872 #endif
5873 if (vim_isfilec(ptr[-1])
5874 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5875 --ptr;
5876 else
5877 break;
5881 * Search forward for the last char of the file name.
5882 * Also allow "://" when ':' is not in 'isfname'.
5884 len = 0;
5885 while (vim_isfilec(ptr[len])
5886 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5887 #ifdef FEAT_MBYTE
5888 if (has_mbyte)
5889 len += (*mb_ptr2len)(ptr + len);
5890 else
5891 #endif
5892 ++len;
5895 * If there is trailing punctuation, remove it.
5896 * But don't remove "..", could be a directory name.
5898 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5899 && ptr[len - 2] != '.')
5900 --len;
5902 if (file_lnum != NULL)
5904 char_u *p;
5906 /* Get the number after the file name and a separator character */
5907 p = ptr + len;
5908 p = skipwhite(p);
5909 if (*p != NUL)
5911 if (!isdigit(*p))
5912 ++p; /* skip the separator */
5913 p = skipwhite(p);
5914 if (isdigit(*p))
5915 *file_lnum = (int)getdigits(&p);
5919 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5922 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5923 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5925 static char_u *
5926 eval_includeexpr(ptr, len)
5927 char_u *ptr;
5928 int len;
5930 char_u *res;
5932 set_vim_var_string(VV_FNAME, ptr, len);
5933 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5934 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5935 set_vim_var_string(VV_FNAME, NULL, 0);
5936 return res;
5938 #endif
5941 * Return the name of the file ptr[len] in 'path'.
5942 * Otherwise like file_name_at_cursor().
5944 char_u *
5945 find_file_name_in_path(ptr, len, options, count, rel_fname)
5946 char_u *ptr;
5947 int len;
5948 int options;
5949 long count;
5950 char_u *rel_fname; /* file we are searching relative to */
5952 char_u *file_name;
5953 int c;
5954 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5955 char_u *tofree = NULL;
5957 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5959 tofree = eval_includeexpr(ptr, len);
5960 if (tofree != NULL)
5962 ptr = tofree;
5963 len = (int)STRLEN(ptr);
5966 # endif
5968 if (options & FNAME_EXP)
5970 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5971 TRUE, rel_fname);
5973 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5975 * If the file could not be found in a normal way, try applying
5976 * 'includeexpr' (unless done already).
5978 if (file_name == NULL
5979 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5981 tofree = eval_includeexpr(ptr, len);
5982 if (tofree != NULL)
5984 ptr = tofree;
5985 len = (int)STRLEN(ptr);
5986 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5987 TRUE, rel_fname);
5990 # endif
5991 if (file_name == NULL && (options & FNAME_MESS))
5993 c = ptr[len];
5994 ptr[len] = NUL;
5995 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5996 ptr[len] = c;
5999 /* Repeat finding the file "count" times. This matters when it
6000 * appears several times in the path. */
6001 while (file_name != NULL && --count > 0)
6003 vim_free(file_name);
6004 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6007 else
6008 file_name = vim_strnsave(ptr, len);
6010 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6011 vim_free(tofree);
6012 # endif
6014 return file_name;
6016 #endif /* FEAT_SEARCHPATH */
6019 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6020 * Also check for ":\\", which MS Internet Explorer accepts, return
6021 * URL_BACKSLASH.
6023 static int
6024 path_is_url(p)
6025 char_u *p;
6027 if (STRNCMP(p, "://", (size_t)3) == 0)
6028 return URL_SLASH;
6029 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6030 return URL_BACKSLASH;
6031 return 0;
6035 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6036 * Return URL_BACKSLASH for "name:\\".
6037 * Return zero otherwise.
6040 path_with_url(fname)
6041 char_u *fname;
6043 char_u *p;
6045 for (p = fname; isalpha(*p); ++p)
6047 return path_is_url(p);
6051 * Return TRUE if "name" is a full (absolute) path name or URL.
6054 vim_isAbsName(name)
6055 char_u *name;
6057 return (path_with_url(name) != 0 || mch_isFullName(name));
6061 * Get absolute file name into buffer "buf[len]".
6063 * return FAIL for failure, OK otherwise
6066 vim_FullName(fname, buf, len, force)
6067 char_u *fname, *buf;
6068 int len;
6069 int force; /* force expansion even when already absolute */
6071 int retval = OK;
6072 int url;
6074 *buf = NUL;
6075 if (fname == NULL)
6076 return FAIL;
6078 url = path_with_url(fname);
6079 if (!url)
6080 retval = mch_FullName(fname, buf, len, force);
6081 if (url || retval == FAIL)
6083 /* something failed; use the file name (truncate when too long) */
6084 vim_strncpy(buf, fname, len - 1);
6086 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6087 slash_adjust(buf);
6088 #endif
6089 return retval;
6093 * Return the minimal number of rows that is needed on the screen to display
6094 * the current number of windows.
6097 min_rows()
6099 int total;
6100 #ifdef FEAT_WINDOWS
6101 tabpage_T *tp;
6102 int n;
6103 #endif
6105 if (firstwin == NULL) /* not initialized yet */
6106 return MIN_LINES;
6108 #ifdef FEAT_WINDOWS
6109 total = 0;
6110 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6112 n = frame_minheight(tp->tp_topframe, NULL);
6113 if (total < n)
6114 total = n;
6116 total += tabline_height();
6117 #else
6118 total = 1; /* at least one window should have a line! */
6119 #endif
6120 total += 1; /* count the room for the command line */
6121 return total;
6125 * Return TRUE if there is only one window (in the current tab page), not
6126 * counting a help or preview window, unless it is the current window.
6127 * Does not count "aucmd_win".
6130 only_one_window()
6132 #ifdef FEAT_WINDOWS
6133 int count = 0;
6134 win_T *wp;
6136 /* If there is another tab page there always is another window. */
6137 if (first_tabpage->tp_next != NULL)
6138 return FALSE;
6140 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6141 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
6142 # ifdef FEAT_QUICKFIX
6143 || wp->w_p_pvw
6144 # endif
6145 ) || wp == curwin)
6146 # ifdef FEAT_AUTOCMD
6147 && wp != aucmd_win
6148 # endif
6150 ++count;
6151 return (count <= 1);
6152 #else
6153 return TRUE;
6154 #endif
6157 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6159 * Correct the cursor line number in other windows. Used after changing the
6160 * current buffer, and before applying autocommands.
6161 * When "do_curwin" is TRUE, also check current window.
6163 void
6164 check_lnums(do_curwin)
6165 int do_curwin;
6167 win_T *wp;
6169 #ifdef FEAT_WINDOWS
6170 tabpage_T *tp;
6172 FOR_ALL_TAB_WINDOWS(tp, wp)
6173 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6174 #else
6175 wp = curwin;
6176 if (do_curwin)
6177 #endif
6179 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6180 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6181 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6182 wp->w_topline = curbuf->b_ml.ml_line_count;
6185 #endif
6187 #if defined(FEAT_WINDOWS) || defined(PROTO)
6190 * A snapshot of the window sizes, to restore them after closing the help
6191 * window.
6192 * Only these fields are used:
6193 * fr_layout
6194 * fr_width
6195 * fr_height
6196 * fr_next
6197 * fr_child
6198 * fr_win (only valid for the old curwin, NULL otherwise)
6202 * Create a snapshot of the current frame sizes.
6204 void
6205 make_snapshot(idx)
6206 int idx;
6208 clear_snapshot(curtab, idx);
6209 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6212 static void
6213 make_snapshot_rec(fr, frp)
6214 frame_T *fr;
6215 frame_T **frp;
6217 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6218 if (*frp == NULL)
6219 return;
6220 (*frp)->fr_layout = fr->fr_layout;
6221 # ifdef FEAT_VERTSPLIT
6222 (*frp)->fr_width = fr->fr_width;
6223 # endif
6224 (*frp)->fr_height = fr->fr_height;
6225 if (fr->fr_next != NULL)
6226 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6227 if (fr->fr_child != NULL)
6228 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6229 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6230 (*frp)->fr_win = curwin;
6234 * Remove any existing snapshot.
6236 static void
6237 clear_snapshot(tp, idx)
6238 tabpage_T *tp;
6239 int idx;
6241 clear_snapshot_rec(tp->tp_snapshot[idx]);
6242 tp->tp_snapshot[idx] = NULL;
6245 static void
6246 clear_snapshot_rec(fr)
6247 frame_T *fr;
6249 if (fr != NULL)
6251 clear_snapshot_rec(fr->fr_next);
6252 clear_snapshot_rec(fr->fr_child);
6253 vim_free(fr);
6258 * Restore a previously created snapshot, if there is any.
6259 * This is only done if the screen size didn't change and the window layout is
6260 * still the same.
6262 void
6263 restore_snapshot(idx, close_curwin)
6264 int idx;
6265 int close_curwin; /* closing current window */
6267 win_T *wp;
6269 if (curtab->tp_snapshot[idx] != NULL
6270 # ifdef FEAT_VERTSPLIT
6271 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6272 # endif
6273 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6274 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6276 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6277 win_comp_pos();
6278 if (wp != NULL && close_curwin)
6279 win_goto(wp);
6280 redraw_all_later(CLEAR);
6282 clear_snapshot(curtab, idx);
6286 * Check if frames "sn" and "fr" have the same layout, same following frames
6287 * and same children.
6289 static int
6290 check_snapshot_rec(sn, fr)
6291 frame_T *sn;
6292 frame_T *fr;
6294 if (sn->fr_layout != fr->fr_layout
6295 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6296 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6297 || (sn->fr_next != NULL
6298 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6299 || (sn->fr_child != NULL
6300 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6301 return FAIL;
6302 return OK;
6306 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6307 * following frames and children.
6308 * Returns a pointer to the old current window, or NULL.
6310 static win_T *
6311 restore_snapshot_rec(sn, fr)
6312 frame_T *sn;
6313 frame_T *fr;
6315 win_T *wp = NULL;
6316 win_T *wp2;
6318 fr->fr_height = sn->fr_height;
6319 # ifdef FEAT_VERTSPLIT
6320 fr->fr_width = sn->fr_width;
6321 # endif
6322 if (fr->fr_layout == FR_LEAF)
6324 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6325 # ifdef FEAT_VERTSPLIT
6326 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6327 # endif
6328 wp = sn->fr_win;
6330 if (sn->fr_next != NULL)
6332 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6333 if (wp2 != NULL)
6334 wp = wp2;
6336 if (sn->fr_child != NULL)
6338 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6339 if (wp2 != NULL)
6340 wp = wp2;
6342 return wp;
6345 #endif
6347 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6349 * Return TRUE if there is any vertically split window.
6352 win_hasvertsplit()
6354 frame_T *fr;
6356 if (topframe->fr_layout == FR_ROW)
6357 return TRUE;
6359 if (topframe->fr_layout == FR_COL)
6360 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6361 if (fr->fr_layout == FR_ROW)
6362 return TRUE;
6364 return FALSE;
6366 #endif
6368 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6370 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6371 * highlighted with the group 'grp' with priority 'prio'.
6372 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6373 * If no particular ID is desired, -1 must be specified for 'id'.
6374 * Return ID of added match, -1 on failure.
6377 match_add(wp, grp, pat, prio, id)
6378 win_T *wp;
6379 char_u *grp;
6380 char_u *pat;
6381 int prio;
6382 int id;
6384 matchitem_T *cur;
6385 matchitem_T *prev;
6386 matchitem_T *m;
6387 int hlg_id;
6388 regprog_T *regprog;
6390 if (*grp == NUL || *pat == NUL)
6391 return -1;
6392 if (id < -1 || id == 0)
6394 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6395 return -1;
6397 if (id != -1)
6399 cur = wp->w_match_head;
6400 while (cur != NULL)
6402 if (cur->id == id)
6404 EMSGN("E801: ID already taken: %ld", id);
6405 return -1;
6407 cur = cur->next;
6410 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6412 EMSG2(_(e_nogroup), grp);
6413 return -1;
6415 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6417 EMSG2(_(e_invarg2), pat);
6418 return -1;
6421 /* Find available match ID. */
6422 while (id == -1)
6424 cur = wp->w_match_head;
6425 while (cur != NULL && cur->id != wp->w_next_match_id)
6426 cur = cur->next;
6427 if (cur == NULL)
6428 id = wp->w_next_match_id;
6429 wp->w_next_match_id++;
6432 /* Build new match. */
6433 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6434 m->id = id;
6435 m->priority = prio;
6436 m->pattern = vim_strsave(pat);
6437 m->hlg_id = hlg_id;
6438 m->match.regprog = regprog;
6439 m->match.rmm_ic = FALSE;
6440 m->match.rmm_maxcol = 0;
6442 /* Insert new match. The match list is in ascending order with regard to
6443 * the match priorities. */
6444 cur = wp->w_match_head;
6445 prev = cur;
6446 while (cur != NULL && prio >= cur->priority)
6448 prev = cur;
6449 cur = cur->next;
6451 if (cur == prev)
6452 wp->w_match_head = m;
6453 else
6454 prev->next = m;
6455 m->next = cur;
6457 redraw_later(SOME_VALID);
6458 return id;
6462 * Delete match with ID 'id' in the match list of window 'wp'.
6463 * Print error messages if 'perr' is TRUE.
6466 match_delete(wp, id, perr)
6467 win_T *wp;
6468 int id;
6469 int perr;
6471 matchitem_T *cur = wp->w_match_head;
6472 matchitem_T *prev = cur;
6474 if (id < 1)
6476 if (perr == TRUE)
6477 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6478 id);
6479 return -1;
6481 while (cur != NULL && cur->id != id)
6483 prev = cur;
6484 cur = cur->next;
6486 if (cur == NULL)
6488 if (perr == TRUE)
6489 EMSGN("E803: ID not found: %ld", id);
6490 return -1;
6492 if (cur == prev)
6493 wp->w_match_head = cur->next;
6494 else
6495 prev->next = cur->next;
6496 vim_free(cur->match.regprog);
6497 vim_free(cur->pattern);
6498 vim_free(cur);
6499 redraw_later(SOME_VALID);
6500 return 0;
6504 * Delete all matches in the match list of window 'wp'.
6506 void
6507 clear_matches(wp)
6508 win_T *wp;
6510 matchitem_T *m;
6512 while (wp->w_match_head != NULL)
6514 m = wp->w_match_head->next;
6515 vim_free(wp->w_match_head->match.regprog);
6516 vim_free(wp->w_match_head->pattern);
6517 vim_free(wp->w_match_head);
6518 wp->w_match_head = m;
6520 redraw_later(SOME_VALID);
6524 * Get match from ID 'id' in window 'wp'.
6525 * Return NULL if match not found.
6527 matchitem_T *
6528 get_match(wp, id)
6529 win_T *wp;
6530 int id;
6532 matchitem_T *cur = wp->w_match_head;
6534 while (cur != NULL && cur->id != id)
6535 cur = cur->next;
6536 return cur;
6538 #endif