Merged from the latest developing branch.
[MacVim.git] / src / window.c
blob2feb9e25b5c539b933e4e0efeddd4fb1a71644ae
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,
535 ECMD_HIDE, NULL);
536 if (nchar == 'F' && lnum >= 0)
538 curwin->w_cursor.lnum = lnum;
539 check_cursor_lnum();
540 beginline(BL_SOL | BL_FIX);
543 vim_free(ptr);
545 break;
546 #endif
548 #ifdef FEAT_FIND_ID
549 /* Go to the first occurrence of the identifier under cursor along path in a
550 * new window -- webb
552 case 'i': /* Go to any match */
553 case Ctrl_I:
554 type = FIND_ANY;
555 /* FALLTHROUGH */
556 case 'd': /* Go to definition, using 'define' */
557 case Ctrl_D:
558 CHECK_CMDWIN
559 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
560 break;
561 find_pattern_in_path(ptr, 0, len, TRUE,
562 Prenum == 0 ? TRUE : FALSE, type,
563 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
564 curwin->w_set_curswant = TRUE;
565 break;
566 #endif
568 case K_KENTER:
569 case CAR:
570 #if defined(FEAT_QUICKFIX)
572 * In a quickfix window a <CR> jumps to the error under the
573 * cursor in a new window.
575 if (bt_quickfix(curbuf))
577 sprintf((char *)cbuf, "split +%ld%s",
578 (long)curwin->w_cursor.lnum,
579 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
580 do_cmdline_cmd(cbuf);
582 #endif
583 break;
586 /* CTRL-W g extended commands */
587 case 'g':
588 case Ctrl_G:
589 CHECK_CMDWIN
590 #ifdef USE_ON_FLY_SCROLL
591 dont_scroll = TRUE; /* disallow scrolling here */
592 #endif
593 ++no_mapping;
594 ++allow_keys; /* no mapping for xchar, but allow key codes */
595 if (xchar == NUL)
596 xchar = plain_vgetc();
597 #ifdef FEAT_LANGMAP
598 LANGMAP_ADJUST(xchar, TRUE);
599 #endif
600 --no_mapping;
601 --allow_keys;
602 #ifdef FEAT_CMDL_INFO
603 (void)add_to_showcmd(xchar);
604 #endif
605 switch (xchar)
607 #if defined(FEAT_QUICKFIX)
608 case '}':
609 xchar = Ctrl_RSB;
610 if (Prenum)
611 g_do_tagpreview = Prenum;
612 else
613 g_do_tagpreview = p_pvh;
614 /*FALLTHROUGH*/
615 #endif
616 case ']':
617 case Ctrl_RSB:
618 #ifdef FEAT_VISUAL
619 reset_VIsual_and_resel(); /* stop Visual mode */
620 #endif
621 if (Prenum)
622 postponed_split = Prenum;
623 else
624 postponed_split = -1;
626 /* Execute the command right here, required when
627 * "wincmd g}" was used in a function. */
628 do_nv_ident('g', xchar);
629 break;
631 #ifdef FEAT_SEARCHPATH
632 case 'f': /* CTRL-W gf: "gf" in a new tab page */
633 case 'F': /* CTRL-W gF: "gF" in a new tab page */
634 cmdmod.tab = TRUE;
635 nchar = xchar;
636 goto wingotofile;
637 #endif
638 default:
639 beep_flush();
640 break;
642 break;
644 default: beep_flush();
645 break;
650 * split the current window, implements CTRL-W s and :split
652 * "size" is the height or width for the new window, 0 to use half of current
653 * height or width.
655 * "flags":
656 * WSP_ROOM: require enough room for new window
657 * WSP_VERT: vertical split.
658 * WSP_TOP: open window at the top-left of the shell (help window).
659 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
660 * WSP_HELP: creating the help window, keep layout snapshot
662 * return FAIL for failure, OK otherwise
665 win_split(size, flags)
666 int size;
667 int flags;
669 /* When the ":tab" modifier was used open a new tab page instead. */
670 if (may_open_tabpage() == OK)
671 return OK;
673 /* Add flags from ":vertical", ":topleft" and ":botright". */
674 flags |= cmdmod.split;
675 if ((flags & WSP_TOP) && (flags & WSP_BOT))
677 EMSG(_("E442: Can't split topleft and botright at the same time"));
678 return FAIL;
681 /* When creating the help window make a snapshot of the window layout.
682 * Otherwise clear the snapshot, it's now invalid. */
683 if (flags & WSP_HELP)
684 make_snapshot();
685 else
686 clear_snapshot(curtab);
688 return win_split_ins(size, flags, NULL, 0);
692 * When "newwin" is NULL: split the current window in two.
693 * When "newwin" is not NULL: insert this window at the far
694 * top/left/right/bottom.
695 * return FAIL for failure, OK otherwise
697 static int
698 win_split_ins(size, flags, newwin, dir)
699 int size;
700 int flags;
701 win_T *newwin;
702 int dir;
704 win_T *wp = newwin;
705 win_T *oldwin;
706 int new_size = size;
707 int i;
708 int need_status = 0;
709 int do_equal = FALSE;
710 int needed;
711 int available;
712 int oldwin_height = 0;
713 int layout;
714 frame_T *frp, *curfrp;
715 int before;
717 if (flags & WSP_TOP)
718 oldwin = firstwin;
719 else if (flags & WSP_BOT)
720 oldwin = lastwin;
721 else
722 oldwin = curwin;
724 /* add a status line when p_ls == 1 and splitting the first window */
725 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
727 if (oldwin->w_height <= p_wmh && newwin == NULL)
729 EMSG(_(e_noroom));
730 return FAIL;
732 need_status = STATUS_HEIGHT;
735 #ifdef FEAT_GUI
736 /* May be needed for the scrollbars that are going to change. */
737 if (gui.in_use)
738 out_flush();
739 #endif
741 #ifdef FEAT_VERTSPLIT
742 if (flags & WSP_VERT)
744 layout = FR_ROW;
747 * Check if we are able to split the current window and compute its
748 * width.
750 needed = p_wmw + 1;
751 if (flags & WSP_ROOM)
752 needed += p_wiw - p_wmw;
753 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
755 available = topframe->fr_width;
756 needed += frame_minwidth(topframe, NULL);
758 else
759 available = oldwin->w_width;
760 if (available < needed && newwin == NULL)
762 EMSG(_(e_noroom));
763 return FAIL;
765 if (new_size == 0)
766 new_size = oldwin->w_width / 2;
767 if (new_size > oldwin->w_width - p_wmw - 1)
768 new_size = oldwin->w_width - p_wmw - 1;
769 if (new_size < p_wmw)
770 new_size = p_wmw;
772 /* if it doesn't fit in the current window, need win_equal() */
773 if (oldwin->w_width - new_size - 1 < p_wmw)
774 do_equal = TRUE;
776 /* We don't like to take lines for the new window from a
777 * 'winfixwidth' window. Take them from a window to the left or right
778 * instead, if possible. */
779 if (oldwin->w_p_wfw)
780 win_setwidth_win(oldwin->w_width + new_size, oldwin);
782 /* Only make all windows the same width if one of them (except oldwin)
783 * is wider than one of the split windows. */
784 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
785 && oldwin->w_frame->fr_parent != NULL)
787 frp = oldwin->w_frame->fr_parent->fr_child;
788 while (frp != NULL)
790 if (frp->fr_win != oldwin && frp->fr_win != NULL
791 && (frp->fr_win->w_width > new_size
792 || frp->fr_win->w_width > oldwin->w_width
793 - new_size - STATUS_HEIGHT))
795 do_equal = TRUE;
796 break;
798 frp = frp->fr_next;
802 else
803 #endif
805 layout = FR_COL;
808 * Check if we are able to split the current window and compute its
809 * height.
811 needed = p_wmh + STATUS_HEIGHT + need_status;
812 if (flags & WSP_ROOM)
813 needed += p_wh - p_wmh;
814 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
816 available = topframe->fr_height;
817 needed += frame_minheight(topframe, NULL);
819 else
821 available = oldwin->w_height;
822 needed += p_wmh;
824 if (available < needed && newwin == NULL)
826 EMSG(_(e_noroom));
827 return FAIL;
829 oldwin_height = oldwin->w_height;
830 if (need_status)
832 oldwin->w_status_height = STATUS_HEIGHT;
833 oldwin_height -= STATUS_HEIGHT;
835 if (new_size == 0)
836 new_size = oldwin_height / 2;
838 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
839 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
840 if (new_size < p_wmh)
841 new_size = p_wmh;
843 /* if it doesn't fit in the current window, need win_equal() */
844 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
845 do_equal = TRUE;
847 /* We don't like to take lines for the new window from a
848 * 'winfixheight' window. Take them from a window above or below
849 * instead, if possible. */
850 if (oldwin->w_p_wfh)
852 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
853 oldwin);
854 oldwin_height = oldwin->w_height;
855 if (need_status)
856 oldwin_height -= STATUS_HEIGHT;
859 /* Only make all windows the same height if one of them (except oldwin)
860 * is higher than one of the split windows. */
861 if (!do_equal && p_ea && size == 0
862 #ifdef FEAT_VERTSPLIT
863 && *p_ead != 'h'
864 #endif
865 && oldwin->w_frame->fr_parent != NULL)
867 frp = oldwin->w_frame->fr_parent->fr_child;
868 while (frp != NULL)
870 if (frp->fr_win != oldwin && frp->fr_win != NULL
871 && (frp->fr_win->w_height > new_size
872 || frp->fr_win->w_height > oldwin_height - new_size
873 - STATUS_HEIGHT))
875 do_equal = TRUE;
876 break;
878 frp = frp->fr_next;
884 * allocate new window structure and link it in the window list
886 if ((flags & WSP_TOP) == 0
887 && ((flags & WSP_BOT)
888 || (flags & WSP_BELOW)
889 || (!(flags & WSP_ABOVE)
890 && (
891 #ifdef FEAT_VERTSPLIT
892 (flags & WSP_VERT) ? p_spr :
893 #endif
894 p_sb))))
896 /* new window below/right of current one */
897 if (newwin == NULL)
898 wp = win_alloc(oldwin);
899 else
900 win_append(oldwin, wp);
902 else
904 if (newwin == NULL)
905 wp = win_alloc(oldwin->w_prev);
906 else
907 win_append(oldwin->w_prev, wp);
910 if (newwin == NULL)
912 if (wp == NULL)
913 return FAIL;
915 /* make the contents of the new window the same as the current one */
916 win_init(wp, curwin);
920 * Reorganise the tree of frames to insert the new window.
922 if (flags & (WSP_TOP | WSP_BOT))
924 #ifdef FEAT_VERTSPLIT
925 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
926 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
927 #else
928 if (topframe->fr_layout == FR_COL)
929 #endif
931 curfrp = topframe->fr_child;
932 if (flags & WSP_BOT)
933 while (curfrp->fr_next != NULL)
934 curfrp = curfrp->fr_next;
936 else
937 curfrp = topframe;
938 before = (flags & WSP_TOP);
940 else
942 curfrp = oldwin->w_frame;
943 if (flags & WSP_BELOW)
944 before = FALSE;
945 else if (flags & WSP_ABOVE)
946 before = TRUE;
947 else
948 #ifdef FEAT_VERTSPLIT
949 if (flags & WSP_VERT)
950 before = !p_spr;
951 else
952 #endif
953 before = !p_sb;
955 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
957 /* Need to create a new frame in the tree to make a branch. */
958 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
959 *frp = *curfrp;
960 curfrp->fr_layout = layout;
961 frp->fr_parent = curfrp;
962 frp->fr_next = NULL;
963 frp->fr_prev = NULL;
964 curfrp->fr_child = frp;
965 curfrp->fr_win = NULL;
966 curfrp = frp;
967 if (frp->fr_win != NULL)
968 oldwin->w_frame = frp;
969 else
970 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
971 frp->fr_parent = curfrp;
974 if (newwin == NULL)
976 /* Create a frame for the new window. */
977 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
978 frp->fr_layout = FR_LEAF;
979 frp->fr_win = wp;
980 wp->w_frame = frp;
982 else
983 frp = newwin->w_frame;
984 frp->fr_parent = curfrp->fr_parent;
986 /* Insert the new frame at the right place in the frame list. */
987 if (before)
988 frame_insert(curfrp, frp);
989 else
990 frame_append(curfrp, frp);
992 #ifdef FEAT_VERTSPLIT
993 if (flags & WSP_VERT)
995 wp->w_p_scr = curwin->w_p_scr;
996 if (need_status)
998 --oldwin->w_height;
999 oldwin->w_status_height = need_status;
1001 if (flags & (WSP_TOP | WSP_BOT))
1003 /* set height and row of new window to full height */
1004 wp->w_winrow = tabline_height();
1005 wp->w_height = curfrp->fr_height - (p_ls > 0);
1006 wp->w_status_height = (p_ls > 0);
1008 else
1010 /* height and row of new window is same as current window */
1011 wp->w_winrow = oldwin->w_winrow;
1012 wp->w_height = oldwin->w_height;
1013 wp->w_status_height = oldwin->w_status_height;
1015 frp->fr_height = curfrp->fr_height;
1017 /* "new_size" of the current window goes to the new window, use
1018 * one column for the vertical separator */
1019 wp->w_width = new_size;
1020 if (before)
1021 wp->w_vsep_width = 1;
1022 else
1024 wp->w_vsep_width = oldwin->w_vsep_width;
1025 oldwin->w_vsep_width = 1;
1027 if (flags & (WSP_TOP | WSP_BOT))
1029 if (flags & WSP_BOT)
1030 frame_add_vsep(curfrp);
1031 /* Set width of neighbor frame */
1032 frame_new_width(curfrp, curfrp->fr_width
1033 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1034 FALSE);
1036 else
1037 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1038 if (before) /* new window left of current one */
1040 wp->w_wincol = oldwin->w_wincol;
1041 oldwin->w_wincol += new_size + 1;
1043 else /* new window right of current one */
1044 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1045 frame_fix_width(oldwin);
1046 frame_fix_width(wp);
1048 else
1049 #endif
1051 /* width and column of new window is same as current window */
1052 #ifdef FEAT_VERTSPLIT
1053 if (flags & (WSP_TOP | WSP_BOT))
1055 wp->w_wincol = 0;
1056 wp->w_width = Columns;
1057 wp->w_vsep_width = 0;
1059 else
1061 wp->w_wincol = oldwin->w_wincol;
1062 wp->w_width = oldwin->w_width;
1063 wp->w_vsep_width = oldwin->w_vsep_width;
1065 frp->fr_width = curfrp->fr_width;
1066 #endif
1068 /* "new_size" of the current window goes to the new window, use
1069 * one row for the status line */
1070 win_new_height(wp, new_size);
1071 if (flags & (WSP_TOP | WSP_BOT))
1072 frame_new_height(curfrp, curfrp->fr_height
1073 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1074 else
1075 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1076 if (before) /* new window above current one */
1078 wp->w_winrow = oldwin->w_winrow;
1079 wp->w_status_height = STATUS_HEIGHT;
1080 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1082 else /* new window below current one */
1084 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1085 wp->w_status_height = oldwin->w_status_height;
1086 oldwin->w_status_height = STATUS_HEIGHT;
1088 #ifdef FEAT_VERTSPLIT
1089 if (flags & WSP_BOT)
1090 frame_add_statusline(curfrp);
1091 #endif
1092 frame_fix_height(wp);
1093 frame_fix_height(oldwin);
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 (void)win_comp_pos();
1100 * Both windows need redrawing
1102 redraw_win_later(wp, NOT_VALID);
1103 wp->w_redr_status = TRUE;
1104 redraw_win_later(oldwin, NOT_VALID);
1105 oldwin->w_redr_status = TRUE;
1107 if (need_status)
1109 msg_row = Rows - 1;
1110 msg_col = sc_col;
1111 msg_clr_eos_force(); /* Old command/ruler may still be there */
1112 comp_col();
1113 msg_row = Rows - 1;
1114 msg_col = 0; /* put position back at start of line */
1118 * make the new window the current window and redraw
1120 if (do_equal || dir != 0)
1121 win_equal(wp, TRUE,
1122 #ifdef FEAT_VERTSPLIT
1123 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1124 : dir == 'h' ? 'b' :
1125 #endif
1126 'v');
1128 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1129 * size was given. */
1130 #ifdef FEAT_VERTSPLIT
1131 if (flags & WSP_VERT)
1133 i = p_wiw;
1134 if (size != 0)
1135 p_wiw = size;
1137 # ifdef FEAT_GUI
1138 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1139 if (gui.in_use)
1140 gui_init_which_components(NULL);
1141 # endif
1143 else
1144 #endif
1146 i = p_wh;
1147 if (size != 0)
1148 p_wh = size;
1150 win_enter(wp, FALSE);
1151 #ifdef FEAT_VERTSPLIT
1152 if (flags & WSP_VERT)
1153 p_wiw = i;
1154 else
1155 #endif
1156 p_wh = i;
1158 return OK;
1162 * Initialize window "newp" from window "oldp".
1163 * Used when splitting a window and when creating a new tab page.
1164 * The windows will both edit the same buffer.
1166 static void
1167 win_init(newp, oldp)
1168 win_T *newp;
1169 win_T *oldp;
1171 int i;
1173 newp->w_buffer = oldp->w_buffer;
1174 oldp->w_buffer->b_nwindows++;
1175 newp->w_cursor = oldp->w_cursor;
1176 newp->w_valid = 0;
1177 newp->w_curswant = oldp->w_curswant;
1178 newp->w_set_curswant = oldp->w_set_curswant;
1179 newp->w_topline = oldp->w_topline;
1180 #ifdef FEAT_DIFF
1181 newp->w_topfill = oldp->w_topfill;
1182 #endif
1183 newp->w_leftcol = oldp->w_leftcol;
1184 newp->w_pcmark = oldp->w_pcmark;
1185 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1186 newp->w_alt_fnum = oldp->w_alt_fnum;
1187 newp->w_wrow = oldp->w_wrow;
1188 newp->w_fraction = oldp->w_fraction;
1189 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1190 #ifdef FEAT_JUMPLIST
1191 copy_jumplist(oldp, newp);
1192 #endif
1193 #ifdef FEAT_QUICKFIX
1194 copy_loclist(oldp, newp);
1195 #endif
1196 if (oldp->w_localdir != NULL)
1197 newp->w_localdir = vim_strsave(oldp->w_localdir);
1199 /* Use the same argument list. */
1200 newp->w_alist = oldp->w_alist;
1201 ++newp->w_alist->al_refcount;
1202 newp->w_arg_idx = oldp->w_arg_idx;
1205 * copy tagstack and options from existing window
1207 for (i = 0; i < oldp->w_tagstacklen; i++)
1209 newp->w_tagstack[i] = oldp->w_tagstack[i];
1210 if (newp->w_tagstack[i].tagname != NULL)
1211 newp->w_tagstack[i].tagname =
1212 vim_strsave(newp->w_tagstack[i].tagname);
1214 newp->w_tagstackidx = oldp->w_tagstackidx;
1215 newp->w_tagstacklen = oldp->w_tagstacklen;
1216 win_copy_options(oldp, newp);
1217 # ifdef FEAT_FOLDING
1218 copyFoldingState(oldp, newp);
1219 # endif
1222 #endif /* FEAT_WINDOWS */
1224 #if defined(FEAT_WINDOWS) || defined(PROTO)
1226 * Check if "win" is a pointer to an existing window.
1229 win_valid(win)
1230 win_T *win;
1232 win_T *wp;
1234 if (win == NULL)
1235 return FALSE;
1236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1237 if (wp == win)
1238 return TRUE;
1239 return FALSE;
1243 * Return the number of windows.
1246 win_count()
1248 win_T *wp;
1249 int count = 0;
1251 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1252 ++count;
1253 return count;
1257 * Make "count" windows on the screen.
1258 * Return actual number of windows on the screen.
1259 * Must be called when there is just one window, filling the whole screen
1260 * (excluding the command line).
1262 /*ARGSUSED*/
1264 make_windows(count, vertical)
1265 int count;
1266 int vertical; /* split windows vertically if TRUE */
1268 int maxcount;
1269 int todo;
1271 #ifdef FEAT_VERTSPLIT
1272 if (vertical)
1274 /* Each windows needs at least 'winminwidth' lines and a separator
1275 * column. */
1276 maxcount = (curwin->w_width + curwin->w_vsep_width
1277 - (p_wiw - p_wmw)) / (p_wmw + 1);
1279 else
1280 #endif
1282 /* Each window needs at least 'winminheight' lines and a status line. */
1283 maxcount = (curwin->w_height + curwin->w_status_height
1284 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1287 if (maxcount < 2)
1288 maxcount = 2;
1289 if (count > maxcount)
1290 count = maxcount;
1293 * add status line now, otherwise first window will be too big
1295 if (count > 1)
1296 last_status(TRUE);
1298 #ifdef FEAT_AUTOCMD
1300 * Don't execute autocommands while creating the windows. Must do that
1301 * when putting the buffers in the windows.
1303 block_autocmds();
1304 #endif
1306 /* todo is number of windows left to create */
1307 for (todo = count - 1; todo > 0; --todo)
1308 #ifdef FEAT_VERTSPLIT
1309 if (vertical)
1311 if (win_split(curwin->w_width - (curwin->w_width - todo)
1312 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1313 break;
1315 else
1316 #endif
1318 if (win_split(curwin->w_height - (curwin->w_height - todo
1319 * STATUS_HEIGHT) / (todo + 1)
1320 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1321 break;
1324 #ifdef FEAT_AUTOCMD
1325 unblock_autocmds();
1326 #endif
1328 /* return actual number of windows */
1329 return (count - todo);
1333 * Exchange current and next window
1335 static void
1336 win_exchange(Prenum)
1337 long Prenum;
1339 frame_T *frp;
1340 frame_T *frp2;
1341 win_T *wp;
1342 win_T *wp2;
1343 int temp;
1345 if (lastwin == firstwin) /* just one window */
1347 beep_flush();
1348 return;
1351 #ifdef FEAT_GUI
1352 need_mouse_correct = TRUE;
1353 #endif
1356 * find window to exchange with
1358 if (Prenum)
1360 frp = curwin->w_frame->fr_parent->fr_child;
1361 while (frp != NULL && --Prenum > 0)
1362 frp = frp->fr_next;
1364 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1365 frp = curwin->w_frame->fr_next;
1366 else /* Swap last window in row/col with previous */
1367 frp = curwin->w_frame->fr_prev;
1369 /* We can only exchange a window with another window, not with a frame
1370 * containing windows. */
1371 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1372 return;
1373 wp = frp->fr_win;
1376 * 1. remove curwin from the list. Remember after which window it was in wp2
1377 * 2. insert curwin before wp in the list
1378 * if wp != wp2
1379 * 3. remove wp from the list
1380 * 4. insert wp after wp2
1381 * 5. exchange the status line height and vsep width.
1383 wp2 = curwin->w_prev;
1384 frp2 = curwin->w_frame->fr_prev;
1385 if (wp->w_prev != curwin)
1387 win_remove(curwin, NULL);
1388 frame_remove(curwin->w_frame);
1389 win_append(wp->w_prev, curwin);
1390 frame_insert(frp, curwin->w_frame);
1392 if (wp != wp2)
1394 win_remove(wp, NULL);
1395 frame_remove(wp->w_frame);
1396 win_append(wp2, wp);
1397 if (frp2 == NULL)
1398 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1399 else
1400 frame_append(frp2, wp->w_frame);
1402 temp = curwin->w_status_height;
1403 curwin->w_status_height = wp->w_status_height;
1404 wp->w_status_height = temp;
1405 #ifdef FEAT_VERTSPLIT
1406 temp = curwin->w_vsep_width;
1407 curwin->w_vsep_width = wp->w_vsep_width;
1408 wp->w_vsep_width = temp;
1410 /* If the windows are not in the same frame, exchange the sizes to avoid
1411 * messing up the window layout. Otherwise fix the frame sizes. */
1412 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1414 temp = curwin->w_height;
1415 curwin->w_height = wp->w_height;
1416 wp->w_height = temp;
1417 temp = curwin->w_width;
1418 curwin->w_width = wp->w_width;
1419 wp->w_width = temp;
1421 else
1423 frame_fix_height(curwin);
1424 frame_fix_height(wp);
1425 frame_fix_width(curwin);
1426 frame_fix_width(wp);
1428 #endif
1430 (void)win_comp_pos(); /* recompute window positions */
1432 win_enter(wp, TRUE);
1433 redraw_later(CLEAR);
1437 * rotate windows: if upwards TRUE the second window becomes the first one
1438 * if upwards FALSE the first window becomes the second one
1440 static void
1441 win_rotate(upwards, count)
1442 int upwards;
1443 int count;
1445 win_T *wp1;
1446 win_T *wp2;
1447 frame_T *frp;
1448 int n;
1450 if (firstwin == lastwin) /* nothing to do */
1452 beep_flush();
1453 return;
1456 #ifdef FEAT_GUI
1457 need_mouse_correct = TRUE;
1458 #endif
1460 #ifdef FEAT_VERTSPLIT
1461 /* Check if all frames in this row/col have one window. */
1462 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1463 frp = frp->fr_next)
1464 if (frp->fr_win == NULL)
1466 EMSG(_("E443: Cannot rotate when another window is split"));
1467 return;
1469 #endif
1471 while (count--)
1473 if (upwards) /* first window becomes last window */
1475 /* remove first window/frame from the list */
1476 frp = curwin->w_frame->fr_parent->fr_child;
1477 wp1 = frp->fr_win;
1478 win_remove(wp1, NULL);
1479 frame_remove(frp);
1481 /* find last frame and append removed window/frame after it */
1482 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1484 win_append(frp->fr_win, wp1);
1485 frame_append(frp, wp1->w_frame);
1487 wp2 = frp->fr_win; /* previously last window */
1489 else /* last window becomes first window */
1491 /* find last window/frame in the list and remove it */
1492 for (frp = curwin->w_frame; frp->fr_next != NULL;
1493 frp = frp->fr_next)
1495 wp1 = frp->fr_win;
1496 wp2 = wp1->w_prev; /* will become last window */
1497 win_remove(wp1, NULL);
1498 frame_remove(frp);
1500 /* append the removed window/frame before the first in the list */
1501 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1502 frame_insert(frp->fr_parent->fr_child, frp);
1505 /* exchange status height and vsep width of old and new last window */
1506 n = wp2->w_status_height;
1507 wp2->w_status_height = wp1->w_status_height;
1508 wp1->w_status_height = n;
1509 frame_fix_height(wp1);
1510 frame_fix_height(wp2);
1511 #ifdef FEAT_VERTSPLIT
1512 n = wp2->w_vsep_width;
1513 wp2->w_vsep_width = wp1->w_vsep_width;
1514 wp1->w_vsep_width = n;
1515 frame_fix_width(wp1);
1516 frame_fix_width(wp2);
1517 #endif
1519 /* recompute w_winrow and w_wincol for all windows */
1520 (void)win_comp_pos();
1523 redraw_later(CLEAR);
1527 * Move the current window to the very top/bottom/left/right of the screen.
1529 static void
1530 win_totop(size, flags)
1531 int size;
1532 int flags;
1534 int dir;
1535 int height = curwin->w_height;
1537 if (lastwin == firstwin)
1539 beep_flush();
1540 return;
1543 /* Remove the window and frame from the tree of frames. */
1544 (void)winframe_remove(curwin, &dir, NULL);
1545 win_remove(curwin, NULL);
1546 last_status(FALSE); /* may need to remove last status line */
1547 (void)win_comp_pos(); /* recompute window positions */
1549 /* Split a window on the desired side and put the window there. */
1550 (void)win_split_ins(size, flags, curwin, dir);
1551 if (!(flags & WSP_VERT))
1553 win_setheight(height);
1554 if (p_ea)
1555 win_equal(curwin, TRUE, 'v');
1558 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1559 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1560 * scrollbars. Have to update them anyway. */
1561 if (gui.in_use)
1563 out_flush();
1564 gui_init_which_components(NULL);
1565 gui_update_scrollbars(TRUE);
1567 need_mouse_correct = TRUE;
1568 #endif
1573 * Move window "win1" to below/right of "win2" and make "win1" the current
1574 * window. Only works within the same frame!
1576 void
1577 win_move_after(win1, win2)
1578 win_T *win1, *win2;
1580 int height;
1582 /* check if the arguments are reasonable */
1583 if (win1 == win2)
1584 return;
1586 /* check if there is something to do */
1587 if (win2->w_next != win1)
1589 /* may need move the status line/vertical separator of the last window
1590 * */
1591 if (win1 == lastwin)
1593 height = win1->w_prev->w_status_height;
1594 win1->w_prev->w_status_height = win1->w_status_height;
1595 win1->w_status_height = height;
1596 #ifdef FEAT_VERTSPLIT
1597 if (win1->w_prev->w_vsep_width == 1)
1599 /* Remove the vertical separator from the last-but-one window,
1600 * add it to the last window. Adjust the frame widths. */
1601 win1->w_prev->w_vsep_width = 0;
1602 win1->w_prev->w_frame->fr_width -= 1;
1603 win1->w_vsep_width = 1;
1604 win1->w_frame->fr_width += 1;
1606 #endif
1608 else if (win2 == lastwin)
1610 height = win1->w_status_height;
1611 win1->w_status_height = win2->w_status_height;
1612 win2->w_status_height = height;
1613 #ifdef FEAT_VERTSPLIT
1614 if (win1->w_vsep_width == 1)
1616 /* Remove the vertical separator from win1, add it to the last
1617 * window, win2. Adjust the frame widths. */
1618 win2->w_vsep_width = 1;
1619 win2->w_frame->fr_width += 1;
1620 win1->w_vsep_width = 0;
1621 win1->w_frame->fr_width -= 1;
1623 #endif
1625 win_remove(win1, NULL);
1626 frame_remove(win1->w_frame);
1627 win_append(win2, win1);
1628 frame_append(win2->w_frame, win1->w_frame);
1630 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1631 redraw_later(NOT_VALID);
1633 win_enter(win1, FALSE);
1637 * Make all windows the same height.
1638 * 'next_curwin' will soon be the current window, make sure it has enough
1639 * rows.
1641 void
1642 win_equal(next_curwin, current, dir)
1643 win_T *next_curwin; /* pointer to current window to be or NULL */
1644 int current; /* do only frame with current window */
1645 int dir; /* 'v' for vertically, 'h' for horizontally,
1646 'b' for both, 0 for using p_ead */
1648 if (dir == 0)
1649 #ifdef FEAT_VERTSPLIT
1650 dir = *p_ead;
1651 #else
1652 dir = 'b';
1653 #endif
1654 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1655 topframe, dir, 0, tabline_height(),
1656 (int)Columns, topframe->fr_height);
1660 * Set a frame to a new position and height, spreading the available room
1661 * equally over contained frames.
1662 * The window "next_curwin" (if not NULL) should at least get the size from
1663 * 'winheight' and 'winwidth' if possible.
1665 static void
1666 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1667 win_T *next_curwin; /* pointer to current window to be or NULL */
1668 int current; /* do only frame with current window */
1669 frame_T *topfr; /* frame to set size off */
1670 int dir; /* 'v', 'h' or 'b', see win_equal() */
1671 int col; /* horizontal position for frame */
1672 int row; /* vertical position for frame */
1673 int width; /* new width of frame */
1674 int height; /* new height of frame */
1676 int n, m;
1677 int extra_sep = 0;
1678 int wincount, totwincount = 0;
1679 frame_T *fr;
1680 int next_curwin_size = 0;
1681 int room = 0;
1682 int new_size;
1683 int has_next_curwin = 0;
1684 int hnc;
1686 if (topfr->fr_layout == FR_LEAF)
1688 /* Set the width/height of this frame.
1689 * Redraw when size or position changes */
1690 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1691 #ifdef FEAT_VERTSPLIT
1692 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1693 #endif
1696 topfr->fr_win->w_winrow = row;
1697 frame_new_height(topfr, height, FALSE, FALSE);
1698 #ifdef FEAT_VERTSPLIT
1699 topfr->fr_win->w_wincol = col;
1700 frame_new_width(topfr, width, FALSE, FALSE);
1701 #endif
1702 redraw_all_later(CLEAR);
1705 #ifdef FEAT_VERTSPLIT
1706 else if (topfr->fr_layout == FR_ROW)
1708 topfr->fr_width = width;
1709 topfr->fr_height = height;
1711 if (dir != 'v') /* equalize frame widths */
1713 /* Compute the maximum number of windows horizontally in this
1714 * frame. */
1715 n = frame_minwidth(topfr, NOWIN);
1716 /* add one for the rightmost window, it doesn't have a separator */
1717 if (col + width == Columns)
1718 extra_sep = 1;
1719 else
1720 extra_sep = 0;
1721 totwincount = (n + extra_sep) / (p_wmw + 1);
1722 has_next_curwin = frame_has_win(topfr, next_curwin);
1725 * Compute width for "next_curwin" window and room available for
1726 * other windows.
1727 * "m" is the minimal width when counting p_wiw for "next_curwin".
1729 m = frame_minwidth(topfr, next_curwin);
1730 room = width - m;
1731 if (room < 0)
1733 next_curwin_size = p_wiw + room;
1734 room = 0;
1736 else
1738 next_curwin_size = -1;
1739 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1741 /* If 'winfixwidth' set keep the window width if
1742 * possible.
1743 * Watch out for this window being the next_curwin. */
1744 if (frame_fixed_width(fr))
1746 n = frame_minwidth(fr, NOWIN);
1747 new_size = fr->fr_width;
1748 if (frame_has_win(fr, next_curwin))
1750 room += p_wiw - p_wmw;
1751 next_curwin_size = 0;
1752 if (new_size < p_wiw)
1753 new_size = p_wiw;
1755 else
1756 /* These windows don't use up room. */
1757 totwincount -= (n + (fr->fr_next == NULL
1758 ? extra_sep : 0)) / (p_wmw + 1);
1759 room -= new_size - n;
1760 if (room < 0)
1762 new_size += room;
1763 room = 0;
1765 fr->fr_newwidth = new_size;
1768 if (next_curwin_size == -1)
1770 if (!has_next_curwin)
1771 next_curwin_size = 0;
1772 else if (totwincount > 1
1773 && (room + (totwincount - 2))
1774 / (totwincount - 1) > p_wiw)
1776 /* Can make all windows wider than 'winwidth', spread
1777 * the room equally. */
1778 next_curwin_size = (room + p_wiw
1779 + (totwincount - 1) * p_wmw
1780 + (totwincount - 1)) / totwincount;
1781 room -= next_curwin_size - p_wiw;
1783 else
1784 next_curwin_size = p_wiw;
1788 if (has_next_curwin)
1789 --totwincount; /* don't count curwin */
1792 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1794 n = m = 0;
1795 wincount = 1;
1796 if (fr->fr_next == NULL)
1797 /* last frame gets all that remains (avoid roundoff error) */
1798 new_size = width;
1799 else if (dir == 'v')
1800 new_size = fr->fr_width;
1801 else if (frame_fixed_width(fr))
1803 new_size = fr->fr_newwidth;
1804 wincount = 0; /* doesn't count as a sizeable window */
1806 else
1808 /* Compute the maximum number of windows horiz. in "fr". */
1809 n = frame_minwidth(fr, NOWIN);
1810 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1811 / (p_wmw + 1);
1812 m = frame_minwidth(fr, next_curwin);
1813 if (has_next_curwin)
1814 hnc = frame_has_win(fr, next_curwin);
1815 else
1816 hnc = FALSE;
1817 if (hnc) /* don't count next_curwin */
1818 --wincount;
1819 if (totwincount == 0)
1820 new_size = room;
1821 else
1822 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1823 / totwincount;
1824 if (hnc) /* add next_curwin size */
1826 next_curwin_size -= p_wiw - (m - n);
1827 new_size += next_curwin_size;
1828 room -= new_size - next_curwin_size;
1830 else
1831 room -= new_size;
1832 new_size += n;
1835 /* Skip frame that is full width when splitting or closing a
1836 * window, unless equalizing all frames. */
1837 if (!current || dir != 'v' || topfr->fr_parent != NULL
1838 || (new_size != fr->fr_width)
1839 || frame_has_win(fr, next_curwin))
1840 win_equal_rec(next_curwin, current, fr, dir, col, row,
1841 new_size, height);
1842 col += new_size;
1843 width -= new_size;
1844 totwincount -= wincount;
1847 #endif
1848 else /* topfr->fr_layout == FR_COL */
1850 #ifdef FEAT_VERTSPLIT
1851 topfr->fr_width = width;
1852 #endif
1853 topfr->fr_height = height;
1855 if (dir != 'h') /* equalize frame heights */
1857 /* Compute maximum number of windows vertically in this frame. */
1858 n = frame_minheight(topfr, NOWIN);
1859 /* add one for the bottom window if it doesn't have a statusline */
1860 if (row + height == cmdline_row && p_ls == 0)
1861 extra_sep = 1;
1862 else
1863 extra_sep = 0;
1864 totwincount = (n + extra_sep) / (p_wmh + 1);
1865 has_next_curwin = frame_has_win(topfr, next_curwin);
1868 * Compute height for "next_curwin" window and room available for
1869 * other windows.
1870 * "m" is the minimal height when counting p_wh for "next_curwin".
1872 m = frame_minheight(topfr, next_curwin);
1873 room = height - m;
1874 if (room < 0)
1876 /* The room is less then 'winheight', use all space for the
1877 * current window. */
1878 next_curwin_size = p_wh + room;
1879 room = 0;
1881 else
1883 next_curwin_size = -1;
1884 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1886 /* If 'winfixheight' set keep the window height if
1887 * possible.
1888 * Watch out for this window being the next_curwin. */
1889 if (frame_fixed_height(fr))
1891 n = frame_minheight(fr, NOWIN);
1892 new_size = fr->fr_height;
1893 if (frame_has_win(fr, next_curwin))
1895 room += p_wh - p_wmh;
1896 next_curwin_size = 0;
1897 if (new_size < p_wh)
1898 new_size = p_wh;
1900 else
1901 /* These windows don't use up room. */
1902 totwincount -= (n + (fr->fr_next == NULL
1903 ? extra_sep : 0)) / (p_wmh + 1);
1904 room -= new_size - n;
1905 if (room < 0)
1907 new_size += room;
1908 room = 0;
1910 fr->fr_newheight = new_size;
1913 if (next_curwin_size == -1)
1915 if (!has_next_curwin)
1916 next_curwin_size = 0;
1917 else if (totwincount > 1
1918 && (room + (totwincount - 2))
1919 / (totwincount - 1) > p_wh)
1921 /* can make all windows higher than 'winheight',
1922 * spread the room equally. */
1923 next_curwin_size = (room + p_wh
1924 + (totwincount - 1) * p_wmh
1925 + (totwincount - 1)) / totwincount;
1926 room -= next_curwin_size - p_wh;
1928 else
1929 next_curwin_size = p_wh;
1933 if (has_next_curwin)
1934 --totwincount; /* don't count curwin */
1937 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1939 n = m = 0;
1940 wincount = 1;
1941 if (fr->fr_next == NULL)
1942 /* last frame gets all that remains (avoid roundoff error) */
1943 new_size = height;
1944 else if (dir == 'h')
1945 new_size = fr->fr_height;
1946 else if (frame_fixed_height(fr))
1948 new_size = fr->fr_newheight;
1949 wincount = 0; /* doesn't count as a sizeable window */
1951 else
1953 /* Compute the maximum number of windows vert. in "fr". */
1954 n = frame_minheight(fr, NOWIN);
1955 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1956 / (p_wmh + 1);
1957 m = frame_minheight(fr, next_curwin);
1958 if (has_next_curwin)
1959 hnc = frame_has_win(fr, next_curwin);
1960 else
1961 hnc = FALSE;
1962 if (hnc) /* don't count next_curwin */
1963 --wincount;
1964 if (totwincount == 0)
1965 new_size = room;
1966 else
1967 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1968 / totwincount;
1969 if (hnc) /* add next_curwin size */
1971 next_curwin_size -= p_wh - (m - n);
1972 new_size += next_curwin_size;
1973 room -= new_size - next_curwin_size;
1975 else
1976 room -= new_size;
1977 new_size += n;
1979 /* Skip frame that is full width when splitting or closing a
1980 * window, unless equalizing all frames. */
1981 if (!current || dir != 'h' || topfr->fr_parent != NULL
1982 || (new_size != fr->fr_height)
1983 || frame_has_win(fr, next_curwin))
1984 win_equal_rec(next_curwin, current, fr, dir, col, row,
1985 width, new_size);
1986 row += new_size;
1987 height -= new_size;
1988 totwincount -= wincount;
1994 * close all windows for buffer 'buf'
1996 void
1997 close_windows(buf, keep_curwin)
1998 buf_T *buf;
1999 int keep_curwin; /* don't close "curwin" */
2001 win_T *wp;
2002 tabpage_T *tp, *nexttp;
2003 int h = tabline_height();
2005 ++RedrawingDisabled;
2007 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2009 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2011 win_close(wp, FALSE);
2013 /* Start all over, autocommands may change the window layout. */
2014 wp = firstwin;
2016 else
2017 wp = wp->w_next;
2020 /* Also check windows in other tab pages. */
2021 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2023 nexttp = tp->tp_next;
2024 if (tp != curtab)
2025 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2026 if (wp->w_buffer == buf)
2028 win_close_othertab(wp, FALSE, tp);
2030 /* Start all over, the tab page may be closed and
2031 * autocommands may change the window layout. */
2032 nexttp = first_tabpage;
2033 break;
2037 --RedrawingDisabled;
2039 if (h != tabline_height())
2040 shell_new_rows();
2044 * Return TRUE if the current window is the only window that exists.
2045 * Returns FALSE if there is a window, possibly in another tab page.
2047 static int
2048 last_window()
2050 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2054 * Close window "win". Only works for the current tab page.
2055 * If "free_buf" is TRUE related buffer may be unloaded.
2057 * called by :quit, :close, :xit, :wq and findtag()
2059 void
2060 win_close(win, free_buf)
2061 win_T *win;
2062 int free_buf;
2064 win_T *wp;
2065 #ifdef FEAT_AUTOCMD
2066 int other_buffer = FALSE;
2067 #endif
2068 int close_curwin = FALSE;
2069 int dir;
2070 int help_window = FALSE;
2071 tabpage_T *prev_curtab = curtab;
2073 if (last_window())
2075 EMSG(_("E444: Cannot close last window"));
2076 return;
2080 * When closing the last window in a tab page first go to another tab
2081 * page and then close the window and the tab page. This avoids that
2082 * curwin and curtab are not invalid while we are freeing memory, they may
2083 * be used in GUI events.
2085 if (firstwin == lastwin)
2087 goto_tabpage_tp(alt_tabpage());
2088 redraw_tabline = TRUE;
2090 /* Safety check: Autocommands may have closed the window when jumping
2091 * to the other tab page. */
2092 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2094 int h = tabline_height();
2096 win_close_othertab(win, free_buf, prev_curtab);
2097 if (h != tabline_height())
2098 shell_new_rows();
2100 return;
2103 /* When closing the help window, try restoring a snapshot after closing
2104 * the window. Otherwise clear the snapshot, it's now invalid. */
2105 if (win->w_buffer->b_help)
2106 help_window = TRUE;
2107 else
2108 clear_snapshot(curtab);
2110 #ifdef FEAT_AUTOCMD
2111 if (win == curwin)
2114 * Guess which window is going to be the new current window.
2115 * This may change because of the autocommands (sigh).
2117 wp = frame2win(win_altframe(win, NULL));
2120 * Be careful: If autocommands delete the window, return now.
2122 if (wp->w_buffer != curbuf)
2124 other_buffer = TRUE;
2125 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2126 if (!win_valid(win) || last_window())
2127 return;
2129 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2130 if (!win_valid(win) || last_window())
2131 return;
2132 # ifdef FEAT_EVAL
2133 /* autocmds may abort script processing */
2134 if (aborting())
2135 return;
2136 # endif
2138 #endif
2140 #ifdef FEAT_GUI
2141 /* Avoid trouble with scrollbars that are going to be deleted in
2142 * win_free(). */
2143 if (gui.in_use)
2144 out_flush();
2145 #endif
2148 * Close the link to the buffer.
2150 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2152 /* Autocommands may have closed the window already, or closed the only
2153 * other window or moved to another tab page. */
2154 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2155 return;
2157 /* Free the memory used for the window. */
2158 wp = win_free_mem(win, &dir, NULL);
2160 /* Make sure curwin isn't invalid. It can cause severe trouble when
2161 * printing an error message. For win_equal() curbuf needs to be valid
2162 * too. */
2163 if (win == curwin)
2165 curwin = wp;
2166 #ifdef FEAT_QUICKFIX
2167 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2170 * If the cursor goes to the preview or the quickfix window, try
2171 * finding another window to go to.
2173 for (;;)
2175 if (wp->w_next == NULL)
2176 wp = firstwin;
2177 else
2178 wp = wp->w_next;
2179 if (wp == curwin)
2180 break;
2181 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2183 curwin = wp;
2184 break;
2188 #endif
2189 curbuf = curwin->w_buffer;
2190 close_curwin = TRUE;
2192 if (p_ea
2193 #ifdef FEAT_VERTSPLIT
2194 && (*p_ead == 'b' || *p_ead == dir)
2195 #endif
2197 win_equal(curwin, TRUE,
2198 #ifdef FEAT_VERTSPLIT
2200 #else
2202 #endif
2204 else
2205 win_comp_pos();
2206 if (close_curwin)
2208 win_enter_ext(wp, FALSE, TRUE);
2209 #ifdef FEAT_AUTOCMD
2210 if (other_buffer)
2211 /* careful: after this wp and win may be invalid! */
2212 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2213 #endif
2217 * If last window has a status line now and we don't want one,
2218 * remove the status line.
2220 last_status(FALSE);
2222 /* After closing the help window, try restoring the window layout from
2223 * before it was opened. */
2224 if (help_window)
2225 restore_snapshot(close_curwin);
2227 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2228 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2229 if (gui.in_use && !win_hasvertsplit())
2230 gui_init_which_components(NULL);
2231 #endif
2233 redraw_all_later(NOT_VALID);
2237 * Close window "win" in tab page "tp", which is not the current tab page.
2238 * This may be the last window ih that tab page and result in closing the tab,
2239 * thus "tp" may become invalid!
2240 * Caller must check if buffer is hidden and whether the tabline needs to be
2241 * updated.
2243 void
2244 win_close_othertab(win, free_buf, tp)
2245 win_T *win;
2246 int free_buf;
2247 tabpage_T *tp;
2249 win_T *wp;
2250 int dir;
2251 tabpage_T *ptp = NULL;
2253 /* Close the link to the buffer. */
2254 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2256 /* Careful: Autocommands may have closed the tab page or made it the
2257 * current tab page. */
2258 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2260 if (ptp == NULL || tp == curtab)
2261 return;
2263 /* Autocommands may have closed the window already. */
2264 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2266 if (wp == NULL)
2267 return;
2269 /* Free the memory used for the window. */
2270 wp = win_free_mem(win, &dir, tp);
2272 /* When closing the last window in a tab page remove the tab page. */
2273 if (wp == NULL)
2275 if (tp == first_tabpage)
2276 first_tabpage = tp->tp_next;
2277 else
2279 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2280 ptp = ptp->tp_next)
2282 if (ptp == NULL)
2284 EMSG2(_(e_intern2), "win_close_othertab()");
2285 return;
2287 ptp->tp_next = tp->tp_next;
2289 free_tabpage(tp);
2294 * Free the memory used for a window.
2295 * Returns a pointer to the window that got the freed up space.
2297 static win_T *
2298 win_free_mem(win, dirp, tp)
2299 win_T *win;
2300 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2301 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2303 frame_T *frp;
2304 win_T *wp;
2306 #ifdef FEAT_FOLDING
2307 clearFolding(win);
2308 #endif
2310 /* reduce the reference count to the argument list. */
2311 alist_unlink(win->w_alist);
2313 /* Remove the window and its frame from the tree of frames. */
2314 frp = win->w_frame;
2315 wp = winframe_remove(win, dirp, tp);
2316 vim_free(frp);
2317 win_free(win, tp);
2319 /* When deleting the current window of another tab page select a new
2320 * current window. */
2321 if (tp != NULL && win == tp->tp_curwin)
2322 tp->tp_curwin = wp;
2324 return wp;
2327 #if defined(EXITFREE) || defined(PROTO)
2328 void
2329 win_free_all()
2331 int dummy;
2333 # ifdef FEAT_WINDOWS
2334 while (first_tabpage->tp_next != NULL)
2335 tabpage_close(TRUE);
2336 # endif
2338 while (firstwin != NULL)
2339 (void)win_free_mem(firstwin, &dummy, NULL);
2341 #endif
2344 * Remove a window and its frame from the tree of frames.
2345 * Returns a pointer to the window that got the freed up space.
2347 /*ARGSUSED*/
2348 static win_T *
2349 winframe_remove(win, dirp, tp)
2350 win_T *win;
2351 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2354 frame_T *frp, *frp2, *frp3;
2355 frame_T *frp_close = win->w_frame;
2356 win_T *wp;
2359 * If there is only one window there is nothing to remove.
2361 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2362 return NULL;
2365 * Remove the window from its frame.
2367 frp2 = win_altframe(win, tp);
2368 wp = frame2win(frp2);
2370 /* Remove this frame from the list of frames. */
2371 frame_remove(frp_close);
2373 #ifdef FEAT_VERTSPLIT
2374 if (frp_close->fr_parent->fr_layout == FR_COL)
2376 #endif
2377 /* When 'winfixheight' is set, try to find another frame in the column
2378 * (as close to the closed frame as possible) to distribute the height
2379 * to. */
2380 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2382 frp = frp_close->fr_prev;
2383 frp3 = frp_close->fr_next;
2384 while (frp != NULL || frp3 != NULL)
2386 if (frp != NULL)
2388 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2390 frp2 = frp;
2391 wp = frp->fr_win;
2392 break;
2394 frp = frp->fr_prev;
2396 if (frp3 != NULL)
2398 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2400 frp2 = frp3;
2401 wp = frp3->fr_win;
2402 break;
2404 frp3 = frp3->fr_next;
2408 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2409 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2410 #ifdef FEAT_VERTSPLIT
2411 *dirp = 'v';
2413 else
2415 /* When 'winfixwidth' is set, try to find another frame in the column
2416 * (as close to the closed frame as possible) to distribute the width
2417 * to. */
2418 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2420 frp = frp_close->fr_prev;
2421 frp3 = frp_close->fr_next;
2422 while (frp != NULL || frp3 != NULL)
2424 if (frp != NULL)
2426 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2428 frp2 = frp;
2429 wp = frp->fr_win;
2430 break;
2432 frp = frp->fr_prev;
2434 if (frp3 != NULL)
2436 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2438 frp2 = frp3;
2439 wp = frp3->fr_win;
2440 break;
2442 frp3 = frp3->fr_next;
2446 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2447 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2448 *dirp = 'h';
2450 #endif
2452 /* If rows/columns go to a window below/right its positions need to be
2453 * updated. Can only be done after the sizes have been updated. */
2454 if (frp2 == frp_close->fr_next)
2456 int row = win->w_winrow;
2457 int col = W_WINCOL(win);
2459 frame_comp_pos(frp2, &row, &col);
2462 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2464 /* There is no other frame in this list, move its info to the parent
2465 * and remove it. */
2466 frp2->fr_parent->fr_layout = frp2->fr_layout;
2467 frp2->fr_parent->fr_child = frp2->fr_child;
2468 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2469 frp->fr_parent = frp2->fr_parent;
2470 frp2->fr_parent->fr_win = frp2->fr_win;
2471 if (frp2->fr_win != NULL)
2472 frp2->fr_win->w_frame = frp2->fr_parent;
2473 frp = frp2->fr_parent;
2474 vim_free(frp2);
2476 frp2 = frp->fr_parent;
2477 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2479 /* The frame above the parent has the same layout, have to merge
2480 * the frames into this list. */
2481 if (frp2->fr_child == frp)
2482 frp2->fr_child = frp->fr_child;
2483 frp->fr_child->fr_prev = frp->fr_prev;
2484 if (frp->fr_prev != NULL)
2485 frp->fr_prev->fr_next = frp->fr_child;
2486 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2488 frp3->fr_parent = frp2;
2489 if (frp3->fr_next == NULL)
2491 frp3->fr_next = frp->fr_next;
2492 if (frp->fr_next != NULL)
2493 frp->fr_next->fr_prev = frp3;
2494 break;
2497 vim_free(frp);
2501 return wp;
2505 * Find out which frame is going to get the freed up space when "win" is
2506 * closed.
2507 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2508 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2509 * This makes opening a window and closing it immediately keep the same window
2510 * layout.
2512 static frame_T *
2513 win_altframe(win, tp)
2514 win_T *win;
2515 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2517 frame_T *frp;
2518 int b;
2520 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2521 /* Last window in this tab page, will go to next tab page. */
2522 return alt_tabpage()->tp_curwin->w_frame;
2524 frp = win->w_frame;
2525 #ifdef FEAT_VERTSPLIT
2526 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2527 b = p_spr;
2528 else
2529 #endif
2530 b = p_sb;
2531 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2532 return frp->fr_next;
2533 return frp->fr_prev;
2537 * Return the tabpage that will be used if the current one is closed.
2539 static tabpage_T *
2540 alt_tabpage()
2542 tabpage_T *tp;
2544 /* Use the next tab page if possible. */
2545 if (curtab->tp_next != NULL)
2546 return curtab->tp_next;
2548 /* Find the last but one tab page. */
2549 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2551 return tp;
2555 * Find the left-upper window in frame "frp".
2557 static win_T *
2558 frame2win(frp)
2559 frame_T *frp;
2561 while (frp->fr_win == NULL)
2562 frp = frp->fr_child;
2563 return frp->fr_win;
2567 * Return TRUE if frame "frp" contains window "wp".
2569 static int
2570 frame_has_win(frp, wp)
2571 frame_T *frp;
2572 win_T *wp;
2574 frame_T *p;
2576 if (frp->fr_layout == FR_LEAF)
2577 return frp->fr_win == wp;
2579 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2580 if (frame_has_win(p, wp))
2581 return TRUE;
2582 return FALSE;
2586 * Set a new height for a frame. Recursively sets the height for contained
2587 * frames and windows. Caller must take care of positions.
2589 static void
2590 frame_new_height(topfrp, height, topfirst, wfh)
2591 frame_T *topfrp;
2592 int height;
2593 int topfirst; /* resize topmost contained frame first */
2594 int wfh; /* obey 'winfixheight' when there is a choice;
2595 may cause the height not to be set */
2597 frame_T *frp;
2598 int extra_lines;
2599 int h;
2601 if (topfrp->fr_win != NULL)
2603 /* Simple case: just one window. */
2604 win_new_height(topfrp->fr_win,
2605 height - topfrp->fr_win->w_status_height);
2607 #ifdef FEAT_VERTSPLIT
2608 else if (topfrp->fr_layout == FR_ROW)
2612 /* All frames in this row get the same new height. */
2613 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2615 frame_new_height(frp, height, topfirst, wfh);
2616 if (frp->fr_height > height)
2618 /* Could not fit the windows, make the whole row higher. */
2619 height = frp->fr_height;
2620 break;
2624 while (frp != NULL);
2626 #endif
2627 else /* fr_layout == FR_COL */
2629 /* Complicated case: Resize a column of frames. Resize the bottom
2630 * frame first, frames above that when needed. */
2632 frp = topfrp->fr_child;
2633 if (wfh)
2634 /* Advance past frames with one window with 'wfh' set. */
2635 while (frame_fixed_height(frp))
2637 frp = frp->fr_next;
2638 if (frp == NULL)
2639 return; /* no frame without 'wfh', give up */
2641 if (!topfirst)
2643 /* Find the bottom frame of this column */
2644 while (frp->fr_next != NULL)
2645 frp = frp->fr_next;
2646 if (wfh)
2647 /* Advance back for frames with one window with 'wfh' set. */
2648 while (frame_fixed_height(frp))
2649 frp = frp->fr_prev;
2652 extra_lines = height - topfrp->fr_height;
2653 if (extra_lines < 0)
2655 /* reduce height of contained frames, bottom or top frame first */
2656 while (frp != NULL)
2658 h = frame_minheight(frp, NULL);
2659 if (frp->fr_height + extra_lines < h)
2661 extra_lines += frp->fr_height - h;
2662 frame_new_height(frp, h, topfirst, wfh);
2664 else
2666 frame_new_height(frp, frp->fr_height + extra_lines,
2667 topfirst, wfh);
2668 break;
2670 if (topfirst)
2673 frp = frp->fr_next;
2674 while (wfh && frp != NULL && frame_fixed_height(frp));
2676 else
2679 frp = frp->fr_prev;
2680 while (wfh && frp != NULL && frame_fixed_height(frp));
2682 /* Increase "height" if we could not reduce enough frames. */
2683 if (frp == NULL)
2684 height -= extra_lines;
2687 else if (extra_lines > 0)
2689 /* increase height of bottom or top frame */
2690 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2693 topfrp->fr_height = height;
2697 * Return TRUE if height of frame "frp" should not be changed because of
2698 * the 'winfixheight' option.
2700 static int
2701 frame_fixed_height(frp)
2702 frame_T *frp;
2704 /* frame with one window: fixed height if 'winfixheight' set. */
2705 if (frp->fr_win != NULL)
2706 return frp->fr_win->w_p_wfh;
2708 if (frp->fr_layout == FR_ROW)
2710 /* The frame is fixed height if one of the frames in the row is fixed
2711 * height. */
2712 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2713 if (frame_fixed_height(frp))
2714 return TRUE;
2715 return FALSE;
2718 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2719 * frames in the row are fixed height. */
2720 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2721 if (!frame_fixed_height(frp))
2722 return FALSE;
2723 return TRUE;
2726 #ifdef FEAT_VERTSPLIT
2728 * Return TRUE if width of frame "frp" should not be changed because of
2729 * the 'winfixwidth' option.
2731 static int
2732 frame_fixed_width(frp)
2733 frame_T *frp;
2735 /* frame with one window: fixed width if 'winfixwidth' set. */
2736 if (frp->fr_win != NULL)
2737 return frp->fr_win->w_p_wfw;
2739 if (frp->fr_layout == FR_COL)
2741 /* The frame is fixed width if one of the frames in the row is fixed
2742 * width. */
2743 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2744 if (frame_fixed_width(frp))
2745 return TRUE;
2746 return FALSE;
2749 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2750 * frames in the row are fixed width. */
2751 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2752 if (!frame_fixed_width(frp))
2753 return FALSE;
2754 return TRUE;
2758 * Add a status line to windows at the bottom of "frp".
2759 * Note: Does not check if there is room!
2761 static void
2762 frame_add_statusline(frp)
2763 frame_T *frp;
2765 win_T *wp;
2767 if (frp->fr_layout == FR_LEAF)
2769 wp = frp->fr_win;
2770 if (wp->w_status_height == 0)
2772 if (wp->w_height > 0) /* don't make it negative */
2773 --wp->w_height;
2774 wp->w_status_height = STATUS_HEIGHT;
2777 else if (frp->fr_layout == FR_ROW)
2779 /* Handle all the frames in the row. */
2780 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2781 frame_add_statusline(frp);
2783 else /* frp->fr_layout == FR_COL */
2785 /* Only need to handle the last frame in the column. */
2786 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2788 frame_add_statusline(frp);
2793 * Set width of a frame. Handles recursively going through contained frames.
2794 * May remove separator line for windows at the right side (for win_close()).
2796 static void
2797 frame_new_width(topfrp, width, leftfirst, wfw)
2798 frame_T *topfrp;
2799 int width;
2800 int leftfirst; /* resize leftmost contained frame first */
2801 int wfw; /* obey 'winfixwidth' when there is a choice;
2802 may cause the width not to be set */
2804 frame_T *frp;
2805 int extra_cols;
2806 int w;
2807 win_T *wp;
2809 if (topfrp->fr_layout == FR_LEAF)
2811 /* Simple case: just one window. */
2812 wp = topfrp->fr_win;
2813 /* Find out if there are any windows right of this one. */
2814 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2815 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2816 break;
2817 if (frp->fr_parent == NULL)
2818 wp->w_vsep_width = 0;
2819 win_new_width(wp, width - wp->w_vsep_width);
2821 else if (topfrp->fr_layout == FR_COL)
2825 /* All frames in this column get the same new width. */
2826 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2828 frame_new_width(frp, width, leftfirst, wfw);
2829 if (frp->fr_width > width)
2831 /* Could not fit the windows, make whole column wider. */
2832 width = frp->fr_width;
2833 break;
2836 } while (frp != NULL);
2838 else /* fr_layout == FR_ROW */
2840 /* Complicated case: Resize a row of frames. Resize the rightmost
2841 * frame first, frames left of it when needed. */
2843 frp = topfrp->fr_child;
2844 if (wfw)
2845 /* Advance past frames with one window with 'wfw' set. */
2846 while (frame_fixed_width(frp))
2848 frp = frp->fr_next;
2849 if (frp == NULL)
2850 return; /* no frame without 'wfw', give up */
2852 if (!leftfirst)
2854 /* Find the rightmost frame of this row */
2855 while (frp->fr_next != NULL)
2856 frp = frp->fr_next;
2857 if (wfw)
2858 /* Advance back for frames with one window with 'wfw' set. */
2859 while (frame_fixed_width(frp))
2860 frp = frp->fr_prev;
2863 extra_cols = width - topfrp->fr_width;
2864 if (extra_cols < 0)
2866 /* reduce frame width, rightmost frame first */
2867 while (frp != NULL)
2869 w = frame_minwidth(frp, NULL);
2870 if (frp->fr_width + extra_cols < w)
2872 extra_cols += frp->fr_width - w;
2873 frame_new_width(frp, w, leftfirst, wfw);
2875 else
2877 frame_new_width(frp, frp->fr_width + extra_cols,
2878 leftfirst, wfw);
2879 break;
2881 if (leftfirst)
2884 frp = frp->fr_next;
2885 while (wfw && frp != NULL && frame_fixed_width(frp));
2887 else
2890 frp = frp->fr_prev;
2891 while (wfw && frp != NULL && frame_fixed_width(frp));
2893 /* Increase "width" if we could not reduce enough frames. */
2894 if (frp == NULL)
2895 width -= extra_cols;
2898 else if (extra_cols > 0)
2900 /* increase width of rightmost frame */
2901 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2904 topfrp->fr_width = width;
2908 * Add the vertical separator to windows at the right side of "frp".
2909 * Note: Does not check if there is room!
2911 static void
2912 frame_add_vsep(frp)
2913 frame_T *frp;
2915 win_T *wp;
2917 if (frp->fr_layout == FR_LEAF)
2919 wp = frp->fr_win;
2920 if (wp->w_vsep_width == 0)
2922 if (wp->w_width > 0) /* don't make it negative */
2923 --wp->w_width;
2924 wp->w_vsep_width = 1;
2927 else if (frp->fr_layout == FR_COL)
2929 /* Handle all the frames in the column. */
2930 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2931 frame_add_vsep(frp);
2933 else /* frp->fr_layout == FR_ROW */
2935 /* Only need to handle the last frame in the row. */
2936 frp = frp->fr_child;
2937 while (frp->fr_next != NULL)
2938 frp = frp->fr_next;
2939 frame_add_vsep(frp);
2944 * Set frame width from the window it contains.
2946 static void
2947 frame_fix_width(wp)
2948 win_T *wp;
2950 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2952 #endif
2955 * Set frame height from the window it contains.
2957 static void
2958 frame_fix_height(wp)
2959 win_T *wp;
2961 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2965 * Compute the minimal height for frame "topfrp".
2966 * Uses the 'winminheight' option.
2967 * When "next_curwin" isn't NULL, use p_wh for this window.
2968 * When "next_curwin" is NOWIN, don't use at least one line for the current
2969 * window.
2971 static int
2972 frame_minheight(topfrp, next_curwin)
2973 frame_T *topfrp;
2974 win_T *next_curwin;
2976 frame_T *frp;
2977 int m;
2978 #ifdef FEAT_VERTSPLIT
2979 int n;
2980 #endif
2982 if (topfrp->fr_win != NULL)
2984 if (topfrp->fr_win == next_curwin)
2985 m = p_wh + topfrp->fr_win->w_status_height;
2986 else
2988 /* window: minimal height of the window plus status line */
2989 m = p_wmh + topfrp->fr_win->w_status_height;
2990 /* Current window is minimal one line high */
2991 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2992 ++m;
2995 #ifdef FEAT_VERTSPLIT
2996 else if (topfrp->fr_layout == FR_ROW)
2998 /* get the minimal height from each frame in this row */
2999 m = 0;
3000 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3002 n = frame_minheight(frp, next_curwin);
3003 if (n > m)
3004 m = n;
3007 #endif
3008 else
3010 /* Add up the minimal heights for all frames in this column. */
3011 m = 0;
3012 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3013 m += frame_minheight(frp, next_curwin);
3016 return m;
3019 #ifdef FEAT_VERTSPLIT
3021 * Compute the minimal width for frame "topfrp".
3022 * When "next_curwin" isn't NULL, use p_wiw for this window.
3023 * When "next_curwin" is NOWIN, don't use at least one column for the current
3024 * window.
3026 static int
3027 frame_minwidth(topfrp, next_curwin)
3028 frame_T *topfrp;
3029 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3031 frame_T *frp;
3032 int m, n;
3034 if (topfrp->fr_win != NULL)
3036 if (topfrp->fr_win == next_curwin)
3037 m = p_wiw + topfrp->fr_win->w_vsep_width;
3038 else
3040 /* window: minimal width of the window plus separator column */
3041 m = p_wmw + topfrp->fr_win->w_vsep_width;
3042 /* Current window is minimal one column wide */
3043 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3044 ++m;
3047 else if (topfrp->fr_layout == FR_COL)
3049 /* get the minimal width from each frame in this column */
3050 m = 0;
3051 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3053 n = frame_minwidth(frp, next_curwin);
3054 if (n > m)
3055 m = n;
3058 else
3060 /* Add up the minimal widths for all frames in this row. */
3061 m = 0;
3062 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3063 m += frame_minwidth(frp, next_curwin);
3066 return m;
3068 #endif
3072 * Try to close all windows except current one.
3073 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3074 * used and the buffer was modified.
3076 * Used by ":bdel" and ":only".
3078 void
3079 close_others(message, forceit)
3080 int message;
3081 int forceit; /* always hide all other windows */
3083 win_T *wp;
3084 win_T *nextwp;
3085 int r;
3087 if (lastwin == firstwin)
3089 if (message
3090 #ifdef FEAT_AUTOCMD
3091 && !autocmd_busy
3092 #endif
3094 MSG(_(m_onlyone));
3095 return;
3098 /* Be very careful here: autocommands may change the window layout. */
3099 for (wp = firstwin; win_valid(wp); wp = nextwp)
3101 nextwp = wp->w_next;
3102 if (wp != curwin) /* don't close current window */
3105 /* Check if it's allowed to abandon this window */
3106 r = can_abandon(wp->w_buffer, forceit);
3107 #ifdef FEAT_AUTOCMD
3108 if (!win_valid(wp)) /* autocommands messed wp up */
3110 nextwp = firstwin;
3111 continue;
3113 #endif
3114 if (!r)
3116 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3117 if (message && (p_confirm || cmdmod.confirm) && p_write)
3119 dialog_changed(wp->w_buffer, FALSE);
3120 # ifdef FEAT_AUTOCMD
3121 if (!win_valid(wp)) /* autocommands messed wp up */
3123 nextwp = firstwin;
3124 continue;
3126 # endif
3128 if (bufIsChanged(wp->w_buffer))
3129 #endif
3130 continue;
3132 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3136 if (message && lastwin != firstwin)
3137 EMSG(_("E445: Other window contains changes"));
3140 #endif /* FEAT_WINDOWS */
3143 * Init the current window "curwin".
3144 * Called when a new file is being edited.
3146 void
3147 curwin_init()
3149 redraw_win_later(curwin, NOT_VALID);
3150 curwin->w_lines_valid = 0;
3151 curwin->w_cursor.lnum = 1;
3152 curwin->w_curswant = curwin->w_cursor.col = 0;
3153 #ifdef FEAT_VIRTUALEDIT
3154 curwin->w_cursor.coladd = 0;
3155 #endif
3156 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3157 curwin->w_pcmark.col = 0;
3158 curwin->w_prev_pcmark.lnum = 0;
3159 curwin->w_prev_pcmark.col = 0;
3160 curwin->w_topline = 1;
3161 #ifdef FEAT_DIFF
3162 curwin->w_topfill = 0;
3163 #endif
3164 curwin->w_botline = 2;
3165 #ifdef FEAT_FKMAP
3166 if (curwin->w_p_rl)
3167 curwin->w_farsi = W_CONV + W_R_L;
3168 else
3169 curwin->w_farsi = W_CONV;
3170 #endif
3174 * Allocate the first window and put an empty buffer in it.
3175 * Called from main().
3176 * Return FAIL when something goes wrong (out of memory).
3179 win_alloc_first()
3181 if (win_alloc_firstwin(NULL) == FAIL)
3182 return FAIL;
3184 #ifdef FEAT_WINDOWS
3185 first_tabpage = alloc_tabpage();
3186 if (first_tabpage == NULL)
3187 return FAIL;
3188 first_tabpage->tp_topframe = topframe;
3189 curtab = first_tabpage;
3190 #endif
3191 return OK;
3195 * Allocate the first window or the first window in a new tab page.
3196 * When "oldwin" is NULL create an empty buffer for it.
3197 * When "oldwin" is not NULL copy info from it to the new window (only with
3198 * FEAT_WINDOWS).
3199 * Return FAIL when something goes wrong (out of memory).
3201 static int
3202 win_alloc_firstwin(oldwin)
3203 win_T *oldwin;
3205 curwin = win_alloc(NULL);
3206 if (oldwin == NULL)
3208 /* Very first window, need to create an empty buffer for it and
3209 * initialize from scratch. */
3210 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3211 if (curwin == NULL || curbuf == NULL)
3212 return FAIL;
3213 curwin->w_buffer = curbuf;
3214 curbuf->b_nwindows = 1; /* there is one window */
3215 #ifdef FEAT_WINDOWS
3216 curwin->w_alist = &global_alist;
3217 #endif
3218 curwin_init(); /* init current window */
3220 #ifdef FEAT_WINDOWS
3221 else
3223 /* First window in new tab page, initialize it from "oldwin". */
3224 win_init(curwin, oldwin);
3226 # ifdef FEAT_SCROLLBIND
3227 /* We don't want scroll-binding in the first window. */
3228 curwin->w_p_scb = FALSE;
3229 # endif
3231 #endif
3233 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3234 if (topframe == NULL)
3235 return FAIL;
3236 topframe->fr_layout = FR_LEAF;
3237 #ifdef FEAT_VERTSPLIT
3238 topframe->fr_width = Columns;
3239 #endif
3240 topframe->fr_height = Rows - p_ch;
3241 topframe->fr_win = curwin;
3242 curwin->w_frame = topframe;
3244 return OK;
3248 * Initialize the window and frame size to the maximum.
3250 void
3251 win_init_size()
3253 firstwin->w_height = ROWS_AVAIL;
3254 topframe->fr_height = ROWS_AVAIL;
3255 #ifdef FEAT_VERTSPLIT
3256 firstwin->w_width = Columns;
3257 topframe->fr_width = Columns;
3258 #endif
3261 #if defined(FEAT_WINDOWS) || defined(PROTO)
3264 * Allocate a new tabpage_T and init the values.
3265 * Returns NULL when out of memory.
3267 static tabpage_T *
3268 alloc_tabpage()
3270 tabpage_T *tp;
3272 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3273 if (tp != NULL)
3275 # ifdef FEAT_GUI
3276 int i;
3278 for (i = 0; i < 3; i++)
3279 tp->tp_prev_which_scrollbars[i] = -1;
3280 # endif
3281 # ifdef FEAT_DIFF
3282 tp->tp_diff_invalid = TRUE;
3283 # endif
3284 #ifdef FEAT_EVAL
3285 /* init t: variables */
3286 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3287 #endif
3288 tp->tp_ch_used = p_ch;
3290 return tp;
3293 void
3294 free_tabpage(tp)
3295 tabpage_T *tp;
3297 # ifdef FEAT_DIFF
3298 diff_clear(tp);
3299 # endif
3300 clear_snapshot(tp);
3301 #ifdef FEAT_EVAL
3302 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3303 #endif
3304 vim_free(tp);
3308 * Create a new Tab page with one window.
3309 * It will edit the current buffer, like after ":split".
3310 * When "after" is 0 put it just after the current Tab page.
3311 * Otherwise put it just before tab page "after".
3312 * Return FAIL or OK.
3315 win_new_tabpage(after)
3316 int after;
3318 tabpage_T *tp = curtab;
3319 tabpage_T *newtp;
3320 int n;
3322 newtp = alloc_tabpage();
3323 if (newtp == NULL)
3324 return FAIL;
3326 /* Remember the current windows in this Tab page. */
3327 if (leave_tabpage(curbuf) == FAIL)
3329 vim_free(newtp);
3330 return FAIL;
3332 curtab = newtp;
3334 /* Create a new empty window. */
3335 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3337 /* Make the new Tab page the new topframe. */
3338 if (after == 1)
3340 /* New tab page becomes the first one. */
3341 newtp->tp_next = first_tabpage;
3342 first_tabpage = newtp;
3344 else
3346 if (after > 0)
3348 /* Put new tab page before tab page "after". */
3349 n = 2;
3350 for (tp = first_tabpage; tp->tp_next != NULL
3351 && n < after; tp = tp->tp_next)
3352 ++n;
3354 newtp->tp_next = tp->tp_next;
3355 tp->tp_next = newtp;
3357 win_init_size();
3358 firstwin->w_winrow = tabline_height();
3359 win_comp_scroll(curwin);
3361 newtp->tp_topframe = topframe;
3362 last_status(FALSE);
3364 #if defined(FEAT_GUI)
3365 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3366 * scrollbars. Have to update them anyway. */
3367 if (gui.in_use && starting == 0)
3369 gui_init_which_components(NULL);
3370 gui_update_scrollbars(TRUE);
3372 need_mouse_correct = TRUE;
3373 #endif
3375 redraw_all_later(CLEAR);
3376 #ifdef FEAT_AUTOCMD
3377 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3378 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3379 #endif
3380 return OK;
3383 /* Failed, get back the previous Tab page */
3384 enter_tabpage(curtab, curbuf);
3385 return FAIL;
3389 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3390 * like with ":split".
3391 * Returns OK if a new tab page was created, FAIL otherwise.
3394 may_open_tabpage()
3396 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3398 if (n != 0)
3400 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3401 postponed_split_tab = 0;
3402 return win_new_tabpage(n);
3404 return FAIL;
3408 * Create up to "maxcount" tabpages with empty windows.
3409 * Returns the number of resulting tab pages.
3412 make_tabpages(maxcount)
3413 int maxcount;
3415 int count = maxcount;
3416 int todo;
3418 /* Limit to 'tabpagemax' tabs. */
3419 if (count > p_tpm)
3420 count = p_tpm;
3422 #ifdef FEAT_AUTOCMD
3424 * Don't execute autocommands while creating the tab pages. Must do that
3425 * when putting the buffers in the windows.
3427 block_autocmds();
3428 #endif
3430 for (todo = count - 1; todo > 0; --todo)
3431 if (win_new_tabpage(0) == FAIL)
3432 break;
3434 #ifdef FEAT_AUTOCMD
3435 unblock_autocmds();
3436 #endif
3438 /* return actual number of tab pages */
3439 return (count - todo);
3443 * Return TRUE when "tpc" points to a valid tab page.
3446 valid_tabpage(tpc)
3447 tabpage_T *tpc;
3449 tabpage_T *tp;
3451 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3452 if (tp == tpc)
3453 return TRUE;
3454 return FALSE;
3458 * Find tab page "n" (first one is 1). Returns NULL when not found.
3460 tabpage_T *
3461 find_tabpage(n)
3462 int n;
3464 tabpage_T *tp;
3465 int i = 1;
3467 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3468 ++i;
3469 return tp;
3473 * Get index of tab page "tp". First one has index 1.
3474 * When not found returns number of tab pages plus one.
3477 tabpage_index(ftp)
3478 tabpage_T *ftp;
3480 int i = 1;
3481 tabpage_T *tp;
3483 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3484 ++i;
3485 return i;
3489 * Prepare for leaving the current tab page.
3490 * When autocomands change "curtab" we don't leave the tab page and return
3491 * FAIL.
3492 * Careful: When OK is returned need to get a new tab page very very soon!
3494 /*ARGSUSED*/
3495 static int
3496 leave_tabpage(new_curbuf)
3497 buf_T *new_curbuf; /* what is going to be the new curbuf,
3498 NULL if unknown */
3500 tabpage_T *tp = curtab;
3502 #ifdef FEAT_VISUAL
3503 reset_VIsual_and_resel(); /* stop Visual mode */
3504 #endif
3505 #ifdef FEAT_AUTOCMD
3506 if (new_curbuf != curbuf)
3508 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3509 if (curtab != tp)
3510 return FAIL;
3512 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3513 if (curtab != tp)
3514 return FAIL;
3515 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3516 if (curtab != tp)
3517 return FAIL;
3518 #endif
3519 #if defined(FEAT_GUI)
3520 /* Remove the scrollbars. They may be added back later. */
3521 if (gui.in_use)
3522 gui_remove_scrollbars();
3523 #endif
3524 tp->tp_curwin = curwin;
3525 tp->tp_prevwin = prevwin;
3526 tp->tp_firstwin = firstwin;
3527 tp->tp_lastwin = lastwin;
3528 tp->tp_old_Rows = Rows;
3529 tp->tp_old_Columns = Columns;
3530 firstwin = NULL;
3531 lastwin = NULL;
3532 return OK;
3536 * Start using tab page "tp".
3537 * Only to be used after leave_tabpage() or freeing the current tab page.
3539 /*ARGSUSED*/
3540 static void
3541 enter_tabpage(tp, old_curbuf)
3542 tabpage_T *tp;
3543 buf_T *old_curbuf;
3545 int old_off = tp->tp_firstwin->w_winrow;
3546 win_T *next_prevwin = tp->tp_prevwin;
3548 curtab = tp;
3549 firstwin = tp->tp_firstwin;
3550 lastwin = tp->tp_lastwin;
3551 topframe = tp->tp_topframe;
3553 /* We would like doing the TabEnter event first, but we don't have a
3554 * valid current window yet, which may break some commands.
3555 * This triggers autocommands, thus may make "tp" invalid. */
3556 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3557 prevwin = next_prevwin;
3559 #ifdef FEAT_AUTOCMD
3560 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3562 if (old_curbuf != curbuf)
3563 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3564 #endif
3566 last_status(FALSE); /* status line may appear or disappear */
3567 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3568 must_redraw = CLEAR; /* need to redraw everything */
3569 #ifdef FEAT_DIFF
3570 diff_need_scrollbind = TRUE;
3571 #endif
3573 /* The tabpage line may have appeared or disappeared, may need to resize
3574 * the frames for that. When the Vim window was resized need to update
3575 * frame sizes too. Use the stored value of p_ch, so that it can be
3576 * different for each tab page. */
3577 p_ch = curtab->tp_ch_used;
3578 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3579 #ifdef FEAT_GUI_TABLINE
3580 && !gui_use_tabline()
3581 #endif
3583 shell_new_rows();
3584 #ifdef FEAT_VERTSPLIT
3585 if (curtab->tp_old_Columns != Columns && starting == 0)
3586 shell_new_columns(); /* update window widths */
3587 #endif
3589 #if defined(FEAT_GUI)
3590 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3591 * scrollbars. Have to update them anyway. */
3592 if (gui.in_use && starting == 0)
3594 gui_init_which_components(NULL);
3595 gui_update_scrollbars(TRUE);
3597 need_mouse_correct = TRUE;
3598 #endif
3600 redraw_all_later(CLEAR);
3604 * Go to tab page "n". For ":tab N" and "Ngt".
3605 * When "n" is 9999 go to the last tab page.
3607 void
3608 goto_tabpage(n)
3609 int n;
3611 tabpage_T *tp;
3612 tabpage_T *ttp;
3613 int i;
3615 if (text_locked())
3617 /* Not allowed when editing the command line. */
3618 #ifdef FEAT_CMDWIN
3619 if (cmdwin_type != 0)
3620 EMSG(_(e_cmdwin));
3621 else
3622 #endif
3623 EMSG(_(e_secure));
3624 return;
3627 /* If there is only one it can't work. */
3628 if (first_tabpage->tp_next == NULL)
3630 if (n > 1)
3631 beep_flush();
3632 return;
3635 if (n == 0)
3637 /* No count, go to next tab page, wrap around end. */
3638 if (curtab->tp_next == NULL)
3639 tp = first_tabpage;
3640 else
3641 tp = curtab->tp_next;
3643 else if (n < 0)
3645 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3646 * this N times. */
3647 ttp = curtab;
3648 for (i = n; i < 0; ++i)
3650 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3651 tp = tp->tp_next)
3653 ttp = tp;
3656 else if (n == 9999)
3658 /* Go to last tab page. */
3659 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3662 else
3664 /* Go to tab page "n". */
3665 tp = find_tabpage(n);
3666 if (tp == NULL)
3668 beep_flush();
3669 return;
3673 goto_tabpage_tp(tp);
3675 #ifdef FEAT_GUI_TABLINE
3676 if (gui_use_tabline())
3677 gui_mch_set_curtab(tabpage_index(curtab));
3678 #endif
3682 * Go to tabpage "tp".
3683 * Note: doesn't update the GUI tab.
3685 void
3686 goto_tabpage_tp(tp)
3687 tabpage_T *tp;
3689 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3691 if (valid_tabpage(tp))
3692 enter_tabpage(tp, curbuf);
3693 else
3694 enter_tabpage(curtab, curbuf);
3699 * Enter window "wp" in tab page "tp".
3700 * Also updates the GUI tab.
3702 void
3703 goto_tabpage_win(tp, wp)
3704 tabpage_T *tp;
3705 win_T *wp;
3707 goto_tabpage_tp(tp);
3708 if (curtab == tp && win_valid(wp))
3710 win_enter(wp, TRUE);
3711 # ifdef FEAT_GUI_TABLINE
3712 if (gui_use_tabline())
3713 gui_mch_set_curtab(tabpage_index(curtab));
3714 # endif
3719 * Move the current tab page to before tab page "nr".
3721 void
3722 tabpage_move(nr)
3723 int nr;
3725 int n = nr;
3726 tabpage_T *tp;
3728 if (first_tabpage->tp_next == NULL)
3729 return;
3731 /* Remove the current tab page from the list of tab pages. */
3732 if (curtab == first_tabpage)
3733 first_tabpage = curtab->tp_next;
3734 else
3736 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3737 if (tp->tp_next == curtab)
3738 break;
3739 if (tp == NULL) /* "cannot happen" */
3740 return;
3741 tp->tp_next = curtab->tp_next;
3744 /* Re-insert it at the specified position. */
3745 if (n == 0)
3747 curtab->tp_next = first_tabpage;
3748 first_tabpage = curtab;
3750 else
3752 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3753 --n;
3754 curtab->tp_next = tp->tp_next;
3755 tp->tp_next = curtab;
3758 /* Need to redraw the tabline. Tab page contents doesn't change. */
3759 redraw_tabline = TRUE;
3764 * Go to another window.
3765 * When jumping to another buffer, stop Visual mode. Do this before
3766 * changing windows so we can yank the selection into the '*' register.
3767 * When jumping to another window on the same buffer, adjust its cursor
3768 * position to keep the same Visual area.
3770 void
3771 win_goto(wp)
3772 win_T *wp;
3774 if (text_locked())
3776 beep_flush();
3777 text_locked_msg();
3778 return;
3780 #ifdef FEAT_AUTOCMD
3781 if (curbuf_locked())
3782 return;
3783 #endif
3785 #ifdef FEAT_VISUAL
3786 if (wp->w_buffer != curbuf)
3787 reset_VIsual_and_resel();
3788 else if (VIsual_active)
3789 wp->w_cursor = curwin->w_cursor;
3790 #endif
3792 #ifdef FEAT_GUI
3793 need_mouse_correct = TRUE;
3794 #endif
3795 win_enter(wp, TRUE);
3798 #if defined(FEAT_PERL) || defined(PROTO)
3800 * Find window number "winnr" (counting top to bottom).
3802 win_T *
3803 win_find_nr(winnr)
3804 int winnr;
3806 win_T *wp;
3808 # ifdef FEAT_WINDOWS
3809 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3810 if (--winnr == 0)
3811 break;
3812 return wp;
3813 # else
3814 return curwin;
3815 # endif
3817 #endif
3819 #ifdef FEAT_VERTSPLIT
3821 * Move to window above or below "count" times.
3823 static void
3824 win_goto_ver(up, count)
3825 int up; /* TRUE to go to win above */
3826 long count;
3828 frame_T *fr;
3829 frame_T *nfr;
3830 frame_T *foundfr;
3832 foundfr = curwin->w_frame;
3833 while (count--)
3836 * First go upwards in the tree of frames until we find a upwards or
3837 * downwards neighbor.
3839 fr = foundfr;
3840 for (;;)
3842 if (fr == topframe)
3843 goto end;
3844 if (up)
3845 nfr = fr->fr_prev;
3846 else
3847 nfr = fr->fr_next;
3848 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3849 break;
3850 fr = fr->fr_parent;
3854 * Now go downwards to find the bottom or top frame in it.
3856 for (;;)
3858 if (nfr->fr_layout == FR_LEAF)
3860 foundfr = nfr;
3861 break;
3863 fr = nfr->fr_child;
3864 if (nfr->fr_layout == FR_ROW)
3866 /* Find the frame at the cursor row. */
3867 while (fr->fr_next != NULL
3868 && frame2win(fr)->w_wincol + fr->fr_width
3869 <= curwin->w_wincol + curwin->w_wcol)
3870 fr = fr->fr_next;
3872 if (nfr->fr_layout == FR_COL && up)
3873 while (fr->fr_next != NULL)
3874 fr = fr->fr_next;
3875 nfr = fr;
3878 end:
3879 if (foundfr != NULL)
3880 win_goto(foundfr->fr_win);
3884 * Move to left or right window.
3886 static void
3887 win_goto_hor(left, count)
3888 int left; /* TRUE to go to left win */
3889 long count;
3891 frame_T *fr;
3892 frame_T *nfr;
3893 frame_T *foundfr;
3895 foundfr = curwin->w_frame;
3896 while (count--)
3899 * First go upwards in the tree of frames until we find a left or
3900 * right neighbor.
3902 fr = foundfr;
3903 for (;;)
3905 if (fr == topframe)
3906 goto end;
3907 if (left)
3908 nfr = fr->fr_prev;
3909 else
3910 nfr = fr->fr_next;
3911 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3912 break;
3913 fr = fr->fr_parent;
3917 * Now go downwards to find the leftmost or rightmost frame in it.
3919 for (;;)
3921 if (nfr->fr_layout == FR_LEAF)
3923 foundfr = nfr;
3924 break;
3926 fr = nfr->fr_child;
3927 if (nfr->fr_layout == FR_COL)
3929 /* Find the frame at the cursor row. */
3930 while (fr->fr_next != NULL
3931 && frame2win(fr)->w_winrow + fr->fr_height
3932 <= curwin->w_winrow + curwin->w_wrow)
3933 fr = fr->fr_next;
3935 if (nfr->fr_layout == FR_ROW && left)
3936 while (fr->fr_next != NULL)
3937 fr = fr->fr_next;
3938 nfr = fr;
3941 end:
3942 if (foundfr != NULL)
3943 win_goto(foundfr->fr_win);
3945 #endif
3948 * Make window "wp" the current window.
3950 void
3951 win_enter(wp, undo_sync)
3952 win_T *wp;
3953 int undo_sync;
3955 win_enter_ext(wp, undo_sync, FALSE);
3959 * Make window wp the current window.
3960 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3961 * been closed and isn't valid.
3963 static void
3964 win_enter_ext(wp, undo_sync, curwin_invalid)
3965 win_T *wp;
3966 int undo_sync;
3967 int curwin_invalid;
3969 #ifdef FEAT_AUTOCMD
3970 int other_buffer = FALSE;
3971 #endif
3973 if (wp == curwin && !curwin_invalid) /* nothing to do */
3974 return;
3976 #ifdef FEAT_AUTOCMD
3977 if (!curwin_invalid)
3980 * Be careful: If autocommands delete the window, return now.
3982 if (wp->w_buffer != curbuf)
3984 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3985 other_buffer = TRUE;
3986 if (!win_valid(wp))
3987 return;
3989 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3990 if (!win_valid(wp))
3991 return;
3992 # ifdef FEAT_EVAL
3993 /* autocmds may abort script processing */
3994 if (aborting())
3995 return;
3996 # endif
3998 #endif
4000 /* sync undo before leaving the current buffer */
4001 if (undo_sync && curbuf != wp->w_buffer)
4002 u_sync(FALSE);
4003 /* may have to copy the buffer options when 'cpo' contains 'S' */
4004 if (wp->w_buffer != curbuf)
4005 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4006 if (!curwin_invalid)
4008 prevwin = curwin; /* remember for CTRL-W p */
4009 curwin->w_redr_status = TRUE;
4011 curwin = wp;
4012 curbuf = wp->w_buffer;
4013 check_cursor();
4014 #ifdef FEAT_VIRTUALEDIT
4015 if (!virtual_active())
4016 curwin->w_cursor.coladd = 0;
4017 #endif
4018 changed_line_abv_curs(); /* assume cursor position needs updating */
4020 if (curwin->w_localdir != NULL)
4022 /* Window has a local directory: Save current directory as global
4023 * directory (unless that was done already) and change to the local
4024 * directory. */
4025 if (globaldir == NULL)
4027 char_u cwd[MAXPATHL];
4029 if (mch_dirname(cwd, MAXPATHL) == OK)
4030 globaldir = vim_strsave(cwd);
4032 mch_chdir((char *)curwin->w_localdir);
4033 shorten_fnames(TRUE);
4035 else if (globaldir != NULL)
4037 /* Window doesn't have a local directory and we are not in the global
4038 * directory: Change to the global directory. */
4039 mch_chdir((char *)globaldir);
4040 vim_free(globaldir);
4041 globaldir = NULL;
4042 shorten_fnames(TRUE);
4045 #ifdef FEAT_AUTOCMD
4046 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4047 if (other_buffer)
4048 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4049 #endif
4051 #ifdef FEAT_TITLE
4052 maketitle();
4053 #endif
4054 curwin->w_redr_status = TRUE;
4055 redraw_tabline = TRUE;
4056 if (restart_edit)
4057 redraw_later(VALID); /* causes status line redraw */
4059 /* set window height to desired minimal value */
4060 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4061 win_setheight((int)p_wh);
4062 else if (curwin->w_height == 0)
4063 win_setheight(1);
4065 #ifdef FEAT_VERTSPLIT
4066 /* set window width to desired minimal value */
4067 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4068 win_setwidth((int)p_wiw);
4069 #endif
4071 #ifdef FEAT_MOUSE
4072 setmouse(); /* in case jumped to/from help buffer */
4073 #endif
4075 /* Change directories when the 'acd' option is set. */
4076 DO_AUTOCHDIR
4079 #endif /* FEAT_WINDOWS */
4081 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4083 * Jump to the first open window that contains buffer "buf", if one exists.
4084 * Returns a pointer to the window found, otherwise NULL.
4086 win_T *
4087 buf_jump_open_win(buf)
4088 buf_T *buf;
4090 # ifdef FEAT_WINDOWS
4091 win_T *wp;
4093 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4094 if (wp->w_buffer == buf)
4095 break;
4096 if (wp != NULL)
4097 win_enter(wp, FALSE);
4098 return wp;
4099 # else
4100 if (curwin->w_buffer == buf)
4101 return curwin;
4102 return NULL;
4103 # endif
4107 * Jump to the first open window in any tab page that contains buffer "buf",
4108 * if one exists.
4109 * Returns a pointer to the window found, otherwise NULL.
4111 win_T *
4112 buf_jump_open_tab(buf)
4113 buf_T *buf;
4115 # ifdef FEAT_WINDOWS
4116 win_T *wp;
4117 tabpage_T *tp;
4119 /* First try the current tab page. */
4120 wp = buf_jump_open_win(buf);
4121 if (wp != NULL)
4122 return wp;
4124 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4125 if (tp != curtab)
4127 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4128 if (wp->w_buffer == buf)
4129 break;
4130 if (wp != NULL)
4132 goto_tabpage_win(tp, wp);
4133 if (curwin != wp)
4134 wp = NULL; /* something went wrong */
4135 break;
4139 return wp;
4140 # else
4141 if (curwin->w_buffer == buf)
4142 return curwin;
4143 return NULL;
4144 # endif
4146 #endif
4149 * allocate a window structure and link it in the window list
4151 /*ARGSUSED*/
4152 static win_T *
4153 win_alloc(after)
4154 win_T *after;
4156 win_T *newwin;
4159 * allocate window structure and linesizes arrays
4161 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4162 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4164 vim_free(newwin);
4165 newwin = NULL;
4168 if (newwin != NULL)
4170 #ifdef FEAT_AUTOCMD
4171 /* Don't execute autocommands while the window is not properly
4172 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4173 * event. */
4174 block_autocmds();
4175 #endif
4177 * link the window in the window list
4179 #ifdef FEAT_WINDOWS
4180 win_append(after, newwin);
4181 #endif
4182 #ifdef FEAT_VERTSPLIT
4183 newwin->w_wincol = 0;
4184 newwin->w_width = Columns;
4185 #endif
4187 /* position the display and the cursor at the top of the file. */
4188 newwin->w_topline = 1;
4189 #ifdef FEAT_DIFF
4190 newwin->w_topfill = 0;
4191 #endif
4192 newwin->w_botline = 2;
4193 newwin->w_cursor.lnum = 1;
4194 #ifdef FEAT_SCROLLBIND
4195 newwin->w_scbind_pos = 1;
4196 #endif
4198 /* We won't calculate w_fraction until resizing the window */
4199 newwin->w_fraction = 0;
4200 newwin->w_prev_fraction_row = -1;
4202 #ifdef FEAT_GUI
4203 if (gui.in_use)
4205 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4206 SBAR_LEFT, newwin);
4207 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4208 SBAR_RIGHT, newwin);
4210 #endif
4211 #ifdef FEAT_EVAL
4212 /* init w: variables */
4213 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4214 #endif
4215 #ifdef FEAT_FOLDING
4216 foldInitWin(newwin);
4217 #endif
4218 #ifdef FEAT_AUTOCMD
4219 unblock_autocmds();
4220 #endif
4221 #ifdef FEAT_SEARCH_EXTRA
4222 newwin->w_match_head = NULL;
4223 newwin->w_next_match_id = 4;
4224 #endif
4226 return newwin;
4229 #if defined(FEAT_WINDOWS) || defined(PROTO)
4232 * remove window 'wp' from the window list and free the structure
4234 static void
4235 win_free(wp, tp)
4236 win_T *wp;
4237 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4239 int i;
4241 #ifdef FEAT_AUTOCMD
4242 /* Don't execute autocommands while the window is halfway being deleted.
4243 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4244 block_autocmds();
4245 #endif
4247 #ifdef FEAT_MZSCHEME
4248 mzscheme_window_free(wp);
4249 #endif
4251 #ifdef FEAT_PERL
4252 perl_win_free(wp);
4253 #endif
4255 #ifdef FEAT_PYTHON
4256 python_window_free(wp);
4257 #endif
4259 #ifdef FEAT_TCL
4260 tcl_window_free(wp);
4261 #endif
4263 #ifdef FEAT_RUBY
4264 ruby_window_free(wp);
4265 #endif
4267 clear_winopt(&wp->w_onebuf_opt);
4268 clear_winopt(&wp->w_allbuf_opt);
4270 #ifdef FEAT_EVAL
4271 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4272 #endif
4274 if (prevwin == wp)
4275 prevwin = NULL;
4276 win_free_lsize(wp);
4278 for (i = 0; i < wp->w_tagstacklen; ++i)
4279 vim_free(wp->w_tagstack[i].tagname);
4281 vim_free(wp->w_localdir);
4283 #ifdef FEAT_SEARCH_EXTRA
4284 clear_matches(wp);
4285 #endif
4287 #ifdef FEAT_JUMPLIST
4288 free_jumplist(wp);
4289 #endif
4291 #ifdef FEAT_QUICKFIX
4292 qf_free_all(wp);
4293 #endif
4295 #ifdef FEAT_GUI
4296 if (gui.in_use)
4298 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4299 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4301 #endif /* FEAT_GUI */
4303 win_remove(wp, tp);
4304 vim_free(wp);
4306 #ifdef FEAT_AUTOCMD
4307 unblock_autocmds();
4308 #endif
4312 * Append window "wp" in the window list after window "after".
4314 static void
4315 win_append(after, wp)
4316 win_T *after, *wp;
4318 win_T *before;
4320 if (after == NULL) /* after NULL is in front of the first */
4321 before = firstwin;
4322 else
4323 before = after->w_next;
4325 wp->w_next = before;
4326 wp->w_prev = after;
4327 if (after == NULL)
4328 firstwin = wp;
4329 else
4330 after->w_next = wp;
4331 if (before == NULL)
4332 lastwin = wp;
4333 else
4334 before->w_prev = wp;
4338 * Remove a window from the window list.
4340 static void
4341 win_remove(wp, tp)
4342 win_T *wp;
4343 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4345 if (wp->w_prev != NULL)
4346 wp->w_prev->w_next = wp->w_next;
4347 else if (tp == NULL)
4348 firstwin = wp->w_next;
4349 else
4350 tp->tp_firstwin = wp->w_next;
4351 if (wp->w_next != NULL)
4352 wp->w_next->w_prev = wp->w_prev;
4353 else if (tp == NULL)
4354 lastwin = wp->w_prev;
4355 else
4356 tp->tp_lastwin = wp->w_prev;
4360 * Append frame "frp" in a frame list after frame "after".
4362 static void
4363 frame_append(after, frp)
4364 frame_T *after, *frp;
4366 frp->fr_next = after->fr_next;
4367 after->fr_next = frp;
4368 if (frp->fr_next != NULL)
4369 frp->fr_next->fr_prev = frp;
4370 frp->fr_prev = after;
4374 * Insert frame "frp" in a frame list before frame "before".
4376 static void
4377 frame_insert(before, frp)
4378 frame_T *before, *frp;
4380 frp->fr_next = before;
4381 frp->fr_prev = before->fr_prev;
4382 before->fr_prev = frp;
4383 if (frp->fr_prev != NULL)
4384 frp->fr_prev->fr_next = frp;
4385 else
4386 frp->fr_parent->fr_child = frp;
4390 * Remove a frame from a frame list.
4392 static void
4393 frame_remove(frp)
4394 frame_T *frp;
4396 if (frp->fr_prev != NULL)
4397 frp->fr_prev->fr_next = frp->fr_next;
4398 else
4399 frp->fr_parent->fr_child = frp->fr_next;
4400 if (frp->fr_next != NULL)
4401 frp->fr_next->fr_prev = frp->fr_prev;
4404 #endif /* FEAT_WINDOWS */
4407 * Allocate w_lines[] for window "wp".
4408 * Return FAIL for failure, OK for success.
4411 win_alloc_lines(wp)
4412 win_T *wp;
4414 wp->w_lines_valid = 0;
4415 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4416 if (wp->w_lines == NULL)
4417 return FAIL;
4418 return OK;
4422 * free lsize arrays for a window
4424 void
4425 win_free_lsize(wp)
4426 win_T *wp;
4428 vim_free(wp->w_lines);
4429 wp->w_lines = NULL;
4433 * Called from win_new_shellsize() after Rows changed.
4434 * This only does the current tab page, others must be done when made active.
4436 void
4437 shell_new_rows()
4439 int h = (int)ROWS_AVAIL;
4441 if (firstwin == NULL) /* not initialized yet */
4442 return;
4443 #ifdef FEAT_WINDOWS
4444 if (h < frame_minheight(topframe, NULL))
4445 h = frame_minheight(topframe, NULL);
4447 /* First try setting the heights of windows with 'winfixheight'. If
4448 * that doesn't result in the right height, forget about that option. */
4449 frame_new_height(topframe, h, FALSE, TRUE);
4450 if (topframe->fr_height != h)
4451 frame_new_height(topframe, h, FALSE, FALSE);
4453 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4454 #else
4455 if (h < 1)
4456 h = 1;
4457 win_new_height(firstwin, h);
4458 #endif
4459 compute_cmdrow();
4460 #ifdef FEAT_WINDOWS
4461 curtab->tp_ch_used = p_ch;
4462 #endif
4464 #if 0
4465 /* Disabled: don't want making the screen smaller make a window larger. */
4466 if (p_ea)
4467 win_equal(curwin, FALSE, 'v');
4468 #endif
4471 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4473 * Called from win_new_shellsize() after Columns changed.
4475 void
4476 shell_new_columns()
4478 if (firstwin == NULL) /* not initialized yet */
4479 return;
4481 /* First try setting the widths of windows with 'winfixwidth'. If that
4482 * doesn't result in the right width, forget about that option. */
4483 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4484 if (topframe->fr_width != Columns)
4485 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4487 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4488 #if 0
4489 /* Disabled: don't want making the screen smaller make a window larger. */
4490 if (p_ea)
4491 win_equal(curwin, FALSE, 'h');
4492 #endif
4494 #endif
4496 #if defined(FEAT_CMDWIN) || defined(PROTO)
4498 * Save the size of all windows in "gap".
4500 void
4501 win_size_save(gap)
4502 garray_T *gap;
4505 win_T *wp;
4507 ga_init2(gap, (int)sizeof(int), 1);
4508 if (ga_grow(gap, win_count() * 2) == OK)
4509 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4511 ((int *)gap->ga_data)[gap->ga_len++] =
4512 wp->w_width + wp->w_vsep_width;
4513 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4518 * Restore window sizes, but only if the number of windows is still the same.
4519 * Does not free the growarray.
4521 void
4522 win_size_restore(gap)
4523 garray_T *gap;
4525 win_T *wp;
4526 int i;
4528 if (win_count() * 2 == gap->ga_len)
4530 i = 0;
4531 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4533 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4534 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4536 /* recompute the window positions */
4537 (void)win_comp_pos();
4540 #endif /* FEAT_CMDWIN */
4542 #if defined(FEAT_WINDOWS) || defined(PROTO)
4544 * Update the position for all windows, using the width and height of the
4545 * frames.
4546 * Returns the row just after the last window.
4549 win_comp_pos()
4551 int row = tabline_height();
4552 int col = 0;
4554 frame_comp_pos(topframe, &row, &col);
4555 return row;
4559 * Update the position of the windows in frame "topfrp", using the width and
4560 * height of the frames.
4561 * "*row" and "*col" are the top-left position of the frame. They are updated
4562 * to the bottom-right position plus one.
4564 static void
4565 frame_comp_pos(topfrp, row, col)
4566 frame_T *topfrp;
4567 int *row;
4568 int *col;
4570 win_T *wp;
4571 frame_T *frp;
4572 #ifdef FEAT_VERTSPLIT
4573 int startcol;
4574 int startrow;
4575 #endif
4577 wp = topfrp->fr_win;
4578 if (wp != NULL)
4580 if (wp->w_winrow != *row
4581 #ifdef FEAT_VERTSPLIT
4582 || wp->w_wincol != *col
4583 #endif
4586 /* position changed, redraw */
4587 wp->w_winrow = *row;
4588 #ifdef FEAT_VERTSPLIT
4589 wp->w_wincol = *col;
4590 #endif
4591 redraw_win_later(wp, NOT_VALID);
4592 wp->w_redr_status = TRUE;
4594 *row += wp->w_height + wp->w_status_height;
4595 #ifdef FEAT_VERTSPLIT
4596 *col += wp->w_width + wp->w_vsep_width;
4597 #endif
4599 else
4601 #ifdef FEAT_VERTSPLIT
4602 startrow = *row;
4603 startcol = *col;
4604 #endif
4605 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4607 #ifdef FEAT_VERTSPLIT
4608 if (topfrp->fr_layout == FR_ROW)
4609 *row = startrow; /* all frames are at the same row */
4610 else
4611 *col = startcol; /* all frames are at the same col */
4612 #endif
4613 frame_comp_pos(frp, row, col);
4618 #endif /* FEAT_WINDOWS */
4621 * Set current window height and take care of repositioning other windows to
4622 * fit around it.
4624 void
4625 win_setheight(height)
4626 int height;
4628 win_setheight_win(height, curwin);
4632 * Set the window height of window "win" and take care of repositioning other
4633 * windows to fit around it.
4635 void
4636 win_setheight_win(height, win)
4637 int height;
4638 win_T *win;
4640 int row;
4642 if (win == curwin)
4644 /* Always keep current window at least one line high, even when
4645 * 'winminheight' is zero. */
4646 #ifdef FEAT_WINDOWS
4647 if (height < p_wmh)
4648 height = p_wmh;
4649 #endif
4650 if (height == 0)
4651 height = 1;
4654 #ifdef FEAT_WINDOWS
4655 frame_setheight(win->w_frame, height + win->w_status_height);
4657 /* recompute the window positions */
4658 row = win_comp_pos();
4659 #else
4660 if (height > topframe->fr_height)
4661 height = topframe->fr_height;
4662 win->w_height = height;
4663 row = height;
4664 #endif
4667 * If there is extra space created between the last window and the command
4668 * line, clear it.
4670 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4671 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4672 cmdline_row = row;
4673 msg_row = row;
4674 msg_col = 0;
4676 redraw_all_later(NOT_VALID);
4679 #if defined(FEAT_WINDOWS) || defined(PROTO)
4682 * Set the height of a frame to "height" and take care that all frames and
4683 * windows inside it are resized. Also resize frames on the left and right if
4684 * the are in the same FR_ROW frame.
4686 * Strategy:
4687 * If the frame is part of a FR_COL frame, try fitting the frame in that
4688 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4689 * go to containing frames to resize them and make room.
4690 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4691 * Check for the minimal height of the FR_ROW frame.
4692 * At the top level we can also use change the command line height.
4694 static void
4695 frame_setheight(curfrp, height)
4696 frame_T *curfrp;
4697 int height;
4699 int room; /* total number of lines available */
4700 int take; /* number of lines taken from other windows */
4701 int room_cmdline; /* lines available from cmdline */
4702 int run;
4703 frame_T *frp;
4704 int h;
4705 int room_reserved;
4707 /* If the height already is the desired value, nothing to do. */
4708 if (curfrp->fr_height == height)
4709 return;
4711 if (curfrp->fr_parent == NULL)
4713 /* topframe: can only change the command line */
4714 if (height > ROWS_AVAIL)
4715 height = ROWS_AVAIL;
4716 if (height > 0)
4717 frame_new_height(curfrp, height, FALSE, FALSE);
4719 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4721 /* Row of frames: Also need to resize frames left and right of this
4722 * one. First check for the minimal height of these. */
4723 h = frame_minheight(curfrp->fr_parent, NULL);
4724 if (height < h)
4725 height = h;
4726 frame_setheight(curfrp->fr_parent, height);
4728 else
4731 * Column of frames: try to change only frames in this column.
4733 #ifdef FEAT_VERTSPLIT
4735 * Do this twice:
4736 * 1: compute room available, if it's not enough try resizing the
4737 * containing frame.
4738 * 2: compute the room available and adjust the height to it.
4739 * Try not to reduce the height of a window with 'winfixheight' set.
4741 for (run = 1; run <= 2; ++run)
4742 #else
4743 for (;;)
4744 #endif
4746 room = 0;
4747 room_reserved = 0;
4748 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4749 frp = frp->fr_next)
4751 if (frp != curfrp
4752 && frp->fr_win != NULL
4753 && frp->fr_win->w_p_wfh)
4754 room_reserved += frp->fr_height;
4755 room += frp->fr_height;
4756 if (frp != curfrp)
4757 room -= frame_minheight(frp, NULL);
4759 #ifdef FEAT_VERTSPLIT
4760 if (curfrp->fr_width != Columns)
4761 room_cmdline = 0;
4762 else
4763 #endif
4765 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4766 + lastwin->w_height + lastwin->w_status_height);
4767 if (room_cmdline < 0)
4768 room_cmdline = 0;
4771 if (height <= room + room_cmdline)
4772 break;
4773 #ifdef FEAT_VERTSPLIT
4774 if (run == 2 || curfrp->fr_width == Columns)
4775 #endif
4777 if (height > room + room_cmdline)
4778 height = room + room_cmdline;
4779 break;
4781 #ifdef FEAT_VERTSPLIT
4782 frame_setheight(curfrp->fr_parent, height
4783 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4784 #endif
4785 /*NOTREACHED*/
4789 * Compute the number of lines we will take from others frames (can be
4790 * negative!).
4792 take = height - curfrp->fr_height;
4794 /* If there is not enough room, also reduce the height of a window
4795 * with 'winfixheight' set. */
4796 if (height > room + room_cmdline - room_reserved)
4797 room_reserved = room + room_cmdline - height;
4798 /* If there is only a 'winfixheight' window and making the
4799 * window smaller, need to make the other window taller. */
4800 if (take < 0 && room - curfrp->fr_height < room_reserved)
4801 room_reserved = 0;
4803 if (take > 0 && room_cmdline > 0)
4805 /* use lines from cmdline first */
4806 if (take < room_cmdline)
4807 room_cmdline = take;
4808 take -= room_cmdline;
4809 topframe->fr_height += room_cmdline;
4813 * set the current frame to the new height
4815 frame_new_height(curfrp, height, FALSE, FALSE);
4818 * First take lines from the frames after the current frame. If
4819 * that is not enough, takes lines from frames above the current
4820 * frame.
4822 for (run = 0; run < 2; ++run)
4824 if (run == 0)
4825 frp = curfrp->fr_next; /* 1st run: start with next window */
4826 else
4827 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4828 while (frp != NULL && take != 0)
4830 h = frame_minheight(frp, NULL);
4831 if (room_reserved > 0
4832 && frp->fr_win != NULL
4833 && frp->fr_win->w_p_wfh)
4835 if (room_reserved >= frp->fr_height)
4836 room_reserved -= frp->fr_height;
4837 else
4839 if (frp->fr_height - room_reserved > take)
4840 room_reserved = frp->fr_height - take;
4841 take -= frp->fr_height - room_reserved;
4842 frame_new_height(frp, room_reserved, FALSE, FALSE);
4843 room_reserved = 0;
4846 else
4848 if (frp->fr_height - take < h)
4850 take -= frp->fr_height - h;
4851 frame_new_height(frp, h, FALSE, FALSE);
4853 else
4855 frame_new_height(frp, frp->fr_height - take,
4856 FALSE, FALSE);
4857 take = 0;
4860 if (run == 0)
4861 frp = frp->fr_next;
4862 else
4863 frp = frp->fr_prev;
4869 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4871 * Set current window width and take care of repositioning other windows to
4872 * fit around it.
4874 void
4875 win_setwidth(width)
4876 int width;
4878 win_setwidth_win(width, curwin);
4881 void
4882 win_setwidth_win(width, wp)
4883 int width;
4884 win_T *wp;
4886 /* Always keep current window at least one column wide, even when
4887 * 'winminwidth' is zero. */
4888 if (wp == curwin)
4890 if (width < p_wmw)
4891 width = p_wmw;
4892 if (width == 0)
4893 width = 1;
4896 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4898 /* recompute the window positions */
4899 (void)win_comp_pos();
4901 redraw_all_later(NOT_VALID);
4905 * Set the width of a frame to "width" and take care that all frames and
4906 * windows inside it are resized. Also resize frames above and below if the
4907 * are in the same FR_ROW frame.
4909 * Strategy is similar to frame_setheight().
4911 static void
4912 frame_setwidth(curfrp, width)
4913 frame_T *curfrp;
4914 int width;
4916 int room; /* total number of lines available */
4917 int take; /* number of lines taken from other windows */
4918 int run;
4919 frame_T *frp;
4920 int w;
4921 int room_reserved;
4923 /* If the width already is the desired value, nothing to do. */
4924 if (curfrp->fr_width == width)
4925 return;
4927 if (curfrp->fr_parent == NULL)
4928 /* topframe: can't change width */
4929 return;
4931 if (curfrp->fr_parent->fr_layout == FR_COL)
4933 /* Column of frames: Also need to resize frames above and below of
4934 * this one. First check for the minimal width of these. */
4935 w = frame_minwidth(curfrp->fr_parent, NULL);
4936 if (width < w)
4937 width = w;
4938 frame_setwidth(curfrp->fr_parent, width);
4940 else
4943 * Row of frames: try to change only frames in this row.
4945 * Do this twice:
4946 * 1: compute room available, if it's not enough try resizing the
4947 * containing frame.
4948 * 2: compute the room available and adjust the width to it.
4950 for (run = 1; run <= 2; ++run)
4952 room = 0;
4953 room_reserved = 0;
4954 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4955 frp = frp->fr_next)
4957 if (frp != curfrp
4958 && frp->fr_win != NULL
4959 && frp->fr_win->w_p_wfw)
4960 room_reserved += frp->fr_width;
4961 room += frp->fr_width;
4962 if (frp != curfrp)
4963 room -= frame_minwidth(frp, NULL);
4966 if (width <= room)
4967 break;
4968 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4970 if (width > room)
4971 width = room;
4972 break;
4974 frame_setwidth(curfrp->fr_parent, width
4975 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4979 * Compute the number of lines we will take from others frames (can be
4980 * negative!).
4982 take = width - curfrp->fr_width;
4984 /* If there is not enough room, also reduce the width of a window
4985 * with 'winfixwidth' set. */
4986 if (width > room - room_reserved)
4987 room_reserved = room - width;
4988 /* If there is only a 'winfixwidth' window and making the
4989 * window smaller, need to make the other window narrower. */
4990 if (take < 0 && room - curfrp->fr_width < room_reserved)
4991 room_reserved = 0;
4994 * set the current frame to the new width
4996 frame_new_width(curfrp, width, FALSE, FALSE);
4999 * First take lines from the frames right of the current frame. If
5000 * that is not enough, takes lines from frames left of the current
5001 * frame.
5003 for (run = 0; run < 2; ++run)
5005 if (run == 0)
5006 frp = curfrp->fr_next; /* 1st run: start with next window */
5007 else
5008 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5009 while (frp != NULL && take != 0)
5011 w = frame_minwidth(frp, NULL);
5012 if (room_reserved > 0
5013 && frp->fr_win != NULL
5014 && frp->fr_win->w_p_wfw)
5016 if (room_reserved >= frp->fr_width)
5017 room_reserved -= frp->fr_width;
5018 else
5020 if (frp->fr_width - room_reserved > take)
5021 room_reserved = frp->fr_width - take;
5022 take -= frp->fr_width - room_reserved;
5023 frame_new_width(frp, room_reserved, FALSE, FALSE);
5024 room_reserved = 0;
5027 else
5029 if (frp->fr_width - take < w)
5031 take -= frp->fr_width - w;
5032 frame_new_width(frp, w, FALSE, FALSE);
5034 else
5036 frame_new_width(frp, frp->fr_width - take,
5037 FALSE, FALSE);
5038 take = 0;
5041 if (run == 0)
5042 frp = frp->fr_next;
5043 else
5044 frp = frp->fr_prev;
5049 #endif /* FEAT_VERTSPLIT */
5052 * Check 'winminheight' for a valid value.
5054 void
5055 win_setminheight()
5057 int room;
5058 int first = TRUE;
5059 win_T *wp;
5061 /* loop until there is a 'winminheight' that is possible */
5062 while (p_wmh > 0)
5064 /* TODO: handle vertical splits */
5065 room = -p_wh;
5066 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5067 room += wp->w_height - p_wmh;
5068 if (room >= 0)
5069 break;
5070 --p_wmh;
5071 if (first)
5073 EMSG(_(e_noroom));
5074 first = FALSE;
5079 #ifdef FEAT_MOUSE
5082 * Status line of dragwin is dragged "offset" lines down (negative is up).
5084 void
5085 win_drag_status_line(dragwin, offset)
5086 win_T *dragwin;
5087 int offset;
5089 frame_T *curfr;
5090 frame_T *fr;
5091 int room;
5092 int row;
5093 int up; /* if TRUE, drag status line up, otherwise down */
5094 int n;
5096 fr = dragwin->w_frame;
5097 curfr = fr;
5098 if (fr != topframe) /* more than one window */
5100 fr = fr->fr_parent;
5101 /* When the parent frame is not a column of frames, its parent should
5102 * be. */
5103 if (fr->fr_layout != FR_COL)
5105 curfr = fr;
5106 if (fr != topframe) /* only a row of windows, may drag statusline */
5107 fr = fr->fr_parent;
5111 /* If this is the last frame in a column, may want to resize the parent
5112 * frame instead (go two up to skip a row of frames). */
5113 while (curfr != topframe && curfr->fr_next == NULL)
5115 if (fr != topframe)
5116 fr = fr->fr_parent;
5117 curfr = fr;
5118 if (fr != topframe)
5119 fr = fr->fr_parent;
5122 if (offset < 0) /* drag up */
5124 up = TRUE;
5125 offset = -offset;
5126 /* sum up the room of the current frame and above it */
5127 if (fr == curfr)
5129 /* only one window */
5130 room = fr->fr_height - frame_minheight(fr, NULL);
5132 else
5134 room = 0;
5135 for (fr = fr->fr_child; ; fr = fr->fr_next)
5137 room += fr->fr_height - frame_minheight(fr, NULL);
5138 if (fr == curfr)
5139 break;
5142 fr = curfr->fr_next; /* put fr at frame that grows */
5144 else /* drag down */
5146 up = FALSE;
5148 * Only dragging the last status line can reduce p_ch.
5150 room = Rows - cmdline_row;
5151 if (curfr->fr_next == NULL)
5152 room -= 1;
5153 else
5154 room -= p_ch;
5155 if (room < 0)
5156 room = 0;
5157 /* sum up the room of frames below of the current one */
5158 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5159 room += fr->fr_height - frame_minheight(fr, NULL);
5160 fr = curfr; /* put fr at window that grows */
5163 if (room < offset) /* Not enough room */
5164 offset = room; /* Move as far as we can */
5165 if (offset <= 0)
5166 return;
5169 * Grow frame fr by "offset" lines.
5170 * Doesn't happen when dragging the last status line up.
5172 if (fr != NULL)
5173 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5175 if (up)
5176 fr = curfr; /* current frame gets smaller */
5177 else
5178 fr = curfr->fr_next; /* next frame gets smaller */
5181 * Now make the other frames smaller.
5183 while (fr != NULL && offset > 0)
5185 n = frame_minheight(fr, NULL);
5186 if (fr->fr_height - offset <= n)
5188 offset -= fr->fr_height - n;
5189 frame_new_height(fr, n, !up, FALSE);
5191 else
5193 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5194 break;
5196 if (up)
5197 fr = fr->fr_prev;
5198 else
5199 fr = fr->fr_next;
5201 row = win_comp_pos();
5202 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5203 cmdline_row = row;
5204 p_ch = Rows - cmdline_row;
5205 if (p_ch < 1)
5206 p_ch = 1;
5207 curtab->tp_ch_used = p_ch;
5208 redraw_all_later(SOME_VALID);
5209 showmode();
5212 #ifdef FEAT_VERTSPLIT
5214 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5216 void
5217 win_drag_vsep_line(dragwin, offset)
5218 win_T *dragwin;
5219 int offset;
5221 frame_T *curfr;
5222 frame_T *fr;
5223 int room;
5224 int left; /* if TRUE, drag separator line left, otherwise right */
5225 int n;
5227 fr = dragwin->w_frame;
5228 if (fr == topframe) /* only one window (cannot happen?) */
5229 return;
5230 curfr = fr;
5231 fr = fr->fr_parent;
5232 /* When the parent frame is not a row of frames, its parent should be. */
5233 if (fr->fr_layout != FR_ROW)
5235 if (fr == topframe) /* only a column of windows (cannot happen?) */
5236 return;
5237 curfr = fr;
5238 fr = fr->fr_parent;
5241 /* If this is the last frame in a row, may want to resize a parent
5242 * frame instead. */
5243 while (curfr->fr_next == NULL)
5245 if (fr == topframe)
5246 break;
5247 curfr = fr;
5248 fr = fr->fr_parent;
5249 if (fr != topframe)
5251 curfr = fr;
5252 fr = fr->fr_parent;
5256 if (offset < 0) /* drag left */
5258 left = TRUE;
5259 offset = -offset;
5260 /* sum up the room of the current frame and left of it */
5261 room = 0;
5262 for (fr = fr->fr_child; ; fr = fr->fr_next)
5264 room += fr->fr_width - frame_minwidth(fr, NULL);
5265 if (fr == curfr)
5266 break;
5268 fr = curfr->fr_next; /* put fr at frame that grows */
5270 else /* drag right */
5272 left = FALSE;
5273 /* sum up the room of frames right of the current one */
5274 room = 0;
5275 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5276 room += fr->fr_width - frame_minwidth(fr, NULL);
5277 fr = curfr; /* put fr at window that grows */
5280 if (room < offset) /* Not enough room */
5281 offset = room; /* Move as far as we can */
5282 if (offset <= 0) /* No room at all, quit. */
5283 return;
5285 /* grow frame fr by offset lines */
5286 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5288 /* shrink other frames: current and at the left or at the right */
5289 if (left)
5290 fr = curfr; /* current frame gets smaller */
5291 else
5292 fr = curfr->fr_next; /* next frame gets smaller */
5294 while (fr != NULL && offset > 0)
5296 n = frame_minwidth(fr, NULL);
5297 if (fr->fr_width - offset <= n)
5299 offset -= fr->fr_width - n;
5300 frame_new_width(fr, n, !left, FALSE);
5302 else
5304 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5305 break;
5307 if (left)
5308 fr = fr->fr_prev;
5309 else
5310 fr = fr->fr_next;
5312 (void)win_comp_pos();
5313 redraw_all_later(NOT_VALID);
5315 #endif /* FEAT_VERTSPLIT */
5316 #endif /* FEAT_MOUSE */
5318 #endif /* FEAT_WINDOWS */
5321 * Set the height of a window.
5322 * This takes care of the things inside the window, not what happens to the
5323 * window position, the frame or to other windows.
5325 static void
5326 win_new_height(wp, height)
5327 win_T *wp;
5328 int height;
5330 linenr_T lnum;
5331 int sline, line_size;
5332 #define FRACTION_MULT 16384L
5334 /* Don't want a negative height. Happens when splitting a tiny window.
5335 * Will equalize heights soon to fix it. */
5336 if (height < 0)
5337 height = 0;
5338 if (wp->w_height == height)
5339 return; /* nothing to do */
5341 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5342 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5343 + FRACTION_MULT / 2) / (long)wp->w_height;
5345 wp->w_height = height;
5346 wp->w_skipcol = 0;
5348 /* Don't change w_topline when height is zero. Don't set w_topline when
5349 * 'scrollbind' is set and this isn't the current window. */
5350 if (height > 0
5351 #ifdef FEAT_SCROLLBIND
5352 && (!wp->w_p_scb || wp == curwin)
5353 #endif
5357 * Find a value for w_topline that shows the cursor at the same
5358 * relative position in the window as before (more or less).
5360 lnum = wp->w_cursor.lnum;
5361 if (lnum < 1) /* can happen when starting up */
5362 lnum = 1;
5363 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5364 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5365 sline = wp->w_wrow - line_size;
5367 if (sline >= 0)
5369 /* Make sure the whole cursor line is visible, if possible. */
5370 int rows = plines_win(wp, lnum, FALSE);
5372 if (sline > wp->w_height - rows)
5374 sline = wp->w_height - rows;
5375 wp->w_wrow -= rows - line_size;
5379 if (sline < 0)
5382 * Cursor line would go off top of screen if w_wrow was this high.
5383 * Make cursor line the first line in the window. If not enough
5384 * room use w_skipcol;
5386 wp->w_wrow = line_size;
5387 if (wp->w_wrow >= wp->w_height
5388 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5390 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5391 --wp->w_wrow;
5392 while (wp->w_wrow >= wp->w_height)
5394 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5395 + win_col_off2(wp);
5396 --wp->w_wrow;
5400 else
5402 while (sline > 0 && lnum > 1)
5404 #ifdef FEAT_FOLDING
5405 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5406 if (lnum == 1)
5408 /* first line in buffer is folded */
5409 line_size = 1;
5410 --sline;
5411 break;
5413 #endif
5414 --lnum;
5415 #ifdef FEAT_DIFF
5416 if (lnum == wp->w_topline)
5417 line_size = plines_win_nofill(wp, lnum, TRUE)
5418 + wp->w_topfill;
5419 else
5420 #endif
5421 line_size = plines_win(wp, lnum, TRUE);
5422 sline -= line_size;
5425 if (sline < 0)
5428 * Line we want at top would go off top of screen. Use next
5429 * line instead.
5431 #ifdef FEAT_FOLDING
5432 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5433 #endif
5434 lnum++;
5435 wp->w_wrow -= line_size + sline;
5437 else if (sline > 0)
5439 /* First line of file reached, use that as topline. */
5440 lnum = 1;
5441 wp->w_wrow -= sline;
5444 set_topline(wp, lnum);
5447 if (wp == curwin)
5449 if (p_so)
5450 update_topline();
5451 curs_columns(FALSE); /* validate w_wrow */
5453 wp->w_prev_fraction_row = wp->w_wrow;
5455 win_comp_scroll(wp);
5456 redraw_win_later(wp, SOME_VALID);
5457 #ifdef FEAT_WINDOWS
5458 wp->w_redr_status = TRUE;
5459 #endif
5460 invalidate_botline_win(wp);
5463 #ifdef FEAT_VERTSPLIT
5465 * Set the width of a window.
5467 static void
5468 win_new_width(wp, width)
5469 win_T *wp;
5470 int width;
5472 wp->w_width = width;
5473 wp->w_lines_valid = 0;
5474 changed_line_abv_curs_win(wp);
5475 invalidate_botline_win(wp);
5476 if (wp == curwin)
5478 update_topline();
5479 curs_columns(TRUE); /* validate w_wrow */
5481 redraw_win_later(wp, NOT_VALID);
5482 wp->w_redr_status = TRUE;
5484 #endif
5486 void
5487 win_comp_scroll(wp)
5488 win_T *wp;
5490 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5491 if (wp->w_p_scr == 0)
5492 wp->w_p_scr = 1;
5496 * command_height: called whenever p_ch has been changed
5498 void
5499 command_height()
5501 #ifdef FEAT_WINDOWS
5502 int h;
5503 frame_T *frp;
5504 int old_p_ch = curtab->tp_ch_used;
5506 /* Use the value of p_ch that we remembered. This is needed for when the
5507 * GUI starts up, we can't be sure in what order things happen. And when
5508 * p_ch was changed in another tab page. */
5509 curtab->tp_ch_used = p_ch;
5511 /* Find bottom frame with width of screen. */
5512 frp = lastwin->w_frame;
5513 # ifdef FEAT_VERTSPLIT
5514 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5515 frp = frp->fr_parent;
5516 # endif
5518 /* Avoid changing the height of a window with 'winfixheight' set. */
5519 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5520 && frp->fr_win->w_p_wfh)
5521 frp = frp->fr_prev;
5523 if (starting != NO_SCREEN)
5525 cmdline_row = Rows - p_ch;
5527 if (p_ch > old_p_ch) /* p_ch got bigger */
5529 while (p_ch > old_p_ch)
5531 if (frp == NULL)
5533 EMSG(_(e_noroom));
5534 p_ch = old_p_ch;
5535 curtab->tp_ch_used = p_ch;
5536 cmdline_row = Rows - p_ch;
5537 break;
5539 h = frp->fr_height - frame_minheight(frp, NULL);
5540 if (h > p_ch - old_p_ch)
5541 h = p_ch - old_p_ch;
5542 old_p_ch += h;
5543 frame_add_height(frp, -h);
5544 frp = frp->fr_prev;
5547 /* Recompute window positions. */
5548 (void)win_comp_pos();
5550 /* clear the lines added to cmdline */
5551 if (full_screen)
5552 screen_fill((int)(cmdline_row), (int)Rows, 0,
5553 (int)Columns, ' ', ' ', 0);
5554 msg_row = cmdline_row;
5555 redraw_cmdline = TRUE;
5556 return;
5559 if (msg_row < cmdline_row)
5560 msg_row = cmdline_row;
5561 redraw_cmdline = TRUE;
5563 frame_add_height(frp, (int)(old_p_ch - p_ch));
5565 /* Recompute window positions. */
5566 if (frp != lastwin->w_frame)
5567 (void)win_comp_pos();
5568 #else
5569 cmdline_row = Rows - p_ch;
5570 win_setheight(cmdline_row);
5571 #endif
5574 #if defined(FEAT_WINDOWS) || defined(PROTO)
5576 * Resize frame "frp" to be "n" lines higher (negative for less high).
5577 * Also resize the frames it is contained in.
5579 static void
5580 frame_add_height(frp, n)
5581 frame_T *frp;
5582 int n;
5584 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5585 for (;;)
5587 frp = frp->fr_parent;
5588 if (frp == NULL)
5589 break;
5590 frp->fr_height += n;
5595 * Add or remove a status line for the bottom window(s), according to the
5596 * value of 'laststatus'.
5598 void
5599 last_status(morewin)
5600 int morewin; /* pretend there are two or more windows */
5602 /* Don't make a difference between horizontal or vertical split. */
5603 last_status_rec(topframe, (p_ls == 2
5604 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5607 static void
5608 last_status_rec(fr, statusline)
5609 frame_T *fr;
5610 int statusline;
5612 frame_T *fp;
5613 win_T *wp;
5615 if (fr->fr_layout == FR_LEAF)
5617 wp = fr->fr_win;
5618 if (wp->w_status_height != 0 && !statusline)
5620 /* remove status line */
5621 win_new_height(wp, wp->w_height + 1);
5622 wp->w_status_height = 0;
5623 comp_col();
5625 else if (wp->w_status_height == 0 && statusline)
5627 /* Find a frame to take a line from. */
5628 fp = fr;
5629 while (fp->fr_height <= frame_minheight(fp, NULL))
5631 if (fp == topframe)
5633 EMSG(_(e_noroom));
5634 return;
5636 /* In a column of frames: go to frame above. If already at
5637 * the top or in a row of frames: go to parent. */
5638 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5639 fp = fp->fr_prev;
5640 else
5641 fp = fp->fr_parent;
5643 wp->w_status_height = 1;
5644 if (fp != fr)
5646 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5647 frame_fix_height(wp);
5648 (void)win_comp_pos();
5650 else
5651 win_new_height(wp, wp->w_height - 1);
5652 comp_col();
5653 redraw_all_later(SOME_VALID);
5656 #ifdef FEAT_VERTSPLIT
5657 else if (fr->fr_layout == FR_ROW)
5659 /* vertically split windows, set status line for each one */
5660 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5661 last_status_rec(fp, statusline);
5663 #endif
5664 else
5666 /* horizontally split window, set status line for last one */
5667 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5669 last_status_rec(fp, statusline);
5674 * Return the number of lines used by the tab page line.
5677 tabline_height()
5679 #ifdef FEAT_GUI_TABLINE
5680 /* When the GUI has the tabline then this always returns zero. */
5681 if (gui_use_tabline())
5682 return 0;
5683 #endif
5684 switch (p_stal)
5686 case 0: return 0;
5687 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5689 return 1;
5692 #endif /* FEAT_WINDOWS */
5694 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5696 * Get the file name at the cursor.
5697 * If Visual mode is active, use the selected text if it's in one line.
5698 * Returns the name in allocated memory, NULL for failure.
5700 char_u *
5701 grab_file_name(count, file_lnum)
5702 long count;
5703 linenr_T *file_lnum;
5705 # ifdef FEAT_VISUAL
5706 if (VIsual_active)
5708 int len;
5709 char_u *ptr;
5711 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5712 return NULL;
5713 return find_file_name_in_path(ptr, len,
5714 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5716 # endif
5717 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5718 file_lnum);
5723 * Return the file name under or after the cursor.
5725 * The 'path' option is searched if the file name is not absolute.
5726 * The string returned has been alloc'ed and should be freed by the caller.
5727 * NULL is returned if the file name or file is not found.
5729 * options:
5730 * FNAME_MESS give error messages
5731 * FNAME_EXP expand to path
5732 * FNAME_HYP check for hypertext link
5733 * FNAME_INCL apply "includeexpr"
5735 char_u *
5736 file_name_at_cursor(options, count, file_lnum)
5737 int options;
5738 long count;
5739 linenr_T *file_lnum;
5741 return file_name_in_line(ml_get_curline(),
5742 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5743 file_lnum);
5747 * Return the name of the file under or after ptr[col].
5748 * Otherwise like file_name_at_cursor().
5750 char_u *
5751 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5752 char_u *line;
5753 int col;
5754 int options;
5755 long count;
5756 char_u *rel_fname; /* file we are searching relative to */
5757 linenr_T *file_lnum; /* line number after the file name */
5759 char_u *ptr;
5760 int len;
5763 * search forward for what could be the start of a file name
5765 ptr = line + col;
5766 while (*ptr != NUL && !vim_isfilec(*ptr))
5767 mb_ptr_adv(ptr);
5768 if (*ptr == NUL) /* nothing found */
5770 if (options & FNAME_MESS)
5771 EMSG(_("E446: No file name under cursor"));
5772 return NULL;
5776 * Search backward for first char of the file name.
5777 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5779 while (ptr > line)
5781 #ifdef FEAT_MBYTE
5782 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5783 ptr -= len + 1;
5784 else
5785 #endif
5786 if (vim_isfilec(ptr[-1])
5787 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5788 --ptr;
5789 else
5790 break;
5794 * Search forward for the last char of the file name.
5795 * Also allow "://" when ':' is not in 'isfname'.
5797 len = 0;
5798 while (vim_isfilec(ptr[len])
5799 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5800 #ifdef FEAT_MBYTE
5801 if (has_mbyte)
5802 len += (*mb_ptr2len)(ptr + len);
5803 else
5804 #endif
5805 ++len;
5808 * If there is trailing punctuation, remove it.
5809 * But don't remove "..", could be a directory name.
5811 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5812 && ptr[len - 2] != '.')
5813 --len;
5815 if (file_lnum != NULL)
5817 char_u *p;
5819 /* Get the number after the file name and a separator character */
5820 p = ptr + len;
5821 p = skipwhite(p);
5822 if (*p != NUL)
5824 if (!isdigit(*p))
5825 ++p; /* skip the separator */
5826 p = skipwhite(p);
5827 if (isdigit(*p))
5828 *file_lnum = (int)getdigits(&p);
5832 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5835 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5836 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5838 static char_u *
5839 eval_includeexpr(ptr, len)
5840 char_u *ptr;
5841 int len;
5843 char_u *res;
5845 set_vim_var_string(VV_FNAME, ptr, len);
5846 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5847 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5848 set_vim_var_string(VV_FNAME, NULL, 0);
5849 return res;
5851 #endif
5854 * Return the name of the file ptr[len] in 'path'.
5855 * Otherwise like file_name_at_cursor().
5857 char_u *
5858 find_file_name_in_path(ptr, len, options, count, rel_fname)
5859 char_u *ptr;
5860 int len;
5861 int options;
5862 long count;
5863 char_u *rel_fname; /* file we are searching relative to */
5865 char_u *file_name;
5866 int c;
5867 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5868 char_u *tofree = NULL;
5870 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5872 tofree = eval_includeexpr(ptr, len);
5873 if (tofree != NULL)
5875 ptr = tofree;
5876 len = (int)STRLEN(ptr);
5879 # endif
5881 if (options & FNAME_EXP)
5883 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5884 TRUE, rel_fname);
5886 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5888 * If the file could not be found in a normal way, try applying
5889 * 'includeexpr' (unless done already).
5891 if (file_name == NULL
5892 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5894 tofree = eval_includeexpr(ptr, len);
5895 if (tofree != NULL)
5897 ptr = tofree;
5898 len = (int)STRLEN(ptr);
5899 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5900 TRUE, rel_fname);
5903 # endif
5904 if (file_name == NULL && (options & FNAME_MESS))
5906 c = ptr[len];
5907 ptr[len] = NUL;
5908 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5909 ptr[len] = c;
5912 /* Repeat finding the file "count" times. This matters when it
5913 * appears several times in the path. */
5914 while (file_name != NULL && --count > 0)
5916 vim_free(file_name);
5917 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5920 else
5921 file_name = vim_strnsave(ptr, len);
5923 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5924 vim_free(tofree);
5925 # endif
5927 return file_name;
5929 #endif /* FEAT_SEARCHPATH */
5932 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5933 * Also check for ":\\", which MS Internet Explorer accepts, return
5934 * URL_BACKSLASH.
5936 static int
5937 path_is_url(p)
5938 char_u *p;
5940 if (STRNCMP(p, "://", (size_t)3) == 0)
5941 return URL_SLASH;
5942 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5943 return URL_BACKSLASH;
5944 return 0;
5948 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5949 * Return URL_BACKSLASH for "name:\\".
5950 * Return zero otherwise.
5953 path_with_url(fname)
5954 char_u *fname;
5956 char_u *p;
5958 for (p = fname; isalpha(*p); ++p)
5960 return path_is_url(p);
5964 * Return TRUE if "name" is a full (absolute) path name or URL.
5967 vim_isAbsName(name)
5968 char_u *name;
5970 return (path_with_url(name) != 0 || mch_isFullName(name));
5974 * Get absolute file name into buffer "buf[len]".
5976 * return FAIL for failure, OK otherwise
5979 vim_FullName(fname, buf, len, force)
5980 char_u *fname, *buf;
5981 int len;
5982 int force; /* force expansion even when already absolute */
5984 int retval = OK;
5985 int url;
5987 *buf = NUL;
5988 if (fname == NULL)
5989 return FAIL;
5991 url = path_with_url(fname);
5992 if (!url)
5993 retval = mch_FullName(fname, buf, len, force);
5994 if (url || retval == FAIL)
5996 /* something failed; use the file name (truncate when too long) */
5997 vim_strncpy(buf, fname, len - 1);
5999 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6000 slash_adjust(buf);
6001 #endif
6002 return retval;
6006 * Return the minimal number of rows that is needed on the screen to display
6007 * the current number of windows.
6010 min_rows()
6012 int total;
6013 #ifdef FEAT_WINDOWS
6014 tabpage_T *tp;
6015 int n;
6016 #endif
6018 if (firstwin == NULL) /* not initialized yet */
6019 return MIN_LINES;
6021 #ifdef FEAT_WINDOWS
6022 total = 0;
6023 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6025 n = frame_minheight(tp->tp_topframe, NULL);
6026 if (total < n)
6027 total = n;
6029 total += tabline_height();
6030 #else
6031 total = 1; /* at least one window should have a line! */
6032 #endif
6033 total += 1; /* count the room for the command line */
6034 return total;
6038 * Return TRUE if there is only one window (in the current tab page), not
6039 * counting a help or preview window, unless it is the current window.
6042 only_one_window()
6044 #ifdef FEAT_WINDOWS
6045 int count = 0;
6046 win_T *wp;
6048 /* If there is another tab page there always is another window. */
6049 if (first_tabpage->tp_next != NULL)
6050 return FALSE;
6052 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6053 if (!((wp->w_buffer->b_help && !curbuf->b_help)
6054 # ifdef FEAT_QUICKFIX
6055 || wp->w_p_pvw
6056 # endif
6057 ) || wp == curwin)
6058 ++count;
6059 return (count <= 1);
6060 #else
6061 return TRUE;
6062 #endif
6065 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6067 * Correct the cursor line number in other windows. Used after changing the
6068 * current buffer, and before applying autocommands.
6069 * When "do_curwin" is TRUE, also check current window.
6071 void
6072 check_lnums(do_curwin)
6073 int do_curwin;
6075 win_T *wp;
6077 #ifdef FEAT_WINDOWS
6078 tabpage_T *tp;
6080 FOR_ALL_TAB_WINDOWS(tp, wp)
6081 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6082 #else
6083 wp = curwin;
6084 if (do_curwin)
6085 #endif
6087 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6088 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6089 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6090 wp->w_topline = curbuf->b_ml.ml_line_count;
6093 #endif
6095 #if defined(FEAT_WINDOWS) || defined(PROTO)
6098 * A snapshot of the window sizes, to restore them after closing the help
6099 * window.
6100 * Only these fields are used:
6101 * fr_layout
6102 * fr_width
6103 * fr_height
6104 * fr_next
6105 * fr_child
6106 * fr_win (only valid for the old curwin, NULL otherwise)
6110 * Create a snapshot of the current frame sizes.
6112 static void
6113 make_snapshot()
6115 clear_snapshot(curtab);
6116 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6119 static void
6120 make_snapshot_rec(fr, frp)
6121 frame_T *fr;
6122 frame_T **frp;
6124 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6125 if (*frp == NULL)
6126 return;
6127 (*frp)->fr_layout = fr->fr_layout;
6128 # ifdef FEAT_VERTSPLIT
6129 (*frp)->fr_width = fr->fr_width;
6130 # endif
6131 (*frp)->fr_height = fr->fr_height;
6132 if (fr->fr_next != NULL)
6133 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6134 if (fr->fr_child != NULL)
6135 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6136 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6137 (*frp)->fr_win = curwin;
6141 * Remove any existing snapshot.
6143 static void
6144 clear_snapshot(tp)
6145 tabpage_T *tp;
6147 clear_snapshot_rec(tp->tp_snapshot);
6148 tp->tp_snapshot = NULL;
6151 static void
6152 clear_snapshot_rec(fr)
6153 frame_T *fr;
6155 if (fr != NULL)
6157 clear_snapshot_rec(fr->fr_next);
6158 clear_snapshot_rec(fr->fr_child);
6159 vim_free(fr);
6164 * Restore a previously created snapshot, if there is any.
6165 * This is only done if the screen size didn't change and the window layout is
6166 * still the same.
6168 static void
6169 restore_snapshot(close_curwin)
6170 int close_curwin; /* closing current window */
6172 win_T *wp;
6174 if (curtab->tp_snapshot != NULL
6175 # ifdef FEAT_VERTSPLIT
6176 && curtab->tp_snapshot->fr_width == topframe->fr_width
6177 # endif
6178 && curtab->tp_snapshot->fr_height == topframe->fr_height
6179 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6181 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6182 win_comp_pos();
6183 if (wp != NULL && close_curwin)
6184 win_goto(wp);
6185 redraw_all_later(CLEAR);
6187 clear_snapshot(curtab);
6191 * Check if frames "sn" and "fr" have the same layout, same following frames
6192 * and same children.
6194 static int
6195 check_snapshot_rec(sn, fr)
6196 frame_T *sn;
6197 frame_T *fr;
6199 if (sn->fr_layout != fr->fr_layout
6200 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6201 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6202 || (sn->fr_next != NULL
6203 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6204 || (sn->fr_child != NULL
6205 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6206 return FAIL;
6207 return OK;
6211 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6212 * following frames and children.
6213 * Returns a pointer to the old current window, or NULL.
6215 static win_T *
6216 restore_snapshot_rec(sn, fr)
6217 frame_T *sn;
6218 frame_T *fr;
6220 win_T *wp = NULL;
6221 win_T *wp2;
6223 fr->fr_height = sn->fr_height;
6224 # ifdef FEAT_VERTSPLIT
6225 fr->fr_width = sn->fr_width;
6226 # endif
6227 if (fr->fr_layout == FR_LEAF)
6229 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6230 # ifdef FEAT_VERTSPLIT
6231 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6232 # endif
6233 wp = sn->fr_win;
6235 if (sn->fr_next != NULL)
6237 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6238 if (wp2 != NULL)
6239 wp = wp2;
6241 if (sn->fr_child != NULL)
6243 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6244 if (wp2 != NULL)
6245 wp = wp2;
6247 return wp;
6250 #endif
6252 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6254 * Return TRUE if there is any vertically split window.
6257 win_hasvertsplit()
6259 frame_T *fr;
6261 if (topframe->fr_layout == FR_ROW)
6262 return TRUE;
6264 if (topframe->fr_layout == FR_COL)
6265 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6266 if (fr->fr_layout == FR_ROW)
6267 return TRUE;
6269 return FALSE;
6271 #endif
6273 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6275 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6276 * highlighted with the group 'grp' with priority 'prio'.
6277 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6278 * If no particular ID is desired, -1 must be specified for 'id'.
6279 * Return ID of added match, -1 on failure.
6282 match_add(wp, grp, pat, prio, id)
6283 win_T *wp;
6284 char_u *grp;
6285 char_u *pat;
6286 int prio;
6287 int id;
6289 matchitem_T *cur;
6290 matchitem_T *prev;
6291 matchitem_T *m;
6292 int hlg_id;
6293 regprog_T *regprog;
6295 if (*grp == NUL || *pat == NUL)
6296 return -1;
6297 if (id < -1 || id == 0)
6299 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6300 return -1;
6302 if (id != -1)
6304 cur = wp->w_match_head;
6305 while (cur != NULL)
6307 if (cur->id == id)
6309 EMSGN("E801: ID already taken: %ld", id);
6310 return -1;
6312 cur = cur->next;
6315 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6317 EMSG2(_(e_nogroup), grp);
6318 return -1;
6320 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6322 EMSG2(_(e_invarg2), pat);
6323 return -1;
6326 /* Find available match ID. */
6327 while (id == -1)
6329 cur = wp->w_match_head;
6330 while (cur != NULL && cur->id != wp->w_next_match_id)
6331 cur = cur->next;
6332 if (cur == NULL)
6333 id = wp->w_next_match_id;
6334 wp->w_next_match_id++;
6337 /* Build new match. */
6338 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6339 m->id = id;
6340 m->priority = prio;
6341 m->pattern = vim_strsave(pat);
6342 m->hlg_id = hlg_id;
6343 m->match.regprog = regprog;
6344 m->match.rmm_ic = FALSE;
6345 m->match.rmm_maxcol = 0;
6347 /* Insert new match. The match list is in ascending order with regard to
6348 * the match priorities. */
6349 cur = wp->w_match_head;
6350 prev = cur;
6351 while (cur != NULL && prio >= cur->priority)
6353 prev = cur;
6354 cur = cur->next;
6356 if (cur == prev)
6357 wp->w_match_head = m;
6358 else
6359 prev->next = m;
6360 m->next = cur;
6362 redraw_later(SOME_VALID);
6363 return id;
6367 * Delete match with ID 'id' in the match list of window 'wp'.
6368 * Print error messages if 'perr' is TRUE.
6371 match_delete(wp, id, perr)
6372 win_T *wp;
6373 int id;
6374 int perr;
6376 matchitem_T *cur = wp->w_match_head;
6377 matchitem_T *prev = cur;
6379 if (id < 1)
6381 if (perr == TRUE)
6382 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6383 id);
6384 return -1;
6386 while (cur != NULL && cur->id != id)
6388 prev = cur;
6389 cur = cur->next;
6391 if (cur == NULL)
6393 if (perr == TRUE)
6394 EMSGN("E803: ID not found: %ld", id);
6395 return -1;
6397 if (cur == prev)
6398 wp->w_match_head = cur->next;
6399 else
6400 prev->next = cur->next;
6401 vim_free(cur->match.regprog);
6402 vim_free(cur->pattern);
6403 vim_free(cur);
6404 redraw_later(SOME_VALID);
6405 return 0;
6409 * Delete all matches in the match list of window 'wp'.
6411 void
6412 clear_matches(wp)
6413 win_T *wp;
6415 matchitem_T *m;
6417 while (wp->w_match_head != NULL)
6419 m = wp->w_match_head->next;
6420 vim_free(wp->w_match_head->match.regprog);
6421 vim_free(wp->w_match_head->pattern);
6422 vim_free(wp->w_match_head);
6423 wp->w_match_head = m;
6425 redraw_later(SOME_VALID);
6429 * Get match from ID 'id' in window 'wp'.
6430 * Return NULL if match not found.
6432 matchitem_T *
6433 get_match(wp, id)
6434 win_T *wp;
6435 int id;
6437 matchitem_T *cur = wp->w_match_head;
6439 while (cur != NULL && cur->id != id)
6440 cur = cur->next;
6441 return cur;
6443 #endif