Ensure valid scroll region when deleting/inserting rows
[MacVim.git] / src / window.c
blob80cbfddba7570c7dc47c418d0ec5064e3981b253
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 int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
15 static void win_init __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 win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
27 static win_T *winframe_remove __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 #if defined(FEAT_WINDOWS) || defined(PROTO)
45 static tabpage_T *alloc_tabpage __ARGS((void));
46 static int leave_tabpage __ARGS((buf_T *new_curbuf));
47 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
48 static void frame_fix_height __ARGS((win_T *wp));
49 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
50 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
51 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
52 static void win_append __ARGS((win_T *, win_T *));
53 static void win_remove __ARGS((win_T *, tabpage_T *tp));
54 static void frame_append __ARGS((frame_T *after, frame_T *frp));
55 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56 static void frame_remove __ARGS((frame_T *frp));
57 #ifdef FEAT_VERTSPLIT
58 static void win_new_width __ARGS((win_T *wp, int width));
59 static void win_goto_ver __ARGS((int up, long count));
60 static void win_goto_hor __ARGS((int left, long count));
61 #endif
62 static void frame_add_height __ARGS((frame_T *frp, int n));
63 static void last_status_rec __ARGS((frame_T *fr, int statusline));
65 static void make_snapshot __ARGS((void));
66 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
67 static void clear_snapshot __ARGS((tabpage_T *tp));
68 static void clear_snapshot_rec __ARGS((frame_T *fr));
69 static void restore_snapshot __ARGS((int close_curwin));
70 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
71 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
73 #endif /* FEAT_WINDOWS */
75 static win_T *win_alloc __ARGS((win_T *after));
76 static void win_new_height __ARGS((win_T *, int));
78 #define URL_SLASH 1 /* path_is_url() has found "://" */
79 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81 #define NOWIN (win_T *)-1 /* non-existing window */
83 #ifdef FEAT_WINDOWS
84 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
85 #else
86 # define ROWS_AVAIL (Rows - p_ch)
87 #endif
89 #if defined(FEAT_WINDOWS) || defined(PROTO)
91 static char *m_onlyone = N_("Already only one window");
94 * all CTRL-W window commands are handled here, called from normal_cmd().
96 void
97 do_window(nchar, Prenum, xchar)
98 int nchar;
99 long Prenum;
100 int xchar; /* extra char from ":wincmd gx" or NUL */
102 long Prenum1;
103 win_T *wp;
104 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
105 char_u *ptr;
106 linenr_T lnum = -1;
107 #endif
108 #ifdef FEAT_FIND_ID
109 int type = FIND_DEFINE;
110 int len;
111 #endif
112 char_u cbuf[40];
114 if (Prenum == 0)
115 Prenum1 = 1;
116 else
117 Prenum1 = Prenum;
119 #ifdef FEAT_CMDWIN
120 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
121 #else
122 # define CHECK_CMDWIN
123 #endif
125 switch (nchar)
127 /* split current window in two parts, horizontally */
128 case 'S':
129 case Ctrl_S:
130 case 's':
131 CHECK_CMDWIN
132 #ifdef FEAT_VISUAL
133 reset_VIsual_and_resel(); /* stop Visual mode */
134 #endif
135 #ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140 #endif
141 #ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143 #endif
144 win_split((int)Prenum, 0);
145 break;
147 #ifdef FEAT_VERTSPLIT
148 /* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
152 # ifdef FEAT_VISUAL
153 reset_VIsual_and_resel(); /* stop Visual mode */
154 # endif
155 # ifdef FEAT_QUICKFIX
156 /* When splitting the quickfix window open a new buffer in it,
157 * don't replicate the quickfix buffer. */
158 if (bt_quickfix(curbuf))
159 goto newwindow;
160 # endif
161 # ifdef FEAT_GUI
162 need_mouse_correct = TRUE;
163 # endif
164 win_split((int)Prenum, WSP_VERT);
165 break;
166 #endif
168 /* split current window and edit alternate file */
169 case Ctrl_HAT:
170 case '^':
171 CHECK_CMDWIN
172 #ifdef FEAT_VISUAL
173 reset_VIsual_and_resel(); /* stop Visual mode */
174 #endif
175 STRCPY(cbuf, "split #");
176 if (Prenum)
177 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
178 "%ld", Prenum);
179 do_cmdline_cmd(cbuf);
180 break;
182 /* open new window */
183 case Ctrl_N:
184 case 'n':
185 CHECK_CMDWIN
186 #ifdef FEAT_VISUAL
187 reset_VIsual_and_resel(); /* stop Visual mode */
188 #endif
189 #ifdef FEAT_QUICKFIX
190 newwindow:
191 #endif
192 if (Prenum)
193 /* window height */
194 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
195 else
196 cbuf[0] = NUL;
197 #if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
198 if (nchar == 'v' || nchar == Ctrl_V)
199 STRCAT(cbuf, "v");
200 #endif
201 STRCAT(cbuf, "new");
202 do_cmdline_cmd(cbuf);
203 break;
205 /* quit current window */
206 case Ctrl_Q:
207 case 'q':
208 #ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210 #endif
211 do_cmdline_cmd((char_u *)"quit");
212 break;
214 /* close current window */
215 case Ctrl_C:
216 case 'c':
217 #ifdef FEAT_VISUAL
218 reset_VIsual_and_resel(); /* stop Visual mode */
219 #endif
220 do_cmdline_cmd((char_u *)"close");
221 break;
223 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
224 /* close preview window */
225 case Ctrl_Z:
226 case 'z':
227 CHECK_CMDWIN
228 #ifdef FEAT_VISUAL
229 reset_VIsual_and_resel(); /* stop Visual mode */
230 #endif
231 do_cmdline_cmd((char_u *)"pclose");
232 break;
234 /* cursor to preview window */
235 case 'P':
236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
237 if (wp->w_p_pvw)
238 break;
239 if (wp == NULL)
240 EMSG(_("E441: There is no preview window"));
241 else
242 win_goto(wp);
243 break;
244 #endif
246 /* close all but current window */
247 case Ctrl_O:
248 case 'o':
249 CHECK_CMDWIN
250 #ifdef FEAT_VISUAL
251 reset_VIsual_and_resel(); /* stop Visual mode */
252 #endif
253 do_cmdline_cmd((char_u *)"only");
254 break;
256 /* cursor to next window with wrap around */
257 case Ctrl_W:
258 case 'w':
259 /* cursor to previous window with wrap around */
260 case 'W':
261 CHECK_CMDWIN
262 if (lastwin == firstwin && Prenum != 1) /* just one window */
263 beep_flush();
264 else
266 if (Prenum) /* go to specified window */
268 for (wp = firstwin; --Prenum > 0; )
270 if (wp->w_next == NULL)
271 break;
272 else
273 wp = wp->w_next;
276 else
278 if (nchar == 'W') /* go to previous window */
280 wp = curwin->w_prev;
281 if (wp == NULL)
282 wp = lastwin; /* wrap around */
284 else /* go to next window */
286 wp = curwin->w_next;
287 if (wp == NULL)
288 wp = firstwin; /* wrap around */
291 win_goto(wp);
293 break;
295 /* cursor to window below */
296 case 'j':
297 case K_DOWN:
298 case Ctrl_J:
299 CHECK_CMDWIN
300 #ifdef FEAT_VERTSPLIT
301 win_goto_ver(FALSE, Prenum1);
302 #else
303 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
304 wp = wp->w_next)
306 win_goto(wp);
307 #endif
308 break;
310 /* cursor to window above */
311 case 'k':
312 case K_UP:
313 case Ctrl_K:
314 CHECK_CMDWIN
315 #ifdef FEAT_VERTSPLIT
316 win_goto_ver(TRUE, Prenum1);
317 #else
318 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
319 wp = wp->w_prev)
321 win_goto(wp);
322 #endif
323 break;
325 #ifdef FEAT_VERTSPLIT
326 /* cursor to left window */
327 case 'h':
328 case K_LEFT:
329 case Ctrl_H:
330 case K_BS:
331 CHECK_CMDWIN
332 win_goto_hor(TRUE, Prenum1);
333 break;
335 /* cursor to right window */
336 case 'l':
337 case K_RIGHT:
338 case Ctrl_L:
339 CHECK_CMDWIN
340 win_goto_hor(FALSE, Prenum1);
341 break;
342 #endif
344 /* move window to new tab page */
345 case 'T':
346 if (firstwin == lastwin)
347 MSG(_(m_onlyone));
348 else
350 tabpage_T *oldtab = curtab;
351 tabpage_T *newtab;
353 /* First create a new tab with the window, then go back to
354 * the old tab and close the window there. */
355 wp = curwin;
356 if (win_new_tabpage((int)Prenum) == OK
357 && valid_tabpage(oldtab))
359 newtab = curtab;
360 goto_tabpage_tp(oldtab);
361 if (curwin == wp)
362 win_close(curwin, FALSE);
363 if (valid_tabpage(newtab))
364 goto_tabpage_tp(newtab);
367 break;
369 /* cursor to top-left window */
370 case 't':
371 case Ctrl_T:
372 win_goto(firstwin);
373 break;
375 /* cursor to bottom-right window */
376 case 'b':
377 case Ctrl_B:
378 win_goto(lastwin);
379 break;
381 /* cursor to last accessed (previous) window */
382 case 'p':
383 case Ctrl_P:
384 if (prevwin == NULL)
385 beep_flush();
386 else
387 win_goto(prevwin);
388 break;
390 /* exchange current and next window */
391 case 'x':
392 case Ctrl_X:
393 CHECK_CMDWIN
394 win_exchange(Prenum);
395 break;
397 /* rotate windows downwards */
398 case Ctrl_R:
399 case 'r':
400 CHECK_CMDWIN
401 #ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403 #endif
404 win_rotate(FALSE, (int)Prenum1); /* downwards */
405 break;
407 /* rotate windows upwards */
408 case 'R':
409 CHECK_CMDWIN
410 #ifdef FEAT_VISUAL
411 reset_VIsual_and_resel(); /* stop Visual mode */
412 #endif
413 win_rotate(TRUE, (int)Prenum1); /* upwards */
414 break;
416 /* move window to the very top/bottom/left/right */
417 case 'K':
418 case 'J':
419 #ifdef FEAT_VERTSPLIT
420 case 'H':
421 case 'L':
422 #endif
423 CHECK_CMDWIN
424 win_totop((int)Prenum,
425 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
426 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
427 break;
429 /* make all windows the same height */
430 case '=':
431 #ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433 #endif
434 win_equal(NULL, FALSE, 'b');
435 break;
437 /* increase current window height */
438 case '+':
439 #ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441 #endif
442 win_setheight(curwin->w_height + (int)Prenum1);
443 break;
445 /* decrease current window height */
446 case '-':
447 #ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449 #endif
450 win_setheight(curwin->w_height - (int)Prenum1);
451 break;
453 /* set current window height */
454 case Ctrl__:
455 case '_':
456 #ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458 #endif
459 win_setheight(Prenum ? (int)Prenum : 9999);
460 break;
462 #ifdef FEAT_VERTSPLIT
463 /* increase current window width */
464 case '>':
465 #ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467 #endif
468 win_setwidth(curwin->w_width + (int)Prenum1);
469 break;
471 /* decrease current window width */
472 case '<':
473 #ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475 #endif
476 win_setwidth(curwin->w_width - (int)Prenum1);
477 break;
479 /* set current window width */
480 case '|':
481 #ifdef FEAT_GUI
482 need_mouse_correct = TRUE;
483 #endif
484 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
485 break;
486 #endif
488 /* jump to tag and split window if tag exists (in preview window) */
489 #if defined(FEAT_QUICKFIX)
490 case '}':
491 CHECK_CMDWIN
492 if (Prenum)
493 g_do_tagpreview = Prenum;
494 else
495 g_do_tagpreview = p_pvh;
496 /*FALLTHROUGH*/
497 #endif
498 case ']':
499 case Ctrl_RSB:
500 CHECK_CMDWIN
501 #ifdef FEAT_VISUAL
502 reset_VIsual_and_resel(); /* stop Visual mode */
503 #endif
504 if (Prenum)
505 postponed_split = Prenum;
506 else
507 postponed_split = -1;
509 /* Execute the command right here, required when
510 * "wincmd ]" was used in a function. */
511 do_nv_ident(Ctrl_RSB, NUL);
512 break;
514 #ifdef FEAT_SEARCHPATH
515 /* edit file name under cursor in a new window */
516 case 'f':
517 case 'F':
518 case Ctrl_F:
519 wingotofile:
520 CHECK_CMDWIN
522 ptr = grab_file_name(Prenum1, &lnum);
523 if (ptr != NULL)
525 # ifdef FEAT_GUI
526 need_mouse_correct = TRUE;
527 # endif
528 setpcmark();
529 if (win_split(0, 0) == OK)
531 # ifdef FEAT_SCROLLBIND
532 curwin->w_p_scb = FALSE;
533 # endif
534 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
535 if (nchar == 'F' && lnum >= 0)
537 curwin->w_cursor.lnum = lnum;
538 check_cursor_lnum();
539 beginline(BL_SOL | BL_FIX);
542 vim_free(ptr);
544 break;
545 #endif
547 #ifdef FEAT_FIND_ID
548 /* Go to the first occurrence of the identifier under cursor along path in a
549 * new window -- webb
551 case 'i': /* Go to any match */
552 case Ctrl_I:
553 type = FIND_ANY;
554 /* FALLTHROUGH */
555 case 'd': /* Go to definition, using 'define' */
556 case Ctrl_D:
557 CHECK_CMDWIN
558 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
559 break;
560 find_pattern_in_path(ptr, 0, len, TRUE,
561 Prenum == 0 ? TRUE : FALSE, type,
562 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
563 curwin->w_set_curswant = TRUE;
564 break;
565 #endif
567 case K_KENTER:
568 case CAR:
569 #if defined(FEAT_QUICKFIX)
571 * In a quickfix window a <CR> jumps to the error under the
572 * cursor in a new window.
574 if (bt_quickfix(curbuf))
576 sprintf((char *)cbuf, "split +%ld%s",
577 (long)curwin->w_cursor.lnum,
578 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
579 do_cmdline_cmd(cbuf);
581 #endif
582 break;
585 /* CTRL-W g extended commands */
586 case 'g':
587 case Ctrl_G:
588 CHECK_CMDWIN
589 #ifdef USE_ON_FLY_SCROLL
590 dont_scroll = TRUE; /* disallow scrolling here */
591 #endif
592 ++no_mapping;
593 ++allow_keys; /* no mapping for xchar, but allow key codes */
594 if (xchar == NUL)
595 xchar = plain_vgetc();
596 #ifdef FEAT_LANGMAP
597 LANGMAP_ADJUST(xchar, TRUE);
598 #endif
599 --no_mapping;
600 --allow_keys;
601 #ifdef FEAT_CMDL_INFO
602 (void)add_to_showcmd(xchar);
603 #endif
604 switch (xchar)
606 #if defined(FEAT_QUICKFIX)
607 case '}':
608 xchar = Ctrl_RSB;
609 if (Prenum)
610 g_do_tagpreview = Prenum;
611 else
612 g_do_tagpreview = p_pvh;
613 /*FALLTHROUGH*/
614 #endif
615 case ']':
616 case Ctrl_RSB:
617 #ifdef FEAT_VISUAL
618 reset_VIsual_and_resel(); /* stop Visual mode */
619 #endif
620 if (Prenum)
621 postponed_split = Prenum;
622 else
623 postponed_split = -1;
625 /* Execute the command right here, required when
626 * "wincmd g}" was used in a function. */
627 do_nv_ident('g', xchar);
628 break;
630 #ifdef FEAT_SEARCHPATH
631 case 'f': /* CTRL-W gf: "gf" in a new tab page */
632 case 'F': /* CTRL-W gF: "gF" in a new tab page */
633 cmdmod.tab = TRUE;
634 nchar = xchar;
635 goto wingotofile;
636 #endif
637 default:
638 beep_flush();
639 break;
641 break;
643 default: beep_flush();
644 break;
649 * split the current window, implements CTRL-W s and :split
651 * "size" is the height or width for the new window, 0 to use half of current
652 * height or width.
654 * "flags":
655 * WSP_ROOM: require enough room for new window
656 * WSP_VERT: vertical split.
657 * WSP_TOP: open window at the top-left of the shell (help window).
658 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
659 * WSP_HELP: creating the help window, keep layout snapshot
661 * return FAIL for failure, OK otherwise
664 win_split(size, flags)
665 int size;
666 int flags;
668 /* When the ":tab" modifier was used open a new tab page instead. */
669 if (may_open_tabpage() == OK)
670 return OK;
672 /* Add flags from ":vertical", ":topleft" and ":botright". */
673 flags |= cmdmod.split;
674 if ((flags & WSP_TOP) && (flags & WSP_BOT))
676 EMSG(_("E442: Can't split topleft and botright at the same time"));
677 return FAIL;
680 /* When creating the help window make a snapshot of the window layout.
681 * Otherwise clear the snapshot, it's now invalid. */
682 if (flags & WSP_HELP)
683 make_snapshot();
684 else
685 clear_snapshot(curtab);
687 return win_split_ins(size, flags, NULL, 0);
691 * When "newwin" is NULL: split the current window in two.
692 * When "newwin" is not NULL: insert this window at the far
693 * top/left/right/bottom.
694 * return FAIL for failure, OK otherwise
696 static int
697 win_split_ins(size, flags, newwin, dir)
698 int size;
699 int flags;
700 win_T *newwin;
701 int dir;
703 win_T *wp = newwin;
704 win_T *oldwin;
705 int new_size = size;
706 int i;
707 int need_status = 0;
708 int do_equal = FALSE;
709 int needed;
710 int available;
711 int oldwin_height = 0;
712 int layout;
713 frame_T *frp, *curfrp;
714 int before;
716 if (flags & WSP_TOP)
717 oldwin = firstwin;
718 else if (flags & WSP_BOT)
719 oldwin = lastwin;
720 else
721 oldwin = curwin;
723 /* add a status line when p_ls == 1 and splitting the first window */
724 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
726 if (oldwin->w_height <= p_wmh && newwin == NULL)
728 EMSG(_(e_noroom));
729 return FAIL;
731 need_status = STATUS_HEIGHT;
734 #ifdef FEAT_GUI
735 /* May be needed for the scrollbars that are going to change. */
736 if (gui.in_use)
737 out_flush();
738 #endif
740 #ifdef FEAT_VERTSPLIT
741 if (flags & WSP_VERT)
743 layout = FR_ROW;
746 * Check if we are able to split the current window and compute its
747 * width.
749 needed = p_wmw + 1;
750 if (flags & WSP_ROOM)
751 needed += p_wiw - p_wmw;
752 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
754 available = topframe->fr_width;
755 needed += frame_minwidth(topframe, NULL);
757 else
758 available = oldwin->w_width;
759 if (available < needed && newwin == NULL)
761 EMSG(_(e_noroom));
762 return FAIL;
764 if (new_size == 0)
765 new_size = oldwin->w_width / 2;
766 if (new_size > oldwin->w_width - p_wmw - 1)
767 new_size = oldwin->w_width - p_wmw - 1;
768 if (new_size < p_wmw)
769 new_size = p_wmw;
771 /* if it doesn't fit in the current window, need win_equal() */
772 if (oldwin->w_width - new_size - 1 < p_wmw)
773 do_equal = TRUE;
775 /* We don't like to take lines for the new window from a
776 * 'winfixwidth' window. Take them from a window to the left or right
777 * instead, if possible. */
778 if (oldwin->w_p_wfw)
779 win_setwidth_win(oldwin->w_width + new_size, oldwin);
781 /* Only make all windows the same width if one of them (except oldwin)
782 * is wider than one of the split windows. */
783 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
784 && oldwin->w_frame->fr_parent != NULL)
786 frp = oldwin->w_frame->fr_parent->fr_child;
787 while (frp != NULL)
789 if (frp->fr_win != oldwin && frp->fr_win != NULL
790 && (frp->fr_win->w_width > new_size
791 || frp->fr_win->w_width > oldwin->w_width
792 - new_size - STATUS_HEIGHT))
794 do_equal = TRUE;
795 break;
797 frp = frp->fr_next;
801 else
802 #endif
804 layout = FR_COL;
807 * Check if we are able to split the current window and compute its
808 * height.
810 needed = p_wmh + STATUS_HEIGHT + need_status;
811 if (flags & WSP_ROOM)
812 needed += p_wh - p_wmh;
813 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
815 available = topframe->fr_height;
816 needed += frame_minheight(topframe, NULL);
818 else
820 available = oldwin->w_height;
821 needed += p_wmh;
823 if (available < needed && newwin == NULL)
825 EMSG(_(e_noroom));
826 return FAIL;
828 oldwin_height = oldwin->w_height;
829 if (need_status)
831 oldwin->w_status_height = STATUS_HEIGHT;
832 oldwin_height -= STATUS_HEIGHT;
834 if (new_size == 0)
835 new_size = oldwin_height / 2;
837 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
838 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
839 if (new_size < p_wmh)
840 new_size = p_wmh;
842 /* if it doesn't fit in the current window, need win_equal() */
843 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
844 do_equal = TRUE;
846 /* We don't like to take lines for the new window from a
847 * 'winfixheight' window. Take them from a window above or below
848 * instead, if possible. */
849 if (oldwin->w_p_wfh)
851 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
852 oldwin);
853 oldwin_height = oldwin->w_height;
854 if (need_status)
855 oldwin_height -= STATUS_HEIGHT;
858 /* Only make all windows the same height if one of them (except oldwin)
859 * is higher than one of the split windows. */
860 if (!do_equal && p_ea && size == 0
861 #ifdef FEAT_VERTSPLIT
862 && *p_ead != 'h'
863 #endif
864 && oldwin->w_frame->fr_parent != NULL)
866 frp = oldwin->w_frame->fr_parent->fr_child;
867 while (frp != NULL)
869 if (frp->fr_win != oldwin && frp->fr_win != NULL
870 && (frp->fr_win->w_height > new_size
871 || frp->fr_win->w_height > oldwin_height - new_size
872 - STATUS_HEIGHT))
874 do_equal = TRUE;
875 break;
877 frp = frp->fr_next;
883 * allocate new window structure and link it in the window list
885 if ((flags & WSP_TOP) == 0
886 && ((flags & WSP_BOT)
887 || (flags & WSP_BELOW)
888 || (!(flags & WSP_ABOVE)
889 && (
890 #ifdef FEAT_VERTSPLIT
891 (flags & WSP_VERT) ? p_spr :
892 #endif
893 p_sb))))
895 /* new window below/right of current one */
896 if (newwin == NULL)
897 wp = win_alloc(oldwin);
898 else
899 win_append(oldwin, wp);
901 else
903 if (newwin == NULL)
904 wp = win_alloc(oldwin->w_prev);
905 else
906 win_append(oldwin->w_prev, wp);
909 if (newwin == NULL)
911 if (wp == NULL)
912 return FAIL;
914 /* make the contents of the new window the same as the current one */
915 win_init(wp, curwin);
919 * Reorganise the tree of frames to insert the new window.
921 if (flags & (WSP_TOP | WSP_BOT))
923 #ifdef FEAT_VERTSPLIT
924 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
925 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
926 #else
927 if (topframe->fr_layout == FR_COL)
928 #endif
930 curfrp = topframe->fr_child;
931 if (flags & WSP_BOT)
932 while (curfrp->fr_next != NULL)
933 curfrp = curfrp->fr_next;
935 else
936 curfrp = topframe;
937 before = (flags & WSP_TOP);
939 else
941 curfrp = oldwin->w_frame;
942 if (flags & WSP_BELOW)
943 before = FALSE;
944 else if (flags & WSP_ABOVE)
945 before = TRUE;
946 else
947 #ifdef FEAT_VERTSPLIT
948 if (flags & WSP_VERT)
949 before = !p_spr;
950 else
951 #endif
952 before = !p_sb;
954 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
956 /* Need to create a new frame in the tree to make a branch. */
957 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
958 *frp = *curfrp;
959 curfrp->fr_layout = layout;
960 frp->fr_parent = curfrp;
961 frp->fr_next = NULL;
962 frp->fr_prev = NULL;
963 curfrp->fr_child = frp;
964 curfrp->fr_win = NULL;
965 curfrp = frp;
966 if (frp->fr_win != NULL)
967 oldwin->w_frame = frp;
968 else
969 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
970 frp->fr_parent = curfrp;
973 if (newwin == NULL)
975 /* Create a frame for the new window. */
976 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
977 frp->fr_layout = FR_LEAF;
978 frp->fr_win = wp;
979 wp->w_frame = frp;
981 else
982 frp = newwin->w_frame;
983 frp->fr_parent = curfrp->fr_parent;
985 /* Insert the new frame at the right place in the frame list. */
986 if (before)
987 frame_insert(curfrp, frp);
988 else
989 frame_append(curfrp, frp);
991 #ifdef FEAT_VERTSPLIT
992 if (flags & WSP_VERT)
994 wp->w_p_scr = curwin->w_p_scr;
995 if (need_status)
997 --oldwin->w_height;
998 oldwin->w_status_height = need_status;
1000 if (flags & (WSP_TOP | WSP_BOT))
1002 /* set height and row of new window to full height */
1003 wp->w_winrow = tabline_height();
1004 wp->w_height = curfrp->fr_height - (p_ls > 0);
1005 wp->w_status_height = (p_ls > 0);
1007 else
1009 /* height and row of new window is same as current window */
1010 wp->w_winrow = oldwin->w_winrow;
1011 wp->w_height = oldwin->w_height;
1012 wp->w_status_height = oldwin->w_status_height;
1014 frp->fr_height = curfrp->fr_height;
1016 /* "new_size" of the current window goes to the new window, use
1017 * one column for the vertical separator */
1018 wp->w_width = new_size;
1019 if (before)
1020 wp->w_vsep_width = 1;
1021 else
1023 wp->w_vsep_width = oldwin->w_vsep_width;
1024 oldwin->w_vsep_width = 1;
1026 if (flags & (WSP_TOP | WSP_BOT))
1028 if (flags & WSP_BOT)
1029 frame_add_vsep(curfrp);
1030 /* Set width of neighbor frame */
1031 frame_new_width(curfrp, curfrp->fr_width
1032 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1033 FALSE);
1035 else
1036 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1037 if (before) /* new window left of current one */
1039 wp->w_wincol = oldwin->w_wincol;
1040 oldwin->w_wincol += new_size + 1;
1042 else /* new window right of current one */
1043 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1044 frame_fix_width(oldwin);
1045 frame_fix_width(wp);
1047 else
1048 #endif
1050 /* width and column of new window is same as current window */
1051 #ifdef FEAT_VERTSPLIT
1052 if (flags & (WSP_TOP | WSP_BOT))
1054 wp->w_wincol = 0;
1055 wp->w_width = Columns;
1056 wp->w_vsep_width = 0;
1058 else
1060 wp->w_wincol = oldwin->w_wincol;
1061 wp->w_width = oldwin->w_width;
1062 wp->w_vsep_width = oldwin->w_vsep_width;
1064 frp->fr_width = curfrp->fr_width;
1065 #endif
1067 /* "new_size" of the current window goes to the new window, use
1068 * one row for the status line */
1069 win_new_height(wp, new_size);
1070 if (flags & (WSP_TOP | WSP_BOT))
1071 frame_new_height(curfrp, curfrp->fr_height
1072 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1073 else
1074 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1075 if (before) /* new window above current one */
1077 wp->w_winrow = oldwin->w_winrow;
1078 wp->w_status_height = STATUS_HEIGHT;
1079 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1081 else /* new window below current one */
1083 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1084 wp->w_status_height = oldwin->w_status_height;
1085 oldwin->w_status_height = STATUS_HEIGHT;
1087 #ifdef FEAT_VERTSPLIT
1088 if (flags & WSP_BOT)
1089 frame_add_statusline(curfrp);
1090 #endif
1091 frame_fix_height(wp);
1092 frame_fix_height(oldwin);
1095 if (flags & (WSP_TOP | WSP_BOT))
1096 (void)win_comp_pos();
1099 * Both windows need redrawing
1101 redraw_win_later(wp, NOT_VALID);
1102 wp->w_redr_status = TRUE;
1103 redraw_win_later(oldwin, NOT_VALID);
1104 oldwin->w_redr_status = TRUE;
1106 if (need_status)
1108 msg_row = Rows - 1;
1109 msg_col = sc_col;
1110 msg_clr_eos_force(); /* Old command/ruler may still be there */
1111 comp_col();
1112 msg_row = Rows - 1;
1113 msg_col = 0; /* put position back at start of line */
1117 * make the new window the current window and redraw
1119 if (do_equal || dir != 0)
1120 win_equal(wp, TRUE,
1121 #ifdef FEAT_VERTSPLIT
1122 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1123 : dir == 'h' ? 'b' :
1124 #endif
1125 'v');
1127 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1128 * size was given. */
1129 #ifdef FEAT_VERTSPLIT
1130 if (flags & WSP_VERT)
1132 i = p_wiw;
1133 if (size != 0)
1134 p_wiw = size;
1136 # ifdef FEAT_GUI
1137 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1138 if (gui.in_use)
1139 gui_init_which_components(NULL);
1140 # endif
1142 else
1143 #endif
1145 i = p_wh;
1146 if (size != 0)
1147 p_wh = size;
1149 win_enter(wp, FALSE);
1150 #ifdef FEAT_VERTSPLIT
1151 if (flags & WSP_VERT)
1152 p_wiw = i;
1153 else
1154 #endif
1155 p_wh = i;
1157 return OK;
1161 * Initialize window "newp" from window "oldp".
1162 * Used when splitting a window and when creating a new tab page.
1163 * The windows will both edit the same buffer.
1165 static void
1166 win_init(newp, oldp)
1167 win_T *newp;
1168 win_T *oldp;
1170 int i;
1172 newp->w_buffer = oldp->w_buffer;
1173 oldp->w_buffer->b_nwindows++;
1174 newp->w_cursor = oldp->w_cursor;
1175 newp->w_valid = 0;
1176 newp->w_curswant = oldp->w_curswant;
1177 newp->w_set_curswant = oldp->w_set_curswant;
1178 newp->w_topline = oldp->w_topline;
1179 #ifdef FEAT_DIFF
1180 newp->w_topfill = oldp->w_topfill;
1181 #endif
1182 newp->w_leftcol = oldp->w_leftcol;
1183 newp->w_pcmark = oldp->w_pcmark;
1184 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1185 newp->w_alt_fnum = oldp->w_alt_fnum;
1186 newp->w_wrow = oldp->w_wrow;
1187 newp->w_fraction = oldp->w_fraction;
1188 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1189 #ifdef FEAT_JUMPLIST
1190 copy_jumplist(oldp, newp);
1191 #endif
1192 #ifdef FEAT_QUICKFIX
1193 copy_loclist(oldp, newp);
1194 #endif
1195 if (oldp->w_localdir != NULL)
1196 newp->w_localdir = vim_strsave(oldp->w_localdir);
1198 /* Use the same argument list. */
1199 newp->w_alist = oldp->w_alist;
1200 ++newp->w_alist->al_refcount;
1201 newp->w_arg_idx = oldp->w_arg_idx;
1204 * copy tagstack and options from existing window
1206 for (i = 0; i < oldp->w_tagstacklen; i++)
1208 newp->w_tagstack[i] = oldp->w_tagstack[i];
1209 if (newp->w_tagstack[i].tagname != NULL)
1210 newp->w_tagstack[i].tagname =
1211 vim_strsave(newp->w_tagstack[i].tagname);
1213 newp->w_tagstackidx = oldp->w_tagstackidx;
1214 newp->w_tagstacklen = oldp->w_tagstacklen;
1215 win_copy_options(oldp, newp);
1216 # ifdef FEAT_FOLDING
1217 copyFoldingState(oldp, newp);
1218 # endif
1221 #endif /* FEAT_WINDOWS */
1223 #if defined(FEAT_WINDOWS) || defined(PROTO)
1225 * Check if "win" is a pointer to an existing window.
1228 win_valid(win)
1229 win_T *win;
1231 win_T *wp;
1233 if (win == NULL)
1234 return FALSE;
1235 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1236 if (wp == win)
1237 return TRUE;
1238 return FALSE;
1242 * Return the number of windows.
1245 win_count()
1247 win_T *wp;
1248 int count = 0;
1250 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1251 ++count;
1252 return count;
1256 * Make "count" windows on the screen.
1257 * Return actual number of windows on the screen.
1258 * Must be called when there is just one window, filling the whole screen
1259 * (excluding the command line).
1261 /*ARGSUSED*/
1263 make_windows(count, vertical)
1264 int count;
1265 int vertical; /* split windows vertically if TRUE */
1267 int maxcount;
1268 int todo;
1270 #ifdef FEAT_VERTSPLIT
1271 if (vertical)
1273 /* Each windows needs at least 'winminwidth' lines and a separator
1274 * column. */
1275 maxcount = (curwin->w_width + curwin->w_vsep_width
1276 - (p_wiw - p_wmw)) / (p_wmw + 1);
1278 else
1279 #endif
1281 /* Each window needs at least 'winminheight' lines and a status line. */
1282 maxcount = (curwin->w_height + curwin->w_status_height
1283 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1286 if (maxcount < 2)
1287 maxcount = 2;
1288 if (count > maxcount)
1289 count = maxcount;
1292 * add status line now, otherwise first window will be too big
1294 if (count > 1)
1295 last_status(TRUE);
1297 #ifdef FEAT_AUTOCMD
1299 * Don't execute autocommands while creating the windows. Must do that
1300 * when putting the buffers in the windows.
1302 block_autocmds();
1303 #endif
1305 /* todo is number of windows left to create */
1306 for (todo = count - 1; todo > 0; --todo)
1307 #ifdef FEAT_VERTSPLIT
1308 if (vertical)
1310 if (win_split(curwin->w_width - (curwin->w_width - todo)
1311 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1312 break;
1314 else
1315 #endif
1317 if (win_split(curwin->w_height - (curwin->w_height - todo
1318 * STATUS_HEIGHT) / (todo + 1)
1319 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1320 break;
1323 #ifdef FEAT_AUTOCMD
1324 unblock_autocmds();
1325 #endif
1327 /* return actual number of windows */
1328 return (count - todo);
1332 * Exchange current and next window
1334 static void
1335 win_exchange(Prenum)
1336 long Prenum;
1338 frame_T *frp;
1339 frame_T *frp2;
1340 win_T *wp;
1341 win_T *wp2;
1342 int temp;
1344 if (lastwin == firstwin) /* just one window */
1346 beep_flush();
1347 return;
1350 #ifdef FEAT_GUI
1351 need_mouse_correct = TRUE;
1352 #endif
1355 * find window to exchange with
1357 if (Prenum)
1359 frp = curwin->w_frame->fr_parent->fr_child;
1360 while (frp != NULL && --Prenum > 0)
1361 frp = frp->fr_next;
1363 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1364 frp = curwin->w_frame->fr_next;
1365 else /* Swap last window in row/col with previous */
1366 frp = curwin->w_frame->fr_prev;
1368 /* We can only exchange a window with another window, not with a frame
1369 * containing windows. */
1370 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1371 return;
1372 wp = frp->fr_win;
1375 * 1. remove curwin from the list. Remember after which window it was in wp2
1376 * 2. insert curwin before wp in the list
1377 * if wp != wp2
1378 * 3. remove wp from the list
1379 * 4. insert wp after wp2
1380 * 5. exchange the status line height and vsep width.
1382 wp2 = curwin->w_prev;
1383 frp2 = curwin->w_frame->fr_prev;
1384 if (wp->w_prev != curwin)
1386 win_remove(curwin, NULL);
1387 frame_remove(curwin->w_frame);
1388 win_append(wp->w_prev, curwin);
1389 frame_insert(frp, curwin->w_frame);
1391 if (wp != wp2)
1393 win_remove(wp, NULL);
1394 frame_remove(wp->w_frame);
1395 win_append(wp2, wp);
1396 if (frp2 == NULL)
1397 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1398 else
1399 frame_append(frp2, wp->w_frame);
1401 temp = curwin->w_status_height;
1402 curwin->w_status_height = wp->w_status_height;
1403 wp->w_status_height = temp;
1404 #ifdef FEAT_VERTSPLIT
1405 temp = curwin->w_vsep_width;
1406 curwin->w_vsep_width = wp->w_vsep_width;
1407 wp->w_vsep_width = temp;
1409 /* If the windows are not in the same frame, exchange the sizes to avoid
1410 * messing up the window layout. Otherwise fix the frame sizes. */
1411 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1413 temp = curwin->w_height;
1414 curwin->w_height = wp->w_height;
1415 wp->w_height = temp;
1416 temp = curwin->w_width;
1417 curwin->w_width = wp->w_width;
1418 wp->w_width = temp;
1420 else
1422 frame_fix_height(curwin);
1423 frame_fix_height(wp);
1424 frame_fix_width(curwin);
1425 frame_fix_width(wp);
1427 #endif
1429 (void)win_comp_pos(); /* recompute window positions */
1431 win_enter(wp, TRUE);
1432 redraw_later(CLEAR);
1436 * rotate windows: if upwards TRUE the second window becomes the first one
1437 * if upwards FALSE the first window becomes the second one
1439 static void
1440 win_rotate(upwards, count)
1441 int upwards;
1442 int count;
1444 win_T *wp1;
1445 win_T *wp2;
1446 frame_T *frp;
1447 int n;
1449 if (firstwin == lastwin) /* nothing to do */
1451 beep_flush();
1452 return;
1455 #ifdef FEAT_GUI
1456 need_mouse_correct = TRUE;
1457 #endif
1459 #ifdef FEAT_VERTSPLIT
1460 /* Check if all frames in this row/col have one window. */
1461 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1462 frp = frp->fr_next)
1463 if (frp->fr_win == NULL)
1465 EMSG(_("E443: Cannot rotate when another window is split"));
1466 return;
1468 #endif
1470 while (count--)
1472 if (upwards) /* first window becomes last window */
1474 /* remove first window/frame from the list */
1475 frp = curwin->w_frame->fr_parent->fr_child;
1476 wp1 = frp->fr_win;
1477 win_remove(wp1, NULL);
1478 frame_remove(frp);
1480 /* find last frame and append removed window/frame after it */
1481 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1483 win_append(frp->fr_win, wp1);
1484 frame_append(frp, wp1->w_frame);
1486 wp2 = frp->fr_win; /* previously last window */
1488 else /* last window becomes first window */
1490 /* find last window/frame in the list and remove it */
1491 for (frp = curwin->w_frame; frp->fr_next != NULL;
1492 frp = frp->fr_next)
1494 wp1 = frp->fr_win;
1495 wp2 = wp1->w_prev; /* will become last window */
1496 win_remove(wp1, NULL);
1497 frame_remove(frp);
1499 /* append the removed window/frame before the first in the list */
1500 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1501 frame_insert(frp->fr_parent->fr_child, frp);
1504 /* exchange status height and vsep width of old and new last window */
1505 n = wp2->w_status_height;
1506 wp2->w_status_height = wp1->w_status_height;
1507 wp1->w_status_height = n;
1508 frame_fix_height(wp1);
1509 frame_fix_height(wp2);
1510 #ifdef FEAT_VERTSPLIT
1511 n = wp2->w_vsep_width;
1512 wp2->w_vsep_width = wp1->w_vsep_width;
1513 wp1->w_vsep_width = n;
1514 frame_fix_width(wp1);
1515 frame_fix_width(wp2);
1516 #endif
1518 /* recompute w_winrow and w_wincol for all windows */
1519 (void)win_comp_pos();
1522 redraw_later(CLEAR);
1526 * Move the current window to the very top/bottom/left/right of the screen.
1528 static void
1529 win_totop(size, flags)
1530 int size;
1531 int flags;
1533 int dir;
1534 int height = curwin->w_height;
1536 if (lastwin == firstwin)
1538 beep_flush();
1539 return;
1542 /* Remove the window and frame from the tree of frames. */
1543 (void)winframe_remove(curwin, &dir, NULL);
1544 win_remove(curwin, NULL);
1545 last_status(FALSE); /* may need to remove last status line */
1546 (void)win_comp_pos(); /* recompute window positions */
1548 /* Split a window on the desired side and put the window there. */
1549 (void)win_split_ins(size, flags, curwin, dir);
1550 if (!(flags & WSP_VERT))
1552 win_setheight(height);
1553 if (p_ea)
1554 win_equal(curwin, TRUE, 'v');
1557 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1558 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1559 * scrollbars. Have to update them anyway. */
1560 if (gui.in_use)
1562 out_flush();
1563 gui_init_which_components(NULL);
1564 gui_update_scrollbars(TRUE);
1566 need_mouse_correct = TRUE;
1567 #endif
1572 * Move window "win1" to below/right of "win2" and make "win1" the current
1573 * window. Only works within the same frame!
1575 void
1576 win_move_after(win1, win2)
1577 win_T *win1, *win2;
1579 int height;
1581 /* check if the arguments are reasonable */
1582 if (win1 == win2)
1583 return;
1585 /* check if there is something to do */
1586 if (win2->w_next != win1)
1588 /* may need move the status line/vertical separator of the last window
1589 * */
1590 if (win1 == lastwin)
1592 height = win1->w_prev->w_status_height;
1593 win1->w_prev->w_status_height = win1->w_status_height;
1594 win1->w_status_height = height;
1595 #ifdef FEAT_VERTSPLIT
1596 if (win1->w_prev->w_vsep_width == 1)
1598 /* Remove the vertical separator from the last-but-one window,
1599 * add it to the last window. Adjust the frame widths. */
1600 win1->w_prev->w_vsep_width = 0;
1601 win1->w_prev->w_frame->fr_width -= 1;
1602 win1->w_vsep_width = 1;
1603 win1->w_frame->fr_width += 1;
1605 #endif
1607 else if (win2 == lastwin)
1609 height = win1->w_status_height;
1610 win1->w_status_height = win2->w_status_height;
1611 win2->w_status_height = height;
1612 #ifdef FEAT_VERTSPLIT
1613 if (win1->w_vsep_width == 1)
1615 /* Remove the vertical separator from win1, add it to the last
1616 * window, win2. Adjust the frame widths. */
1617 win2->w_vsep_width = 1;
1618 win2->w_frame->fr_width += 1;
1619 win1->w_vsep_width = 0;
1620 win1->w_frame->fr_width -= 1;
1622 #endif
1624 win_remove(win1, NULL);
1625 frame_remove(win1->w_frame);
1626 win_append(win2, win1);
1627 frame_append(win2->w_frame, win1->w_frame);
1629 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1630 redraw_later(NOT_VALID);
1632 win_enter(win1, FALSE);
1636 * Make all windows the same height.
1637 * 'next_curwin' will soon be the current window, make sure it has enough
1638 * rows.
1640 void
1641 win_equal(next_curwin, current, dir)
1642 win_T *next_curwin; /* pointer to current window to be or NULL */
1643 int current; /* do only frame with current window */
1644 int dir; /* 'v' for vertically, 'h' for horizontally,
1645 'b' for both, 0 for using p_ead */
1647 if (dir == 0)
1648 #ifdef FEAT_VERTSPLIT
1649 dir = *p_ead;
1650 #else
1651 dir = 'b';
1652 #endif
1653 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1654 topframe, dir, 0, tabline_height(),
1655 (int)Columns, topframe->fr_height);
1659 * Set a frame to a new position and height, spreading the available room
1660 * equally over contained frames.
1661 * The window "next_curwin" (if not NULL) should at least get the size from
1662 * 'winheight' and 'winwidth' if possible.
1664 static void
1665 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1666 win_T *next_curwin; /* pointer to current window to be or NULL */
1667 int current; /* do only frame with current window */
1668 frame_T *topfr; /* frame to set size off */
1669 int dir; /* 'v', 'h' or 'b', see win_equal() */
1670 int col; /* horizontal position for frame */
1671 int row; /* vertical position for frame */
1672 int width; /* new width of frame */
1673 int height; /* new height of frame */
1675 int n, m;
1676 int extra_sep = 0;
1677 int wincount, totwincount = 0;
1678 frame_T *fr;
1679 int next_curwin_size = 0;
1680 int room = 0;
1681 int new_size;
1682 int has_next_curwin = 0;
1683 int hnc;
1685 if (topfr->fr_layout == FR_LEAF)
1687 /* Set the width/height of this frame.
1688 * Redraw when size or position changes */
1689 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1690 #ifdef FEAT_VERTSPLIT
1691 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1692 #endif
1695 topfr->fr_win->w_winrow = row;
1696 frame_new_height(topfr, height, FALSE, FALSE);
1697 #ifdef FEAT_VERTSPLIT
1698 topfr->fr_win->w_wincol = col;
1699 frame_new_width(topfr, width, FALSE, FALSE);
1700 #endif
1701 redraw_all_later(CLEAR);
1704 #ifdef FEAT_VERTSPLIT
1705 else if (topfr->fr_layout == FR_ROW)
1707 topfr->fr_width = width;
1708 topfr->fr_height = height;
1710 if (dir != 'v') /* equalize frame widths */
1712 /* Compute the maximum number of windows horizontally in this
1713 * frame. */
1714 n = frame_minwidth(topfr, NOWIN);
1715 /* add one for the rightmost window, it doesn't have a separator */
1716 if (col + width == Columns)
1717 extra_sep = 1;
1718 else
1719 extra_sep = 0;
1720 totwincount = (n + extra_sep) / (p_wmw + 1);
1721 has_next_curwin = frame_has_win(topfr, next_curwin);
1724 * Compute width for "next_curwin" window and room available for
1725 * other windows.
1726 * "m" is the minimal width when counting p_wiw for "next_curwin".
1728 m = frame_minwidth(topfr, next_curwin);
1729 room = width - m;
1730 if (room < 0)
1732 next_curwin_size = p_wiw + room;
1733 room = 0;
1735 else
1737 next_curwin_size = -1;
1738 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1740 /* If 'winfixwidth' set keep the window width if
1741 * possible.
1742 * Watch out for this window being the next_curwin. */
1743 if (frame_fixed_width(fr))
1745 n = frame_minwidth(fr, NOWIN);
1746 new_size = fr->fr_width;
1747 if (frame_has_win(fr, next_curwin))
1749 room += p_wiw - p_wmw;
1750 next_curwin_size = 0;
1751 if (new_size < p_wiw)
1752 new_size = p_wiw;
1754 else
1755 /* These windows don't use up room. */
1756 totwincount -= (n + (fr->fr_next == NULL
1757 ? extra_sep : 0)) / (p_wmw + 1);
1758 room -= new_size - n;
1759 if (room < 0)
1761 new_size += room;
1762 room = 0;
1764 fr->fr_newwidth = new_size;
1767 if (next_curwin_size == -1)
1769 if (!has_next_curwin)
1770 next_curwin_size = 0;
1771 else if (totwincount > 1
1772 && (room + (totwincount - 2))
1773 / (totwincount - 1) > p_wiw)
1775 /* Can make all windows wider than 'winwidth', spread
1776 * the room equally. */
1777 next_curwin_size = (room + p_wiw
1778 + (totwincount - 1) * p_wmw
1779 + (totwincount - 1)) / totwincount;
1780 room -= next_curwin_size - p_wiw;
1782 else
1783 next_curwin_size = p_wiw;
1787 if (has_next_curwin)
1788 --totwincount; /* don't count curwin */
1791 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1793 n = m = 0;
1794 wincount = 1;
1795 if (fr->fr_next == NULL)
1796 /* last frame gets all that remains (avoid roundoff error) */
1797 new_size = width;
1798 else if (dir == 'v')
1799 new_size = fr->fr_width;
1800 else if (frame_fixed_width(fr))
1802 new_size = fr->fr_newwidth;
1803 wincount = 0; /* doesn't count as a sizeable window */
1805 else
1807 /* Compute the maximum number of windows horiz. in "fr". */
1808 n = frame_minwidth(fr, NOWIN);
1809 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1810 / (p_wmw + 1);
1811 m = frame_minwidth(fr, next_curwin);
1812 if (has_next_curwin)
1813 hnc = frame_has_win(fr, next_curwin);
1814 else
1815 hnc = FALSE;
1816 if (hnc) /* don't count next_curwin */
1817 --wincount;
1818 if (totwincount == 0)
1819 new_size = room;
1820 else
1821 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1822 / totwincount;
1823 if (hnc) /* add next_curwin size */
1825 next_curwin_size -= p_wiw - (m - n);
1826 new_size += next_curwin_size;
1827 room -= new_size - next_curwin_size;
1829 else
1830 room -= new_size;
1831 new_size += n;
1834 /* Skip frame that is full width when splitting or closing a
1835 * window, unless equalizing all frames. */
1836 if (!current || dir != 'v' || topfr->fr_parent != NULL
1837 || (new_size != fr->fr_width)
1838 || frame_has_win(fr, next_curwin))
1839 win_equal_rec(next_curwin, current, fr, dir, col, row,
1840 new_size, height);
1841 col += new_size;
1842 width -= new_size;
1843 totwincount -= wincount;
1846 #endif
1847 else /* topfr->fr_layout == FR_COL */
1849 #ifdef FEAT_VERTSPLIT
1850 topfr->fr_width = width;
1851 #endif
1852 topfr->fr_height = height;
1854 if (dir != 'h') /* equalize frame heights */
1856 /* Compute maximum number of windows vertically in this frame. */
1857 n = frame_minheight(topfr, NOWIN);
1858 /* add one for the bottom window if it doesn't have a statusline */
1859 if (row + height == cmdline_row && p_ls == 0)
1860 extra_sep = 1;
1861 else
1862 extra_sep = 0;
1863 totwincount = (n + extra_sep) / (p_wmh + 1);
1864 has_next_curwin = frame_has_win(topfr, next_curwin);
1867 * Compute height for "next_curwin" window and room available for
1868 * other windows.
1869 * "m" is the minimal height when counting p_wh for "next_curwin".
1871 m = frame_minheight(topfr, next_curwin);
1872 room = height - m;
1873 if (room < 0)
1875 /* The room is less then 'winheight', use all space for the
1876 * current window. */
1877 next_curwin_size = p_wh + room;
1878 room = 0;
1880 else
1882 next_curwin_size = -1;
1883 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1885 /* If 'winfixheight' set keep the window height if
1886 * possible.
1887 * Watch out for this window being the next_curwin. */
1888 if (frame_fixed_height(fr))
1890 n = frame_minheight(fr, NOWIN);
1891 new_size = fr->fr_height;
1892 if (frame_has_win(fr, next_curwin))
1894 room += p_wh - p_wmh;
1895 next_curwin_size = 0;
1896 if (new_size < p_wh)
1897 new_size = p_wh;
1899 else
1900 /* These windows don't use up room. */
1901 totwincount -= (n + (fr->fr_next == NULL
1902 ? extra_sep : 0)) / (p_wmh + 1);
1903 room -= new_size - n;
1904 if (room < 0)
1906 new_size += room;
1907 room = 0;
1909 fr->fr_newheight = new_size;
1912 if (next_curwin_size == -1)
1914 if (!has_next_curwin)
1915 next_curwin_size = 0;
1916 else if (totwincount > 1
1917 && (room + (totwincount - 2))
1918 / (totwincount - 1) > p_wh)
1920 /* can make all windows higher than 'winheight',
1921 * spread the room equally. */
1922 next_curwin_size = (room + p_wh
1923 + (totwincount - 1) * p_wmh
1924 + (totwincount - 1)) / totwincount;
1925 room -= next_curwin_size - p_wh;
1927 else
1928 next_curwin_size = p_wh;
1932 if (has_next_curwin)
1933 --totwincount; /* don't count curwin */
1936 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1938 n = m = 0;
1939 wincount = 1;
1940 if (fr->fr_next == NULL)
1941 /* last frame gets all that remains (avoid roundoff error) */
1942 new_size = height;
1943 else if (dir == 'h')
1944 new_size = fr->fr_height;
1945 else if (frame_fixed_height(fr))
1947 new_size = fr->fr_newheight;
1948 wincount = 0; /* doesn't count as a sizeable window */
1950 else
1952 /* Compute the maximum number of windows vert. in "fr". */
1953 n = frame_minheight(fr, NOWIN);
1954 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1955 / (p_wmh + 1);
1956 m = frame_minheight(fr, next_curwin);
1957 if (has_next_curwin)
1958 hnc = frame_has_win(fr, next_curwin);
1959 else
1960 hnc = FALSE;
1961 if (hnc) /* don't count next_curwin */
1962 --wincount;
1963 if (totwincount == 0)
1964 new_size = room;
1965 else
1966 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1967 / totwincount;
1968 if (hnc) /* add next_curwin size */
1970 next_curwin_size -= p_wh - (m - n);
1971 new_size += next_curwin_size;
1972 room -= new_size - next_curwin_size;
1974 else
1975 room -= new_size;
1976 new_size += n;
1978 /* Skip frame that is full width when splitting or closing a
1979 * window, unless equalizing all frames. */
1980 if (!current || dir != 'h' || topfr->fr_parent != NULL
1981 || (new_size != fr->fr_height)
1982 || frame_has_win(fr, next_curwin))
1983 win_equal_rec(next_curwin, current, fr, dir, col, row,
1984 width, new_size);
1985 row += new_size;
1986 height -= new_size;
1987 totwincount -= wincount;
1993 * close all windows for buffer 'buf'
1995 void
1996 close_windows(buf, keep_curwin)
1997 buf_T *buf;
1998 int keep_curwin; /* don't close "curwin" */
2000 win_T *wp;
2001 tabpage_T *tp, *nexttp;
2002 int h = tabline_height();
2004 ++RedrawingDisabled;
2006 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2008 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2010 win_close(wp, FALSE);
2012 /* Start all over, autocommands may change the window layout. */
2013 wp = firstwin;
2015 else
2016 wp = wp->w_next;
2019 /* Also check windows in other tab pages. */
2020 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2022 nexttp = tp->tp_next;
2023 if (tp != curtab)
2024 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2025 if (wp->w_buffer == buf)
2027 win_close_othertab(wp, FALSE, tp);
2029 /* Start all over, the tab page may be closed and
2030 * autocommands may change the window layout. */
2031 nexttp = first_tabpage;
2032 break;
2036 --RedrawingDisabled;
2038 if (h != tabline_height())
2039 shell_new_rows();
2043 * Return TRUE if the current window is the only window that exists.
2044 * Returns FALSE if there is a window, possibly in another tab page.
2046 static int
2047 last_window()
2049 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2053 * Close window "win". Only works for the current tab page.
2054 * If "free_buf" is TRUE related buffer may be unloaded.
2056 * called by :quit, :close, :xit, :wq and findtag()
2058 void
2059 win_close(win, free_buf)
2060 win_T *win;
2061 int free_buf;
2063 win_T *wp;
2064 #ifdef FEAT_AUTOCMD
2065 int other_buffer = FALSE;
2066 #endif
2067 int close_curwin = FALSE;
2068 int dir;
2069 int help_window = FALSE;
2070 tabpage_T *prev_curtab = curtab;
2072 if (last_window())
2074 EMSG(_("E444: Cannot close last window"));
2075 return;
2079 * When closing the last window in a tab page first go to another tab
2080 * page and then close the window and the tab page. This avoids that
2081 * curwin and curtab are not invalid while we are freeing memory, they may
2082 * be used in GUI events.
2084 if (firstwin == lastwin)
2086 goto_tabpage_tp(alt_tabpage());
2087 redraw_tabline = TRUE;
2089 /* Safety check: Autocommands may have closed the window when jumping
2090 * to the other tab page. */
2091 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2093 int h = tabline_height();
2095 win_close_othertab(win, free_buf, prev_curtab);
2096 if (h != tabline_height())
2097 shell_new_rows();
2099 return;
2102 /* When closing the help window, try restoring a snapshot after closing
2103 * the window. Otherwise clear the snapshot, it's now invalid. */
2104 if (win->w_buffer->b_help)
2105 help_window = TRUE;
2106 else
2107 clear_snapshot(curtab);
2109 #ifdef FEAT_AUTOCMD
2110 if (win == curwin)
2113 * Guess which window is going to be the new current window.
2114 * This may change because of the autocommands (sigh).
2116 wp = frame2win(win_altframe(win, NULL));
2119 * Be careful: If autocommands delete the window, return now.
2121 if (wp->w_buffer != curbuf)
2123 other_buffer = TRUE;
2124 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2125 if (!win_valid(win) || last_window())
2126 return;
2128 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2129 if (!win_valid(win) || last_window())
2130 return;
2131 # ifdef FEAT_EVAL
2132 /* autocmds may abort script processing */
2133 if (aborting())
2134 return;
2135 # endif
2137 #endif
2139 #ifdef FEAT_GUI
2140 /* Avoid trouble with scrollbars that are going to be deleted in
2141 * win_free(). */
2142 if (gui.in_use)
2143 out_flush();
2144 #endif
2147 * Close the link to the buffer.
2149 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2151 /* Autocommands may have closed the window already, or closed the only
2152 * other window or moved to another tab page. */
2153 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2154 return;
2156 /* Free the memory used for the window. */
2157 wp = win_free_mem(win, &dir, NULL);
2159 /* Make sure curwin isn't invalid. It can cause severe trouble when
2160 * printing an error message. For win_equal() curbuf needs to be valid
2161 * too. */
2162 if (win == curwin)
2164 curwin = wp;
2165 #ifdef FEAT_QUICKFIX
2166 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2169 * If the cursor goes to the preview or the quickfix window, try
2170 * finding another window to go to.
2172 for (;;)
2174 if (wp->w_next == NULL)
2175 wp = firstwin;
2176 else
2177 wp = wp->w_next;
2178 if (wp == curwin)
2179 break;
2180 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2182 curwin = wp;
2183 break;
2187 #endif
2188 curbuf = curwin->w_buffer;
2189 close_curwin = TRUE;
2191 if (p_ea
2192 #ifdef FEAT_VERTSPLIT
2193 && (*p_ead == 'b' || *p_ead == dir)
2194 #endif
2196 win_equal(curwin, TRUE,
2197 #ifdef FEAT_VERTSPLIT
2199 #else
2201 #endif
2203 else
2204 win_comp_pos();
2205 if (close_curwin)
2207 win_enter_ext(wp, FALSE, TRUE);
2208 #ifdef FEAT_AUTOCMD
2209 if (other_buffer)
2210 /* careful: after this wp and win may be invalid! */
2211 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2212 #endif
2216 * If last window has a status line now and we don't want one,
2217 * remove the status line.
2219 last_status(FALSE);
2221 /* After closing the help window, try restoring the window layout from
2222 * before it was opened. */
2223 if (help_window)
2224 restore_snapshot(close_curwin);
2226 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2227 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2228 if (gui.in_use && !win_hasvertsplit())
2229 gui_init_which_components(NULL);
2230 #endif
2232 redraw_all_later(NOT_VALID);
2236 * Close window "win" in tab page "tp", which is not the current tab page.
2237 * This may be the last window ih that tab page and result in closing the tab,
2238 * thus "tp" may become invalid!
2239 * Caller must check if buffer is hidden and whether the tabline needs to be
2240 * updated.
2242 void
2243 win_close_othertab(win, free_buf, tp)
2244 win_T *win;
2245 int free_buf;
2246 tabpage_T *tp;
2248 win_T *wp;
2249 int dir;
2250 tabpage_T *ptp = NULL;
2252 /* Close the link to the buffer. */
2253 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2255 /* Careful: Autocommands may have closed the tab page or made it the
2256 * current tab page. */
2257 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2259 if (ptp == NULL || tp == curtab)
2260 return;
2262 /* Autocommands may have closed the window already. */
2263 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2265 if (wp == NULL)
2266 return;
2268 /* Free the memory used for the window. */
2269 wp = win_free_mem(win, &dir, tp);
2271 /* When closing the last window in a tab page remove the tab page. */
2272 if (wp == NULL)
2274 if (tp == first_tabpage)
2275 first_tabpage = tp->tp_next;
2276 else
2278 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2279 ptp = ptp->tp_next)
2281 if (ptp == NULL)
2283 EMSG2(_(e_intern2), "win_close_othertab()");
2284 return;
2286 ptp->tp_next = tp->tp_next;
2288 free_tabpage(tp);
2293 * Free the memory used for a window.
2294 * Returns a pointer to the window that got the freed up space.
2296 static win_T *
2297 win_free_mem(win, dirp, tp)
2298 win_T *win;
2299 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2300 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2302 frame_T *frp;
2303 win_T *wp;
2305 #ifdef FEAT_FOLDING
2306 clearFolding(win);
2307 #endif
2309 /* reduce the reference count to the argument list. */
2310 alist_unlink(win->w_alist);
2312 /* Remove the window and its frame from the tree of frames. */
2313 frp = win->w_frame;
2314 wp = winframe_remove(win, dirp, tp);
2315 vim_free(frp);
2316 win_free(win, tp);
2318 /* When deleting the current window of another tab page select a new
2319 * current window. */
2320 if (tp != NULL && win == tp->tp_curwin)
2321 tp->tp_curwin = wp;
2323 return wp;
2326 #if defined(EXITFREE) || defined(PROTO)
2327 void
2328 win_free_all()
2330 int dummy;
2332 # ifdef FEAT_WINDOWS
2333 while (first_tabpage->tp_next != NULL)
2334 tabpage_close(TRUE);
2335 # endif
2337 while (firstwin != NULL)
2338 (void)win_free_mem(firstwin, &dummy, NULL);
2340 #endif
2343 * Remove a window and its frame from the tree of frames.
2344 * Returns a pointer to the window that got the freed up space.
2346 /*ARGSUSED*/
2347 static win_T *
2348 winframe_remove(win, dirp, tp)
2349 win_T *win;
2350 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2351 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2353 frame_T *frp, *frp2, *frp3;
2354 frame_T *frp_close = win->w_frame;
2355 win_T *wp;
2358 * If there is only one window there is nothing to remove.
2360 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2361 return NULL;
2364 * Remove the window from its frame.
2366 frp2 = win_altframe(win, tp);
2367 wp = frame2win(frp2);
2369 /* Remove this frame from the list of frames. */
2370 frame_remove(frp_close);
2372 #ifdef FEAT_VERTSPLIT
2373 if (frp_close->fr_parent->fr_layout == FR_COL)
2375 #endif
2376 /* When 'winfixheight' is set, try to find another frame in the column
2377 * (as close to the closed frame as possible) to distribute the height
2378 * to. */
2379 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2381 frp = frp_close->fr_prev;
2382 frp3 = frp_close->fr_next;
2383 while (frp != NULL || frp3 != NULL)
2385 if (frp != NULL)
2387 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2389 frp2 = frp;
2390 wp = frp->fr_win;
2391 break;
2393 frp = frp->fr_prev;
2395 if (frp3 != NULL)
2397 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2399 frp2 = frp3;
2400 wp = frp3->fr_win;
2401 break;
2403 frp3 = frp3->fr_next;
2407 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2408 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2409 #ifdef FEAT_VERTSPLIT
2410 *dirp = 'v';
2412 else
2414 /* When 'winfixwidth' is set, try to find another frame in the column
2415 * (as close to the closed frame as possible) to distribute the width
2416 * to. */
2417 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2419 frp = frp_close->fr_prev;
2420 frp3 = frp_close->fr_next;
2421 while (frp != NULL || frp3 != NULL)
2423 if (frp != NULL)
2425 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2427 frp2 = frp;
2428 wp = frp->fr_win;
2429 break;
2431 frp = frp->fr_prev;
2433 if (frp3 != NULL)
2435 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2437 frp2 = frp3;
2438 wp = frp3->fr_win;
2439 break;
2441 frp3 = frp3->fr_next;
2445 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2446 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2447 *dirp = 'h';
2449 #endif
2451 /* If rows/columns go to a window below/right its positions need to be
2452 * updated. Can only be done after the sizes have been updated. */
2453 if (frp2 == frp_close->fr_next)
2455 int row = win->w_winrow;
2456 int col = W_WINCOL(win);
2458 frame_comp_pos(frp2, &row, &col);
2461 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2463 /* There is no other frame in this list, move its info to the parent
2464 * and remove it. */
2465 frp2->fr_parent->fr_layout = frp2->fr_layout;
2466 frp2->fr_parent->fr_child = frp2->fr_child;
2467 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2468 frp->fr_parent = frp2->fr_parent;
2469 frp2->fr_parent->fr_win = frp2->fr_win;
2470 if (frp2->fr_win != NULL)
2471 frp2->fr_win->w_frame = frp2->fr_parent;
2472 frp = frp2->fr_parent;
2473 vim_free(frp2);
2475 frp2 = frp->fr_parent;
2476 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2478 /* The frame above the parent has the same layout, have to merge
2479 * the frames into this list. */
2480 if (frp2->fr_child == frp)
2481 frp2->fr_child = frp->fr_child;
2482 frp->fr_child->fr_prev = frp->fr_prev;
2483 if (frp->fr_prev != NULL)
2484 frp->fr_prev->fr_next = frp->fr_child;
2485 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2487 frp3->fr_parent = frp2;
2488 if (frp3->fr_next == NULL)
2490 frp3->fr_next = frp->fr_next;
2491 if (frp->fr_next != NULL)
2492 frp->fr_next->fr_prev = frp3;
2493 break;
2496 vim_free(frp);
2500 return wp;
2504 * Find out which frame is going to get the freed up space when "win" is
2505 * closed.
2506 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2507 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2508 * This makes opening a window and closing it immediately keep the same window
2509 * layout.
2511 static frame_T *
2512 win_altframe(win, tp)
2513 win_T *win;
2514 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2516 frame_T *frp;
2517 int b;
2519 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2520 /* Last window in this tab page, will go to next tab page. */
2521 return alt_tabpage()->tp_curwin->w_frame;
2523 frp = win->w_frame;
2524 #ifdef FEAT_VERTSPLIT
2525 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2526 b = p_spr;
2527 else
2528 #endif
2529 b = p_sb;
2530 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2531 return frp->fr_next;
2532 return frp->fr_prev;
2536 * Return the tabpage that will be used if the current one is closed.
2538 static tabpage_T *
2539 alt_tabpage()
2541 tabpage_T *tp;
2543 /* Use the next tab page if possible. */
2544 if (curtab->tp_next != NULL)
2545 return curtab->tp_next;
2547 /* Find the last but one tab page. */
2548 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2550 return tp;
2554 * Find the left-upper window in frame "frp".
2556 static win_T *
2557 frame2win(frp)
2558 frame_T *frp;
2560 while (frp->fr_win == NULL)
2561 frp = frp->fr_child;
2562 return frp->fr_win;
2566 * Return TRUE if frame "frp" contains window "wp".
2568 static int
2569 frame_has_win(frp, wp)
2570 frame_T *frp;
2571 win_T *wp;
2573 frame_T *p;
2575 if (frp->fr_layout == FR_LEAF)
2576 return frp->fr_win == wp;
2578 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2579 if (frame_has_win(p, wp))
2580 return TRUE;
2581 return FALSE;
2585 * Set a new height for a frame. Recursively sets the height for contained
2586 * frames and windows. Caller must take care of positions.
2588 static void
2589 frame_new_height(topfrp, height, topfirst, wfh)
2590 frame_T *topfrp;
2591 int height;
2592 int topfirst; /* resize topmost contained frame first */
2593 int wfh; /* obey 'winfixheight' when there is a choice;
2594 may cause the height not to be set */
2596 frame_T *frp;
2597 int extra_lines;
2598 int h;
2600 if (topfrp->fr_win != NULL)
2602 /* Simple case: just one window. */
2603 win_new_height(topfrp->fr_win,
2604 height - topfrp->fr_win->w_status_height);
2606 #ifdef FEAT_VERTSPLIT
2607 else if (topfrp->fr_layout == FR_ROW)
2611 /* All frames in this row get the same new height. */
2612 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2614 frame_new_height(frp, height, topfirst, wfh);
2615 if (frp->fr_height > height)
2617 /* Could not fit the windows, make the whole row higher. */
2618 height = frp->fr_height;
2619 break;
2623 while (frp != NULL);
2625 #endif
2626 else /* fr_layout == FR_COL */
2628 /* Complicated case: Resize a column of frames. Resize the bottom
2629 * frame first, frames above that when needed. */
2631 frp = topfrp->fr_child;
2632 if (wfh)
2633 /* Advance past frames with one window with 'wfh' set. */
2634 while (frame_fixed_height(frp))
2636 frp = frp->fr_next;
2637 if (frp == NULL)
2638 return; /* no frame without 'wfh', give up */
2640 if (!topfirst)
2642 /* Find the bottom frame of this column */
2643 while (frp->fr_next != NULL)
2644 frp = frp->fr_next;
2645 if (wfh)
2646 /* Advance back for frames with one window with 'wfh' set. */
2647 while (frame_fixed_height(frp))
2648 frp = frp->fr_prev;
2651 extra_lines = height - topfrp->fr_height;
2652 if (extra_lines < 0)
2654 /* reduce height of contained frames, bottom or top frame first */
2655 while (frp != NULL)
2657 h = frame_minheight(frp, NULL);
2658 if (frp->fr_height + extra_lines < h)
2660 extra_lines += frp->fr_height - h;
2661 frame_new_height(frp, h, topfirst, wfh);
2663 else
2665 frame_new_height(frp, frp->fr_height + extra_lines,
2666 topfirst, wfh);
2667 break;
2669 if (topfirst)
2672 frp = frp->fr_next;
2673 while (wfh && frp != NULL && frame_fixed_height(frp));
2675 else
2678 frp = frp->fr_prev;
2679 while (wfh && frp != NULL && frame_fixed_height(frp));
2681 /* Increase "height" if we could not reduce enough frames. */
2682 if (frp == NULL)
2683 height -= extra_lines;
2686 else if (extra_lines > 0)
2688 /* increase height of bottom or top frame */
2689 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2692 topfrp->fr_height = height;
2696 * Return TRUE if height of frame "frp" should not be changed because of
2697 * the 'winfixheight' option.
2699 static int
2700 frame_fixed_height(frp)
2701 frame_T *frp;
2703 /* frame with one window: fixed height if 'winfixheight' set. */
2704 if (frp->fr_win != NULL)
2705 return frp->fr_win->w_p_wfh;
2707 if (frp->fr_layout == FR_ROW)
2709 /* The frame is fixed height if one of the frames in the row is fixed
2710 * height. */
2711 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2712 if (frame_fixed_height(frp))
2713 return TRUE;
2714 return FALSE;
2717 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2718 * frames in the row are fixed height. */
2719 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2720 if (!frame_fixed_height(frp))
2721 return FALSE;
2722 return TRUE;
2725 #ifdef FEAT_VERTSPLIT
2727 * Return TRUE if width of frame "frp" should not be changed because of
2728 * the 'winfixwidth' option.
2730 static int
2731 frame_fixed_width(frp)
2732 frame_T *frp;
2734 /* frame with one window: fixed width if 'winfixwidth' set. */
2735 if (frp->fr_win != NULL)
2736 return frp->fr_win->w_p_wfw;
2738 if (frp->fr_layout == FR_COL)
2740 /* The frame is fixed width if one of the frames in the row is fixed
2741 * width. */
2742 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2743 if (frame_fixed_width(frp))
2744 return TRUE;
2745 return FALSE;
2748 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2749 * frames in the row are fixed width. */
2750 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2751 if (!frame_fixed_width(frp))
2752 return FALSE;
2753 return TRUE;
2757 * Add a status line to windows at the bottom of "frp".
2758 * Note: Does not check if there is room!
2760 static void
2761 frame_add_statusline(frp)
2762 frame_T *frp;
2764 win_T *wp;
2766 if (frp->fr_layout == FR_LEAF)
2768 wp = frp->fr_win;
2769 if (wp->w_status_height == 0)
2771 if (wp->w_height > 0) /* don't make it negative */
2772 --wp->w_height;
2773 wp->w_status_height = STATUS_HEIGHT;
2776 else if (frp->fr_layout == FR_ROW)
2778 /* Handle all the frames in the row. */
2779 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2780 frame_add_statusline(frp);
2782 else /* frp->fr_layout == FR_COL */
2784 /* Only need to handle the last frame in the column. */
2785 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2787 frame_add_statusline(frp);
2792 * Set width of a frame. Handles recursively going through contained frames.
2793 * May remove separator line for windows at the right side (for win_close()).
2795 static void
2796 frame_new_width(topfrp, width, leftfirst, wfw)
2797 frame_T *topfrp;
2798 int width;
2799 int leftfirst; /* resize leftmost contained frame first */
2800 int wfw; /* obey 'winfixwidth' when there is a choice;
2801 may cause the width not to be set */
2803 frame_T *frp;
2804 int extra_cols;
2805 int w;
2806 win_T *wp;
2808 if (topfrp->fr_layout == FR_LEAF)
2810 /* Simple case: just one window. */
2811 wp = topfrp->fr_win;
2812 /* Find out if there are any windows right of this one. */
2813 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2814 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2815 break;
2816 if (frp->fr_parent == NULL)
2817 wp->w_vsep_width = 0;
2818 win_new_width(wp, width - wp->w_vsep_width);
2820 else if (topfrp->fr_layout == FR_COL)
2824 /* All frames in this column get the same new width. */
2825 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2827 frame_new_width(frp, width, leftfirst, wfw);
2828 if (frp->fr_width > width)
2830 /* Could not fit the windows, make whole column wider. */
2831 width = frp->fr_width;
2832 break;
2835 } while (frp != NULL);
2837 else /* fr_layout == FR_ROW */
2839 /* Complicated case: Resize a row of frames. Resize the rightmost
2840 * frame first, frames left of it when needed. */
2842 frp = topfrp->fr_child;
2843 if (wfw)
2844 /* Advance past frames with one window with 'wfw' set. */
2845 while (frame_fixed_width(frp))
2847 frp = frp->fr_next;
2848 if (frp == NULL)
2849 return; /* no frame without 'wfw', give up */
2851 if (!leftfirst)
2853 /* Find the rightmost frame of this row */
2854 while (frp->fr_next != NULL)
2855 frp = frp->fr_next;
2856 if (wfw)
2857 /* Advance back for frames with one window with 'wfw' set. */
2858 while (frame_fixed_width(frp))
2859 frp = frp->fr_prev;
2862 extra_cols = width - topfrp->fr_width;
2863 if (extra_cols < 0)
2865 /* reduce frame width, rightmost frame first */
2866 while (frp != NULL)
2868 w = frame_minwidth(frp, NULL);
2869 if (frp->fr_width + extra_cols < w)
2871 extra_cols += frp->fr_width - w;
2872 frame_new_width(frp, w, leftfirst, wfw);
2874 else
2876 frame_new_width(frp, frp->fr_width + extra_cols,
2877 leftfirst, wfw);
2878 break;
2880 if (leftfirst)
2883 frp = frp->fr_next;
2884 while (wfw && frp != NULL && frame_fixed_width(frp));
2886 else
2889 frp = frp->fr_prev;
2890 while (wfw && frp != NULL && frame_fixed_width(frp));
2892 /* Increase "width" if we could not reduce enough frames. */
2893 if (frp == NULL)
2894 width -= extra_cols;
2897 else if (extra_cols > 0)
2899 /* increase width of rightmost frame */
2900 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2903 topfrp->fr_width = width;
2907 * Add the vertical separator to windows at the right side of "frp".
2908 * Note: Does not check if there is room!
2910 static void
2911 frame_add_vsep(frp)
2912 frame_T *frp;
2914 win_T *wp;
2916 if (frp->fr_layout == FR_LEAF)
2918 wp = frp->fr_win;
2919 if (wp->w_vsep_width == 0)
2921 if (wp->w_width > 0) /* don't make it negative */
2922 --wp->w_width;
2923 wp->w_vsep_width = 1;
2926 else if (frp->fr_layout == FR_COL)
2928 /* Handle all the frames in the column. */
2929 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2930 frame_add_vsep(frp);
2932 else /* frp->fr_layout == FR_ROW */
2934 /* Only need to handle the last frame in the row. */
2935 frp = frp->fr_child;
2936 while (frp->fr_next != NULL)
2937 frp = frp->fr_next;
2938 frame_add_vsep(frp);
2943 * Set frame width from the window it contains.
2945 static void
2946 frame_fix_width(wp)
2947 win_T *wp;
2949 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2951 #endif
2954 * Set frame height from the window it contains.
2956 static void
2957 frame_fix_height(wp)
2958 win_T *wp;
2960 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2964 * Compute the minimal height for frame "topfrp".
2965 * Uses the 'winminheight' option.
2966 * When "next_curwin" isn't NULL, use p_wh for this window.
2967 * When "next_curwin" is NOWIN, don't use at least one line for the current
2968 * window.
2970 static int
2971 frame_minheight(topfrp, next_curwin)
2972 frame_T *topfrp;
2973 win_T *next_curwin;
2975 frame_T *frp;
2976 int m;
2977 #ifdef FEAT_VERTSPLIT
2978 int n;
2979 #endif
2981 if (topfrp->fr_win != NULL)
2983 if (topfrp->fr_win == next_curwin)
2984 m = p_wh + topfrp->fr_win->w_status_height;
2985 else
2987 /* window: minimal height of the window plus status line */
2988 m = p_wmh + topfrp->fr_win->w_status_height;
2989 /* Current window is minimal one line high */
2990 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2991 ++m;
2994 #ifdef FEAT_VERTSPLIT
2995 else if (topfrp->fr_layout == FR_ROW)
2997 /* get the minimal height from each frame in this row */
2998 m = 0;
2999 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3001 n = frame_minheight(frp, next_curwin);
3002 if (n > m)
3003 m = n;
3006 #endif
3007 else
3009 /* Add up the minimal heights for all frames in this column. */
3010 m = 0;
3011 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3012 m += frame_minheight(frp, next_curwin);
3015 return m;
3018 #ifdef FEAT_VERTSPLIT
3020 * Compute the minimal width for frame "topfrp".
3021 * When "next_curwin" isn't NULL, use p_wiw for this window.
3022 * When "next_curwin" is NOWIN, don't use at least one column for the current
3023 * window.
3025 static int
3026 frame_minwidth(topfrp, next_curwin)
3027 frame_T *topfrp;
3028 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3030 frame_T *frp;
3031 int m, n;
3033 if (topfrp->fr_win != NULL)
3035 if (topfrp->fr_win == next_curwin)
3036 m = p_wiw + topfrp->fr_win->w_vsep_width;
3037 else
3039 /* window: minimal width of the window plus separator column */
3040 m = p_wmw + topfrp->fr_win->w_vsep_width;
3041 /* Current window is minimal one column wide */
3042 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3043 ++m;
3046 else if (topfrp->fr_layout == FR_COL)
3048 /* get the minimal width from each frame in this column */
3049 m = 0;
3050 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3052 n = frame_minwidth(frp, next_curwin);
3053 if (n > m)
3054 m = n;
3057 else
3059 /* Add up the minimal widths for all frames in this row. */
3060 m = 0;
3061 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3062 m += frame_minwidth(frp, next_curwin);
3065 return m;
3067 #endif
3071 * Try to close all windows except current one.
3072 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3073 * used and the buffer was modified.
3075 * Used by ":bdel" and ":only".
3077 void
3078 close_others(message, forceit)
3079 int message;
3080 int forceit; /* always hide all other windows */
3082 win_T *wp;
3083 win_T *nextwp;
3084 int r;
3086 if (lastwin == firstwin)
3088 if (message
3089 #ifdef FEAT_AUTOCMD
3090 && !autocmd_busy
3091 #endif
3093 MSG(_(m_onlyone));
3094 return;
3097 /* Be very careful here: autocommands may change the window layout. */
3098 for (wp = firstwin; win_valid(wp); wp = nextwp)
3100 nextwp = wp->w_next;
3101 if (wp != curwin) /* don't close current window */
3104 /* Check if it's allowed to abandon this window */
3105 r = can_abandon(wp->w_buffer, forceit);
3106 #ifdef FEAT_AUTOCMD
3107 if (!win_valid(wp)) /* autocommands messed wp up */
3109 nextwp = firstwin;
3110 continue;
3112 #endif
3113 if (!r)
3115 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3116 if (message && (p_confirm || cmdmod.confirm) && p_write)
3118 dialog_changed(wp->w_buffer, FALSE);
3119 # ifdef FEAT_AUTOCMD
3120 if (!win_valid(wp)) /* autocommands messed wp up */
3122 nextwp = firstwin;
3123 continue;
3125 # endif
3127 if (bufIsChanged(wp->w_buffer))
3128 #endif
3129 continue;
3131 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3135 if (message && lastwin != firstwin)
3136 EMSG(_("E445: Other window contains changes"));
3139 #endif /* FEAT_WINDOWS */
3142 * Init the current window "curwin".
3143 * Called when a new file is being edited.
3145 void
3146 curwin_init()
3148 redraw_win_later(curwin, NOT_VALID);
3149 curwin->w_lines_valid = 0;
3150 curwin->w_cursor.lnum = 1;
3151 curwin->w_curswant = curwin->w_cursor.col = 0;
3152 #ifdef FEAT_VIRTUALEDIT
3153 curwin->w_cursor.coladd = 0;
3154 #endif
3155 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3156 curwin->w_pcmark.col = 0;
3157 curwin->w_prev_pcmark.lnum = 0;
3158 curwin->w_prev_pcmark.col = 0;
3159 curwin->w_topline = 1;
3160 #ifdef FEAT_DIFF
3161 curwin->w_topfill = 0;
3162 #endif
3163 curwin->w_botline = 2;
3164 #ifdef FEAT_FKMAP
3165 if (curwin->w_p_rl)
3166 curwin->w_farsi = W_CONV + W_R_L;
3167 else
3168 curwin->w_farsi = W_CONV;
3169 #endif
3173 * Allocate the first window and put an empty buffer in it.
3174 * Called from main().
3175 * Return FAIL when something goes wrong (out of memory).
3178 win_alloc_first()
3180 if (win_alloc_firstwin(NULL) == FAIL)
3181 return FAIL;
3183 #ifdef FEAT_WINDOWS
3184 first_tabpage = alloc_tabpage();
3185 if (first_tabpage == NULL)
3186 return FAIL;
3187 first_tabpage->tp_topframe = topframe;
3188 curtab = first_tabpage;
3189 #endif
3190 return OK;
3194 * Allocate the first window or the first window in a new tab page.
3195 * When "oldwin" is NULL create an empty buffer for it.
3196 * When "oldwin" is not NULL copy info from it to the new window (only with
3197 * FEAT_WINDOWS).
3198 * Return FAIL when something goes wrong (out of memory).
3200 static int
3201 win_alloc_firstwin(oldwin)
3202 win_T *oldwin;
3204 curwin = win_alloc(NULL);
3205 if (oldwin == NULL)
3207 /* Very first window, need to create an empty buffer for it and
3208 * initialize from scratch. */
3209 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3210 if (curwin == NULL || curbuf == NULL)
3211 return FAIL;
3212 curwin->w_buffer = curbuf;
3213 curbuf->b_nwindows = 1; /* there is one window */
3214 #ifdef FEAT_WINDOWS
3215 curwin->w_alist = &global_alist;
3216 #endif
3217 curwin_init(); /* init current window */
3219 #ifdef FEAT_WINDOWS
3220 else
3222 /* First window in new tab page, initialize it from "oldwin". */
3223 win_init(curwin, oldwin);
3225 # ifdef FEAT_SCROLLBIND
3226 /* We don't want scroll-binding in the first window. */
3227 curwin->w_p_scb = FALSE;
3228 # endif
3230 #endif
3232 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3233 if (topframe == NULL)
3234 return FAIL;
3235 topframe->fr_layout = FR_LEAF;
3236 #ifdef FEAT_VERTSPLIT
3237 topframe->fr_width = Columns;
3238 #endif
3239 topframe->fr_height = Rows - p_ch;
3240 topframe->fr_win = curwin;
3241 curwin->w_frame = topframe;
3243 return OK;
3247 * Initialize the window and frame size to the maximum.
3249 void
3250 win_init_size()
3252 firstwin->w_height = ROWS_AVAIL;
3253 topframe->fr_height = ROWS_AVAIL;
3254 #ifdef FEAT_VERTSPLIT
3255 firstwin->w_width = Columns;
3256 topframe->fr_width = Columns;
3257 #endif
3260 #if defined(FEAT_WINDOWS) || defined(PROTO)
3263 * Allocate a new tabpage_T and init the values.
3264 * Returns NULL when out of memory.
3266 static tabpage_T *
3267 alloc_tabpage()
3269 tabpage_T *tp;
3271 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3272 if (tp != NULL)
3274 # ifdef FEAT_GUI
3275 int i;
3277 for (i = 0; i < 3; i++)
3278 tp->tp_prev_which_scrollbars[i] = -1;
3279 # endif
3280 # ifdef FEAT_DIFF
3281 tp->tp_diff_invalid = TRUE;
3282 # endif
3283 #ifdef FEAT_EVAL
3284 /* init t: variables */
3285 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3286 #endif
3287 tp->tp_ch_used = p_ch;
3289 return tp;
3292 void
3293 free_tabpage(tp)
3294 tabpage_T *tp;
3296 # ifdef FEAT_DIFF
3297 diff_clear(tp);
3298 # endif
3299 clear_snapshot(tp);
3300 #ifdef FEAT_EVAL
3301 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3302 #endif
3303 vim_free(tp);
3307 * Create a new Tab page with one window.
3308 * It will edit the current buffer, like after ":split".
3309 * When "after" is 0 put it just after the current Tab page.
3310 * Otherwise put it just before tab page "after".
3311 * Return FAIL or OK.
3314 win_new_tabpage(after)
3315 int after;
3317 tabpage_T *tp = curtab;
3318 tabpage_T *newtp;
3319 int n;
3321 newtp = alloc_tabpage();
3322 if (newtp == NULL)
3323 return FAIL;
3325 /* Remember the current windows in this Tab page. */
3326 if (leave_tabpage(curbuf) == FAIL)
3328 vim_free(newtp);
3329 return FAIL;
3331 curtab = newtp;
3333 /* Create a new empty window. */
3334 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3336 /* Make the new Tab page the new topframe. */
3337 if (after == 1)
3339 /* New tab page becomes the first one. */
3340 newtp->tp_next = first_tabpage;
3341 first_tabpage = newtp;
3343 else
3345 if (after > 0)
3347 /* Put new tab page before tab page "after". */
3348 n = 2;
3349 for (tp = first_tabpage; tp->tp_next != NULL
3350 && n < after; tp = tp->tp_next)
3351 ++n;
3353 newtp->tp_next = tp->tp_next;
3354 tp->tp_next = newtp;
3356 win_init_size();
3357 firstwin->w_winrow = tabline_height();
3358 win_comp_scroll(curwin);
3360 newtp->tp_topframe = topframe;
3361 last_status(FALSE);
3363 #if defined(FEAT_GUI)
3364 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3365 * scrollbars. Have to update them anyway. */
3366 if (gui.in_use && starting == 0)
3368 gui_init_which_components(NULL);
3369 gui_update_scrollbars(TRUE);
3371 need_mouse_correct = TRUE;
3372 #endif
3374 redraw_all_later(CLEAR);
3375 #ifdef FEAT_AUTOCMD
3376 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3377 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3378 #endif
3379 return OK;
3382 /* Failed, get back the previous Tab page */
3383 enter_tabpage(curtab, curbuf);
3384 return FAIL;
3388 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3389 * like with ":split".
3390 * Returns OK if a new tab page was created, FAIL otherwise.
3393 may_open_tabpage()
3395 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3397 if (n != 0)
3399 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3400 postponed_split_tab = 0;
3401 return win_new_tabpage(n);
3403 return FAIL;
3407 * Create up to "maxcount" tabpages with empty windows.
3408 * Returns the number of resulting tab pages.
3411 make_tabpages(maxcount)
3412 int maxcount;
3414 int count = maxcount;
3415 int todo;
3417 /* Limit to 'tabpagemax' tabs. */
3418 if (count > p_tpm)
3419 count = p_tpm;
3421 #ifdef FEAT_AUTOCMD
3423 * Don't execute autocommands while creating the tab pages. Must do that
3424 * when putting the buffers in the windows.
3426 block_autocmds();
3427 #endif
3429 for (todo = count - 1; todo > 0; --todo)
3430 if (win_new_tabpage(0) == FAIL)
3431 break;
3433 #ifdef FEAT_AUTOCMD
3434 unblock_autocmds();
3435 #endif
3437 /* return actual number of tab pages */
3438 return (count - todo);
3442 * Return TRUE when "tpc" points to a valid tab page.
3445 valid_tabpage(tpc)
3446 tabpage_T *tpc;
3448 tabpage_T *tp;
3450 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3451 if (tp == tpc)
3452 return TRUE;
3453 return FALSE;
3457 * Find tab page "n" (first one is 1). Returns NULL when not found.
3459 tabpage_T *
3460 find_tabpage(n)
3461 int n;
3463 tabpage_T *tp;
3464 int i = 1;
3466 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3467 ++i;
3468 return tp;
3472 * Get index of tab page "tp". First one has index 1.
3473 * When not found returns number of tab pages plus one.
3476 tabpage_index(ftp)
3477 tabpage_T *ftp;
3479 int i = 1;
3480 tabpage_T *tp;
3482 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3483 ++i;
3484 return i;
3488 * Prepare for leaving the current tab page.
3489 * When autocomands change "curtab" we don't leave the tab page and return
3490 * FAIL.
3491 * Careful: When OK is returned need to get a new tab page very very soon!
3493 /*ARGSUSED*/
3494 static int
3495 leave_tabpage(new_curbuf)
3496 buf_T *new_curbuf; /* what is going to be the new curbuf,
3497 NULL if unknown */
3499 tabpage_T *tp = curtab;
3501 #ifdef FEAT_VISUAL
3502 reset_VIsual_and_resel(); /* stop Visual mode */
3503 #endif
3504 #ifdef FEAT_AUTOCMD
3505 if (new_curbuf != curbuf)
3507 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3508 if (curtab != tp)
3509 return FAIL;
3511 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3512 if (curtab != tp)
3513 return FAIL;
3514 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3515 if (curtab != tp)
3516 return FAIL;
3517 #endif
3518 #if defined(FEAT_GUI)
3519 /* Remove the scrollbars. They may be added back later. */
3520 if (gui.in_use)
3521 gui_remove_scrollbars();
3522 #endif
3523 tp->tp_curwin = curwin;
3524 tp->tp_prevwin = prevwin;
3525 tp->tp_firstwin = firstwin;
3526 tp->tp_lastwin = lastwin;
3527 tp->tp_old_Rows = Rows;
3528 tp->tp_old_Columns = Columns;
3529 firstwin = NULL;
3530 lastwin = NULL;
3531 return OK;
3535 * Start using tab page "tp".
3536 * Only to be used after leave_tabpage() or freeing the current tab page.
3538 /*ARGSUSED*/
3539 static void
3540 enter_tabpage(tp, old_curbuf)
3541 tabpage_T *tp;
3542 buf_T *old_curbuf;
3544 int old_off = tp->tp_firstwin->w_winrow;
3545 win_T *next_prevwin = tp->tp_prevwin;
3547 curtab = tp;
3548 firstwin = tp->tp_firstwin;
3549 lastwin = tp->tp_lastwin;
3550 topframe = tp->tp_topframe;
3552 /* We would like doing the TabEnter event first, but we don't have a
3553 * valid current window yet, which may break some commands.
3554 * This triggers autocommands, thus may make "tp" invalid. */
3555 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3556 prevwin = next_prevwin;
3558 #ifdef FEAT_AUTOCMD
3559 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3561 if (old_curbuf != curbuf)
3562 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3563 #endif
3565 last_status(FALSE); /* status line may appear or disappear */
3566 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3567 must_redraw = CLEAR; /* need to redraw everything */
3568 #ifdef FEAT_DIFF
3569 diff_need_scrollbind = TRUE;
3570 #endif
3572 /* The tabpage line may have appeared or disappeared, may need to resize
3573 * the frames for that. When the Vim window was resized need to update
3574 * frame sizes too. Use the stored value of p_ch, so that it can be
3575 * different for each tab page. */
3576 p_ch = curtab->tp_ch_used;
3577 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3578 #ifdef FEAT_GUI_TABLINE
3579 && !gui_use_tabline()
3580 #endif
3582 shell_new_rows();
3583 #ifdef FEAT_VERTSPLIT
3584 if (curtab->tp_old_Columns != Columns && starting == 0)
3585 shell_new_columns(); /* update window widths */
3586 #endif
3588 #if defined(FEAT_GUI)
3589 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3590 * scrollbars. Have to update them anyway. */
3591 if (gui.in_use && starting == 0)
3593 gui_init_which_components(NULL);
3594 gui_update_scrollbars(TRUE);
3596 need_mouse_correct = TRUE;
3597 #endif
3599 redraw_all_later(CLEAR);
3603 * Go to tab page "n". For ":tab N" and "Ngt".
3604 * When "n" is 9999 go to the last tab page.
3606 void
3607 goto_tabpage(n)
3608 int n;
3610 tabpage_T *tp;
3611 tabpage_T *ttp;
3612 int i;
3614 if (text_locked())
3616 /* Not allowed when editing the command line. */
3617 #ifdef FEAT_CMDWIN
3618 if (cmdwin_type != 0)
3619 EMSG(_(e_cmdwin));
3620 else
3621 #endif
3622 EMSG(_(e_secure));
3623 return;
3626 /* If there is only one it can't work. */
3627 if (first_tabpage->tp_next == NULL)
3629 if (n > 1)
3630 beep_flush();
3631 return;
3634 if (n == 0)
3636 /* No count, go to next tab page, wrap around end. */
3637 if (curtab->tp_next == NULL)
3638 tp = first_tabpage;
3639 else
3640 tp = curtab->tp_next;
3642 else if (n < 0)
3644 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3645 * this N times. */
3646 ttp = curtab;
3647 for (i = n; i < 0; ++i)
3649 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3650 tp = tp->tp_next)
3652 ttp = tp;
3655 else if (n == 9999)
3657 /* Go to last tab page. */
3658 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3661 else
3663 /* Go to tab page "n". */
3664 tp = find_tabpage(n);
3665 if (tp == NULL)
3667 beep_flush();
3668 return;
3672 goto_tabpage_tp(tp);
3674 #ifdef FEAT_GUI_TABLINE
3675 if (gui_use_tabline())
3676 gui_mch_set_curtab(tabpage_index(curtab));
3677 #endif
3681 * Go to tabpage "tp".
3682 * Note: doesn't update the GUI tab.
3684 void
3685 goto_tabpage_tp(tp)
3686 tabpage_T *tp;
3688 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3690 if (valid_tabpage(tp))
3691 enter_tabpage(tp, curbuf);
3692 else
3693 enter_tabpage(curtab, curbuf);
3698 * Enter window "wp" in tab page "tp".
3699 * Also updates the GUI tab.
3701 void
3702 goto_tabpage_win(tp, wp)
3703 tabpage_T *tp;
3704 win_T *wp;
3706 goto_tabpage_tp(tp);
3707 if (curtab == tp && win_valid(wp))
3709 win_enter(wp, TRUE);
3710 # ifdef FEAT_GUI_TABLINE
3711 if (gui_use_tabline())
3712 gui_mch_set_curtab(tabpage_index(curtab));
3713 # endif
3718 * Move the current tab page to before tab page "nr".
3720 void
3721 tabpage_move(nr)
3722 int nr;
3724 int n = nr;
3725 tabpage_T *tp;
3727 if (first_tabpage->tp_next == NULL)
3728 return;
3730 /* Remove the current tab page from the list of tab pages. */
3731 if (curtab == first_tabpage)
3732 first_tabpage = curtab->tp_next;
3733 else
3735 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3736 if (tp->tp_next == curtab)
3737 break;
3738 if (tp == NULL) /* "cannot happen" */
3739 return;
3740 tp->tp_next = curtab->tp_next;
3743 /* Re-insert it at the specified position. */
3744 if (n == 0)
3746 curtab->tp_next = first_tabpage;
3747 first_tabpage = curtab;
3749 else
3751 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3752 --n;
3753 curtab->tp_next = tp->tp_next;
3754 tp->tp_next = curtab;
3757 /* Need to redraw the tabline. Tab page contents doesn't change. */
3758 redraw_tabline = TRUE;
3763 * Go to another window.
3764 * When jumping to another buffer, stop Visual mode. Do this before
3765 * changing windows so we can yank the selection into the '*' register.
3766 * When jumping to another window on the same buffer, adjust its cursor
3767 * position to keep the same Visual area.
3769 void
3770 win_goto(wp)
3771 win_T *wp;
3773 if (text_locked())
3775 beep_flush();
3776 text_locked_msg();
3777 return;
3779 #ifdef FEAT_AUTOCMD
3780 if (curbuf_locked())
3781 return;
3782 #endif
3784 #ifdef FEAT_VISUAL
3785 if (wp->w_buffer != curbuf)
3786 reset_VIsual_and_resel();
3787 else if (VIsual_active)
3788 wp->w_cursor = curwin->w_cursor;
3789 #endif
3791 #ifdef FEAT_GUI
3792 need_mouse_correct = TRUE;
3793 #endif
3794 win_enter(wp, TRUE);
3797 #if defined(FEAT_PERL) || defined(PROTO)
3799 * Find window number "winnr" (counting top to bottom).
3801 win_T *
3802 win_find_nr(winnr)
3803 int winnr;
3805 win_T *wp;
3807 # ifdef FEAT_WINDOWS
3808 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3809 if (--winnr == 0)
3810 break;
3811 return wp;
3812 # else
3813 return curwin;
3814 # endif
3816 #endif
3818 #ifdef FEAT_VERTSPLIT
3820 * Move to window above or below "count" times.
3822 static void
3823 win_goto_ver(up, count)
3824 int up; /* TRUE to go to win above */
3825 long count;
3827 frame_T *fr;
3828 frame_T *nfr;
3829 frame_T *foundfr;
3831 foundfr = curwin->w_frame;
3832 while (count--)
3835 * First go upwards in the tree of frames until we find a upwards or
3836 * downwards neighbor.
3838 fr = foundfr;
3839 for (;;)
3841 if (fr == topframe)
3842 goto end;
3843 if (up)
3844 nfr = fr->fr_prev;
3845 else
3846 nfr = fr->fr_next;
3847 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3848 break;
3849 fr = fr->fr_parent;
3853 * Now go downwards to find the bottom or top frame in it.
3855 for (;;)
3857 if (nfr->fr_layout == FR_LEAF)
3859 foundfr = nfr;
3860 break;
3862 fr = nfr->fr_child;
3863 if (nfr->fr_layout == FR_ROW)
3865 /* Find the frame at the cursor row. */
3866 while (fr->fr_next != NULL
3867 && frame2win(fr)->w_wincol + fr->fr_width
3868 <= curwin->w_wincol + curwin->w_wcol)
3869 fr = fr->fr_next;
3871 if (nfr->fr_layout == FR_COL && up)
3872 while (fr->fr_next != NULL)
3873 fr = fr->fr_next;
3874 nfr = fr;
3877 end:
3878 if (foundfr != NULL)
3879 win_goto(foundfr->fr_win);
3883 * Move to left or right window.
3885 static void
3886 win_goto_hor(left, count)
3887 int left; /* TRUE to go to left win */
3888 long count;
3890 frame_T *fr;
3891 frame_T *nfr;
3892 frame_T *foundfr;
3894 foundfr = curwin->w_frame;
3895 while (count--)
3898 * First go upwards in the tree of frames until we find a left or
3899 * right neighbor.
3901 fr = foundfr;
3902 for (;;)
3904 if (fr == topframe)
3905 goto end;
3906 if (left)
3907 nfr = fr->fr_prev;
3908 else
3909 nfr = fr->fr_next;
3910 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3911 break;
3912 fr = fr->fr_parent;
3916 * Now go downwards to find the leftmost or rightmost frame in it.
3918 for (;;)
3920 if (nfr->fr_layout == FR_LEAF)
3922 foundfr = nfr;
3923 break;
3925 fr = nfr->fr_child;
3926 if (nfr->fr_layout == FR_COL)
3928 /* Find the frame at the cursor row. */
3929 while (fr->fr_next != NULL
3930 && frame2win(fr)->w_winrow + fr->fr_height
3931 <= curwin->w_winrow + curwin->w_wrow)
3932 fr = fr->fr_next;
3934 if (nfr->fr_layout == FR_ROW && left)
3935 while (fr->fr_next != NULL)
3936 fr = fr->fr_next;
3937 nfr = fr;
3940 end:
3941 if (foundfr != NULL)
3942 win_goto(foundfr->fr_win);
3944 #endif
3947 * Make window "wp" the current window.
3949 void
3950 win_enter(wp, undo_sync)
3951 win_T *wp;
3952 int undo_sync;
3954 win_enter_ext(wp, undo_sync, FALSE);
3958 * Make window wp the current window.
3959 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3960 * been closed and isn't valid.
3962 static void
3963 win_enter_ext(wp, undo_sync, curwin_invalid)
3964 win_T *wp;
3965 int undo_sync;
3966 int curwin_invalid;
3968 #ifdef FEAT_AUTOCMD
3969 int other_buffer = FALSE;
3970 #endif
3972 if (wp == curwin && !curwin_invalid) /* nothing to do */
3973 return;
3975 #ifdef FEAT_AUTOCMD
3976 if (!curwin_invalid)
3979 * Be careful: If autocommands delete the window, return now.
3981 if (wp->w_buffer != curbuf)
3983 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3984 other_buffer = TRUE;
3985 if (!win_valid(wp))
3986 return;
3988 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3989 if (!win_valid(wp))
3990 return;
3991 # ifdef FEAT_EVAL
3992 /* autocmds may abort script processing */
3993 if (aborting())
3994 return;
3995 # endif
3997 #endif
3999 /* sync undo before leaving the current buffer */
4000 if (undo_sync && curbuf != wp->w_buffer)
4001 u_sync(FALSE);
4002 /* may have to copy the buffer options when 'cpo' contains 'S' */
4003 if (wp->w_buffer != curbuf)
4004 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4005 if (!curwin_invalid)
4007 prevwin = curwin; /* remember for CTRL-W p */
4008 curwin->w_redr_status = TRUE;
4010 curwin = wp;
4011 curbuf = wp->w_buffer;
4012 check_cursor();
4013 #ifdef FEAT_VIRTUALEDIT
4014 if (!virtual_active())
4015 curwin->w_cursor.coladd = 0;
4016 #endif
4017 changed_line_abv_curs(); /* assume cursor position needs updating */
4019 if (curwin->w_localdir != NULL)
4021 /* Window has a local directory: Save current directory as global
4022 * directory (unless that was done already) and change to the local
4023 * directory. */
4024 if (globaldir == NULL)
4026 char_u cwd[MAXPATHL];
4028 if (mch_dirname(cwd, MAXPATHL) == OK)
4029 globaldir = vim_strsave(cwd);
4031 mch_chdir((char *)curwin->w_localdir);
4032 shorten_fnames(TRUE);
4034 else if (globaldir != NULL)
4036 /* Window doesn't have a local directory and we are not in the global
4037 * directory: Change to the global directory. */
4038 mch_chdir((char *)globaldir);
4039 vim_free(globaldir);
4040 globaldir = NULL;
4041 shorten_fnames(TRUE);
4044 #ifdef FEAT_AUTOCMD
4045 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4046 if (other_buffer)
4047 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4048 #endif
4050 #ifdef FEAT_TITLE
4051 maketitle();
4052 #endif
4053 curwin->w_redr_status = TRUE;
4054 redraw_tabline = TRUE;
4055 if (restart_edit)
4056 redraw_later(VALID); /* causes status line redraw */
4058 /* set window height to desired minimal value */
4059 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4060 win_setheight((int)p_wh);
4061 else if (curwin->w_height == 0)
4062 win_setheight(1);
4064 #ifdef FEAT_VERTSPLIT
4065 /* set window width to desired minimal value */
4066 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4067 win_setwidth((int)p_wiw);
4068 #endif
4070 #ifdef FEAT_MOUSE
4071 setmouse(); /* in case jumped to/from help buffer */
4072 #endif
4074 /* Change directories when the 'acd' option is set. */
4075 DO_AUTOCHDIR
4078 #endif /* FEAT_WINDOWS */
4080 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4082 * Jump to the first open window that contains buffer "buf", if one exists.
4083 * Returns a pointer to the window found, otherwise NULL.
4085 win_T *
4086 buf_jump_open_win(buf)
4087 buf_T *buf;
4089 # ifdef FEAT_WINDOWS
4090 win_T *wp;
4092 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4093 if (wp->w_buffer == buf)
4094 break;
4095 if (wp != NULL)
4096 win_enter(wp, FALSE);
4097 return wp;
4098 # else
4099 if (curwin->w_buffer == buf)
4100 return curwin;
4101 return NULL;
4102 # endif
4106 * Jump to the first open window in any tab page that contains buffer "buf",
4107 * if one exists.
4108 * Returns a pointer to the window found, otherwise NULL.
4110 win_T *
4111 buf_jump_open_tab(buf)
4112 buf_T *buf;
4114 # ifdef FEAT_WINDOWS
4115 win_T *wp;
4116 tabpage_T *tp;
4118 /* First try the current tab page. */
4119 wp = buf_jump_open_win(buf);
4120 if (wp != NULL)
4121 return wp;
4123 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4124 if (tp != curtab)
4126 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4127 if (wp->w_buffer == buf)
4128 break;
4129 if (wp != NULL)
4131 goto_tabpage_win(tp, wp);
4132 if (curwin != wp)
4133 wp = NULL; /* something went wrong */
4134 break;
4138 return wp;
4139 # else
4140 if (curwin->w_buffer == buf)
4141 return curwin;
4142 return NULL;
4143 # endif
4145 #endif
4148 * allocate a window structure and link it in the window list
4150 /*ARGSUSED*/
4151 static win_T *
4152 win_alloc(after)
4153 win_T *after;
4155 win_T *newwin;
4158 * allocate window structure and linesizes arrays
4160 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4161 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4163 vim_free(newwin);
4164 newwin = NULL;
4167 if (newwin != NULL)
4169 #ifdef FEAT_AUTOCMD
4170 /* Don't execute autocommands while the window is not properly
4171 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4172 * event. */
4173 block_autocmds();
4174 #endif
4176 * link the window in the window list
4178 #ifdef FEAT_WINDOWS
4179 win_append(after, newwin);
4180 #endif
4181 #ifdef FEAT_VERTSPLIT
4182 newwin->w_wincol = 0;
4183 newwin->w_width = Columns;
4184 #endif
4186 /* position the display and the cursor at the top of the file. */
4187 newwin->w_topline = 1;
4188 #ifdef FEAT_DIFF
4189 newwin->w_topfill = 0;
4190 #endif
4191 newwin->w_botline = 2;
4192 newwin->w_cursor.lnum = 1;
4193 #ifdef FEAT_SCROLLBIND
4194 newwin->w_scbind_pos = 1;
4195 #endif
4197 /* We won't calculate w_fraction until resizing the window */
4198 newwin->w_fraction = 0;
4199 newwin->w_prev_fraction_row = -1;
4201 #ifdef FEAT_GUI
4202 if (gui.in_use)
4204 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4205 SBAR_LEFT, newwin);
4206 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4207 SBAR_RIGHT, newwin);
4209 #endif
4210 #ifdef FEAT_EVAL
4211 /* init w: variables */
4212 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4213 #endif
4214 #ifdef FEAT_FOLDING
4215 foldInitWin(newwin);
4216 #endif
4217 #ifdef FEAT_AUTOCMD
4218 unblock_autocmds();
4219 #endif
4220 #ifdef FEAT_SEARCH_EXTRA
4221 newwin->w_match_head = NULL;
4222 newwin->w_next_match_id = 4;
4223 #endif
4225 return newwin;
4228 #if defined(FEAT_WINDOWS) || defined(PROTO)
4231 * remove window 'wp' from the window list and free the structure
4233 static void
4234 win_free(wp, tp)
4235 win_T *wp;
4236 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4238 int i;
4240 #ifdef FEAT_AUTOCMD
4241 /* Don't execute autocommands while the window is halfway being deleted.
4242 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4243 block_autocmds();
4244 #endif
4246 #ifdef FEAT_MZSCHEME
4247 mzscheme_window_free(wp);
4248 #endif
4250 #ifdef FEAT_PERL
4251 perl_win_free(wp);
4252 #endif
4254 #ifdef FEAT_PYTHON
4255 python_window_free(wp);
4256 #endif
4258 #ifdef FEAT_TCL
4259 tcl_window_free(wp);
4260 #endif
4262 #ifdef FEAT_RUBY
4263 ruby_window_free(wp);
4264 #endif
4266 clear_winopt(&wp->w_onebuf_opt);
4267 clear_winopt(&wp->w_allbuf_opt);
4269 #ifdef FEAT_EVAL
4270 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4271 #endif
4273 if (prevwin == wp)
4274 prevwin = NULL;
4275 win_free_lsize(wp);
4277 for (i = 0; i < wp->w_tagstacklen; ++i)
4278 vim_free(wp->w_tagstack[i].tagname);
4280 vim_free(wp->w_localdir);
4282 #ifdef FEAT_SEARCH_EXTRA
4283 clear_matches(wp);
4284 #endif
4286 #ifdef FEAT_JUMPLIST
4287 free_jumplist(wp);
4288 #endif
4290 #ifdef FEAT_QUICKFIX
4291 qf_free_all(wp);
4292 #endif
4294 #ifdef FEAT_GUI
4295 if (gui.in_use)
4297 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4298 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4300 #endif /* FEAT_GUI */
4302 win_remove(wp, tp);
4303 vim_free(wp);
4305 #ifdef FEAT_AUTOCMD
4306 unblock_autocmds();
4307 #endif
4311 * Append window "wp" in the window list after window "after".
4313 static void
4314 win_append(after, wp)
4315 win_T *after, *wp;
4317 win_T *before;
4319 if (after == NULL) /* after NULL is in front of the first */
4320 before = firstwin;
4321 else
4322 before = after->w_next;
4324 wp->w_next = before;
4325 wp->w_prev = after;
4326 if (after == NULL)
4327 firstwin = wp;
4328 else
4329 after->w_next = wp;
4330 if (before == NULL)
4331 lastwin = wp;
4332 else
4333 before->w_prev = wp;
4337 * Remove a window from the window list.
4339 static void
4340 win_remove(wp, tp)
4341 win_T *wp;
4342 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4344 if (wp->w_prev != NULL)
4345 wp->w_prev->w_next = wp->w_next;
4346 else if (tp == NULL)
4347 firstwin = wp->w_next;
4348 else
4349 tp->tp_firstwin = wp->w_next;
4350 if (wp->w_next != NULL)
4351 wp->w_next->w_prev = wp->w_prev;
4352 else if (tp == NULL)
4353 lastwin = wp->w_prev;
4354 else
4355 tp->tp_lastwin = wp->w_prev;
4359 * Append frame "frp" in a frame list after frame "after".
4361 static void
4362 frame_append(after, frp)
4363 frame_T *after, *frp;
4365 frp->fr_next = after->fr_next;
4366 after->fr_next = frp;
4367 if (frp->fr_next != NULL)
4368 frp->fr_next->fr_prev = frp;
4369 frp->fr_prev = after;
4373 * Insert frame "frp" in a frame list before frame "before".
4375 static void
4376 frame_insert(before, frp)
4377 frame_T *before, *frp;
4379 frp->fr_next = before;
4380 frp->fr_prev = before->fr_prev;
4381 before->fr_prev = frp;
4382 if (frp->fr_prev != NULL)
4383 frp->fr_prev->fr_next = frp;
4384 else
4385 frp->fr_parent->fr_child = frp;
4389 * Remove a frame from a frame list.
4391 static void
4392 frame_remove(frp)
4393 frame_T *frp;
4395 if (frp->fr_prev != NULL)
4396 frp->fr_prev->fr_next = frp->fr_next;
4397 else
4398 frp->fr_parent->fr_child = frp->fr_next;
4399 if (frp->fr_next != NULL)
4400 frp->fr_next->fr_prev = frp->fr_prev;
4403 #endif /* FEAT_WINDOWS */
4406 * Allocate w_lines[] for window "wp".
4407 * Return FAIL for failure, OK for success.
4410 win_alloc_lines(wp)
4411 win_T *wp;
4413 wp->w_lines_valid = 0;
4414 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4415 if (wp->w_lines == NULL)
4416 return FAIL;
4417 return OK;
4421 * free lsize arrays for a window
4423 void
4424 win_free_lsize(wp)
4425 win_T *wp;
4427 vim_free(wp->w_lines);
4428 wp->w_lines = NULL;
4432 * Called from win_new_shellsize() after Rows changed.
4433 * This only does the current tab page, others must be done when made active.
4435 void
4436 shell_new_rows()
4438 int h = (int)ROWS_AVAIL;
4440 if (firstwin == NULL) /* not initialized yet */
4441 return;
4442 #ifdef FEAT_WINDOWS
4443 if (h < frame_minheight(topframe, NULL))
4444 h = frame_minheight(topframe, NULL);
4446 /* First try setting the heights of windows with 'winfixheight'. If
4447 * that doesn't result in the right height, forget about that option. */
4448 frame_new_height(topframe, h, FALSE, TRUE);
4449 if (topframe->fr_height != h)
4450 frame_new_height(topframe, h, FALSE, FALSE);
4452 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4453 #else
4454 if (h < 1)
4455 h = 1;
4456 win_new_height(firstwin, h);
4457 #endif
4458 compute_cmdrow();
4459 #ifdef FEAT_WINDOWS
4460 curtab->tp_ch_used = p_ch;
4461 #endif
4463 #if 0
4464 /* Disabled: don't want making the screen smaller make a window larger. */
4465 if (p_ea)
4466 win_equal(curwin, FALSE, 'v');
4467 #endif
4470 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4472 * Called from win_new_shellsize() after Columns changed.
4474 void
4475 shell_new_columns()
4477 if (firstwin == NULL) /* not initialized yet */
4478 return;
4480 /* First try setting the widths of windows with 'winfixwidth'. If that
4481 * doesn't result in the right width, forget about that option. */
4482 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4483 if (topframe->fr_width != Columns)
4484 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4486 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4487 #if 0
4488 /* Disabled: don't want making the screen smaller make a window larger. */
4489 if (p_ea)
4490 win_equal(curwin, FALSE, 'h');
4491 #endif
4493 #endif
4495 #if defined(FEAT_CMDWIN) || defined(PROTO)
4497 * Save the size of all windows in "gap".
4499 void
4500 win_size_save(gap)
4501 garray_T *gap;
4504 win_T *wp;
4506 ga_init2(gap, (int)sizeof(int), 1);
4507 if (ga_grow(gap, win_count() * 2) == OK)
4508 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4510 ((int *)gap->ga_data)[gap->ga_len++] =
4511 wp->w_width + wp->w_vsep_width;
4512 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4517 * Restore window sizes, but only if the number of windows is still the same.
4518 * Does not free the growarray.
4520 void
4521 win_size_restore(gap)
4522 garray_T *gap;
4524 win_T *wp;
4525 int i;
4527 if (win_count() * 2 == gap->ga_len)
4529 i = 0;
4530 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4532 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4533 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4535 /* recompute the window positions */
4536 (void)win_comp_pos();
4539 #endif /* FEAT_CMDWIN */
4541 #if defined(FEAT_WINDOWS) || defined(PROTO)
4543 * Update the position for all windows, using the width and height of the
4544 * frames.
4545 * Returns the row just after the last window.
4548 win_comp_pos()
4550 int row = tabline_height();
4551 int col = 0;
4553 frame_comp_pos(topframe, &row, &col);
4554 return row;
4558 * Update the position of the windows in frame "topfrp", using the width and
4559 * height of the frames.
4560 * "*row" and "*col" are the top-left position of the frame. They are updated
4561 * to the bottom-right position plus one.
4563 static void
4564 frame_comp_pos(topfrp, row, col)
4565 frame_T *topfrp;
4566 int *row;
4567 int *col;
4569 win_T *wp;
4570 frame_T *frp;
4571 #ifdef FEAT_VERTSPLIT
4572 int startcol;
4573 int startrow;
4574 #endif
4576 wp = topfrp->fr_win;
4577 if (wp != NULL)
4579 if (wp->w_winrow != *row
4580 #ifdef FEAT_VERTSPLIT
4581 || wp->w_wincol != *col
4582 #endif
4585 /* position changed, redraw */
4586 wp->w_winrow = *row;
4587 #ifdef FEAT_VERTSPLIT
4588 wp->w_wincol = *col;
4589 #endif
4590 redraw_win_later(wp, NOT_VALID);
4591 wp->w_redr_status = TRUE;
4593 *row += wp->w_height + wp->w_status_height;
4594 #ifdef FEAT_VERTSPLIT
4595 *col += wp->w_width + wp->w_vsep_width;
4596 #endif
4598 else
4600 #ifdef FEAT_VERTSPLIT
4601 startrow = *row;
4602 startcol = *col;
4603 #endif
4604 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4606 #ifdef FEAT_VERTSPLIT
4607 if (topfrp->fr_layout == FR_ROW)
4608 *row = startrow; /* all frames are at the same row */
4609 else
4610 *col = startcol; /* all frames are at the same col */
4611 #endif
4612 frame_comp_pos(frp, row, col);
4617 #endif /* FEAT_WINDOWS */
4620 * Set current window height and take care of repositioning other windows to
4621 * fit around it.
4623 void
4624 win_setheight(height)
4625 int height;
4627 win_setheight_win(height, curwin);
4631 * Set the window height of window "win" and take care of repositioning other
4632 * windows to fit around it.
4634 void
4635 win_setheight_win(height, win)
4636 int height;
4637 win_T *win;
4639 int row;
4641 if (win == curwin)
4643 /* Always keep current window at least one line high, even when
4644 * 'winminheight' is zero. */
4645 #ifdef FEAT_WINDOWS
4646 if (height < p_wmh)
4647 height = p_wmh;
4648 #endif
4649 if (height == 0)
4650 height = 1;
4653 #ifdef FEAT_WINDOWS
4654 frame_setheight(win->w_frame, height + win->w_status_height);
4656 /* recompute the window positions */
4657 row = win_comp_pos();
4658 #else
4659 if (height > topframe->fr_height)
4660 height = topframe->fr_height;
4661 win->w_height = height;
4662 row = height;
4663 #endif
4666 * If there is extra space created between the last window and the command
4667 * line, clear it.
4669 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4670 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4671 cmdline_row = row;
4672 msg_row = row;
4673 msg_col = 0;
4675 redraw_all_later(NOT_VALID);
4678 #if defined(FEAT_WINDOWS) || defined(PROTO)
4681 * Set the height of a frame to "height" and take care that all frames and
4682 * windows inside it are resized. Also resize frames on the left and right if
4683 * the are in the same FR_ROW frame.
4685 * Strategy:
4686 * If the frame is part of a FR_COL frame, try fitting the frame in that
4687 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4688 * go to containing frames to resize them and make room.
4689 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4690 * Check for the minimal height of the FR_ROW frame.
4691 * At the top level we can also use change the command line height.
4693 static void
4694 frame_setheight(curfrp, height)
4695 frame_T *curfrp;
4696 int height;
4698 int room; /* total number of lines available */
4699 int take; /* number of lines taken from other windows */
4700 int room_cmdline; /* lines available from cmdline */
4701 int run;
4702 frame_T *frp;
4703 int h;
4704 int room_reserved;
4706 /* If the height already is the desired value, nothing to do. */
4707 if (curfrp->fr_height == height)
4708 return;
4710 if (curfrp->fr_parent == NULL)
4712 /* topframe: can only change the command line */
4713 if (height > ROWS_AVAIL)
4714 height = ROWS_AVAIL;
4715 if (height > 0)
4716 frame_new_height(curfrp, height, FALSE, FALSE);
4718 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4720 /* Row of frames: Also need to resize frames left and right of this
4721 * one. First check for the minimal height of these. */
4722 h = frame_minheight(curfrp->fr_parent, NULL);
4723 if (height < h)
4724 height = h;
4725 frame_setheight(curfrp->fr_parent, height);
4727 else
4730 * Column of frames: try to change only frames in this column.
4732 #ifdef FEAT_VERTSPLIT
4734 * Do this twice:
4735 * 1: compute room available, if it's not enough try resizing the
4736 * containing frame.
4737 * 2: compute the room available and adjust the height to it.
4738 * Try not to reduce the height of a window with 'winfixheight' set.
4740 for (run = 1; run <= 2; ++run)
4741 #else
4742 for (;;)
4743 #endif
4745 room = 0;
4746 room_reserved = 0;
4747 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4748 frp = frp->fr_next)
4750 if (frp != curfrp
4751 && frp->fr_win != NULL
4752 && frp->fr_win->w_p_wfh)
4753 room_reserved += frp->fr_height;
4754 room += frp->fr_height;
4755 if (frp != curfrp)
4756 room -= frame_minheight(frp, NULL);
4758 #ifdef FEAT_VERTSPLIT
4759 if (curfrp->fr_width != Columns)
4760 room_cmdline = 0;
4761 else
4762 #endif
4764 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4765 + lastwin->w_height + lastwin->w_status_height);
4766 if (room_cmdline < 0)
4767 room_cmdline = 0;
4770 if (height <= room + room_cmdline)
4771 break;
4772 #ifdef FEAT_VERTSPLIT
4773 if (run == 2 || curfrp->fr_width == Columns)
4774 #endif
4776 if (height > room + room_cmdline)
4777 height = room + room_cmdline;
4778 break;
4780 #ifdef FEAT_VERTSPLIT
4781 frame_setheight(curfrp->fr_parent, height
4782 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4783 #endif
4784 /*NOTREACHED*/
4788 * Compute the number of lines we will take from others frames (can be
4789 * negative!).
4791 take = height - curfrp->fr_height;
4793 /* If there is not enough room, also reduce the height of a window
4794 * with 'winfixheight' set. */
4795 if (height > room + room_cmdline - room_reserved)
4796 room_reserved = room + room_cmdline - height;
4797 /* If there is only a 'winfixheight' window and making the
4798 * window smaller, need to make the other window taller. */
4799 if (take < 0 && room - curfrp->fr_height < room_reserved)
4800 room_reserved = 0;
4802 if (take > 0 && room_cmdline > 0)
4804 /* use lines from cmdline first */
4805 if (take < room_cmdline)
4806 room_cmdline = take;
4807 take -= room_cmdline;
4808 topframe->fr_height += room_cmdline;
4812 * set the current frame to the new height
4814 frame_new_height(curfrp, height, FALSE, FALSE);
4817 * First take lines from the frames after the current frame. If
4818 * that is not enough, takes lines from frames above the current
4819 * frame.
4821 for (run = 0; run < 2; ++run)
4823 if (run == 0)
4824 frp = curfrp->fr_next; /* 1st run: start with next window */
4825 else
4826 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4827 while (frp != NULL && take != 0)
4829 h = frame_minheight(frp, NULL);
4830 if (room_reserved > 0
4831 && frp->fr_win != NULL
4832 && frp->fr_win->w_p_wfh)
4834 if (room_reserved >= frp->fr_height)
4835 room_reserved -= frp->fr_height;
4836 else
4838 if (frp->fr_height - room_reserved > take)
4839 room_reserved = frp->fr_height - take;
4840 take -= frp->fr_height - room_reserved;
4841 frame_new_height(frp, room_reserved, FALSE, FALSE);
4842 room_reserved = 0;
4845 else
4847 if (frp->fr_height - take < h)
4849 take -= frp->fr_height - h;
4850 frame_new_height(frp, h, FALSE, FALSE);
4852 else
4854 frame_new_height(frp, frp->fr_height - take,
4855 FALSE, FALSE);
4856 take = 0;
4859 if (run == 0)
4860 frp = frp->fr_next;
4861 else
4862 frp = frp->fr_prev;
4868 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4870 * Set current window width and take care of repositioning other windows to
4871 * fit around it.
4873 void
4874 win_setwidth(width)
4875 int width;
4877 win_setwidth_win(width, curwin);
4880 void
4881 win_setwidth_win(width, wp)
4882 int width;
4883 win_T *wp;
4885 /* Always keep current window at least one column wide, even when
4886 * 'winminwidth' is zero. */
4887 if (wp == curwin)
4889 if (width < p_wmw)
4890 width = p_wmw;
4891 if (width == 0)
4892 width = 1;
4895 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4897 /* recompute the window positions */
4898 (void)win_comp_pos();
4900 redraw_all_later(NOT_VALID);
4904 * Set the width of a frame to "width" and take care that all frames and
4905 * windows inside it are resized. Also resize frames above and below if the
4906 * are in the same FR_ROW frame.
4908 * Strategy is similar to frame_setheight().
4910 static void
4911 frame_setwidth(curfrp, width)
4912 frame_T *curfrp;
4913 int width;
4915 int room; /* total number of lines available */
4916 int take; /* number of lines taken from other windows */
4917 int run;
4918 frame_T *frp;
4919 int w;
4920 int room_reserved;
4922 /* If the width already is the desired value, nothing to do. */
4923 if (curfrp->fr_width == width)
4924 return;
4926 if (curfrp->fr_parent == NULL)
4927 /* topframe: can't change width */
4928 return;
4930 if (curfrp->fr_parent->fr_layout == FR_COL)
4932 /* Column of frames: Also need to resize frames above and below of
4933 * this one. First check for the minimal width of these. */
4934 w = frame_minwidth(curfrp->fr_parent, NULL);
4935 if (width < w)
4936 width = w;
4937 frame_setwidth(curfrp->fr_parent, width);
4939 else
4942 * Row of frames: try to change only frames in this row.
4944 * Do this twice:
4945 * 1: compute room available, if it's not enough try resizing the
4946 * containing frame.
4947 * 2: compute the room available and adjust the width to it.
4949 for (run = 1; run <= 2; ++run)
4951 room = 0;
4952 room_reserved = 0;
4953 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4954 frp = frp->fr_next)
4956 if (frp != curfrp
4957 && frp->fr_win != NULL
4958 && frp->fr_win->w_p_wfw)
4959 room_reserved += frp->fr_width;
4960 room += frp->fr_width;
4961 if (frp != curfrp)
4962 room -= frame_minwidth(frp, NULL);
4965 if (width <= room)
4966 break;
4967 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4969 if (width > room)
4970 width = room;
4971 break;
4973 frame_setwidth(curfrp->fr_parent, width
4974 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4978 * Compute the number of lines we will take from others frames (can be
4979 * negative!).
4981 take = width - curfrp->fr_width;
4983 /* If there is not enough room, also reduce the width of a window
4984 * with 'winfixwidth' set. */
4985 if (width > room - room_reserved)
4986 room_reserved = room - width;
4987 /* If there is only a 'winfixwidth' window and making the
4988 * window smaller, need to make the other window narrower. */
4989 if (take < 0 && room - curfrp->fr_width < room_reserved)
4990 room_reserved = 0;
4993 * set the current frame to the new width
4995 frame_new_width(curfrp, width, FALSE, FALSE);
4998 * First take lines from the frames right of the current frame. If
4999 * that is not enough, takes lines from frames left of the current
5000 * frame.
5002 for (run = 0; run < 2; ++run)
5004 if (run == 0)
5005 frp = curfrp->fr_next; /* 1st run: start with next window */
5006 else
5007 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5008 while (frp != NULL && take != 0)
5010 w = frame_minwidth(frp, NULL);
5011 if (room_reserved > 0
5012 && frp->fr_win != NULL
5013 && frp->fr_win->w_p_wfw)
5015 if (room_reserved >= frp->fr_width)
5016 room_reserved -= frp->fr_width;
5017 else
5019 if (frp->fr_width - room_reserved > take)
5020 room_reserved = frp->fr_width - take;
5021 take -= frp->fr_width - room_reserved;
5022 frame_new_width(frp, room_reserved, FALSE, FALSE);
5023 room_reserved = 0;
5026 else
5028 if (frp->fr_width - take < w)
5030 take -= frp->fr_width - w;
5031 frame_new_width(frp, w, FALSE, FALSE);
5033 else
5035 frame_new_width(frp, frp->fr_width - take,
5036 FALSE, FALSE);
5037 take = 0;
5040 if (run == 0)
5041 frp = frp->fr_next;
5042 else
5043 frp = frp->fr_prev;
5048 #endif /* FEAT_VERTSPLIT */
5051 * Check 'winminheight' for a valid value.
5053 void
5054 win_setminheight()
5056 int room;
5057 int first = TRUE;
5058 win_T *wp;
5060 /* loop until there is a 'winminheight' that is possible */
5061 while (p_wmh > 0)
5063 /* TODO: handle vertical splits */
5064 room = -p_wh;
5065 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5066 room += wp->w_height - p_wmh;
5067 if (room >= 0)
5068 break;
5069 --p_wmh;
5070 if (first)
5072 EMSG(_(e_noroom));
5073 first = FALSE;
5078 #ifdef FEAT_MOUSE
5081 * Status line of dragwin is dragged "offset" lines down (negative is up).
5083 void
5084 win_drag_status_line(dragwin, offset)
5085 win_T *dragwin;
5086 int offset;
5088 frame_T *curfr;
5089 frame_T *fr;
5090 int room;
5091 int row;
5092 int up; /* if TRUE, drag status line up, otherwise down */
5093 int n;
5095 fr = dragwin->w_frame;
5096 curfr = fr;
5097 if (fr != topframe) /* more than one window */
5099 fr = fr->fr_parent;
5100 /* When the parent frame is not a column of frames, its parent should
5101 * be. */
5102 if (fr->fr_layout != FR_COL)
5104 curfr = fr;
5105 if (fr != topframe) /* only a row of windows, may drag statusline */
5106 fr = fr->fr_parent;
5110 /* If this is the last frame in a column, may want to resize the parent
5111 * frame instead (go two up to skip a row of frames). */
5112 while (curfr != topframe && curfr->fr_next == NULL)
5114 if (fr != topframe)
5115 fr = fr->fr_parent;
5116 curfr = fr;
5117 if (fr != topframe)
5118 fr = fr->fr_parent;
5121 if (offset < 0) /* drag up */
5123 up = TRUE;
5124 offset = -offset;
5125 /* sum up the room of the current frame and above it */
5126 if (fr == curfr)
5128 /* only one window */
5129 room = fr->fr_height - frame_minheight(fr, NULL);
5131 else
5133 room = 0;
5134 for (fr = fr->fr_child; ; fr = fr->fr_next)
5136 room += fr->fr_height - frame_minheight(fr, NULL);
5137 if (fr == curfr)
5138 break;
5141 fr = curfr->fr_next; /* put fr at frame that grows */
5143 else /* drag down */
5145 up = FALSE;
5147 * Only dragging the last status line can reduce p_ch.
5149 room = Rows - cmdline_row;
5150 if (curfr->fr_next == NULL)
5151 room -= 1;
5152 else
5153 room -= p_ch;
5154 if (room < 0)
5155 room = 0;
5156 /* sum up the room of frames below of the current one */
5157 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5158 room += fr->fr_height - frame_minheight(fr, NULL);
5159 fr = curfr; /* put fr at window that grows */
5162 if (room < offset) /* Not enough room */
5163 offset = room; /* Move as far as we can */
5164 if (offset <= 0)
5165 return;
5168 * Grow frame fr by "offset" lines.
5169 * Doesn't happen when dragging the last status line up.
5171 if (fr != NULL)
5172 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5174 if (up)
5175 fr = curfr; /* current frame gets smaller */
5176 else
5177 fr = curfr->fr_next; /* next frame gets smaller */
5180 * Now make the other frames smaller.
5182 while (fr != NULL && offset > 0)
5184 n = frame_minheight(fr, NULL);
5185 if (fr->fr_height - offset <= n)
5187 offset -= fr->fr_height - n;
5188 frame_new_height(fr, n, !up, FALSE);
5190 else
5192 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5193 break;
5195 if (up)
5196 fr = fr->fr_prev;
5197 else
5198 fr = fr->fr_next;
5200 row = win_comp_pos();
5201 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5202 cmdline_row = row;
5203 p_ch = Rows - cmdline_row;
5204 if (p_ch < 1)
5205 p_ch = 1;
5206 curtab->tp_ch_used = p_ch;
5207 redraw_all_later(SOME_VALID);
5208 showmode();
5211 #ifdef FEAT_VERTSPLIT
5213 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5215 void
5216 win_drag_vsep_line(dragwin, offset)
5217 win_T *dragwin;
5218 int offset;
5220 frame_T *curfr;
5221 frame_T *fr;
5222 int room;
5223 int left; /* if TRUE, drag separator line left, otherwise right */
5224 int n;
5226 fr = dragwin->w_frame;
5227 if (fr == topframe) /* only one window (cannot happen?) */
5228 return;
5229 curfr = fr;
5230 fr = fr->fr_parent;
5231 /* When the parent frame is not a row of frames, its parent should be. */
5232 if (fr->fr_layout != FR_ROW)
5234 if (fr == topframe) /* only a column of windows (cannot happen?) */
5235 return;
5236 curfr = fr;
5237 fr = fr->fr_parent;
5240 /* If this is the last frame in a row, may want to resize a parent
5241 * frame instead. */
5242 while (curfr->fr_next == NULL)
5244 if (fr == topframe)
5245 break;
5246 curfr = fr;
5247 fr = fr->fr_parent;
5248 if (fr != topframe)
5250 curfr = fr;
5251 fr = fr->fr_parent;
5255 if (offset < 0) /* drag left */
5257 left = TRUE;
5258 offset = -offset;
5259 /* sum up the room of the current frame and left of it */
5260 room = 0;
5261 for (fr = fr->fr_child; ; fr = fr->fr_next)
5263 room += fr->fr_width - frame_minwidth(fr, NULL);
5264 if (fr == curfr)
5265 break;
5267 fr = curfr->fr_next; /* put fr at frame that grows */
5269 else /* drag right */
5271 left = FALSE;
5272 /* sum up the room of frames right of the current one */
5273 room = 0;
5274 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5275 room += fr->fr_width - frame_minwidth(fr, NULL);
5276 fr = curfr; /* put fr at window that grows */
5279 if (room < offset) /* Not enough room */
5280 offset = room; /* Move as far as we can */
5281 if (offset <= 0) /* No room at all, quit. */
5282 return;
5284 /* grow frame fr by offset lines */
5285 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5287 /* shrink other frames: current and at the left or at the right */
5288 if (left)
5289 fr = curfr; /* current frame gets smaller */
5290 else
5291 fr = curfr->fr_next; /* next frame gets smaller */
5293 while (fr != NULL && offset > 0)
5295 n = frame_minwidth(fr, NULL);
5296 if (fr->fr_width - offset <= n)
5298 offset -= fr->fr_width - n;
5299 frame_new_width(fr, n, !left, FALSE);
5301 else
5303 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5304 break;
5306 if (left)
5307 fr = fr->fr_prev;
5308 else
5309 fr = fr->fr_next;
5311 (void)win_comp_pos();
5312 redraw_all_later(NOT_VALID);
5314 #endif /* FEAT_VERTSPLIT */
5315 #endif /* FEAT_MOUSE */
5317 #endif /* FEAT_WINDOWS */
5320 * Set the height of a window.
5321 * This takes care of the things inside the window, not what happens to the
5322 * window position, the frame or to other windows.
5324 static void
5325 win_new_height(wp, height)
5326 win_T *wp;
5327 int height;
5329 linenr_T lnum;
5330 int sline, line_size;
5331 #define FRACTION_MULT 16384L
5333 /* Don't want a negative height. Happens when splitting a tiny window.
5334 * Will equalize heights soon to fix it. */
5335 if (height < 0)
5336 height = 0;
5337 if (wp->w_height == height)
5338 return; /* nothing to do */
5340 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5341 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5342 + FRACTION_MULT / 2) / (long)wp->w_height;
5344 wp->w_height = height;
5345 wp->w_skipcol = 0;
5347 /* Don't change w_topline when height is zero. Don't set w_topline when
5348 * 'scrollbind' is set and this isn't the current window. */
5349 if (height > 0
5350 #ifdef FEAT_SCROLLBIND
5351 && (!wp->w_p_scb || wp == curwin)
5352 #endif
5356 * Find a value for w_topline that shows the cursor at the same
5357 * relative position in the window as before (more or less).
5359 lnum = wp->w_cursor.lnum;
5360 if (lnum < 1) /* can happen when starting up */
5361 lnum = 1;
5362 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5363 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5364 sline = wp->w_wrow - line_size;
5366 if (sline >= 0)
5368 /* Make sure the whole cursor line is visible, if possible. */
5369 int rows = plines_win(wp, lnum, FALSE);
5371 if (sline > wp->w_height - rows)
5373 sline = wp->w_height - rows;
5374 wp->w_wrow -= rows - line_size;
5378 if (sline < 0)
5381 * Cursor line would go off top of screen if w_wrow was this high.
5382 * Make cursor line the first line in the window. If not enough
5383 * room use w_skipcol;
5385 wp->w_wrow = line_size;
5386 if (wp->w_wrow >= wp->w_height
5387 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5389 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5390 --wp->w_wrow;
5391 while (wp->w_wrow >= wp->w_height)
5393 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5394 + win_col_off2(wp);
5395 --wp->w_wrow;
5399 else
5401 while (sline > 0 && lnum > 1)
5403 #ifdef FEAT_FOLDING
5404 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5405 if (lnum == 1)
5407 /* first line in buffer is folded */
5408 line_size = 1;
5409 --sline;
5410 break;
5412 #endif
5413 --lnum;
5414 #ifdef FEAT_DIFF
5415 if (lnum == wp->w_topline)
5416 line_size = plines_win_nofill(wp, lnum, TRUE)
5417 + wp->w_topfill;
5418 else
5419 #endif
5420 line_size = plines_win(wp, lnum, TRUE);
5421 sline -= line_size;
5424 if (sline < 0)
5427 * Line we want at top would go off top of screen. Use next
5428 * line instead.
5430 #ifdef FEAT_FOLDING
5431 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5432 #endif
5433 lnum++;
5434 wp->w_wrow -= line_size + sline;
5436 else if (sline > 0)
5438 /* First line of file reached, use that as topline. */
5439 lnum = 1;
5440 wp->w_wrow -= sline;
5443 set_topline(wp, lnum);
5446 if (wp == curwin)
5448 if (p_so)
5449 update_topline();
5450 curs_columns(FALSE); /* validate w_wrow */
5452 wp->w_prev_fraction_row = wp->w_wrow;
5454 win_comp_scroll(wp);
5455 redraw_win_later(wp, SOME_VALID);
5456 #ifdef FEAT_WINDOWS
5457 wp->w_redr_status = TRUE;
5458 #endif
5459 invalidate_botline_win(wp);
5462 #ifdef FEAT_VERTSPLIT
5464 * Set the width of a window.
5466 static void
5467 win_new_width(wp, width)
5468 win_T *wp;
5469 int width;
5471 wp->w_width = width;
5472 wp->w_lines_valid = 0;
5473 changed_line_abv_curs_win(wp);
5474 invalidate_botline_win(wp);
5475 if (wp == curwin)
5477 update_topline();
5478 curs_columns(TRUE); /* validate w_wrow */
5480 redraw_win_later(wp, NOT_VALID);
5481 wp->w_redr_status = TRUE;
5483 #endif
5485 void
5486 win_comp_scroll(wp)
5487 win_T *wp;
5489 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5490 if (wp->w_p_scr == 0)
5491 wp->w_p_scr = 1;
5495 * command_height: called whenever p_ch has been changed
5497 void
5498 command_height()
5500 #ifdef FEAT_WINDOWS
5501 int h;
5502 frame_T *frp;
5503 int old_p_ch = curtab->tp_ch_used;
5505 /* Use the value of p_ch that we remembered. This is needed for when the
5506 * GUI starts up, we can't be sure in what order things happen. And when
5507 * p_ch was changed in another tab page. */
5508 curtab->tp_ch_used = p_ch;
5510 /* Find bottom frame with width of screen. */
5511 frp = lastwin->w_frame;
5512 # ifdef FEAT_VERTSPLIT
5513 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5514 frp = frp->fr_parent;
5515 # endif
5517 /* Avoid changing the height of a window with 'winfixheight' set. */
5518 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5519 && frp->fr_win->w_p_wfh)
5520 frp = frp->fr_prev;
5522 if (starting != NO_SCREEN)
5524 cmdline_row = Rows - p_ch;
5526 if (p_ch > old_p_ch) /* p_ch got bigger */
5528 while (p_ch > old_p_ch)
5530 if (frp == NULL)
5532 EMSG(_(e_noroom));
5533 p_ch = old_p_ch;
5534 curtab->tp_ch_used = p_ch;
5535 cmdline_row = Rows - p_ch;
5536 break;
5538 h = frp->fr_height - frame_minheight(frp, NULL);
5539 if (h > p_ch - old_p_ch)
5540 h = p_ch - old_p_ch;
5541 old_p_ch += h;
5542 frame_add_height(frp, -h);
5543 frp = frp->fr_prev;
5546 /* Recompute window positions. */
5547 (void)win_comp_pos();
5549 /* clear the lines added to cmdline */
5550 if (full_screen)
5551 screen_fill((int)(cmdline_row), (int)Rows, 0,
5552 (int)Columns, ' ', ' ', 0);
5553 msg_row = cmdline_row;
5554 redraw_cmdline = TRUE;
5555 return;
5558 if (msg_row < cmdline_row)
5559 msg_row = cmdline_row;
5560 redraw_cmdline = TRUE;
5562 frame_add_height(frp, (int)(old_p_ch - p_ch));
5564 /* Recompute window positions. */
5565 if (frp != lastwin->w_frame)
5566 (void)win_comp_pos();
5567 #else
5568 cmdline_row = Rows - p_ch;
5569 win_setheight(cmdline_row);
5570 #endif
5573 #if defined(FEAT_WINDOWS) || defined(PROTO)
5575 * Resize frame "frp" to be "n" lines higher (negative for less high).
5576 * Also resize the frames it is contained in.
5578 static void
5579 frame_add_height(frp, n)
5580 frame_T *frp;
5581 int n;
5583 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5584 for (;;)
5586 frp = frp->fr_parent;
5587 if (frp == NULL)
5588 break;
5589 frp->fr_height += n;
5594 * Add or remove a status line for the bottom window(s), according to the
5595 * value of 'laststatus'.
5597 void
5598 last_status(morewin)
5599 int morewin; /* pretend there are two or more windows */
5601 /* Don't make a difference between horizontal or vertical split. */
5602 last_status_rec(topframe, (p_ls == 2
5603 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5606 static void
5607 last_status_rec(fr, statusline)
5608 frame_T *fr;
5609 int statusline;
5611 frame_T *fp;
5612 win_T *wp;
5614 if (fr->fr_layout == FR_LEAF)
5616 wp = fr->fr_win;
5617 if (wp->w_status_height != 0 && !statusline)
5619 /* remove status line */
5620 win_new_height(wp, wp->w_height + 1);
5621 wp->w_status_height = 0;
5622 comp_col();
5624 else if (wp->w_status_height == 0 && statusline)
5626 /* Find a frame to take a line from. */
5627 fp = fr;
5628 while (fp->fr_height <= frame_minheight(fp, NULL))
5630 if (fp == topframe)
5632 EMSG(_(e_noroom));
5633 return;
5635 /* In a column of frames: go to frame above. If already at
5636 * the top or in a row of frames: go to parent. */
5637 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5638 fp = fp->fr_prev;
5639 else
5640 fp = fp->fr_parent;
5642 wp->w_status_height = 1;
5643 if (fp != fr)
5645 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5646 frame_fix_height(wp);
5647 (void)win_comp_pos();
5649 else
5650 win_new_height(wp, wp->w_height - 1);
5651 comp_col();
5652 redraw_all_later(SOME_VALID);
5655 #ifdef FEAT_VERTSPLIT
5656 else if (fr->fr_layout == FR_ROW)
5658 /* vertically split windows, set status line for each one */
5659 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5660 last_status_rec(fp, statusline);
5662 #endif
5663 else
5665 /* horizontally split window, set status line for last one */
5666 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5668 last_status_rec(fp, statusline);
5673 * Return the number of lines used by the tab page line.
5676 tabline_height()
5678 #ifdef FEAT_GUI_TABLINE
5679 /* When the GUI has the tabline then this always returns zero. */
5680 if (gui_use_tabline())
5681 return 0;
5682 #endif
5683 switch (p_stal)
5685 case 0: return 0;
5686 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5688 return 1;
5691 #endif /* FEAT_WINDOWS */
5693 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5695 * Get the file name at the cursor.
5696 * If Visual mode is active, use the selected text if it's in one line.
5697 * Returns the name in allocated memory, NULL for failure.
5699 char_u *
5700 grab_file_name(count, file_lnum)
5701 long count;
5702 linenr_T *file_lnum;
5704 # ifdef FEAT_VISUAL
5705 if (VIsual_active)
5707 int len;
5708 char_u *ptr;
5710 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5711 return NULL;
5712 return find_file_name_in_path(ptr, len,
5713 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5715 # endif
5716 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5717 file_lnum);
5722 * Return the file name under or after the cursor.
5724 * The 'path' option is searched if the file name is not absolute.
5725 * The string returned has been alloc'ed and should be freed by the caller.
5726 * NULL is returned if the file name or file is not found.
5728 * options:
5729 * FNAME_MESS give error messages
5730 * FNAME_EXP expand to path
5731 * FNAME_HYP check for hypertext link
5732 * FNAME_INCL apply "includeexpr"
5734 char_u *
5735 file_name_at_cursor(options, count, file_lnum)
5736 int options;
5737 long count;
5738 linenr_T *file_lnum;
5740 return file_name_in_line(ml_get_curline(),
5741 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5742 file_lnum);
5746 * Return the name of the file under or after ptr[col].
5747 * Otherwise like file_name_at_cursor().
5749 char_u *
5750 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5751 char_u *line;
5752 int col;
5753 int options;
5754 long count;
5755 char_u *rel_fname; /* file we are searching relative to */
5756 linenr_T *file_lnum; /* line number after the file name */
5758 char_u *ptr;
5759 int len;
5762 * search forward for what could be the start of a file name
5764 ptr = line + col;
5765 while (*ptr != NUL && !vim_isfilec(*ptr))
5766 mb_ptr_adv(ptr);
5767 if (*ptr == NUL) /* nothing found */
5769 if (options & FNAME_MESS)
5770 EMSG(_("E446: No file name under cursor"));
5771 return NULL;
5775 * Search backward for first char of the file name.
5776 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5778 while (ptr > line)
5780 #ifdef FEAT_MBYTE
5781 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5782 ptr -= len + 1;
5783 else
5784 #endif
5785 if (vim_isfilec(ptr[-1])
5786 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5787 --ptr;
5788 else
5789 break;
5793 * Search forward for the last char of the file name.
5794 * Also allow "://" when ':' is not in 'isfname'.
5796 len = 0;
5797 while (vim_isfilec(ptr[len])
5798 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5799 #ifdef FEAT_MBYTE
5800 if (has_mbyte)
5801 len += (*mb_ptr2len)(ptr + len);
5802 else
5803 #endif
5804 ++len;
5807 * If there is trailing punctuation, remove it.
5808 * But don't remove "..", could be a directory name.
5810 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5811 && ptr[len - 2] != '.')
5812 --len;
5814 if (file_lnum != NULL)
5816 char_u *p;
5818 /* Get the number after the file name and a separator character */
5819 p = ptr + len;
5820 p = skipwhite(p);
5821 if (*p != NUL)
5823 if (!isdigit(*p))
5824 ++p; /* skip the separator */
5825 p = skipwhite(p);
5826 if (isdigit(*p))
5827 *file_lnum = (int)getdigits(&p);
5831 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5834 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5835 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5837 static char_u *
5838 eval_includeexpr(ptr, len)
5839 char_u *ptr;
5840 int len;
5842 char_u *res;
5844 set_vim_var_string(VV_FNAME, ptr, len);
5845 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5846 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5847 set_vim_var_string(VV_FNAME, NULL, 0);
5848 return res;
5850 #endif
5853 * Return the name of the file ptr[len] in 'path'.
5854 * Otherwise like file_name_at_cursor().
5856 char_u *
5857 find_file_name_in_path(ptr, len, options, count, rel_fname)
5858 char_u *ptr;
5859 int len;
5860 int options;
5861 long count;
5862 char_u *rel_fname; /* file we are searching relative to */
5864 char_u *file_name;
5865 int c;
5866 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5867 char_u *tofree = NULL;
5869 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5871 tofree = eval_includeexpr(ptr, len);
5872 if (tofree != NULL)
5874 ptr = tofree;
5875 len = (int)STRLEN(ptr);
5878 # endif
5880 if (options & FNAME_EXP)
5882 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5883 TRUE, rel_fname);
5885 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5887 * If the file could not be found in a normal way, try applying
5888 * 'includeexpr' (unless done already).
5890 if (file_name == NULL
5891 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5893 tofree = eval_includeexpr(ptr, len);
5894 if (tofree != NULL)
5896 ptr = tofree;
5897 len = (int)STRLEN(ptr);
5898 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5899 TRUE, rel_fname);
5902 # endif
5903 if (file_name == NULL && (options & FNAME_MESS))
5905 c = ptr[len];
5906 ptr[len] = NUL;
5907 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5908 ptr[len] = c;
5911 /* Repeat finding the file "count" times. This matters when it
5912 * appears several times in the path. */
5913 while (file_name != NULL && --count > 0)
5915 vim_free(file_name);
5916 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5919 else
5920 file_name = vim_strnsave(ptr, len);
5922 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5923 vim_free(tofree);
5924 # endif
5926 return file_name;
5928 #endif /* FEAT_SEARCHPATH */
5931 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5932 * Also check for ":\\", which MS Internet Explorer accepts, return
5933 * URL_BACKSLASH.
5935 static int
5936 path_is_url(p)
5937 char_u *p;
5939 if (STRNCMP(p, "://", (size_t)3) == 0)
5940 return URL_SLASH;
5941 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5942 return URL_BACKSLASH;
5943 return 0;
5947 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5948 * Return URL_BACKSLASH for "name:\\".
5949 * Return zero otherwise.
5952 path_with_url(fname)
5953 char_u *fname;
5955 char_u *p;
5957 for (p = fname; isalpha(*p); ++p)
5959 return path_is_url(p);
5963 * Return TRUE if "name" is a full (absolute) path name or URL.
5966 vim_isAbsName(name)
5967 char_u *name;
5969 return (path_with_url(name) != 0 || mch_isFullName(name));
5973 * Get absolute file name into buffer "buf[len]".
5975 * return FAIL for failure, OK otherwise
5978 vim_FullName(fname, buf, len, force)
5979 char_u *fname, *buf;
5980 int len;
5981 int force; /* force expansion even when already absolute */
5983 int retval = OK;
5984 int url;
5986 *buf = NUL;
5987 if (fname == NULL)
5988 return FAIL;
5990 url = path_with_url(fname);
5991 if (!url)
5992 retval = mch_FullName(fname, buf, len, force);
5993 if (url || retval == FAIL)
5995 /* something failed; use the file name (truncate when too long) */
5996 vim_strncpy(buf, fname, len - 1);
5998 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5999 slash_adjust(buf);
6000 #endif
6001 return retval;
6005 * Return the minimal number of rows that is needed on the screen to display
6006 * the current number of windows.
6009 min_rows()
6011 int total;
6012 #ifdef FEAT_WINDOWS
6013 tabpage_T *tp;
6014 int n;
6015 #endif
6017 if (firstwin == NULL) /* not initialized yet */
6018 return MIN_LINES;
6020 #ifdef FEAT_WINDOWS
6021 total = 0;
6022 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6024 n = frame_minheight(tp->tp_topframe, NULL);
6025 if (total < n)
6026 total = n;
6028 total += tabline_height();
6029 #else
6030 total = 1; /* at least one window should have a line! */
6031 #endif
6032 total += 1; /* count the room for the command line */
6033 return total;
6037 * Return TRUE if there is only one window (in the current tab page), not
6038 * counting a help or preview window, unless it is the current window.
6041 only_one_window()
6043 #ifdef FEAT_WINDOWS
6044 int count = 0;
6045 win_T *wp;
6047 /* If there is another tab page there always is another window. */
6048 if (first_tabpage->tp_next != NULL)
6049 return FALSE;
6051 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6052 if (!((wp->w_buffer->b_help && !curbuf->b_help)
6053 # ifdef FEAT_QUICKFIX
6054 || wp->w_p_pvw
6055 # endif
6056 ) || wp == curwin)
6057 ++count;
6058 return (count <= 1);
6059 #else
6060 return TRUE;
6061 #endif
6064 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6066 * Correct the cursor line number in other windows. Used after changing the
6067 * current buffer, and before applying autocommands.
6068 * When "do_curwin" is TRUE, also check current window.
6070 void
6071 check_lnums(do_curwin)
6072 int do_curwin;
6074 win_T *wp;
6076 #ifdef FEAT_WINDOWS
6077 tabpage_T *tp;
6079 FOR_ALL_TAB_WINDOWS(tp, wp)
6080 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6081 #else
6082 wp = curwin;
6083 if (do_curwin)
6084 #endif
6086 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6087 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6088 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6089 wp->w_topline = curbuf->b_ml.ml_line_count;
6092 #endif
6094 #if defined(FEAT_WINDOWS) || defined(PROTO)
6097 * A snapshot of the window sizes, to restore them after closing the help
6098 * window.
6099 * Only these fields are used:
6100 * fr_layout
6101 * fr_width
6102 * fr_height
6103 * fr_next
6104 * fr_child
6105 * fr_win (only valid for the old curwin, NULL otherwise)
6109 * Create a snapshot of the current frame sizes.
6111 static void
6112 make_snapshot()
6114 clear_snapshot(curtab);
6115 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6118 static void
6119 make_snapshot_rec(fr, frp)
6120 frame_T *fr;
6121 frame_T **frp;
6123 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6124 if (*frp == NULL)
6125 return;
6126 (*frp)->fr_layout = fr->fr_layout;
6127 # ifdef FEAT_VERTSPLIT
6128 (*frp)->fr_width = fr->fr_width;
6129 # endif
6130 (*frp)->fr_height = fr->fr_height;
6131 if (fr->fr_next != NULL)
6132 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6133 if (fr->fr_child != NULL)
6134 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6135 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6136 (*frp)->fr_win = curwin;
6140 * Remove any existing snapshot.
6142 static void
6143 clear_snapshot(tp)
6144 tabpage_T *tp;
6146 clear_snapshot_rec(tp->tp_snapshot);
6147 tp->tp_snapshot = NULL;
6150 static void
6151 clear_snapshot_rec(fr)
6152 frame_T *fr;
6154 if (fr != NULL)
6156 clear_snapshot_rec(fr->fr_next);
6157 clear_snapshot_rec(fr->fr_child);
6158 vim_free(fr);
6163 * Restore a previously created snapshot, if there is any.
6164 * This is only done if the screen size didn't change and the window layout is
6165 * still the same.
6167 static void
6168 restore_snapshot(close_curwin)
6169 int close_curwin; /* closing current window */
6171 win_T *wp;
6173 if (curtab->tp_snapshot != NULL
6174 # ifdef FEAT_VERTSPLIT
6175 && curtab->tp_snapshot->fr_width == topframe->fr_width
6176 # endif
6177 && curtab->tp_snapshot->fr_height == topframe->fr_height
6178 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6180 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6181 win_comp_pos();
6182 if (wp != NULL && close_curwin)
6183 win_goto(wp);
6184 redraw_all_later(CLEAR);
6186 clear_snapshot(curtab);
6190 * Check if frames "sn" and "fr" have the same layout, same following frames
6191 * and same children.
6193 static int
6194 check_snapshot_rec(sn, fr)
6195 frame_T *sn;
6196 frame_T *fr;
6198 if (sn->fr_layout != fr->fr_layout
6199 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6200 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6201 || (sn->fr_next != NULL
6202 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6203 || (sn->fr_child != NULL
6204 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6205 return FAIL;
6206 return OK;
6210 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6211 * following frames and children.
6212 * Returns a pointer to the old current window, or NULL.
6214 static win_T *
6215 restore_snapshot_rec(sn, fr)
6216 frame_T *sn;
6217 frame_T *fr;
6219 win_T *wp = NULL;
6220 win_T *wp2;
6222 fr->fr_height = sn->fr_height;
6223 # ifdef FEAT_VERTSPLIT
6224 fr->fr_width = sn->fr_width;
6225 # endif
6226 if (fr->fr_layout == FR_LEAF)
6228 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6229 # ifdef FEAT_VERTSPLIT
6230 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6231 # endif
6232 wp = sn->fr_win;
6234 if (sn->fr_next != NULL)
6236 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6237 if (wp2 != NULL)
6238 wp = wp2;
6240 if (sn->fr_child != NULL)
6242 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6243 if (wp2 != NULL)
6244 wp = wp2;
6246 return wp;
6249 #endif
6251 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6253 * Return TRUE if there is any vertically split window.
6256 win_hasvertsplit()
6258 frame_T *fr;
6260 if (topframe->fr_layout == FR_ROW)
6261 return TRUE;
6263 if (topframe->fr_layout == FR_COL)
6264 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6265 if (fr->fr_layout == FR_ROW)
6266 return TRUE;
6268 return FALSE;
6270 #endif
6272 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6274 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6275 * highlighted with the group 'grp' with priority 'prio'.
6276 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6277 * If no particular ID is desired, -1 must be specified for 'id'.
6278 * Return ID of added match, -1 on failure.
6281 match_add(wp, grp, pat, prio, id)
6282 win_T *wp;
6283 char_u *grp;
6284 char_u *pat;
6285 int prio;
6286 int id;
6288 matchitem_T *cur;
6289 matchitem_T *prev;
6290 matchitem_T *m;
6291 int hlg_id;
6292 regprog_T *regprog;
6294 if (*grp == NUL || *pat == NUL)
6295 return -1;
6296 if (id < -1 || id == 0)
6298 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6299 return -1;
6301 if (id != -1)
6303 cur = wp->w_match_head;
6304 while (cur != NULL)
6306 if (cur->id == id)
6308 EMSGN("E801: ID already taken: %ld", id);
6309 return -1;
6311 cur = cur->next;
6314 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6316 EMSG2(_(e_nogroup), grp);
6317 return -1;
6319 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6321 EMSG2(_(e_invarg2), pat);
6322 return -1;
6325 /* Find available match ID. */
6326 while (id == -1)
6328 cur = wp->w_match_head;
6329 while (cur != NULL && cur->id != wp->w_next_match_id)
6330 cur = cur->next;
6331 if (cur == NULL)
6332 id = wp->w_next_match_id;
6333 wp->w_next_match_id++;
6336 /* Build new match. */
6337 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6338 m->id = id;
6339 m->priority = prio;
6340 m->pattern = vim_strsave(pat);
6341 m->hlg_id = hlg_id;
6342 m->match.regprog = regprog;
6343 m->match.rmm_ic = FALSE;
6344 m->match.rmm_maxcol = 0;
6346 /* Insert new match. The match list is in ascending order with regard to
6347 * the match priorities. */
6348 cur = wp->w_match_head;
6349 prev = cur;
6350 while (cur != NULL && prio >= cur->priority)
6352 prev = cur;
6353 cur = cur->next;
6355 if (cur == prev)
6356 wp->w_match_head = m;
6357 else
6358 prev->next = m;
6359 m->next = cur;
6361 redraw_later(SOME_VALID);
6362 return id;
6366 * Delete match with ID 'id' in the match list of window 'wp'.
6367 * Print error messages if 'perr' is TRUE.
6370 match_delete(wp, id, perr)
6371 win_T *wp;
6372 int id;
6373 int perr;
6375 matchitem_T *cur = wp->w_match_head;
6376 matchitem_T *prev = cur;
6378 if (id < 1)
6380 if (perr == TRUE)
6381 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6382 id);
6383 return -1;
6385 while (cur != NULL && cur->id != id)
6387 prev = cur;
6388 cur = cur->next;
6390 if (cur == NULL)
6392 if (perr == TRUE)
6393 EMSGN("E803: ID not found: %ld", id);
6394 return -1;
6396 if (cur == prev)
6397 wp->w_match_head = cur->next;
6398 else
6399 prev->next = cur->next;
6400 vim_free(cur->match.regprog);
6401 vim_free(cur->pattern);
6402 vim_free(cur);
6403 redraw_later(SOME_VALID);
6404 return 0;
6408 * Delete all matches in the match list of window 'wp'.
6410 void
6411 clear_matches(wp)
6412 win_T *wp;
6414 matchitem_T *m;
6416 while (wp->w_match_head != NULL)
6418 m = wp->w_match_head->next;
6419 vim_free(wp->w_match_head->match.regprog);
6420 vim_free(wp->w_match_head->pattern);
6421 vim_free(wp->w_match_head);
6422 wp->w_match_head = m;
6424 redraw_later(SOME_VALID);
6428 * Get match from ID 'id' in window 'wp'.
6429 * Return NULL if match not found.
6431 matchitem_T *
6432 get_match(wp, id)
6433 win_T *wp;
6434 int id;
6436 matchitem_T *cur = wp->w_match_head;
6438 while (cur != NULL && cur->id != id)
6439 cur = cur->next;
6440 return cur;
6442 #endif