Merge branch 'vim'
[MacVim.git] / src / window.c
blob9e5b9e98b0b266666ea36dfbe5d4940b342dac9b
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 = tabpage_index(curtab) + 1;
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 #ifdef FEAT_GUI_MACVIM
1101 /* The view may have moved, so clear all or display may get corrupted. */
1102 redraw_win_later(oldwin, CLEAR);
1103 #else
1104 redraw_win_later(oldwin, NOT_VALID);
1105 #endif
1106 oldwin->w_redr_status = TRUE;
1108 if (need_status)
1110 msg_row = Rows - 1;
1111 msg_col = sc_col;
1112 msg_clr_eos_force(); /* Old command/ruler may still be there */
1113 comp_col();
1114 msg_row = Rows - 1;
1115 msg_col = 0; /* put position back at start of line */
1119 * make the new window the current window and redraw
1121 if (do_equal || dir != 0)
1122 win_equal(wp, TRUE,
1123 #ifdef FEAT_VERTSPLIT
1124 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1125 : dir == 'h' ? 'b' :
1126 #endif
1127 'v');
1129 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1130 * size was given. */
1131 #ifdef FEAT_VERTSPLIT
1132 if (flags & WSP_VERT)
1134 i = p_wiw;
1135 if (size != 0)
1136 p_wiw = size;
1138 # ifdef FEAT_GUI
1139 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1140 if (gui.in_use)
1141 gui_init_which_components(NULL);
1142 # endif
1144 else
1145 #endif
1147 i = p_wh;
1148 if (size != 0)
1149 p_wh = size;
1151 win_enter(wp, FALSE);
1152 #ifdef FEAT_VERTSPLIT
1153 if (flags & WSP_VERT)
1154 p_wiw = i;
1155 else
1156 #endif
1157 p_wh = i;
1159 return OK;
1164 * Initialize window "newp" from window "oldp".
1165 * Used when splitting a window and when creating a new tab page.
1166 * The windows will both edit the same buffer.
1167 * WSP_NEWLOC may be specified in flags to prevent the location list from
1168 * being copied.
1170 static void
1171 win_init(newp, oldp, flags)
1172 win_T *newp;
1173 win_T *oldp;
1174 int flags UNUSED;
1176 int i;
1178 newp->w_buffer = oldp->w_buffer;
1179 oldp->w_buffer->b_nwindows++;
1180 newp->w_cursor = oldp->w_cursor;
1181 newp->w_valid = 0;
1182 newp->w_curswant = oldp->w_curswant;
1183 newp->w_set_curswant = oldp->w_set_curswant;
1184 newp->w_topline = oldp->w_topline;
1185 #ifdef FEAT_DIFF
1186 newp->w_topfill = oldp->w_topfill;
1187 #endif
1188 newp->w_leftcol = oldp->w_leftcol;
1189 newp->w_pcmark = oldp->w_pcmark;
1190 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1191 newp->w_alt_fnum = oldp->w_alt_fnum;
1192 newp->w_wrow = oldp->w_wrow;
1193 newp->w_fraction = oldp->w_fraction;
1194 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1195 #ifdef FEAT_JUMPLIST
1196 copy_jumplist(oldp, newp);
1197 #endif
1198 #ifdef FEAT_QUICKFIX
1199 if (flags & WSP_NEWLOC)
1201 /* Don't copy the location list. */
1202 newp->w_llist = NULL;
1203 newp->w_llist_ref = NULL;
1205 else
1206 copy_loclist(oldp, newp);
1207 #endif
1208 if (oldp->w_localdir != NULL)
1209 newp->w_localdir = vim_strsave(oldp->w_localdir);
1211 /* copy tagstack and folds */
1212 for (i = 0; i < oldp->w_tagstacklen; i++)
1214 newp->w_tagstack[i] = oldp->w_tagstack[i];
1215 if (newp->w_tagstack[i].tagname != NULL)
1216 newp->w_tagstack[i].tagname =
1217 vim_strsave(newp->w_tagstack[i].tagname);
1219 newp->w_tagstackidx = oldp->w_tagstackidx;
1220 newp->w_tagstacklen = oldp->w_tagstacklen;
1221 # ifdef FEAT_FOLDING
1222 copyFoldingState(oldp, newp);
1223 # endif
1225 win_init_some(newp, oldp);
1229 * Initialize window "newp" from window"old".
1230 * Only the essential things are copied.
1232 static void
1233 win_init_some(newp, oldp)
1234 win_T *newp;
1235 win_T *oldp;
1237 /* Use the same argument list. */
1238 newp->w_alist = oldp->w_alist;
1239 ++newp->w_alist->al_refcount;
1240 newp->w_arg_idx = oldp->w_arg_idx;
1242 /* copy options from existing window */
1243 win_copy_options(oldp, newp);
1246 #endif /* FEAT_WINDOWS */
1248 #if defined(FEAT_WINDOWS) || defined(PROTO)
1250 * Check if "win" is a pointer to an existing window.
1253 win_valid(win)
1254 win_T *win;
1256 win_T *wp;
1258 if (win == NULL)
1259 return FALSE;
1260 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1261 if (wp == win)
1262 return TRUE;
1263 return FALSE;
1267 * Return the number of windows.
1270 win_count()
1272 win_T *wp;
1273 int count = 0;
1275 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1276 ++count;
1277 return count;
1281 * Make "count" windows on the screen.
1282 * Return actual number of windows on the screen.
1283 * Must be called when there is just one window, filling the whole screen
1284 * (excluding the command line).
1287 make_windows(count, vertical)
1288 int count;
1289 int vertical UNUSED; /* split windows vertically if TRUE */
1291 int maxcount;
1292 int todo;
1294 #ifdef FEAT_VERTSPLIT
1295 if (vertical)
1297 /* Each windows needs at least 'winminwidth' lines and a separator
1298 * column. */
1299 maxcount = (curwin->w_width + curwin->w_vsep_width
1300 - (p_wiw - p_wmw)) / (p_wmw + 1);
1302 else
1303 #endif
1305 /* Each window needs at least 'winminheight' lines and a status line. */
1306 maxcount = (curwin->w_height + curwin->w_status_height
1307 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1310 if (maxcount < 2)
1311 maxcount = 2;
1312 if (count > maxcount)
1313 count = maxcount;
1316 * add status line now, otherwise first window will be too big
1318 if (count > 1)
1319 last_status(TRUE);
1321 #ifdef FEAT_AUTOCMD
1323 * Don't execute autocommands while creating the windows. Must do that
1324 * when putting the buffers in the windows.
1326 block_autocmds();
1327 #endif
1329 /* todo is number of windows left to create */
1330 for (todo = count - 1; todo > 0; --todo)
1331 #ifdef FEAT_VERTSPLIT
1332 if (vertical)
1334 if (win_split(curwin->w_width - (curwin->w_width - todo)
1335 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1336 break;
1338 else
1339 #endif
1341 if (win_split(curwin->w_height - (curwin->w_height - todo
1342 * STATUS_HEIGHT) / (todo + 1)
1343 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1344 break;
1347 #ifdef FEAT_AUTOCMD
1348 unblock_autocmds();
1349 #endif
1351 /* return actual number of windows */
1352 return (count - todo);
1356 * Exchange current and next window
1358 static void
1359 win_exchange(Prenum)
1360 long Prenum;
1362 frame_T *frp;
1363 frame_T *frp2;
1364 win_T *wp;
1365 win_T *wp2;
1366 int temp;
1368 if (lastwin == firstwin) /* just one window */
1370 beep_flush();
1371 return;
1374 #ifdef FEAT_GUI
1375 need_mouse_correct = TRUE;
1376 #endif
1379 * find window to exchange with
1381 if (Prenum)
1383 frp = curwin->w_frame->fr_parent->fr_child;
1384 while (frp != NULL && --Prenum > 0)
1385 frp = frp->fr_next;
1387 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1388 frp = curwin->w_frame->fr_next;
1389 else /* Swap last window in row/col with previous */
1390 frp = curwin->w_frame->fr_prev;
1392 /* We can only exchange a window with another window, not with a frame
1393 * containing windows. */
1394 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1395 return;
1396 wp = frp->fr_win;
1399 * 1. remove curwin from the list. Remember after which window it was in wp2
1400 * 2. insert curwin before wp in the list
1401 * if wp != wp2
1402 * 3. remove wp from the list
1403 * 4. insert wp after wp2
1404 * 5. exchange the status line height and vsep width.
1406 wp2 = curwin->w_prev;
1407 frp2 = curwin->w_frame->fr_prev;
1408 if (wp->w_prev != curwin)
1410 win_remove(curwin, NULL);
1411 frame_remove(curwin->w_frame);
1412 win_append(wp->w_prev, curwin);
1413 frame_insert(frp, curwin->w_frame);
1415 if (wp != wp2)
1417 win_remove(wp, NULL);
1418 frame_remove(wp->w_frame);
1419 win_append(wp2, wp);
1420 if (frp2 == NULL)
1421 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1422 else
1423 frame_append(frp2, wp->w_frame);
1425 temp = curwin->w_status_height;
1426 curwin->w_status_height = wp->w_status_height;
1427 wp->w_status_height = temp;
1428 #ifdef FEAT_VERTSPLIT
1429 temp = curwin->w_vsep_width;
1430 curwin->w_vsep_width = wp->w_vsep_width;
1431 wp->w_vsep_width = temp;
1433 /* If the windows are not in the same frame, exchange the sizes to avoid
1434 * messing up the window layout. Otherwise fix the frame sizes. */
1435 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1437 temp = curwin->w_height;
1438 curwin->w_height = wp->w_height;
1439 wp->w_height = temp;
1440 temp = curwin->w_width;
1441 curwin->w_width = wp->w_width;
1442 wp->w_width = temp;
1444 else
1446 frame_fix_height(curwin);
1447 frame_fix_height(wp);
1448 frame_fix_width(curwin);
1449 frame_fix_width(wp);
1451 #endif
1453 (void)win_comp_pos(); /* recompute window positions */
1455 win_enter(wp, TRUE);
1456 redraw_later(CLEAR);
1460 * rotate windows: if upwards TRUE the second window becomes the first one
1461 * if upwards FALSE the first window becomes the second one
1463 static void
1464 win_rotate(upwards, count)
1465 int upwards;
1466 int count;
1468 win_T *wp1;
1469 win_T *wp2;
1470 frame_T *frp;
1471 int n;
1473 if (firstwin == lastwin) /* nothing to do */
1475 beep_flush();
1476 return;
1479 #ifdef FEAT_GUI
1480 need_mouse_correct = TRUE;
1481 #endif
1483 #ifdef FEAT_VERTSPLIT
1484 /* Check if all frames in this row/col have one window. */
1485 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1486 frp = frp->fr_next)
1487 if (frp->fr_win == NULL)
1489 EMSG(_("E443: Cannot rotate when another window is split"));
1490 return;
1492 #endif
1494 while (count--)
1496 if (upwards) /* first window becomes last window */
1498 /* remove first window/frame from the list */
1499 frp = curwin->w_frame->fr_parent->fr_child;
1500 wp1 = frp->fr_win;
1501 win_remove(wp1, NULL);
1502 frame_remove(frp);
1504 /* find last frame and append removed window/frame after it */
1505 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1507 win_append(frp->fr_win, wp1);
1508 frame_append(frp, wp1->w_frame);
1510 wp2 = frp->fr_win; /* previously last window */
1512 else /* last window becomes first window */
1514 /* find last window/frame in the list and remove it */
1515 for (frp = curwin->w_frame; frp->fr_next != NULL;
1516 frp = frp->fr_next)
1518 wp1 = frp->fr_win;
1519 wp2 = wp1->w_prev; /* will become last window */
1520 win_remove(wp1, NULL);
1521 frame_remove(frp);
1523 /* append the removed window/frame before the first in the list */
1524 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1525 frame_insert(frp->fr_parent->fr_child, frp);
1528 /* exchange status height and vsep width of old and new last window */
1529 n = wp2->w_status_height;
1530 wp2->w_status_height = wp1->w_status_height;
1531 wp1->w_status_height = n;
1532 frame_fix_height(wp1);
1533 frame_fix_height(wp2);
1534 #ifdef FEAT_VERTSPLIT
1535 n = wp2->w_vsep_width;
1536 wp2->w_vsep_width = wp1->w_vsep_width;
1537 wp1->w_vsep_width = n;
1538 frame_fix_width(wp1);
1539 frame_fix_width(wp2);
1540 #endif
1542 /* recompute w_winrow and w_wincol for all windows */
1543 (void)win_comp_pos();
1546 redraw_later(CLEAR);
1550 * Move the current window to the very top/bottom/left/right of the screen.
1552 static void
1553 win_totop(size, flags)
1554 int size;
1555 int flags;
1557 int dir;
1558 int height = curwin->w_height;
1560 if (lastwin == firstwin)
1562 beep_flush();
1563 return;
1566 /* Remove the window and frame from the tree of frames. */
1567 (void)winframe_remove(curwin, &dir, NULL);
1568 win_remove(curwin, NULL);
1569 last_status(FALSE); /* may need to remove last status line */
1570 (void)win_comp_pos(); /* recompute window positions */
1572 /* Split a window on the desired side and put the window there. */
1573 (void)win_split_ins(size, flags, curwin, dir);
1574 if (!(flags & WSP_VERT))
1576 win_setheight(height);
1577 if (p_ea)
1578 win_equal(curwin, TRUE, 'v');
1581 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1582 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1583 * scrollbars. Have to update them anyway. */
1584 gui_may_update_scrollbars();
1585 #endif
1589 * Move window "win1" to below/right of "win2" and make "win1" the current
1590 * window. Only works within the same frame!
1592 void
1593 win_move_after(win1, win2)
1594 win_T *win1, *win2;
1596 int height;
1598 /* check if the arguments are reasonable */
1599 if (win1 == win2)
1600 return;
1602 /* check if there is something to do */
1603 if (win2->w_next != win1)
1605 /* may need move the status line/vertical separator of the last window
1606 * */
1607 if (win1 == lastwin)
1609 height = win1->w_prev->w_status_height;
1610 win1->w_prev->w_status_height = win1->w_status_height;
1611 win1->w_status_height = height;
1612 #ifdef FEAT_VERTSPLIT
1613 if (win1->w_prev->w_vsep_width == 1)
1615 /* Remove the vertical separator from the last-but-one window,
1616 * add it to the last window. Adjust the frame widths. */
1617 win1->w_prev->w_vsep_width = 0;
1618 win1->w_prev->w_frame->fr_width -= 1;
1619 win1->w_vsep_width = 1;
1620 win1->w_frame->fr_width += 1;
1622 #endif
1624 else if (win2 == lastwin)
1626 height = win1->w_status_height;
1627 win1->w_status_height = win2->w_status_height;
1628 win2->w_status_height = height;
1629 #ifdef FEAT_VERTSPLIT
1630 if (win1->w_vsep_width == 1)
1632 /* Remove the vertical separator from win1, add it to the last
1633 * window, win2. Adjust the frame widths. */
1634 win2->w_vsep_width = 1;
1635 win2->w_frame->fr_width += 1;
1636 win1->w_vsep_width = 0;
1637 win1->w_frame->fr_width -= 1;
1639 #endif
1641 win_remove(win1, NULL);
1642 frame_remove(win1->w_frame);
1643 win_append(win2, win1);
1644 frame_append(win2->w_frame, win1->w_frame);
1646 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1647 redraw_later(NOT_VALID);
1649 win_enter(win1, FALSE);
1653 * Make all windows the same height.
1654 * 'next_curwin' will soon be the current window, make sure it has enough
1655 * rows.
1657 void
1658 win_equal(next_curwin, current, dir)
1659 win_T *next_curwin; /* pointer to current window to be or NULL */
1660 int current; /* do only frame with current window */
1661 int dir; /* 'v' for vertically, 'h' for horizontally,
1662 'b' for both, 0 for using p_ead */
1664 if (dir == 0)
1665 #ifdef FEAT_VERTSPLIT
1666 dir = *p_ead;
1667 #else
1668 dir = 'b';
1669 #endif
1670 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1671 topframe, dir, 0, tabline_height(),
1672 (int)Columns, topframe->fr_height);
1676 * Set a frame to a new position and height, spreading the available room
1677 * equally over contained frames.
1678 * The window "next_curwin" (if not NULL) should at least get the size from
1679 * 'winheight' and 'winwidth' if possible.
1681 static void
1682 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1683 win_T *next_curwin; /* pointer to current window to be or NULL */
1684 int current; /* do only frame with current window */
1685 frame_T *topfr; /* frame to set size off */
1686 int dir; /* 'v', 'h' or 'b', see win_equal() */
1687 int col; /* horizontal position for frame */
1688 int row; /* vertical position for frame */
1689 int width; /* new width of frame */
1690 int height; /* new height of frame */
1692 int n, m;
1693 int extra_sep = 0;
1694 int wincount, totwincount = 0;
1695 frame_T *fr;
1696 int next_curwin_size = 0;
1697 int room = 0;
1698 int new_size;
1699 int has_next_curwin = 0;
1700 int hnc;
1702 if (topfr->fr_layout == FR_LEAF)
1704 /* Set the width/height of this frame.
1705 * Redraw when size or position changes */
1706 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1707 #ifdef FEAT_VERTSPLIT
1708 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1709 #endif
1712 topfr->fr_win->w_winrow = row;
1713 frame_new_height(topfr, height, FALSE, FALSE);
1714 #ifdef FEAT_VERTSPLIT
1715 topfr->fr_win->w_wincol = col;
1716 frame_new_width(topfr, width, FALSE, FALSE);
1717 #endif
1718 redraw_all_later(CLEAR);
1721 #ifdef FEAT_VERTSPLIT
1722 else if (topfr->fr_layout == FR_ROW)
1724 topfr->fr_width = width;
1725 topfr->fr_height = height;
1727 if (dir != 'v') /* equalize frame widths */
1729 /* Compute the maximum number of windows horizontally in this
1730 * frame. */
1731 n = frame_minwidth(topfr, NOWIN);
1732 /* add one for the rightmost window, it doesn't have a separator */
1733 if (col + width == Columns)
1734 extra_sep = 1;
1735 else
1736 extra_sep = 0;
1737 totwincount = (n + extra_sep) / (p_wmw + 1);
1738 has_next_curwin = frame_has_win(topfr, next_curwin);
1741 * Compute width for "next_curwin" window and room available for
1742 * other windows.
1743 * "m" is the minimal width when counting p_wiw for "next_curwin".
1745 m = frame_minwidth(topfr, next_curwin);
1746 room = width - m;
1747 if (room < 0)
1749 next_curwin_size = p_wiw + room;
1750 room = 0;
1752 else
1754 next_curwin_size = -1;
1755 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1757 /* If 'winfixwidth' set keep the window width if
1758 * possible.
1759 * Watch out for this window being the next_curwin. */
1760 if (frame_fixed_width(fr))
1762 n = frame_minwidth(fr, NOWIN);
1763 new_size = fr->fr_width;
1764 if (frame_has_win(fr, next_curwin))
1766 room += p_wiw - p_wmw;
1767 next_curwin_size = 0;
1768 if (new_size < p_wiw)
1769 new_size = p_wiw;
1771 else
1772 /* These windows don't use up room. */
1773 totwincount -= (n + (fr->fr_next == NULL
1774 ? extra_sep : 0)) / (p_wmw + 1);
1775 room -= new_size - n;
1776 if (room < 0)
1778 new_size += room;
1779 room = 0;
1781 fr->fr_newwidth = new_size;
1784 if (next_curwin_size == -1)
1786 if (!has_next_curwin)
1787 next_curwin_size = 0;
1788 else if (totwincount > 1
1789 && (room + (totwincount - 2))
1790 / (totwincount - 1) > p_wiw)
1792 /* Can make all windows wider than 'winwidth', spread
1793 * the room equally. */
1794 next_curwin_size = (room + p_wiw
1795 + (totwincount - 1) * p_wmw
1796 + (totwincount - 1)) / totwincount;
1797 room -= next_curwin_size - p_wiw;
1799 else
1800 next_curwin_size = p_wiw;
1804 if (has_next_curwin)
1805 --totwincount; /* don't count curwin */
1808 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1810 n = m = 0;
1811 wincount = 1;
1812 if (fr->fr_next == NULL)
1813 /* last frame gets all that remains (avoid roundoff error) */
1814 new_size = width;
1815 else if (dir == 'v')
1816 new_size = fr->fr_width;
1817 else if (frame_fixed_width(fr))
1819 new_size = fr->fr_newwidth;
1820 wincount = 0; /* doesn't count as a sizeable window */
1822 else
1824 /* Compute the maximum number of windows horiz. in "fr". */
1825 n = frame_minwidth(fr, NOWIN);
1826 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1827 / (p_wmw + 1);
1828 m = frame_minwidth(fr, next_curwin);
1829 if (has_next_curwin)
1830 hnc = frame_has_win(fr, next_curwin);
1831 else
1832 hnc = FALSE;
1833 if (hnc) /* don't count next_curwin */
1834 --wincount;
1835 if (totwincount == 0)
1836 new_size = room;
1837 else
1838 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1839 / totwincount;
1840 if (hnc) /* add next_curwin size */
1842 next_curwin_size -= p_wiw - (m - n);
1843 new_size += next_curwin_size;
1844 room -= new_size - next_curwin_size;
1846 else
1847 room -= new_size;
1848 new_size += n;
1851 /* Skip frame that is full width when splitting or closing a
1852 * window, unless equalizing all frames. */
1853 if (!current || dir != 'v' || topfr->fr_parent != NULL
1854 || (new_size != fr->fr_width)
1855 || frame_has_win(fr, next_curwin))
1856 win_equal_rec(next_curwin, current, fr, dir, col, row,
1857 new_size, height);
1858 col += new_size;
1859 width -= new_size;
1860 totwincount -= wincount;
1863 #endif
1864 else /* topfr->fr_layout == FR_COL */
1866 #ifdef FEAT_VERTSPLIT
1867 topfr->fr_width = width;
1868 #endif
1869 topfr->fr_height = height;
1871 if (dir != 'h') /* equalize frame heights */
1873 /* Compute maximum number of windows vertically in this frame. */
1874 n = frame_minheight(topfr, NOWIN);
1875 /* add one for the bottom window if it doesn't have a statusline */
1876 if (row + height == cmdline_row && p_ls == 0)
1877 extra_sep = 1;
1878 else
1879 extra_sep = 0;
1880 totwincount = (n + extra_sep) / (p_wmh + 1);
1881 has_next_curwin = frame_has_win(topfr, next_curwin);
1884 * Compute height for "next_curwin" window and room available for
1885 * other windows.
1886 * "m" is the minimal height when counting p_wh for "next_curwin".
1888 m = frame_minheight(topfr, next_curwin);
1889 room = height - m;
1890 if (room < 0)
1892 /* The room is less then 'winheight', use all space for the
1893 * current window. */
1894 next_curwin_size = p_wh + room;
1895 room = 0;
1897 else
1899 next_curwin_size = -1;
1900 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1902 /* If 'winfixheight' set keep the window height if
1903 * possible.
1904 * Watch out for this window being the next_curwin. */
1905 if (frame_fixed_height(fr))
1907 n = frame_minheight(fr, NOWIN);
1908 new_size = fr->fr_height;
1909 if (frame_has_win(fr, next_curwin))
1911 room += p_wh - p_wmh;
1912 next_curwin_size = 0;
1913 if (new_size < p_wh)
1914 new_size = p_wh;
1916 else
1917 /* These windows don't use up room. */
1918 totwincount -= (n + (fr->fr_next == NULL
1919 ? extra_sep : 0)) / (p_wmh + 1);
1920 room -= new_size - n;
1921 if (room < 0)
1923 new_size += room;
1924 room = 0;
1926 fr->fr_newheight = new_size;
1929 if (next_curwin_size == -1)
1931 if (!has_next_curwin)
1932 next_curwin_size = 0;
1933 else if (totwincount > 1
1934 && (room + (totwincount - 2))
1935 / (totwincount - 1) > p_wh)
1937 /* can make all windows higher than 'winheight',
1938 * spread the room equally. */
1939 next_curwin_size = (room + p_wh
1940 + (totwincount - 1) * p_wmh
1941 + (totwincount - 1)) / totwincount;
1942 room -= next_curwin_size - p_wh;
1944 else
1945 next_curwin_size = p_wh;
1949 if (has_next_curwin)
1950 --totwincount; /* don't count curwin */
1953 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1955 n = m = 0;
1956 wincount = 1;
1957 if (fr->fr_next == NULL)
1958 /* last frame gets all that remains (avoid roundoff error) */
1959 new_size = height;
1960 else if (dir == 'h')
1961 new_size = fr->fr_height;
1962 else if (frame_fixed_height(fr))
1964 new_size = fr->fr_newheight;
1965 wincount = 0; /* doesn't count as a sizeable window */
1967 else
1969 /* Compute the maximum number of windows vert. in "fr". */
1970 n = frame_minheight(fr, NOWIN);
1971 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1972 / (p_wmh + 1);
1973 m = frame_minheight(fr, next_curwin);
1974 if (has_next_curwin)
1975 hnc = frame_has_win(fr, next_curwin);
1976 else
1977 hnc = FALSE;
1978 if (hnc) /* don't count next_curwin */
1979 --wincount;
1980 if (totwincount == 0)
1981 new_size = room;
1982 else
1983 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1984 / totwincount;
1985 if (hnc) /* add next_curwin size */
1987 next_curwin_size -= p_wh - (m - n);
1988 new_size += next_curwin_size;
1989 room -= new_size - next_curwin_size;
1991 else
1992 room -= new_size;
1993 new_size += n;
1995 /* Skip frame that is full width when splitting or closing a
1996 * window, unless equalizing all frames. */
1997 if (!current || dir != 'h' || topfr->fr_parent != NULL
1998 || (new_size != fr->fr_height)
1999 || frame_has_win(fr, next_curwin))
2000 win_equal_rec(next_curwin, current, fr, dir, col, row,
2001 width, new_size);
2002 row += new_size;
2003 height -= new_size;
2004 totwincount -= wincount;
2010 * close all windows for buffer 'buf'
2012 void
2013 close_windows(buf, keep_curwin)
2014 buf_T *buf;
2015 int keep_curwin; /* don't close "curwin" */
2017 win_T *wp;
2018 tabpage_T *tp, *nexttp;
2019 int h = tabline_height();
2021 ++RedrawingDisabled;
2023 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2025 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2027 win_close(wp, FALSE);
2029 /* Start all over, autocommands may change the window layout. */
2030 wp = firstwin;
2032 else
2033 wp = wp->w_next;
2036 /* Also check windows in other tab pages. */
2037 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2039 nexttp = tp->tp_next;
2040 if (tp != curtab)
2041 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2042 if (wp->w_buffer == buf)
2044 win_close_othertab(wp, FALSE, tp);
2046 /* Start all over, the tab page may be closed and
2047 * autocommands may change the window layout. */
2048 nexttp = first_tabpage;
2049 break;
2053 --RedrawingDisabled;
2055 if (h != tabline_height())
2056 shell_new_rows();
2060 * Return TRUE if the current window is the only window that exists (ignoring
2061 * "aucmd_win").
2062 * Returns FALSE if there is a window, possibly in another tab page.
2064 static int
2065 last_window()
2067 return (one_window() && first_tabpage->tp_next == NULL);
2071 * Return TRUE if there is only one window other than "aucmd_win" in the
2072 * current tab page.
2074 static int
2075 one_window()
2077 #ifdef FEAT_AUTOCMD
2078 win_T *wp;
2079 int seen_one = FALSE;
2081 FOR_ALL_WINDOWS(wp)
2083 if (wp != aucmd_win)
2085 if (seen_one)
2086 return FALSE;
2087 seen_one = TRUE;
2090 return TRUE;
2091 #else
2092 return firstwin == lastwin;
2093 #endif
2097 * Close window "win". Only works for the current tab page.
2098 * If "free_buf" is TRUE related buffer may be unloaded.
2100 * called by :quit, :close, :xit, :wq and findtag()
2102 void
2103 win_close(win, free_buf)
2104 win_T *win;
2105 int free_buf;
2107 win_T *wp;
2108 #ifdef FEAT_AUTOCMD
2109 int other_buffer = FALSE;
2110 #endif
2111 int close_curwin = FALSE;
2112 int dir;
2113 int help_window = FALSE;
2114 tabpage_T *prev_curtab = curtab;
2116 if (last_window())
2118 EMSG(_("E444: Cannot close last window"));
2119 return;
2122 #ifdef FEAT_AUTOCMD
2123 if (win == aucmd_win)
2125 EMSG(_("E813: Cannot close autocmd window"));
2126 return;
2128 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2130 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2131 return;
2133 #endif
2136 * When closing the last window in a tab page first go to another tab
2137 * page and then close the window and the tab page. This avoids that
2138 * curwin and curtab are not invalid while we are freeing memory, they may
2139 * be used in GUI events.
2141 if (firstwin == lastwin)
2143 goto_tabpage_tp(alt_tabpage());
2144 redraw_tabline = TRUE;
2146 /* Safety check: Autocommands may have closed the window when jumping
2147 * to the other tab page. */
2148 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2150 int h = tabline_height();
2152 win_close_othertab(win, free_buf, prev_curtab);
2153 if (h != tabline_height())
2154 shell_new_rows();
2156 return;
2159 /* When closing the help window, try restoring a snapshot after closing
2160 * the window. Otherwise clear the snapshot, it's now invalid. */
2161 if (win->w_buffer->b_help)
2162 help_window = TRUE;
2163 else
2164 clear_snapshot(curtab, SNAP_HELP_IDX);
2166 #ifdef FEAT_AUTOCMD
2167 if (win == curwin)
2170 * Guess which window is going to be the new current window.
2171 * This may change because of the autocommands (sigh).
2173 wp = frame2win(win_altframe(win, NULL));
2176 * Be careful: If autocommands delete the window, return now.
2178 if (wp->w_buffer != curbuf)
2180 other_buffer = TRUE;
2181 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2182 if (!win_valid(win) || last_window())
2183 return;
2185 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2186 if (!win_valid(win) || last_window())
2187 return;
2188 # ifdef FEAT_EVAL
2189 /* autocmds may abort script processing */
2190 if (aborting())
2191 return;
2192 # endif
2194 #endif
2196 #ifdef FEAT_GUI
2197 /* Avoid trouble with scrollbars that are going to be deleted in
2198 * win_free(). */
2199 if (gui.in_use)
2200 out_flush();
2201 #endif
2204 * Close the link to the buffer.
2206 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2208 /* Autocommands may have closed the window already, or closed the only
2209 * other window or moved to another tab page. */
2210 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2211 return;
2213 /* Free the memory used for the window. */
2214 wp = win_free_mem(win, &dir, NULL);
2216 /* Make sure curwin isn't invalid. It can cause severe trouble when
2217 * printing an error message. For win_equal() curbuf needs to be valid
2218 * too. */
2219 if (win == curwin)
2221 curwin = wp;
2222 #ifdef FEAT_QUICKFIX
2223 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2226 * If the cursor goes to the preview or the quickfix window, try
2227 * finding another window to go to.
2229 for (;;)
2231 if (wp->w_next == NULL)
2232 wp = firstwin;
2233 else
2234 wp = wp->w_next;
2235 if (wp == curwin)
2236 break;
2237 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2239 curwin = wp;
2240 break;
2244 #endif
2245 curbuf = curwin->w_buffer;
2246 close_curwin = TRUE;
2248 if (p_ea
2249 #ifdef FEAT_VERTSPLIT
2250 && (*p_ead == 'b' || *p_ead == dir)
2251 #endif
2253 win_equal(curwin, TRUE,
2254 #ifdef FEAT_VERTSPLIT
2256 #else
2258 #endif
2260 else
2261 win_comp_pos();
2262 if (close_curwin)
2264 win_enter_ext(wp, FALSE, TRUE);
2265 #ifdef FEAT_AUTOCMD
2266 if (other_buffer)
2267 /* careful: after this wp and win may be invalid! */
2268 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2269 #endif
2273 * If last window has a status line now and we don't want one,
2274 * remove the status line.
2276 last_status(FALSE);
2278 /* After closing the help window, try restoring the window layout from
2279 * before it was opened. */
2280 if (help_window)
2281 restore_snapshot(SNAP_HELP_IDX, close_curwin);
2283 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2284 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2285 if (gui.in_use && !win_hasvertsplit())
2286 gui_init_which_components(NULL);
2287 #endif
2289 redraw_all_later(NOT_VALID);
2293 * Close window "win" in tab page "tp", which is not the current tab page.
2294 * This may be the last window ih that tab page and result in closing the tab,
2295 * thus "tp" may become invalid!
2296 * Caller must check if buffer is hidden and whether the tabline needs to be
2297 * updated.
2299 void
2300 win_close_othertab(win, free_buf, tp)
2301 win_T *win;
2302 int free_buf;
2303 tabpage_T *tp;
2305 win_T *wp;
2306 int dir;
2307 tabpage_T *ptp = NULL;
2309 /* Close the link to the buffer. */
2310 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2312 /* Careful: Autocommands may have closed the tab page or made it the
2313 * current tab page. */
2314 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2316 if (ptp == NULL || tp == curtab)
2317 return;
2319 /* Autocommands may have closed the window already. */
2320 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2322 if (wp == NULL)
2323 return;
2325 /* Free the memory used for the window. */
2326 wp = win_free_mem(win, &dir, tp);
2328 /* When closing the last window in a tab page remove the tab page. */
2329 if (wp == NULL)
2331 if (tp == first_tabpage)
2332 first_tabpage = tp->tp_next;
2333 else
2335 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2336 ptp = ptp->tp_next)
2338 if (ptp == NULL)
2340 EMSG2(_(e_intern2), "win_close_othertab()");
2341 return;
2343 ptp->tp_next = tp->tp_next;
2345 free_tabpage(tp);
2350 * Free the memory used for a window.
2351 * Returns a pointer to the window that got the freed up space.
2353 static win_T *
2354 win_free_mem(win, dirp, tp)
2355 win_T *win;
2356 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2357 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2359 frame_T *frp;
2360 win_T *wp;
2362 /* Remove the window and its frame from the tree of frames. */
2363 frp = win->w_frame;
2364 wp = winframe_remove(win, dirp, tp);
2365 vim_free(frp);
2366 win_free(win, tp);
2368 /* When deleting the current window of another tab page select a new
2369 * current window. */
2370 if (tp != NULL && win == tp->tp_curwin)
2371 tp->tp_curwin = wp;
2373 return wp;
2376 #if defined(EXITFREE) || defined(PROTO)
2377 void
2378 win_free_all()
2380 int dummy;
2382 # ifdef FEAT_WINDOWS
2383 while (first_tabpage->tp_next != NULL)
2384 tabpage_close(TRUE);
2385 # endif
2387 # ifdef FEAT_AUTOCMD
2388 if (aucmd_win != NULL)
2390 (void)win_free_mem(aucmd_win, &dummy, NULL);
2391 aucmd_win = NULL;
2393 # endif
2395 while (firstwin != NULL)
2396 (void)win_free_mem(firstwin, &dummy, NULL);
2398 #endif
2401 * Remove a window and its frame from the tree of frames.
2402 * Returns a pointer to the window that got the freed up space.
2404 win_T *
2405 winframe_remove(win, dirp, tp)
2406 win_T *win;
2407 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
2408 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2410 frame_T *frp, *frp2, *frp3;
2411 frame_T *frp_close = win->w_frame;
2412 win_T *wp;
2415 * If there is only one window there is nothing to remove.
2417 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2418 return NULL;
2421 * Remove the window from its frame.
2423 frp2 = win_altframe(win, tp);
2424 wp = frame2win(frp2);
2426 /* Remove this frame from the list of frames. */
2427 frame_remove(frp_close);
2429 #ifdef FEAT_VERTSPLIT
2430 if (frp_close->fr_parent->fr_layout == FR_COL)
2432 #endif
2433 /* When 'winfixheight' is set, try to find another frame in the column
2434 * (as close to the closed frame as possible) to distribute the height
2435 * to. */
2436 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2438 frp = frp_close->fr_prev;
2439 frp3 = frp_close->fr_next;
2440 while (frp != NULL || frp3 != NULL)
2442 if (frp != NULL)
2444 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2446 frp2 = frp;
2447 wp = frp->fr_win;
2448 break;
2450 frp = frp->fr_prev;
2452 if (frp3 != NULL)
2454 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2456 frp2 = frp3;
2457 wp = frp3->fr_win;
2458 break;
2460 frp3 = frp3->fr_next;
2464 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2465 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2466 #ifdef FEAT_VERTSPLIT
2467 *dirp = 'v';
2469 else
2471 /* When 'winfixwidth' is set, try to find another frame in the column
2472 * (as close to the closed frame as possible) to distribute the width
2473 * to. */
2474 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2476 frp = frp_close->fr_prev;
2477 frp3 = frp_close->fr_next;
2478 while (frp != NULL || frp3 != NULL)
2480 if (frp != NULL)
2482 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2484 frp2 = frp;
2485 wp = frp->fr_win;
2486 break;
2488 frp = frp->fr_prev;
2490 if (frp3 != NULL)
2492 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2494 frp2 = frp3;
2495 wp = frp3->fr_win;
2496 break;
2498 frp3 = frp3->fr_next;
2502 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2503 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2504 *dirp = 'h';
2506 #endif
2508 /* If rows/columns go to a window below/right its positions need to be
2509 * updated. Can only be done after the sizes have been updated. */
2510 if (frp2 == frp_close->fr_next)
2512 int row = win->w_winrow;
2513 int col = W_WINCOL(win);
2515 frame_comp_pos(frp2, &row, &col);
2518 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2520 /* There is no other frame in this list, move its info to the parent
2521 * and remove it. */
2522 frp2->fr_parent->fr_layout = frp2->fr_layout;
2523 frp2->fr_parent->fr_child = frp2->fr_child;
2524 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2525 frp->fr_parent = frp2->fr_parent;
2526 frp2->fr_parent->fr_win = frp2->fr_win;
2527 if (frp2->fr_win != NULL)
2528 frp2->fr_win->w_frame = frp2->fr_parent;
2529 frp = frp2->fr_parent;
2530 vim_free(frp2);
2532 frp2 = frp->fr_parent;
2533 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2535 /* The frame above the parent has the same layout, have to merge
2536 * the frames into this list. */
2537 if (frp2->fr_child == frp)
2538 frp2->fr_child = frp->fr_child;
2539 frp->fr_child->fr_prev = frp->fr_prev;
2540 if (frp->fr_prev != NULL)
2541 frp->fr_prev->fr_next = frp->fr_child;
2542 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2544 frp3->fr_parent = frp2;
2545 if (frp3->fr_next == NULL)
2547 frp3->fr_next = frp->fr_next;
2548 if (frp->fr_next != NULL)
2549 frp->fr_next->fr_prev = frp3;
2550 break;
2553 vim_free(frp);
2557 return wp;
2561 * Find out which frame is going to get the freed up space when "win" is
2562 * closed.
2563 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2564 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2565 * This makes opening a window and closing it immediately keep the same window
2566 * layout.
2568 static frame_T *
2569 win_altframe(win, tp)
2570 win_T *win;
2571 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2573 frame_T *frp;
2574 int b;
2576 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2577 /* Last window in this tab page, will go to next tab page. */
2578 return alt_tabpage()->tp_curwin->w_frame;
2580 frp = win->w_frame;
2581 #ifdef FEAT_VERTSPLIT
2582 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2583 b = p_spr;
2584 else
2585 #endif
2586 b = p_sb;
2587 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2588 return frp->fr_next;
2589 return frp->fr_prev;
2593 * Return the tabpage that will be used if the current one is closed.
2595 static tabpage_T *
2596 alt_tabpage()
2598 tabpage_T *tp;
2600 /* Use the next tab page if possible. */
2601 if (curtab->tp_next != NULL)
2602 return curtab->tp_next;
2604 /* Find the last but one tab page. */
2605 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2607 return tp;
2611 * Find the left-upper window in frame "frp".
2613 static win_T *
2614 frame2win(frp)
2615 frame_T *frp;
2617 while (frp->fr_win == NULL)
2618 frp = frp->fr_child;
2619 return frp->fr_win;
2623 * Return TRUE if frame "frp" contains window "wp".
2625 static int
2626 frame_has_win(frp, wp)
2627 frame_T *frp;
2628 win_T *wp;
2630 frame_T *p;
2632 if (frp->fr_layout == FR_LEAF)
2633 return frp->fr_win == wp;
2635 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2636 if (frame_has_win(p, wp))
2637 return TRUE;
2638 return FALSE;
2642 * Set a new height for a frame. Recursively sets the height for contained
2643 * frames and windows. Caller must take care of positions.
2645 static void
2646 frame_new_height(topfrp, height, topfirst, wfh)
2647 frame_T *topfrp;
2648 int height;
2649 int topfirst; /* resize topmost contained frame first */
2650 int wfh; /* obey 'winfixheight' when there is a choice;
2651 may cause the height not to be set */
2653 frame_T *frp;
2654 int extra_lines;
2655 int h;
2657 if (topfrp->fr_win != NULL)
2659 /* Simple case: just one window. */
2660 win_new_height(topfrp->fr_win,
2661 height - topfrp->fr_win->w_status_height);
2663 #ifdef FEAT_VERTSPLIT
2664 else if (topfrp->fr_layout == FR_ROW)
2668 /* All frames in this row get the same new height. */
2669 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2671 frame_new_height(frp, height, topfirst, wfh);
2672 if (frp->fr_height > height)
2674 /* Could not fit the windows, make the whole row higher. */
2675 height = frp->fr_height;
2676 break;
2680 while (frp != NULL);
2682 #endif
2683 else /* fr_layout == FR_COL */
2685 /* Complicated case: Resize a column of frames. Resize the bottom
2686 * frame first, frames above that when needed. */
2688 frp = topfrp->fr_child;
2689 if (wfh)
2690 /* Advance past frames with one window with 'wfh' set. */
2691 while (frame_fixed_height(frp))
2693 frp = frp->fr_next;
2694 if (frp == NULL)
2695 return; /* no frame without 'wfh', give up */
2697 if (!topfirst)
2699 /* Find the bottom frame of this column */
2700 while (frp->fr_next != NULL)
2701 frp = frp->fr_next;
2702 if (wfh)
2703 /* Advance back for frames with one window with 'wfh' set. */
2704 while (frame_fixed_height(frp))
2705 frp = frp->fr_prev;
2708 extra_lines = height - topfrp->fr_height;
2709 if (extra_lines < 0)
2711 /* reduce height of contained frames, bottom or top frame first */
2712 while (frp != NULL)
2714 h = frame_minheight(frp, NULL);
2715 if (frp->fr_height + extra_lines < h)
2717 extra_lines += frp->fr_height - h;
2718 frame_new_height(frp, h, topfirst, wfh);
2720 else
2722 frame_new_height(frp, frp->fr_height + extra_lines,
2723 topfirst, wfh);
2724 break;
2726 if (topfirst)
2729 frp = frp->fr_next;
2730 while (wfh && frp != NULL && frame_fixed_height(frp));
2732 else
2735 frp = frp->fr_prev;
2736 while (wfh && frp != NULL && frame_fixed_height(frp));
2738 /* Increase "height" if we could not reduce enough frames. */
2739 if (frp == NULL)
2740 height -= extra_lines;
2743 else if (extra_lines > 0)
2745 /* increase height of bottom or top frame */
2746 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2749 topfrp->fr_height = height;
2753 * Return TRUE if height of frame "frp" should not be changed because of
2754 * the 'winfixheight' option.
2756 static int
2757 frame_fixed_height(frp)
2758 frame_T *frp;
2760 /* frame with one window: fixed height if 'winfixheight' set. */
2761 if (frp->fr_win != NULL)
2762 return frp->fr_win->w_p_wfh;
2764 if (frp->fr_layout == FR_ROW)
2766 /* The frame is fixed height if one of the frames in the row is fixed
2767 * height. */
2768 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2769 if (frame_fixed_height(frp))
2770 return TRUE;
2771 return FALSE;
2774 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2775 * frames in the row are fixed height. */
2776 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2777 if (!frame_fixed_height(frp))
2778 return FALSE;
2779 return TRUE;
2782 #ifdef FEAT_VERTSPLIT
2784 * Return TRUE if width of frame "frp" should not be changed because of
2785 * the 'winfixwidth' option.
2787 static int
2788 frame_fixed_width(frp)
2789 frame_T *frp;
2791 /* frame with one window: fixed width if 'winfixwidth' set. */
2792 if (frp->fr_win != NULL)
2793 return frp->fr_win->w_p_wfw;
2795 if (frp->fr_layout == FR_COL)
2797 /* The frame is fixed width if one of the frames in the row is fixed
2798 * width. */
2799 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2800 if (frame_fixed_width(frp))
2801 return TRUE;
2802 return FALSE;
2805 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2806 * frames in the row are fixed width. */
2807 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2808 if (!frame_fixed_width(frp))
2809 return FALSE;
2810 return TRUE;
2814 * Add a status line to windows at the bottom of "frp".
2815 * Note: Does not check if there is room!
2817 static void
2818 frame_add_statusline(frp)
2819 frame_T *frp;
2821 win_T *wp;
2823 if (frp->fr_layout == FR_LEAF)
2825 wp = frp->fr_win;
2826 if (wp->w_status_height == 0)
2828 if (wp->w_height > 0) /* don't make it negative */
2829 --wp->w_height;
2830 wp->w_status_height = STATUS_HEIGHT;
2833 else if (frp->fr_layout == FR_ROW)
2835 /* Handle all the frames in the row. */
2836 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2837 frame_add_statusline(frp);
2839 else /* frp->fr_layout == FR_COL */
2841 /* Only need to handle the last frame in the column. */
2842 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2844 frame_add_statusline(frp);
2849 * Set width of a frame. Handles recursively going through contained frames.
2850 * May remove separator line for windows at the right side (for win_close()).
2852 static void
2853 frame_new_width(topfrp, width, leftfirst, wfw)
2854 frame_T *topfrp;
2855 int width;
2856 int leftfirst; /* resize leftmost contained frame first */
2857 int wfw; /* obey 'winfixwidth' when there is a choice;
2858 may cause the width not to be set */
2860 frame_T *frp;
2861 int extra_cols;
2862 int w;
2863 win_T *wp;
2865 if (topfrp->fr_layout == FR_LEAF)
2867 /* Simple case: just one window. */
2868 wp = topfrp->fr_win;
2869 /* Find out if there are any windows right of this one. */
2870 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2871 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2872 break;
2873 if (frp->fr_parent == NULL)
2874 wp->w_vsep_width = 0;
2875 win_new_width(wp, width - wp->w_vsep_width);
2877 else if (topfrp->fr_layout == FR_COL)
2881 /* All frames in this column get the same new width. */
2882 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2884 frame_new_width(frp, width, leftfirst, wfw);
2885 if (frp->fr_width > width)
2887 /* Could not fit the windows, make whole column wider. */
2888 width = frp->fr_width;
2889 break;
2892 } while (frp != NULL);
2894 else /* fr_layout == FR_ROW */
2896 /* Complicated case: Resize a row of frames. Resize the rightmost
2897 * frame first, frames left of it when needed. */
2899 frp = topfrp->fr_child;
2900 if (wfw)
2901 /* Advance past frames with one window with 'wfw' set. */
2902 while (frame_fixed_width(frp))
2904 frp = frp->fr_next;
2905 if (frp == NULL)
2906 return; /* no frame without 'wfw', give up */
2908 if (!leftfirst)
2910 /* Find the rightmost frame of this row */
2911 while (frp->fr_next != NULL)
2912 frp = frp->fr_next;
2913 if (wfw)
2914 /* Advance back for frames with one window with 'wfw' set. */
2915 while (frame_fixed_width(frp))
2916 frp = frp->fr_prev;
2919 extra_cols = width - topfrp->fr_width;
2920 if (extra_cols < 0)
2922 /* reduce frame width, rightmost frame first */
2923 while (frp != NULL)
2925 w = frame_minwidth(frp, NULL);
2926 if (frp->fr_width + extra_cols < w)
2928 extra_cols += frp->fr_width - w;
2929 frame_new_width(frp, w, leftfirst, wfw);
2931 else
2933 frame_new_width(frp, frp->fr_width + extra_cols,
2934 leftfirst, wfw);
2935 break;
2937 if (leftfirst)
2940 frp = frp->fr_next;
2941 while (wfw && frp != NULL && frame_fixed_width(frp));
2943 else
2946 frp = frp->fr_prev;
2947 while (wfw && frp != NULL && frame_fixed_width(frp));
2949 /* Increase "width" if we could not reduce enough frames. */
2950 if (frp == NULL)
2951 width -= extra_cols;
2954 else if (extra_cols > 0)
2956 /* increase width of rightmost frame */
2957 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2960 topfrp->fr_width = width;
2964 * Add the vertical separator to windows at the right side of "frp".
2965 * Note: Does not check if there is room!
2967 static void
2968 frame_add_vsep(frp)
2969 frame_T *frp;
2971 win_T *wp;
2973 if (frp->fr_layout == FR_LEAF)
2975 wp = frp->fr_win;
2976 if (wp->w_vsep_width == 0)
2978 if (wp->w_width > 0) /* don't make it negative */
2979 --wp->w_width;
2980 wp->w_vsep_width = 1;
2983 else if (frp->fr_layout == FR_COL)
2985 /* Handle all the frames in the column. */
2986 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2987 frame_add_vsep(frp);
2989 else /* frp->fr_layout == FR_ROW */
2991 /* Only need to handle the last frame in the row. */
2992 frp = frp->fr_child;
2993 while (frp->fr_next != NULL)
2994 frp = frp->fr_next;
2995 frame_add_vsep(frp);
3000 * Set frame width from the window it contains.
3002 static void
3003 frame_fix_width(wp)
3004 win_T *wp;
3006 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3008 #endif
3011 * Set frame height from the window it contains.
3013 static void
3014 frame_fix_height(wp)
3015 win_T *wp;
3017 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3021 * Compute the minimal height for frame "topfrp".
3022 * Uses the 'winminheight' option.
3023 * When "next_curwin" isn't NULL, use p_wh for this window.
3024 * When "next_curwin" is NOWIN, don't use at least one line for the current
3025 * window.
3027 static int
3028 frame_minheight(topfrp, next_curwin)
3029 frame_T *topfrp;
3030 win_T *next_curwin;
3032 frame_T *frp;
3033 int m;
3034 #ifdef FEAT_VERTSPLIT
3035 int n;
3036 #endif
3038 if (topfrp->fr_win != NULL)
3040 if (topfrp->fr_win == next_curwin)
3041 m = p_wh + topfrp->fr_win->w_status_height;
3042 else
3044 /* window: minimal height of the window plus status line */
3045 m = p_wmh + topfrp->fr_win->w_status_height;
3046 /* Current window is minimal one line high */
3047 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3048 ++m;
3051 #ifdef FEAT_VERTSPLIT
3052 else if (topfrp->fr_layout == FR_ROW)
3054 /* get the minimal height from each frame in this row */
3055 m = 0;
3056 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3058 n = frame_minheight(frp, next_curwin);
3059 if (n > m)
3060 m = n;
3063 #endif
3064 else
3066 /* Add up the minimal heights for all frames in this column. */
3067 m = 0;
3068 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3069 m += frame_minheight(frp, next_curwin);
3072 return m;
3075 #ifdef FEAT_VERTSPLIT
3077 * Compute the minimal width for frame "topfrp".
3078 * When "next_curwin" isn't NULL, use p_wiw for this window.
3079 * When "next_curwin" is NOWIN, don't use at least one column for the current
3080 * window.
3082 static int
3083 frame_minwidth(topfrp, next_curwin)
3084 frame_T *topfrp;
3085 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3087 frame_T *frp;
3088 int m, n;
3090 if (topfrp->fr_win != NULL)
3092 if (topfrp->fr_win == next_curwin)
3093 m = p_wiw + topfrp->fr_win->w_vsep_width;
3094 else
3096 /* window: minimal width of the window plus separator column */
3097 m = p_wmw + topfrp->fr_win->w_vsep_width;
3098 /* Current window is minimal one column wide */
3099 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3100 ++m;
3103 else if (topfrp->fr_layout == FR_COL)
3105 /* get the minimal width from each frame in this column */
3106 m = 0;
3107 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3109 n = frame_minwidth(frp, next_curwin);
3110 if (n > m)
3111 m = n;
3114 else
3116 /* Add up the minimal widths for all frames in this row. */
3117 m = 0;
3118 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3119 m += frame_minwidth(frp, next_curwin);
3122 return m;
3124 #endif
3128 * Try to close all windows except current one.
3129 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3130 * used and the buffer was modified.
3132 * Used by ":bdel" and ":only".
3134 void
3135 close_others(message, forceit)
3136 int message;
3137 int forceit; /* always hide all other windows */
3139 win_T *wp;
3140 win_T *nextwp;
3141 int r;
3143 if (one_window())
3145 if (message
3146 #ifdef FEAT_AUTOCMD
3147 && !autocmd_busy
3148 #endif
3150 MSG(_(m_onlyone));
3151 return;
3154 /* Be very careful here: autocommands may change the window layout. */
3155 for (wp = firstwin; win_valid(wp); wp = nextwp)
3157 nextwp = wp->w_next;
3158 if (wp != curwin) /* don't close current window */
3161 /* Check if it's allowed to abandon this window */
3162 r = can_abandon(wp->w_buffer, forceit);
3163 #ifdef FEAT_AUTOCMD
3164 if (!win_valid(wp)) /* autocommands messed wp up */
3166 nextwp = firstwin;
3167 continue;
3169 #endif
3170 if (!r)
3172 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3173 if (message && (p_confirm || cmdmod.confirm) && p_write)
3175 dialog_changed(wp->w_buffer, FALSE);
3176 # ifdef FEAT_AUTOCMD
3177 if (!win_valid(wp)) /* autocommands messed wp up */
3179 nextwp = firstwin;
3180 continue;
3182 # endif
3184 if (bufIsChanged(wp->w_buffer))
3185 #endif
3186 continue;
3188 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3192 if (message && lastwin != firstwin)
3193 EMSG(_("E445: Other window contains changes"));
3196 #endif /* FEAT_WINDOWS */
3199 * Init the current window "curwin".
3200 * Called when a new file is being edited.
3202 void
3203 curwin_init()
3205 win_init_empty(curwin);
3208 void
3209 win_init_empty(wp)
3210 win_T *wp;
3212 redraw_win_later(wp, NOT_VALID);
3213 wp->w_lines_valid = 0;
3214 wp->w_cursor.lnum = 1;
3215 wp->w_curswant = wp->w_cursor.col = 0;
3216 #ifdef FEAT_VIRTUALEDIT
3217 wp->w_cursor.coladd = 0;
3218 #endif
3219 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3220 wp->w_pcmark.col = 0;
3221 wp->w_prev_pcmark.lnum = 0;
3222 wp->w_prev_pcmark.col = 0;
3223 wp->w_topline = 1;
3224 #ifdef FEAT_DIFF
3225 wp->w_topfill = 0;
3226 #endif
3227 wp->w_botline = 2;
3228 #ifdef FEAT_FKMAP
3229 if (wp->w_p_rl)
3230 wp->w_farsi = W_CONV + W_R_L;
3231 else
3232 wp->w_farsi = W_CONV;
3233 #endif
3237 * Allocate the first window and put an empty buffer in it.
3238 * Called from main().
3239 * Return FAIL when something goes wrong (out of memory).
3242 win_alloc_first()
3244 if (win_alloc_firstwin(NULL) == FAIL)
3245 return FAIL;
3247 #ifdef FEAT_WINDOWS
3248 first_tabpage = alloc_tabpage();
3249 if (first_tabpage == NULL)
3250 return FAIL;
3251 first_tabpage->tp_topframe = topframe;
3252 curtab = first_tabpage;
3253 #endif
3255 return OK;
3258 #if defined(FEAT_AUTOCMD) || defined(PROTO)
3260 * Init "aucmd_win". This can only be done after the first
3261 * window is fully initialized, thus it can't be in win_alloc_first().
3263 void
3264 win_alloc_aucmd_win()
3266 aucmd_win = win_alloc(NULL, TRUE);
3267 if (aucmd_win != NULL)
3269 win_init_some(aucmd_win, curwin);
3270 # ifdef FEAT_SCROLLBIND
3271 aucmd_win->w_p_scb = FALSE;
3272 # endif
3273 new_frame(aucmd_win);
3276 #endif
3279 * Allocate the first window or the first window in a new tab page.
3280 * When "oldwin" is NULL create an empty buffer for it.
3281 * When "oldwin" is not NULL copy info from it to the new window (only with
3282 * FEAT_WINDOWS).
3283 * Return FAIL when something goes wrong (out of memory).
3285 static int
3286 win_alloc_firstwin(oldwin)
3287 win_T *oldwin;
3289 curwin = win_alloc(NULL, FALSE);
3290 if (oldwin == NULL)
3292 /* Very first window, need to create an empty buffer for it and
3293 * initialize from scratch. */
3294 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3295 if (curwin == NULL || curbuf == NULL)
3296 return FAIL;
3297 curwin->w_buffer = curbuf;
3298 curbuf->b_nwindows = 1; /* there is one window */
3299 #ifdef FEAT_WINDOWS
3300 curwin->w_alist = &global_alist;
3301 #endif
3302 curwin_init(); /* init current window */
3304 #ifdef FEAT_WINDOWS
3305 else
3307 /* First window in new tab page, initialize it from "oldwin". */
3308 win_init(curwin, oldwin, 0);
3310 # ifdef FEAT_SCROLLBIND
3311 /* We don't want scroll-binding in the first window. */
3312 curwin->w_p_scb = FALSE;
3313 # endif
3315 #endif
3317 new_frame(curwin);
3318 if (curwin->w_frame == NULL)
3319 return FAIL;
3320 topframe = curwin->w_frame;
3321 #ifdef FEAT_VERTSPLIT
3322 topframe->fr_width = Columns;
3323 #endif
3324 topframe->fr_height = Rows - p_ch;
3325 topframe->fr_win = curwin;
3327 return OK;
3331 * Create a frame for window "wp".
3333 static void
3334 new_frame(win_T *wp)
3336 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3338 wp->w_frame = frp;
3339 if (frp != NULL)
3341 frp->fr_layout = FR_LEAF;
3342 frp->fr_win = wp;
3347 * Initialize the window and frame size to the maximum.
3349 void
3350 win_init_size()
3352 firstwin->w_height = ROWS_AVAIL;
3353 topframe->fr_height = ROWS_AVAIL;
3354 #ifdef FEAT_VERTSPLIT
3355 firstwin->w_width = Columns;
3356 topframe->fr_width = Columns;
3357 #endif
3360 #if defined(FEAT_WINDOWS) || defined(PROTO)
3363 * Allocate a new tabpage_T and init the values.
3364 * Returns NULL when out of memory.
3366 static tabpage_T *
3367 alloc_tabpage()
3369 tabpage_T *tp;
3371 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3372 if (tp != NULL)
3374 # ifdef FEAT_GUI
3375 int i;
3377 for (i = 0; i < 3; i++)
3378 tp->tp_prev_which_scrollbars[i] = -1;
3379 # endif
3380 # ifdef FEAT_DIFF
3381 tp->tp_diff_invalid = TRUE;
3382 # endif
3383 #ifdef FEAT_EVAL
3384 /* init t: variables */
3385 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3386 #endif
3387 tp->tp_ch_used = p_ch;
3389 return tp;
3392 void
3393 free_tabpage(tp)
3394 tabpage_T *tp;
3396 int idx;
3398 # ifdef FEAT_DIFF
3399 diff_clear(tp);
3400 # endif
3401 for (idx = 0; idx < SNAP_COUNT; ++idx)
3402 clear_snapshot(tp, idx);
3403 #ifdef FEAT_EVAL
3404 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3405 #endif
3406 vim_free(tp);
3410 * Create a new Tab page with one window.
3411 * It will edit the current buffer, like after ":split".
3412 * When "after" is 0 put it just after the current Tab page.
3413 * Otherwise put it just before tab page "after".
3414 * Return FAIL or OK.
3417 win_new_tabpage(after)
3418 int after;
3420 tabpage_T *tp = curtab;
3421 tabpage_T *newtp;
3422 int n;
3424 newtp = alloc_tabpage();
3425 if (newtp == NULL)
3426 return FAIL;
3428 /* Remember the current windows in this Tab page. */
3429 if (leave_tabpage(curbuf) == FAIL)
3431 vim_free(newtp);
3432 return FAIL;
3434 curtab = newtp;
3436 /* Create a new empty window. */
3437 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3439 /* Make the new Tab page the new topframe. */
3440 if (after == 1)
3442 /* New tab page becomes the first one. */
3443 newtp->tp_next = first_tabpage;
3444 first_tabpage = newtp;
3446 else
3448 if (after > 0)
3450 /* Put new tab page before tab page "after". */
3451 n = 2;
3452 for (tp = first_tabpage; tp->tp_next != NULL
3453 && n < after; tp = tp->tp_next)
3454 ++n;
3456 newtp->tp_next = tp->tp_next;
3457 tp->tp_next = newtp;
3459 win_init_size();
3460 firstwin->w_winrow = tabline_height();
3461 win_comp_scroll(curwin);
3463 newtp->tp_topframe = topframe;
3464 last_status(FALSE);
3466 #if defined(FEAT_GUI)
3467 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3468 * scrollbars. Have to update them anyway. */
3469 gui_may_update_scrollbars();
3470 #endif
3472 redraw_all_later(CLEAR);
3473 #ifdef FEAT_AUTOCMD
3474 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3475 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3476 #endif
3477 return OK;
3480 /* Failed, get back the previous Tab page */
3481 enter_tabpage(curtab, curbuf);
3482 return FAIL;
3486 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3487 * like with ":split".
3488 * Returns OK if a new tab page was created, FAIL otherwise.
3491 may_open_tabpage()
3493 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3495 if (n != 0)
3497 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3498 postponed_split_tab = 0;
3499 return win_new_tabpage(n);
3501 return FAIL;
3505 * Create up to "maxcount" tabpages with empty windows.
3506 * Returns the number of resulting tab pages.
3509 make_tabpages(maxcount)
3510 int maxcount;
3512 int count = maxcount;
3513 int todo;
3515 /* Limit to 'tabpagemax' tabs. */
3516 if (count > p_tpm)
3517 count = p_tpm;
3519 #ifdef FEAT_AUTOCMD
3521 * Don't execute autocommands while creating the tab pages. Must do that
3522 * when putting the buffers in the windows.
3524 block_autocmds();
3525 #endif
3527 for (todo = count - 1; todo > 0; --todo)
3528 if (win_new_tabpage(0) == FAIL)
3529 break;
3531 #ifdef FEAT_AUTOCMD
3532 unblock_autocmds();
3533 #endif
3535 /* return actual number of tab pages */
3536 return (count - todo);
3540 * Return TRUE when "tpc" points to a valid tab page.
3543 valid_tabpage(tpc)
3544 tabpage_T *tpc;
3546 tabpage_T *tp;
3548 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3549 if (tp == tpc)
3550 return TRUE;
3551 return FALSE;
3555 * Find tab page "n" (first one is 1). Returns NULL when not found.
3557 tabpage_T *
3558 find_tabpage(n)
3559 int n;
3561 tabpage_T *tp;
3562 int i = 1;
3564 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3565 ++i;
3566 return tp;
3570 * Get index of tab page "tp". First one has index 1.
3571 * When not found returns number of tab pages plus one.
3574 tabpage_index(ftp)
3575 tabpage_T *ftp;
3577 int i = 1;
3578 tabpage_T *tp;
3580 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3581 ++i;
3582 return i;
3586 * Prepare for leaving the current tab page.
3587 * When autocomands change "curtab" we don't leave the tab page and return
3588 * FAIL.
3589 * Careful: When OK is returned need to get a new tab page very very soon!
3591 static int
3592 leave_tabpage(new_curbuf)
3593 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
3594 NULL if unknown */
3596 tabpage_T *tp = curtab;
3598 #ifdef FEAT_VISUAL
3599 reset_VIsual_and_resel(); /* stop Visual mode */
3600 #endif
3601 #ifdef FEAT_AUTOCMD
3602 if (new_curbuf != curbuf)
3604 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3605 if (curtab != tp)
3606 return FAIL;
3608 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3609 if (curtab != tp)
3610 return FAIL;
3611 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3612 if (curtab != tp)
3613 return FAIL;
3614 #endif
3615 #if defined(FEAT_GUI)
3616 /* Remove the scrollbars. They may be added back later. */
3617 if (gui.in_use)
3618 gui_remove_scrollbars();
3619 #endif
3620 tp->tp_curwin = curwin;
3621 tp->tp_prevwin = prevwin;
3622 tp->tp_firstwin = firstwin;
3623 tp->tp_lastwin = lastwin;
3624 tp->tp_old_Rows = Rows;
3625 tp->tp_old_Columns = Columns;
3626 firstwin = NULL;
3627 lastwin = NULL;
3628 return OK;
3632 * Start using tab page "tp".
3633 * Only to be used after leave_tabpage() or freeing the current tab page.
3635 static void
3636 enter_tabpage(tp, old_curbuf)
3637 tabpage_T *tp;
3638 buf_T *old_curbuf UNUSED;
3640 int old_off = tp->tp_firstwin->w_winrow;
3641 win_T *next_prevwin = tp->tp_prevwin;
3643 curtab = tp;
3644 firstwin = tp->tp_firstwin;
3645 lastwin = tp->tp_lastwin;
3646 topframe = tp->tp_topframe;
3648 /* We would like doing the TabEnter event first, but we don't have a
3649 * valid current window yet, which may break some commands.
3650 * This triggers autocommands, thus may make "tp" invalid. */
3651 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3652 prevwin = next_prevwin;
3654 #ifdef FEAT_AUTOCMD
3655 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3657 if (old_curbuf != curbuf)
3658 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3659 #endif
3661 last_status(FALSE); /* status line may appear or disappear */
3662 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3663 must_redraw = CLEAR; /* need to redraw everything */
3664 #ifdef FEAT_DIFF
3665 diff_need_scrollbind = TRUE;
3666 #endif
3668 /* The tabpage line may have appeared or disappeared, may need to resize
3669 * the frames for that. When the Vim window was resized need to update
3670 * frame sizes too. Use the stored value of p_ch, so that it can be
3671 * different for each tab page. */
3672 p_ch = curtab->tp_ch_used;
3673 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3674 #ifdef FEAT_GUI_TABLINE
3675 && !gui_use_tabline()
3676 #endif
3678 shell_new_rows();
3679 #ifdef FEAT_VERTSPLIT
3680 if (curtab->tp_old_Columns != Columns && starting == 0)
3681 shell_new_columns(); /* update window widths */
3682 #endif
3684 #if defined(FEAT_GUI)
3685 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3686 * scrollbars. Have to update them anyway. */
3687 gui_may_update_scrollbars();
3688 #endif
3690 redraw_all_later(CLEAR);
3694 * Go to tab page "n". For ":tab N" and "Ngt".
3695 * When "n" is 9999 go to the last tab page.
3697 void
3698 goto_tabpage(n)
3699 int n;
3701 tabpage_T *tp;
3702 tabpage_T *ttp;
3703 int i;
3705 if (text_locked())
3707 /* Not allowed when editing the command line. */
3708 #ifdef FEAT_CMDWIN
3709 if (cmdwin_type != 0)
3710 EMSG(_(e_cmdwin));
3711 else
3712 #endif
3713 EMSG(_(e_secure));
3714 return;
3717 /* If there is only one it can't work. */
3718 if (first_tabpage->tp_next == NULL)
3720 if (n > 1)
3721 beep_flush();
3722 return;
3725 if (n == 0)
3727 /* No count, go to next tab page, wrap around end. */
3728 if (curtab->tp_next == NULL)
3729 tp = first_tabpage;
3730 else
3731 tp = curtab->tp_next;
3733 else if (n < 0)
3735 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3736 * this N times. */
3737 ttp = curtab;
3738 for (i = n; i < 0; ++i)
3740 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3741 tp = tp->tp_next)
3743 ttp = tp;
3746 else if (n == 9999)
3748 /* Go to last tab page. */
3749 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3752 else
3754 /* Go to tab page "n". */
3755 tp = find_tabpage(n);
3756 if (tp == NULL)
3758 beep_flush();
3759 return;
3763 goto_tabpage_tp(tp);
3765 #ifdef FEAT_GUI_TABLINE
3766 if (gui_use_tabline())
3767 gui_mch_set_curtab(tabpage_index(curtab));
3768 #endif
3772 * Go to tabpage "tp".
3773 * Note: doesn't update the GUI tab.
3775 void
3776 goto_tabpage_tp(tp)
3777 tabpage_T *tp;
3779 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3781 if (valid_tabpage(tp))
3782 enter_tabpage(tp, curbuf);
3783 else
3784 enter_tabpage(curtab, curbuf);
3789 * Enter window "wp" in tab page "tp".
3790 * Also updates the GUI tab.
3792 void
3793 goto_tabpage_win(tp, wp)
3794 tabpage_T *tp;
3795 win_T *wp;
3797 goto_tabpage_tp(tp);
3798 if (curtab == tp && win_valid(wp))
3800 win_enter(wp, TRUE);
3801 # ifdef FEAT_GUI_TABLINE
3802 if (gui_use_tabline())
3803 gui_mch_set_curtab(tabpage_index(curtab));
3804 # endif
3809 * Move the current tab page to before tab page "nr".
3811 void
3812 tabpage_move(nr)
3813 int nr;
3815 int n = nr;
3816 tabpage_T *tp;
3818 if (first_tabpage->tp_next == NULL)
3819 return;
3821 /* Remove the current tab page from the list of tab pages. */
3822 if (curtab == first_tabpage)
3823 first_tabpage = curtab->tp_next;
3824 else
3826 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3827 if (tp->tp_next == curtab)
3828 break;
3829 if (tp == NULL) /* "cannot happen" */
3830 return;
3831 tp->tp_next = curtab->tp_next;
3834 /* Re-insert it at the specified position. */
3835 if (n == 0)
3837 curtab->tp_next = first_tabpage;
3838 first_tabpage = curtab;
3840 else
3842 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3843 --n;
3844 curtab->tp_next = tp->tp_next;
3845 tp->tp_next = curtab;
3848 /* Need to redraw the tabline. Tab page contents doesn't change. */
3849 redraw_tabline = TRUE;
3854 * Go to another window.
3855 * When jumping to another buffer, stop Visual mode. Do this before
3856 * changing windows so we can yank the selection into the '*' register.
3857 * When jumping to another window on the same buffer, adjust its cursor
3858 * position to keep the same Visual area.
3860 void
3861 win_goto(wp)
3862 win_T *wp;
3864 if (text_locked())
3866 beep_flush();
3867 text_locked_msg();
3868 return;
3870 #ifdef FEAT_AUTOCMD
3871 if (curbuf_locked())
3872 return;
3873 #endif
3875 #ifdef FEAT_VISUAL
3876 if (wp->w_buffer != curbuf)
3877 reset_VIsual_and_resel();
3878 else if (VIsual_active)
3879 wp->w_cursor = curwin->w_cursor;
3880 #endif
3882 #ifdef FEAT_GUI
3883 need_mouse_correct = TRUE;
3884 #endif
3885 win_enter(wp, TRUE);
3888 #if defined(FEAT_PERL) || defined(PROTO)
3890 * Find window number "winnr" (counting top to bottom).
3892 win_T *
3893 win_find_nr(winnr)
3894 int winnr;
3896 win_T *wp;
3898 # ifdef FEAT_WINDOWS
3899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3900 if (--winnr == 0)
3901 break;
3902 return wp;
3903 # else
3904 return curwin;
3905 # endif
3907 #endif
3909 #ifdef FEAT_VERTSPLIT
3911 * Move to window above or below "count" times.
3913 static void
3914 win_goto_ver(up, count)
3915 int up; /* TRUE to go to win above */
3916 long count;
3918 frame_T *fr;
3919 frame_T *nfr;
3920 frame_T *foundfr;
3922 foundfr = curwin->w_frame;
3923 while (count--)
3926 * First go upwards in the tree of frames until we find a upwards or
3927 * downwards neighbor.
3929 fr = foundfr;
3930 for (;;)
3932 if (fr == topframe)
3933 goto end;
3934 if (up)
3935 nfr = fr->fr_prev;
3936 else
3937 nfr = fr->fr_next;
3938 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3939 break;
3940 fr = fr->fr_parent;
3944 * Now go downwards to find the bottom or top frame in it.
3946 for (;;)
3948 if (nfr->fr_layout == FR_LEAF)
3950 foundfr = nfr;
3951 break;
3953 fr = nfr->fr_child;
3954 if (nfr->fr_layout == FR_ROW)
3956 /* Find the frame at the cursor row. */
3957 while (fr->fr_next != NULL
3958 && frame2win(fr)->w_wincol + fr->fr_width
3959 <= curwin->w_wincol + curwin->w_wcol)
3960 fr = fr->fr_next;
3962 if (nfr->fr_layout == FR_COL && up)
3963 while (fr->fr_next != NULL)
3964 fr = fr->fr_next;
3965 nfr = fr;
3968 end:
3969 if (foundfr != NULL)
3970 win_goto(foundfr->fr_win);
3974 * Move to left or right window.
3976 static void
3977 win_goto_hor(left, count)
3978 int left; /* TRUE to go to left win */
3979 long count;
3981 frame_T *fr;
3982 frame_T *nfr;
3983 frame_T *foundfr;
3985 foundfr = curwin->w_frame;
3986 while (count--)
3989 * First go upwards in the tree of frames until we find a left or
3990 * right neighbor.
3992 fr = foundfr;
3993 for (;;)
3995 if (fr == topframe)
3996 goto end;
3997 if (left)
3998 nfr = fr->fr_prev;
3999 else
4000 nfr = fr->fr_next;
4001 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4002 break;
4003 fr = fr->fr_parent;
4007 * Now go downwards to find the leftmost or rightmost frame in it.
4009 for (;;)
4011 if (nfr->fr_layout == FR_LEAF)
4013 foundfr = nfr;
4014 break;
4016 fr = nfr->fr_child;
4017 if (nfr->fr_layout == FR_COL)
4019 /* Find the frame at the cursor row. */
4020 while (fr->fr_next != NULL
4021 && frame2win(fr)->w_winrow + fr->fr_height
4022 <= curwin->w_winrow + curwin->w_wrow)
4023 fr = fr->fr_next;
4025 if (nfr->fr_layout == FR_ROW && left)
4026 while (fr->fr_next != NULL)
4027 fr = fr->fr_next;
4028 nfr = fr;
4031 end:
4032 if (foundfr != NULL)
4033 win_goto(foundfr->fr_win);
4035 #endif
4038 * Make window "wp" the current window.
4040 void
4041 win_enter(wp, undo_sync)
4042 win_T *wp;
4043 int undo_sync;
4045 win_enter_ext(wp, undo_sync, FALSE);
4049 * Make window wp the current window.
4050 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4051 * been closed and isn't valid.
4053 static void
4054 win_enter_ext(wp, undo_sync, curwin_invalid)
4055 win_T *wp;
4056 int undo_sync;
4057 int curwin_invalid;
4059 #ifdef FEAT_AUTOCMD
4060 int other_buffer = FALSE;
4061 #endif
4063 if (wp == curwin && !curwin_invalid) /* nothing to do */
4064 return;
4066 #ifdef FEAT_AUTOCMD
4067 if (!curwin_invalid)
4070 * Be careful: If autocommands delete the window, return now.
4072 if (wp->w_buffer != curbuf)
4074 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4075 other_buffer = TRUE;
4076 if (!win_valid(wp))
4077 return;
4079 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4080 if (!win_valid(wp))
4081 return;
4082 # ifdef FEAT_EVAL
4083 /* autocmds may abort script processing */
4084 if (aborting())
4085 return;
4086 # endif
4088 #endif
4090 /* sync undo before leaving the current buffer */
4091 if (undo_sync && curbuf != wp->w_buffer)
4092 u_sync(FALSE);
4093 /* may have to copy the buffer options when 'cpo' contains 'S' */
4094 if (wp->w_buffer != curbuf)
4095 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4096 if (!curwin_invalid)
4098 prevwin = curwin; /* remember for CTRL-W p */
4099 curwin->w_redr_status = TRUE;
4101 curwin = wp;
4102 curbuf = wp->w_buffer;
4103 check_cursor();
4104 #ifdef FEAT_VIRTUALEDIT
4105 if (!virtual_active())
4106 curwin->w_cursor.coladd = 0;
4107 #endif
4108 changed_line_abv_curs(); /* assume cursor position needs updating */
4110 if (curwin->w_localdir != NULL)
4112 /* Window has a local directory: Save current directory as global
4113 * directory (unless that was done already) and change to the local
4114 * directory. */
4115 if (globaldir == NULL)
4117 char_u cwd[MAXPATHL];
4119 if (mch_dirname(cwd, MAXPATHL) == OK)
4120 globaldir = vim_strsave(cwd);
4122 if (mch_chdir((char *)curwin->w_localdir) == 0)
4123 shorten_fnames(TRUE);
4125 else if (globaldir != NULL)
4127 /* Window doesn't have a local directory and we are not in the global
4128 * directory: Change to the global directory. */
4129 ignored = mch_chdir((char *)globaldir);
4130 vim_free(globaldir);
4131 globaldir = NULL;
4132 shorten_fnames(TRUE);
4135 #ifdef FEAT_AUTOCMD
4136 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4137 if (other_buffer)
4138 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4139 #endif
4141 #ifdef FEAT_TITLE
4142 maketitle();
4143 #endif
4144 curwin->w_redr_status = TRUE;
4145 redraw_tabline = TRUE;
4146 if (restart_edit)
4147 redraw_later(VALID); /* causes status line redraw */
4149 /* set window height to desired minimal value */
4150 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4151 win_setheight((int)p_wh);
4152 else if (curwin->w_height == 0)
4153 win_setheight(1);
4155 #ifdef FEAT_VERTSPLIT
4156 /* set window width to desired minimal value */
4157 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4158 win_setwidth((int)p_wiw);
4159 #endif
4161 #ifdef FEAT_MOUSE
4162 setmouse(); /* in case jumped to/from help buffer */
4163 #endif
4165 /* Change directories when the 'acd' option is set. */
4166 DO_AUTOCHDIR
4169 #endif /* FEAT_WINDOWS */
4171 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4173 * Jump to the first open window that contains buffer "buf", if one exists.
4174 * Returns a pointer to the window found, otherwise NULL.
4176 win_T *
4177 buf_jump_open_win(buf)
4178 buf_T *buf;
4180 # ifdef FEAT_WINDOWS
4181 win_T *wp;
4183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4184 if (wp->w_buffer == buf)
4185 break;
4186 if (wp != NULL)
4187 win_enter(wp, FALSE);
4188 return wp;
4189 # else
4190 if (curwin->w_buffer == buf)
4191 return curwin;
4192 return NULL;
4193 # endif
4197 * Jump to the first open window in any tab page that contains buffer "buf",
4198 * if one exists.
4199 * Returns a pointer to the window found, otherwise NULL.
4201 win_T *
4202 buf_jump_open_tab(buf)
4203 buf_T *buf;
4205 # ifdef FEAT_WINDOWS
4206 win_T *wp;
4207 tabpage_T *tp;
4209 /* First try the current tab page. */
4210 wp = buf_jump_open_win(buf);
4211 if (wp != NULL)
4212 return wp;
4214 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4215 if (tp != curtab)
4217 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4218 if (wp->w_buffer == buf)
4219 break;
4220 if (wp != NULL)
4222 goto_tabpage_win(tp, wp);
4223 if (curwin != wp)
4224 wp = NULL; /* something went wrong */
4225 break;
4229 return wp;
4230 # else
4231 if (curwin->w_buffer == buf)
4232 return curwin;
4233 return NULL;
4234 # endif
4236 #endif
4239 * Allocate a window structure and link it in the window list when "hidden" is
4240 * FALSE.
4242 static win_T *
4243 win_alloc(after, hidden)
4244 win_T *after UNUSED;
4245 int hidden UNUSED;
4247 win_T *newwin;
4250 * allocate window structure and linesizes arrays
4252 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4253 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4255 vim_free(newwin);
4256 newwin = NULL;
4259 if (newwin != NULL)
4261 #ifdef FEAT_AUTOCMD
4262 /* Don't execute autocommands while the window is not properly
4263 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4264 * event. */
4265 block_autocmds();
4266 #endif
4268 * link the window in the window list
4270 #ifdef FEAT_WINDOWS
4271 if (!hidden)
4272 win_append(after, newwin);
4273 #endif
4274 #ifdef FEAT_VERTSPLIT
4275 newwin->w_wincol = 0;
4276 newwin->w_width = Columns;
4277 #endif
4279 /* position the display and the cursor at the top of the file. */
4280 newwin->w_topline = 1;
4281 #ifdef FEAT_DIFF
4282 newwin->w_topfill = 0;
4283 #endif
4284 newwin->w_botline = 2;
4285 newwin->w_cursor.lnum = 1;
4286 #ifdef FEAT_SCROLLBIND
4287 newwin->w_scbind_pos = 1;
4288 #endif
4290 /* We won't calculate w_fraction until resizing the window */
4291 newwin->w_fraction = 0;
4292 newwin->w_prev_fraction_row = -1;
4294 #ifdef FEAT_GUI
4295 if (gui.in_use)
4297 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4298 SBAR_LEFT, newwin);
4299 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4300 SBAR_RIGHT, newwin);
4302 #endif
4303 #ifdef FEAT_EVAL
4304 /* init w: variables */
4305 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4306 #endif
4307 #ifdef FEAT_FOLDING
4308 foldInitWin(newwin);
4309 #endif
4310 #ifdef FEAT_AUTOCMD
4311 unblock_autocmds();
4312 #endif
4313 #ifdef FEAT_SEARCH_EXTRA
4314 newwin->w_match_head = NULL;
4315 newwin->w_next_match_id = 4;
4316 #endif
4318 return newwin;
4321 #if defined(FEAT_WINDOWS) || defined(PROTO)
4324 * remove window 'wp' from the window list and free the structure
4326 static void
4327 win_free(wp, tp)
4328 win_T *wp;
4329 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4331 int i;
4333 #ifdef FEAT_FOLDING
4334 clearFolding(wp);
4335 #endif
4337 /* reduce the reference count to the argument list. */
4338 alist_unlink(wp->w_alist);
4340 #ifdef FEAT_AUTOCMD
4341 /* Don't execute autocommands while the window is halfway being deleted.
4342 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4343 block_autocmds();
4344 #endif
4346 #ifdef FEAT_MZSCHEME
4347 mzscheme_window_free(wp);
4348 #endif
4350 #ifdef FEAT_PERL
4351 perl_win_free(wp);
4352 #endif
4354 #ifdef FEAT_PYTHON
4355 python_window_free(wp);
4356 #endif
4358 #ifdef FEAT_TCL
4359 tcl_window_free(wp);
4360 #endif
4362 #ifdef FEAT_RUBY
4363 ruby_window_free(wp);
4364 #endif
4366 clear_winopt(&wp->w_onebuf_opt);
4367 clear_winopt(&wp->w_allbuf_opt);
4369 #ifdef FEAT_EVAL
4370 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4371 #endif
4373 if (prevwin == wp)
4374 prevwin = NULL;
4375 win_free_lsize(wp);
4377 for (i = 0; i < wp->w_tagstacklen; ++i)
4378 vim_free(wp->w_tagstack[i].tagname);
4380 vim_free(wp->w_localdir);
4382 #ifdef FEAT_SEARCH_EXTRA
4383 clear_matches(wp);
4384 #endif
4386 #ifdef FEAT_JUMPLIST
4387 free_jumplist(wp);
4388 #endif
4390 #ifdef FEAT_QUICKFIX
4391 qf_free_all(wp);
4392 #endif
4394 #ifdef FEAT_GUI
4395 if (gui.in_use)
4397 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4398 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4400 #endif /* FEAT_GUI */
4402 #ifdef FEAT_AUTOCMD
4403 if (wp != aucmd_win)
4404 #endif
4405 win_remove(wp, tp);
4406 vim_free(wp);
4408 #ifdef FEAT_AUTOCMD
4409 unblock_autocmds();
4410 #endif
4414 * Append window "wp" in the window list after window "after".
4416 void
4417 win_append(after, wp)
4418 win_T *after, *wp;
4420 win_T *before;
4422 if (after == NULL) /* after NULL is in front of the first */
4423 before = firstwin;
4424 else
4425 before = after->w_next;
4427 wp->w_next = before;
4428 wp->w_prev = after;
4429 if (after == NULL)
4430 firstwin = wp;
4431 else
4432 after->w_next = wp;
4433 if (before == NULL)
4434 lastwin = wp;
4435 else
4436 before->w_prev = wp;
4440 * Remove a window from the window list.
4442 void
4443 win_remove(wp, tp)
4444 win_T *wp;
4445 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4447 if (wp->w_prev != NULL)
4448 wp->w_prev->w_next = wp->w_next;
4449 else if (tp == NULL)
4450 firstwin = wp->w_next;
4451 else
4452 tp->tp_firstwin = wp->w_next;
4453 if (wp->w_next != NULL)
4454 wp->w_next->w_prev = wp->w_prev;
4455 else if (tp == NULL)
4456 lastwin = wp->w_prev;
4457 else
4458 tp->tp_lastwin = wp->w_prev;
4462 * Append frame "frp" in a frame list after frame "after".
4464 static void
4465 frame_append(after, frp)
4466 frame_T *after, *frp;
4468 frp->fr_next = after->fr_next;
4469 after->fr_next = frp;
4470 if (frp->fr_next != NULL)
4471 frp->fr_next->fr_prev = frp;
4472 frp->fr_prev = after;
4476 * Insert frame "frp" in a frame list before frame "before".
4478 static void
4479 frame_insert(before, frp)
4480 frame_T *before, *frp;
4482 frp->fr_next = before;
4483 frp->fr_prev = before->fr_prev;
4484 before->fr_prev = frp;
4485 if (frp->fr_prev != NULL)
4486 frp->fr_prev->fr_next = frp;
4487 else
4488 frp->fr_parent->fr_child = frp;
4492 * Remove a frame from a frame list.
4494 static void
4495 frame_remove(frp)
4496 frame_T *frp;
4498 if (frp->fr_prev != NULL)
4499 frp->fr_prev->fr_next = frp->fr_next;
4500 else
4501 frp->fr_parent->fr_child = frp->fr_next;
4502 if (frp->fr_next != NULL)
4503 frp->fr_next->fr_prev = frp->fr_prev;
4506 #endif /* FEAT_WINDOWS */
4509 * Allocate w_lines[] for window "wp".
4510 * Return FAIL for failure, OK for success.
4513 win_alloc_lines(wp)
4514 win_T *wp;
4516 wp->w_lines_valid = 0;
4517 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4518 if (wp->w_lines == NULL)
4519 return FAIL;
4520 return OK;
4524 * free lsize arrays for a window
4526 void
4527 win_free_lsize(wp)
4528 win_T *wp;
4530 vim_free(wp->w_lines);
4531 wp->w_lines = NULL;
4535 * Called from win_new_shellsize() after Rows changed.
4536 * This only does the current tab page, others must be done when made active.
4538 void
4539 shell_new_rows()
4541 int h = (int)ROWS_AVAIL;
4543 if (firstwin == NULL) /* not initialized yet */
4544 return;
4545 #ifdef FEAT_WINDOWS
4546 if (h < frame_minheight(topframe, NULL))
4547 h = frame_minheight(topframe, NULL);
4549 /* First try setting the heights of windows with 'winfixheight'. If
4550 * that doesn't result in the right height, forget about that option. */
4551 frame_new_height(topframe, h, FALSE, TRUE);
4552 if (topframe->fr_height != h)
4553 frame_new_height(topframe, h, FALSE, FALSE);
4555 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4556 #else
4557 if (h < 1)
4558 h = 1;
4559 win_new_height(firstwin, h);
4560 #endif
4561 compute_cmdrow();
4562 #ifdef FEAT_WINDOWS
4563 curtab->tp_ch_used = p_ch;
4564 #endif
4566 #if 0
4567 /* Disabled: don't want making the screen smaller make a window larger. */
4568 if (p_ea)
4569 win_equal(curwin, FALSE, 'v');
4570 #endif
4573 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4575 * Called from win_new_shellsize() after Columns changed.
4577 void
4578 shell_new_columns()
4580 if (firstwin == NULL) /* not initialized yet */
4581 return;
4583 /* First try setting the widths of windows with 'winfixwidth'. If that
4584 * doesn't result in the right width, forget about that option. */
4585 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4586 if (topframe->fr_width != Columns)
4587 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4589 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4590 #if 0
4591 /* Disabled: don't want making the screen smaller make a window larger. */
4592 if (p_ea)
4593 win_equal(curwin, FALSE, 'h');
4594 #endif
4596 #endif
4598 #if defined(FEAT_CMDWIN) || defined(PROTO)
4600 * Save the size of all windows in "gap".
4602 void
4603 win_size_save(gap)
4604 garray_T *gap;
4607 win_T *wp;
4609 ga_init2(gap, (int)sizeof(int), 1);
4610 if (ga_grow(gap, win_count() * 2) == OK)
4611 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4613 ((int *)gap->ga_data)[gap->ga_len++] =
4614 wp->w_width + wp->w_vsep_width;
4615 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4620 * Restore window sizes, but only if the number of windows is still the same.
4621 * Does not free the growarray.
4623 void
4624 win_size_restore(gap)
4625 garray_T *gap;
4627 win_T *wp;
4628 int i;
4630 if (win_count() * 2 == gap->ga_len)
4632 i = 0;
4633 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4635 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4636 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4638 /* recompute the window positions */
4639 (void)win_comp_pos();
4642 #endif /* FEAT_CMDWIN */
4644 #if defined(FEAT_WINDOWS) || defined(PROTO)
4646 * Update the position for all windows, using the width and height of the
4647 * frames.
4648 * Returns the row just after the last window.
4651 win_comp_pos()
4653 int row = tabline_height();
4654 int col = 0;
4656 frame_comp_pos(topframe, &row, &col);
4657 return row;
4661 * Update the position of the windows in frame "topfrp", using the width and
4662 * height of the frames.
4663 * "*row" and "*col" are the top-left position of the frame. They are updated
4664 * to the bottom-right position plus one.
4666 static void
4667 frame_comp_pos(topfrp, row, col)
4668 frame_T *topfrp;
4669 int *row;
4670 int *col;
4672 win_T *wp;
4673 frame_T *frp;
4674 #ifdef FEAT_VERTSPLIT
4675 int startcol;
4676 int startrow;
4677 #endif
4679 wp = topfrp->fr_win;
4680 if (wp != NULL)
4682 if (wp->w_winrow != *row
4683 #ifdef FEAT_VERTSPLIT
4684 || wp->w_wincol != *col
4685 #endif
4688 /* position changed, redraw */
4689 wp->w_winrow = *row;
4690 #ifdef FEAT_VERTSPLIT
4691 wp->w_wincol = *col;
4692 #endif
4693 redraw_win_later(wp, NOT_VALID);
4694 wp->w_redr_status = TRUE;
4696 *row += wp->w_height + wp->w_status_height;
4697 #ifdef FEAT_VERTSPLIT
4698 *col += wp->w_width + wp->w_vsep_width;
4699 #endif
4701 else
4703 #ifdef FEAT_VERTSPLIT
4704 startrow = *row;
4705 startcol = *col;
4706 #endif
4707 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4709 #ifdef FEAT_VERTSPLIT
4710 if (topfrp->fr_layout == FR_ROW)
4711 *row = startrow; /* all frames are at the same row */
4712 else
4713 *col = startcol; /* all frames are at the same col */
4714 #endif
4715 frame_comp_pos(frp, row, col);
4720 #endif /* FEAT_WINDOWS */
4723 * Set current window height and take care of repositioning other windows to
4724 * fit around it.
4726 void
4727 win_setheight(height)
4728 int height;
4730 win_setheight_win(height, curwin);
4734 * Set the window height of window "win" and take care of repositioning other
4735 * windows to fit around it.
4737 void
4738 win_setheight_win(height, win)
4739 int height;
4740 win_T *win;
4742 int row;
4744 if (win == curwin)
4746 /* Always keep current window at least one line high, even when
4747 * 'winminheight' is zero. */
4748 #ifdef FEAT_WINDOWS
4749 if (height < p_wmh)
4750 height = p_wmh;
4751 #endif
4752 if (height == 0)
4753 height = 1;
4756 #ifdef FEAT_WINDOWS
4757 frame_setheight(win->w_frame, height + win->w_status_height);
4759 /* recompute the window positions */
4760 row = win_comp_pos();
4761 #else
4762 if (height > topframe->fr_height)
4763 height = topframe->fr_height;
4764 win->w_height = height;
4765 row = height;
4766 #endif
4769 * If there is extra space created between the last window and the command
4770 * line, clear it.
4772 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4773 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4774 cmdline_row = row;
4775 msg_row = row;
4776 msg_col = 0;
4778 redraw_all_later(NOT_VALID);
4781 #if defined(FEAT_WINDOWS) || defined(PROTO)
4784 * Set the height of a frame to "height" and take care that all frames and
4785 * windows inside it are resized. Also resize frames on the left and right if
4786 * the are in the same FR_ROW frame.
4788 * Strategy:
4789 * If the frame is part of a FR_COL frame, try fitting the frame in that
4790 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4791 * go to containing frames to resize them and make room.
4792 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4793 * Check for the minimal height of the FR_ROW frame.
4794 * At the top level we can also use change the command line height.
4796 static void
4797 frame_setheight(curfrp, height)
4798 frame_T *curfrp;
4799 int height;
4801 int room; /* total number of lines available */
4802 int take; /* number of lines taken from other windows */
4803 int room_cmdline; /* lines available from cmdline */
4804 int run;
4805 frame_T *frp;
4806 int h;
4807 int room_reserved;
4809 /* If the height already is the desired value, nothing to do. */
4810 if (curfrp->fr_height == height)
4811 return;
4813 if (curfrp->fr_parent == NULL)
4815 /* topframe: can only change the command line */
4816 if (height > ROWS_AVAIL)
4817 height = ROWS_AVAIL;
4818 if (height > 0)
4819 frame_new_height(curfrp, height, FALSE, FALSE);
4821 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4823 /* Row of frames: Also need to resize frames left and right of this
4824 * one. First check for the minimal height of these. */
4825 h = frame_minheight(curfrp->fr_parent, NULL);
4826 if (height < h)
4827 height = h;
4828 frame_setheight(curfrp->fr_parent, height);
4830 else
4833 * Column of frames: try to change only frames in this column.
4835 #ifdef FEAT_VERTSPLIT
4837 * Do this twice:
4838 * 1: compute room available, if it's not enough try resizing the
4839 * containing frame.
4840 * 2: compute the room available and adjust the height to it.
4841 * Try not to reduce the height of a window with 'winfixheight' set.
4843 for (run = 1; run <= 2; ++run)
4844 #else
4845 for (;;)
4846 #endif
4848 room = 0;
4849 room_reserved = 0;
4850 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4851 frp = frp->fr_next)
4853 if (frp != curfrp
4854 && frp->fr_win != NULL
4855 && frp->fr_win->w_p_wfh)
4856 room_reserved += frp->fr_height;
4857 room += frp->fr_height;
4858 if (frp != curfrp)
4859 room -= frame_minheight(frp, NULL);
4861 #ifdef FEAT_VERTSPLIT
4862 if (curfrp->fr_width != Columns)
4863 room_cmdline = 0;
4864 else
4865 #endif
4867 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4868 + lastwin->w_height + lastwin->w_status_height);
4869 if (room_cmdline < 0)
4870 room_cmdline = 0;
4873 if (height <= room + room_cmdline)
4874 break;
4875 #ifdef FEAT_VERTSPLIT
4876 if (run == 2 || curfrp->fr_width == Columns)
4877 #endif
4879 if (height > room + room_cmdline)
4880 height = room + room_cmdline;
4881 break;
4883 #ifdef FEAT_VERTSPLIT
4884 frame_setheight(curfrp->fr_parent, height
4885 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4886 #endif
4887 /*NOTREACHED*/
4891 * Compute the number of lines we will take from others frames (can be
4892 * negative!).
4894 take = height - curfrp->fr_height;
4896 /* If there is not enough room, also reduce the height of a window
4897 * with 'winfixheight' set. */
4898 if (height > room + room_cmdline - room_reserved)
4899 room_reserved = room + room_cmdline - height;
4900 /* If there is only a 'winfixheight' window and making the
4901 * window smaller, need to make the other window taller. */
4902 if (take < 0 && room - curfrp->fr_height < room_reserved)
4903 room_reserved = 0;
4905 if (take > 0 && room_cmdline > 0)
4907 /* use lines from cmdline first */
4908 if (take < room_cmdline)
4909 room_cmdline = take;
4910 take -= room_cmdline;
4911 topframe->fr_height += room_cmdline;
4915 * set the current frame to the new height
4917 frame_new_height(curfrp, height, FALSE, FALSE);
4920 * First take lines from the frames after the current frame. If
4921 * that is not enough, takes lines from frames above the current
4922 * frame.
4924 for (run = 0; run < 2; ++run)
4926 if (run == 0)
4927 frp = curfrp->fr_next; /* 1st run: start with next window */
4928 else
4929 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4930 while (frp != NULL && take != 0)
4932 h = frame_minheight(frp, NULL);
4933 if (room_reserved > 0
4934 && frp->fr_win != NULL
4935 && frp->fr_win->w_p_wfh)
4937 if (room_reserved >= frp->fr_height)
4938 room_reserved -= frp->fr_height;
4939 else
4941 if (frp->fr_height - room_reserved > take)
4942 room_reserved = frp->fr_height - take;
4943 take -= frp->fr_height - room_reserved;
4944 frame_new_height(frp, room_reserved, FALSE, FALSE);
4945 room_reserved = 0;
4948 else
4950 if (frp->fr_height - take < h)
4952 take -= frp->fr_height - h;
4953 frame_new_height(frp, h, FALSE, FALSE);
4955 else
4957 frame_new_height(frp, frp->fr_height - take,
4958 FALSE, FALSE);
4959 take = 0;
4962 if (run == 0)
4963 frp = frp->fr_next;
4964 else
4965 frp = frp->fr_prev;
4971 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4973 * Set current window width and take care of repositioning other windows to
4974 * fit around it.
4976 void
4977 win_setwidth(width)
4978 int width;
4980 win_setwidth_win(width, curwin);
4983 void
4984 win_setwidth_win(width, wp)
4985 int width;
4986 win_T *wp;
4988 /* Always keep current window at least one column wide, even when
4989 * 'winminwidth' is zero. */
4990 if (wp == curwin)
4992 if (width < p_wmw)
4993 width = p_wmw;
4994 if (width == 0)
4995 width = 1;
4998 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5000 /* recompute the window positions */
5001 (void)win_comp_pos();
5003 redraw_all_later(NOT_VALID);
5007 * Set the width of a frame to "width" and take care that all frames and
5008 * windows inside it are resized. Also resize frames above and below if the
5009 * are in the same FR_ROW frame.
5011 * Strategy is similar to frame_setheight().
5013 static void
5014 frame_setwidth(curfrp, width)
5015 frame_T *curfrp;
5016 int width;
5018 int room; /* total number of lines available */
5019 int take; /* number of lines taken from other windows */
5020 int run;
5021 frame_T *frp;
5022 int w;
5023 int room_reserved;
5025 /* If the width already is the desired value, nothing to do. */
5026 if (curfrp->fr_width == width)
5027 return;
5029 if (curfrp->fr_parent == NULL)
5030 /* topframe: can't change width */
5031 return;
5033 if (curfrp->fr_parent->fr_layout == FR_COL)
5035 /* Column of frames: Also need to resize frames above and below of
5036 * this one. First check for the minimal width of these. */
5037 w = frame_minwidth(curfrp->fr_parent, NULL);
5038 if (width < w)
5039 width = w;
5040 frame_setwidth(curfrp->fr_parent, width);
5042 else
5045 * Row of frames: try to change only frames in this row.
5047 * Do this twice:
5048 * 1: compute room available, if it's not enough try resizing the
5049 * containing frame.
5050 * 2: compute the room available and adjust the width to it.
5052 for (run = 1; run <= 2; ++run)
5054 room = 0;
5055 room_reserved = 0;
5056 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5057 frp = frp->fr_next)
5059 if (frp != curfrp
5060 && frp->fr_win != NULL
5061 && frp->fr_win->w_p_wfw)
5062 room_reserved += frp->fr_width;
5063 room += frp->fr_width;
5064 if (frp != curfrp)
5065 room -= frame_minwidth(frp, NULL);
5068 if (width <= room)
5069 break;
5070 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5072 if (width > room)
5073 width = room;
5074 break;
5076 frame_setwidth(curfrp->fr_parent, width
5077 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5081 * Compute the number of lines we will take from others frames (can be
5082 * negative!).
5084 take = width - curfrp->fr_width;
5086 /* If there is not enough room, also reduce the width of a window
5087 * with 'winfixwidth' set. */
5088 if (width > room - room_reserved)
5089 room_reserved = room - width;
5090 /* If there is only a 'winfixwidth' window and making the
5091 * window smaller, need to make the other window narrower. */
5092 if (take < 0 && room - curfrp->fr_width < room_reserved)
5093 room_reserved = 0;
5096 * set the current frame to the new width
5098 frame_new_width(curfrp, width, FALSE, FALSE);
5101 * First take lines from the frames right of the current frame. If
5102 * that is not enough, takes lines from frames left of the current
5103 * frame.
5105 for (run = 0; run < 2; ++run)
5107 if (run == 0)
5108 frp = curfrp->fr_next; /* 1st run: start with next window */
5109 else
5110 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5111 while (frp != NULL && take != 0)
5113 w = frame_minwidth(frp, NULL);
5114 if (room_reserved > 0
5115 && frp->fr_win != NULL
5116 && frp->fr_win->w_p_wfw)
5118 if (room_reserved >= frp->fr_width)
5119 room_reserved -= frp->fr_width;
5120 else
5122 if (frp->fr_width - room_reserved > take)
5123 room_reserved = frp->fr_width - take;
5124 take -= frp->fr_width - room_reserved;
5125 frame_new_width(frp, room_reserved, FALSE, FALSE);
5126 room_reserved = 0;
5129 else
5131 if (frp->fr_width - take < w)
5133 take -= frp->fr_width - w;
5134 frame_new_width(frp, w, FALSE, FALSE);
5136 else
5138 frame_new_width(frp, frp->fr_width - take,
5139 FALSE, FALSE);
5140 take = 0;
5143 if (run == 0)
5144 frp = frp->fr_next;
5145 else
5146 frp = frp->fr_prev;
5151 #endif /* FEAT_VERTSPLIT */
5154 * Check 'winminheight' for a valid value.
5156 void
5157 win_setminheight()
5159 int room;
5160 int first = TRUE;
5161 win_T *wp;
5163 /* loop until there is a 'winminheight' that is possible */
5164 while (p_wmh > 0)
5166 /* TODO: handle vertical splits */
5167 room = -p_wh;
5168 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5169 room += wp->w_height - p_wmh;
5170 if (room >= 0)
5171 break;
5172 --p_wmh;
5173 if (first)
5175 EMSG(_(e_noroom));
5176 first = FALSE;
5181 #ifdef FEAT_MOUSE
5184 * Status line of dragwin is dragged "offset" lines down (negative is up).
5186 void
5187 win_drag_status_line(dragwin, offset)
5188 win_T *dragwin;
5189 int offset;
5191 frame_T *curfr;
5192 frame_T *fr;
5193 int room;
5194 int row;
5195 int up; /* if TRUE, drag status line up, otherwise down */
5196 int n;
5198 fr = dragwin->w_frame;
5199 curfr = fr;
5200 if (fr != topframe) /* more than one window */
5202 fr = fr->fr_parent;
5203 /* When the parent frame is not a column of frames, its parent should
5204 * be. */
5205 if (fr->fr_layout != FR_COL)
5207 curfr = fr;
5208 if (fr != topframe) /* only a row of windows, may drag statusline */
5209 fr = fr->fr_parent;
5213 /* If this is the last frame in a column, may want to resize the parent
5214 * frame instead (go two up to skip a row of frames). */
5215 while (curfr != topframe && curfr->fr_next == NULL)
5217 if (fr != topframe)
5218 fr = fr->fr_parent;
5219 curfr = fr;
5220 if (fr != topframe)
5221 fr = fr->fr_parent;
5224 if (offset < 0) /* drag up */
5226 up = TRUE;
5227 offset = -offset;
5228 /* sum up the room of the current frame and above it */
5229 if (fr == curfr)
5231 /* only one window */
5232 room = fr->fr_height - frame_minheight(fr, NULL);
5234 else
5236 room = 0;
5237 for (fr = fr->fr_child; ; fr = fr->fr_next)
5239 room += fr->fr_height - frame_minheight(fr, NULL);
5240 if (fr == curfr)
5241 break;
5244 fr = curfr->fr_next; /* put fr at frame that grows */
5246 else /* drag down */
5248 up = FALSE;
5250 * Only dragging the last status line can reduce p_ch.
5252 room = Rows - cmdline_row;
5253 if (curfr->fr_next == NULL)
5254 room -= 1;
5255 else
5256 room -= p_ch;
5257 if (room < 0)
5258 room = 0;
5259 /* sum up the room of frames below of the current one */
5260 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5261 room += fr->fr_height - frame_minheight(fr, NULL);
5262 fr = curfr; /* put fr at window that grows */
5265 if (room < offset) /* Not enough room */
5266 offset = room; /* Move as far as we can */
5267 if (offset <= 0)
5268 return;
5271 * Grow frame fr by "offset" lines.
5272 * Doesn't happen when dragging the last status line up.
5274 if (fr != NULL)
5275 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5277 if (up)
5278 fr = curfr; /* current frame gets smaller */
5279 else
5280 fr = curfr->fr_next; /* next frame gets smaller */
5283 * Now make the other frames smaller.
5285 while (fr != NULL && offset > 0)
5287 n = frame_minheight(fr, NULL);
5288 if (fr->fr_height - offset <= n)
5290 offset -= fr->fr_height - n;
5291 frame_new_height(fr, n, !up, FALSE);
5293 else
5295 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5296 break;
5298 if (up)
5299 fr = fr->fr_prev;
5300 else
5301 fr = fr->fr_next;
5303 row = win_comp_pos();
5304 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5305 cmdline_row = row;
5306 p_ch = Rows - cmdline_row;
5307 if (p_ch < 1)
5308 p_ch = 1;
5309 curtab->tp_ch_used = p_ch;
5310 redraw_all_later(SOME_VALID);
5311 showmode();
5314 #ifdef FEAT_VERTSPLIT
5316 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5318 void
5319 win_drag_vsep_line(dragwin, offset)
5320 win_T *dragwin;
5321 int offset;
5323 frame_T *curfr;
5324 frame_T *fr;
5325 int room;
5326 int left; /* if TRUE, drag separator line left, otherwise right */
5327 int n;
5329 fr = dragwin->w_frame;
5330 if (fr == topframe) /* only one window (cannot happen?) */
5331 return;
5332 curfr = fr;
5333 fr = fr->fr_parent;
5334 /* When the parent frame is not a row of frames, its parent should be. */
5335 if (fr->fr_layout != FR_ROW)
5337 if (fr == topframe) /* only a column of windows (cannot happen?) */
5338 return;
5339 curfr = fr;
5340 fr = fr->fr_parent;
5343 /* If this is the last frame in a row, may want to resize a parent
5344 * frame instead. */
5345 while (curfr->fr_next == NULL)
5347 if (fr == topframe)
5348 break;
5349 curfr = fr;
5350 fr = fr->fr_parent;
5351 if (fr != topframe)
5353 curfr = fr;
5354 fr = fr->fr_parent;
5358 if (offset < 0) /* drag left */
5360 left = TRUE;
5361 offset = -offset;
5362 /* sum up the room of the current frame and left of it */
5363 room = 0;
5364 for (fr = fr->fr_child; ; fr = fr->fr_next)
5366 room += fr->fr_width - frame_minwidth(fr, NULL);
5367 if (fr == curfr)
5368 break;
5370 fr = curfr->fr_next; /* put fr at frame that grows */
5372 else /* drag right */
5374 left = FALSE;
5375 /* sum up the room of frames right of the current one */
5376 room = 0;
5377 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5378 room += fr->fr_width - frame_minwidth(fr, NULL);
5379 fr = curfr; /* put fr at window that grows */
5382 if (room < offset) /* Not enough room */
5383 offset = room; /* Move as far as we can */
5384 if (offset <= 0) /* No room at all, quit. */
5385 return;
5387 /* grow frame fr by offset lines */
5388 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5390 /* shrink other frames: current and at the left or at the right */
5391 if (left)
5392 fr = curfr; /* current frame gets smaller */
5393 else
5394 fr = curfr->fr_next; /* next frame gets smaller */
5396 while (fr != NULL && offset > 0)
5398 n = frame_minwidth(fr, NULL);
5399 if (fr->fr_width - offset <= n)
5401 offset -= fr->fr_width - n;
5402 frame_new_width(fr, n, !left, FALSE);
5404 else
5406 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5407 break;
5409 if (left)
5410 fr = fr->fr_prev;
5411 else
5412 fr = fr->fr_next;
5414 (void)win_comp_pos();
5415 redraw_all_later(NOT_VALID);
5417 #endif /* FEAT_VERTSPLIT */
5418 #endif /* FEAT_MOUSE */
5420 #endif /* FEAT_WINDOWS */
5423 * Set the height of a window.
5424 * This takes care of the things inside the window, not what happens to the
5425 * window position, the frame or to other windows.
5427 static void
5428 win_new_height(wp, height)
5429 win_T *wp;
5430 int height;
5432 linenr_T lnum;
5433 int sline, line_size;
5434 #define FRACTION_MULT 16384L
5436 /* Don't want a negative height. Happens when splitting a tiny window.
5437 * Will equalize heights soon to fix it. */
5438 if (height < 0)
5439 height = 0;
5440 if (wp->w_height == height)
5441 return; /* nothing to do */
5443 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5444 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5445 + FRACTION_MULT / 2) / (long)wp->w_height;
5447 wp->w_height = height;
5448 wp->w_skipcol = 0;
5450 /* Don't change w_topline when height is zero. Don't set w_topline when
5451 * 'scrollbind' is set and this isn't the current window. */
5452 if (height > 0
5453 #ifdef FEAT_SCROLLBIND
5454 && (!wp->w_p_scb || wp == curwin)
5455 #endif
5459 * Find a value for w_topline that shows the cursor at the same
5460 * relative position in the window as before (more or less).
5462 lnum = wp->w_cursor.lnum;
5463 if (lnum < 1) /* can happen when starting up */
5464 lnum = 1;
5465 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5466 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5467 sline = wp->w_wrow - line_size;
5469 if (sline >= 0)
5471 /* Make sure the whole cursor line is visible, if possible. */
5472 int rows = plines_win(wp, lnum, FALSE);
5474 if (sline > wp->w_height - rows)
5476 sline = wp->w_height - rows;
5477 wp->w_wrow -= rows - line_size;
5481 if (sline < 0)
5484 * Cursor line would go off top of screen if w_wrow was this high.
5485 * Make cursor line the first line in the window. If not enough
5486 * room use w_skipcol;
5488 wp->w_wrow = line_size;
5489 if (wp->w_wrow >= wp->w_height
5490 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5492 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5493 --wp->w_wrow;
5494 while (wp->w_wrow >= wp->w_height)
5496 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5497 + win_col_off2(wp);
5498 --wp->w_wrow;
5502 else
5504 while (sline > 0 && lnum > 1)
5506 #ifdef FEAT_FOLDING
5507 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5508 if (lnum == 1)
5510 /* first line in buffer is folded */
5511 line_size = 1;
5512 --sline;
5513 break;
5515 #endif
5516 --lnum;
5517 #ifdef FEAT_DIFF
5518 if (lnum == wp->w_topline)
5519 line_size = plines_win_nofill(wp, lnum, TRUE)
5520 + wp->w_topfill;
5521 else
5522 #endif
5523 line_size = plines_win(wp, lnum, TRUE);
5524 sline -= line_size;
5527 if (sline < 0)
5530 * Line we want at top would go off top of screen. Use next
5531 * line instead.
5533 #ifdef FEAT_FOLDING
5534 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5535 #endif
5536 lnum++;
5537 wp->w_wrow -= line_size + sline;
5539 else if (sline > 0)
5541 /* First line of file reached, use that as topline. */
5542 lnum = 1;
5543 wp->w_wrow -= sline;
5546 set_topline(wp, lnum);
5549 if (wp == curwin)
5551 if (p_so)
5552 update_topline();
5553 curs_columns(FALSE); /* validate w_wrow */
5555 wp->w_prev_fraction_row = wp->w_wrow;
5557 win_comp_scroll(wp);
5558 redraw_win_later(wp, SOME_VALID);
5559 #ifdef FEAT_WINDOWS
5560 wp->w_redr_status = TRUE;
5561 #endif
5562 invalidate_botline_win(wp);
5565 #ifdef FEAT_VERTSPLIT
5567 * Set the width of a window.
5569 static void
5570 win_new_width(wp, width)
5571 win_T *wp;
5572 int width;
5574 wp->w_width = width;
5575 wp->w_lines_valid = 0;
5576 changed_line_abv_curs_win(wp);
5577 invalidate_botline_win(wp);
5578 if (wp == curwin)
5580 update_topline();
5581 curs_columns(TRUE); /* validate w_wrow */
5583 #ifdef FEAT_GUI_MACVIM
5584 /* The view may have moved, so clear all or display may get corrupted. */
5585 redraw_win_later(wp, CLEAR);
5586 #else
5587 redraw_win_later(wp, NOT_VALID);
5588 #endif
5589 wp->w_redr_status = TRUE;
5591 #endif
5593 void
5594 win_comp_scroll(wp)
5595 win_T *wp;
5597 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5598 if (wp->w_p_scr == 0)
5599 wp->w_p_scr = 1;
5603 * command_height: called whenever p_ch has been changed
5605 void
5606 command_height()
5608 #ifdef FEAT_WINDOWS
5609 int h;
5610 frame_T *frp;
5611 int old_p_ch = curtab->tp_ch_used;
5613 /* Use the value of p_ch that we remembered. This is needed for when the
5614 * GUI starts up, we can't be sure in what order things happen. And when
5615 * p_ch was changed in another tab page. */
5616 curtab->tp_ch_used = p_ch;
5618 /* Find bottom frame with width of screen. */
5619 frp = lastwin->w_frame;
5620 # ifdef FEAT_VERTSPLIT
5621 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5622 frp = frp->fr_parent;
5623 # endif
5625 /* Avoid changing the height of a window with 'winfixheight' set. */
5626 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5627 && frp->fr_win->w_p_wfh)
5628 frp = frp->fr_prev;
5630 if (starting != NO_SCREEN)
5632 cmdline_row = Rows - p_ch;
5634 if (p_ch > old_p_ch) /* p_ch got bigger */
5636 while (p_ch > old_p_ch)
5638 if (frp == NULL)
5640 EMSG(_(e_noroom));
5641 p_ch = old_p_ch;
5642 curtab->tp_ch_used = p_ch;
5643 cmdline_row = Rows - p_ch;
5644 break;
5646 h = frp->fr_height - frame_minheight(frp, NULL);
5647 if (h > p_ch - old_p_ch)
5648 h = p_ch - old_p_ch;
5649 old_p_ch += h;
5650 frame_add_height(frp, -h);
5651 frp = frp->fr_prev;
5654 /* Recompute window positions. */
5655 (void)win_comp_pos();
5657 /* clear the lines added to cmdline */
5658 if (full_screen)
5659 screen_fill((int)(cmdline_row), (int)Rows, 0,
5660 (int)Columns, ' ', ' ', 0);
5661 msg_row = cmdline_row;
5662 redraw_cmdline = TRUE;
5663 return;
5666 if (msg_row < cmdline_row)
5667 msg_row = cmdline_row;
5668 redraw_cmdline = TRUE;
5670 frame_add_height(frp, (int)(old_p_ch - p_ch));
5672 /* Recompute window positions. */
5673 if (frp != lastwin->w_frame)
5674 (void)win_comp_pos();
5675 #else
5676 cmdline_row = Rows - p_ch;
5677 win_setheight(cmdline_row);
5678 #endif
5681 #if defined(FEAT_WINDOWS) || defined(PROTO)
5683 * Resize frame "frp" to be "n" lines higher (negative for less high).
5684 * Also resize the frames it is contained in.
5686 static void
5687 frame_add_height(frp, n)
5688 frame_T *frp;
5689 int n;
5691 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5692 for (;;)
5694 frp = frp->fr_parent;
5695 if (frp == NULL)
5696 break;
5697 frp->fr_height += n;
5702 * Add or remove a status line for the bottom window(s), according to the
5703 * value of 'laststatus'.
5705 void
5706 last_status(morewin)
5707 int morewin; /* pretend there are two or more windows */
5709 /* Don't make a difference between horizontal or vertical split. */
5710 last_status_rec(topframe, (p_ls == 2
5711 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5714 static void
5715 last_status_rec(fr, statusline)
5716 frame_T *fr;
5717 int statusline;
5719 frame_T *fp;
5720 win_T *wp;
5722 if (fr->fr_layout == FR_LEAF)
5724 wp = fr->fr_win;
5725 if (wp->w_status_height != 0 && !statusline)
5727 /* remove status line */
5728 win_new_height(wp, wp->w_height + 1);
5729 wp->w_status_height = 0;
5730 comp_col();
5732 else if (wp->w_status_height == 0 && statusline)
5734 /* Find a frame to take a line from. */
5735 fp = fr;
5736 while (fp->fr_height <= frame_minheight(fp, NULL))
5738 if (fp == topframe)
5740 EMSG(_(e_noroom));
5741 return;
5743 /* In a column of frames: go to frame above. If already at
5744 * the top or in a row of frames: go to parent. */
5745 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5746 fp = fp->fr_prev;
5747 else
5748 fp = fp->fr_parent;
5750 wp->w_status_height = 1;
5751 if (fp != fr)
5753 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5754 frame_fix_height(wp);
5755 (void)win_comp_pos();
5757 else
5758 win_new_height(wp, wp->w_height - 1);
5759 comp_col();
5760 redraw_all_later(SOME_VALID);
5763 #ifdef FEAT_VERTSPLIT
5764 else if (fr->fr_layout == FR_ROW)
5766 /* vertically split windows, set status line for each one */
5767 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5768 last_status_rec(fp, statusline);
5770 #endif
5771 else
5773 /* horizontally split window, set status line for last one */
5774 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5776 last_status_rec(fp, statusline);
5781 * Return the number of lines used by the tab page line.
5784 tabline_height()
5786 #ifdef FEAT_GUI_TABLINE
5787 /* When the GUI has the tabline then this always returns zero. */
5788 if (gui_use_tabline())
5789 return 0;
5790 #endif
5791 switch (p_stal)
5793 case 0: return 0;
5794 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5796 return 1;
5799 #endif /* FEAT_WINDOWS */
5801 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5803 * Get the file name at the cursor.
5804 * If Visual mode is active, use the selected text if it's in one line.
5805 * Returns the name in allocated memory, NULL for failure.
5807 char_u *
5808 grab_file_name(count, file_lnum)
5809 long count;
5810 linenr_T *file_lnum;
5812 # ifdef FEAT_VISUAL
5813 if (VIsual_active)
5815 int len;
5816 char_u *ptr;
5818 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5819 return NULL;
5820 return find_file_name_in_path(ptr, len,
5821 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5823 # endif
5824 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5825 file_lnum);
5830 * Return the file name under or after the cursor.
5832 * The 'path' option is searched if the file name is not absolute.
5833 * The string returned has been alloc'ed and should be freed by the caller.
5834 * NULL is returned if the file name or file is not found.
5836 * options:
5837 * FNAME_MESS give error messages
5838 * FNAME_EXP expand to path
5839 * FNAME_HYP check for hypertext link
5840 * FNAME_INCL apply "includeexpr"
5842 char_u *
5843 file_name_at_cursor(options, count, file_lnum)
5844 int options;
5845 long count;
5846 linenr_T *file_lnum;
5848 return file_name_in_line(ml_get_curline(),
5849 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5850 file_lnum);
5854 * Return the name of the file under or after ptr[col].
5855 * Otherwise like file_name_at_cursor().
5857 char_u *
5858 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5859 char_u *line;
5860 int col;
5861 int options;
5862 long count;
5863 char_u *rel_fname; /* file we are searching relative to */
5864 linenr_T *file_lnum; /* line number after the file name */
5866 char_u *ptr;
5867 int len;
5870 * search forward for what could be the start of a file name
5872 ptr = line + col;
5873 while (*ptr != NUL && !vim_isfilec(*ptr))
5874 mb_ptr_adv(ptr);
5875 if (*ptr == NUL) /* nothing found */
5877 if (options & FNAME_MESS)
5878 EMSG(_("E446: No file name under cursor"));
5879 return NULL;
5883 * Search backward for first char of the file name.
5884 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5886 while (ptr > line)
5888 #ifdef FEAT_MBYTE
5889 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5890 ptr -= len + 1;
5891 else
5892 #endif
5893 if (vim_isfilec(ptr[-1])
5894 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5895 --ptr;
5896 else
5897 break;
5901 * Search forward for the last char of the file name.
5902 * Also allow "://" when ':' is not in 'isfname'.
5904 len = 0;
5905 while (vim_isfilec(ptr[len])
5906 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5907 #ifdef FEAT_MBYTE
5908 if (has_mbyte)
5909 len += (*mb_ptr2len)(ptr + len);
5910 else
5911 #endif
5912 ++len;
5915 * If there is trailing punctuation, remove it.
5916 * But don't remove "..", could be a directory name.
5918 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5919 && ptr[len - 2] != '.')
5920 --len;
5922 if (file_lnum != NULL)
5924 char_u *p;
5926 /* Get the number after the file name and a separator character */
5927 p = ptr + len;
5928 p = skipwhite(p);
5929 if (*p != NUL)
5931 if (!isdigit(*p))
5932 ++p; /* skip the separator */
5933 p = skipwhite(p);
5934 if (isdigit(*p))
5935 *file_lnum = (int)getdigits(&p);
5939 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5942 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5943 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5945 static char_u *
5946 eval_includeexpr(ptr, len)
5947 char_u *ptr;
5948 int len;
5950 char_u *res;
5952 set_vim_var_string(VV_FNAME, ptr, len);
5953 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5954 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5955 set_vim_var_string(VV_FNAME, NULL, 0);
5956 return res;
5958 #endif
5961 * Return the name of the file ptr[len] in 'path'.
5962 * Otherwise like file_name_at_cursor().
5964 char_u *
5965 find_file_name_in_path(ptr, len, options, count, rel_fname)
5966 char_u *ptr;
5967 int len;
5968 int options;
5969 long count;
5970 char_u *rel_fname; /* file we are searching relative to */
5972 char_u *file_name;
5973 int c;
5974 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5975 char_u *tofree = NULL;
5977 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5979 tofree = eval_includeexpr(ptr, len);
5980 if (tofree != NULL)
5982 ptr = tofree;
5983 len = (int)STRLEN(ptr);
5986 # endif
5988 if (options & FNAME_EXP)
5990 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5991 TRUE, rel_fname);
5993 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5995 * If the file could not be found in a normal way, try applying
5996 * 'includeexpr' (unless done already).
5998 if (file_name == NULL
5999 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6001 tofree = eval_includeexpr(ptr, len);
6002 if (tofree != NULL)
6004 ptr = tofree;
6005 len = (int)STRLEN(ptr);
6006 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6007 TRUE, rel_fname);
6010 # endif
6011 if (file_name == NULL && (options & FNAME_MESS))
6013 c = ptr[len];
6014 ptr[len] = NUL;
6015 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6016 ptr[len] = c;
6019 /* Repeat finding the file "count" times. This matters when it
6020 * appears several times in the path. */
6021 while (file_name != NULL && --count > 0)
6023 vim_free(file_name);
6024 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6027 else
6028 file_name = vim_strnsave(ptr, len);
6030 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6031 vim_free(tofree);
6032 # endif
6034 return file_name;
6036 #endif /* FEAT_SEARCHPATH */
6039 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6040 * Also check for ":\\", which MS Internet Explorer accepts, return
6041 * URL_BACKSLASH.
6043 static int
6044 path_is_url(p)
6045 char_u *p;
6047 if (STRNCMP(p, "://", (size_t)3) == 0)
6048 return URL_SLASH;
6049 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6050 return URL_BACKSLASH;
6051 return 0;
6055 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6056 * Return URL_BACKSLASH for "name:\\".
6057 * Return zero otherwise.
6060 path_with_url(fname)
6061 char_u *fname;
6063 char_u *p;
6065 for (p = fname; isalpha(*p); ++p)
6067 return path_is_url(p);
6071 * Return TRUE if "name" is a full (absolute) path name or URL.
6074 vim_isAbsName(name)
6075 char_u *name;
6077 return (path_with_url(name) != 0 || mch_isFullName(name));
6081 * Get absolute file name into buffer "buf[len]".
6083 * return FAIL for failure, OK otherwise
6086 vim_FullName(fname, buf, len, force)
6087 char_u *fname, *buf;
6088 int len;
6089 int force; /* force expansion even when already absolute */
6091 int retval = OK;
6092 int url;
6094 *buf = NUL;
6095 if (fname == NULL)
6096 return FAIL;
6098 url = path_with_url(fname);
6099 if (!url)
6100 retval = mch_FullName(fname, buf, len, force);
6101 if (url || retval == FAIL)
6103 /* something failed; use the file name (truncate when too long) */
6104 vim_strncpy(buf, fname, len - 1);
6106 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6107 slash_adjust(buf);
6108 #endif
6109 return retval;
6113 * Return the minimal number of rows that is needed on the screen to display
6114 * the current number of windows.
6117 min_rows()
6119 int total;
6120 #ifdef FEAT_WINDOWS
6121 tabpage_T *tp;
6122 int n;
6123 #endif
6125 if (firstwin == NULL) /* not initialized yet */
6126 return MIN_LINES;
6128 #ifdef FEAT_WINDOWS
6129 total = 0;
6130 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6132 n = frame_minheight(tp->tp_topframe, NULL);
6133 if (total < n)
6134 total = n;
6136 total += tabline_height();
6137 #else
6138 total = 1; /* at least one window should have a line! */
6139 #endif
6140 total += 1; /* count the room for the command line */
6141 return total;
6145 * Return TRUE if there is only one window (in the current tab page), not
6146 * counting a help or preview window, unless it is the current window.
6147 * Does not count "aucmd_win".
6150 only_one_window()
6152 #ifdef FEAT_WINDOWS
6153 int count = 0;
6154 win_T *wp;
6156 /* If there is another tab page there always is another window. */
6157 if (first_tabpage->tp_next != NULL)
6158 return FALSE;
6160 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6161 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
6162 # ifdef FEAT_QUICKFIX
6163 || wp->w_p_pvw
6164 # endif
6165 ) || wp == curwin)
6166 # ifdef FEAT_AUTOCMD
6167 && wp != aucmd_win
6168 # endif
6170 ++count;
6171 return (count <= 1);
6172 #else
6173 return TRUE;
6174 #endif
6177 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6179 * Correct the cursor line number in other windows. Used after changing the
6180 * current buffer, and before applying autocommands.
6181 * When "do_curwin" is TRUE, also check current window.
6183 void
6184 check_lnums(do_curwin)
6185 int do_curwin;
6187 win_T *wp;
6189 #ifdef FEAT_WINDOWS
6190 tabpage_T *tp;
6192 FOR_ALL_TAB_WINDOWS(tp, wp)
6193 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6194 #else
6195 wp = curwin;
6196 if (do_curwin)
6197 #endif
6199 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6200 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6201 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6202 wp->w_topline = curbuf->b_ml.ml_line_count;
6205 #endif
6207 #if defined(FEAT_WINDOWS) || defined(PROTO)
6210 * A snapshot of the window sizes, to restore them after closing the help
6211 * window.
6212 * Only these fields are used:
6213 * fr_layout
6214 * fr_width
6215 * fr_height
6216 * fr_next
6217 * fr_child
6218 * fr_win (only valid for the old curwin, NULL otherwise)
6222 * Create a snapshot of the current frame sizes.
6224 void
6225 make_snapshot(idx)
6226 int idx;
6228 clear_snapshot(curtab, idx);
6229 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6232 static void
6233 make_snapshot_rec(fr, frp)
6234 frame_T *fr;
6235 frame_T **frp;
6237 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6238 if (*frp == NULL)
6239 return;
6240 (*frp)->fr_layout = fr->fr_layout;
6241 # ifdef FEAT_VERTSPLIT
6242 (*frp)->fr_width = fr->fr_width;
6243 # endif
6244 (*frp)->fr_height = fr->fr_height;
6245 if (fr->fr_next != NULL)
6246 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6247 if (fr->fr_child != NULL)
6248 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6249 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6250 (*frp)->fr_win = curwin;
6254 * Remove any existing snapshot.
6256 static void
6257 clear_snapshot(tp, idx)
6258 tabpage_T *tp;
6259 int idx;
6261 clear_snapshot_rec(tp->tp_snapshot[idx]);
6262 tp->tp_snapshot[idx] = NULL;
6265 static void
6266 clear_snapshot_rec(fr)
6267 frame_T *fr;
6269 if (fr != NULL)
6271 clear_snapshot_rec(fr->fr_next);
6272 clear_snapshot_rec(fr->fr_child);
6273 vim_free(fr);
6278 * Restore a previously created snapshot, if there is any.
6279 * This is only done if the screen size didn't change and the window layout is
6280 * still the same.
6282 void
6283 restore_snapshot(idx, close_curwin)
6284 int idx;
6285 int close_curwin; /* closing current window */
6287 win_T *wp;
6289 if (curtab->tp_snapshot[idx] != NULL
6290 # ifdef FEAT_VERTSPLIT
6291 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6292 # endif
6293 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6294 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6296 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6297 win_comp_pos();
6298 if (wp != NULL && close_curwin)
6299 win_goto(wp);
6300 redraw_all_later(CLEAR);
6302 clear_snapshot(curtab, idx);
6306 * Check if frames "sn" and "fr" have the same layout, same following frames
6307 * and same children.
6309 static int
6310 check_snapshot_rec(sn, fr)
6311 frame_T *sn;
6312 frame_T *fr;
6314 if (sn->fr_layout != fr->fr_layout
6315 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6316 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6317 || (sn->fr_next != NULL
6318 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6319 || (sn->fr_child != NULL
6320 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6321 return FAIL;
6322 return OK;
6326 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6327 * following frames and children.
6328 * Returns a pointer to the old current window, or NULL.
6330 static win_T *
6331 restore_snapshot_rec(sn, fr)
6332 frame_T *sn;
6333 frame_T *fr;
6335 win_T *wp = NULL;
6336 win_T *wp2;
6338 fr->fr_height = sn->fr_height;
6339 # ifdef FEAT_VERTSPLIT
6340 fr->fr_width = sn->fr_width;
6341 # endif
6342 if (fr->fr_layout == FR_LEAF)
6344 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6345 # ifdef FEAT_VERTSPLIT
6346 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6347 # endif
6348 wp = sn->fr_win;
6350 if (sn->fr_next != NULL)
6352 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6353 if (wp2 != NULL)
6354 wp = wp2;
6356 if (sn->fr_child != NULL)
6358 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6359 if (wp2 != NULL)
6360 wp = wp2;
6362 return wp;
6365 #endif
6367 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6369 * Return TRUE if there is any vertically split window.
6372 win_hasvertsplit()
6374 frame_T *fr;
6376 if (topframe->fr_layout == FR_ROW)
6377 return TRUE;
6379 if (topframe->fr_layout == FR_COL)
6380 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6381 if (fr->fr_layout == FR_ROW)
6382 return TRUE;
6384 return FALSE;
6386 #endif
6388 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6390 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6391 * highlighted with the group 'grp' with priority 'prio'.
6392 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6393 * If no particular ID is desired, -1 must be specified for 'id'.
6394 * Return ID of added match, -1 on failure.
6397 match_add(wp, grp, pat, prio, id)
6398 win_T *wp;
6399 char_u *grp;
6400 char_u *pat;
6401 int prio;
6402 int id;
6404 matchitem_T *cur;
6405 matchitem_T *prev;
6406 matchitem_T *m;
6407 int hlg_id;
6408 regprog_T *regprog;
6410 if (*grp == NUL || *pat == NUL)
6411 return -1;
6412 if (id < -1 || id == 0)
6414 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6415 return -1;
6417 if (id != -1)
6419 cur = wp->w_match_head;
6420 while (cur != NULL)
6422 if (cur->id == id)
6424 EMSGN("E801: ID already taken: %ld", id);
6425 return -1;
6427 cur = cur->next;
6430 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6432 EMSG2(_(e_nogroup), grp);
6433 return -1;
6435 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6437 EMSG2(_(e_invarg2), pat);
6438 return -1;
6441 /* Find available match ID. */
6442 while (id == -1)
6444 cur = wp->w_match_head;
6445 while (cur != NULL && cur->id != wp->w_next_match_id)
6446 cur = cur->next;
6447 if (cur == NULL)
6448 id = wp->w_next_match_id;
6449 wp->w_next_match_id++;
6452 /* Build new match. */
6453 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6454 m->id = id;
6455 m->priority = prio;
6456 m->pattern = vim_strsave(pat);
6457 m->hlg_id = hlg_id;
6458 m->match.regprog = regprog;
6459 m->match.rmm_ic = FALSE;
6460 m->match.rmm_maxcol = 0;
6462 /* Insert new match. The match list is in ascending order with regard to
6463 * the match priorities. */
6464 cur = wp->w_match_head;
6465 prev = cur;
6466 while (cur != NULL && prio >= cur->priority)
6468 prev = cur;
6469 cur = cur->next;
6471 if (cur == prev)
6472 wp->w_match_head = m;
6473 else
6474 prev->next = m;
6475 m->next = cur;
6477 redraw_later(SOME_VALID);
6478 return id;
6482 * Delete match with ID 'id' in the match list of window 'wp'.
6483 * Print error messages if 'perr' is TRUE.
6486 match_delete(wp, id, perr)
6487 win_T *wp;
6488 int id;
6489 int perr;
6491 matchitem_T *cur = wp->w_match_head;
6492 matchitem_T *prev = cur;
6494 if (id < 1)
6496 if (perr == TRUE)
6497 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6498 id);
6499 return -1;
6501 while (cur != NULL && cur->id != id)
6503 prev = cur;
6504 cur = cur->next;
6506 if (cur == NULL)
6508 if (perr == TRUE)
6509 EMSGN("E803: ID not found: %ld", id);
6510 return -1;
6512 if (cur == prev)
6513 wp->w_match_head = cur->next;
6514 else
6515 prev->next = cur->next;
6516 vim_free(cur->match.regprog);
6517 vim_free(cur->pattern);
6518 vim_free(cur);
6519 redraw_later(SOME_VALID);
6520 return 0;
6524 * Delete all matches in the match list of window 'wp'.
6526 void
6527 clear_matches(wp)
6528 win_T *wp;
6530 matchitem_T *m;
6532 while (wp->w_match_head != NULL)
6534 m = wp->w_match_head->next;
6535 vim_free(wp->w_match_head->match.regprog);
6536 vim_free(wp->w_match_head->pattern);
6537 vim_free(wp->w_match_head);
6538 wp->w_match_head = m;
6540 redraw_later(SOME_VALID);
6544 * Get match from ID 'id' in window 'wp'.
6545 * Return NULL if match not found.
6547 matchitem_T *
6548 get_match(wp, id)
6549 win_T *wp;
6550 int id;
6552 matchitem_T *cur = wp->w_match_head;
6554 while (cur != NULL && cur->id != id)
6555 cur = cur->next;
6556 return cur;
6558 #endif