Merge branch 'vim'
[MacVim.git] / src / window.c
blobc1955ad7000245234310f67e78bfaf339458db54
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
10 #include "vim.h"
12 static int path_is_url __ARGS((char_u *p));
13 #if defined(FEAT_WINDOWS) || defined(PROTO)
14 static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
15 static void win_init_some __ARGS((win_T *newp, win_T *oldp));
16 static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17 static void frame_setheight __ARGS((frame_T *curfrp, int height));
18 #ifdef FEAT_VERTSPLIT
19 static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20 #endif
21 static void win_exchange __ARGS((long));
22 static void win_rotate __ARGS((int, int));
23 static void win_totop __ARGS((int size, int flags));
24 static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
25 static int last_window __ARGS((void));
26 static int one_window __ARGS((void));
27 static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
28 static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
29 static tabpage_T *alt_tabpage __ARGS((void));
30 static win_T *frame2win __ARGS((frame_T *frp));
31 static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32 static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33 static int frame_fixed_height __ARGS((frame_T *frp));
34 #ifdef FEAT_VERTSPLIT
35 static int frame_fixed_width __ARGS((frame_T *frp));
36 static void frame_add_statusline __ARGS((frame_T *frp));
37 static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
38 static void frame_add_vsep __ARGS((frame_T *frp));
39 static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40 static void frame_fix_width __ARGS((win_T *wp));
41 #endif
42 #endif
43 static int win_alloc_firstwin __ARGS((win_T *oldwin));
44 static void new_frame __ARGS((win_T *wp));
45 #if defined(FEAT_WINDOWS) || defined(PROTO)
46 static tabpage_T *alloc_tabpage __ARGS((void));
47 static int leave_tabpage __ARGS((buf_T *new_curbuf));
48 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
49 static void frame_fix_height __ARGS((win_T *wp));
50 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
51 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
52 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
53 static void frame_append __ARGS((frame_T *after, frame_T *frp));
54 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55 static void frame_remove __ARGS((frame_T *frp));
56 #ifdef FEAT_VERTSPLIT
57 static void win_new_width __ARGS((win_T *wp, int width));
58 static void win_goto_ver __ARGS((int up, long count));
59 static void win_goto_hor __ARGS((int left, long count));
60 #endif
61 static void frame_add_height __ARGS((frame_T *frp, int n));
62 static void last_status_rec __ARGS((frame_T *fr, int statusline));
64 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
65 static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
66 static void clear_snapshot_rec __ARGS((frame_T *fr));
67 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
70 #endif /* FEAT_WINDOWS */
72 static win_T *win_alloc __ARGS((win_T *after, int hidden));
73 static void win_new_height __ARGS((win_T *, int));
75 #define URL_SLASH 1 /* path_is_url() has found "://" */
76 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
78 #define NOWIN (win_T *)-1 /* non-existing window */
80 #ifdef FEAT_WINDOWS
81 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
82 #else
83 # define ROWS_AVAIL (Rows - p_ch)
84 #endif
86 #if defined(FEAT_WINDOWS) || defined(PROTO)
88 static char *m_onlyone = N_("Already only one window");
91 * all CTRL-W window commands are handled here, called from normal_cmd().
93 void
94 do_window(nchar, Prenum, xchar)
95 int nchar;
96 long Prenum;
97 int xchar; /* extra char from ":wincmd gx" or NUL */
99 long Prenum1;
100 win_T *wp;
101 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
102 char_u *ptr;
103 linenr_T lnum = -1;
104 #endif
105 #ifdef FEAT_FIND_ID
106 int type = FIND_DEFINE;
107 int len;
108 #endif
109 char_u cbuf[40];
111 if (Prenum == 0)
112 Prenum1 = 1;
113 else
114 Prenum1 = Prenum;
116 #ifdef FEAT_CMDWIN
117 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
118 #else
119 # define CHECK_CMDWIN
120 #endif
122 switch (nchar)
124 /* split current window in two parts, horizontally */
125 case 'S':
126 case Ctrl_S:
127 case 's':
128 CHECK_CMDWIN
129 #ifdef FEAT_VISUAL
130 reset_VIsual_and_resel(); /* stop Visual mode */
131 #endif
132 #ifdef FEAT_QUICKFIX
133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
137 #endif
138 #ifdef FEAT_GUI
139 need_mouse_correct = TRUE;
140 #endif
141 win_split((int)Prenum, 0);
142 break;
144 #ifdef FEAT_VERTSPLIT
145 /* split current window in two parts, vertically */
146 case Ctrl_V:
147 case 'v':
148 CHECK_CMDWIN
149 # ifdef FEAT_VISUAL
150 reset_VIsual_and_resel(); /* stop Visual mode */
151 # endif
152 # ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157 # endif
158 # ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160 # endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163 #endif
165 /* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169 #ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171 #endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
174 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
175 "%ld", Prenum);
176 do_cmdline_cmd(cbuf);
177 break;
179 /* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183 #ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185 #endif
186 #ifdef FEAT_QUICKFIX
187 newwindow:
188 #endif
189 if (Prenum)
190 /* window height */
191 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
192 else
193 cbuf[0] = NUL;
194 #if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
195 if (nchar == 'v' || nchar == Ctrl_V)
196 STRCAT(cbuf, "v");
197 #endif
198 STRCAT(cbuf, "new");
199 do_cmdline_cmd(cbuf);
200 break;
202 /* quit current window */
203 case Ctrl_Q:
204 case 'q':
205 #ifdef FEAT_VISUAL
206 reset_VIsual_and_resel(); /* stop Visual mode */
207 #endif
208 do_cmdline_cmd((char_u *)"quit");
209 break;
211 /* close current window */
212 case Ctrl_C:
213 case 'c':
214 #ifdef FEAT_VISUAL
215 reset_VIsual_and_resel(); /* stop Visual mode */
216 #endif
217 do_cmdline_cmd((char_u *)"close");
218 break;
220 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
221 /* close preview window */
222 case Ctrl_Z:
223 case 'z':
224 CHECK_CMDWIN
225 #ifdef FEAT_VISUAL
226 reset_VIsual_and_resel(); /* stop Visual mode */
227 #endif
228 do_cmdline_cmd((char_u *)"pclose");
229 break;
231 /* cursor to preview window */
232 case 'P':
233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
234 if (wp->w_p_pvw)
235 break;
236 if (wp == NULL)
237 EMSG(_("E441: There is no preview window"));
238 else
239 win_goto(wp);
240 break;
241 #endif
243 /* close all but current window */
244 case Ctrl_O:
245 case 'o':
246 CHECK_CMDWIN
247 #ifdef FEAT_VISUAL
248 reset_VIsual_and_resel(); /* stop Visual mode */
249 #endif
250 do_cmdline_cmd((char_u *)"only");
251 break;
253 /* cursor to next window with wrap around */
254 case Ctrl_W:
255 case 'w':
256 /* cursor to previous window with wrap around */
257 case 'W':
258 CHECK_CMDWIN
259 if (firstwin == lastwin && Prenum != 1) /* just one window */
260 beep_flush();
261 else
263 if (Prenum) /* go to specified window */
265 for (wp = firstwin; --Prenum > 0; )
267 if (wp->w_next == NULL)
268 break;
269 else
270 wp = wp->w_next;
273 else
275 if (nchar == 'W') /* go to previous window */
277 wp = curwin->w_prev;
278 if (wp == NULL)
279 wp = lastwin; /* wrap around */
281 else /* go to next window */
283 wp = curwin->w_next;
284 if (wp == NULL)
285 wp = firstwin; /* wrap around */
288 win_goto(wp);
290 break;
292 /* cursor to window below */
293 case 'j':
294 case K_DOWN:
295 case Ctrl_J:
296 CHECK_CMDWIN
297 #ifdef FEAT_VERTSPLIT
298 win_goto_ver(FALSE, Prenum1);
299 #else
300 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
301 wp = wp->w_next)
303 win_goto(wp);
304 #endif
305 break;
307 /* cursor to window above */
308 case 'k':
309 case K_UP:
310 case Ctrl_K:
311 CHECK_CMDWIN
312 #ifdef FEAT_VERTSPLIT
313 win_goto_ver(TRUE, Prenum1);
314 #else
315 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
316 wp = wp->w_prev)
318 win_goto(wp);
319 #endif
320 break;
322 #ifdef FEAT_VERTSPLIT
323 /* cursor to left window */
324 case 'h':
325 case K_LEFT:
326 case Ctrl_H:
327 case K_BS:
328 CHECK_CMDWIN
329 win_goto_hor(TRUE, Prenum1);
330 break;
332 /* cursor to right window */
333 case 'l':
334 case K_RIGHT:
335 case Ctrl_L:
336 CHECK_CMDWIN
337 win_goto_hor(FALSE, Prenum1);
338 break;
339 #endif
341 /* move window to new tab page */
342 case 'T':
343 if (one_window())
344 MSG(_(m_onlyone));
345 else
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
352 wp = curwin;
353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
356 newtab = curtab;
357 goto_tabpage_tp(oldtab);
358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
361 goto_tabpage_tp(newtab);
364 break;
366 /* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
372 /* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
378 /* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
381 if (prevwin == NULL)
382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
387 /* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
390 CHECK_CMDWIN
391 win_exchange(Prenum);
392 break;
394 /* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
397 CHECK_CMDWIN
398 #ifdef FEAT_VISUAL
399 reset_VIsual_and_resel(); /* stop Visual mode */
400 #endif
401 win_rotate(FALSE, (int)Prenum1); /* downwards */
402 break;
404 /* rotate windows upwards */
405 case 'R':
406 CHECK_CMDWIN
407 #ifdef FEAT_VISUAL
408 reset_VIsual_and_resel(); /* stop Visual mode */
409 #endif
410 win_rotate(TRUE, (int)Prenum1); /* upwards */
411 break;
413 /* move window to the very top/bottom/left/right */
414 case 'K':
415 case 'J':
416 #ifdef FEAT_VERTSPLIT
417 case 'H':
418 case 'L':
419 #endif
420 CHECK_CMDWIN
421 win_totop((int)Prenum,
422 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
423 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
424 break;
426 /* make all windows the same height */
427 case '=':
428 #ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430 #endif
431 win_equal(NULL, FALSE, 'b');
432 break;
434 /* increase current window height */
435 case '+':
436 #ifdef FEAT_GUI
437 need_mouse_correct = TRUE;
438 #endif
439 win_setheight(curwin->w_height + (int)Prenum1);
440 break;
442 /* decrease current window height */
443 case '-':
444 #ifdef FEAT_GUI
445 need_mouse_correct = TRUE;
446 #endif
447 win_setheight(curwin->w_height - (int)Prenum1);
448 break;
450 /* set current window height */
451 case Ctrl__:
452 case '_':
453 #ifdef FEAT_GUI
454 need_mouse_correct = TRUE;
455 #endif
456 win_setheight(Prenum ? (int)Prenum : 9999);
457 break;
459 #ifdef FEAT_VERTSPLIT
460 /* increase current window width */
461 case '>':
462 #ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464 #endif
465 win_setwidth(curwin->w_width + (int)Prenum1);
466 break;
468 /* decrease current window width */
469 case '<':
470 #ifdef FEAT_GUI
471 need_mouse_correct = TRUE;
472 #endif
473 win_setwidth(curwin->w_width - (int)Prenum1);
474 break;
476 /* set current window width */
477 case '|':
478 #ifdef FEAT_GUI
479 need_mouse_correct = TRUE;
480 #endif
481 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
482 break;
483 #endif
485 /* jump to tag and split window if tag exists (in preview window) */
486 #if defined(FEAT_QUICKFIX)
487 case '}':
488 CHECK_CMDWIN
489 if (Prenum)
490 g_do_tagpreview = Prenum;
491 else
492 g_do_tagpreview = p_pvh;
493 /*FALLTHROUGH*/
494 #endif
495 case ']':
496 case Ctrl_RSB:
497 CHECK_CMDWIN
498 #ifdef FEAT_VISUAL
499 reset_VIsual_and_resel(); /* stop Visual mode */
500 #endif
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
506 /* Execute the command right here, required when
507 * "wincmd ]" was used in a function. */
508 do_nv_ident(Ctrl_RSB, NUL);
509 break;
511 #ifdef FEAT_SEARCHPATH
512 /* edit file name under cursor in a new window */
513 case 'f':
514 case 'F':
515 case Ctrl_F:
516 wingotofile:
517 CHECK_CMDWIN
519 ptr = grab_file_name(Prenum1, &lnum);
520 if (ptr != NULL)
522 # ifdef FEAT_GUI
523 need_mouse_correct = TRUE;
524 # endif
525 setpcmark();
526 if (win_split(0, 0) == OK)
528 # ifdef FEAT_SCROLLBIND
529 curwin->w_p_scb = FALSE;
530 # endif
531 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
532 ECMD_HIDE, NULL);
533 if (nchar == 'F' && lnum >= 0)
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
540 vim_free(ptr);
542 break;
543 #endif
545 #ifdef FEAT_FIND_ID
546 /* Go to the first occurrence of the identifier under cursor along path in a
547 * new window -- webb
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
555 CHECK_CMDWIN
556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563 #endif
565 case K_KENTER:
566 case CAR:
567 #if defined(FEAT_QUICKFIX)
569 * In a quickfix window a <CR> jumps to the error under the
570 * cursor in a new window.
572 if (bt_quickfix(curbuf))
574 sprintf((char *)cbuf, "split +%ld%s",
575 (long)curwin->w_cursor.lnum,
576 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
577 do_cmdline_cmd(cbuf);
579 #endif
580 break;
583 /* CTRL-W g extended commands */
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN
587 #ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; /* disallow scrolling here */
589 #endif
590 ++no_mapping;
591 ++allow_keys; /* no mapping for xchar, but allow key codes */
592 if (xchar == NUL)
593 xchar = plain_vgetc();
594 LANGMAP_ADJUST(xchar, TRUE);
595 --no_mapping;
596 --allow_keys;
597 #ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599 #endif
600 switch (xchar)
602 #if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 /*FALLTHROUGH*/
610 #endif
611 case ']':
612 case Ctrl_RSB:
613 #ifdef FEAT_VISUAL
614 reset_VIsual_and_resel(); /* stop Visual mode */
615 #endif
616 if (Prenum)
617 postponed_split = Prenum;
618 else
619 postponed_split = -1;
621 /* Execute the command right here, required when
622 * "wincmd g}" was used in a function. */
623 do_nv_ident('g', xchar);
624 break;
626 #ifdef FEAT_SEARCHPATH
627 case 'f': /* CTRL-W gf: "gf" in a new tab page */
628 case 'F': /* CTRL-W gF: "gF" in a new tab page */
629 cmdmod.tab = TRUE;
630 nchar = xchar;
631 goto wingotofile;
632 #endif
633 default:
634 beep_flush();
635 break;
637 break;
639 default: beep_flush();
640 break;
645 * split the current window, implements CTRL-W s and :split
647 * "size" is the height or width for the new window, 0 to use half of current
648 * height or width.
650 * "flags":
651 * WSP_ROOM: require enough room for new window
652 * WSP_VERT: vertical split.
653 * WSP_TOP: open window at the top-left of the shell (help window).
654 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
655 * WSP_HELP: creating the help window, keep layout snapshot
657 * return FAIL for failure, OK otherwise
660 win_split(size, flags)
661 int size;
662 int flags;
664 /* When the ":tab" modifier was used open a new tab page instead. */
665 if (may_open_tabpage() == OK)
666 return OK;
668 /* Add flags from ":vertical", ":topleft" and ":botright". */
669 flags |= cmdmod.split;
670 if ((flags & WSP_TOP) && (flags & WSP_BOT))
672 EMSG(_("E442: Can't split topleft and botright at the same time"));
673 return FAIL;
676 /* When creating the help window make a snapshot of the window layout.
677 * Otherwise clear the snapshot, it's now invalid. */
678 if (flags & WSP_HELP)
679 make_snapshot(SNAP_HELP_IDX);
680 else
681 clear_snapshot(curtab, SNAP_HELP_IDX);
683 return win_split_ins(size, flags, NULL, 0);
687 * When "newwin" is NULL: split the current window in two.
688 * When "newwin" is not NULL: insert this window at the far
689 * top/left/right/bottom.
690 * return FAIL for failure, OK otherwise
693 win_split_ins(size, flags, newwin, dir)
694 int size;
695 int flags;
696 win_T *newwin;
697 int dir;
699 win_T *wp = newwin;
700 win_T *oldwin;
701 int new_size = size;
702 int i;
703 int need_status = 0;
704 int do_equal = FALSE;
705 int needed;
706 int available;
707 int oldwin_height = 0;
708 int layout;
709 frame_T *frp, *curfrp;
710 int before;
712 if (flags & WSP_TOP)
713 oldwin = firstwin;
714 else if (flags & WSP_BOT)
715 oldwin = lastwin;
716 else
717 oldwin = curwin;
719 /* add a status line when p_ls == 1 and splitting the first window */
720 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
722 if (oldwin->w_height <= p_wmh && newwin == NULL)
724 EMSG(_(e_noroom));
725 return FAIL;
727 need_status = STATUS_HEIGHT;
730 #ifdef FEAT_GUI
731 /* May be needed for the scrollbars that are going to change. */
732 if (gui.in_use)
733 out_flush();
734 #endif
736 #ifdef FEAT_VERTSPLIT
737 if (flags & WSP_VERT)
739 layout = FR_ROW;
742 * Check if we are able to split the current window and compute its
743 * width.
745 needed = p_wmw + 1;
746 if (flags & WSP_ROOM)
747 needed += p_wiw - p_wmw;
748 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
750 available = topframe->fr_width;
751 needed += frame_minwidth(topframe, NULL);
753 else
754 available = oldwin->w_width;
755 if (available < needed && newwin == NULL)
757 EMSG(_(e_noroom));
758 return FAIL;
760 if (new_size == 0)
761 new_size = oldwin->w_width / 2;
762 if (new_size > oldwin->w_width - p_wmw - 1)
763 new_size = oldwin->w_width - p_wmw - 1;
764 if (new_size < p_wmw)
765 new_size = p_wmw;
767 /* if it doesn't fit in the current window, need win_equal() */
768 if (oldwin->w_width - new_size - 1 < p_wmw)
769 do_equal = TRUE;
771 /* We don't like to take lines for the new window from a
772 * 'winfixwidth' window. Take them from a window to the left or right
773 * instead, if possible. */
774 if (oldwin->w_p_wfw)
775 win_setwidth_win(oldwin->w_width + new_size, oldwin);
777 /* Only make all windows the same width if one of them (except oldwin)
778 * is wider than one of the split windows. */
779 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
780 && oldwin->w_frame->fr_parent != NULL)
782 frp = oldwin->w_frame->fr_parent->fr_child;
783 while (frp != NULL)
785 if (frp->fr_win != oldwin && frp->fr_win != NULL
786 && (frp->fr_win->w_width > new_size
787 || frp->fr_win->w_width > oldwin->w_width
788 - new_size - STATUS_HEIGHT))
790 do_equal = TRUE;
791 break;
793 frp = frp->fr_next;
797 else
798 #endif
800 layout = FR_COL;
803 * Check if we are able to split the current window and compute its
804 * height.
806 needed = p_wmh + STATUS_HEIGHT + need_status;
807 if (flags & WSP_ROOM)
808 needed += p_wh - p_wmh;
809 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
811 available = topframe->fr_height;
812 needed += frame_minheight(topframe, NULL);
814 else
816 available = oldwin->w_height;
817 needed += p_wmh;
819 if (available < needed && newwin == NULL)
821 EMSG(_(e_noroom));
822 return FAIL;
824 oldwin_height = oldwin->w_height;
825 if (need_status)
827 oldwin->w_status_height = STATUS_HEIGHT;
828 oldwin_height -= STATUS_HEIGHT;
830 if (new_size == 0)
831 new_size = oldwin_height / 2;
833 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
834 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
835 if (new_size < p_wmh)
836 new_size = p_wmh;
838 /* if it doesn't fit in the current window, need win_equal() */
839 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
840 do_equal = TRUE;
842 /* We don't like to take lines for the new window from a
843 * 'winfixheight' window. Take them from a window above or below
844 * instead, if possible. */
845 if (oldwin->w_p_wfh)
847 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
848 oldwin);
849 oldwin_height = oldwin->w_height;
850 if (need_status)
851 oldwin_height -= STATUS_HEIGHT;
854 /* Only make all windows the same height if one of them (except oldwin)
855 * is higher than one of the split windows. */
856 if (!do_equal && p_ea && size == 0
857 #ifdef FEAT_VERTSPLIT
858 && *p_ead != 'h'
859 #endif
860 && oldwin->w_frame->fr_parent != NULL)
862 frp = oldwin->w_frame->fr_parent->fr_child;
863 while (frp != NULL)
865 if (frp->fr_win != oldwin && frp->fr_win != NULL
866 && (frp->fr_win->w_height > new_size
867 || frp->fr_win->w_height > oldwin_height - new_size
868 - STATUS_HEIGHT))
870 do_equal = TRUE;
871 break;
873 frp = frp->fr_next;
879 * allocate new window structure and link it in the window list
881 if ((flags & WSP_TOP) == 0
882 && ((flags & WSP_BOT)
883 || (flags & WSP_BELOW)
884 || (!(flags & WSP_ABOVE)
885 && (
886 #ifdef FEAT_VERTSPLIT
887 (flags & WSP_VERT) ? p_spr :
888 #endif
889 p_sb))))
891 /* new window below/right of current one */
892 if (newwin == NULL)
893 wp = win_alloc(oldwin, FALSE);
894 else
895 win_append(oldwin, wp);
897 else
899 if (newwin == NULL)
900 wp = win_alloc(oldwin->w_prev, FALSE);
901 else
902 win_append(oldwin->w_prev, wp);
905 if (newwin == NULL)
907 if (wp == NULL)
908 return FAIL;
910 new_frame(wp);
911 if (wp->w_frame == NULL)
913 win_free(wp, NULL);
914 return FAIL;
917 /* make the contents of the new window the same as the current one */
918 win_init(wp, curwin, flags);
922 * Reorganise the tree of frames to insert the new window.
924 if (flags & (WSP_TOP | WSP_BOT))
926 #ifdef FEAT_VERTSPLIT
927 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
928 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
929 #else
930 if (topframe->fr_layout == FR_COL)
931 #endif
933 curfrp = topframe->fr_child;
934 if (flags & WSP_BOT)
935 while (curfrp->fr_next != NULL)
936 curfrp = curfrp->fr_next;
938 else
939 curfrp = topframe;
940 before = (flags & WSP_TOP);
942 else
944 curfrp = oldwin->w_frame;
945 if (flags & WSP_BELOW)
946 before = FALSE;
947 else if (flags & WSP_ABOVE)
948 before = TRUE;
949 else
950 #ifdef FEAT_VERTSPLIT
951 if (flags & WSP_VERT)
952 before = !p_spr;
953 else
954 #endif
955 before = !p_sb;
957 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
959 /* Need to create a new frame in the tree to make a branch. */
960 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
961 *frp = *curfrp;
962 curfrp->fr_layout = layout;
963 frp->fr_parent = curfrp;
964 frp->fr_next = NULL;
965 frp->fr_prev = NULL;
966 curfrp->fr_child = frp;
967 curfrp->fr_win = NULL;
968 curfrp = frp;
969 if (frp->fr_win != NULL)
970 oldwin->w_frame = frp;
971 else
972 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
973 frp->fr_parent = curfrp;
976 if (newwin == NULL)
977 frp = wp->w_frame;
978 else
979 frp = newwin->w_frame;
980 frp->fr_parent = curfrp->fr_parent;
982 /* Insert the new frame at the right place in the frame list. */
983 if (before)
984 frame_insert(curfrp, frp);
985 else
986 frame_append(curfrp, frp);
988 #ifdef FEAT_VERTSPLIT
989 if (flags & WSP_VERT)
991 wp->w_p_scr = curwin->w_p_scr;
992 if (need_status)
994 --oldwin->w_height;
995 oldwin->w_status_height = need_status;
997 if (flags & (WSP_TOP | WSP_BOT))
999 /* set height and row of new window to full height */
1000 wp->w_winrow = tabline_height();
1001 wp->w_height = curfrp->fr_height - (p_ls > 0);
1002 wp->w_status_height = (p_ls > 0);
1004 else
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
1008 wp->w_height = oldwin->w_height;
1009 wp->w_status_height = oldwin->w_status_height;
1011 frp->fr_height = curfrp->fr_height;
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
1015 wp->w_width = new_size;
1016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1023 if (flags & (WSP_TOP | WSP_BOT))
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
1029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
1032 else
1033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1034 if (before) /* new window left of current one */
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1044 else
1045 #endif
1047 /* width and column of new window is same as current window */
1048 #ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1051 wp->w_wincol = 0;
1052 wp->w_width = Columns;
1053 wp->w_vsep_width = 0;
1055 else
1057 wp->w_wincol = oldwin->w_wincol;
1058 wp->w_width = oldwin->w_width;
1059 wp->w_vsep_width = oldwin->w_vsep_width;
1061 frp->fr_width = curfrp->fr_width;
1062 #endif
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1078 else /* new window below current one */
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1084 #ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087 #endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1096 * Both windows need redrawing
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1103 if (need_status)
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1114 * make the new window the current window and redraw
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118 #ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121 #endif
1122 'v');
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126 #ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1133 # ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137 # endif
1139 else
1140 #endif
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1146 win_enter(wp, FALSE);
1147 #ifdef FEAT_VERTSPLIT
1148 if (flags & WSP_VERT)
1149 p_wiw = i;
1150 else
1151 #endif
1152 p_wh = i;
1154 return OK;
1159 * Initialize window "newp" from window "oldp".
1160 * Used when splitting a window and when creating a new tab page.
1161 * The windows will both edit the same buffer.
1162 * WSP_NEWLOC may be specified in flags to prevent the location list from
1163 * being copied.
1165 static void
1166 win_init(newp, oldp, flags)
1167 win_T *newp;
1168 win_T *oldp;
1169 int flags UNUSED;
1171 int i;
1173 newp->w_buffer = oldp->w_buffer;
1174 oldp->w_buffer->b_nwindows++;
1175 newp->w_cursor = oldp->w_cursor;
1176 newp->w_valid = 0;
1177 newp->w_curswant = oldp->w_curswant;
1178 newp->w_set_curswant = oldp->w_set_curswant;
1179 newp->w_topline = oldp->w_topline;
1180 #ifdef FEAT_DIFF
1181 newp->w_topfill = oldp->w_topfill;
1182 #endif
1183 newp->w_leftcol = oldp->w_leftcol;
1184 newp->w_pcmark = oldp->w_pcmark;
1185 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1186 newp->w_alt_fnum = oldp->w_alt_fnum;
1187 newp->w_wrow = oldp->w_wrow;
1188 newp->w_fraction = oldp->w_fraction;
1189 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1190 #ifdef FEAT_JUMPLIST
1191 copy_jumplist(oldp, newp);
1192 #endif
1193 #ifdef FEAT_QUICKFIX
1194 if (flags & WSP_NEWLOC)
1196 /* Don't copy the location list. */
1197 newp->w_llist = NULL;
1198 newp->w_llist_ref = NULL;
1200 else
1201 copy_loclist(oldp, newp);
1202 #endif
1203 if (oldp->w_localdir != NULL)
1204 newp->w_localdir = vim_strsave(oldp->w_localdir);
1206 /* copy tagstack and folds */
1207 for (i = 0; i < oldp->w_tagstacklen; i++)
1209 newp->w_tagstack[i] = oldp->w_tagstack[i];
1210 if (newp->w_tagstack[i].tagname != NULL)
1211 newp->w_tagstack[i].tagname =
1212 vim_strsave(newp->w_tagstack[i].tagname);
1214 newp->w_tagstackidx = oldp->w_tagstackidx;
1215 newp->w_tagstacklen = oldp->w_tagstacklen;
1216 # ifdef FEAT_FOLDING
1217 copyFoldingState(oldp, newp);
1218 # endif
1220 win_init_some(newp, oldp);
1224 * Initialize window "newp" from window"old".
1225 * Only the essential things are copied.
1227 static void
1228 win_init_some(newp, oldp)
1229 win_T *newp;
1230 win_T *oldp;
1232 /* Use the same argument list. */
1233 newp->w_alist = oldp->w_alist;
1234 ++newp->w_alist->al_refcount;
1235 newp->w_arg_idx = oldp->w_arg_idx;
1237 /* copy options from existing window */
1238 win_copy_options(oldp, newp);
1241 #endif /* FEAT_WINDOWS */
1243 #if defined(FEAT_WINDOWS) || defined(PROTO)
1245 * Check if "win" is a pointer to an existing window.
1248 win_valid(win)
1249 win_T *win;
1251 win_T *wp;
1253 if (win == NULL)
1254 return FALSE;
1255 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1256 if (wp == win)
1257 return TRUE;
1258 return FALSE;
1262 * Return the number of windows.
1265 win_count()
1267 win_T *wp;
1268 int count = 0;
1270 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1271 ++count;
1272 return count;
1276 * Make "count" windows on the screen.
1277 * Return actual number of windows on the screen.
1278 * Must be called when there is just one window, filling the whole screen
1279 * (excluding the command line).
1282 make_windows(count, vertical)
1283 int count;
1284 int vertical UNUSED; /* split windows vertically if TRUE */
1286 int maxcount;
1287 int todo;
1289 #ifdef FEAT_VERTSPLIT
1290 if (vertical)
1292 /* Each windows needs at least 'winminwidth' lines and a separator
1293 * column. */
1294 maxcount = (curwin->w_width + curwin->w_vsep_width
1295 - (p_wiw - p_wmw)) / (p_wmw + 1);
1297 else
1298 #endif
1300 /* Each window needs at least 'winminheight' lines and a status line. */
1301 maxcount = (curwin->w_height + curwin->w_status_height
1302 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1305 if (maxcount < 2)
1306 maxcount = 2;
1307 if (count > maxcount)
1308 count = maxcount;
1311 * add status line now, otherwise first window will be too big
1313 if (count > 1)
1314 last_status(TRUE);
1316 #ifdef FEAT_AUTOCMD
1318 * Don't execute autocommands while creating the windows. Must do that
1319 * when putting the buffers in the windows.
1321 block_autocmds();
1322 #endif
1324 /* todo is number of windows left to create */
1325 for (todo = count - 1; todo > 0; --todo)
1326 #ifdef FEAT_VERTSPLIT
1327 if (vertical)
1329 if (win_split(curwin->w_width - (curwin->w_width - todo)
1330 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1331 break;
1333 else
1334 #endif
1336 if (win_split(curwin->w_height - (curwin->w_height - todo
1337 * STATUS_HEIGHT) / (todo + 1)
1338 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1339 break;
1342 #ifdef FEAT_AUTOCMD
1343 unblock_autocmds();
1344 #endif
1346 /* return actual number of windows */
1347 return (count - todo);
1351 * Exchange current and next window
1353 static void
1354 win_exchange(Prenum)
1355 long Prenum;
1357 frame_T *frp;
1358 frame_T *frp2;
1359 win_T *wp;
1360 win_T *wp2;
1361 int temp;
1363 if (lastwin == firstwin) /* just one window */
1365 beep_flush();
1366 return;
1369 #ifdef FEAT_GUI
1370 need_mouse_correct = TRUE;
1371 #endif
1374 * find window to exchange with
1376 if (Prenum)
1378 frp = curwin->w_frame->fr_parent->fr_child;
1379 while (frp != NULL && --Prenum > 0)
1380 frp = frp->fr_next;
1382 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1383 frp = curwin->w_frame->fr_next;
1384 else /* Swap last window in row/col with previous */
1385 frp = curwin->w_frame->fr_prev;
1387 /* We can only exchange a window with another window, not with a frame
1388 * containing windows. */
1389 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1390 return;
1391 wp = frp->fr_win;
1394 * 1. remove curwin from the list. Remember after which window it was in wp2
1395 * 2. insert curwin before wp in the list
1396 * if wp != wp2
1397 * 3. remove wp from the list
1398 * 4. insert wp after wp2
1399 * 5. exchange the status line height and vsep width.
1401 wp2 = curwin->w_prev;
1402 frp2 = curwin->w_frame->fr_prev;
1403 if (wp->w_prev != curwin)
1405 win_remove(curwin, NULL);
1406 frame_remove(curwin->w_frame);
1407 win_append(wp->w_prev, curwin);
1408 frame_insert(frp, curwin->w_frame);
1410 if (wp != wp2)
1412 win_remove(wp, NULL);
1413 frame_remove(wp->w_frame);
1414 win_append(wp2, wp);
1415 if (frp2 == NULL)
1416 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1417 else
1418 frame_append(frp2, wp->w_frame);
1420 temp = curwin->w_status_height;
1421 curwin->w_status_height = wp->w_status_height;
1422 wp->w_status_height = temp;
1423 #ifdef FEAT_VERTSPLIT
1424 temp = curwin->w_vsep_width;
1425 curwin->w_vsep_width = wp->w_vsep_width;
1426 wp->w_vsep_width = temp;
1428 /* If the windows are not in the same frame, exchange the sizes to avoid
1429 * messing up the window layout. Otherwise fix the frame sizes. */
1430 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1432 temp = curwin->w_height;
1433 curwin->w_height = wp->w_height;
1434 wp->w_height = temp;
1435 temp = curwin->w_width;
1436 curwin->w_width = wp->w_width;
1437 wp->w_width = temp;
1439 else
1441 frame_fix_height(curwin);
1442 frame_fix_height(wp);
1443 frame_fix_width(curwin);
1444 frame_fix_width(wp);
1446 #endif
1448 (void)win_comp_pos(); /* recompute window positions */
1450 win_enter(wp, TRUE);
1451 redraw_later(CLEAR);
1455 * rotate windows: if upwards TRUE the second window becomes the first one
1456 * if upwards FALSE the first window becomes the second one
1458 static void
1459 win_rotate(upwards, count)
1460 int upwards;
1461 int count;
1463 win_T *wp1;
1464 win_T *wp2;
1465 frame_T *frp;
1466 int n;
1468 if (firstwin == lastwin) /* nothing to do */
1470 beep_flush();
1471 return;
1474 #ifdef FEAT_GUI
1475 need_mouse_correct = TRUE;
1476 #endif
1478 #ifdef FEAT_VERTSPLIT
1479 /* Check if all frames in this row/col have one window. */
1480 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1481 frp = frp->fr_next)
1482 if (frp->fr_win == NULL)
1484 EMSG(_("E443: Cannot rotate when another window is split"));
1485 return;
1487 #endif
1489 while (count--)
1491 if (upwards) /* first window becomes last window */
1493 /* remove first window/frame from the list */
1494 frp = curwin->w_frame->fr_parent->fr_child;
1495 wp1 = frp->fr_win;
1496 win_remove(wp1, NULL);
1497 frame_remove(frp);
1499 /* find last frame and append removed window/frame after it */
1500 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1502 win_append(frp->fr_win, wp1);
1503 frame_append(frp, wp1->w_frame);
1505 wp2 = frp->fr_win; /* previously last window */
1507 else /* last window becomes first window */
1509 /* find last window/frame in the list and remove it */
1510 for (frp = curwin->w_frame; frp->fr_next != NULL;
1511 frp = frp->fr_next)
1513 wp1 = frp->fr_win;
1514 wp2 = wp1->w_prev; /* will become last window */
1515 win_remove(wp1, NULL);
1516 frame_remove(frp);
1518 /* append the removed window/frame before the first in the list */
1519 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1520 frame_insert(frp->fr_parent->fr_child, frp);
1523 /* exchange status height and vsep width of old and new last window */
1524 n = wp2->w_status_height;
1525 wp2->w_status_height = wp1->w_status_height;
1526 wp1->w_status_height = n;
1527 frame_fix_height(wp1);
1528 frame_fix_height(wp2);
1529 #ifdef FEAT_VERTSPLIT
1530 n = wp2->w_vsep_width;
1531 wp2->w_vsep_width = wp1->w_vsep_width;
1532 wp1->w_vsep_width = n;
1533 frame_fix_width(wp1);
1534 frame_fix_width(wp2);
1535 #endif
1537 /* recompute w_winrow and w_wincol for all windows */
1538 (void)win_comp_pos();
1541 redraw_later(CLEAR);
1545 * Move the current window to the very top/bottom/left/right of the screen.
1547 static void
1548 win_totop(size, flags)
1549 int size;
1550 int flags;
1552 int dir;
1553 int height = curwin->w_height;
1555 if (lastwin == firstwin)
1557 beep_flush();
1558 return;
1561 /* Remove the window and frame from the tree of frames. */
1562 (void)winframe_remove(curwin, &dir, NULL);
1563 win_remove(curwin, NULL);
1564 last_status(FALSE); /* may need to remove last status line */
1565 (void)win_comp_pos(); /* recompute window positions */
1567 /* Split a window on the desired side and put the window there. */
1568 (void)win_split_ins(size, flags, curwin, dir);
1569 if (!(flags & WSP_VERT))
1571 win_setheight(height);
1572 if (p_ea)
1573 win_equal(curwin, TRUE, 'v');
1576 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1577 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1578 * scrollbars. Have to update them anyway. */
1579 gui_may_update_scrollbars();
1580 #endif
1584 * Move window "win1" to below/right of "win2" and make "win1" the current
1585 * window. Only works within the same frame!
1587 void
1588 win_move_after(win1, win2)
1589 win_T *win1, *win2;
1591 int height;
1593 /* check if the arguments are reasonable */
1594 if (win1 == win2)
1595 return;
1597 /* check if there is something to do */
1598 if (win2->w_next != win1)
1600 /* may need move the status line/vertical separator of the last window
1601 * */
1602 if (win1 == lastwin)
1604 height = win1->w_prev->w_status_height;
1605 win1->w_prev->w_status_height = win1->w_status_height;
1606 win1->w_status_height = height;
1607 #ifdef FEAT_VERTSPLIT
1608 if (win1->w_prev->w_vsep_width == 1)
1610 /* Remove the vertical separator from the last-but-one window,
1611 * add it to the last window. Adjust the frame widths. */
1612 win1->w_prev->w_vsep_width = 0;
1613 win1->w_prev->w_frame->fr_width -= 1;
1614 win1->w_vsep_width = 1;
1615 win1->w_frame->fr_width += 1;
1617 #endif
1619 else if (win2 == lastwin)
1621 height = win1->w_status_height;
1622 win1->w_status_height = win2->w_status_height;
1623 win2->w_status_height = height;
1624 #ifdef FEAT_VERTSPLIT
1625 if (win1->w_vsep_width == 1)
1627 /* Remove the vertical separator from win1, add it to the last
1628 * window, win2. Adjust the frame widths. */
1629 win2->w_vsep_width = 1;
1630 win2->w_frame->fr_width += 1;
1631 win1->w_vsep_width = 0;
1632 win1->w_frame->fr_width -= 1;
1634 #endif
1636 win_remove(win1, NULL);
1637 frame_remove(win1->w_frame);
1638 win_append(win2, win1);
1639 frame_append(win2->w_frame, win1->w_frame);
1641 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1642 redraw_later(NOT_VALID);
1644 win_enter(win1, FALSE);
1648 * Make all windows the same height.
1649 * 'next_curwin' will soon be the current window, make sure it has enough
1650 * rows.
1652 void
1653 win_equal(next_curwin, current, dir)
1654 win_T *next_curwin; /* pointer to current window to be or NULL */
1655 int current; /* do only frame with current window */
1656 int dir; /* 'v' for vertically, 'h' for horizontally,
1657 'b' for both, 0 for using p_ead */
1659 if (dir == 0)
1660 #ifdef FEAT_VERTSPLIT
1661 dir = *p_ead;
1662 #else
1663 dir = 'b';
1664 #endif
1665 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1666 topframe, dir, 0, tabline_height(),
1667 (int)Columns, topframe->fr_height);
1671 * Set a frame to a new position and height, spreading the available room
1672 * equally over contained frames.
1673 * The window "next_curwin" (if not NULL) should at least get the size from
1674 * 'winheight' and 'winwidth' if possible.
1676 static void
1677 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1678 win_T *next_curwin; /* pointer to current window to be or NULL */
1679 int current; /* do only frame with current window */
1680 frame_T *topfr; /* frame to set size off */
1681 int dir; /* 'v', 'h' or 'b', see win_equal() */
1682 int col; /* horizontal position for frame */
1683 int row; /* vertical position for frame */
1684 int width; /* new width of frame */
1685 int height; /* new height of frame */
1687 int n, m;
1688 int extra_sep = 0;
1689 int wincount, totwincount = 0;
1690 frame_T *fr;
1691 int next_curwin_size = 0;
1692 int room = 0;
1693 int new_size;
1694 int has_next_curwin = 0;
1695 int hnc;
1697 if (topfr->fr_layout == FR_LEAF)
1699 /* Set the width/height of this frame.
1700 * Redraw when size or position changes */
1701 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1702 #ifdef FEAT_VERTSPLIT
1703 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1704 #endif
1707 topfr->fr_win->w_winrow = row;
1708 frame_new_height(topfr, height, FALSE, FALSE);
1709 #ifdef FEAT_VERTSPLIT
1710 topfr->fr_win->w_wincol = col;
1711 frame_new_width(topfr, width, FALSE, FALSE);
1712 #endif
1713 redraw_all_later(CLEAR);
1716 #ifdef FEAT_VERTSPLIT
1717 else if (topfr->fr_layout == FR_ROW)
1719 topfr->fr_width = width;
1720 topfr->fr_height = height;
1722 if (dir != 'v') /* equalize frame widths */
1724 /* Compute the maximum number of windows horizontally in this
1725 * frame. */
1726 n = frame_minwidth(topfr, NOWIN);
1727 /* add one for the rightmost window, it doesn't have a separator */
1728 if (col + width == Columns)
1729 extra_sep = 1;
1730 else
1731 extra_sep = 0;
1732 totwincount = (n + extra_sep) / (p_wmw + 1);
1733 has_next_curwin = frame_has_win(topfr, next_curwin);
1736 * Compute width for "next_curwin" window and room available for
1737 * other windows.
1738 * "m" is the minimal width when counting p_wiw for "next_curwin".
1740 m = frame_minwidth(topfr, next_curwin);
1741 room = width - m;
1742 if (room < 0)
1744 next_curwin_size = p_wiw + room;
1745 room = 0;
1747 else
1749 next_curwin_size = -1;
1750 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1752 /* If 'winfixwidth' set keep the window width if
1753 * possible.
1754 * Watch out for this window being the next_curwin. */
1755 if (frame_fixed_width(fr))
1757 n = frame_minwidth(fr, NOWIN);
1758 new_size = fr->fr_width;
1759 if (frame_has_win(fr, next_curwin))
1761 room += p_wiw - p_wmw;
1762 next_curwin_size = 0;
1763 if (new_size < p_wiw)
1764 new_size = p_wiw;
1766 else
1767 /* These windows don't use up room. */
1768 totwincount -= (n + (fr->fr_next == NULL
1769 ? extra_sep : 0)) / (p_wmw + 1);
1770 room -= new_size - n;
1771 if (room < 0)
1773 new_size += room;
1774 room = 0;
1776 fr->fr_newwidth = new_size;
1779 if (next_curwin_size == -1)
1781 if (!has_next_curwin)
1782 next_curwin_size = 0;
1783 else if (totwincount > 1
1784 && (room + (totwincount - 2))
1785 / (totwincount - 1) > p_wiw)
1787 /* Can make all windows wider than 'winwidth', spread
1788 * the room equally. */
1789 next_curwin_size = (room + p_wiw
1790 + (totwincount - 1) * p_wmw
1791 + (totwincount - 1)) / totwincount;
1792 room -= next_curwin_size - p_wiw;
1794 else
1795 next_curwin_size = p_wiw;
1799 if (has_next_curwin)
1800 --totwincount; /* don't count curwin */
1803 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1805 n = m = 0;
1806 wincount = 1;
1807 if (fr->fr_next == NULL)
1808 /* last frame gets all that remains (avoid roundoff error) */
1809 new_size = width;
1810 else if (dir == 'v')
1811 new_size = fr->fr_width;
1812 else if (frame_fixed_width(fr))
1814 new_size = fr->fr_newwidth;
1815 wincount = 0; /* doesn't count as a sizeable window */
1817 else
1819 /* Compute the maximum number of windows horiz. in "fr". */
1820 n = frame_minwidth(fr, NOWIN);
1821 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1822 / (p_wmw + 1);
1823 m = frame_minwidth(fr, next_curwin);
1824 if (has_next_curwin)
1825 hnc = frame_has_win(fr, next_curwin);
1826 else
1827 hnc = FALSE;
1828 if (hnc) /* don't count next_curwin */
1829 --wincount;
1830 if (totwincount == 0)
1831 new_size = room;
1832 else
1833 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1834 / totwincount;
1835 if (hnc) /* add next_curwin size */
1837 next_curwin_size -= p_wiw - (m - n);
1838 new_size += next_curwin_size;
1839 room -= new_size - next_curwin_size;
1841 else
1842 room -= new_size;
1843 new_size += n;
1846 /* Skip frame that is full width when splitting or closing a
1847 * window, unless equalizing all frames. */
1848 if (!current || dir != 'v' || topfr->fr_parent != NULL
1849 || (new_size != fr->fr_width)
1850 || frame_has_win(fr, next_curwin))
1851 win_equal_rec(next_curwin, current, fr, dir, col, row,
1852 new_size, height);
1853 col += new_size;
1854 width -= new_size;
1855 totwincount -= wincount;
1858 #endif
1859 else /* topfr->fr_layout == FR_COL */
1861 #ifdef FEAT_VERTSPLIT
1862 topfr->fr_width = width;
1863 #endif
1864 topfr->fr_height = height;
1866 if (dir != 'h') /* equalize frame heights */
1868 /* Compute maximum number of windows vertically in this frame. */
1869 n = frame_minheight(topfr, NOWIN);
1870 /* add one for the bottom window if it doesn't have a statusline */
1871 if (row + height == cmdline_row && p_ls == 0)
1872 extra_sep = 1;
1873 else
1874 extra_sep = 0;
1875 totwincount = (n + extra_sep) / (p_wmh + 1);
1876 has_next_curwin = frame_has_win(topfr, next_curwin);
1879 * Compute height for "next_curwin" window and room available for
1880 * other windows.
1881 * "m" is the minimal height when counting p_wh for "next_curwin".
1883 m = frame_minheight(topfr, next_curwin);
1884 room = height - m;
1885 if (room < 0)
1887 /* The room is less then 'winheight', use all space for the
1888 * current window. */
1889 next_curwin_size = p_wh + room;
1890 room = 0;
1892 else
1894 next_curwin_size = -1;
1895 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1897 /* If 'winfixheight' set keep the window height if
1898 * possible.
1899 * Watch out for this window being the next_curwin. */
1900 if (frame_fixed_height(fr))
1902 n = frame_minheight(fr, NOWIN);
1903 new_size = fr->fr_height;
1904 if (frame_has_win(fr, next_curwin))
1906 room += p_wh - p_wmh;
1907 next_curwin_size = 0;
1908 if (new_size < p_wh)
1909 new_size = p_wh;
1911 else
1912 /* These windows don't use up room. */
1913 totwincount -= (n + (fr->fr_next == NULL
1914 ? extra_sep : 0)) / (p_wmh + 1);
1915 room -= new_size - n;
1916 if (room < 0)
1918 new_size += room;
1919 room = 0;
1921 fr->fr_newheight = new_size;
1924 if (next_curwin_size == -1)
1926 if (!has_next_curwin)
1927 next_curwin_size = 0;
1928 else if (totwincount > 1
1929 && (room + (totwincount - 2))
1930 / (totwincount - 1) > p_wh)
1932 /* can make all windows higher than 'winheight',
1933 * spread the room equally. */
1934 next_curwin_size = (room + p_wh
1935 + (totwincount - 1) * p_wmh
1936 + (totwincount - 1)) / totwincount;
1937 room -= next_curwin_size - p_wh;
1939 else
1940 next_curwin_size = p_wh;
1944 if (has_next_curwin)
1945 --totwincount; /* don't count curwin */
1948 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1950 n = m = 0;
1951 wincount = 1;
1952 if (fr->fr_next == NULL)
1953 /* last frame gets all that remains (avoid roundoff error) */
1954 new_size = height;
1955 else if (dir == 'h')
1956 new_size = fr->fr_height;
1957 else if (frame_fixed_height(fr))
1959 new_size = fr->fr_newheight;
1960 wincount = 0; /* doesn't count as a sizeable window */
1962 else
1964 /* Compute the maximum number of windows vert. in "fr". */
1965 n = frame_minheight(fr, NOWIN);
1966 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1967 / (p_wmh + 1);
1968 m = frame_minheight(fr, next_curwin);
1969 if (has_next_curwin)
1970 hnc = frame_has_win(fr, next_curwin);
1971 else
1972 hnc = FALSE;
1973 if (hnc) /* don't count next_curwin */
1974 --wincount;
1975 if (totwincount == 0)
1976 new_size = room;
1977 else
1978 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1979 / totwincount;
1980 if (hnc) /* add next_curwin size */
1982 next_curwin_size -= p_wh - (m - n);
1983 new_size += next_curwin_size;
1984 room -= new_size - next_curwin_size;
1986 else
1987 room -= new_size;
1988 new_size += n;
1990 /* Skip frame that is full width when splitting or closing a
1991 * window, unless equalizing all frames. */
1992 if (!current || dir != 'h' || topfr->fr_parent != NULL
1993 || (new_size != fr->fr_height)
1994 || frame_has_win(fr, next_curwin))
1995 win_equal_rec(next_curwin, current, fr, dir, col, row,
1996 width, new_size);
1997 row += new_size;
1998 height -= new_size;
1999 totwincount -= wincount;
2005 * close all windows for buffer 'buf'
2007 void
2008 close_windows(buf, keep_curwin)
2009 buf_T *buf;
2010 int keep_curwin; /* don't close "curwin" */
2012 win_T *wp;
2013 tabpage_T *tp, *nexttp;
2014 int h = tabline_height();
2016 ++RedrawingDisabled;
2018 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2020 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2022 win_close(wp, FALSE);
2024 /* Start all over, autocommands may change the window layout. */
2025 wp = firstwin;
2027 else
2028 wp = wp->w_next;
2031 /* Also check windows in other tab pages. */
2032 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2034 nexttp = tp->tp_next;
2035 if (tp != curtab)
2036 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2037 if (wp->w_buffer == buf)
2039 win_close_othertab(wp, FALSE, tp);
2041 /* Start all over, the tab page may be closed and
2042 * autocommands may change the window layout. */
2043 nexttp = first_tabpage;
2044 break;
2048 --RedrawingDisabled;
2050 if (h != tabline_height())
2051 shell_new_rows();
2055 * Return TRUE if the current window is the only window that exists (ignoring
2056 * "aucmd_win").
2057 * Returns FALSE if there is a window, possibly in another tab page.
2059 static int
2060 last_window()
2062 return (one_window() && first_tabpage->tp_next == NULL);
2066 * Return TRUE if there is only one window other than "aucmd_win" in the
2067 * current tab page.
2069 static int
2070 one_window()
2072 #ifdef FEAT_AUTOCMD
2073 win_T *wp;
2074 int seen_one = FALSE;
2076 FOR_ALL_WINDOWS(wp)
2078 if (wp != aucmd_win)
2080 if (seen_one)
2081 return FALSE;
2082 seen_one = TRUE;
2085 return TRUE;
2086 #else
2087 return firstwin == lastwin;
2088 #endif
2092 * Close window "win". Only works for the current tab page.
2093 * If "free_buf" is TRUE related buffer may be unloaded.
2095 * called by :quit, :close, :xit, :wq and findtag()
2097 void
2098 win_close(win, free_buf)
2099 win_T *win;
2100 int free_buf;
2102 win_T *wp;
2103 #ifdef FEAT_AUTOCMD
2104 int other_buffer = FALSE;
2105 #endif
2106 int close_curwin = FALSE;
2107 int dir;
2108 int help_window = FALSE;
2109 tabpage_T *prev_curtab = curtab;
2111 if (last_window())
2113 EMSG(_("E444: Cannot close last window"));
2114 return;
2117 #ifdef FEAT_AUTOCMD
2118 if (win == aucmd_win)
2120 EMSG(_("E813: Cannot close autocmd window"));
2121 return;
2123 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2125 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2126 return;
2128 #endif
2131 * When closing the last window in a tab page first go to another tab
2132 * page and then close the window and the tab page. This avoids that
2133 * curwin and curtab are not invalid while we are freeing memory, they may
2134 * be used in GUI events.
2136 if (firstwin == lastwin)
2138 goto_tabpage_tp(alt_tabpage());
2139 redraw_tabline = TRUE;
2141 /* Safety check: Autocommands may have closed the window when jumping
2142 * to the other tab page. */
2143 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2145 int h = tabline_height();
2147 win_close_othertab(win, free_buf, prev_curtab);
2148 if (h != tabline_height())
2149 shell_new_rows();
2151 return;
2154 /* When closing the help window, try restoring a snapshot after closing
2155 * the window. Otherwise clear the snapshot, it's now invalid. */
2156 if (win->w_buffer->b_help)
2157 help_window = TRUE;
2158 else
2159 clear_snapshot(curtab, SNAP_HELP_IDX);
2161 #ifdef FEAT_AUTOCMD
2162 if (win == curwin)
2165 * Guess which window is going to be the new current window.
2166 * This may change because of the autocommands (sigh).
2168 wp = frame2win(win_altframe(win, NULL));
2171 * Be careful: If autocommands delete the window, return now.
2173 if (wp->w_buffer != curbuf)
2175 other_buffer = TRUE;
2176 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2177 if (!win_valid(win) || last_window())
2178 return;
2180 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2181 if (!win_valid(win) || last_window())
2182 return;
2183 # ifdef FEAT_EVAL
2184 /* autocmds may abort script processing */
2185 if (aborting())
2186 return;
2187 # endif
2189 #endif
2191 #ifdef FEAT_GUI
2192 /* Avoid trouble with scrollbars that are going to be deleted in
2193 * win_free(). */
2194 if (gui.in_use)
2195 out_flush();
2196 #endif
2199 * Close the link to the buffer.
2201 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2203 /* Autocommands may have closed the window already, or closed the only
2204 * other window or moved to another tab page. */
2205 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2206 return;
2208 /* Free the memory used for the window. */
2209 wp = win_free_mem(win, &dir, NULL);
2211 /* Make sure curwin isn't invalid. It can cause severe trouble when
2212 * printing an error message. For win_equal() curbuf needs to be valid
2213 * too. */
2214 if (win == curwin)
2216 curwin = wp;
2217 #ifdef FEAT_QUICKFIX
2218 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2221 * If the cursor goes to the preview or the quickfix window, try
2222 * finding another window to go to.
2224 for (;;)
2226 if (wp->w_next == NULL)
2227 wp = firstwin;
2228 else
2229 wp = wp->w_next;
2230 if (wp == curwin)
2231 break;
2232 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2234 curwin = wp;
2235 break;
2239 #endif
2240 curbuf = curwin->w_buffer;
2241 close_curwin = TRUE;
2243 if (p_ea
2244 #ifdef FEAT_VERTSPLIT
2245 && (*p_ead == 'b' || *p_ead == dir)
2246 #endif
2248 win_equal(curwin, TRUE,
2249 #ifdef FEAT_VERTSPLIT
2251 #else
2253 #endif
2255 else
2256 win_comp_pos();
2257 if (close_curwin)
2259 win_enter_ext(wp, FALSE, TRUE);
2260 #ifdef FEAT_AUTOCMD
2261 if (other_buffer)
2262 /* careful: after this wp and win may be invalid! */
2263 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2264 #endif
2268 * If last window has a status line now and we don't want one,
2269 * remove the status line.
2271 last_status(FALSE);
2273 /* After closing the help window, try restoring the window layout from
2274 * before it was opened. */
2275 if (help_window)
2276 restore_snapshot(SNAP_HELP_IDX, close_curwin);
2278 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2279 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2280 if (gui.in_use && !win_hasvertsplit())
2281 gui_init_which_components(NULL);
2282 #endif
2284 redraw_all_later(NOT_VALID);
2288 * Close window "win" in tab page "tp", which is not the current tab page.
2289 * This may be the last window ih that tab page and result in closing the tab,
2290 * thus "tp" may become invalid!
2291 * Caller must check if buffer is hidden and whether the tabline needs to be
2292 * updated.
2294 void
2295 win_close_othertab(win, free_buf, tp)
2296 win_T *win;
2297 int free_buf;
2298 tabpage_T *tp;
2300 win_T *wp;
2301 int dir;
2302 tabpage_T *ptp = NULL;
2304 /* Close the link to the buffer. */
2305 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2307 /* Careful: Autocommands may have closed the tab page or made it the
2308 * current tab page. */
2309 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2311 if (ptp == NULL || tp == curtab)
2312 return;
2314 /* Autocommands may have closed the window already. */
2315 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2317 if (wp == NULL)
2318 return;
2320 /* Free the memory used for the window. */
2321 wp = win_free_mem(win, &dir, tp);
2323 /* When closing the last window in a tab page remove the tab page. */
2324 if (wp == NULL)
2326 if (tp == first_tabpage)
2327 first_tabpage = tp->tp_next;
2328 else
2330 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2331 ptp = ptp->tp_next)
2333 if (ptp == NULL)
2335 EMSG2(_(e_intern2), "win_close_othertab()");
2336 return;
2338 ptp->tp_next = tp->tp_next;
2340 free_tabpage(tp);
2345 * Free the memory used for a window.
2346 * Returns a pointer to the window that got the freed up space.
2348 static win_T *
2349 win_free_mem(win, dirp, tp)
2350 win_T *win;
2351 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2354 frame_T *frp;
2355 win_T *wp;
2357 /* Remove the window and its frame from the tree of frames. */
2358 frp = win->w_frame;
2359 wp = winframe_remove(win, dirp, tp);
2360 vim_free(frp);
2361 win_free(win, tp);
2363 /* When deleting the current window of another tab page select a new
2364 * current window. */
2365 if (tp != NULL && win == tp->tp_curwin)
2366 tp->tp_curwin = wp;
2368 return wp;
2371 #if defined(EXITFREE) || defined(PROTO)
2372 void
2373 win_free_all()
2375 int dummy;
2377 # ifdef FEAT_WINDOWS
2378 while (first_tabpage->tp_next != NULL)
2379 tabpage_close(TRUE);
2380 # endif
2382 # ifdef FEAT_AUTOCMD
2383 if (aucmd_win != NULL)
2385 (void)win_free_mem(aucmd_win, &dummy, NULL);
2386 aucmd_win = NULL;
2388 # endif
2390 while (firstwin != NULL)
2391 (void)win_free_mem(firstwin, &dummy, NULL);
2393 #endif
2396 * Remove a window and its frame from the tree of frames.
2397 * Returns a pointer to the window that got the freed up space.
2399 win_T *
2400 winframe_remove(win, dirp, tp)
2401 win_T *win;
2402 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
2403 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2405 frame_T *frp, *frp2, *frp3;
2406 frame_T *frp_close = win->w_frame;
2407 win_T *wp;
2410 * If there is only one window there is nothing to remove.
2412 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2413 return NULL;
2416 * Remove the window from its frame.
2418 frp2 = win_altframe(win, tp);
2419 wp = frame2win(frp2);
2421 /* Remove this frame from the list of frames. */
2422 frame_remove(frp_close);
2424 #ifdef FEAT_VERTSPLIT
2425 if (frp_close->fr_parent->fr_layout == FR_COL)
2427 #endif
2428 /* When 'winfixheight' is set, try to find another frame in the column
2429 * (as close to the closed frame as possible) to distribute the height
2430 * to. */
2431 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2433 frp = frp_close->fr_prev;
2434 frp3 = frp_close->fr_next;
2435 while (frp != NULL || frp3 != NULL)
2437 if (frp != NULL)
2439 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2441 frp2 = frp;
2442 wp = frp->fr_win;
2443 break;
2445 frp = frp->fr_prev;
2447 if (frp3 != NULL)
2449 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2451 frp2 = frp3;
2452 wp = frp3->fr_win;
2453 break;
2455 frp3 = frp3->fr_next;
2459 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2460 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2461 #ifdef FEAT_VERTSPLIT
2462 *dirp = 'v';
2464 else
2466 /* When 'winfixwidth' is set, try to find another frame in the column
2467 * (as close to the closed frame as possible) to distribute the width
2468 * to. */
2469 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2471 frp = frp_close->fr_prev;
2472 frp3 = frp_close->fr_next;
2473 while (frp != NULL || frp3 != NULL)
2475 if (frp != NULL)
2477 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2479 frp2 = frp;
2480 wp = frp->fr_win;
2481 break;
2483 frp = frp->fr_prev;
2485 if (frp3 != NULL)
2487 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2489 frp2 = frp3;
2490 wp = frp3->fr_win;
2491 break;
2493 frp3 = frp3->fr_next;
2497 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2498 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2499 *dirp = 'h';
2501 #endif
2503 /* If rows/columns go to a window below/right its positions need to be
2504 * updated. Can only be done after the sizes have been updated. */
2505 if (frp2 == frp_close->fr_next)
2507 int row = win->w_winrow;
2508 int col = W_WINCOL(win);
2510 frame_comp_pos(frp2, &row, &col);
2513 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2515 /* There is no other frame in this list, move its info to the parent
2516 * and remove it. */
2517 frp2->fr_parent->fr_layout = frp2->fr_layout;
2518 frp2->fr_parent->fr_child = frp2->fr_child;
2519 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2520 frp->fr_parent = frp2->fr_parent;
2521 frp2->fr_parent->fr_win = frp2->fr_win;
2522 if (frp2->fr_win != NULL)
2523 frp2->fr_win->w_frame = frp2->fr_parent;
2524 frp = frp2->fr_parent;
2525 vim_free(frp2);
2527 frp2 = frp->fr_parent;
2528 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2530 /* The frame above the parent has the same layout, have to merge
2531 * the frames into this list. */
2532 if (frp2->fr_child == frp)
2533 frp2->fr_child = frp->fr_child;
2534 frp->fr_child->fr_prev = frp->fr_prev;
2535 if (frp->fr_prev != NULL)
2536 frp->fr_prev->fr_next = frp->fr_child;
2537 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2539 frp3->fr_parent = frp2;
2540 if (frp3->fr_next == NULL)
2542 frp3->fr_next = frp->fr_next;
2543 if (frp->fr_next != NULL)
2544 frp->fr_next->fr_prev = frp3;
2545 break;
2548 vim_free(frp);
2552 return wp;
2556 * Find out which frame is going to get the freed up space when "win" is
2557 * closed.
2558 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2559 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2560 * This makes opening a window and closing it immediately keep the same window
2561 * layout.
2563 static frame_T *
2564 win_altframe(win, tp)
2565 win_T *win;
2566 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2568 frame_T *frp;
2569 int b;
2571 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2572 /* Last window in this tab page, will go to next tab page. */
2573 return alt_tabpage()->tp_curwin->w_frame;
2575 frp = win->w_frame;
2576 #ifdef FEAT_VERTSPLIT
2577 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2578 b = p_spr;
2579 else
2580 #endif
2581 b = p_sb;
2582 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2583 return frp->fr_next;
2584 return frp->fr_prev;
2588 * Return the tabpage that will be used if the current one is closed.
2590 static tabpage_T *
2591 alt_tabpage()
2593 tabpage_T *tp;
2595 /* Use the next tab page if possible. */
2596 if (curtab->tp_next != NULL)
2597 return curtab->tp_next;
2599 /* Find the last but one tab page. */
2600 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2602 return tp;
2606 * Find the left-upper window in frame "frp".
2608 static win_T *
2609 frame2win(frp)
2610 frame_T *frp;
2612 while (frp->fr_win == NULL)
2613 frp = frp->fr_child;
2614 return frp->fr_win;
2618 * Return TRUE if frame "frp" contains window "wp".
2620 static int
2621 frame_has_win(frp, wp)
2622 frame_T *frp;
2623 win_T *wp;
2625 frame_T *p;
2627 if (frp->fr_layout == FR_LEAF)
2628 return frp->fr_win == wp;
2630 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2631 if (frame_has_win(p, wp))
2632 return TRUE;
2633 return FALSE;
2637 * Set a new height for a frame. Recursively sets the height for contained
2638 * frames and windows. Caller must take care of positions.
2640 static void
2641 frame_new_height(topfrp, height, topfirst, wfh)
2642 frame_T *topfrp;
2643 int height;
2644 int topfirst; /* resize topmost contained frame first */
2645 int wfh; /* obey 'winfixheight' when there is a choice;
2646 may cause the height not to be set */
2648 frame_T *frp;
2649 int extra_lines;
2650 int h;
2652 if (topfrp->fr_win != NULL)
2654 /* Simple case: just one window. */
2655 win_new_height(topfrp->fr_win,
2656 height - topfrp->fr_win->w_status_height);
2658 #ifdef FEAT_VERTSPLIT
2659 else if (topfrp->fr_layout == FR_ROW)
2663 /* All frames in this row get the same new height. */
2664 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2666 frame_new_height(frp, height, topfirst, wfh);
2667 if (frp->fr_height > height)
2669 /* Could not fit the windows, make the whole row higher. */
2670 height = frp->fr_height;
2671 break;
2675 while (frp != NULL);
2677 #endif
2678 else /* fr_layout == FR_COL */
2680 /* Complicated case: Resize a column of frames. Resize the bottom
2681 * frame first, frames above that when needed. */
2683 frp = topfrp->fr_child;
2684 if (wfh)
2685 /* Advance past frames with one window with 'wfh' set. */
2686 while (frame_fixed_height(frp))
2688 frp = frp->fr_next;
2689 if (frp == NULL)
2690 return; /* no frame without 'wfh', give up */
2692 if (!topfirst)
2694 /* Find the bottom frame of this column */
2695 while (frp->fr_next != NULL)
2696 frp = frp->fr_next;
2697 if (wfh)
2698 /* Advance back for frames with one window with 'wfh' set. */
2699 while (frame_fixed_height(frp))
2700 frp = frp->fr_prev;
2703 extra_lines = height - topfrp->fr_height;
2704 if (extra_lines < 0)
2706 /* reduce height of contained frames, bottom or top frame first */
2707 while (frp != NULL)
2709 h = frame_minheight(frp, NULL);
2710 if (frp->fr_height + extra_lines < h)
2712 extra_lines += frp->fr_height - h;
2713 frame_new_height(frp, h, topfirst, wfh);
2715 else
2717 frame_new_height(frp, frp->fr_height + extra_lines,
2718 topfirst, wfh);
2719 break;
2721 if (topfirst)
2724 frp = frp->fr_next;
2725 while (wfh && frp != NULL && frame_fixed_height(frp));
2727 else
2730 frp = frp->fr_prev;
2731 while (wfh && frp != NULL && frame_fixed_height(frp));
2733 /* Increase "height" if we could not reduce enough frames. */
2734 if (frp == NULL)
2735 height -= extra_lines;
2738 else if (extra_lines > 0)
2740 /* increase height of bottom or top frame */
2741 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2744 topfrp->fr_height = height;
2748 * Return TRUE if height of frame "frp" should not be changed because of
2749 * the 'winfixheight' option.
2751 static int
2752 frame_fixed_height(frp)
2753 frame_T *frp;
2755 /* frame with one window: fixed height if 'winfixheight' set. */
2756 if (frp->fr_win != NULL)
2757 return frp->fr_win->w_p_wfh;
2759 if (frp->fr_layout == FR_ROW)
2761 /* The frame is fixed height if one of the frames in the row is fixed
2762 * height. */
2763 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2764 if (frame_fixed_height(frp))
2765 return TRUE;
2766 return FALSE;
2769 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2770 * frames in the row are fixed height. */
2771 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2772 if (!frame_fixed_height(frp))
2773 return FALSE;
2774 return TRUE;
2777 #ifdef FEAT_VERTSPLIT
2779 * Return TRUE if width of frame "frp" should not be changed because of
2780 * the 'winfixwidth' option.
2782 static int
2783 frame_fixed_width(frp)
2784 frame_T *frp;
2786 /* frame with one window: fixed width if 'winfixwidth' set. */
2787 if (frp->fr_win != NULL)
2788 return frp->fr_win->w_p_wfw;
2790 if (frp->fr_layout == FR_COL)
2792 /* The frame is fixed width if one of the frames in the row is fixed
2793 * width. */
2794 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2795 if (frame_fixed_width(frp))
2796 return TRUE;
2797 return FALSE;
2800 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2801 * frames in the row are fixed width. */
2802 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2803 if (!frame_fixed_width(frp))
2804 return FALSE;
2805 return TRUE;
2809 * Add a status line to windows at the bottom of "frp".
2810 * Note: Does not check if there is room!
2812 static void
2813 frame_add_statusline(frp)
2814 frame_T *frp;
2816 win_T *wp;
2818 if (frp->fr_layout == FR_LEAF)
2820 wp = frp->fr_win;
2821 if (wp->w_status_height == 0)
2823 if (wp->w_height > 0) /* don't make it negative */
2824 --wp->w_height;
2825 wp->w_status_height = STATUS_HEIGHT;
2828 else if (frp->fr_layout == FR_ROW)
2830 /* Handle all the frames in the row. */
2831 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2832 frame_add_statusline(frp);
2834 else /* frp->fr_layout == FR_COL */
2836 /* Only need to handle the last frame in the column. */
2837 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2839 frame_add_statusline(frp);
2844 * Set width of a frame. Handles recursively going through contained frames.
2845 * May remove separator line for windows at the right side (for win_close()).
2847 static void
2848 frame_new_width(topfrp, width, leftfirst, wfw)
2849 frame_T *topfrp;
2850 int width;
2851 int leftfirst; /* resize leftmost contained frame first */
2852 int wfw; /* obey 'winfixwidth' when there is a choice;
2853 may cause the width not to be set */
2855 frame_T *frp;
2856 int extra_cols;
2857 int w;
2858 win_T *wp;
2860 if (topfrp->fr_layout == FR_LEAF)
2862 /* Simple case: just one window. */
2863 wp = topfrp->fr_win;
2864 /* Find out if there are any windows right of this one. */
2865 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2866 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2867 break;
2868 if (frp->fr_parent == NULL)
2869 wp->w_vsep_width = 0;
2870 win_new_width(wp, width - wp->w_vsep_width);
2872 else if (topfrp->fr_layout == FR_COL)
2876 /* All frames in this column get the same new width. */
2877 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2879 frame_new_width(frp, width, leftfirst, wfw);
2880 if (frp->fr_width > width)
2882 /* Could not fit the windows, make whole column wider. */
2883 width = frp->fr_width;
2884 break;
2887 } while (frp != NULL);
2889 else /* fr_layout == FR_ROW */
2891 /* Complicated case: Resize a row of frames. Resize the rightmost
2892 * frame first, frames left of it when needed. */
2894 frp = topfrp->fr_child;
2895 if (wfw)
2896 /* Advance past frames with one window with 'wfw' set. */
2897 while (frame_fixed_width(frp))
2899 frp = frp->fr_next;
2900 if (frp == NULL)
2901 return; /* no frame without 'wfw', give up */
2903 if (!leftfirst)
2905 /* Find the rightmost frame of this row */
2906 while (frp->fr_next != NULL)
2907 frp = frp->fr_next;
2908 if (wfw)
2909 /* Advance back for frames with one window with 'wfw' set. */
2910 while (frame_fixed_width(frp))
2911 frp = frp->fr_prev;
2914 extra_cols = width - topfrp->fr_width;
2915 if (extra_cols < 0)
2917 /* reduce frame width, rightmost frame first */
2918 while (frp != NULL)
2920 w = frame_minwidth(frp, NULL);
2921 if (frp->fr_width + extra_cols < w)
2923 extra_cols += frp->fr_width - w;
2924 frame_new_width(frp, w, leftfirst, wfw);
2926 else
2928 frame_new_width(frp, frp->fr_width + extra_cols,
2929 leftfirst, wfw);
2930 break;
2932 if (leftfirst)
2935 frp = frp->fr_next;
2936 while (wfw && frp != NULL && frame_fixed_width(frp));
2938 else
2941 frp = frp->fr_prev;
2942 while (wfw && frp != NULL && frame_fixed_width(frp));
2944 /* Increase "width" if we could not reduce enough frames. */
2945 if (frp == NULL)
2946 width -= extra_cols;
2949 else if (extra_cols > 0)
2951 /* increase width of rightmost frame */
2952 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2955 topfrp->fr_width = width;
2959 * Add the vertical separator to windows at the right side of "frp".
2960 * Note: Does not check if there is room!
2962 static void
2963 frame_add_vsep(frp)
2964 frame_T *frp;
2966 win_T *wp;
2968 if (frp->fr_layout == FR_LEAF)
2970 wp = frp->fr_win;
2971 if (wp->w_vsep_width == 0)
2973 if (wp->w_width > 0) /* don't make it negative */
2974 --wp->w_width;
2975 wp->w_vsep_width = 1;
2978 else if (frp->fr_layout == FR_COL)
2980 /* Handle all the frames in the column. */
2981 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2982 frame_add_vsep(frp);
2984 else /* frp->fr_layout == FR_ROW */
2986 /* Only need to handle the last frame in the row. */
2987 frp = frp->fr_child;
2988 while (frp->fr_next != NULL)
2989 frp = frp->fr_next;
2990 frame_add_vsep(frp);
2995 * Set frame width from the window it contains.
2997 static void
2998 frame_fix_width(wp)
2999 win_T *wp;
3001 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3003 #endif
3006 * Set frame height from the window it contains.
3008 static void
3009 frame_fix_height(wp)
3010 win_T *wp;
3012 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3016 * Compute the minimal height for frame "topfrp".
3017 * Uses the 'winminheight' option.
3018 * When "next_curwin" isn't NULL, use p_wh for this window.
3019 * When "next_curwin" is NOWIN, don't use at least one line for the current
3020 * window.
3022 static int
3023 frame_minheight(topfrp, next_curwin)
3024 frame_T *topfrp;
3025 win_T *next_curwin;
3027 frame_T *frp;
3028 int m;
3029 #ifdef FEAT_VERTSPLIT
3030 int n;
3031 #endif
3033 if (topfrp->fr_win != NULL)
3035 if (topfrp->fr_win == next_curwin)
3036 m = p_wh + topfrp->fr_win->w_status_height;
3037 else
3039 /* window: minimal height of the window plus status line */
3040 m = p_wmh + topfrp->fr_win->w_status_height;
3041 /* Current window is minimal one line high */
3042 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3043 ++m;
3046 #ifdef FEAT_VERTSPLIT
3047 else if (topfrp->fr_layout == FR_ROW)
3049 /* get the minimal height from each frame in this row */
3050 m = 0;
3051 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3053 n = frame_minheight(frp, next_curwin);
3054 if (n > m)
3055 m = n;
3058 #endif
3059 else
3061 /* Add up the minimal heights for all frames in this column. */
3062 m = 0;
3063 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3064 m += frame_minheight(frp, next_curwin);
3067 return m;
3070 #ifdef FEAT_VERTSPLIT
3072 * Compute the minimal width for frame "topfrp".
3073 * When "next_curwin" isn't NULL, use p_wiw for this window.
3074 * When "next_curwin" is NOWIN, don't use at least one column for the current
3075 * window.
3077 static int
3078 frame_minwidth(topfrp, next_curwin)
3079 frame_T *topfrp;
3080 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3082 frame_T *frp;
3083 int m, n;
3085 if (topfrp->fr_win != NULL)
3087 if (topfrp->fr_win == next_curwin)
3088 m = p_wiw + topfrp->fr_win->w_vsep_width;
3089 else
3091 /* window: minimal width of the window plus separator column */
3092 m = p_wmw + topfrp->fr_win->w_vsep_width;
3093 /* Current window is minimal one column wide */
3094 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3095 ++m;
3098 else if (topfrp->fr_layout == FR_COL)
3100 /* get the minimal width from each frame in this column */
3101 m = 0;
3102 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3104 n = frame_minwidth(frp, next_curwin);
3105 if (n > m)
3106 m = n;
3109 else
3111 /* Add up the minimal widths for all frames in this row. */
3112 m = 0;
3113 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3114 m += frame_minwidth(frp, next_curwin);
3117 return m;
3119 #endif
3123 * Try to close all windows except current one.
3124 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3125 * used and the buffer was modified.
3127 * Used by ":bdel" and ":only".
3129 void
3130 close_others(message, forceit)
3131 int message;
3132 int forceit; /* always hide all other windows */
3134 win_T *wp;
3135 win_T *nextwp;
3136 int r;
3138 if (one_window())
3140 if (message
3141 #ifdef FEAT_AUTOCMD
3142 && !autocmd_busy
3143 #endif
3145 MSG(_(m_onlyone));
3146 return;
3149 /* Be very careful here: autocommands may change the window layout. */
3150 for (wp = firstwin; win_valid(wp); wp = nextwp)
3152 nextwp = wp->w_next;
3153 if (wp != curwin) /* don't close current window */
3156 /* Check if it's allowed to abandon this window */
3157 r = can_abandon(wp->w_buffer, forceit);
3158 #ifdef FEAT_AUTOCMD
3159 if (!win_valid(wp)) /* autocommands messed wp up */
3161 nextwp = firstwin;
3162 continue;
3164 #endif
3165 if (!r)
3167 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3168 if (message && (p_confirm || cmdmod.confirm) && p_write)
3170 dialog_changed(wp->w_buffer, FALSE);
3171 # ifdef FEAT_AUTOCMD
3172 if (!win_valid(wp)) /* autocommands messed wp up */
3174 nextwp = firstwin;
3175 continue;
3177 # endif
3179 if (bufIsChanged(wp->w_buffer))
3180 #endif
3181 continue;
3183 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3187 if (message && lastwin != firstwin)
3188 EMSG(_("E445: Other window contains changes"));
3191 #endif /* FEAT_WINDOWS */
3194 * Init the current window "curwin".
3195 * Called when a new file is being edited.
3197 void
3198 curwin_init()
3200 win_init_empty(curwin);
3203 void
3204 win_init_empty(wp)
3205 win_T *wp;
3207 redraw_win_later(wp, NOT_VALID);
3208 wp->w_lines_valid = 0;
3209 wp->w_cursor.lnum = 1;
3210 wp->w_curswant = wp->w_cursor.col = 0;
3211 #ifdef FEAT_VIRTUALEDIT
3212 wp->w_cursor.coladd = 0;
3213 #endif
3214 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3215 wp->w_pcmark.col = 0;
3216 wp->w_prev_pcmark.lnum = 0;
3217 wp->w_prev_pcmark.col = 0;
3218 wp->w_topline = 1;
3219 #ifdef FEAT_DIFF
3220 wp->w_topfill = 0;
3221 #endif
3222 wp->w_botline = 2;
3223 #ifdef FEAT_FKMAP
3224 if (wp->w_p_rl)
3225 wp->w_farsi = W_CONV + W_R_L;
3226 else
3227 wp->w_farsi = W_CONV;
3228 #endif
3232 * Allocate the first window and put an empty buffer in it.
3233 * Called from main().
3234 * Return FAIL when something goes wrong (out of memory).
3237 win_alloc_first()
3239 if (win_alloc_firstwin(NULL) == FAIL)
3240 return FAIL;
3242 #ifdef FEAT_WINDOWS
3243 first_tabpage = alloc_tabpage();
3244 if (first_tabpage == NULL)
3245 return FAIL;
3246 first_tabpage->tp_topframe = topframe;
3247 curtab = first_tabpage;
3248 #endif
3250 return OK;
3253 #if defined(FEAT_AUTOCMD) || defined(PROTO)
3255 * Init "aucmd_win". This can only be done after the first
3256 * window is fully initialized, thus it can't be in win_alloc_first().
3258 void
3259 win_alloc_aucmd_win()
3261 aucmd_win = win_alloc(NULL, TRUE);
3262 if (aucmd_win != NULL)
3264 win_init_some(aucmd_win, curwin);
3265 # ifdef FEAT_SCROLLBIND
3266 aucmd_win->w_p_scb = FALSE;
3267 # endif
3268 new_frame(aucmd_win);
3271 #endif
3274 * Allocate the first window or the first window in a new tab page.
3275 * When "oldwin" is NULL create an empty buffer for it.
3276 * When "oldwin" is not NULL copy info from it to the new window (only with
3277 * FEAT_WINDOWS).
3278 * Return FAIL when something goes wrong (out of memory).
3280 static int
3281 win_alloc_firstwin(oldwin)
3282 win_T *oldwin;
3284 curwin = win_alloc(NULL, FALSE);
3285 if (oldwin == NULL)
3287 /* Very first window, need to create an empty buffer for it and
3288 * initialize from scratch. */
3289 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3290 if (curwin == NULL || curbuf == NULL)
3291 return FAIL;
3292 curwin->w_buffer = curbuf;
3293 curbuf->b_nwindows = 1; /* there is one window */
3294 #ifdef FEAT_WINDOWS
3295 curwin->w_alist = &global_alist;
3296 #endif
3297 curwin_init(); /* init current window */
3299 #ifdef FEAT_WINDOWS
3300 else
3302 /* First window in new tab page, initialize it from "oldwin". */
3303 win_init(curwin, oldwin, 0);
3305 # ifdef FEAT_SCROLLBIND
3306 /* We don't want scroll-binding in the first window. */
3307 curwin->w_p_scb = FALSE;
3308 # endif
3310 #endif
3312 new_frame(curwin);
3313 if (curwin->w_frame == NULL)
3314 return FAIL;
3315 topframe = curwin->w_frame;
3316 #ifdef FEAT_VERTSPLIT
3317 topframe->fr_width = Columns;
3318 #endif
3319 topframe->fr_height = Rows - p_ch;
3320 topframe->fr_win = curwin;
3322 return OK;
3326 * Create a frame for window "wp".
3328 static void
3329 new_frame(win_T *wp)
3331 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3333 wp->w_frame = frp;
3334 if (frp != NULL)
3336 frp->fr_layout = FR_LEAF;
3337 frp->fr_win = wp;
3342 * Initialize the window and frame size to the maximum.
3344 void
3345 win_init_size()
3347 firstwin->w_height = ROWS_AVAIL;
3348 topframe->fr_height = ROWS_AVAIL;
3349 #ifdef FEAT_VERTSPLIT
3350 firstwin->w_width = Columns;
3351 topframe->fr_width = Columns;
3352 #endif
3355 #if defined(FEAT_WINDOWS) || defined(PROTO)
3358 * Allocate a new tabpage_T and init the values.
3359 * Returns NULL when out of memory.
3361 static tabpage_T *
3362 alloc_tabpage()
3364 tabpage_T *tp;
3366 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3367 if (tp != NULL)
3369 # ifdef FEAT_GUI
3370 int i;
3372 for (i = 0; i < 3; i++)
3373 tp->tp_prev_which_scrollbars[i] = -1;
3374 # endif
3375 # ifdef FEAT_DIFF
3376 tp->tp_diff_invalid = TRUE;
3377 # endif
3378 #ifdef FEAT_EVAL
3379 /* init t: variables */
3380 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3381 #endif
3382 tp->tp_ch_used = p_ch;
3384 return tp;
3387 void
3388 free_tabpage(tp)
3389 tabpage_T *tp;
3391 int idx;
3393 # ifdef FEAT_DIFF
3394 diff_clear(tp);
3395 # endif
3396 for (idx = 0; idx < SNAP_COUNT; ++idx)
3397 clear_snapshot(tp, idx);
3398 #ifdef FEAT_EVAL
3399 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3400 #endif
3401 vim_free(tp);
3405 * Create a new Tab page with one window.
3406 * It will edit the current buffer, like after ":split".
3407 * When "after" is 0 put it just after the current Tab page.
3408 * Otherwise put it just before tab page "after".
3409 * Return FAIL or OK.
3412 win_new_tabpage(after)
3413 int after;
3415 tabpage_T *tp = curtab;
3416 tabpage_T *newtp;
3417 int n;
3419 newtp = alloc_tabpage();
3420 if (newtp == NULL)
3421 return FAIL;
3423 /* Remember the current windows in this Tab page. */
3424 if (leave_tabpage(curbuf) == FAIL)
3426 vim_free(newtp);
3427 return FAIL;
3429 curtab = newtp;
3431 /* Create a new empty window. */
3432 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3434 /* Make the new Tab page the new topframe. */
3435 if (after == 1)
3437 /* New tab page becomes the first one. */
3438 newtp->tp_next = first_tabpage;
3439 first_tabpage = newtp;
3441 else
3443 if (after > 0)
3445 /* Put new tab page before tab page "after". */
3446 n = 2;
3447 for (tp = first_tabpage; tp->tp_next != NULL
3448 && n < after; tp = tp->tp_next)
3449 ++n;
3451 newtp->tp_next = tp->tp_next;
3452 tp->tp_next = newtp;
3454 win_init_size();
3455 firstwin->w_winrow = tabline_height();
3456 win_comp_scroll(curwin);
3458 newtp->tp_topframe = topframe;
3459 last_status(FALSE);
3461 #if defined(FEAT_GUI)
3462 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3463 * scrollbars. Have to update them anyway. */
3464 gui_may_update_scrollbars();
3465 #endif
3467 redraw_all_later(CLEAR);
3468 #ifdef FEAT_AUTOCMD
3469 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3470 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3471 #endif
3472 return OK;
3475 /* Failed, get back the previous Tab page */
3476 enter_tabpage(curtab, curbuf);
3477 return FAIL;
3481 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3482 * like with ":split".
3483 * Returns OK if a new tab page was created, FAIL otherwise.
3486 may_open_tabpage()
3488 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3490 if (n != 0)
3492 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3493 postponed_split_tab = 0;
3494 return win_new_tabpage(n);
3496 return FAIL;
3500 * Create up to "maxcount" tabpages with empty windows.
3501 * Returns the number of resulting tab pages.
3504 make_tabpages(maxcount)
3505 int maxcount;
3507 int count = maxcount;
3508 int todo;
3510 /* Limit to 'tabpagemax' tabs. */
3511 if (count > p_tpm)
3512 count = p_tpm;
3514 #ifdef FEAT_AUTOCMD
3516 * Don't execute autocommands while creating the tab pages. Must do that
3517 * when putting the buffers in the windows.
3519 block_autocmds();
3520 #endif
3522 for (todo = count - 1; todo > 0; --todo)
3523 if (win_new_tabpage(0) == FAIL)
3524 break;
3526 #ifdef FEAT_AUTOCMD
3527 unblock_autocmds();
3528 #endif
3530 /* return actual number of tab pages */
3531 return (count - todo);
3535 * Return TRUE when "tpc" points to a valid tab page.
3538 valid_tabpage(tpc)
3539 tabpage_T *tpc;
3541 tabpage_T *tp;
3543 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3544 if (tp == tpc)
3545 return TRUE;
3546 return FALSE;
3550 * Find tab page "n" (first one is 1). Returns NULL when not found.
3552 tabpage_T *
3553 find_tabpage(n)
3554 int n;
3556 tabpage_T *tp;
3557 int i = 1;
3559 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3560 ++i;
3561 return tp;
3565 * Get index of tab page "tp". First one has index 1.
3566 * When not found returns number of tab pages plus one.
3569 tabpage_index(ftp)
3570 tabpage_T *ftp;
3572 int i = 1;
3573 tabpage_T *tp;
3575 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3576 ++i;
3577 return i;
3581 * Prepare for leaving the current tab page.
3582 * When autocomands change "curtab" we don't leave the tab page and return
3583 * FAIL.
3584 * Careful: When OK is returned need to get a new tab page very very soon!
3586 static int
3587 leave_tabpage(new_curbuf)
3588 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
3589 NULL if unknown */
3591 tabpage_T *tp = curtab;
3593 #ifdef FEAT_VISUAL
3594 reset_VIsual_and_resel(); /* stop Visual mode */
3595 #endif
3596 #ifdef FEAT_AUTOCMD
3597 if (new_curbuf != curbuf)
3599 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3600 if (curtab != tp)
3601 return FAIL;
3603 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3604 if (curtab != tp)
3605 return FAIL;
3606 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3607 if (curtab != tp)
3608 return FAIL;
3609 #endif
3610 #if defined(FEAT_GUI)
3611 /* Remove the scrollbars. They may be added back later. */
3612 if (gui.in_use)
3613 gui_remove_scrollbars();
3614 #endif
3615 tp->tp_curwin = curwin;
3616 tp->tp_prevwin = prevwin;
3617 tp->tp_firstwin = firstwin;
3618 tp->tp_lastwin = lastwin;
3619 tp->tp_old_Rows = Rows;
3620 tp->tp_old_Columns = Columns;
3621 firstwin = NULL;
3622 lastwin = NULL;
3623 return OK;
3627 * Start using tab page "tp".
3628 * Only to be used after leave_tabpage() or freeing the current tab page.
3630 static void
3631 enter_tabpage(tp, old_curbuf)
3632 tabpage_T *tp;
3633 buf_T *old_curbuf UNUSED;
3635 int old_off = tp->tp_firstwin->w_winrow;
3636 win_T *next_prevwin = tp->tp_prevwin;
3638 curtab = tp;
3639 firstwin = tp->tp_firstwin;
3640 lastwin = tp->tp_lastwin;
3641 topframe = tp->tp_topframe;
3643 /* We would like doing the TabEnter event first, but we don't have a
3644 * valid current window yet, which may break some commands.
3645 * This triggers autocommands, thus may make "tp" invalid. */
3646 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3647 prevwin = next_prevwin;
3649 #ifdef FEAT_AUTOCMD
3650 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3652 if (old_curbuf != curbuf)
3653 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3654 #endif
3656 last_status(FALSE); /* status line may appear or disappear */
3657 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3658 must_redraw = CLEAR; /* need to redraw everything */
3659 #ifdef FEAT_DIFF
3660 diff_need_scrollbind = TRUE;
3661 #endif
3663 /* The tabpage line may have appeared or disappeared, may need to resize
3664 * the frames for that. When the Vim window was resized need to update
3665 * frame sizes too. Use the stored value of p_ch, so that it can be
3666 * different for each tab page. */
3667 p_ch = curtab->tp_ch_used;
3668 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3669 #ifdef FEAT_GUI_TABLINE
3670 && !gui_use_tabline()
3671 #endif
3673 shell_new_rows();
3674 #ifdef FEAT_VERTSPLIT
3675 if (curtab->tp_old_Columns != Columns && starting == 0)
3676 shell_new_columns(); /* update window widths */
3677 #endif
3679 #if defined(FEAT_GUI)
3680 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3681 * scrollbars. Have to update them anyway. */
3682 gui_may_update_scrollbars();
3683 #endif
3685 redraw_all_later(CLEAR);
3689 * Go to tab page "n". For ":tab N" and "Ngt".
3690 * When "n" is 9999 go to the last tab page.
3692 void
3693 goto_tabpage(n)
3694 int n;
3696 tabpage_T *tp;
3697 tabpage_T *ttp;
3698 int i;
3700 if (text_locked())
3702 /* Not allowed when editing the command line. */
3703 #ifdef FEAT_CMDWIN
3704 if (cmdwin_type != 0)
3705 EMSG(_(e_cmdwin));
3706 else
3707 #endif
3708 EMSG(_(e_secure));
3709 return;
3712 /* If there is only one it can't work. */
3713 if (first_tabpage->tp_next == NULL)
3715 if (n > 1)
3716 beep_flush();
3717 return;
3720 if (n == 0)
3722 /* No count, go to next tab page, wrap around end. */
3723 if (curtab->tp_next == NULL)
3724 tp = first_tabpage;
3725 else
3726 tp = curtab->tp_next;
3728 else if (n < 0)
3730 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3731 * this N times. */
3732 ttp = curtab;
3733 for (i = n; i < 0; ++i)
3735 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3736 tp = tp->tp_next)
3738 ttp = tp;
3741 else if (n == 9999)
3743 /* Go to last tab page. */
3744 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3747 else
3749 /* Go to tab page "n". */
3750 tp = find_tabpage(n);
3751 if (tp == NULL)
3753 beep_flush();
3754 return;
3758 goto_tabpage_tp(tp);
3760 #ifdef FEAT_GUI_TABLINE
3761 if (gui_use_tabline())
3762 gui_mch_set_curtab(tabpage_index(curtab));
3763 #endif
3767 * Go to tabpage "tp".
3768 * Note: doesn't update the GUI tab.
3770 void
3771 goto_tabpage_tp(tp)
3772 tabpage_T *tp;
3774 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3776 if (valid_tabpage(tp))
3777 enter_tabpage(tp, curbuf);
3778 else
3779 enter_tabpage(curtab, curbuf);
3784 * Enter window "wp" in tab page "tp".
3785 * Also updates the GUI tab.
3787 void
3788 goto_tabpage_win(tp, wp)
3789 tabpage_T *tp;
3790 win_T *wp;
3792 goto_tabpage_tp(tp);
3793 if (curtab == tp && win_valid(wp))
3795 win_enter(wp, TRUE);
3796 # ifdef FEAT_GUI_TABLINE
3797 if (gui_use_tabline())
3798 gui_mch_set_curtab(tabpage_index(curtab));
3799 # endif
3804 * Move the current tab page to before tab page "nr".
3806 void
3807 tabpage_move(nr)
3808 int nr;
3810 int n = nr;
3811 tabpage_T *tp;
3813 if (first_tabpage->tp_next == NULL)
3814 return;
3816 /* Remove the current tab page from the list of tab pages. */
3817 if (curtab == first_tabpage)
3818 first_tabpage = curtab->tp_next;
3819 else
3821 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3822 if (tp->tp_next == curtab)
3823 break;
3824 if (tp == NULL) /* "cannot happen" */
3825 return;
3826 tp->tp_next = curtab->tp_next;
3829 /* Re-insert it at the specified position. */
3830 if (n == 0)
3832 curtab->tp_next = first_tabpage;
3833 first_tabpage = curtab;
3835 else
3837 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3838 --n;
3839 curtab->tp_next = tp->tp_next;
3840 tp->tp_next = curtab;
3843 /* Need to redraw the tabline. Tab page contents doesn't change. */
3844 redraw_tabline = TRUE;
3849 * Go to another window.
3850 * When jumping to another buffer, stop Visual mode. Do this before
3851 * changing windows so we can yank the selection into the '*' register.
3852 * When jumping to another window on the same buffer, adjust its cursor
3853 * position to keep the same Visual area.
3855 void
3856 win_goto(wp)
3857 win_T *wp;
3859 if (text_locked())
3861 beep_flush();
3862 text_locked_msg();
3863 return;
3865 #ifdef FEAT_AUTOCMD
3866 if (curbuf_locked())
3867 return;
3868 #endif
3870 #ifdef FEAT_VISUAL
3871 if (wp->w_buffer != curbuf)
3872 reset_VIsual_and_resel();
3873 else if (VIsual_active)
3874 wp->w_cursor = curwin->w_cursor;
3875 #endif
3877 #ifdef FEAT_GUI
3878 need_mouse_correct = TRUE;
3879 #endif
3880 win_enter(wp, TRUE);
3883 #if defined(FEAT_PERL) || defined(PROTO)
3885 * Find window number "winnr" (counting top to bottom).
3887 win_T *
3888 win_find_nr(winnr)
3889 int winnr;
3891 win_T *wp;
3893 # ifdef FEAT_WINDOWS
3894 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3895 if (--winnr == 0)
3896 break;
3897 return wp;
3898 # else
3899 return curwin;
3900 # endif
3902 #endif
3904 #ifdef FEAT_VERTSPLIT
3906 * Move to window above or below "count" times.
3908 static void
3909 win_goto_ver(up, count)
3910 int up; /* TRUE to go to win above */
3911 long count;
3913 frame_T *fr;
3914 frame_T *nfr;
3915 frame_T *foundfr;
3917 foundfr = curwin->w_frame;
3918 while (count--)
3921 * First go upwards in the tree of frames until we find a upwards or
3922 * downwards neighbor.
3924 fr = foundfr;
3925 for (;;)
3927 if (fr == topframe)
3928 goto end;
3929 if (up)
3930 nfr = fr->fr_prev;
3931 else
3932 nfr = fr->fr_next;
3933 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3934 break;
3935 fr = fr->fr_parent;
3939 * Now go downwards to find the bottom or top frame in it.
3941 for (;;)
3943 if (nfr->fr_layout == FR_LEAF)
3945 foundfr = nfr;
3946 break;
3948 fr = nfr->fr_child;
3949 if (nfr->fr_layout == FR_ROW)
3951 /* Find the frame at the cursor row. */
3952 while (fr->fr_next != NULL
3953 && frame2win(fr)->w_wincol + fr->fr_width
3954 <= curwin->w_wincol + curwin->w_wcol)
3955 fr = fr->fr_next;
3957 if (nfr->fr_layout == FR_COL && up)
3958 while (fr->fr_next != NULL)
3959 fr = fr->fr_next;
3960 nfr = fr;
3963 end:
3964 if (foundfr != NULL)
3965 win_goto(foundfr->fr_win);
3969 * Move to left or right window.
3971 static void
3972 win_goto_hor(left, count)
3973 int left; /* TRUE to go to left win */
3974 long count;
3976 frame_T *fr;
3977 frame_T *nfr;
3978 frame_T *foundfr;
3980 foundfr = curwin->w_frame;
3981 while (count--)
3984 * First go upwards in the tree of frames until we find a left or
3985 * right neighbor.
3987 fr = foundfr;
3988 for (;;)
3990 if (fr == topframe)
3991 goto end;
3992 if (left)
3993 nfr = fr->fr_prev;
3994 else
3995 nfr = fr->fr_next;
3996 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3997 break;
3998 fr = fr->fr_parent;
4002 * Now go downwards to find the leftmost or rightmost frame in it.
4004 for (;;)
4006 if (nfr->fr_layout == FR_LEAF)
4008 foundfr = nfr;
4009 break;
4011 fr = nfr->fr_child;
4012 if (nfr->fr_layout == FR_COL)
4014 /* Find the frame at the cursor row. */
4015 while (fr->fr_next != NULL
4016 && frame2win(fr)->w_winrow + fr->fr_height
4017 <= curwin->w_winrow + curwin->w_wrow)
4018 fr = fr->fr_next;
4020 if (nfr->fr_layout == FR_ROW && left)
4021 while (fr->fr_next != NULL)
4022 fr = fr->fr_next;
4023 nfr = fr;
4026 end:
4027 if (foundfr != NULL)
4028 win_goto(foundfr->fr_win);
4030 #endif
4033 * Make window "wp" the current window.
4035 void
4036 win_enter(wp, undo_sync)
4037 win_T *wp;
4038 int undo_sync;
4040 win_enter_ext(wp, undo_sync, FALSE);
4044 * Make window wp the current window.
4045 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4046 * been closed and isn't valid.
4048 static void
4049 win_enter_ext(wp, undo_sync, curwin_invalid)
4050 win_T *wp;
4051 int undo_sync;
4052 int curwin_invalid;
4054 #ifdef FEAT_AUTOCMD
4055 int other_buffer = FALSE;
4056 #endif
4058 if (wp == curwin && !curwin_invalid) /* nothing to do */
4059 return;
4061 #ifdef FEAT_AUTOCMD
4062 if (!curwin_invalid)
4065 * Be careful: If autocommands delete the window, return now.
4067 if (wp->w_buffer != curbuf)
4069 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4070 other_buffer = TRUE;
4071 if (!win_valid(wp))
4072 return;
4074 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4075 if (!win_valid(wp))
4076 return;
4077 # ifdef FEAT_EVAL
4078 /* autocmds may abort script processing */
4079 if (aborting())
4080 return;
4081 # endif
4083 #endif
4085 /* sync undo before leaving the current buffer */
4086 if (undo_sync && curbuf != wp->w_buffer)
4087 u_sync(FALSE);
4088 /* may have to copy the buffer options when 'cpo' contains 'S' */
4089 if (wp->w_buffer != curbuf)
4090 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4091 if (!curwin_invalid)
4093 prevwin = curwin; /* remember for CTRL-W p */
4094 curwin->w_redr_status = TRUE;
4096 curwin = wp;
4097 curbuf = wp->w_buffer;
4098 check_cursor();
4099 #ifdef FEAT_VIRTUALEDIT
4100 if (!virtual_active())
4101 curwin->w_cursor.coladd = 0;
4102 #endif
4103 changed_line_abv_curs(); /* assume cursor position needs updating */
4105 if (curwin->w_localdir != NULL)
4107 /* Window has a local directory: Save current directory as global
4108 * directory (unless that was done already) and change to the local
4109 * directory. */
4110 if (globaldir == NULL)
4112 char_u cwd[MAXPATHL];
4114 if (mch_dirname(cwd, MAXPATHL) == OK)
4115 globaldir = vim_strsave(cwd);
4117 if (mch_chdir((char *)curwin->w_localdir) == 0)
4118 shorten_fnames(TRUE);
4120 else if (globaldir != NULL)
4122 /* Window doesn't have a local directory and we are not in the global
4123 * directory: Change to the global directory. */
4124 ignored = mch_chdir((char *)globaldir);
4125 vim_free(globaldir);
4126 globaldir = NULL;
4127 shorten_fnames(TRUE);
4130 #ifdef FEAT_AUTOCMD
4131 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4132 if (other_buffer)
4133 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4134 #endif
4136 #ifdef FEAT_TITLE
4137 maketitle();
4138 #endif
4139 curwin->w_redr_status = TRUE;
4140 redraw_tabline = TRUE;
4141 if (restart_edit)
4142 redraw_later(VALID); /* causes status line redraw */
4144 /* set window height to desired minimal value */
4145 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4146 win_setheight((int)p_wh);
4147 else if (curwin->w_height == 0)
4148 win_setheight(1);
4150 #ifdef FEAT_VERTSPLIT
4151 /* set window width to desired minimal value */
4152 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4153 win_setwidth((int)p_wiw);
4154 #endif
4156 #ifdef FEAT_MOUSE
4157 setmouse(); /* in case jumped to/from help buffer */
4158 #endif
4160 /* Change directories when the 'acd' option is set. */
4161 DO_AUTOCHDIR
4164 #endif /* FEAT_WINDOWS */
4166 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4168 * Jump to the first open window that contains buffer "buf", if one exists.
4169 * Returns a pointer to the window found, otherwise NULL.
4171 win_T *
4172 buf_jump_open_win(buf)
4173 buf_T *buf;
4175 # ifdef FEAT_WINDOWS
4176 win_T *wp;
4178 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4179 if (wp->w_buffer == buf)
4180 break;
4181 if (wp != NULL)
4182 win_enter(wp, FALSE);
4183 return wp;
4184 # else
4185 if (curwin->w_buffer == buf)
4186 return curwin;
4187 return NULL;
4188 # endif
4192 * Jump to the first open window in any tab page that contains buffer "buf",
4193 * if one exists.
4194 * Returns a pointer to the window found, otherwise NULL.
4196 win_T *
4197 buf_jump_open_tab(buf)
4198 buf_T *buf;
4200 # ifdef FEAT_WINDOWS
4201 win_T *wp;
4202 tabpage_T *tp;
4204 /* First try the current tab page. */
4205 wp = buf_jump_open_win(buf);
4206 if (wp != NULL)
4207 return wp;
4209 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4210 if (tp != curtab)
4212 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4213 if (wp->w_buffer == buf)
4214 break;
4215 if (wp != NULL)
4217 goto_tabpage_win(tp, wp);
4218 if (curwin != wp)
4219 wp = NULL; /* something went wrong */
4220 break;
4224 return wp;
4225 # else
4226 if (curwin->w_buffer == buf)
4227 return curwin;
4228 return NULL;
4229 # endif
4231 #endif
4234 * Allocate a window structure and link it in the window list when "hidden" is
4235 * FALSE.
4237 static win_T *
4238 win_alloc(after, hidden)
4239 win_T *after UNUSED;
4240 int hidden UNUSED;
4242 win_T *newwin;
4245 * allocate window structure and linesizes arrays
4247 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4248 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4250 vim_free(newwin);
4251 newwin = NULL;
4254 if (newwin != NULL)
4256 #ifdef FEAT_AUTOCMD
4257 /* Don't execute autocommands while the window is not properly
4258 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4259 * event. */
4260 block_autocmds();
4261 #endif
4263 * link the window in the window list
4265 #ifdef FEAT_WINDOWS
4266 if (!hidden)
4267 win_append(after, newwin);
4268 #endif
4269 #ifdef FEAT_VERTSPLIT
4270 newwin->w_wincol = 0;
4271 newwin->w_width = Columns;
4272 #endif
4274 /* position the display and the cursor at the top of the file. */
4275 newwin->w_topline = 1;
4276 #ifdef FEAT_DIFF
4277 newwin->w_topfill = 0;
4278 #endif
4279 newwin->w_botline = 2;
4280 newwin->w_cursor.lnum = 1;
4281 #ifdef FEAT_SCROLLBIND
4282 newwin->w_scbind_pos = 1;
4283 #endif
4285 /* We won't calculate w_fraction until resizing the window */
4286 newwin->w_fraction = 0;
4287 newwin->w_prev_fraction_row = -1;
4289 #ifdef FEAT_GUI
4290 if (gui.in_use)
4292 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4293 SBAR_LEFT, newwin);
4294 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4295 SBAR_RIGHT, newwin);
4297 #endif
4298 #ifdef FEAT_EVAL
4299 /* init w: variables */
4300 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4301 #endif
4302 #ifdef FEAT_FOLDING
4303 foldInitWin(newwin);
4304 #endif
4305 #ifdef FEAT_AUTOCMD
4306 unblock_autocmds();
4307 #endif
4308 #ifdef FEAT_SEARCH_EXTRA
4309 newwin->w_match_head = NULL;
4310 newwin->w_next_match_id = 4;
4311 #endif
4313 return newwin;
4316 #if defined(FEAT_WINDOWS) || defined(PROTO)
4319 * remove window 'wp' from the window list and free the structure
4321 static void
4322 win_free(wp, tp)
4323 win_T *wp;
4324 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4326 int i;
4328 #ifdef FEAT_FOLDING
4329 clearFolding(wp);
4330 #endif
4332 /* reduce the reference count to the argument list. */
4333 alist_unlink(wp->w_alist);
4335 #ifdef FEAT_AUTOCMD
4336 /* Don't execute autocommands while the window is halfway being deleted.
4337 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4338 block_autocmds();
4339 #endif
4341 #ifdef FEAT_MZSCHEME
4342 mzscheme_window_free(wp);
4343 #endif
4345 #ifdef FEAT_PERL
4346 perl_win_free(wp);
4347 #endif
4349 #ifdef FEAT_PYTHON
4350 python_window_free(wp);
4351 #endif
4353 #ifdef FEAT_TCL
4354 tcl_window_free(wp);
4355 #endif
4357 #ifdef FEAT_RUBY
4358 ruby_window_free(wp);
4359 #endif
4361 clear_winopt(&wp->w_onebuf_opt);
4362 clear_winopt(&wp->w_allbuf_opt);
4364 #ifdef FEAT_EVAL
4365 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4366 #endif
4368 if (prevwin == wp)
4369 prevwin = NULL;
4370 win_free_lsize(wp);
4372 for (i = 0; i < wp->w_tagstacklen; ++i)
4373 vim_free(wp->w_tagstack[i].tagname);
4375 vim_free(wp->w_localdir);
4377 #ifdef FEAT_SEARCH_EXTRA
4378 clear_matches(wp);
4379 #endif
4381 #ifdef FEAT_JUMPLIST
4382 free_jumplist(wp);
4383 #endif
4385 #ifdef FEAT_QUICKFIX
4386 qf_free_all(wp);
4387 #endif
4389 #ifdef FEAT_GUI
4390 if (gui.in_use)
4392 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4393 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4395 #endif /* FEAT_GUI */
4397 #ifdef FEAT_AUTOCMD
4398 if (wp != aucmd_win)
4399 #endif
4400 win_remove(wp, tp);
4401 vim_free(wp);
4403 #ifdef FEAT_AUTOCMD
4404 unblock_autocmds();
4405 #endif
4409 * Append window "wp" in the window list after window "after".
4411 void
4412 win_append(after, wp)
4413 win_T *after, *wp;
4415 win_T *before;
4417 if (after == NULL) /* after NULL is in front of the first */
4418 before = firstwin;
4419 else
4420 before = after->w_next;
4422 wp->w_next = before;
4423 wp->w_prev = after;
4424 if (after == NULL)
4425 firstwin = wp;
4426 else
4427 after->w_next = wp;
4428 if (before == NULL)
4429 lastwin = wp;
4430 else
4431 before->w_prev = wp;
4435 * Remove a window from the window list.
4437 void
4438 win_remove(wp, tp)
4439 win_T *wp;
4440 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4442 if (wp->w_prev != NULL)
4443 wp->w_prev->w_next = wp->w_next;
4444 else if (tp == NULL)
4445 firstwin = wp->w_next;
4446 else
4447 tp->tp_firstwin = wp->w_next;
4448 if (wp->w_next != NULL)
4449 wp->w_next->w_prev = wp->w_prev;
4450 else if (tp == NULL)
4451 lastwin = wp->w_prev;
4452 else
4453 tp->tp_lastwin = wp->w_prev;
4457 * Append frame "frp" in a frame list after frame "after".
4459 static void
4460 frame_append(after, frp)
4461 frame_T *after, *frp;
4463 frp->fr_next = after->fr_next;
4464 after->fr_next = frp;
4465 if (frp->fr_next != NULL)
4466 frp->fr_next->fr_prev = frp;
4467 frp->fr_prev = after;
4471 * Insert frame "frp" in a frame list before frame "before".
4473 static void
4474 frame_insert(before, frp)
4475 frame_T *before, *frp;
4477 frp->fr_next = before;
4478 frp->fr_prev = before->fr_prev;
4479 before->fr_prev = frp;
4480 if (frp->fr_prev != NULL)
4481 frp->fr_prev->fr_next = frp;
4482 else
4483 frp->fr_parent->fr_child = frp;
4487 * Remove a frame from a frame list.
4489 static void
4490 frame_remove(frp)
4491 frame_T *frp;
4493 if (frp->fr_prev != NULL)
4494 frp->fr_prev->fr_next = frp->fr_next;
4495 else
4496 frp->fr_parent->fr_child = frp->fr_next;
4497 if (frp->fr_next != NULL)
4498 frp->fr_next->fr_prev = frp->fr_prev;
4501 #endif /* FEAT_WINDOWS */
4504 * Allocate w_lines[] for window "wp".
4505 * Return FAIL for failure, OK for success.
4508 win_alloc_lines(wp)
4509 win_T *wp;
4511 wp->w_lines_valid = 0;
4512 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4513 if (wp->w_lines == NULL)
4514 return FAIL;
4515 return OK;
4519 * free lsize arrays for a window
4521 void
4522 win_free_lsize(wp)
4523 win_T *wp;
4525 vim_free(wp->w_lines);
4526 wp->w_lines = NULL;
4530 * Called from win_new_shellsize() after Rows changed.
4531 * This only does the current tab page, others must be done when made active.
4533 void
4534 shell_new_rows()
4536 int h = (int)ROWS_AVAIL;
4538 if (firstwin == NULL) /* not initialized yet */
4539 return;
4540 #ifdef FEAT_WINDOWS
4541 if (h < frame_minheight(topframe, NULL))
4542 h = frame_minheight(topframe, NULL);
4544 /* First try setting the heights of windows with 'winfixheight'. If
4545 * that doesn't result in the right height, forget about that option. */
4546 frame_new_height(topframe, h, FALSE, TRUE);
4547 if (topframe->fr_height != h)
4548 frame_new_height(topframe, h, FALSE, FALSE);
4550 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4551 #else
4552 if (h < 1)
4553 h = 1;
4554 win_new_height(firstwin, h);
4555 #endif
4556 compute_cmdrow();
4557 #ifdef FEAT_WINDOWS
4558 curtab->tp_ch_used = p_ch;
4559 #endif
4561 #if 0
4562 /* Disabled: don't want making the screen smaller make a window larger. */
4563 if (p_ea)
4564 win_equal(curwin, FALSE, 'v');
4565 #endif
4568 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4570 * Called from win_new_shellsize() after Columns changed.
4572 void
4573 shell_new_columns()
4575 if (firstwin == NULL) /* not initialized yet */
4576 return;
4578 /* First try setting the widths of windows with 'winfixwidth'. If that
4579 * doesn't result in the right width, forget about that option. */
4580 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4581 if (topframe->fr_width != Columns)
4582 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4584 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4585 #if 0
4586 /* Disabled: don't want making the screen smaller make a window larger. */
4587 if (p_ea)
4588 win_equal(curwin, FALSE, 'h');
4589 #endif
4591 #endif
4593 #if defined(FEAT_CMDWIN) || defined(PROTO)
4595 * Save the size of all windows in "gap".
4597 void
4598 win_size_save(gap)
4599 garray_T *gap;
4602 win_T *wp;
4604 ga_init2(gap, (int)sizeof(int), 1);
4605 if (ga_grow(gap, win_count() * 2) == OK)
4606 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4608 ((int *)gap->ga_data)[gap->ga_len++] =
4609 wp->w_width + wp->w_vsep_width;
4610 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4615 * Restore window sizes, but only if the number of windows is still the same.
4616 * Does not free the growarray.
4618 void
4619 win_size_restore(gap)
4620 garray_T *gap;
4622 win_T *wp;
4623 int i;
4625 if (win_count() * 2 == gap->ga_len)
4627 i = 0;
4628 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4630 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4631 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4633 /* recompute the window positions */
4634 (void)win_comp_pos();
4637 #endif /* FEAT_CMDWIN */
4639 #if defined(FEAT_WINDOWS) || defined(PROTO)
4641 * Update the position for all windows, using the width and height of the
4642 * frames.
4643 * Returns the row just after the last window.
4646 win_comp_pos()
4648 int row = tabline_height();
4649 int col = 0;
4651 frame_comp_pos(topframe, &row, &col);
4652 return row;
4656 * Update the position of the windows in frame "topfrp", using the width and
4657 * height of the frames.
4658 * "*row" and "*col" are the top-left position of the frame. They are updated
4659 * to the bottom-right position plus one.
4661 static void
4662 frame_comp_pos(topfrp, row, col)
4663 frame_T *topfrp;
4664 int *row;
4665 int *col;
4667 win_T *wp;
4668 frame_T *frp;
4669 #ifdef FEAT_VERTSPLIT
4670 int startcol;
4671 int startrow;
4672 #endif
4674 wp = topfrp->fr_win;
4675 if (wp != NULL)
4677 if (wp->w_winrow != *row
4678 #ifdef FEAT_VERTSPLIT
4679 || wp->w_wincol != *col
4680 #endif
4683 /* position changed, redraw */
4684 wp->w_winrow = *row;
4685 #ifdef FEAT_VERTSPLIT
4686 wp->w_wincol = *col;
4687 #endif
4688 redraw_win_later(wp, NOT_VALID);
4689 wp->w_redr_status = TRUE;
4691 *row += wp->w_height + wp->w_status_height;
4692 #ifdef FEAT_VERTSPLIT
4693 *col += wp->w_width + wp->w_vsep_width;
4694 #endif
4696 else
4698 #ifdef FEAT_VERTSPLIT
4699 startrow = *row;
4700 startcol = *col;
4701 #endif
4702 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4704 #ifdef FEAT_VERTSPLIT
4705 if (topfrp->fr_layout == FR_ROW)
4706 *row = startrow; /* all frames are at the same row */
4707 else
4708 *col = startcol; /* all frames are at the same col */
4709 #endif
4710 frame_comp_pos(frp, row, col);
4715 #endif /* FEAT_WINDOWS */
4718 * Set current window height and take care of repositioning other windows to
4719 * fit around it.
4721 void
4722 win_setheight(height)
4723 int height;
4725 win_setheight_win(height, curwin);
4729 * Set the window height of window "win" and take care of repositioning other
4730 * windows to fit around it.
4732 void
4733 win_setheight_win(height, win)
4734 int height;
4735 win_T *win;
4737 int row;
4739 if (win == curwin)
4741 /* Always keep current window at least one line high, even when
4742 * 'winminheight' is zero. */
4743 #ifdef FEAT_WINDOWS
4744 if (height < p_wmh)
4745 height = p_wmh;
4746 #endif
4747 if (height == 0)
4748 height = 1;
4751 #ifdef FEAT_WINDOWS
4752 frame_setheight(win->w_frame, height + win->w_status_height);
4754 /* recompute the window positions */
4755 row = win_comp_pos();
4756 #else
4757 if (height > topframe->fr_height)
4758 height = topframe->fr_height;
4759 win->w_height = height;
4760 row = height;
4761 #endif
4764 * If there is extra space created between the last window and the command
4765 * line, clear it.
4767 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4768 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4769 cmdline_row = row;
4770 msg_row = row;
4771 msg_col = 0;
4773 redraw_all_later(NOT_VALID);
4776 #if defined(FEAT_WINDOWS) || defined(PROTO)
4779 * Set the height of a frame to "height" and take care that all frames and
4780 * windows inside it are resized. Also resize frames on the left and right if
4781 * the are in the same FR_ROW frame.
4783 * Strategy:
4784 * If the frame is part of a FR_COL frame, try fitting the frame in that
4785 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4786 * go to containing frames to resize them and make room.
4787 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4788 * Check for the minimal height of the FR_ROW frame.
4789 * At the top level we can also use change the command line height.
4791 static void
4792 frame_setheight(curfrp, height)
4793 frame_T *curfrp;
4794 int height;
4796 int room; /* total number of lines available */
4797 int take; /* number of lines taken from other windows */
4798 int room_cmdline; /* lines available from cmdline */
4799 int run;
4800 frame_T *frp;
4801 int h;
4802 int room_reserved;
4804 /* If the height already is the desired value, nothing to do. */
4805 if (curfrp->fr_height == height)
4806 return;
4808 if (curfrp->fr_parent == NULL)
4810 /* topframe: can only change the command line */
4811 if (height > ROWS_AVAIL)
4812 height = ROWS_AVAIL;
4813 if (height > 0)
4814 frame_new_height(curfrp, height, FALSE, FALSE);
4816 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4818 /* Row of frames: Also need to resize frames left and right of this
4819 * one. First check for the minimal height of these. */
4820 h = frame_minheight(curfrp->fr_parent, NULL);
4821 if (height < h)
4822 height = h;
4823 frame_setheight(curfrp->fr_parent, height);
4825 else
4828 * Column of frames: try to change only frames in this column.
4830 #ifdef FEAT_VERTSPLIT
4832 * Do this twice:
4833 * 1: compute room available, if it's not enough try resizing the
4834 * containing frame.
4835 * 2: compute the room available and adjust the height to it.
4836 * Try not to reduce the height of a window with 'winfixheight' set.
4838 for (run = 1; run <= 2; ++run)
4839 #else
4840 for (;;)
4841 #endif
4843 room = 0;
4844 room_reserved = 0;
4845 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4846 frp = frp->fr_next)
4848 if (frp != curfrp
4849 && frp->fr_win != NULL
4850 && frp->fr_win->w_p_wfh)
4851 room_reserved += frp->fr_height;
4852 room += frp->fr_height;
4853 if (frp != curfrp)
4854 room -= frame_minheight(frp, NULL);
4856 #ifdef FEAT_VERTSPLIT
4857 if (curfrp->fr_width != Columns)
4858 room_cmdline = 0;
4859 else
4860 #endif
4862 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4863 + lastwin->w_height + lastwin->w_status_height);
4864 if (room_cmdline < 0)
4865 room_cmdline = 0;
4868 if (height <= room + room_cmdline)
4869 break;
4870 #ifdef FEAT_VERTSPLIT
4871 if (run == 2 || curfrp->fr_width == Columns)
4872 #endif
4874 if (height > room + room_cmdline)
4875 height = room + room_cmdline;
4876 break;
4878 #ifdef FEAT_VERTSPLIT
4879 frame_setheight(curfrp->fr_parent, height
4880 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4881 #endif
4882 /*NOTREACHED*/
4886 * Compute the number of lines we will take from others frames (can be
4887 * negative!).
4889 take = height - curfrp->fr_height;
4891 /* If there is not enough room, also reduce the height of a window
4892 * with 'winfixheight' set. */
4893 if (height > room + room_cmdline - room_reserved)
4894 room_reserved = room + room_cmdline - height;
4895 /* If there is only a 'winfixheight' window and making the
4896 * window smaller, need to make the other window taller. */
4897 if (take < 0 && room - curfrp->fr_height < room_reserved)
4898 room_reserved = 0;
4900 if (take > 0 && room_cmdline > 0)
4902 /* use lines from cmdline first */
4903 if (take < room_cmdline)
4904 room_cmdline = take;
4905 take -= room_cmdline;
4906 topframe->fr_height += room_cmdline;
4910 * set the current frame to the new height
4912 frame_new_height(curfrp, height, FALSE, FALSE);
4915 * First take lines from the frames after the current frame. If
4916 * that is not enough, takes lines from frames above the current
4917 * frame.
4919 for (run = 0; run < 2; ++run)
4921 if (run == 0)
4922 frp = curfrp->fr_next; /* 1st run: start with next window */
4923 else
4924 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4925 while (frp != NULL && take != 0)
4927 h = frame_minheight(frp, NULL);
4928 if (room_reserved > 0
4929 && frp->fr_win != NULL
4930 && frp->fr_win->w_p_wfh)
4932 if (room_reserved >= frp->fr_height)
4933 room_reserved -= frp->fr_height;
4934 else
4936 if (frp->fr_height - room_reserved > take)
4937 room_reserved = frp->fr_height - take;
4938 take -= frp->fr_height - room_reserved;
4939 frame_new_height(frp, room_reserved, FALSE, FALSE);
4940 room_reserved = 0;
4943 else
4945 if (frp->fr_height - take < h)
4947 take -= frp->fr_height - h;
4948 frame_new_height(frp, h, FALSE, FALSE);
4950 else
4952 frame_new_height(frp, frp->fr_height - take,
4953 FALSE, FALSE);
4954 take = 0;
4957 if (run == 0)
4958 frp = frp->fr_next;
4959 else
4960 frp = frp->fr_prev;
4966 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4968 * Set current window width and take care of repositioning other windows to
4969 * fit around it.
4971 void
4972 win_setwidth(width)
4973 int width;
4975 win_setwidth_win(width, curwin);
4978 void
4979 win_setwidth_win(width, wp)
4980 int width;
4981 win_T *wp;
4983 /* Always keep current window at least one column wide, even when
4984 * 'winminwidth' is zero. */
4985 if (wp == curwin)
4987 if (width < p_wmw)
4988 width = p_wmw;
4989 if (width == 0)
4990 width = 1;
4993 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4995 /* recompute the window positions */
4996 (void)win_comp_pos();
4998 redraw_all_later(NOT_VALID);
5002 * Set the width of a frame to "width" and take care that all frames and
5003 * windows inside it are resized. Also resize frames above and below if the
5004 * are in the same FR_ROW frame.
5006 * Strategy is similar to frame_setheight().
5008 static void
5009 frame_setwidth(curfrp, width)
5010 frame_T *curfrp;
5011 int width;
5013 int room; /* total number of lines available */
5014 int take; /* number of lines taken from other windows */
5015 int run;
5016 frame_T *frp;
5017 int w;
5018 int room_reserved;
5020 /* If the width already is the desired value, nothing to do. */
5021 if (curfrp->fr_width == width)
5022 return;
5024 if (curfrp->fr_parent == NULL)
5025 /* topframe: can't change width */
5026 return;
5028 if (curfrp->fr_parent->fr_layout == FR_COL)
5030 /* Column of frames: Also need to resize frames above and below of
5031 * this one. First check for the minimal width of these. */
5032 w = frame_minwidth(curfrp->fr_parent, NULL);
5033 if (width < w)
5034 width = w;
5035 frame_setwidth(curfrp->fr_parent, width);
5037 else
5040 * Row of frames: try to change only frames in this row.
5042 * Do this twice:
5043 * 1: compute room available, if it's not enough try resizing the
5044 * containing frame.
5045 * 2: compute the room available and adjust the width to it.
5047 for (run = 1; run <= 2; ++run)
5049 room = 0;
5050 room_reserved = 0;
5051 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5052 frp = frp->fr_next)
5054 if (frp != curfrp
5055 && frp->fr_win != NULL
5056 && frp->fr_win->w_p_wfw)
5057 room_reserved += frp->fr_width;
5058 room += frp->fr_width;
5059 if (frp != curfrp)
5060 room -= frame_minwidth(frp, NULL);
5063 if (width <= room)
5064 break;
5065 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5067 if (width > room)
5068 width = room;
5069 break;
5071 frame_setwidth(curfrp->fr_parent, width
5072 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5076 * Compute the number of lines we will take from others frames (can be
5077 * negative!).
5079 take = width - curfrp->fr_width;
5081 /* If there is not enough room, also reduce the width of a window
5082 * with 'winfixwidth' set. */
5083 if (width > room - room_reserved)
5084 room_reserved = room - width;
5085 /* If there is only a 'winfixwidth' window and making the
5086 * window smaller, need to make the other window narrower. */
5087 if (take < 0 && room - curfrp->fr_width < room_reserved)
5088 room_reserved = 0;
5091 * set the current frame to the new width
5093 frame_new_width(curfrp, width, FALSE, FALSE);
5096 * First take lines from the frames right of the current frame. If
5097 * that is not enough, takes lines from frames left of the current
5098 * frame.
5100 for (run = 0; run < 2; ++run)
5102 if (run == 0)
5103 frp = curfrp->fr_next; /* 1st run: start with next window */
5104 else
5105 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5106 while (frp != NULL && take != 0)
5108 w = frame_minwidth(frp, NULL);
5109 if (room_reserved > 0
5110 && frp->fr_win != NULL
5111 && frp->fr_win->w_p_wfw)
5113 if (room_reserved >= frp->fr_width)
5114 room_reserved -= frp->fr_width;
5115 else
5117 if (frp->fr_width - room_reserved > take)
5118 room_reserved = frp->fr_width - take;
5119 take -= frp->fr_width - room_reserved;
5120 frame_new_width(frp, room_reserved, FALSE, FALSE);
5121 room_reserved = 0;
5124 else
5126 if (frp->fr_width - take < w)
5128 take -= frp->fr_width - w;
5129 frame_new_width(frp, w, FALSE, FALSE);
5131 else
5133 frame_new_width(frp, frp->fr_width - take,
5134 FALSE, FALSE);
5135 take = 0;
5138 if (run == 0)
5139 frp = frp->fr_next;
5140 else
5141 frp = frp->fr_prev;
5146 #endif /* FEAT_VERTSPLIT */
5149 * Check 'winminheight' for a valid value.
5151 void
5152 win_setminheight()
5154 int room;
5155 int first = TRUE;
5156 win_T *wp;
5158 /* loop until there is a 'winminheight' that is possible */
5159 while (p_wmh > 0)
5161 /* TODO: handle vertical splits */
5162 room = -p_wh;
5163 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5164 room += wp->w_height - p_wmh;
5165 if (room >= 0)
5166 break;
5167 --p_wmh;
5168 if (first)
5170 EMSG(_(e_noroom));
5171 first = FALSE;
5176 #ifdef FEAT_MOUSE
5179 * Status line of dragwin is dragged "offset" lines down (negative is up).
5181 void
5182 win_drag_status_line(dragwin, offset)
5183 win_T *dragwin;
5184 int offset;
5186 frame_T *curfr;
5187 frame_T *fr;
5188 int room;
5189 int row;
5190 int up; /* if TRUE, drag status line up, otherwise down */
5191 int n;
5193 fr = dragwin->w_frame;
5194 curfr = fr;
5195 if (fr != topframe) /* more than one window */
5197 fr = fr->fr_parent;
5198 /* When the parent frame is not a column of frames, its parent should
5199 * be. */
5200 if (fr->fr_layout != FR_COL)
5202 curfr = fr;
5203 if (fr != topframe) /* only a row of windows, may drag statusline */
5204 fr = fr->fr_parent;
5208 /* If this is the last frame in a column, may want to resize the parent
5209 * frame instead (go two up to skip a row of frames). */
5210 while (curfr != topframe && curfr->fr_next == NULL)
5212 if (fr != topframe)
5213 fr = fr->fr_parent;
5214 curfr = fr;
5215 if (fr != topframe)
5216 fr = fr->fr_parent;
5219 if (offset < 0) /* drag up */
5221 up = TRUE;
5222 offset = -offset;
5223 /* sum up the room of the current frame and above it */
5224 if (fr == curfr)
5226 /* only one window */
5227 room = fr->fr_height - frame_minheight(fr, NULL);
5229 else
5231 room = 0;
5232 for (fr = fr->fr_child; ; fr = fr->fr_next)
5234 room += fr->fr_height - frame_minheight(fr, NULL);
5235 if (fr == curfr)
5236 break;
5239 fr = curfr->fr_next; /* put fr at frame that grows */
5241 else /* drag down */
5243 up = FALSE;
5245 * Only dragging the last status line can reduce p_ch.
5247 room = Rows - cmdline_row;
5248 if (curfr->fr_next == NULL)
5249 room -= 1;
5250 else
5251 room -= p_ch;
5252 if (room < 0)
5253 room = 0;
5254 /* sum up the room of frames below of the current one */
5255 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5256 room += fr->fr_height - frame_minheight(fr, NULL);
5257 fr = curfr; /* put fr at window that grows */
5260 if (room < offset) /* Not enough room */
5261 offset = room; /* Move as far as we can */
5262 if (offset <= 0)
5263 return;
5266 * Grow frame fr by "offset" lines.
5267 * Doesn't happen when dragging the last status line up.
5269 if (fr != NULL)
5270 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5272 if (up)
5273 fr = curfr; /* current frame gets smaller */
5274 else
5275 fr = curfr->fr_next; /* next frame gets smaller */
5278 * Now make the other frames smaller.
5280 while (fr != NULL && offset > 0)
5282 n = frame_minheight(fr, NULL);
5283 if (fr->fr_height - offset <= n)
5285 offset -= fr->fr_height - n;
5286 frame_new_height(fr, n, !up, FALSE);
5288 else
5290 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5291 break;
5293 if (up)
5294 fr = fr->fr_prev;
5295 else
5296 fr = fr->fr_next;
5298 row = win_comp_pos();
5299 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5300 cmdline_row = row;
5301 p_ch = Rows - cmdline_row;
5302 if (p_ch < 1)
5303 p_ch = 1;
5304 curtab->tp_ch_used = p_ch;
5305 redraw_all_later(SOME_VALID);
5306 showmode();
5309 #ifdef FEAT_VERTSPLIT
5311 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5313 void
5314 win_drag_vsep_line(dragwin, offset)
5315 win_T *dragwin;
5316 int offset;
5318 frame_T *curfr;
5319 frame_T *fr;
5320 int room;
5321 int left; /* if TRUE, drag separator line left, otherwise right */
5322 int n;
5324 fr = dragwin->w_frame;
5325 if (fr == topframe) /* only one window (cannot happen?) */
5326 return;
5327 curfr = fr;
5328 fr = fr->fr_parent;
5329 /* When the parent frame is not a row of frames, its parent should be. */
5330 if (fr->fr_layout != FR_ROW)
5332 if (fr == topframe) /* only a column of windows (cannot happen?) */
5333 return;
5334 curfr = fr;
5335 fr = fr->fr_parent;
5338 /* If this is the last frame in a row, may want to resize a parent
5339 * frame instead. */
5340 while (curfr->fr_next == NULL)
5342 if (fr == topframe)
5343 break;
5344 curfr = fr;
5345 fr = fr->fr_parent;
5346 if (fr != topframe)
5348 curfr = fr;
5349 fr = fr->fr_parent;
5353 if (offset < 0) /* drag left */
5355 left = TRUE;
5356 offset = -offset;
5357 /* sum up the room of the current frame and left of it */
5358 room = 0;
5359 for (fr = fr->fr_child; ; fr = fr->fr_next)
5361 room += fr->fr_width - frame_minwidth(fr, NULL);
5362 if (fr == curfr)
5363 break;
5365 fr = curfr->fr_next; /* put fr at frame that grows */
5367 else /* drag right */
5369 left = FALSE;
5370 /* sum up the room of frames right of the current one */
5371 room = 0;
5372 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5373 room += fr->fr_width - frame_minwidth(fr, NULL);
5374 fr = curfr; /* put fr at window that grows */
5377 if (room < offset) /* Not enough room */
5378 offset = room; /* Move as far as we can */
5379 if (offset <= 0) /* No room at all, quit. */
5380 return;
5382 /* grow frame fr by offset lines */
5383 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5385 /* shrink other frames: current and at the left or at the right */
5386 if (left)
5387 fr = curfr; /* current frame gets smaller */
5388 else
5389 fr = curfr->fr_next; /* next frame gets smaller */
5391 while (fr != NULL && offset > 0)
5393 n = frame_minwidth(fr, NULL);
5394 if (fr->fr_width - offset <= n)
5396 offset -= fr->fr_width - n;
5397 frame_new_width(fr, n, !left, FALSE);
5399 else
5401 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5402 break;
5404 if (left)
5405 fr = fr->fr_prev;
5406 else
5407 fr = fr->fr_next;
5409 (void)win_comp_pos();
5410 redraw_all_later(NOT_VALID);
5412 #endif /* FEAT_VERTSPLIT */
5413 #endif /* FEAT_MOUSE */
5415 #endif /* FEAT_WINDOWS */
5418 * Set the height of a window.
5419 * This takes care of the things inside the window, not what happens to the
5420 * window position, the frame or to other windows.
5422 static void
5423 win_new_height(wp, height)
5424 win_T *wp;
5425 int height;
5427 linenr_T lnum;
5428 int sline, line_size;
5429 #define FRACTION_MULT 16384L
5431 /* Don't want a negative height. Happens when splitting a tiny window.
5432 * Will equalize heights soon to fix it. */
5433 if (height < 0)
5434 height = 0;
5435 if (wp->w_height == height)
5436 return; /* nothing to do */
5438 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5439 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5440 + FRACTION_MULT / 2) / (long)wp->w_height;
5442 wp->w_height = height;
5443 wp->w_skipcol = 0;
5445 /* Don't change w_topline when height is zero. Don't set w_topline when
5446 * 'scrollbind' is set and this isn't the current window. */
5447 if (height > 0
5448 #ifdef FEAT_SCROLLBIND
5449 && (!wp->w_p_scb || wp == curwin)
5450 #endif
5454 * Find a value for w_topline that shows the cursor at the same
5455 * relative position in the window as before (more or less).
5457 lnum = wp->w_cursor.lnum;
5458 if (lnum < 1) /* can happen when starting up */
5459 lnum = 1;
5460 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5461 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5462 sline = wp->w_wrow - line_size;
5464 if (sline >= 0)
5466 /* Make sure the whole cursor line is visible, if possible. */
5467 int rows = plines_win(wp, lnum, FALSE);
5469 if (sline > wp->w_height - rows)
5471 sline = wp->w_height - rows;
5472 wp->w_wrow -= rows - line_size;
5476 if (sline < 0)
5479 * Cursor line would go off top of screen if w_wrow was this high.
5480 * Make cursor line the first line in the window. If not enough
5481 * room use w_skipcol;
5483 wp->w_wrow = line_size;
5484 if (wp->w_wrow >= wp->w_height
5485 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5487 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5488 --wp->w_wrow;
5489 while (wp->w_wrow >= wp->w_height)
5491 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5492 + win_col_off2(wp);
5493 --wp->w_wrow;
5497 else
5499 while (sline > 0 && lnum > 1)
5501 #ifdef FEAT_FOLDING
5502 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5503 if (lnum == 1)
5505 /* first line in buffer is folded */
5506 line_size = 1;
5507 --sline;
5508 break;
5510 #endif
5511 --lnum;
5512 #ifdef FEAT_DIFF
5513 if (lnum == wp->w_topline)
5514 line_size = plines_win_nofill(wp, lnum, TRUE)
5515 + wp->w_topfill;
5516 else
5517 #endif
5518 line_size = plines_win(wp, lnum, TRUE);
5519 sline -= line_size;
5522 if (sline < 0)
5525 * Line we want at top would go off top of screen. Use next
5526 * line instead.
5528 #ifdef FEAT_FOLDING
5529 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5530 #endif
5531 lnum++;
5532 wp->w_wrow -= line_size + sline;
5534 else if (sline > 0)
5536 /* First line of file reached, use that as topline. */
5537 lnum = 1;
5538 wp->w_wrow -= sline;
5541 set_topline(wp, lnum);
5544 if (wp == curwin)
5546 if (p_so)
5547 update_topline();
5548 curs_columns(FALSE); /* validate w_wrow */
5550 wp->w_prev_fraction_row = wp->w_wrow;
5552 win_comp_scroll(wp);
5553 redraw_win_later(wp, SOME_VALID);
5554 #ifdef FEAT_WINDOWS
5555 wp->w_redr_status = TRUE;
5556 #endif
5557 invalidate_botline_win(wp);
5560 #ifdef FEAT_VERTSPLIT
5562 * Set the width of a window.
5564 static void
5565 win_new_width(wp, width)
5566 win_T *wp;
5567 int width;
5569 wp->w_width = width;
5570 wp->w_lines_valid = 0;
5571 changed_line_abv_curs_win(wp);
5572 invalidate_botline_win(wp);
5573 if (wp == curwin)
5575 update_topline();
5576 curs_columns(TRUE); /* validate w_wrow */
5578 redraw_win_later(wp, NOT_VALID);
5579 wp->w_redr_status = TRUE;
5581 #endif
5583 void
5584 win_comp_scroll(wp)
5585 win_T *wp;
5587 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5588 if (wp->w_p_scr == 0)
5589 wp->w_p_scr = 1;
5593 * command_height: called whenever p_ch has been changed
5595 void
5596 command_height()
5598 #ifdef FEAT_WINDOWS
5599 int h;
5600 frame_T *frp;
5601 int old_p_ch = curtab->tp_ch_used;
5603 /* Use the value of p_ch that we remembered. This is needed for when the
5604 * GUI starts up, we can't be sure in what order things happen. And when
5605 * p_ch was changed in another tab page. */
5606 curtab->tp_ch_used = p_ch;
5608 /* Find bottom frame with width of screen. */
5609 frp = lastwin->w_frame;
5610 # ifdef FEAT_VERTSPLIT
5611 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5612 frp = frp->fr_parent;
5613 # endif
5615 /* Avoid changing the height of a window with 'winfixheight' set. */
5616 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5617 && frp->fr_win->w_p_wfh)
5618 frp = frp->fr_prev;
5620 if (starting != NO_SCREEN)
5622 cmdline_row = Rows - p_ch;
5624 if (p_ch > old_p_ch) /* p_ch got bigger */
5626 while (p_ch > old_p_ch)
5628 if (frp == NULL)
5630 EMSG(_(e_noroom));
5631 p_ch = old_p_ch;
5632 curtab->tp_ch_used = p_ch;
5633 cmdline_row = Rows - p_ch;
5634 break;
5636 h = frp->fr_height - frame_minheight(frp, NULL);
5637 if (h > p_ch - old_p_ch)
5638 h = p_ch - old_p_ch;
5639 old_p_ch += h;
5640 frame_add_height(frp, -h);
5641 frp = frp->fr_prev;
5644 /* Recompute window positions. */
5645 (void)win_comp_pos();
5647 /* clear the lines added to cmdline */
5648 if (full_screen)
5649 screen_fill((int)(cmdline_row), (int)Rows, 0,
5650 (int)Columns, ' ', ' ', 0);
5651 msg_row = cmdline_row;
5652 redraw_cmdline = TRUE;
5653 return;
5656 if (msg_row < cmdline_row)
5657 msg_row = cmdline_row;
5658 redraw_cmdline = TRUE;
5660 frame_add_height(frp, (int)(old_p_ch - p_ch));
5662 /* Recompute window positions. */
5663 if (frp != lastwin->w_frame)
5664 (void)win_comp_pos();
5665 #else
5666 cmdline_row = Rows - p_ch;
5667 win_setheight(cmdline_row);
5668 #endif
5671 #if defined(FEAT_WINDOWS) || defined(PROTO)
5673 * Resize frame "frp" to be "n" lines higher (negative for less high).
5674 * Also resize the frames it is contained in.
5676 static void
5677 frame_add_height(frp, n)
5678 frame_T *frp;
5679 int n;
5681 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5682 for (;;)
5684 frp = frp->fr_parent;
5685 if (frp == NULL)
5686 break;
5687 frp->fr_height += n;
5692 * Add or remove a status line for the bottom window(s), according to the
5693 * value of 'laststatus'.
5695 void
5696 last_status(morewin)
5697 int morewin; /* pretend there are two or more windows */
5699 /* Don't make a difference between horizontal or vertical split. */
5700 last_status_rec(topframe, (p_ls == 2
5701 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5704 static void
5705 last_status_rec(fr, statusline)
5706 frame_T *fr;
5707 int statusline;
5709 frame_T *fp;
5710 win_T *wp;
5712 if (fr->fr_layout == FR_LEAF)
5714 wp = fr->fr_win;
5715 if (wp->w_status_height != 0 && !statusline)
5717 /* remove status line */
5718 win_new_height(wp, wp->w_height + 1);
5719 wp->w_status_height = 0;
5720 comp_col();
5722 else if (wp->w_status_height == 0 && statusline)
5724 /* Find a frame to take a line from. */
5725 fp = fr;
5726 while (fp->fr_height <= frame_minheight(fp, NULL))
5728 if (fp == topframe)
5730 EMSG(_(e_noroom));
5731 return;
5733 /* In a column of frames: go to frame above. If already at
5734 * the top or in a row of frames: go to parent. */
5735 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5736 fp = fp->fr_prev;
5737 else
5738 fp = fp->fr_parent;
5740 wp->w_status_height = 1;
5741 if (fp != fr)
5743 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5744 frame_fix_height(wp);
5745 (void)win_comp_pos();
5747 else
5748 win_new_height(wp, wp->w_height - 1);
5749 comp_col();
5750 redraw_all_later(SOME_VALID);
5753 #ifdef FEAT_VERTSPLIT
5754 else if (fr->fr_layout == FR_ROW)
5756 /* vertically split windows, set status line for each one */
5757 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5758 last_status_rec(fp, statusline);
5760 #endif
5761 else
5763 /* horizontally split window, set status line for last one */
5764 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5766 last_status_rec(fp, statusline);
5771 * Return the number of lines used by the tab page line.
5774 tabline_height()
5776 #ifdef FEAT_GUI_TABLINE
5777 /* When the GUI has the tabline then this always returns zero. */
5778 if (gui_use_tabline())
5779 return 0;
5780 #endif
5781 switch (p_stal)
5783 case 0: return 0;
5784 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5786 return 1;
5789 #endif /* FEAT_WINDOWS */
5791 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5793 * Get the file name at the cursor.
5794 * If Visual mode is active, use the selected text if it's in one line.
5795 * Returns the name in allocated memory, NULL for failure.
5797 char_u *
5798 grab_file_name(count, file_lnum)
5799 long count;
5800 linenr_T *file_lnum;
5802 # ifdef FEAT_VISUAL
5803 if (VIsual_active)
5805 int len;
5806 char_u *ptr;
5808 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5809 return NULL;
5810 return find_file_name_in_path(ptr, len,
5811 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5813 # endif
5814 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5815 file_lnum);
5820 * Return the file name under or after the cursor.
5822 * The 'path' option is searched if the file name is not absolute.
5823 * The string returned has been alloc'ed and should be freed by the caller.
5824 * NULL is returned if the file name or file is not found.
5826 * options:
5827 * FNAME_MESS give error messages
5828 * FNAME_EXP expand to path
5829 * FNAME_HYP check for hypertext link
5830 * FNAME_INCL apply "includeexpr"
5832 char_u *
5833 file_name_at_cursor(options, count, file_lnum)
5834 int options;
5835 long count;
5836 linenr_T *file_lnum;
5838 return file_name_in_line(ml_get_curline(),
5839 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5840 file_lnum);
5844 * Return the name of the file under or after ptr[col].
5845 * Otherwise like file_name_at_cursor().
5847 char_u *
5848 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5849 char_u *line;
5850 int col;
5851 int options;
5852 long count;
5853 char_u *rel_fname; /* file we are searching relative to */
5854 linenr_T *file_lnum; /* line number after the file name */
5856 char_u *ptr;
5857 int len;
5860 * search forward for what could be the start of a file name
5862 ptr = line + col;
5863 while (*ptr != NUL && !vim_isfilec(*ptr))
5864 mb_ptr_adv(ptr);
5865 if (*ptr == NUL) /* nothing found */
5867 if (options & FNAME_MESS)
5868 EMSG(_("E446: No file name under cursor"));
5869 return NULL;
5873 * Search backward for first char of the file name.
5874 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5876 while (ptr > line)
5878 #ifdef FEAT_MBYTE
5879 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5880 ptr -= len + 1;
5881 else
5882 #endif
5883 if (vim_isfilec(ptr[-1])
5884 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5885 --ptr;
5886 else
5887 break;
5891 * Search forward for the last char of the file name.
5892 * Also allow "://" when ':' is not in 'isfname'.
5894 len = 0;
5895 while (vim_isfilec(ptr[len])
5896 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5897 #ifdef FEAT_MBYTE
5898 if (has_mbyte)
5899 len += (*mb_ptr2len)(ptr + len);
5900 else
5901 #endif
5902 ++len;
5905 * If there is trailing punctuation, remove it.
5906 * But don't remove "..", could be a directory name.
5908 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5909 && ptr[len - 2] != '.')
5910 --len;
5912 if (file_lnum != NULL)
5914 char_u *p;
5916 /* Get the number after the file name and a separator character */
5917 p = ptr + len;
5918 p = skipwhite(p);
5919 if (*p != NUL)
5921 if (!isdigit(*p))
5922 ++p; /* skip the separator */
5923 p = skipwhite(p);
5924 if (isdigit(*p))
5925 *file_lnum = (int)getdigits(&p);
5929 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5932 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5933 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5935 static char_u *
5936 eval_includeexpr(ptr, len)
5937 char_u *ptr;
5938 int len;
5940 char_u *res;
5942 set_vim_var_string(VV_FNAME, ptr, len);
5943 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5944 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5945 set_vim_var_string(VV_FNAME, NULL, 0);
5946 return res;
5948 #endif
5951 * Return the name of the file ptr[len] in 'path'.
5952 * Otherwise like file_name_at_cursor().
5954 char_u *
5955 find_file_name_in_path(ptr, len, options, count, rel_fname)
5956 char_u *ptr;
5957 int len;
5958 int options;
5959 long count;
5960 char_u *rel_fname; /* file we are searching relative to */
5962 char_u *file_name;
5963 int c;
5964 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5965 char_u *tofree = NULL;
5967 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5969 tofree = eval_includeexpr(ptr, len);
5970 if (tofree != NULL)
5972 ptr = tofree;
5973 len = (int)STRLEN(ptr);
5976 # endif
5978 if (options & FNAME_EXP)
5980 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5981 TRUE, rel_fname);
5983 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5985 * If the file could not be found in a normal way, try applying
5986 * 'includeexpr' (unless done already).
5988 if (file_name == NULL
5989 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5991 tofree = eval_includeexpr(ptr, len);
5992 if (tofree != NULL)
5994 ptr = tofree;
5995 len = (int)STRLEN(ptr);
5996 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5997 TRUE, rel_fname);
6000 # endif
6001 if (file_name == NULL && (options & FNAME_MESS))
6003 c = ptr[len];
6004 ptr[len] = NUL;
6005 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6006 ptr[len] = c;
6009 /* Repeat finding the file "count" times. This matters when it
6010 * appears several times in the path. */
6011 while (file_name != NULL && --count > 0)
6013 vim_free(file_name);
6014 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6017 else
6018 file_name = vim_strnsave(ptr, len);
6020 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6021 vim_free(tofree);
6022 # endif
6024 return file_name;
6026 #endif /* FEAT_SEARCHPATH */
6029 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6030 * Also check for ":\\", which MS Internet Explorer accepts, return
6031 * URL_BACKSLASH.
6033 static int
6034 path_is_url(p)
6035 char_u *p;
6037 if (STRNCMP(p, "://", (size_t)3) == 0)
6038 return URL_SLASH;
6039 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6040 return URL_BACKSLASH;
6041 return 0;
6045 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6046 * Return URL_BACKSLASH for "name:\\".
6047 * Return zero otherwise.
6050 path_with_url(fname)
6051 char_u *fname;
6053 char_u *p;
6055 for (p = fname; isalpha(*p); ++p)
6057 return path_is_url(p);
6061 * Return TRUE if "name" is a full (absolute) path name or URL.
6064 vim_isAbsName(name)
6065 char_u *name;
6067 return (path_with_url(name) != 0 || mch_isFullName(name));
6071 * Get absolute file name into buffer "buf[len]".
6073 * return FAIL for failure, OK otherwise
6076 vim_FullName(fname, buf, len, force)
6077 char_u *fname, *buf;
6078 int len;
6079 int force; /* force expansion even when already absolute */
6081 int retval = OK;
6082 int url;
6084 *buf = NUL;
6085 if (fname == NULL)
6086 return FAIL;
6088 url = path_with_url(fname);
6089 if (!url)
6090 retval = mch_FullName(fname, buf, len, force);
6091 if (url || retval == FAIL)
6093 /* something failed; use the file name (truncate when too long) */
6094 vim_strncpy(buf, fname, len - 1);
6096 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6097 slash_adjust(buf);
6098 #endif
6099 return retval;
6103 * Return the minimal number of rows that is needed on the screen to display
6104 * the current number of windows.
6107 min_rows()
6109 int total;
6110 #ifdef FEAT_WINDOWS
6111 tabpage_T *tp;
6112 int n;
6113 #endif
6115 if (firstwin == NULL) /* not initialized yet */
6116 return MIN_LINES;
6118 #ifdef FEAT_WINDOWS
6119 total = 0;
6120 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6122 n = frame_minheight(tp->tp_topframe, NULL);
6123 if (total < n)
6124 total = n;
6126 total += tabline_height();
6127 #else
6128 total = 1; /* at least one window should have a line! */
6129 #endif
6130 total += 1; /* count the room for the command line */
6131 return total;
6135 * Return TRUE if there is only one window (in the current tab page), not
6136 * counting a help or preview window, unless it is the current window.
6137 * Does not count "aucmd_win".
6140 only_one_window()
6142 #ifdef FEAT_WINDOWS
6143 int count = 0;
6144 win_T *wp;
6146 /* If there is another tab page there always is another window. */
6147 if (first_tabpage->tp_next != NULL)
6148 return FALSE;
6150 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6151 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
6152 # ifdef FEAT_QUICKFIX
6153 || wp->w_p_pvw
6154 # endif
6155 ) || wp == curwin)
6156 # ifdef FEAT_AUTOCMD
6157 && wp != aucmd_win
6158 # endif
6160 ++count;
6161 return (count <= 1);
6162 #else
6163 return TRUE;
6164 #endif
6167 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6169 * Correct the cursor line number in other windows. Used after changing the
6170 * current buffer, and before applying autocommands.
6171 * When "do_curwin" is TRUE, also check current window.
6173 void
6174 check_lnums(do_curwin)
6175 int do_curwin;
6177 win_T *wp;
6179 #ifdef FEAT_WINDOWS
6180 tabpage_T *tp;
6182 FOR_ALL_TAB_WINDOWS(tp, wp)
6183 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6184 #else
6185 wp = curwin;
6186 if (do_curwin)
6187 #endif
6189 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6190 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6191 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6192 wp->w_topline = curbuf->b_ml.ml_line_count;
6195 #endif
6197 #if defined(FEAT_WINDOWS) || defined(PROTO)
6200 * A snapshot of the window sizes, to restore them after closing the help
6201 * window.
6202 * Only these fields are used:
6203 * fr_layout
6204 * fr_width
6205 * fr_height
6206 * fr_next
6207 * fr_child
6208 * fr_win (only valid for the old curwin, NULL otherwise)
6212 * Create a snapshot of the current frame sizes.
6214 void
6215 make_snapshot(idx)
6216 int idx;
6218 clear_snapshot(curtab, idx);
6219 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6222 static void
6223 make_snapshot_rec(fr, frp)
6224 frame_T *fr;
6225 frame_T **frp;
6227 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6228 if (*frp == NULL)
6229 return;
6230 (*frp)->fr_layout = fr->fr_layout;
6231 # ifdef FEAT_VERTSPLIT
6232 (*frp)->fr_width = fr->fr_width;
6233 # endif
6234 (*frp)->fr_height = fr->fr_height;
6235 if (fr->fr_next != NULL)
6236 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6237 if (fr->fr_child != NULL)
6238 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6239 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6240 (*frp)->fr_win = curwin;
6244 * Remove any existing snapshot.
6246 static void
6247 clear_snapshot(tp, idx)
6248 tabpage_T *tp;
6249 int idx;
6251 clear_snapshot_rec(tp->tp_snapshot[idx]);
6252 tp->tp_snapshot[idx] = NULL;
6255 static void
6256 clear_snapshot_rec(fr)
6257 frame_T *fr;
6259 if (fr != NULL)
6261 clear_snapshot_rec(fr->fr_next);
6262 clear_snapshot_rec(fr->fr_child);
6263 vim_free(fr);
6268 * Restore a previously created snapshot, if there is any.
6269 * This is only done if the screen size didn't change and the window layout is
6270 * still the same.
6272 void
6273 restore_snapshot(idx, close_curwin)
6274 int idx;
6275 int close_curwin; /* closing current window */
6277 win_T *wp;
6279 if (curtab->tp_snapshot[idx] != NULL
6280 # ifdef FEAT_VERTSPLIT
6281 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6282 # endif
6283 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6284 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6286 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6287 win_comp_pos();
6288 if (wp != NULL && close_curwin)
6289 win_goto(wp);
6290 redraw_all_later(CLEAR);
6292 clear_snapshot(curtab, idx);
6296 * Check if frames "sn" and "fr" have the same layout, same following frames
6297 * and same children.
6299 static int
6300 check_snapshot_rec(sn, fr)
6301 frame_T *sn;
6302 frame_T *fr;
6304 if (sn->fr_layout != fr->fr_layout
6305 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6306 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6307 || (sn->fr_next != NULL
6308 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6309 || (sn->fr_child != NULL
6310 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6311 return FAIL;
6312 return OK;
6316 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6317 * following frames and children.
6318 * Returns a pointer to the old current window, or NULL.
6320 static win_T *
6321 restore_snapshot_rec(sn, fr)
6322 frame_T *sn;
6323 frame_T *fr;
6325 win_T *wp = NULL;
6326 win_T *wp2;
6328 fr->fr_height = sn->fr_height;
6329 # ifdef FEAT_VERTSPLIT
6330 fr->fr_width = sn->fr_width;
6331 # endif
6332 if (fr->fr_layout == FR_LEAF)
6334 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6335 # ifdef FEAT_VERTSPLIT
6336 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6337 # endif
6338 wp = sn->fr_win;
6340 if (sn->fr_next != NULL)
6342 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6343 if (wp2 != NULL)
6344 wp = wp2;
6346 if (sn->fr_child != NULL)
6348 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6349 if (wp2 != NULL)
6350 wp = wp2;
6352 return wp;
6355 #endif
6357 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6359 * Return TRUE if there is any vertically split window.
6362 win_hasvertsplit()
6364 frame_T *fr;
6366 if (topframe->fr_layout == FR_ROW)
6367 return TRUE;
6369 if (topframe->fr_layout == FR_COL)
6370 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6371 if (fr->fr_layout == FR_ROW)
6372 return TRUE;
6374 return FALSE;
6376 #endif
6378 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6380 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6381 * highlighted with the group 'grp' with priority 'prio'.
6382 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6383 * If no particular ID is desired, -1 must be specified for 'id'.
6384 * Return ID of added match, -1 on failure.
6387 match_add(wp, grp, pat, prio, id)
6388 win_T *wp;
6389 char_u *grp;
6390 char_u *pat;
6391 int prio;
6392 int id;
6394 matchitem_T *cur;
6395 matchitem_T *prev;
6396 matchitem_T *m;
6397 int hlg_id;
6398 regprog_T *regprog;
6400 if (*grp == NUL || *pat == NUL)
6401 return -1;
6402 if (id < -1 || id == 0)
6404 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6405 return -1;
6407 if (id != -1)
6409 cur = wp->w_match_head;
6410 while (cur != NULL)
6412 if (cur->id == id)
6414 EMSGN("E801: ID already taken: %ld", id);
6415 return -1;
6417 cur = cur->next;
6420 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6422 EMSG2(_(e_nogroup), grp);
6423 return -1;
6425 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6427 EMSG2(_(e_invarg2), pat);
6428 return -1;
6431 /* Find available match ID. */
6432 while (id == -1)
6434 cur = wp->w_match_head;
6435 while (cur != NULL && cur->id != wp->w_next_match_id)
6436 cur = cur->next;
6437 if (cur == NULL)
6438 id = wp->w_next_match_id;
6439 wp->w_next_match_id++;
6442 /* Build new match. */
6443 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6444 m->id = id;
6445 m->priority = prio;
6446 m->pattern = vim_strsave(pat);
6447 m->hlg_id = hlg_id;
6448 m->match.regprog = regprog;
6449 m->match.rmm_ic = FALSE;
6450 m->match.rmm_maxcol = 0;
6452 /* Insert new match. The match list is in ascending order with regard to
6453 * the match priorities. */
6454 cur = wp->w_match_head;
6455 prev = cur;
6456 while (cur != NULL && prio >= cur->priority)
6458 prev = cur;
6459 cur = cur->next;
6461 if (cur == prev)
6462 wp->w_match_head = m;
6463 else
6464 prev->next = m;
6465 m->next = cur;
6467 redraw_later(SOME_VALID);
6468 return id;
6472 * Delete match with ID 'id' in the match list of window 'wp'.
6473 * Print error messages if 'perr' is TRUE.
6476 match_delete(wp, id, perr)
6477 win_T *wp;
6478 int id;
6479 int perr;
6481 matchitem_T *cur = wp->w_match_head;
6482 matchitem_T *prev = cur;
6484 if (id < 1)
6486 if (perr == TRUE)
6487 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6488 id);
6489 return -1;
6491 while (cur != NULL && cur->id != id)
6493 prev = cur;
6494 cur = cur->next;
6496 if (cur == NULL)
6498 if (perr == TRUE)
6499 EMSGN("E803: ID not found: %ld", id);
6500 return -1;
6502 if (cur == prev)
6503 wp->w_match_head = cur->next;
6504 else
6505 prev->next = cur->next;
6506 vim_free(cur->match.regprog);
6507 vim_free(cur->pattern);
6508 vim_free(cur);
6509 redraw_later(SOME_VALID);
6510 return 0;
6514 * Delete all matches in the match list of window 'wp'.
6516 void
6517 clear_matches(wp)
6518 win_T *wp;
6520 matchitem_T *m;
6522 while (wp->w_match_head != NULL)
6524 m = wp->w_match_head->next;
6525 vim_free(wp->w_match_head->match.regprog);
6526 vim_free(wp->w_match_head->pattern);
6527 vim_free(wp->w_match_head);
6528 wp->w_match_head = m;
6530 redraw_later(SOME_VALID);
6534 * Get match from ID 'id' in window 'wp'.
6535 * Return NULL if match not found.
6537 matchitem_T *
6538 get_match(wp, id)
6539 win_T *wp;
6540 int id;
6542 matchitem_T *cur = wp->w_match_head;
6544 while (cur != NULL && cur->id != id)
6545 cur = cur->next;
6546 return cur;
6548 #endif