Merged from the latest developing branch.
[MacVim/KaoriYa.git] / src / window.c
blob427fd1f354ff5fcf2d6846995d64d006efad7040
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, int flags));
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 LANGMAP_ADJUST(xchar, TRUE);
598 --no_mapping;
599 --allow_keys;
600 #ifdef FEAT_CMDL_INFO
601 (void)add_to_showcmd(xchar);
602 #endif
603 switch (xchar)
605 #if defined(FEAT_QUICKFIX)
606 case '}':
607 xchar = Ctrl_RSB;
608 if (Prenum)
609 g_do_tagpreview = Prenum;
610 else
611 g_do_tagpreview = p_pvh;
612 /*FALLTHROUGH*/
613 #endif
614 case ']':
615 case Ctrl_RSB:
616 #ifdef FEAT_VISUAL
617 reset_VIsual_and_resel(); /* stop Visual mode */
618 #endif
619 if (Prenum)
620 postponed_split = Prenum;
621 else
622 postponed_split = -1;
624 /* Execute the command right here, required when
625 * "wincmd g}" was used in a function. */
626 do_nv_ident('g', xchar);
627 break;
629 #ifdef FEAT_SEARCHPATH
630 case 'f': /* CTRL-W gf: "gf" in a new tab page */
631 case 'F': /* CTRL-W gF: "gF" in a new tab page */
632 cmdmod.tab = TRUE;
633 nchar = xchar;
634 goto wingotofile;
635 #endif
636 default:
637 beep_flush();
638 break;
640 break;
642 default: beep_flush();
643 break;
648 * split the current window, implements CTRL-W s and :split
650 * "size" is the height or width for the new window, 0 to use half of current
651 * height or width.
653 * "flags":
654 * WSP_ROOM: require enough room for new window
655 * WSP_VERT: vertical split.
656 * WSP_TOP: open window at the top-left of the shell (help window).
657 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
658 * WSP_HELP: creating the help window, keep layout snapshot
660 * return FAIL for failure, OK otherwise
663 win_split(size, flags)
664 int size;
665 int flags;
667 /* When the ":tab" modifier was used open a new tab page instead. */
668 if (may_open_tabpage() == OK)
669 return OK;
671 /* Add flags from ":vertical", ":topleft" and ":botright". */
672 flags |= cmdmod.split;
673 if ((flags & WSP_TOP) && (flags & WSP_BOT))
675 EMSG(_("E442: Can't split topleft and botright at the same time"));
676 return FAIL;
679 /* When creating the help window make a snapshot of the window layout.
680 * Otherwise clear the snapshot, it's now invalid. */
681 if (flags & WSP_HELP)
682 make_snapshot();
683 else
684 clear_snapshot(curtab);
686 return win_split_ins(size, flags, NULL, 0);
690 * When "newwin" is NULL: split the current window in two.
691 * When "newwin" is not NULL: insert this window at the far
692 * top/left/right/bottom.
693 * return FAIL for failure, OK otherwise
695 static int
696 win_split_ins(size, flags, newwin, dir)
697 int size;
698 int flags;
699 win_T *newwin;
700 int dir;
702 win_T *wp = newwin;
703 win_T *oldwin;
704 int new_size = size;
705 int i;
706 int need_status = 0;
707 int do_equal = FALSE;
708 int needed;
709 int available;
710 int oldwin_height = 0;
711 int layout;
712 frame_T *frp, *curfrp;
713 int before;
715 if (flags & WSP_TOP)
716 oldwin = firstwin;
717 else if (flags & WSP_BOT)
718 oldwin = lastwin;
719 else
720 oldwin = curwin;
722 /* add a status line when p_ls == 1 and splitting the first window */
723 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
725 if (oldwin->w_height <= p_wmh && newwin == NULL)
727 EMSG(_(e_noroom));
728 return FAIL;
730 need_status = STATUS_HEIGHT;
733 #ifdef FEAT_GUI
734 /* May be needed for the scrollbars that are going to change. */
735 if (gui.in_use)
736 out_flush();
737 #endif
739 #ifdef FEAT_VERTSPLIT
740 if (flags & WSP_VERT)
742 layout = FR_ROW;
745 * Check if we are able to split the current window and compute its
746 * width.
748 needed = p_wmw + 1;
749 if (flags & WSP_ROOM)
750 needed += p_wiw - p_wmw;
751 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
753 available = topframe->fr_width;
754 needed += frame_minwidth(topframe, NULL);
756 else
757 available = oldwin->w_width;
758 if (available < needed && newwin == NULL)
760 EMSG(_(e_noroom));
761 return FAIL;
763 if (new_size == 0)
764 new_size = oldwin->w_width / 2;
765 if (new_size > oldwin->w_width - p_wmw - 1)
766 new_size = oldwin->w_width - p_wmw - 1;
767 if (new_size < p_wmw)
768 new_size = p_wmw;
770 /* if it doesn't fit in the current window, need win_equal() */
771 if (oldwin->w_width - new_size - 1 < p_wmw)
772 do_equal = TRUE;
774 /* We don't like to take lines for the new window from a
775 * 'winfixwidth' window. Take them from a window to the left or right
776 * instead, if possible. */
777 if (oldwin->w_p_wfw)
778 win_setwidth_win(oldwin->w_width + new_size, oldwin);
780 /* Only make all windows the same width if one of them (except oldwin)
781 * is wider than one of the split windows. */
782 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
783 && oldwin->w_frame->fr_parent != NULL)
785 frp = oldwin->w_frame->fr_parent->fr_child;
786 while (frp != NULL)
788 if (frp->fr_win != oldwin && frp->fr_win != NULL
789 && (frp->fr_win->w_width > new_size
790 || frp->fr_win->w_width > oldwin->w_width
791 - new_size - STATUS_HEIGHT))
793 do_equal = TRUE;
794 break;
796 frp = frp->fr_next;
800 else
801 #endif
803 layout = FR_COL;
806 * Check if we are able to split the current window and compute its
807 * height.
809 needed = p_wmh + STATUS_HEIGHT + need_status;
810 if (flags & WSP_ROOM)
811 needed += p_wh - p_wmh;
812 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
814 available = topframe->fr_height;
815 needed += frame_minheight(topframe, NULL);
817 else
819 available = oldwin->w_height;
820 needed += p_wmh;
822 if (available < needed && newwin == NULL)
824 EMSG(_(e_noroom));
825 return FAIL;
827 oldwin_height = oldwin->w_height;
828 if (need_status)
830 oldwin->w_status_height = STATUS_HEIGHT;
831 oldwin_height -= STATUS_HEIGHT;
833 if (new_size == 0)
834 new_size = oldwin_height / 2;
836 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
837 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
838 if (new_size < p_wmh)
839 new_size = p_wmh;
841 /* if it doesn't fit in the current window, need win_equal() */
842 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
843 do_equal = TRUE;
845 /* We don't like to take lines for the new window from a
846 * 'winfixheight' window. Take them from a window above or below
847 * instead, if possible. */
848 if (oldwin->w_p_wfh)
850 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
851 oldwin);
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 oldwin_height -= STATUS_HEIGHT;
857 /* Only make all windows the same height if one of them (except oldwin)
858 * is higher than one of the split windows. */
859 if (!do_equal && p_ea && size == 0
860 #ifdef FEAT_VERTSPLIT
861 && *p_ead != 'h'
862 #endif
863 && oldwin->w_frame->fr_parent != NULL)
865 frp = oldwin->w_frame->fr_parent->fr_child;
866 while (frp != NULL)
868 if (frp->fr_win != oldwin && frp->fr_win != NULL
869 && (frp->fr_win->w_height > new_size
870 || frp->fr_win->w_height > oldwin_height - new_size
871 - STATUS_HEIGHT))
873 do_equal = TRUE;
874 break;
876 frp = frp->fr_next;
882 * allocate new window structure and link it in the window list
884 if ((flags & WSP_TOP) == 0
885 && ((flags & WSP_BOT)
886 || (flags & WSP_BELOW)
887 || (!(flags & WSP_ABOVE)
888 && (
889 #ifdef FEAT_VERTSPLIT
890 (flags & WSP_VERT) ? p_spr :
891 #endif
892 p_sb))))
894 /* new window below/right of current one */
895 if (newwin == NULL)
896 wp = win_alloc(oldwin);
897 else
898 win_append(oldwin, wp);
900 else
902 if (newwin == NULL)
903 wp = win_alloc(oldwin->w_prev);
904 else
905 win_append(oldwin->w_prev, wp);
908 if (newwin == NULL)
910 if (wp == NULL)
911 return FAIL;
913 /* make the contents of the new window the same as the current one */
914 win_init(wp, curwin, flags);
918 * Reorganise the tree of frames to insert the new window.
920 if (flags & (WSP_TOP | WSP_BOT))
922 #ifdef FEAT_VERTSPLIT
923 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
924 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
925 #else
926 if (topframe->fr_layout == FR_COL)
927 #endif
929 curfrp = topframe->fr_child;
930 if (flags & WSP_BOT)
931 while (curfrp->fr_next != NULL)
932 curfrp = curfrp->fr_next;
934 else
935 curfrp = topframe;
936 before = (flags & WSP_TOP);
938 else
940 curfrp = oldwin->w_frame;
941 if (flags & WSP_BELOW)
942 before = FALSE;
943 else if (flags & WSP_ABOVE)
944 before = TRUE;
945 else
946 #ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
948 before = !p_spr;
949 else
950 #endif
951 before = !p_sb;
953 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
955 /* Need to create a new frame in the tree to make a branch. */
956 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
957 *frp = *curfrp;
958 curfrp->fr_layout = layout;
959 frp->fr_parent = curfrp;
960 frp->fr_next = NULL;
961 frp->fr_prev = NULL;
962 curfrp->fr_child = frp;
963 curfrp->fr_win = NULL;
964 curfrp = frp;
965 if (frp->fr_win != NULL)
966 oldwin->w_frame = frp;
967 else
968 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
969 frp->fr_parent = curfrp;
972 if (newwin == NULL)
974 /* Create a frame for the new window. */
975 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
976 frp->fr_layout = FR_LEAF;
977 frp->fr_win = wp;
978 wp->w_frame = frp;
980 else
981 frp = newwin->w_frame;
982 frp->fr_parent = curfrp->fr_parent;
984 /* Insert the new frame at the right place in the frame list. */
985 if (before)
986 frame_insert(curfrp, frp);
987 else
988 frame_append(curfrp, frp);
990 #ifdef FEAT_VERTSPLIT
991 if (flags & WSP_VERT)
993 wp->w_p_scr = curwin->w_p_scr;
994 if (need_status)
996 --oldwin->w_height;
997 oldwin->w_status_height = need_status;
999 if (flags & (WSP_TOP | WSP_BOT))
1001 /* set height and row of new window to full height */
1002 wp->w_winrow = tabline_height();
1003 wp->w_height = curfrp->fr_height - (p_ls > 0);
1004 wp->w_status_height = (p_ls > 0);
1006 else
1008 /* height and row of new window is same as current window */
1009 wp->w_winrow = oldwin->w_winrow;
1010 wp->w_height = oldwin->w_height;
1011 wp->w_status_height = oldwin->w_status_height;
1013 frp->fr_height = curfrp->fr_height;
1015 /* "new_size" of the current window goes to the new window, use
1016 * one column for the vertical separator */
1017 wp->w_width = new_size;
1018 if (before)
1019 wp->w_vsep_width = 1;
1020 else
1022 wp->w_vsep_width = oldwin->w_vsep_width;
1023 oldwin->w_vsep_width = 1;
1025 if (flags & (WSP_TOP | WSP_BOT))
1027 if (flags & WSP_BOT)
1028 frame_add_vsep(curfrp);
1029 /* Set width of neighbor frame */
1030 frame_new_width(curfrp, curfrp->fr_width
1031 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1032 FALSE);
1034 else
1035 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1036 if (before) /* new window left of current one */
1038 wp->w_wincol = oldwin->w_wincol;
1039 oldwin->w_wincol += new_size + 1;
1041 else /* new window right of current one */
1042 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1043 frame_fix_width(oldwin);
1044 frame_fix_width(wp);
1046 else
1047 #endif
1049 /* width and column of new window is same as current window */
1050 #ifdef FEAT_VERTSPLIT
1051 if (flags & (WSP_TOP | WSP_BOT))
1053 wp->w_wincol = 0;
1054 wp->w_width = Columns;
1055 wp->w_vsep_width = 0;
1057 else
1059 wp->w_wincol = oldwin->w_wincol;
1060 wp->w_width = oldwin->w_width;
1061 wp->w_vsep_width = oldwin->w_vsep_width;
1063 frp->fr_width = curfrp->fr_width;
1064 #endif
1066 /* "new_size" of the current window goes to the new window, use
1067 * one row for the status line */
1068 win_new_height(wp, new_size);
1069 if (flags & (WSP_TOP | WSP_BOT))
1070 frame_new_height(curfrp, curfrp->fr_height
1071 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1072 else
1073 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1074 if (before) /* new window above current one */
1076 wp->w_winrow = oldwin->w_winrow;
1077 wp->w_status_height = STATUS_HEIGHT;
1078 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1080 else /* new window below current one */
1082 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1083 wp->w_status_height = oldwin->w_status_height;
1084 oldwin->w_status_height = STATUS_HEIGHT;
1086 #ifdef FEAT_VERTSPLIT
1087 if (flags & WSP_BOT)
1088 frame_add_statusline(curfrp);
1089 #endif
1090 frame_fix_height(wp);
1091 frame_fix_height(oldwin);
1094 if (flags & (WSP_TOP | WSP_BOT))
1095 (void)win_comp_pos();
1098 * Both windows need redrawing
1100 redraw_win_later(wp, NOT_VALID);
1101 wp->w_redr_status = TRUE;
1102 redraw_win_later(oldwin, NOT_VALID);
1103 oldwin->w_redr_status = TRUE;
1105 if (need_status)
1107 msg_row = Rows - 1;
1108 msg_col = sc_col;
1109 msg_clr_eos_force(); /* Old command/ruler may still be there */
1110 comp_col();
1111 msg_row = Rows - 1;
1112 msg_col = 0; /* put position back at start of line */
1116 * make the new window the current window and redraw
1118 if (do_equal || dir != 0)
1119 win_equal(wp, TRUE,
1120 #ifdef FEAT_VERTSPLIT
1121 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1122 : dir == 'h' ? 'b' :
1123 #endif
1124 'v');
1126 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1127 * size was given. */
1128 #ifdef FEAT_VERTSPLIT
1129 if (flags & WSP_VERT)
1131 i = p_wiw;
1132 if (size != 0)
1133 p_wiw = size;
1135 # ifdef FEAT_GUI
1136 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1137 if (gui.in_use)
1138 gui_init_which_components(NULL);
1139 # endif
1141 else
1142 #endif
1144 i = p_wh;
1145 if (size != 0)
1146 p_wh = size;
1148 win_enter(wp, FALSE);
1149 #ifdef FEAT_VERTSPLIT
1150 if (flags & WSP_VERT)
1151 p_wiw = i;
1152 else
1153 #endif
1154 p_wh = i;
1156 return OK;
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
1163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
1166 static void
1167 win_init(newp, oldp, flags)
1168 win_T *newp;
1169 win_T *oldp;
1170 int flags UNUSED;
1172 int i;
1174 newp->w_buffer = oldp->w_buffer;
1175 oldp->w_buffer->b_nwindows++;
1176 newp->w_cursor = oldp->w_cursor;
1177 newp->w_valid = 0;
1178 newp->w_curswant = oldp->w_curswant;
1179 newp->w_set_curswant = oldp->w_set_curswant;
1180 newp->w_topline = oldp->w_topline;
1181 #ifdef FEAT_DIFF
1182 newp->w_topfill = oldp->w_topfill;
1183 #endif
1184 newp->w_leftcol = oldp->w_leftcol;
1185 newp->w_pcmark = oldp->w_pcmark;
1186 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1187 newp->w_alt_fnum = oldp->w_alt_fnum;
1188 newp->w_wrow = oldp->w_wrow;
1189 newp->w_fraction = oldp->w_fraction;
1190 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1191 #ifdef FEAT_JUMPLIST
1192 copy_jumplist(oldp, newp);
1193 #endif
1194 #ifdef FEAT_QUICKFIX
1195 if (flags & WSP_NEWLOC)
1197 /* Don't copy the location list. */
1198 newp->w_llist = NULL;
1199 newp->w_llist_ref = NULL;
1201 else
1202 copy_loclist(oldp, newp);
1203 #endif
1204 if (oldp->w_localdir != NULL)
1205 newp->w_localdir = vim_strsave(oldp->w_localdir);
1207 /* Use the same argument list. */
1208 newp->w_alist = oldp->w_alist;
1209 ++newp->w_alist->al_refcount;
1210 newp->w_arg_idx = oldp->w_arg_idx;
1213 * copy tagstack and options from existing window
1215 for (i = 0; i < oldp->w_tagstacklen; i++)
1217 newp->w_tagstack[i] = oldp->w_tagstack[i];
1218 if (newp->w_tagstack[i].tagname != NULL)
1219 newp->w_tagstack[i].tagname =
1220 vim_strsave(newp->w_tagstack[i].tagname);
1222 newp->w_tagstackidx = oldp->w_tagstackidx;
1223 newp->w_tagstacklen = oldp->w_tagstacklen;
1224 win_copy_options(oldp, newp);
1225 # ifdef FEAT_FOLDING
1226 copyFoldingState(oldp, newp);
1227 # endif
1230 #endif /* FEAT_WINDOWS */
1232 #if defined(FEAT_WINDOWS) || defined(PROTO)
1234 * Check if "win" is a pointer to an existing window.
1237 win_valid(win)
1238 win_T *win;
1240 win_T *wp;
1242 if (win == NULL)
1243 return FALSE;
1244 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1245 if (wp == win)
1246 return TRUE;
1247 return FALSE;
1251 * Return the number of windows.
1254 win_count()
1256 win_T *wp;
1257 int count = 0;
1259 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1260 ++count;
1261 return count;
1265 * Make "count" windows on the screen.
1266 * Return actual number of windows on the screen.
1267 * Must be called when there is just one window, filling the whole screen
1268 * (excluding the command line).
1271 make_windows(count, vertical)
1272 int count;
1273 int vertical UNUSED; /* split windows vertically if TRUE */
1275 int maxcount;
1276 int todo;
1278 #ifdef FEAT_VERTSPLIT
1279 if (vertical)
1281 /* Each windows needs at least 'winminwidth' lines and a separator
1282 * column. */
1283 maxcount = (curwin->w_width + curwin->w_vsep_width
1284 - (p_wiw - p_wmw)) / (p_wmw + 1);
1286 else
1287 #endif
1289 /* Each window needs at least 'winminheight' lines and a status line. */
1290 maxcount = (curwin->w_height + curwin->w_status_height
1291 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1294 if (maxcount < 2)
1295 maxcount = 2;
1296 if (count > maxcount)
1297 count = maxcount;
1300 * add status line now, otherwise first window will be too big
1302 if (count > 1)
1303 last_status(TRUE);
1305 #ifdef FEAT_AUTOCMD
1307 * Don't execute autocommands while creating the windows. Must do that
1308 * when putting the buffers in the windows.
1310 block_autocmds();
1311 #endif
1313 /* todo is number of windows left to create */
1314 for (todo = count - 1; todo > 0; --todo)
1315 #ifdef FEAT_VERTSPLIT
1316 if (vertical)
1318 if (win_split(curwin->w_width - (curwin->w_width - todo)
1319 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1320 break;
1322 else
1323 #endif
1325 if (win_split(curwin->w_height - (curwin->w_height - todo
1326 * STATUS_HEIGHT) / (todo + 1)
1327 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1328 break;
1331 #ifdef FEAT_AUTOCMD
1332 unblock_autocmds();
1333 #endif
1335 /* return actual number of windows */
1336 return (count - todo);
1340 * Exchange current and next window
1342 static void
1343 win_exchange(Prenum)
1344 long Prenum;
1346 frame_T *frp;
1347 frame_T *frp2;
1348 win_T *wp;
1349 win_T *wp2;
1350 int temp;
1352 if (lastwin == firstwin) /* just one window */
1354 beep_flush();
1355 return;
1358 #ifdef FEAT_GUI
1359 need_mouse_correct = TRUE;
1360 #endif
1363 * find window to exchange with
1365 if (Prenum)
1367 frp = curwin->w_frame->fr_parent->fr_child;
1368 while (frp != NULL && --Prenum > 0)
1369 frp = frp->fr_next;
1371 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1372 frp = curwin->w_frame->fr_next;
1373 else /* Swap last window in row/col with previous */
1374 frp = curwin->w_frame->fr_prev;
1376 /* We can only exchange a window with another window, not with a frame
1377 * containing windows. */
1378 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1379 return;
1380 wp = frp->fr_win;
1383 * 1. remove curwin from the list. Remember after which window it was in wp2
1384 * 2. insert curwin before wp in the list
1385 * if wp != wp2
1386 * 3. remove wp from the list
1387 * 4. insert wp after wp2
1388 * 5. exchange the status line height and vsep width.
1390 wp2 = curwin->w_prev;
1391 frp2 = curwin->w_frame->fr_prev;
1392 if (wp->w_prev != curwin)
1394 win_remove(curwin, NULL);
1395 frame_remove(curwin->w_frame);
1396 win_append(wp->w_prev, curwin);
1397 frame_insert(frp, curwin->w_frame);
1399 if (wp != wp2)
1401 win_remove(wp, NULL);
1402 frame_remove(wp->w_frame);
1403 win_append(wp2, wp);
1404 if (frp2 == NULL)
1405 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1406 else
1407 frame_append(frp2, wp->w_frame);
1409 temp = curwin->w_status_height;
1410 curwin->w_status_height = wp->w_status_height;
1411 wp->w_status_height = temp;
1412 #ifdef FEAT_VERTSPLIT
1413 temp = curwin->w_vsep_width;
1414 curwin->w_vsep_width = wp->w_vsep_width;
1415 wp->w_vsep_width = temp;
1417 /* If the windows are not in the same frame, exchange the sizes to avoid
1418 * messing up the window layout. Otherwise fix the frame sizes. */
1419 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1421 temp = curwin->w_height;
1422 curwin->w_height = wp->w_height;
1423 wp->w_height = temp;
1424 temp = curwin->w_width;
1425 curwin->w_width = wp->w_width;
1426 wp->w_width = temp;
1428 else
1430 frame_fix_height(curwin);
1431 frame_fix_height(wp);
1432 frame_fix_width(curwin);
1433 frame_fix_width(wp);
1435 #endif
1437 (void)win_comp_pos(); /* recompute window positions */
1439 win_enter(wp, TRUE);
1440 redraw_later(CLEAR);
1444 * rotate windows: if upwards TRUE the second window becomes the first one
1445 * if upwards FALSE the first window becomes the second one
1447 static void
1448 win_rotate(upwards, count)
1449 int upwards;
1450 int count;
1452 win_T *wp1;
1453 win_T *wp2;
1454 frame_T *frp;
1455 int n;
1457 if (firstwin == lastwin) /* nothing to do */
1459 beep_flush();
1460 return;
1463 #ifdef FEAT_GUI
1464 need_mouse_correct = TRUE;
1465 #endif
1467 #ifdef FEAT_VERTSPLIT
1468 /* Check if all frames in this row/col have one window. */
1469 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1470 frp = frp->fr_next)
1471 if (frp->fr_win == NULL)
1473 EMSG(_("E443: Cannot rotate when another window is split"));
1474 return;
1476 #endif
1478 while (count--)
1480 if (upwards) /* first window becomes last window */
1482 /* remove first window/frame from the list */
1483 frp = curwin->w_frame->fr_parent->fr_child;
1484 wp1 = frp->fr_win;
1485 win_remove(wp1, NULL);
1486 frame_remove(frp);
1488 /* find last frame and append removed window/frame after it */
1489 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1491 win_append(frp->fr_win, wp1);
1492 frame_append(frp, wp1->w_frame);
1494 wp2 = frp->fr_win; /* previously last window */
1496 else /* last window becomes first window */
1498 /* find last window/frame in the list and remove it */
1499 for (frp = curwin->w_frame; frp->fr_next != NULL;
1500 frp = frp->fr_next)
1502 wp1 = frp->fr_win;
1503 wp2 = wp1->w_prev; /* will become last window */
1504 win_remove(wp1, NULL);
1505 frame_remove(frp);
1507 /* append the removed window/frame before the first in the list */
1508 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1509 frame_insert(frp->fr_parent->fr_child, frp);
1512 /* exchange status height and vsep width of old and new last window */
1513 n = wp2->w_status_height;
1514 wp2->w_status_height = wp1->w_status_height;
1515 wp1->w_status_height = n;
1516 frame_fix_height(wp1);
1517 frame_fix_height(wp2);
1518 #ifdef FEAT_VERTSPLIT
1519 n = wp2->w_vsep_width;
1520 wp2->w_vsep_width = wp1->w_vsep_width;
1521 wp1->w_vsep_width = n;
1522 frame_fix_width(wp1);
1523 frame_fix_width(wp2);
1524 #endif
1526 /* recompute w_winrow and w_wincol for all windows */
1527 (void)win_comp_pos();
1530 redraw_later(CLEAR);
1534 * Move the current window to the very top/bottom/left/right of the screen.
1536 static void
1537 win_totop(size, flags)
1538 int size;
1539 int flags;
1541 int dir;
1542 int height = curwin->w_height;
1544 if (lastwin == firstwin)
1546 beep_flush();
1547 return;
1550 /* Remove the window and frame from the tree of frames. */
1551 (void)winframe_remove(curwin, &dir, NULL);
1552 win_remove(curwin, NULL);
1553 last_status(FALSE); /* may need to remove last status line */
1554 (void)win_comp_pos(); /* recompute window positions */
1556 /* Split a window on the desired side and put the window there. */
1557 (void)win_split_ins(size, flags, curwin, dir);
1558 if (!(flags & WSP_VERT))
1560 win_setheight(height);
1561 if (p_ea)
1562 win_equal(curwin, TRUE, 'v');
1565 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1566 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1567 * scrollbars. Have to update them anyway. */
1568 if (gui.in_use)
1570 out_flush();
1571 gui_init_which_components(NULL);
1572 gui_update_scrollbars(TRUE);
1574 need_mouse_correct = TRUE;
1575 #endif
1580 * Move window "win1" to below/right of "win2" and make "win1" the current
1581 * window. Only works within the same frame!
1583 void
1584 win_move_after(win1, win2)
1585 win_T *win1, *win2;
1587 int height;
1589 /* check if the arguments are reasonable */
1590 if (win1 == win2)
1591 return;
1593 /* check if there is something to do */
1594 if (win2->w_next != win1)
1596 /* may need move the status line/vertical separator of the last window
1597 * */
1598 if (win1 == lastwin)
1600 height = win1->w_prev->w_status_height;
1601 win1->w_prev->w_status_height = win1->w_status_height;
1602 win1->w_status_height = height;
1603 #ifdef FEAT_VERTSPLIT
1604 if (win1->w_prev->w_vsep_width == 1)
1606 /* Remove the vertical separator from the last-but-one window,
1607 * add it to the last window. Adjust the frame widths. */
1608 win1->w_prev->w_vsep_width = 0;
1609 win1->w_prev->w_frame->fr_width -= 1;
1610 win1->w_vsep_width = 1;
1611 win1->w_frame->fr_width += 1;
1613 #endif
1615 else if (win2 == lastwin)
1617 height = win1->w_status_height;
1618 win1->w_status_height = win2->w_status_height;
1619 win2->w_status_height = height;
1620 #ifdef FEAT_VERTSPLIT
1621 if (win1->w_vsep_width == 1)
1623 /* Remove the vertical separator from win1, add it to the last
1624 * window, win2. Adjust the frame widths. */
1625 win2->w_vsep_width = 1;
1626 win2->w_frame->fr_width += 1;
1627 win1->w_vsep_width = 0;
1628 win1->w_frame->fr_width -= 1;
1630 #endif
1632 win_remove(win1, NULL);
1633 frame_remove(win1->w_frame);
1634 win_append(win2, win1);
1635 frame_append(win2->w_frame, win1->w_frame);
1637 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1638 redraw_later(NOT_VALID);
1640 win_enter(win1, FALSE);
1644 * Make all windows the same height.
1645 * 'next_curwin' will soon be the current window, make sure it has enough
1646 * rows.
1648 void
1649 win_equal(next_curwin, current, dir)
1650 win_T *next_curwin; /* pointer to current window to be or NULL */
1651 int current; /* do only frame with current window */
1652 int dir; /* 'v' for vertically, 'h' for horizontally,
1653 'b' for both, 0 for using p_ead */
1655 if (dir == 0)
1656 #ifdef FEAT_VERTSPLIT
1657 dir = *p_ead;
1658 #else
1659 dir = 'b';
1660 #endif
1661 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1662 topframe, dir, 0, tabline_height(),
1663 (int)Columns, topframe->fr_height);
1667 * Set a frame to a new position and height, spreading the available room
1668 * equally over contained frames.
1669 * The window "next_curwin" (if not NULL) should at least get the size from
1670 * 'winheight' and 'winwidth' if possible.
1672 static void
1673 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1674 win_T *next_curwin; /* pointer to current window to be or NULL */
1675 int current; /* do only frame with current window */
1676 frame_T *topfr; /* frame to set size off */
1677 int dir; /* 'v', 'h' or 'b', see win_equal() */
1678 int col; /* horizontal position for frame */
1679 int row; /* vertical position for frame */
1680 int width; /* new width of frame */
1681 int height; /* new height of frame */
1683 int n, m;
1684 int extra_sep = 0;
1685 int wincount, totwincount = 0;
1686 frame_T *fr;
1687 int next_curwin_size = 0;
1688 int room = 0;
1689 int new_size;
1690 int has_next_curwin = 0;
1691 int hnc;
1693 if (topfr->fr_layout == FR_LEAF)
1695 /* Set the width/height of this frame.
1696 * Redraw when size or position changes */
1697 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1698 #ifdef FEAT_VERTSPLIT
1699 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1700 #endif
1703 topfr->fr_win->w_winrow = row;
1704 frame_new_height(topfr, height, FALSE, FALSE);
1705 #ifdef FEAT_VERTSPLIT
1706 topfr->fr_win->w_wincol = col;
1707 frame_new_width(topfr, width, FALSE, FALSE);
1708 #endif
1709 redraw_all_later(CLEAR);
1712 #ifdef FEAT_VERTSPLIT
1713 else if (topfr->fr_layout == FR_ROW)
1715 topfr->fr_width = width;
1716 topfr->fr_height = height;
1718 if (dir != 'v') /* equalize frame widths */
1720 /* Compute the maximum number of windows horizontally in this
1721 * frame. */
1722 n = frame_minwidth(topfr, NOWIN);
1723 /* add one for the rightmost window, it doesn't have a separator */
1724 if (col + width == Columns)
1725 extra_sep = 1;
1726 else
1727 extra_sep = 0;
1728 totwincount = (n + extra_sep) / (p_wmw + 1);
1729 has_next_curwin = frame_has_win(topfr, next_curwin);
1732 * Compute width for "next_curwin" window and room available for
1733 * other windows.
1734 * "m" is the minimal width when counting p_wiw for "next_curwin".
1736 m = frame_minwidth(topfr, next_curwin);
1737 room = width - m;
1738 if (room < 0)
1740 next_curwin_size = p_wiw + room;
1741 room = 0;
1743 else
1745 next_curwin_size = -1;
1746 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1748 /* If 'winfixwidth' set keep the window width if
1749 * possible.
1750 * Watch out for this window being the next_curwin. */
1751 if (frame_fixed_width(fr))
1753 n = frame_minwidth(fr, NOWIN);
1754 new_size = fr->fr_width;
1755 if (frame_has_win(fr, next_curwin))
1757 room += p_wiw - p_wmw;
1758 next_curwin_size = 0;
1759 if (new_size < p_wiw)
1760 new_size = p_wiw;
1762 else
1763 /* These windows don't use up room. */
1764 totwincount -= (n + (fr->fr_next == NULL
1765 ? extra_sep : 0)) / (p_wmw + 1);
1766 room -= new_size - n;
1767 if (room < 0)
1769 new_size += room;
1770 room = 0;
1772 fr->fr_newwidth = new_size;
1775 if (next_curwin_size == -1)
1777 if (!has_next_curwin)
1778 next_curwin_size = 0;
1779 else if (totwincount > 1
1780 && (room + (totwincount - 2))
1781 / (totwincount - 1) > p_wiw)
1783 /* Can make all windows wider than 'winwidth', spread
1784 * the room equally. */
1785 next_curwin_size = (room + p_wiw
1786 + (totwincount - 1) * p_wmw
1787 + (totwincount - 1)) / totwincount;
1788 room -= next_curwin_size - p_wiw;
1790 else
1791 next_curwin_size = p_wiw;
1795 if (has_next_curwin)
1796 --totwincount; /* don't count curwin */
1799 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1801 n = m = 0;
1802 wincount = 1;
1803 if (fr->fr_next == NULL)
1804 /* last frame gets all that remains (avoid roundoff error) */
1805 new_size = width;
1806 else if (dir == 'v')
1807 new_size = fr->fr_width;
1808 else if (frame_fixed_width(fr))
1810 new_size = fr->fr_newwidth;
1811 wincount = 0; /* doesn't count as a sizeable window */
1813 else
1815 /* Compute the maximum number of windows horiz. in "fr". */
1816 n = frame_minwidth(fr, NOWIN);
1817 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1818 / (p_wmw + 1);
1819 m = frame_minwidth(fr, next_curwin);
1820 if (has_next_curwin)
1821 hnc = frame_has_win(fr, next_curwin);
1822 else
1823 hnc = FALSE;
1824 if (hnc) /* don't count next_curwin */
1825 --wincount;
1826 if (totwincount == 0)
1827 new_size = room;
1828 else
1829 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1830 / totwincount;
1831 if (hnc) /* add next_curwin size */
1833 next_curwin_size -= p_wiw - (m - n);
1834 new_size += next_curwin_size;
1835 room -= new_size - next_curwin_size;
1837 else
1838 room -= new_size;
1839 new_size += n;
1842 /* Skip frame that is full width when splitting or closing a
1843 * window, unless equalizing all frames. */
1844 if (!current || dir != 'v' || topfr->fr_parent != NULL
1845 || (new_size != fr->fr_width)
1846 || frame_has_win(fr, next_curwin))
1847 win_equal_rec(next_curwin, current, fr, dir, col, row,
1848 new_size, height);
1849 col += new_size;
1850 width -= new_size;
1851 totwincount -= wincount;
1854 #endif
1855 else /* topfr->fr_layout == FR_COL */
1857 #ifdef FEAT_VERTSPLIT
1858 topfr->fr_width = width;
1859 #endif
1860 topfr->fr_height = height;
1862 if (dir != 'h') /* equalize frame heights */
1864 /* Compute maximum number of windows vertically in this frame. */
1865 n = frame_minheight(topfr, NOWIN);
1866 /* add one for the bottom window if it doesn't have a statusline */
1867 if (row + height == cmdline_row && p_ls == 0)
1868 extra_sep = 1;
1869 else
1870 extra_sep = 0;
1871 totwincount = (n + extra_sep) / (p_wmh + 1);
1872 has_next_curwin = frame_has_win(topfr, next_curwin);
1875 * Compute height for "next_curwin" window and room available for
1876 * other windows.
1877 * "m" is the minimal height when counting p_wh for "next_curwin".
1879 m = frame_minheight(topfr, next_curwin);
1880 room = height - m;
1881 if (room < 0)
1883 /* The room is less then 'winheight', use all space for the
1884 * current window. */
1885 next_curwin_size = p_wh + room;
1886 room = 0;
1888 else
1890 next_curwin_size = -1;
1891 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1893 /* If 'winfixheight' set keep the window height if
1894 * possible.
1895 * Watch out for this window being the next_curwin. */
1896 if (frame_fixed_height(fr))
1898 n = frame_minheight(fr, NOWIN);
1899 new_size = fr->fr_height;
1900 if (frame_has_win(fr, next_curwin))
1902 room += p_wh - p_wmh;
1903 next_curwin_size = 0;
1904 if (new_size < p_wh)
1905 new_size = p_wh;
1907 else
1908 /* These windows don't use up room. */
1909 totwincount -= (n + (fr->fr_next == NULL
1910 ? extra_sep : 0)) / (p_wmh + 1);
1911 room -= new_size - n;
1912 if (room < 0)
1914 new_size += room;
1915 room = 0;
1917 fr->fr_newheight = new_size;
1920 if (next_curwin_size == -1)
1922 if (!has_next_curwin)
1923 next_curwin_size = 0;
1924 else if (totwincount > 1
1925 && (room + (totwincount - 2))
1926 / (totwincount - 1) > p_wh)
1928 /* can make all windows higher than 'winheight',
1929 * spread the room equally. */
1930 next_curwin_size = (room + p_wh
1931 + (totwincount - 1) * p_wmh
1932 + (totwincount - 1)) / totwincount;
1933 room -= next_curwin_size - p_wh;
1935 else
1936 next_curwin_size = p_wh;
1940 if (has_next_curwin)
1941 --totwincount; /* don't count curwin */
1944 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1946 n = m = 0;
1947 wincount = 1;
1948 if (fr->fr_next == NULL)
1949 /* last frame gets all that remains (avoid roundoff error) */
1950 new_size = height;
1951 else if (dir == 'h')
1952 new_size = fr->fr_height;
1953 else if (frame_fixed_height(fr))
1955 new_size = fr->fr_newheight;
1956 wincount = 0; /* doesn't count as a sizeable window */
1958 else
1960 /* Compute the maximum number of windows vert. in "fr". */
1961 n = frame_minheight(fr, NOWIN);
1962 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1963 / (p_wmh + 1);
1964 m = frame_minheight(fr, next_curwin);
1965 if (has_next_curwin)
1966 hnc = frame_has_win(fr, next_curwin);
1967 else
1968 hnc = FALSE;
1969 if (hnc) /* don't count next_curwin */
1970 --wincount;
1971 if (totwincount == 0)
1972 new_size = room;
1973 else
1974 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1975 / totwincount;
1976 if (hnc) /* add next_curwin size */
1978 next_curwin_size -= p_wh - (m - n);
1979 new_size += next_curwin_size;
1980 room -= new_size - next_curwin_size;
1982 else
1983 room -= new_size;
1984 new_size += n;
1986 /* Skip frame that is full width when splitting or closing a
1987 * window, unless equalizing all frames. */
1988 if (!current || dir != 'h' || topfr->fr_parent != NULL
1989 || (new_size != fr->fr_height)
1990 || frame_has_win(fr, next_curwin))
1991 win_equal_rec(next_curwin, current, fr, dir, col, row,
1992 width, new_size);
1993 row += new_size;
1994 height -= new_size;
1995 totwincount -= wincount;
2001 * close all windows for buffer 'buf'
2003 void
2004 close_windows(buf, keep_curwin)
2005 buf_T *buf;
2006 int keep_curwin; /* don't close "curwin" */
2008 win_T *wp;
2009 tabpage_T *tp, *nexttp;
2010 int h = tabline_height();
2012 ++RedrawingDisabled;
2014 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2016 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2018 win_close(wp, FALSE);
2020 /* Start all over, autocommands may change the window layout. */
2021 wp = firstwin;
2023 else
2024 wp = wp->w_next;
2027 /* Also check windows in other tab pages. */
2028 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2030 nexttp = tp->tp_next;
2031 if (tp != curtab)
2032 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2033 if (wp->w_buffer == buf)
2035 win_close_othertab(wp, FALSE, tp);
2037 /* Start all over, the tab page may be closed and
2038 * autocommands may change the window layout. */
2039 nexttp = first_tabpage;
2040 break;
2044 --RedrawingDisabled;
2046 if (h != tabline_height())
2047 shell_new_rows();
2051 * Return TRUE if the current window is the only window that exists.
2052 * Returns FALSE if there is a window, possibly in another tab page.
2054 static int
2055 last_window()
2057 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2061 * Close window "win". Only works for the current tab page.
2062 * If "free_buf" is TRUE related buffer may be unloaded.
2064 * called by :quit, :close, :xit, :wq and findtag()
2066 void
2067 win_close(win, free_buf)
2068 win_T *win;
2069 int free_buf;
2071 win_T *wp;
2072 #ifdef FEAT_AUTOCMD
2073 int other_buffer = FALSE;
2074 #endif
2075 int close_curwin = FALSE;
2076 int dir;
2077 int help_window = FALSE;
2078 tabpage_T *prev_curtab = curtab;
2080 if (last_window())
2082 EMSG(_("E444: Cannot close last window"));
2083 return;
2087 * When closing the last window in a tab page first go to another tab
2088 * page and then close the window and the tab page. This avoids that
2089 * curwin and curtab are not invalid while we are freeing memory, they may
2090 * be used in GUI events.
2092 if (firstwin == lastwin)
2094 goto_tabpage_tp(alt_tabpage());
2095 redraw_tabline = TRUE;
2097 /* Safety check: Autocommands may have closed the window when jumping
2098 * to the other tab page. */
2099 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2101 int h = tabline_height();
2103 win_close_othertab(win, free_buf, prev_curtab);
2104 if (h != tabline_height())
2105 shell_new_rows();
2107 return;
2110 /* When closing the help window, try restoring a snapshot after closing
2111 * the window. Otherwise clear the snapshot, it's now invalid. */
2112 if (win->w_buffer->b_help)
2113 help_window = TRUE;
2114 else
2115 clear_snapshot(curtab);
2117 #ifdef FEAT_AUTOCMD
2118 if (win == curwin)
2121 * Guess which window is going to be the new current window.
2122 * This may change because of the autocommands (sigh).
2124 wp = frame2win(win_altframe(win, NULL));
2127 * Be careful: If autocommands delete the window, return now.
2129 if (wp->w_buffer != curbuf)
2131 other_buffer = TRUE;
2132 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2133 if (!win_valid(win) || last_window())
2134 return;
2136 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2137 if (!win_valid(win) || last_window())
2138 return;
2139 # ifdef FEAT_EVAL
2140 /* autocmds may abort script processing */
2141 if (aborting())
2142 return;
2143 # endif
2145 #endif
2147 #ifdef FEAT_GUI
2148 /* Avoid trouble with scrollbars that are going to be deleted in
2149 * win_free(). */
2150 if (gui.in_use)
2151 out_flush();
2152 #endif
2155 * Close the link to the buffer.
2157 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2159 /* Autocommands may have closed the window already, or closed the only
2160 * other window or moved to another tab page. */
2161 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2162 return;
2164 /* Free the memory used for the window. */
2165 wp = win_free_mem(win, &dir, NULL);
2167 /* Make sure curwin isn't invalid. It can cause severe trouble when
2168 * printing an error message. For win_equal() curbuf needs to be valid
2169 * too. */
2170 if (win == curwin)
2172 curwin = wp;
2173 #ifdef FEAT_QUICKFIX
2174 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2177 * If the cursor goes to the preview or the quickfix window, try
2178 * finding another window to go to.
2180 for (;;)
2182 if (wp->w_next == NULL)
2183 wp = firstwin;
2184 else
2185 wp = wp->w_next;
2186 if (wp == curwin)
2187 break;
2188 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2190 curwin = wp;
2191 break;
2195 #endif
2196 curbuf = curwin->w_buffer;
2197 close_curwin = TRUE;
2199 if (p_ea
2200 #ifdef FEAT_VERTSPLIT
2201 && (*p_ead == 'b' || *p_ead == dir)
2202 #endif
2204 win_equal(curwin, TRUE,
2205 #ifdef FEAT_VERTSPLIT
2207 #else
2209 #endif
2211 else
2212 win_comp_pos();
2213 if (close_curwin)
2215 win_enter_ext(wp, FALSE, TRUE);
2216 #ifdef FEAT_AUTOCMD
2217 if (other_buffer)
2218 /* careful: after this wp and win may be invalid! */
2219 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2220 #endif
2224 * If last window has a status line now and we don't want one,
2225 * remove the status line.
2227 last_status(FALSE);
2229 /* After closing the help window, try restoring the window layout from
2230 * before it was opened. */
2231 if (help_window)
2232 restore_snapshot(close_curwin);
2234 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2235 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2236 if (gui.in_use && !win_hasvertsplit())
2237 gui_init_which_components(NULL);
2238 #endif
2240 redraw_all_later(NOT_VALID);
2244 * Close window "win" in tab page "tp", which is not the current tab page.
2245 * This may be the last window ih that tab page and result in closing the tab,
2246 * thus "tp" may become invalid!
2247 * Caller must check if buffer is hidden and whether the tabline needs to be
2248 * updated.
2250 void
2251 win_close_othertab(win, free_buf, tp)
2252 win_T *win;
2253 int free_buf;
2254 tabpage_T *tp;
2256 win_T *wp;
2257 int dir;
2258 tabpage_T *ptp = NULL;
2260 /* Close the link to the buffer. */
2261 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2263 /* Careful: Autocommands may have closed the tab page or made it the
2264 * current tab page. */
2265 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2267 if (ptp == NULL || tp == curtab)
2268 return;
2270 /* Autocommands may have closed the window already. */
2271 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2273 if (wp == NULL)
2274 return;
2276 /* Free the memory used for the window. */
2277 wp = win_free_mem(win, &dir, tp);
2279 /* When closing the last window in a tab page remove the tab page. */
2280 if (wp == NULL)
2282 if (tp == first_tabpage)
2283 first_tabpage = tp->tp_next;
2284 else
2286 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2287 ptp = ptp->tp_next)
2289 if (ptp == NULL)
2291 EMSG2(_(e_intern2), "win_close_othertab()");
2292 return;
2294 ptp->tp_next = tp->tp_next;
2296 free_tabpage(tp);
2301 * Free the memory used for a window.
2302 * Returns a pointer to the window that got the freed up space.
2304 static win_T *
2305 win_free_mem(win, dirp, tp)
2306 win_T *win;
2307 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2308 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2310 frame_T *frp;
2311 win_T *wp;
2313 #ifdef FEAT_FOLDING
2314 clearFolding(win);
2315 #endif
2317 /* reduce the reference count to the argument list. */
2318 alist_unlink(win->w_alist);
2320 /* Remove the window and its frame from the tree of frames. */
2321 frp = win->w_frame;
2322 wp = winframe_remove(win, dirp, tp);
2323 vim_free(frp);
2324 win_free(win, tp);
2326 /* When deleting the current window of another tab page select a new
2327 * current window. */
2328 if (tp != NULL && win == tp->tp_curwin)
2329 tp->tp_curwin = wp;
2331 return wp;
2334 #if defined(EXITFREE) || defined(PROTO)
2335 void
2336 win_free_all()
2338 int dummy;
2340 # ifdef FEAT_WINDOWS
2341 while (first_tabpage->tp_next != NULL)
2342 tabpage_close(TRUE);
2343 # endif
2345 while (firstwin != NULL)
2346 (void)win_free_mem(firstwin, &dummy, NULL);
2348 #endif
2351 * Remove a window and its frame from the tree of frames.
2352 * Returns a pointer to the window that got the freed up space.
2354 static win_T *
2355 winframe_remove(win, dirp, tp)
2356 win_T *win;
2357 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
2358 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2360 frame_T *frp, *frp2, *frp3;
2361 frame_T *frp_close = win->w_frame;
2362 win_T *wp;
2365 * If there is only one window there is nothing to remove.
2367 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2368 return NULL;
2371 * Remove the window from its frame.
2373 frp2 = win_altframe(win, tp);
2374 wp = frame2win(frp2);
2376 /* Remove this frame from the list of frames. */
2377 frame_remove(frp_close);
2379 #ifdef FEAT_VERTSPLIT
2380 if (frp_close->fr_parent->fr_layout == FR_COL)
2382 #endif
2383 /* When 'winfixheight' is set, try to find another frame in the column
2384 * (as close to the closed frame as possible) to distribute the height
2385 * to. */
2386 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2388 frp = frp_close->fr_prev;
2389 frp3 = frp_close->fr_next;
2390 while (frp != NULL || frp3 != NULL)
2392 if (frp != NULL)
2394 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2396 frp2 = frp;
2397 wp = frp->fr_win;
2398 break;
2400 frp = frp->fr_prev;
2402 if (frp3 != NULL)
2404 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2406 frp2 = frp3;
2407 wp = frp3->fr_win;
2408 break;
2410 frp3 = frp3->fr_next;
2414 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2415 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2416 #ifdef FEAT_VERTSPLIT
2417 *dirp = 'v';
2419 else
2421 /* When 'winfixwidth' is set, try to find another frame in the column
2422 * (as close to the closed frame as possible) to distribute the width
2423 * to. */
2424 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2426 frp = frp_close->fr_prev;
2427 frp3 = frp_close->fr_next;
2428 while (frp != NULL || frp3 != NULL)
2430 if (frp != NULL)
2432 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2434 frp2 = frp;
2435 wp = frp->fr_win;
2436 break;
2438 frp = frp->fr_prev;
2440 if (frp3 != NULL)
2442 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2444 frp2 = frp3;
2445 wp = frp3->fr_win;
2446 break;
2448 frp3 = frp3->fr_next;
2452 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2453 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2454 *dirp = 'h';
2456 #endif
2458 /* If rows/columns go to a window below/right its positions need to be
2459 * updated. Can only be done after the sizes have been updated. */
2460 if (frp2 == frp_close->fr_next)
2462 int row = win->w_winrow;
2463 int col = W_WINCOL(win);
2465 frame_comp_pos(frp2, &row, &col);
2468 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2470 /* There is no other frame in this list, move its info to the parent
2471 * and remove it. */
2472 frp2->fr_parent->fr_layout = frp2->fr_layout;
2473 frp2->fr_parent->fr_child = frp2->fr_child;
2474 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2475 frp->fr_parent = frp2->fr_parent;
2476 frp2->fr_parent->fr_win = frp2->fr_win;
2477 if (frp2->fr_win != NULL)
2478 frp2->fr_win->w_frame = frp2->fr_parent;
2479 frp = frp2->fr_parent;
2480 vim_free(frp2);
2482 frp2 = frp->fr_parent;
2483 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2485 /* The frame above the parent has the same layout, have to merge
2486 * the frames into this list. */
2487 if (frp2->fr_child == frp)
2488 frp2->fr_child = frp->fr_child;
2489 frp->fr_child->fr_prev = frp->fr_prev;
2490 if (frp->fr_prev != NULL)
2491 frp->fr_prev->fr_next = frp->fr_child;
2492 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2494 frp3->fr_parent = frp2;
2495 if (frp3->fr_next == NULL)
2497 frp3->fr_next = frp->fr_next;
2498 if (frp->fr_next != NULL)
2499 frp->fr_next->fr_prev = frp3;
2500 break;
2503 vim_free(frp);
2507 return wp;
2511 * Find out which frame is going to get the freed up space when "win" is
2512 * closed.
2513 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2514 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2515 * This makes opening a window and closing it immediately keep the same window
2516 * layout.
2518 static frame_T *
2519 win_altframe(win, tp)
2520 win_T *win;
2521 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2523 frame_T *frp;
2524 int b;
2526 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2527 /* Last window in this tab page, will go to next tab page. */
2528 return alt_tabpage()->tp_curwin->w_frame;
2530 frp = win->w_frame;
2531 #ifdef FEAT_VERTSPLIT
2532 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2533 b = p_spr;
2534 else
2535 #endif
2536 b = p_sb;
2537 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2538 return frp->fr_next;
2539 return frp->fr_prev;
2543 * Return the tabpage that will be used if the current one is closed.
2545 static tabpage_T *
2546 alt_tabpage()
2548 tabpage_T *tp;
2550 /* Use the next tab page if possible. */
2551 if (curtab->tp_next != NULL)
2552 return curtab->tp_next;
2554 /* Find the last but one tab page. */
2555 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2557 return tp;
2561 * Find the left-upper window in frame "frp".
2563 static win_T *
2564 frame2win(frp)
2565 frame_T *frp;
2567 while (frp->fr_win == NULL)
2568 frp = frp->fr_child;
2569 return frp->fr_win;
2573 * Return TRUE if frame "frp" contains window "wp".
2575 static int
2576 frame_has_win(frp, wp)
2577 frame_T *frp;
2578 win_T *wp;
2580 frame_T *p;
2582 if (frp->fr_layout == FR_LEAF)
2583 return frp->fr_win == wp;
2585 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2586 if (frame_has_win(p, wp))
2587 return TRUE;
2588 return FALSE;
2592 * Set a new height for a frame. Recursively sets the height for contained
2593 * frames and windows. Caller must take care of positions.
2595 static void
2596 frame_new_height(topfrp, height, topfirst, wfh)
2597 frame_T *topfrp;
2598 int height;
2599 int topfirst; /* resize topmost contained frame first */
2600 int wfh; /* obey 'winfixheight' when there is a choice;
2601 may cause the height not to be set */
2603 frame_T *frp;
2604 int extra_lines;
2605 int h;
2607 if (topfrp->fr_win != NULL)
2609 /* Simple case: just one window. */
2610 win_new_height(topfrp->fr_win,
2611 height - topfrp->fr_win->w_status_height);
2613 #ifdef FEAT_VERTSPLIT
2614 else if (topfrp->fr_layout == FR_ROW)
2618 /* All frames in this row get the same new height. */
2619 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2621 frame_new_height(frp, height, topfirst, wfh);
2622 if (frp->fr_height > height)
2624 /* Could not fit the windows, make the whole row higher. */
2625 height = frp->fr_height;
2626 break;
2630 while (frp != NULL);
2632 #endif
2633 else /* fr_layout == FR_COL */
2635 /* Complicated case: Resize a column of frames. Resize the bottom
2636 * frame first, frames above that when needed. */
2638 frp = topfrp->fr_child;
2639 if (wfh)
2640 /* Advance past frames with one window with 'wfh' set. */
2641 while (frame_fixed_height(frp))
2643 frp = frp->fr_next;
2644 if (frp == NULL)
2645 return; /* no frame without 'wfh', give up */
2647 if (!topfirst)
2649 /* Find the bottom frame of this column */
2650 while (frp->fr_next != NULL)
2651 frp = frp->fr_next;
2652 if (wfh)
2653 /* Advance back for frames with one window with 'wfh' set. */
2654 while (frame_fixed_height(frp))
2655 frp = frp->fr_prev;
2658 extra_lines = height - topfrp->fr_height;
2659 if (extra_lines < 0)
2661 /* reduce height of contained frames, bottom or top frame first */
2662 while (frp != NULL)
2664 h = frame_minheight(frp, NULL);
2665 if (frp->fr_height + extra_lines < h)
2667 extra_lines += frp->fr_height - h;
2668 frame_new_height(frp, h, topfirst, wfh);
2670 else
2672 frame_new_height(frp, frp->fr_height + extra_lines,
2673 topfirst, wfh);
2674 break;
2676 if (topfirst)
2679 frp = frp->fr_next;
2680 while (wfh && frp != NULL && frame_fixed_height(frp));
2682 else
2685 frp = frp->fr_prev;
2686 while (wfh && frp != NULL && frame_fixed_height(frp));
2688 /* Increase "height" if we could not reduce enough frames. */
2689 if (frp == NULL)
2690 height -= extra_lines;
2693 else if (extra_lines > 0)
2695 /* increase height of bottom or top frame */
2696 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2699 topfrp->fr_height = height;
2703 * Return TRUE if height of frame "frp" should not be changed because of
2704 * the 'winfixheight' option.
2706 static int
2707 frame_fixed_height(frp)
2708 frame_T *frp;
2710 /* frame with one window: fixed height if 'winfixheight' set. */
2711 if (frp->fr_win != NULL)
2712 return frp->fr_win->w_p_wfh;
2714 if (frp->fr_layout == FR_ROW)
2716 /* The frame is fixed height if one of the frames in the row is fixed
2717 * height. */
2718 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2719 if (frame_fixed_height(frp))
2720 return TRUE;
2721 return FALSE;
2724 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2725 * frames in the row are fixed height. */
2726 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2727 if (!frame_fixed_height(frp))
2728 return FALSE;
2729 return TRUE;
2732 #ifdef FEAT_VERTSPLIT
2734 * Return TRUE if width of frame "frp" should not be changed because of
2735 * the 'winfixwidth' option.
2737 static int
2738 frame_fixed_width(frp)
2739 frame_T *frp;
2741 /* frame with one window: fixed width if 'winfixwidth' set. */
2742 if (frp->fr_win != NULL)
2743 return frp->fr_win->w_p_wfw;
2745 if (frp->fr_layout == FR_COL)
2747 /* The frame is fixed width if one of the frames in the row is fixed
2748 * width. */
2749 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2750 if (frame_fixed_width(frp))
2751 return TRUE;
2752 return FALSE;
2755 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2756 * frames in the row are fixed width. */
2757 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2758 if (!frame_fixed_width(frp))
2759 return FALSE;
2760 return TRUE;
2764 * Add a status line to windows at the bottom of "frp".
2765 * Note: Does not check if there is room!
2767 static void
2768 frame_add_statusline(frp)
2769 frame_T *frp;
2771 win_T *wp;
2773 if (frp->fr_layout == FR_LEAF)
2775 wp = frp->fr_win;
2776 if (wp->w_status_height == 0)
2778 if (wp->w_height > 0) /* don't make it negative */
2779 --wp->w_height;
2780 wp->w_status_height = STATUS_HEIGHT;
2783 else if (frp->fr_layout == FR_ROW)
2785 /* Handle all the frames in the row. */
2786 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2787 frame_add_statusline(frp);
2789 else /* frp->fr_layout == FR_COL */
2791 /* Only need to handle the last frame in the column. */
2792 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2794 frame_add_statusline(frp);
2799 * Set width of a frame. Handles recursively going through contained frames.
2800 * May remove separator line for windows at the right side (for win_close()).
2802 static void
2803 frame_new_width(topfrp, width, leftfirst, wfw)
2804 frame_T *topfrp;
2805 int width;
2806 int leftfirst; /* resize leftmost contained frame first */
2807 int wfw; /* obey 'winfixwidth' when there is a choice;
2808 may cause the width not to be set */
2810 frame_T *frp;
2811 int extra_cols;
2812 int w;
2813 win_T *wp;
2815 if (topfrp->fr_layout == FR_LEAF)
2817 /* Simple case: just one window. */
2818 wp = topfrp->fr_win;
2819 /* Find out if there are any windows right of this one. */
2820 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2821 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2822 break;
2823 if (frp->fr_parent == NULL)
2824 wp->w_vsep_width = 0;
2825 win_new_width(wp, width - wp->w_vsep_width);
2827 else if (topfrp->fr_layout == FR_COL)
2831 /* All frames in this column get the same new width. */
2832 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2834 frame_new_width(frp, width, leftfirst, wfw);
2835 if (frp->fr_width > width)
2837 /* Could not fit the windows, make whole column wider. */
2838 width = frp->fr_width;
2839 break;
2842 } while (frp != NULL);
2844 else /* fr_layout == FR_ROW */
2846 /* Complicated case: Resize a row of frames. Resize the rightmost
2847 * frame first, frames left of it when needed. */
2849 frp = topfrp->fr_child;
2850 if (wfw)
2851 /* Advance past frames with one window with 'wfw' set. */
2852 while (frame_fixed_width(frp))
2854 frp = frp->fr_next;
2855 if (frp == NULL)
2856 return; /* no frame without 'wfw', give up */
2858 if (!leftfirst)
2860 /* Find the rightmost frame of this row */
2861 while (frp->fr_next != NULL)
2862 frp = frp->fr_next;
2863 if (wfw)
2864 /* Advance back for frames with one window with 'wfw' set. */
2865 while (frame_fixed_width(frp))
2866 frp = frp->fr_prev;
2869 extra_cols = width - topfrp->fr_width;
2870 if (extra_cols < 0)
2872 /* reduce frame width, rightmost frame first */
2873 while (frp != NULL)
2875 w = frame_minwidth(frp, NULL);
2876 if (frp->fr_width + extra_cols < w)
2878 extra_cols += frp->fr_width - w;
2879 frame_new_width(frp, w, leftfirst, wfw);
2881 else
2883 frame_new_width(frp, frp->fr_width + extra_cols,
2884 leftfirst, wfw);
2885 break;
2887 if (leftfirst)
2890 frp = frp->fr_next;
2891 while (wfw && frp != NULL && frame_fixed_width(frp));
2893 else
2896 frp = frp->fr_prev;
2897 while (wfw && frp != NULL && frame_fixed_width(frp));
2899 /* Increase "width" if we could not reduce enough frames. */
2900 if (frp == NULL)
2901 width -= extra_cols;
2904 else if (extra_cols > 0)
2906 /* increase width of rightmost frame */
2907 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2910 topfrp->fr_width = width;
2914 * Add the vertical separator to windows at the right side of "frp".
2915 * Note: Does not check if there is room!
2917 static void
2918 frame_add_vsep(frp)
2919 frame_T *frp;
2921 win_T *wp;
2923 if (frp->fr_layout == FR_LEAF)
2925 wp = frp->fr_win;
2926 if (wp->w_vsep_width == 0)
2928 if (wp->w_width > 0) /* don't make it negative */
2929 --wp->w_width;
2930 wp->w_vsep_width = 1;
2933 else if (frp->fr_layout == FR_COL)
2935 /* Handle all the frames in the column. */
2936 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2937 frame_add_vsep(frp);
2939 else /* frp->fr_layout == FR_ROW */
2941 /* Only need to handle the last frame in the row. */
2942 frp = frp->fr_child;
2943 while (frp->fr_next != NULL)
2944 frp = frp->fr_next;
2945 frame_add_vsep(frp);
2950 * Set frame width from the window it contains.
2952 static void
2953 frame_fix_width(wp)
2954 win_T *wp;
2956 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2958 #endif
2961 * Set frame height from the window it contains.
2963 static void
2964 frame_fix_height(wp)
2965 win_T *wp;
2967 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2971 * Compute the minimal height for frame "topfrp".
2972 * Uses the 'winminheight' option.
2973 * When "next_curwin" isn't NULL, use p_wh for this window.
2974 * When "next_curwin" is NOWIN, don't use at least one line for the current
2975 * window.
2977 static int
2978 frame_minheight(topfrp, next_curwin)
2979 frame_T *topfrp;
2980 win_T *next_curwin;
2982 frame_T *frp;
2983 int m;
2984 #ifdef FEAT_VERTSPLIT
2985 int n;
2986 #endif
2988 if (topfrp->fr_win != NULL)
2990 if (topfrp->fr_win == next_curwin)
2991 m = p_wh + topfrp->fr_win->w_status_height;
2992 else
2994 /* window: minimal height of the window plus status line */
2995 m = p_wmh + topfrp->fr_win->w_status_height;
2996 /* Current window is minimal one line high */
2997 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2998 ++m;
3001 #ifdef FEAT_VERTSPLIT
3002 else if (topfrp->fr_layout == FR_ROW)
3004 /* get the minimal height from each frame in this row */
3005 m = 0;
3006 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3008 n = frame_minheight(frp, next_curwin);
3009 if (n > m)
3010 m = n;
3013 #endif
3014 else
3016 /* Add up the minimal heights for all frames in this column. */
3017 m = 0;
3018 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3019 m += frame_minheight(frp, next_curwin);
3022 return m;
3025 #ifdef FEAT_VERTSPLIT
3027 * Compute the minimal width for frame "topfrp".
3028 * When "next_curwin" isn't NULL, use p_wiw for this window.
3029 * When "next_curwin" is NOWIN, don't use at least one column for the current
3030 * window.
3032 static int
3033 frame_minwidth(topfrp, next_curwin)
3034 frame_T *topfrp;
3035 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3037 frame_T *frp;
3038 int m, n;
3040 if (topfrp->fr_win != NULL)
3042 if (topfrp->fr_win == next_curwin)
3043 m = p_wiw + topfrp->fr_win->w_vsep_width;
3044 else
3046 /* window: minimal width of the window plus separator column */
3047 m = p_wmw + topfrp->fr_win->w_vsep_width;
3048 /* Current window is minimal one column wide */
3049 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3050 ++m;
3053 else if (topfrp->fr_layout == FR_COL)
3055 /* get the minimal width from each frame in this column */
3056 m = 0;
3057 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3059 n = frame_minwidth(frp, next_curwin);
3060 if (n > m)
3061 m = n;
3064 else
3066 /* Add up the minimal widths for all frames in this row. */
3067 m = 0;
3068 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3069 m += frame_minwidth(frp, next_curwin);
3072 return m;
3074 #endif
3078 * Try to close all windows except current one.
3079 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3080 * used and the buffer was modified.
3082 * Used by ":bdel" and ":only".
3084 void
3085 close_others(message, forceit)
3086 int message;
3087 int forceit; /* always hide all other windows */
3089 win_T *wp;
3090 win_T *nextwp;
3091 int r;
3093 if (lastwin == firstwin)
3095 if (message
3096 #ifdef FEAT_AUTOCMD
3097 && !autocmd_busy
3098 #endif
3100 MSG(_(m_onlyone));
3101 return;
3104 /* Be very careful here: autocommands may change the window layout. */
3105 for (wp = firstwin; win_valid(wp); wp = nextwp)
3107 nextwp = wp->w_next;
3108 if (wp != curwin) /* don't close current window */
3111 /* Check if it's allowed to abandon this window */
3112 r = can_abandon(wp->w_buffer, forceit);
3113 #ifdef FEAT_AUTOCMD
3114 if (!win_valid(wp)) /* autocommands messed wp up */
3116 nextwp = firstwin;
3117 continue;
3119 #endif
3120 if (!r)
3122 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3123 if (message && (p_confirm || cmdmod.confirm) && p_write)
3125 dialog_changed(wp->w_buffer, FALSE);
3126 # ifdef FEAT_AUTOCMD
3127 if (!win_valid(wp)) /* autocommands messed wp up */
3129 nextwp = firstwin;
3130 continue;
3132 # endif
3134 if (bufIsChanged(wp->w_buffer))
3135 #endif
3136 continue;
3138 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3142 if (message && lastwin != firstwin)
3143 EMSG(_("E445: Other window contains changes"));
3146 #endif /* FEAT_WINDOWS */
3149 * Init the current window "curwin".
3150 * Called when a new file is being edited.
3152 void
3153 curwin_init()
3155 redraw_win_later(curwin, NOT_VALID);
3156 curwin->w_lines_valid = 0;
3157 curwin->w_cursor.lnum = 1;
3158 curwin->w_curswant = curwin->w_cursor.col = 0;
3159 #ifdef FEAT_VIRTUALEDIT
3160 curwin->w_cursor.coladd = 0;
3161 #endif
3162 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3163 curwin->w_pcmark.col = 0;
3164 curwin->w_prev_pcmark.lnum = 0;
3165 curwin->w_prev_pcmark.col = 0;
3166 curwin->w_topline = 1;
3167 #ifdef FEAT_DIFF
3168 curwin->w_topfill = 0;
3169 #endif
3170 curwin->w_botline = 2;
3171 #ifdef FEAT_FKMAP
3172 if (curwin->w_p_rl)
3173 curwin->w_farsi = W_CONV + W_R_L;
3174 else
3175 curwin->w_farsi = W_CONV;
3176 #endif
3180 * Allocate the first window and put an empty buffer in it.
3181 * Called from main().
3182 * Return FAIL when something goes wrong (out of memory).
3185 win_alloc_first()
3187 if (win_alloc_firstwin(NULL) == FAIL)
3188 return FAIL;
3190 #ifdef FEAT_WINDOWS
3191 first_tabpage = alloc_tabpage();
3192 if (first_tabpage == NULL)
3193 return FAIL;
3194 first_tabpage->tp_topframe = topframe;
3195 curtab = first_tabpage;
3196 #endif
3197 return OK;
3201 * Allocate the first window or the first window in a new tab page.
3202 * When "oldwin" is NULL create an empty buffer for it.
3203 * When "oldwin" is not NULL copy info from it to the new window (only with
3204 * FEAT_WINDOWS).
3205 * Return FAIL when something goes wrong (out of memory).
3207 static int
3208 win_alloc_firstwin(oldwin)
3209 win_T *oldwin;
3211 curwin = win_alloc(NULL);
3212 if (oldwin == NULL)
3214 /* Very first window, need to create an empty buffer for it and
3215 * initialize from scratch. */
3216 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3217 if (curwin == NULL || curbuf == NULL)
3218 return FAIL;
3219 curwin->w_buffer = curbuf;
3220 curbuf->b_nwindows = 1; /* there is one window */
3221 #ifdef FEAT_WINDOWS
3222 curwin->w_alist = &global_alist;
3223 #endif
3224 curwin_init(); /* init current window */
3226 #ifdef FEAT_WINDOWS
3227 else
3229 /* First window in new tab page, initialize it from "oldwin". */
3230 win_init(curwin, oldwin, 0);
3232 # ifdef FEAT_SCROLLBIND
3233 /* We don't want scroll-binding in the first window. */
3234 curwin->w_p_scb = FALSE;
3235 # endif
3237 #endif
3239 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3240 if (topframe == NULL)
3241 return FAIL;
3242 topframe->fr_layout = FR_LEAF;
3243 #ifdef FEAT_VERTSPLIT
3244 topframe->fr_width = Columns;
3245 #endif
3246 topframe->fr_height = Rows - p_ch;
3247 topframe->fr_win = curwin;
3248 curwin->w_frame = topframe;
3250 return OK;
3254 * Initialize the window and frame size to the maximum.
3256 void
3257 win_init_size()
3259 firstwin->w_height = ROWS_AVAIL;
3260 topframe->fr_height = ROWS_AVAIL;
3261 #ifdef FEAT_VERTSPLIT
3262 firstwin->w_width = Columns;
3263 topframe->fr_width = Columns;
3264 #endif
3267 #if defined(FEAT_WINDOWS) || defined(PROTO)
3270 * Allocate a new tabpage_T and init the values.
3271 * Returns NULL when out of memory.
3273 static tabpage_T *
3274 alloc_tabpage()
3276 tabpage_T *tp;
3278 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3279 if (tp != NULL)
3281 # ifdef FEAT_GUI
3282 int i;
3284 for (i = 0; i < 3; i++)
3285 tp->tp_prev_which_scrollbars[i] = -1;
3286 # endif
3287 # ifdef FEAT_DIFF
3288 tp->tp_diff_invalid = TRUE;
3289 # endif
3290 #ifdef FEAT_EVAL
3291 /* init t: variables */
3292 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3293 #endif
3294 tp->tp_ch_used = p_ch;
3296 return tp;
3299 void
3300 free_tabpage(tp)
3301 tabpage_T *tp;
3303 # ifdef FEAT_DIFF
3304 diff_clear(tp);
3305 # endif
3306 clear_snapshot(tp);
3307 #ifdef FEAT_EVAL
3308 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3309 #endif
3310 vim_free(tp);
3314 * Create a new Tab page with one window.
3315 * It will edit the current buffer, like after ":split".
3316 * When "after" is 0 put it just after the current Tab page.
3317 * Otherwise put it just before tab page "after".
3318 * Return FAIL or OK.
3321 win_new_tabpage(after)
3322 int after;
3324 tabpage_T *tp = curtab;
3325 tabpage_T *newtp;
3326 int n;
3328 newtp = alloc_tabpage();
3329 if (newtp == NULL)
3330 return FAIL;
3332 /* Remember the current windows in this Tab page. */
3333 if (leave_tabpage(curbuf) == FAIL)
3335 vim_free(newtp);
3336 return FAIL;
3338 curtab = newtp;
3340 /* Create a new empty window. */
3341 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3343 /* Make the new Tab page the new topframe. */
3344 if (after == 1)
3346 /* New tab page becomes the first one. */
3347 newtp->tp_next = first_tabpage;
3348 first_tabpage = newtp;
3350 else
3352 if (after > 0)
3354 /* Put new tab page before tab page "after". */
3355 n = 2;
3356 for (tp = first_tabpage; tp->tp_next != NULL
3357 && n < after; tp = tp->tp_next)
3358 ++n;
3360 newtp->tp_next = tp->tp_next;
3361 tp->tp_next = newtp;
3363 win_init_size();
3364 firstwin->w_winrow = tabline_height();
3365 win_comp_scroll(curwin);
3367 newtp->tp_topframe = topframe;
3368 last_status(FALSE);
3370 #if defined(FEAT_GUI)
3371 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3372 * scrollbars. Have to update them anyway. */
3373 if (gui.in_use && starting == 0)
3375 gui_init_which_components(NULL);
3376 gui_update_scrollbars(TRUE);
3378 need_mouse_correct = TRUE;
3379 #endif
3381 redraw_all_later(CLEAR);
3382 #ifdef FEAT_AUTOCMD
3383 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3384 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3385 #endif
3386 return OK;
3389 /* Failed, get back the previous Tab page */
3390 enter_tabpage(curtab, curbuf);
3391 return FAIL;
3395 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3396 * like with ":split".
3397 * Returns OK if a new tab page was created, FAIL otherwise.
3400 may_open_tabpage()
3402 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3404 if (n != 0)
3406 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3407 postponed_split_tab = 0;
3408 return win_new_tabpage(n);
3410 return FAIL;
3414 * Create up to "maxcount" tabpages with empty windows.
3415 * Returns the number of resulting tab pages.
3418 make_tabpages(maxcount)
3419 int maxcount;
3421 int count = maxcount;
3422 int todo;
3424 /* Limit to 'tabpagemax' tabs. */
3425 if (count > p_tpm)
3426 count = p_tpm;
3428 #ifdef FEAT_AUTOCMD
3430 * Don't execute autocommands while creating the tab pages. Must do that
3431 * when putting the buffers in the windows.
3433 block_autocmds();
3434 #endif
3436 for (todo = count - 1; todo > 0; --todo)
3437 if (win_new_tabpage(0) == FAIL)
3438 break;
3440 #ifdef FEAT_AUTOCMD
3441 unblock_autocmds();
3442 #endif
3444 /* return actual number of tab pages */
3445 return (count - todo);
3449 * Return TRUE when "tpc" points to a valid tab page.
3452 valid_tabpage(tpc)
3453 tabpage_T *tpc;
3455 tabpage_T *tp;
3457 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3458 if (tp == tpc)
3459 return TRUE;
3460 return FALSE;
3464 * Find tab page "n" (first one is 1). Returns NULL when not found.
3466 tabpage_T *
3467 find_tabpage(n)
3468 int n;
3470 tabpage_T *tp;
3471 int i = 1;
3473 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3474 ++i;
3475 return tp;
3479 * Get index of tab page "tp". First one has index 1.
3480 * When not found returns number of tab pages plus one.
3483 tabpage_index(ftp)
3484 tabpage_T *ftp;
3486 int i = 1;
3487 tabpage_T *tp;
3489 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3490 ++i;
3491 return i;
3495 * Prepare for leaving the current tab page.
3496 * When autocomands change "curtab" we don't leave the tab page and return
3497 * FAIL.
3498 * Careful: When OK is returned need to get a new tab page very very soon!
3500 static int
3501 leave_tabpage(new_curbuf)
3502 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
3503 NULL if unknown */
3505 tabpage_T *tp = curtab;
3507 #ifdef FEAT_VISUAL
3508 reset_VIsual_and_resel(); /* stop Visual mode */
3509 #endif
3510 #ifdef FEAT_AUTOCMD
3511 if (new_curbuf != curbuf)
3513 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3514 if (curtab != tp)
3515 return FAIL;
3517 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3518 if (curtab != tp)
3519 return FAIL;
3520 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3521 if (curtab != tp)
3522 return FAIL;
3523 #endif
3524 #if defined(FEAT_GUI)
3525 /* Remove the scrollbars. They may be added back later. */
3526 if (gui.in_use)
3527 gui_remove_scrollbars();
3528 #endif
3529 tp->tp_curwin = curwin;
3530 tp->tp_prevwin = prevwin;
3531 tp->tp_firstwin = firstwin;
3532 tp->tp_lastwin = lastwin;
3533 tp->tp_old_Rows = Rows;
3534 tp->tp_old_Columns = Columns;
3535 firstwin = NULL;
3536 lastwin = NULL;
3537 return OK;
3541 * Start using tab page "tp".
3542 * Only to be used after leave_tabpage() or freeing the current tab page.
3544 static void
3545 enter_tabpage(tp, old_curbuf)
3546 tabpage_T *tp;
3547 buf_T *old_curbuf UNUSED;
3549 int old_off = tp->tp_firstwin->w_winrow;
3550 win_T *next_prevwin = tp->tp_prevwin;
3552 curtab = tp;
3553 firstwin = tp->tp_firstwin;
3554 lastwin = tp->tp_lastwin;
3555 topframe = tp->tp_topframe;
3557 /* We would like doing the TabEnter event first, but we don't have a
3558 * valid current window yet, which may break some commands.
3559 * This triggers autocommands, thus may make "tp" invalid. */
3560 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3561 prevwin = next_prevwin;
3563 #ifdef FEAT_AUTOCMD
3564 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3566 if (old_curbuf != curbuf)
3567 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3568 #endif
3570 last_status(FALSE); /* status line may appear or disappear */
3571 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3572 must_redraw = CLEAR; /* need to redraw everything */
3573 #ifdef FEAT_DIFF
3574 diff_need_scrollbind = TRUE;
3575 #endif
3577 /* The tabpage line may have appeared or disappeared, may need to resize
3578 * the frames for that. When the Vim window was resized need to update
3579 * frame sizes too. Use the stored value of p_ch, so that it can be
3580 * different for each tab page. */
3581 p_ch = curtab->tp_ch_used;
3582 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3583 #ifdef FEAT_GUI_TABLINE
3584 && !gui_use_tabline()
3585 #endif
3587 shell_new_rows();
3588 #ifdef FEAT_VERTSPLIT
3589 if (curtab->tp_old_Columns != Columns && starting == 0)
3590 shell_new_columns(); /* update window widths */
3591 #endif
3593 #if defined(FEAT_GUI)
3594 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3595 * scrollbars. Have to update them anyway. */
3596 if (gui.in_use && starting == 0)
3598 gui_init_which_components(NULL);
3599 gui_update_scrollbars(TRUE);
3601 need_mouse_correct = TRUE;
3602 #endif
3604 redraw_all_later(CLEAR);
3608 * Go to tab page "n". For ":tab N" and "Ngt".
3609 * When "n" is 9999 go to the last tab page.
3611 void
3612 goto_tabpage(n)
3613 int n;
3615 tabpage_T *tp;
3616 tabpage_T *ttp;
3617 int i;
3619 if (text_locked())
3621 /* Not allowed when editing the command line. */
3622 #ifdef FEAT_CMDWIN
3623 if (cmdwin_type != 0)
3624 EMSG(_(e_cmdwin));
3625 else
3626 #endif
3627 EMSG(_(e_secure));
3628 return;
3631 /* If there is only one it can't work. */
3632 if (first_tabpage->tp_next == NULL)
3634 if (n > 1)
3635 beep_flush();
3636 return;
3639 if (n == 0)
3641 /* No count, go to next tab page, wrap around end. */
3642 if (curtab->tp_next == NULL)
3643 tp = first_tabpage;
3644 else
3645 tp = curtab->tp_next;
3647 else if (n < 0)
3649 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3650 * this N times. */
3651 ttp = curtab;
3652 for (i = n; i < 0; ++i)
3654 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3655 tp = tp->tp_next)
3657 ttp = tp;
3660 else if (n == 9999)
3662 /* Go to last tab page. */
3663 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3666 else
3668 /* Go to tab page "n". */
3669 tp = find_tabpage(n);
3670 if (tp == NULL)
3672 beep_flush();
3673 return;
3677 goto_tabpage_tp(tp);
3679 #ifdef FEAT_GUI_TABLINE
3680 if (gui_use_tabline())
3681 gui_mch_set_curtab(tabpage_index(curtab));
3682 #endif
3686 * Go to tabpage "tp".
3687 * Note: doesn't update the GUI tab.
3689 void
3690 goto_tabpage_tp(tp)
3691 tabpage_T *tp;
3693 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3695 if (valid_tabpage(tp))
3696 enter_tabpage(tp, curbuf);
3697 else
3698 enter_tabpage(curtab, curbuf);
3703 * Enter window "wp" in tab page "tp".
3704 * Also updates the GUI tab.
3706 void
3707 goto_tabpage_win(tp, wp)
3708 tabpage_T *tp;
3709 win_T *wp;
3711 goto_tabpage_tp(tp);
3712 if (curtab == tp && win_valid(wp))
3714 win_enter(wp, TRUE);
3715 # ifdef FEAT_GUI_TABLINE
3716 if (gui_use_tabline())
3717 gui_mch_set_curtab(tabpage_index(curtab));
3718 # endif
3723 * Move the current tab page to before tab page "nr".
3725 void
3726 tabpage_move(nr)
3727 int nr;
3729 int n = nr;
3730 tabpage_T *tp;
3732 if (first_tabpage->tp_next == NULL)
3733 return;
3735 /* Remove the current tab page from the list of tab pages. */
3736 if (curtab == first_tabpage)
3737 first_tabpage = curtab->tp_next;
3738 else
3740 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3741 if (tp->tp_next == curtab)
3742 break;
3743 if (tp == NULL) /* "cannot happen" */
3744 return;
3745 tp->tp_next = curtab->tp_next;
3748 /* Re-insert it at the specified position. */
3749 if (n == 0)
3751 curtab->tp_next = first_tabpage;
3752 first_tabpage = curtab;
3754 else
3756 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3757 --n;
3758 curtab->tp_next = tp->tp_next;
3759 tp->tp_next = curtab;
3762 /* Need to redraw the tabline. Tab page contents doesn't change. */
3763 redraw_tabline = TRUE;
3768 * Go to another window.
3769 * When jumping to another buffer, stop Visual mode. Do this before
3770 * changing windows so we can yank the selection into the '*' register.
3771 * When jumping to another window on the same buffer, adjust its cursor
3772 * position to keep the same Visual area.
3774 void
3775 win_goto(wp)
3776 win_T *wp;
3778 if (text_locked())
3780 beep_flush();
3781 text_locked_msg();
3782 return;
3784 #ifdef FEAT_AUTOCMD
3785 if (curbuf_locked())
3786 return;
3787 #endif
3789 #ifdef FEAT_VISUAL
3790 if (wp->w_buffer != curbuf)
3791 reset_VIsual_and_resel();
3792 else if (VIsual_active)
3793 wp->w_cursor = curwin->w_cursor;
3794 #endif
3796 #ifdef FEAT_GUI
3797 need_mouse_correct = TRUE;
3798 #endif
3799 win_enter(wp, TRUE);
3802 #if defined(FEAT_PERL) || defined(PROTO)
3804 * Find window number "winnr" (counting top to bottom).
3806 win_T *
3807 win_find_nr(winnr)
3808 int winnr;
3810 win_T *wp;
3812 # ifdef FEAT_WINDOWS
3813 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3814 if (--winnr == 0)
3815 break;
3816 return wp;
3817 # else
3818 return curwin;
3819 # endif
3821 #endif
3823 #ifdef FEAT_VERTSPLIT
3825 * Move to window above or below "count" times.
3827 static void
3828 win_goto_ver(up, count)
3829 int up; /* TRUE to go to win above */
3830 long count;
3832 frame_T *fr;
3833 frame_T *nfr;
3834 frame_T *foundfr;
3836 foundfr = curwin->w_frame;
3837 while (count--)
3840 * First go upwards in the tree of frames until we find a upwards or
3841 * downwards neighbor.
3843 fr = foundfr;
3844 for (;;)
3846 if (fr == topframe)
3847 goto end;
3848 if (up)
3849 nfr = fr->fr_prev;
3850 else
3851 nfr = fr->fr_next;
3852 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3853 break;
3854 fr = fr->fr_parent;
3858 * Now go downwards to find the bottom or top frame in it.
3860 for (;;)
3862 if (nfr->fr_layout == FR_LEAF)
3864 foundfr = nfr;
3865 break;
3867 fr = nfr->fr_child;
3868 if (nfr->fr_layout == FR_ROW)
3870 /* Find the frame at the cursor row. */
3871 while (fr->fr_next != NULL
3872 && frame2win(fr)->w_wincol + fr->fr_width
3873 <= curwin->w_wincol + curwin->w_wcol)
3874 fr = fr->fr_next;
3876 if (nfr->fr_layout == FR_COL && up)
3877 while (fr->fr_next != NULL)
3878 fr = fr->fr_next;
3879 nfr = fr;
3882 end:
3883 if (foundfr != NULL)
3884 win_goto(foundfr->fr_win);
3888 * Move to left or right window.
3890 static void
3891 win_goto_hor(left, count)
3892 int left; /* TRUE to go to left win */
3893 long count;
3895 frame_T *fr;
3896 frame_T *nfr;
3897 frame_T *foundfr;
3899 foundfr = curwin->w_frame;
3900 while (count--)
3903 * First go upwards in the tree of frames until we find a left or
3904 * right neighbor.
3906 fr = foundfr;
3907 for (;;)
3909 if (fr == topframe)
3910 goto end;
3911 if (left)
3912 nfr = fr->fr_prev;
3913 else
3914 nfr = fr->fr_next;
3915 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3916 break;
3917 fr = fr->fr_parent;
3921 * Now go downwards to find the leftmost or rightmost frame in it.
3923 for (;;)
3925 if (nfr->fr_layout == FR_LEAF)
3927 foundfr = nfr;
3928 break;
3930 fr = nfr->fr_child;
3931 if (nfr->fr_layout == FR_COL)
3933 /* Find the frame at the cursor row. */
3934 while (fr->fr_next != NULL
3935 && frame2win(fr)->w_winrow + fr->fr_height
3936 <= curwin->w_winrow + curwin->w_wrow)
3937 fr = fr->fr_next;
3939 if (nfr->fr_layout == FR_ROW && left)
3940 while (fr->fr_next != NULL)
3941 fr = fr->fr_next;
3942 nfr = fr;
3945 end:
3946 if (foundfr != NULL)
3947 win_goto(foundfr->fr_win);
3949 #endif
3952 * Make window "wp" the current window.
3954 void
3955 win_enter(wp, undo_sync)
3956 win_T *wp;
3957 int undo_sync;
3959 win_enter_ext(wp, undo_sync, FALSE);
3963 * Make window wp the current window.
3964 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3965 * been closed and isn't valid.
3967 static void
3968 win_enter_ext(wp, undo_sync, curwin_invalid)
3969 win_T *wp;
3970 int undo_sync;
3971 int curwin_invalid;
3973 #ifdef FEAT_AUTOCMD
3974 int other_buffer = FALSE;
3975 #endif
3977 if (wp == curwin && !curwin_invalid) /* nothing to do */
3978 return;
3980 #ifdef FEAT_AUTOCMD
3981 if (!curwin_invalid)
3984 * Be careful: If autocommands delete the window, return now.
3986 if (wp->w_buffer != curbuf)
3988 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3989 other_buffer = TRUE;
3990 if (!win_valid(wp))
3991 return;
3993 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3994 if (!win_valid(wp))
3995 return;
3996 # ifdef FEAT_EVAL
3997 /* autocmds may abort script processing */
3998 if (aborting())
3999 return;
4000 # endif
4002 #endif
4004 /* sync undo before leaving the current buffer */
4005 if (undo_sync && curbuf != wp->w_buffer)
4006 u_sync(FALSE);
4007 /* may have to copy the buffer options when 'cpo' contains 'S' */
4008 if (wp->w_buffer != curbuf)
4009 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4010 if (!curwin_invalid)
4012 prevwin = curwin; /* remember for CTRL-W p */
4013 curwin->w_redr_status = TRUE;
4015 curwin = wp;
4016 curbuf = wp->w_buffer;
4017 check_cursor();
4018 #ifdef FEAT_VIRTUALEDIT
4019 if (!virtual_active())
4020 curwin->w_cursor.coladd = 0;
4021 #endif
4022 changed_line_abv_curs(); /* assume cursor position needs updating */
4024 if (curwin->w_localdir != NULL)
4026 /* Window has a local directory: Save current directory as global
4027 * directory (unless that was done already) and change to the local
4028 * directory. */
4029 if (globaldir == NULL)
4031 char_u cwd[MAXPATHL];
4033 if (mch_dirname(cwd, MAXPATHL) == OK)
4034 globaldir = vim_strsave(cwd);
4036 if (mch_chdir((char *)curwin->w_localdir) == 0)
4037 shorten_fnames(TRUE);
4039 else if (globaldir != NULL)
4041 /* Window doesn't have a local directory and we are not in the global
4042 * directory: Change to the global directory. */
4043 ignored = mch_chdir((char *)globaldir);
4044 vim_free(globaldir);
4045 globaldir = NULL;
4046 shorten_fnames(TRUE);
4049 #ifdef FEAT_AUTOCMD
4050 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4051 if (other_buffer)
4052 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4053 #endif
4055 #ifdef FEAT_TITLE
4056 maketitle();
4057 #endif
4058 curwin->w_redr_status = TRUE;
4059 redraw_tabline = TRUE;
4060 if (restart_edit)
4061 redraw_later(VALID); /* causes status line redraw */
4063 /* set window height to desired minimal value */
4064 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4065 win_setheight((int)p_wh);
4066 else if (curwin->w_height == 0)
4067 win_setheight(1);
4069 #ifdef FEAT_VERTSPLIT
4070 /* set window width to desired minimal value */
4071 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4072 win_setwidth((int)p_wiw);
4073 #endif
4075 #ifdef FEAT_MOUSE
4076 setmouse(); /* in case jumped to/from help buffer */
4077 #endif
4079 /* Change directories when the 'acd' option is set. */
4080 DO_AUTOCHDIR
4083 #endif /* FEAT_WINDOWS */
4085 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4087 * Jump to the first open window that contains buffer "buf", if one exists.
4088 * Returns a pointer to the window found, otherwise NULL.
4090 win_T *
4091 buf_jump_open_win(buf)
4092 buf_T *buf;
4094 # ifdef FEAT_WINDOWS
4095 win_T *wp;
4097 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4098 if (wp->w_buffer == buf)
4099 break;
4100 if (wp != NULL)
4101 win_enter(wp, FALSE);
4102 return wp;
4103 # else
4104 if (curwin->w_buffer == buf)
4105 return curwin;
4106 return NULL;
4107 # endif
4111 * Jump to the first open window in any tab page that contains buffer "buf",
4112 * if one exists.
4113 * Returns a pointer to the window found, otherwise NULL.
4115 win_T *
4116 buf_jump_open_tab(buf)
4117 buf_T *buf;
4119 # ifdef FEAT_WINDOWS
4120 win_T *wp;
4121 tabpage_T *tp;
4123 /* First try the current tab page. */
4124 wp = buf_jump_open_win(buf);
4125 if (wp != NULL)
4126 return wp;
4128 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4129 if (tp != curtab)
4131 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4132 if (wp->w_buffer == buf)
4133 break;
4134 if (wp != NULL)
4136 goto_tabpage_win(tp, wp);
4137 if (curwin != wp)
4138 wp = NULL; /* something went wrong */
4139 break;
4143 return wp;
4144 # else
4145 if (curwin->w_buffer == buf)
4146 return curwin;
4147 return NULL;
4148 # endif
4150 #endif
4153 * allocate a window structure and link it in the window list
4155 static win_T *
4156 win_alloc(after)
4157 win_T *after UNUSED;
4159 win_T *newwin;
4162 * allocate window structure and linesizes arrays
4164 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4165 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4167 vim_free(newwin);
4168 newwin = NULL;
4171 if (newwin != NULL)
4173 #ifdef FEAT_AUTOCMD
4174 /* Don't execute autocommands while the window is not properly
4175 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4176 * event. */
4177 block_autocmds();
4178 #endif
4180 * link the window in the window list
4182 #ifdef FEAT_WINDOWS
4183 win_append(after, newwin);
4184 #endif
4185 #ifdef FEAT_VERTSPLIT
4186 newwin->w_wincol = 0;
4187 newwin->w_width = Columns;
4188 #endif
4190 /* position the display and the cursor at the top of the file. */
4191 newwin->w_topline = 1;
4192 #ifdef FEAT_DIFF
4193 newwin->w_topfill = 0;
4194 #endif
4195 newwin->w_botline = 2;
4196 newwin->w_cursor.lnum = 1;
4197 #ifdef FEAT_SCROLLBIND
4198 newwin->w_scbind_pos = 1;
4199 #endif
4201 /* We won't calculate w_fraction until resizing the window */
4202 newwin->w_fraction = 0;
4203 newwin->w_prev_fraction_row = -1;
4205 #ifdef FEAT_GUI
4206 if (gui.in_use)
4208 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4209 SBAR_LEFT, newwin);
4210 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4211 SBAR_RIGHT, newwin);
4213 #endif
4214 #ifdef FEAT_EVAL
4215 /* init w: variables */
4216 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4217 #endif
4218 #ifdef FEAT_FOLDING
4219 foldInitWin(newwin);
4220 #endif
4221 #ifdef FEAT_AUTOCMD
4222 unblock_autocmds();
4223 #endif
4224 #ifdef FEAT_SEARCH_EXTRA
4225 newwin->w_match_head = NULL;
4226 newwin->w_next_match_id = 4;
4227 #endif
4229 return newwin;
4232 #if defined(FEAT_WINDOWS) || defined(PROTO)
4235 * remove window 'wp' from the window list and free the structure
4237 static void
4238 win_free(wp, tp)
4239 win_T *wp;
4240 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4242 int i;
4244 #ifdef FEAT_AUTOCMD
4245 /* Don't execute autocommands while the window is halfway being deleted.
4246 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4247 block_autocmds();
4248 #endif
4250 #ifdef FEAT_MZSCHEME
4251 mzscheme_window_free(wp);
4252 #endif
4254 #ifdef FEAT_PERL
4255 perl_win_free(wp);
4256 #endif
4258 #ifdef FEAT_PYTHON
4259 python_window_free(wp);
4260 #endif
4262 #ifdef FEAT_TCL
4263 tcl_window_free(wp);
4264 #endif
4266 #ifdef FEAT_RUBY
4267 ruby_window_free(wp);
4268 #endif
4270 clear_winopt(&wp->w_onebuf_opt);
4271 clear_winopt(&wp->w_allbuf_opt);
4273 #ifdef FEAT_EVAL
4274 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4275 #endif
4277 if (prevwin == wp)
4278 prevwin = NULL;
4279 win_free_lsize(wp);
4281 for (i = 0; i < wp->w_tagstacklen; ++i)
4282 vim_free(wp->w_tagstack[i].tagname);
4284 vim_free(wp->w_localdir);
4286 #ifdef FEAT_SEARCH_EXTRA
4287 clear_matches(wp);
4288 #endif
4290 #ifdef FEAT_JUMPLIST
4291 free_jumplist(wp);
4292 #endif
4294 #ifdef FEAT_QUICKFIX
4295 qf_free_all(wp);
4296 #endif
4298 #ifdef FEAT_GUI
4299 if (gui.in_use)
4301 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4302 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4304 #endif /* FEAT_GUI */
4306 win_remove(wp, tp);
4307 vim_free(wp);
4309 #ifdef FEAT_AUTOCMD
4310 unblock_autocmds();
4311 #endif
4315 * Append window "wp" in the window list after window "after".
4317 static void
4318 win_append(after, wp)
4319 win_T *after, *wp;
4321 win_T *before;
4323 if (after == NULL) /* after NULL is in front of the first */
4324 before = firstwin;
4325 else
4326 before = after->w_next;
4328 wp->w_next = before;
4329 wp->w_prev = after;
4330 if (after == NULL)
4331 firstwin = wp;
4332 else
4333 after->w_next = wp;
4334 if (before == NULL)
4335 lastwin = wp;
4336 else
4337 before->w_prev = wp;
4341 * Remove a window from the window list.
4343 static void
4344 win_remove(wp, tp)
4345 win_T *wp;
4346 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4348 if (wp->w_prev != NULL)
4349 wp->w_prev->w_next = wp->w_next;
4350 else if (tp == NULL)
4351 firstwin = wp->w_next;
4352 else
4353 tp->tp_firstwin = wp->w_next;
4354 if (wp->w_next != NULL)
4355 wp->w_next->w_prev = wp->w_prev;
4356 else if (tp == NULL)
4357 lastwin = wp->w_prev;
4358 else
4359 tp->tp_lastwin = wp->w_prev;
4363 * Append frame "frp" in a frame list after frame "after".
4365 static void
4366 frame_append(after, frp)
4367 frame_T *after, *frp;
4369 frp->fr_next = after->fr_next;
4370 after->fr_next = frp;
4371 if (frp->fr_next != NULL)
4372 frp->fr_next->fr_prev = frp;
4373 frp->fr_prev = after;
4377 * Insert frame "frp" in a frame list before frame "before".
4379 static void
4380 frame_insert(before, frp)
4381 frame_T *before, *frp;
4383 frp->fr_next = before;
4384 frp->fr_prev = before->fr_prev;
4385 before->fr_prev = frp;
4386 if (frp->fr_prev != NULL)
4387 frp->fr_prev->fr_next = frp;
4388 else
4389 frp->fr_parent->fr_child = frp;
4393 * Remove a frame from a frame list.
4395 static void
4396 frame_remove(frp)
4397 frame_T *frp;
4399 if (frp->fr_prev != NULL)
4400 frp->fr_prev->fr_next = frp->fr_next;
4401 else
4402 frp->fr_parent->fr_child = frp->fr_next;
4403 if (frp->fr_next != NULL)
4404 frp->fr_next->fr_prev = frp->fr_prev;
4407 #endif /* FEAT_WINDOWS */
4410 * Allocate w_lines[] for window "wp".
4411 * Return FAIL for failure, OK for success.
4414 win_alloc_lines(wp)
4415 win_T *wp;
4417 wp->w_lines_valid = 0;
4418 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4419 if (wp->w_lines == NULL)
4420 return FAIL;
4421 return OK;
4425 * free lsize arrays for a window
4427 void
4428 win_free_lsize(wp)
4429 win_T *wp;
4431 vim_free(wp->w_lines);
4432 wp->w_lines = NULL;
4436 * Called from win_new_shellsize() after Rows changed.
4437 * This only does the current tab page, others must be done when made active.
4439 void
4440 shell_new_rows()
4442 int h = (int)ROWS_AVAIL;
4444 if (firstwin == NULL) /* not initialized yet */
4445 return;
4446 #ifdef FEAT_WINDOWS
4447 if (h < frame_minheight(topframe, NULL))
4448 h = frame_minheight(topframe, NULL);
4450 /* First try setting the heights of windows with 'winfixheight'. If
4451 * that doesn't result in the right height, forget about that option. */
4452 frame_new_height(topframe, h, FALSE, TRUE);
4453 if (topframe->fr_height != h)
4454 frame_new_height(topframe, h, FALSE, FALSE);
4456 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4457 #else
4458 if (h < 1)
4459 h = 1;
4460 win_new_height(firstwin, h);
4461 #endif
4462 compute_cmdrow();
4463 #ifdef FEAT_WINDOWS
4464 curtab->tp_ch_used = p_ch;
4465 #endif
4467 #if 0
4468 /* Disabled: don't want making the screen smaller make a window larger. */
4469 if (p_ea)
4470 win_equal(curwin, FALSE, 'v');
4471 #endif
4474 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4476 * Called from win_new_shellsize() after Columns changed.
4478 void
4479 shell_new_columns()
4481 if (firstwin == NULL) /* not initialized yet */
4482 return;
4484 /* First try setting the widths of windows with 'winfixwidth'. If that
4485 * doesn't result in the right width, forget about that option. */
4486 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4487 if (topframe->fr_width != Columns)
4488 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4490 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4491 #if 0
4492 /* Disabled: don't want making the screen smaller make a window larger. */
4493 if (p_ea)
4494 win_equal(curwin, FALSE, 'h');
4495 #endif
4497 #endif
4499 #if defined(FEAT_CMDWIN) || defined(PROTO)
4501 * Save the size of all windows in "gap".
4503 void
4504 win_size_save(gap)
4505 garray_T *gap;
4508 win_T *wp;
4510 ga_init2(gap, (int)sizeof(int), 1);
4511 if (ga_grow(gap, win_count() * 2) == OK)
4512 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4514 ((int *)gap->ga_data)[gap->ga_len++] =
4515 wp->w_width + wp->w_vsep_width;
4516 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4521 * Restore window sizes, but only if the number of windows is still the same.
4522 * Does not free the growarray.
4524 void
4525 win_size_restore(gap)
4526 garray_T *gap;
4528 win_T *wp;
4529 int i;
4531 if (win_count() * 2 == gap->ga_len)
4533 i = 0;
4534 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4536 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4537 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4539 /* recompute the window positions */
4540 (void)win_comp_pos();
4543 #endif /* FEAT_CMDWIN */
4545 #if defined(FEAT_WINDOWS) || defined(PROTO)
4547 * Update the position for all windows, using the width and height of the
4548 * frames.
4549 * Returns the row just after the last window.
4552 win_comp_pos()
4554 int row = tabline_height();
4555 int col = 0;
4557 frame_comp_pos(topframe, &row, &col);
4558 return row;
4562 * Update the position of the windows in frame "topfrp", using the width and
4563 * height of the frames.
4564 * "*row" and "*col" are the top-left position of the frame. They are updated
4565 * to the bottom-right position plus one.
4567 static void
4568 frame_comp_pos(topfrp, row, col)
4569 frame_T *topfrp;
4570 int *row;
4571 int *col;
4573 win_T *wp;
4574 frame_T *frp;
4575 #ifdef FEAT_VERTSPLIT
4576 int startcol;
4577 int startrow;
4578 #endif
4580 wp = topfrp->fr_win;
4581 if (wp != NULL)
4583 if (wp->w_winrow != *row
4584 #ifdef FEAT_VERTSPLIT
4585 || wp->w_wincol != *col
4586 #endif
4589 /* position changed, redraw */
4590 wp->w_winrow = *row;
4591 #ifdef FEAT_VERTSPLIT
4592 wp->w_wincol = *col;
4593 #endif
4594 redraw_win_later(wp, NOT_VALID);
4595 wp->w_redr_status = TRUE;
4597 *row += wp->w_height + wp->w_status_height;
4598 #ifdef FEAT_VERTSPLIT
4599 *col += wp->w_width + wp->w_vsep_width;
4600 #endif
4602 else
4604 #ifdef FEAT_VERTSPLIT
4605 startrow = *row;
4606 startcol = *col;
4607 #endif
4608 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4610 #ifdef FEAT_VERTSPLIT
4611 if (topfrp->fr_layout == FR_ROW)
4612 *row = startrow; /* all frames are at the same row */
4613 else
4614 *col = startcol; /* all frames are at the same col */
4615 #endif
4616 frame_comp_pos(frp, row, col);
4621 #endif /* FEAT_WINDOWS */
4624 * Set current window height and take care of repositioning other windows to
4625 * fit around it.
4627 void
4628 win_setheight(height)
4629 int height;
4631 win_setheight_win(height, curwin);
4635 * Set the window height of window "win" and take care of repositioning other
4636 * windows to fit around it.
4638 void
4639 win_setheight_win(height, win)
4640 int height;
4641 win_T *win;
4643 int row;
4645 if (win == curwin)
4647 /* Always keep current window at least one line high, even when
4648 * 'winminheight' is zero. */
4649 #ifdef FEAT_WINDOWS
4650 if (height < p_wmh)
4651 height = p_wmh;
4652 #endif
4653 if (height == 0)
4654 height = 1;
4657 #ifdef FEAT_WINDOWS
4658 frame_setheight(win->w_frame, height + win->w_status_height);
4660 /* recompute the window positions */
4661 row = win_comp_pos();
4662 #else
4663 if (height > topframe->fr_height)
4664 height = topframe->fr_height;
4665 win->w_height = height;
4666 row = height;
4667 #endif
4670 * If there is extra space created between the last window and the command
4671 * line, clear it.
4673 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4674 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4675 cmdline_row = row;
4676 msg_row = row;
4677 msg_col = 0;
4679 redraw_all_later(NOT_VALID);
4682 #if defined(FEAT_WINDOWS) || defined(PROTO)
4685 * Set the height of a frame to "height" and take care that all frames and
4686 * windows inside it are resized. Also resize frames on the left and right if
4687 * the are in the same FR_ROW frame.
4689 * Strategy:
4690 * If the frame is part of a FR_COL frame, try fitting the frame in that
4691 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4692 * go to containing frames to resize them and make room.
4693 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4694 * Check for the minimal height of the FR_ROW frame.
4695 * At the top level we can also use change the command line height.
4697 static void
4698 frame_setheight(curfrp, height)
4699 frame_T *curfrp;
4700 int height;
4702 int room; /* total number of lines available */
4703 int take; /* number of lines taken from other windows */
4704 int room_cmdline; /* lines available from cmdline */
4705 int run;
4706 frame_T *frp;
4707 int h;
4708 int room_reserved;
4710 /* If the height already is the desired value, nothing to do. */
4711 if (curfrp->fr_height == height)
4712 return;
4714 if (curfrp->fr_parent == NULL)
4716 /* topframe: can only change the command line */
4717 if (height > ROWS_AVAIL)
4718 height = ROWS_AVAIL;
4719 if (height > 0)
4720 frame_new_height(curfrp, height, FALSE, FALSE);
4722 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4724 /* Row of frames: Also need to resize frames left and right of this
4725 * one. First check for the minimal height of these. */
4726 h = frame_minheight(curfrp->fr_parent, NULL);
4727 if (height < h)
4728 height = h;
4729 frame_setheight(curfrp->fr_parent, height);
4731 else
4734 * Column of frames: try to change only frames in this column.
4736 #ifdef FEAT_VERTSPLIT
4738 * Do this twice:
4739 * 1: compute room available, if it's not enough try resizing the
4740 * containing frame.
4741 * 2: compute the room available and adjust the height to it.
4742 * Try not to reduce the height of a window with 'winfixheight' set.
4744 for (run = 1; run <= 2; ++run)
4745 #else
4746 for (;;)
4747 #endif
4749 room = 0;
4750 room_reserved = 0;
4751 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4752 frp = frp->fr_next)
4754 if (frp != curfrp
4755 && frp->fr_win != NULL
4756 && frp->fr_win->w_p_wfh)
4757 room_reserved += frp->fr_height;
4758 room += frp->fr_height;
4759 if (frp != curfrp)
4760 room -= frame_minheight(frp, NULL);
4762 #ifdef FEAT_VERTSPLIT
4763 if (curfrp->fr_width != Columns)
4764 room_cmdline = 0;
4765 else
4766 #endif
4768 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4769 + lastwin->w_height + lastwin->w_status_height);
4770 if (room_cmdline < 0)
4771 room_cmdline = 0;
4774 if (height <= room + room_cmdline)
4775 break;
4776 #ifdef FEAT_VERTSPLIT
4777 if (run == 2 || curfrp->fr_width == Columns)
4778 #endif
4780 if (height > room + room_cmdline)
4781 height = room + room_cmdline;
4782 break;
4784 #ifdef FEAT_VERTSPLIT
4785 frame_setheight(curfrp->fr_parent, height
4786 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4787 #endif
4788 /*NOTREACHED*/
4792 * Compute the number of lines we will take from others frames (can be
4793 * negative!).
4795 take = height - curfrp->fr_height;
4797 /* If there is not enough room, also reduce the height of a window
4798 * with 'winfixheight' set. */
4799 if (height > room + room_cmdline - room_reserved)
4800 room_reserved = room + room_cmdline - height;
4801 /* If there is only a 'winfixheight' window and making the
4802 * window smaller, need to make the other window taller. */
4803 if (take < 0 && room - curfrp->fr_height < room_reserved)
4804 room_reserved = 0;
4806 if (take > 0 && room_cmdline > 0)
4808 /* use lines from cmdline first */
4809 if (take < room_cmdline)
4810 room_cmdline = take;
4811 take -= room_cmdline;
4812 topframe->fr_height += room_cmdline;
4816 * set the current frame to the new height
4818 frame_new_height(curfrp, height, FALSE, FALSE);
4821 * First take lines from the frames after the current frame. If
4822 * that is not enough, takes lines from frames above the current
4823 * frame.
4825 for (run = 0; run < 2; ++run)
4827 if (run == 0)
4828 frp = curfrp->fr_next; /* 1st run: start with next window */
4829 else
4830 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4831 while (frp != NULL && take != 0)
4833 h = frame_minheight(frp, NULL);
4834 if (room_reserved > 0
4835 && frp->fr_win != NULL
4836 && frp->fr_win->w_p_wfh)
4838 if (room_reserved >= frp->fr_height)
4839 room_reserved -= frp->fr_height;
4840 else
4842 if (frp->fr_height - room_reserved > take)
4843 room_reserved = frp->fr_height - take;
4844 take -= frp->fr_height - room_reserved;
4845 frame_new_height(frp, room_reserved, FALSE, FALSE);
4846 room_reserved = 0;
4849 else
4851 if (frp->fr_height - take < h)
4853 take -= frp->fr_height - h;
4854 frame_new_height(frp, h, FALSE, FALSE);
4856 else
4858 frame_new_height(frp, frp->fr_height - take,
4859 FALSE, FALSE);
4860 take = 0;
4863 if (run == 0)
4864 frp = frp->fr_next;
4865 else
4866 frp = frp->fr_prev;
4872 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4874 * Set current window width and take care of repositioning other windows to
4875 * fit around it.
4877 void
4878 win_setwidth(width)
4879 int width;
4881 win_setwidth_win(width, curwin);
4884 void
4885 win_setwidth_win(width, wp)
4886 int width;
4887 win_T *wp;
4889 /* Always keep current window at least one column wide, even when
4890 * 'winminwidth' is zero. */
4891 if (wp == curwin)
4893 if (width < p_wmw)
4894 width = p_wmw;
4895 if (width == 0)
4896 width = 1;
4899 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4901 /* recompute the window positions */
4902 (void)win_comp_pos();
4904 redraw_all_later(NOT_VALID);
4908 * Set the width of a frame to "width" and take care that all frames and
4909 * windows inside it are resized. Also resize frames above and below if the
4910 * are in the same FR_ROW frame.
4912 * Strategy is similar to frame_setheight().
4914 static void
4915 frame_setwidth(curfrp, width)
4916 frame_T *curfrp;
4917 int width;
4919 int room; /* total number of lines available */
4920 int take; /* number of lines taken from other windows */
4921 int run;
4922 frame_T *frp;
4923 int w;
4924 int room_reserved;
4926 /* If the width already is the desired value, nothing to do. */
4927 if (curfrp->fr_width == width)
4928 return;
4930 if (curfrp->fr_parent == NULL)
4931 /* topframe: can't change width */
4932 return;
4934 if (curfrp->fr_parent->fr_layout == FR_COL)
4936 /* Column of frames: Also need to resize frames above and below of
4937 * this one. First check for the minimal width of these. */
4938 w = frame_minwidth(curfrp->fr_parent, NULL);
4939 if (width < w)
4940 width = w;
4941 frame_setwidth(curfrp->fr_parent, width);
4943 else
4946 * Row of frames: try to change only frames in this row.
4948 * Do this twice:
4949 * 1: compute room available, if it's not enough try resizing the
4950 * containing frame.
4951 * 2: compute the room available and adjust the width to it.
4953 for (run = 1; run <= 2; ++run)
4955 room = 0;
4956 room_reserved = 0;
4957 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4958 frp = frp->fr_next)
4960 if (frp != curfrp
4961 && frp->fr_win != NULL
4962 && frp->fr_win->w_p_wfw)
4963 room_reserved += frp->fr_width;
4964 room += frp->fr_width;
4965 if (frp != curfrp)
4966 room -= frame_minwidth(frp, NULL);
4969 if (width <= room)
4970 break;
4971 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4973 if (width > room)
4974 width = room;
4975 break;
4977 frame_setwidth(curfrp->fr_parent, width
4978 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4982 * Compute the number of lines we will take from others frames (can be
4983 * negative!).
4985 take = width - curfrp->fr_width;
4987 /* If there is not enough room, also reduce the width of a window
4988 * with 'winfixwidth' set. */
4989 if (width > room - room_reserved)
4990 room_reserved = room - width;
4991 /* If there is only a 'winfixwidth' window and making the
4992 * window smaller, need to make the other window narrower. */
4993 if (take < 0 && room - curfrp->fr_width < room_reserved)
4994 room_reserved = 0;
4997 * set the current frame to the new width
4999 frame_new_width(curfrp, width, FALSE, FALSE);
5002 * First take lines from the frames right of the current frame. If
5003 * that is not enough, takes lines from frames left of the current
5004 * frame.
5006 for (run = 0; run < 2; ++run)
5008 if (run == 0)
5009 frp = curfrp->fr_next; /* 1st run: start with next window */
5010 else
5011 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5012 while (frp != NULL && take != 0)
5014 w = frame_minwidth(frp, NULL);
5015 if (room_reserved > 0
5016 && frp->fr_win != NULL
5017 && frp->fr_win->w_p_wfw)
5019 if (room_reserved >= frp->fr_width)
5020 room_reserved -= frp->fr_width;
5021 else
5023 if (frp->fr_width - room_reserved > take)
5024 room_reserved = frp->fr_width - take;
5025 take -= frp->fr_width - room_reserved;
5026 frame_new_width(frp, room_reserved, FALSE, FALSE);
5027 room_reserved = 0;
5030 else
5032 if (frp->fr_width - take < w)
5034 take -= frp->fr_width - w;
5035 frame_new_width(frp, w, FALSE, FALSE);
5037 else
5039 frame_new_width(frp, frp->fr_width - take,
5040 FALSE, FALSE);
5041 take = 0;
5044 if (run == 0)
5045 frp = frp->fr_next;
5046 else
5047 frp = frp->fr_prev;
5052 #endif /* FEAT_VERTSPLIT */
5055 * Check 'winminheight' for a valid value.
5057 void
5058 win_setminheight()
5060 int room;
5061 int first = TRUE;
5062 win_T *wp;
5064 /* loop until there is a 'winminheight' that is possible */
5065 while (p_wmh > 0)
5067 /* TODO: handle vertical splits */
5068 room = -p_wh;
5069 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5070 room += wp->w_height - p_wmh;
5071 if (room >= 0)
5072 break;
5073 --p_wmh;
5074 if (first)
5076 EMSG(_(e_noroom));
5077 first = FALSE;
5082 #ifdef FEAT_MOUSE
5085 * Status line of dragwin is dragged "offset" lines down (negative is up).
5087 void
5088 win_drag_status_line(dragwin, offset)
5089 win_T *dragwin;
5090 int offset;
5092 frame_T *curfr;
5093 frame_T *fr;
5094 int room;
5095 int row;
5096 int up; /* if TRUE, drag status line up, otherwise down */
5097 int n;
5099 fr = dragwin->w_frame;
5100 curfr = fr;
5101 if (fr != topframe) /* more than one window */
5103 fr = fr->fr_parent;
5104 /* When the parent frame is not a column of frames, its parent should
5105 * be. */
5106 if (fr->fr_layout != FR_COL)
5108 curfr = fr;
5109 if (fr != topframe) /* only a row of windows, may drag statusline */
5110 fr = fr->fr_parent;
5114 /* If this is the last frame in a column, may want to resize the parent
5115 * frame instead (go two up to skip a row of frames). */
5116 while (curfr != topframe && curfr->fr_next == NULL)
5118 if (fr != topframe)
5119 fr = fr->fr_parent;
5120 curfr = fr;
5121 if (fr != topframe)
5122 fr = fr->fr_parent;
5125 if (offset < 0) /* drag up */
5127 up = TRUE;
5128 offset = -offset;
5129 /* sum up the room of the current frame and above it */
5130 if (fr == curfr)
5132 /* only one window */
5133 room = fr->fr_height - frame_minheight(fr, NULL);
5135 else
5137 room = 0;
5138 for (fr = fr->fr_child; ; fr = fr->fr_next)
5140 room += fr->fr_height - frame_minheight(fr, NULL);
5141 if (fr == curfr)
5142 break;
5145 fr = curfr->fr_next; /* put fr at frame that grows */
5147 else /* drag down */
5149 up = FALSE;
5151 * Only dragging the last status line can reduce p_ch.
5153 room = Rows - cmdline_row;
5154 if (curfr->fr_next == NULL)
5155 room -= 1;
5156 else
5157 room -= p_ch;
5158 if (room < 0)
5159 room = 0;
5160 /* sum up the room of frames below of the current one */
5161 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5162 room += fr->fr_height - frame_minheight(fr, NULL);
5163 fr = curfr; /* put fr at window that grows */
5166 if (room < offset) /* Not enough room */
5167 offset = room; /* Move as far as we can */
5168 if (offset <= 0)
5169 return;
5172 * Grow frame fr by "offset" lines.
5173 * Doesn't happen when dragging the last status line up.
5175 if (fr != NULL)
5176 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5178 if (up)
5179 fr = curfr; /* current frame gets smaller */
5180 else
5181 fr = curfr->fr_next; /* next frame gets smaller */
5184 * Now make the other frames smaller.
5186 while (fr != NULL && offset > 0)
5188 n = frame_minheight(fr, NULL);
5189 if (fr->fr_height - offset <= n)
5191 offset -= fr->fr_height - n;
5192 frame_new_height(fr, n, !up, FALSE);
5194 else
5196 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5197 break;
5199 if (up)
5200 fr = fr->fr_prev;
5201 else
5202 fr = fr->fr_next;
5204 row = win_comp_pos();
5205 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5206 cmdline_row = row;
5207 p_ch = Rows - cmdline_row;
5208 if (p_ch < 1)
5209 p_ch = 1;
5210 curtab->tp_ch_used = p_ch;
5211 redraw_all_later(SOME_VALID);
5212 showmode();
5215 #ifdef FEAT_VERTSPLIT
5217 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5219 void
5220 win_drag_vsep_line(dragwin, offset)
5221 win_T *dragwin;
5222 int offset;
5224 frame_T *curfr;
5225 frame_T *fr;
5226 int room;
5227 int left; /* if TRUE, drag separator line left, otherwise right */
5228 int n;
5230 fr = dragwin->w_frame;
5231 if (fr == topframe) /* only one window (cannot happen?) */
5232 return;
5233 curfr = fr;
5234 fr = fr->fr_parent;
5235 /* When the parent frame is not a row of frames, its parent should be. */
5236 if (fr->fr_layout != FR_ROW)
5238 if (fr == topframe) /* only a column of windows (cannot happen?) */
5239 return;
5240 curfr = fr;
5241 fr = fr->fr_parent;
5244 /* If this is the last frame in a row, may want to resize a parent
5245 * frame instead. */
5246 while (curfr->fr_next == NULL)
5248 if (fr == topframe)
5249 break;
5250 curfr = fr;
5251 fr = fr->fr_parent;
5252 if (fr != topframe)
5254 curfr = fr;
5255 fr = fr->fr_parent;
5259 if (offset < 0) /* drag left */
5261 left = TRUE;
5262 offset = -offset;
5263 /* sum up the room of the current frame and left of it */
5264 room = 0;
5265 for (fr = fr->fr_child; ; fr = fr->fr_next)
5267 room += fr->fr_width - frame_minwidth(fr, NULL);
5268 if (fr == curfr)
5269 break;
5271 fr = curfr->fr_next; /* put fr at frame that grows */
5273 else /* drag right */
5275 left = FALSE;
5276 /* sum up the room of frames right of the current one */
5277 room = 0;
5278 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5279 room += fr->fr_width - frame_minwidth(fr, NULL);
5280 fr = curfr; /* put fr at window that grows */
5283 if (room < offset) /* Not enough room */
5284 offset = room; /* Move as far as we can */
5285 if (offset <= 0) /* No room at all, quit. */
5286 return;
5288 /* grow frame fr by offset lines */
5289 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5291 /* shrink other frames: current and at the left or at the right */
5292 if (left)
5293 fr = curfr; /* current frame gets smaller */
5294 else
5295 fr = curfr->fr_next; /* next frame gets smaller */
5297 while (fr != NULL && offset > 0)
5299 n = frame_minwidth(fr, NULL);
5300 if (fr->fr_width - offset <= n)
5302 offset -= fr->fr_width - n;
5303 frame_new_width(fr, n, !left, FALSE);
5305 else
5307 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5308 break;
5310 if (left)
5311 fr = fr->fr_prev;
5312 else
5313 fr = fr->fr_next;
5315 (void)win_comp_pos();
5316 redraw_all_later(NOT_VALID);
5318 #endif /* FEAT_VERTSPLIT */
5319 #endif /* FEAT_MOUSE */
5321 #endif /* FEAT_WINDOWS */
5324 * Set the height of a window.
5325 * This takes care of the things inside the window, not what happens to the
5326 * window position, the frame or to other windows.
5328 static void
5329 win_new_height(wp, height)
5330 win_T *wp;
5331 int height;
5333 linenr_T lnum;
5334 int sline, line_size;
5335 #define FRACTION_MULT 16384L
5337 /* Don't want a negative height. Happens when splitting a tiny window.
5338 * Will equalize heights soon to fix it. */
5339 if (height < 0)
5340 height = 0;
5341 if (wp->w_height == height)
5342 return; /* nothing to do */
5344 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5345 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5346 + FRACTION_MULT / 2) / (long)wp->w_height;
5348 wp->w_height = height;
5349 wp->w_skipcol = 0;
5351 /* Don't change w_topline when height is zero. Don't set w_topline when
5352 * 'scrollbind' is set and this isn't the current window. */
5353 if (height > 0
5354 #ifdef FEAT_SCROLLBIND
5355 && (!wp->w_p_scb || wp == curwin)
5356 #endif
5360 * Find a value for w_topline that shows the cursor at the same
5361 * relative position in the window as before (more or less).
5363 lnum = wp->w_cursor.lnum;
5364 if (lnum < 1) /* can happen when starting up */
5365 lnum = 1;
5366 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5367 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5368 sline = wp->w_wrow - line_size;
5370 if (sline >= 0)
5372 /* Make sure the whole cursor line is visible, if possible. */
5373 int rows = plines_win(wp, lnum, FALSE);
5375 if (sline > wp->w_height - rows)
5377 sline = wp->w_height - rows;
5378 wp->w_wrow -= rows - line_size;
5382 if (sline < 0)
5385 * Cursor line would go off top of screen if w_wrow was this high.
5386 * Make cursor line the first line in the window. If not enough
5387 * room use w_skipcol;
5389 wp->w_wrow = line_size;
5390 if (wp->w_wrow >= wp->w_height
5391 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5393 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5394 --wp->w_wrow;
5395 while (wp->w_wrow >= wp->w_height)
5397 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5398 + win_col_off2(wp);
5399 --wp->w_wrow;
5403 else
5405 while (sline > 0 && lnum > 1)
5407 #ifdef FEAT_FOLDING
5408 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5409 if (lnum == 1)
5411 /* first line in buffer is folded */
5412 line_size = 1;
5413 --sline;
5414 break;
5416 #endif
5417 --lnum;
5418 #ifdef FEAT_DIFF
5419 if (lnum == wp->w_topline)
5420 line_size = plines_win_nofill(wp, lnum, TRUE)
5421 + wp->w_topfill;
5422 else
5423 #endif
5424 line_size = plines_win(wp, lnum, TRUE);
5425 sline -= line_size;
5428 if (sline < 0)
5431 * Line we want at top would go off top of screen. Use next
5432 * line instead.
5434 #ifdef FEAT_FOLDING
5435 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5436 #endif
5437 lnum++;
5438 wp->w_wrow -= line_size + sline;
5440 else if (sline > 0)
5442 /* First line of file reached, use that as topline. */
5443 lnum = 1;
5444 wp->w_wrow -= sline;
5447 set_topline(wp, lnum);
5450 if (wp == curwin)
5452 if (p_so)
5453 update_topline();
5454 curs_columns(FALSE); /* validate w_wrow */
5456 wp->w_prev_fraction_row = wp->w_wrow;
5458 win_comp_scroll(wp);
5459 redraw_win_later(wp, SOME_VALID);
5460 #ifdef FEAT_WINDOWS
5461 wp->w_redr_status = TRUE;
5462 #endif
5463 invalidate_botline_win(wp);
5466 #ifdef FEAT_VERTSPLIT
5468 * Set the width of a window.
5470 static void
5471 win_new_width(wp, width)
5472 win_T *wp;
5473 int width;
5475 wp->w_width = width;
5476 wp->w_lines_valid = 0;
5477 changed_line_abv_curs_win(wp);
5478 invalidate_botline_win(wp);
5479 if (wp == curwin)
5481 update_topline();
5482 curs_columns(TRUE); /* validate w_wrow */
5484 redraw_win_later(wp, NOT_VALID);
5485 wp->w_redr_status = TRUE;
5487 #endif
5489 void
5490 win_comp_scroll(wp)
5491 win_T *wp;
5493 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5494 if (wp->w_p_scr == 0)
5495 wp->w_p_scr = 1;
5499 * command_height: called whenever p_ch has been changed
5501 void
5502 command_height()
5504 #ifdef FEAT_WINDOWS
5505 int h;
5506 frame_T *frp;
5507 int old_p_ch = curtab->tp_ch_used;
5509 /* Use the value of p_ch that we remembered. This is needed for when the
5510 * GUI starts up, we can't be sure in what order things happen. And when
5511 * p_ch was changed in another tab page. */
5512 curtab->tp_ch_used = p_ch;
5514 /* Find bottom frame with width of screen. */
5515 frp = lastwin->w_frame;
5516 # ifdef FEAT_VERTSPLIT
5517 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5518 frp = frp->fr_parent;
5519 # endif
5521 /* Avoid changing the height of a window with 'winfixheight' set. */
5522 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5523 && frp->fr_win->w_p_wfh)
5524 frp = frp->fr_prev;
5526 if (starting != NO_SCREEN)
5528 cmdline_row = Rows - p_ch;
5530 if (p_ch > old_p_ch) /* p_ch got bigger */
5532 while (p_ch > old_p_ch)
5534 if (frp == NULL)
5536 EMSG(_(e_noroom));
5537 p_ch = old_p_ch;
5538 curtab->tp_ch_used = p_ch;
5539 cmdline_row = Rows - p_ch;
5540 break;
5542 h = frp->fr_height - frame_minheight(frp, NULL);
5543 if (h > p_ch - old_p_ch)
5544 h = p_ch - old_p_ch;
5545 old_p_ch += h;
5546 frame_add_height(frp, -h);
5547 frp = frp->fr_prev;
5550 /* Recompute window positions. */
5551 (void)win_comp_pos();
5553 /* clear the lines added to cmdline */
5554 if (full_screen)
5555 screen_fill((int)(cmdline_row), (int)Rows, 0,
5556 (int)Columns, ' ', ' ', 0);
5557 msg_row = cmdline_row;
5558 redraw_cmdline = TRUE;
5559 return;
5562 if (msg_row < cmdline_row)
5563 msg_row = cmdline_row;
5564 redraw_cmdline = TRUE;
5566 frame_add_height(frp, (int)(old_p_ch - p_ch));
5568 /* Recompute window positions. */
5569 if (frp != lastwin->w_frame)
5570 (void)win_comp_pos();
5571 #else
5572 cmdline_row = Rows - p_ch;
5573 win_setheight(cmdline_row);
5574 #endif
5577 #if defined(FEAT_WINDOWS) || defined(PROTO)
5579 * Resize frame "frp" to be "n" lines higher (negative for less high).
5580 * Also resize the frames it is contained in.
5582 static void
5583 frame_add_height(frp, n)
5584 frame_T *frp;
5585 int n;
5587 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5588 for (;;)
5590 frp = frp->fr_parent;
5591 if (frp == NULL)
5592 break;
5593 frp->fr_height += n;
5598 * Add or remove a status line for the bottom window(s), according to the
5599 * value of 'laststatus'.
5601 void
5602 last_status(morewin)
5603 int morewin; /* pretend there are two or more windows */
5605 /* Don't make a difference between horizontal or vertical split. */
5606 last_status_rec(topframe, (p_ls == 2
5607 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5610 static void
5611 last_status_rec(fr, statusline)
5612 frame_T *fr;
5613 int statusline;
5615 frame_T *fp;
5616 win_T *wp;
5618 if (fr->fr_layout == FR_LEAF)
5620 wp = fr->fr_win;
5621 if (wp->w_status_height != 0 && !statusline)
5623 /* remove status line */
5624 win_new_height(wp, wp->w_height + 1);
5625 wp->w_status_height = 0;
5626 comp_col();
5628 else if (wp->w_status_height == 0 && statusline)
5630 /* Find a frame to take a line from. */
5631 fp = fr;
5632 while (fp->fr_height <= frame_minheight(fp, NULL))
5634 if (fp == topframe)
5636 EMSG(_(e_noroom));
5637 return;
5639 /* In a column of frames: go to frame above. If already at
5640 * the top or in a row of frames: go to parent. */
5641 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5642 fp = fp->fr_prev;
5643 else
5644 fp = fp->fr_parent;
5646 wp->w_status_height = 1;
5647 if (fp != fr)
5649 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5650 frame_fix_height(wp);
5651 (void)win_comp_pos();
5653 else
5654 win_new_height(wp, wp->w_height - 1);
5655 comp_col();
5656 redraw_all_later(SOME_VALID);
5659 #ifdef FEAT_VERTSPLIT
5660 else if (fr->fr_layout == FR_ROW)
5662 /* vertically split windows, set status line for each one */
5663 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5664 last_status_rec(fp, statusline);
5666 #endif
5667 else
5669 /* horizontally split window, set status line for last one */
5670 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5672 last_status_rec(fp, statusline);
5677 * Return the number of lines used by the tab page line.
5680 tabline_height()
5682 #ifdef FEAT_GUI_TABLINE
5683 /* When the GUI has the tabline then this always returns zero. */
5684 if (gui_use_tabline())
5685 return 0;
5686 #endif
5687 switch (p_stal)
5689 case 0: return 0;
5690 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5692 return 1;
5695 #endif /* FEAT_WINDOWS */
5697 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5699 * Get the file name at the cursor.
5700 * If Visual mode is active, use the selected text if it's in one line.
5701 * Returns the name in allocated memory, NULL for failure.
5703 char_u *
5704 grab_file_name(count, file_lnum)
5705 long count;
5706 linenr_T *file_lnum;
5708 # ifdef FEAT_VISUAL
5709 if (VIsual_active)
5711 int len;
5712 char_u *ptr;
5714 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5715 return NULL;
5716 return find_file_name_in_path(ptr, len,
5717 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5719 # endif
5720 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5721 file_lnum);
5726 * Return the file name under or after the cursor.
5728 * The 'path' option is searched if the file name is not absolute.
5729 * The string returned has been alloc'ed and should be freed by the caller.
5730 * NULL is returned if the file name or file is not found.
5732 * options:
5733 * FNAME_MESS give error messages
5734 * FNAME_EXP expand to path
5735 * FNAME_HYP check for hypertext link
5736 * FNAME_INCL apply "includeexpr"
5738 char_u *
5739 file_name_at_cursor(options, count, file_lnum)
5740 int options;
5741 long count;
5742 linenr_T *file_lnum;
5744 return file_name_in_line(ml_get_curline(),
5745 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5746 file_lnum);
5750 * Return the name of the file under or after ptr[col].
5751 * Otherwise like file_name_at_cursor().
5753 char_u *
5754 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5755 char_u *line;
5756 int col;
5757 int options;
5758 long count;
5759 char_u *rel_fname; /* file we are searching relative to */
5760 linenr_T *file_lnum; /* line number after the file name */
5762 char_u *ptr;
5763 int len;
5766 * search forward for what could be the start of a file name
5768 ptr = line + col;
5769 while (*ptr != NUL && !vim_isfilec(*ptr))
5770 mb_ptr_adv(ptr);
5771 if (*ptr == NUL) /* nothing found */
5773 if (options & FNAME_MESS)
5774 EMSG(_("E446: No file name under cursor"));
5775 return NULL;
5779 * Search backward for first char of the file name.
5780 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5782 while (ptr > line)
5784 #ifdef FEAT_MBYTE
5785 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5786 ptr -= len + 1;
5787 else
5788 #endif
5789 if (vim_isfilec(ptr[-1])
5790 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5791 --ptr;
5792 else
5793 break;
5797 * Search forward for the last char of the file name.
5798 * Also allow "://" when ':' is not in 'isfname'.
5800 len = 0;
5801 while (vim_isfilec(ptr[len])
5802 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5803 #ifdef FEAT_MBYTE
5804 if (has_mbyte)
5805 len += (*mb_ptr2len)(ptr + len);
5806 else
5807 #endif
5808 ++len;
5811 * If there is trailing punctuation, remove it.
5812 * But don't remove "..", could be a directory name.
5814 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5815 && ptr[len - 2] != '.')
5816 --len;
5818 if (file_lnum != NULL)
5820 char_u *p;
5822 /* Get the number after the file name and a separator character */
5823 p = ptr + len;
5824 p = skipwhite(p);
5825 if (*p != NUL)
5827 if (!isdigit(*p))
5828 ++p; /* skip the separator */
5829 p = skipwhite(p);
5830 if (isdigit(*p))
5831 *file_lnum = (int)getdigits(&p);
5835 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5838 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5839 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5841 static char_u *
5842 eval_includeexpr(ptr, len)
5843 char_u *ptr;
5844 int len;
5846 char_u *res;
5848 set_vim_var_string(VV_FNAME, ptr, len);
5849 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5850 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5851 set_vim_var_string(VV_FNAME, NULL, 0);
5852 return res;
5854 #endif
5857 * Return the name of the file ptr[len] in 'path'.
5858 * Otherwise like file_name_at_cursor().
5860 char_u *
5861 find_file_name_in_path(ptr, len, options, count, rel_fname)
5862 char_u *ptr;
5863 int len;
5864 int options;
5865 long count;
5866 char_u *rel_fname; /* file we are searching relative to */
5868 char_u *file_name;
5869 int c;
5870 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5871 char_u *tofree = NULL;
5873 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5875 tofree = eval_includeexpr(ptr, len);
5876 if (tofree != NULL)
5878 ptr = tofree;
5879 len = (int)STRLEN(ptr);
5882 # endif
5884 if (options & FNAME_EXP)
5886 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5887 TRUE, rel_fname);
5889 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5891 * If the file could not be found in a normal way, try applying
5892 * 'includeexpr' (unless done already).
5894 if (file_name == NULL
5895 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5897 tofree = eval_includeexpr(ptr, len);
5898 if (tofree != NULL)
5900 ptr = tofree;
5901 len = (int)STRLEN(ptr);
5902 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5903 TRUE, rel_fname);
5906 # endif
5907 if (file_name == NULL && (options & FNAME_MESS))
5909 c = ptr[len];
5910 ptr[len] = NUL;
5911 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5912 ptr[len] = c;
5915 /* Repeat finding the file "count" times. This matters when it
5916 * appears several times in the path. */
5917 while (file_name != NULL && --count > 0)
5919 vim_free(file_name);
5920 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5923 else
5924 file_name = vim_strnsave(ptr, len);
5926 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5927 vim_free(tofree);
5928 # endif
5930 return file_name;
5932 #endif /* FEAT_SEARCHPATH */
5935 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5936 * Also check for ":\\", which MS Internet Explorer accepts, return
5937 * URL_BACKSLASH.
5939 static int
5940 path_is_url(p)
5941 char_u *p;
5943 if (STRNCMP(p, "://", (size_t)3) == 0)
5944 return URL_SLASH;
5945 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5946 return URL_BACKSLASH;
5947 return 0;
5951 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5952 * Return URL_BACKSLASH for "name:\\".
5953 * Return zero otherwise.
5956 path_with_url(fname)
5957 char_u *fname;
5959 char_u *p;
5961 for (p = fname; isalpha(*p); ++p)
5963 return path_is_url(p);
5967 * Return TRUE if "name" is a full (absolute) path name or URL.
5970 vim_isAbsName(name)
5971 char_u *name;
5973 return (path_with_url(name) != 0 || mch_isFullName(name));
5977 * Get absolute file name into buffer "buf[len]".
5979 * return FAIL for failure, OK otherwise
5982 vim_FullName(fname, buf, len, force)
5983 char_u *fname, *buf;
5984 int len;
5985 int force; /* force expansion even when already absolute */
5987 int retval = OK;
5988 int url;
5990 *buf = NUL;
5991 if (fname == NULL)
5992 return FAIL;
5994 url = path_with_url(fname);
5995 if (!url)
5996 retval = mch_FullName(fname, buf, len, force);
5997 if (url || retval == FAIL)
5999 /* something failed; use the file name (truncate when too long) */
6000 vim_strncpy(buf, fname, len - 1);
6002 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6003 slash_adjust(buf);
6004 #endif
6005 return retval;
6009 * Return the minimal number of rows that is needed on the screen to display
6010 * the current number of windows.
6013 min_rows()
6015 int total;
6016 #ifdef FEAT_WINDOWS
6017 tabpage_T *tp;
6018 int n;
6019 #endif
6021 if (firstwin == NULL) /* not initialized yet */
6022 return MIN_LINES;
6024 #ifdef FEAT_WINDOWS
6025 total = 0;
6026 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6028 n = frame_minheight(tp->tp_topframe, NULL);
6029 if (total < n)
6030 total = n;
6032 total += tabline_height();
6033 #else
6034 total = 1; /* at least one window should have a line! */
6035 #endif
6036 total += 1; /* count the room for the command line */
6037 return total;
6041 * Return TRUE if there is only one window (in the current tab page), not
6042 * counting a help or preview window, unless it is the current window.
6045 only_one_window()
6047 #ifdef FEAT_WINDOWS
6048 int count = 0;
6049 win_T *wp;
6051 /* If there is another tab page there always is another window. */
6052 if (first_tabpage->tp_next != NULL)
6053 return FALSE;
6055 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6056 if (!((wp->w_buffer->b_help && !curbuf->b_help)
6057 # ifdef FEAT_QUICKFIX
6058 || wp->w_p_pvw
6059 # endif
6060 ) || wp == curwin)
6061 ++count;
6062 return (count <= 1);
6063 #else
6064 return TRUE;
6065 #endif
6068 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6070 * Correct the cursor line number in other windows. Used after changing the
6071 * current buffer, and before applying autocommands.
6072 * When "do_curwin" is TRUE, also check current window.
6074 void
6075 check_lnums(do_curwin)
6076 int do_curwin;
6078 win_T *wp;
6080 #ifdef FEAT_WINDOWS
6081 tabpage_T *tp;
6083 FOR_ALL_TAB_WINDOWS(tp, wp)
6084 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6085 #else
6086 wp = curwin;
6087 if (do_curwin)
6088 #endif
6090 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6091 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6092 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6093 wp->w_topline = curbuf->b_ml.ml_line_count;
6096 #endif
6098 #if defined(FEAT_WINDOWS) || defined(PROTO)
6101 * A snapshot of the window sizes, to restore them after closing the help
6102 * window.
6103 * Only these fields are used:
6104 * fr_layout
6105 * fr_width
6106 * fr_height
6107 * fr_next
6108 * fr_child
6109 * fr_win (only valid for the old curwin, NULL otherwise)
6113 * Create a snapshot of the current frame sizes.
6115 static void
6116 make_snapshot()
6118 clear_snapshot(curtab);
6119 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6122 static void
6123 make_snapshot_rec(fr, frp)
6124 frame_T *fr;
6125 frame_T **frp;
6127 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6128 if (*frp == NULL)
6129 return;
6130 (*frp)->fr_layout = fr->fr_layout;
6131 # ifdef FEAT_VERTSPLIT
6132 (*frp)->fr_width = fr->fr_width;
6133 # endif
6134 (*frp)->fr_height = fr->fr_height;
6135 if (fr->fr_next != NULL)
6136 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6137 if (fr->fr_child != NULL)
6138 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6139 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6140 (*frp)->fr_win = curwin;
6144 * Remove any existing snapshot.
6146 static void
6147 clear_snapshot(tp)
6148 tabpage_T *tp;
6150 clear_snapshot_rec(tp->tp_snapshot);
6151 tp->tp_snapshot = NULL;
6154 static void
6155 clear_snapshot_rec(fr)
6156 frame_T *fr;
6158 if (fr != NULL)
6160 clear_snapshot_rec(fr->fr_next);
6161 clear_snapshot_rec(fr->fr_child);
6162 vim_free(fr);
6167 * Restore a previously created snapshot, if there is any.
6168 * This is only done if the screen size didn't change and the window layout is
6169 * still the same.
6171 static void
6172 restore_snapshot(close_curwin)
6173 int close_curwin; /* closing current window */
6175 win_T *wp;
6177 if (curtab->tp_snapshot != NULL
6178 # ifdef FEAT_VERTSPLIT
6179 && curtab->tp_snapshot->fr_width == topframe->fr_width
6180 # endif
6181 && curtab->tp_snapshot->fr_height == topframe->fr_height
6182 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6184 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6185 win_comp_pos();
6186 if (wp != NULL && close_curwin)
6187 win_goto(wp);
6188 redraw_all_later(CLEAR);
6190 clear_snapshot(curtab);
6194 * Check if frames "sn" and "fr" have the same layout, same following frames
6195 * and same children.
6197 static int
6198 check_snapshot_rec(sn, fr)
6199 frame_T *sn;
6200 frame_T *fr;
6202 if (sn->fr_layout != fr->fr_layout
6203 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6204 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6205 || (sn->fr_next != NULL
6206 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6207 || (sn->fr_child != NULL
6208 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6209 return FAIL;
6210 return OK;
6214 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6215 * following frames and children.
6216 * Returns a pointer to the old current window, or NULL.
6218 static win_T *
6219 restore_snapshot_rec(sn, fr)
6220 frame_T *sn;
6221 frame_T *fr;
6223 win_T *wp = NULL;
6224 win_T *wp2;
6226 fr->fr_height = sn->fr_height;
6227 # ifdef FEAT_VERTSPLIT
6228 fr->fr_width = sn->fr_width;
6229 # endif
6230 if (fr->fr_layout == FR_LEAF)
6232 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6233 # ifdef FEAT_VERTSPLIT
6234 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6235 # endif
6236 wp = sn->fr_win;
6238 if (sn->fr_next != NULL)
6240 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6241 if (wp2 != NULL)
6242 wp = wp2;
6244 if (sn->fr_child != NULL)
6246 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6247 if (wp2 != NULL)
6248 wp = wp2;
6250 return wp;
6253 #endif
6255 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6257 * Return TRUE if there is any vertically split window.
6260 win_hasvertsplit()
6262 frame_T *fr;
6264 if (topframe->fr_layout == FR_ROW)
6265 return TRUE;
6267 if (topframe->fr_layout == FR_COL)
6268 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6269 if (fr->fr_layout == FR_ROW)
6270 return TRUE;
6272 return FALSE;
6274 #endif
6276 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6278 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6279 * highlighted with the group 'grp' with priority 'prio'.
6280 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6281 * If no particular ID is desired, -1 must be specified for 'id'.
6282 * Return ID of added match, -1 on failure.
6285 match_add(wp, grp, pat, prio, id)
6286 win_T *wp;
6287 char_u *grp;
6288 char_u *pat;
6289 int prio;
6290 int id;
6292 matchitem_T *cur;
6293 matchitem_T *prev;
6294 matchitem_T *m;
6295 int hlg_id;
6296 regprog_T *regprog;
6298 if (*grp == NUL || *pat == NUL)
6299 return -1;
6300 if (id < -1 || id == 0)
6302 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6303 return -1;
6305 if (id != -1)
6307 cur = wp->w_match_head;
6308 while (cur != NULL)
6310 if (cur->id == id)
6312 EMSGN("E801: ID already taken: %ld", id);
6313 return -1;
6315 cur = cur->next;
6318 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6320 EMSG2(_(e_nogroup), grp);
6321 return -1;
6323 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6325 EMSG2(_(e_invarg2), pat);
6326 return -1;
6329 /* Find available match ID. */
6330 while (id == -1)
6332 cur = wp->w_match_head;
6333 while (cur != NULL && cur->id != wp->w_next_match_id)
6334 cur = cur->next;
6335 if (cur == NULL)
6336 id = wp->w_next_match_id;
6337 wp->w_next_match_id++;
6340 /* Build new match. */
6341 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6342 m->id = id;
6343 m->priority = prio;
6344 m->pattern = vim_strsave(pat);
6345 m->hlg_id = hlg_id;
6346 m->match.regprog = regprog;
6347 m->match.rmm_ic = FALSE;
6348 m->match.rmm_maxcol = 0;
6350 /* Insert new match. The match list is in ascending order with regard to
6351 * the match priorities. */
6352 cur = wp->w_match_head;
6353 prev = cur;
6354 while (cur != NULL && prio >= cur->priority)
6356 prev = cur;
6357 cur = cur->next;
6359 if (cur == prev)
6360 wp->w_match_head = m;
6361 else
6362 prev->next = m;
6363 m->next = cur;
6365 redraw_later(SOME_VALID);
6366 return id;
6370 * Delete match with ID 'id' in the match list of window 'wp'.
6371 * Print error messages if 'perr' is TRUE.
6374 match_delete(wp, id, perr)
6375 win_T *wp;
6376 int id;
6377 int perr;
6379 matchitem_T *cur = wp->w_match_head;
6380 matchitem_T *prev = cur;
6382 if (id < 1)
6384 if (perr == TRUE)
6385 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6386 id);
6387 return -1;
6389 while (cur != NULL && cur->id != id)
6391 prev = cur;
6392 cur = cur->next;
6394 if (cur == NULL)
6396 if (perr == TRUE)
6397 EMSGN("E803: ID not found: %ld", id);
6398 return -1;
6400 if (cur == prev)
6401 wp->w_match_head = cur->next;
6402 else
6403 prev->next = cur->next;
6404 vim_free(cur->match.regprog);
6405 vim_free(cur->pattern);
6406 vim_free(cur);
6407 redraw_later(SOME_VALID);
6408 return 0;
6412 * Delete all matches in the match list of window 'wp'.
6414 void
6415 clear_matches(wp)
6416 win_T *wp;
6418 matchitem_T *m;
6420 while (wp->w_match_head != NULL)
6422 m = wp->w_match_head->next;
6423 vim_free(wp->w_match_head->match.regprog);
6424 vim_free(wp->w_match_head->pattern);
6425 vim_free(wp->w_match_head);
6426 wp->w_match_head = m;
6428 redraw_later(SOME_VALID);
6432 * Get match from ID 'id' in window 'wp'.
6433 * Return NULL if match not found.
6435 matchitem_T *
6436 get_match(wp, id)
6437 win_T *wp;
6438 int id;
6440 matchitem_T *cur = wp->w_match_head;
6442 while (cur != NULL && cur->id != id)
6443 cur = cur->next;
6444 return cur;
6446 #endif