Merge branch 'vim'
[MacVim.git] / src / window.c
blobb5061cd5a3808eca6d875c806312c640a6afdf87
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 win_new_height(oldwin, oldwin->w_height - 1);
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 win_new_height(wp, 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 win_new_height(wp, 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 win_new_width(wp, 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 win_new_width(wp, Columns);
1053 wp->w_vsep_width = 0;
1055 else
1057 wp->w_wincol = oldwin->w_wincol;
1058 win_new_width(wp, 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 * equalize the window sizes.
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;
1153 * make the new window the current window
1155 win_enter(wp, FALSE);
1156 #ifdef FEAT_VERTSPLIT
1157 if (flags & WSP_VERT)
1158 p_wiw = i;
1159 else
1160 #endif
1161 p_wh = i;
1163 return OK;
1168 * Initialize window "newp" from window "oldp".
1169 * Used when splitting a window and when creating a new tab page.
1170 * The windows will both edit the same buffer.
1171 * WSP_NEWLOC may be specified in flags to prevent the location list from
1172 * being copied.
1174 static void
1175 win_init(newp, oldp, flags)
1176 win_T *newp;
1177 win_T *oldp;
1178 int flags UNUSED;
1180 int i;
1182 newp->w_buffer = oldp->w_buffer;
1183 oldp->w_buffer->b_nwindows++;
1184 newp->w_cursor = oldp->w_cursor;
1185 newp->w_valid = 0;
1186 newp->w_curswant = oldp->w_curswant;
1187 newp->w_set_curswant = oldp->w_set_curswant;
1188 newp->w_topline = oldp->w_topline;
1189 #ifdef FEAT_DIFF
1190 newp->w_topfill = oldp->w_topfill;
1191 #endif
1192 newp->w_leftcol = oldp->w_leftcol;
1193 newp->w_pcmark = oldp->w_pcmark;
1194 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1195 newp->w_alt_fnum = oldp->w_alt_fnum;
1196 newp->w_wrow = oldp->w_wrow;
1197 newp->w_fraction = oldp->w_fraction;
1198 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1199 #ifdef FEAT_JUMPLIST
1200 copy_jumplist(oldp, newp);
1201 #endif
1202 #ifdef FEAT_QUICKFIX
1203 if (flags & WSP_NEWLOC)
1205 /* Don't copy the location list. */
1206 newp->w_llist = NULL;
1207 newp->w_llist_ref = NULL;
1209 else
1210 copy_loclist(oldp, newp);
1211 #endif
1212 if (oldp->w_localdir != NULL)
1213 newp->w_localdir = vim_strsave(oldp->w_localdir);
1215 /* copy tagstack and folds */
1216 for (i = 0; i < oldp->w_tagstacklen; i++)
1218 newp->w_tagstack[i] = oldp->w_tagstack[i];
1219 if (newp->w_tagstack[i].tagname != NULL)
1220 newp->w_tagstack[i].tagname =
1221 vim_strsave(newp->w_tagstack[i].tagname);
1223 newp->w_tagstackidx = oldp->w_tagstackidx;
1224 newp->w_tagstacklen = oldp->w_tagstacklen;
1225 # ifdef FEAT_FOLDING
1226 copyFoldingState(oldp, newp);
1227 # endif
1229 win_init_some(newp, oldp);
1233 * Initialize window "newp" from window"old".
1234 * Only the essential things are copied.
1236 static void
1237 win_init_some(newp, oldp)
1238 win_T *newp;
1239 win_T *oldp;
1241 /* Use the same argument list. */
1242 newp->w_alist = oldp->w_alist;
1243 ++newp->w_alist->al_refcount;
1244 newp->w_arg_idx = oldp->w_arg_idx;
1246 /* copy options from existing window */
1247 win_copy_options(oldp, newp);
1250 #endif /* FEAT_WINDOWS */
1252 #if defined(FEAT_WINDOWS) || defined(PROTO)
1254 * Check if "win" is a pointer to an existing window.
1257 win_valid(win)
1258 win_T *win;
1260 win_T *wp;
1262 if (win == NULL)
1263 return FALSE;
1264 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1265 if (wp == win)
1266 return TRUE;
1267 return FALSE;
1271 * Return the number of windows.
1274 win_count()
1276 win_T *wp;
1277 int count = 0;
1279 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1280 ++count;
1281 return count;
1285 * Make "count" windows on the screen.
1286 * Return actual number of windows on the screen.
1287 * Must be called when there is just one window, filling the whole screen
1288 * (excluding the command line).
1291 make_windows(count, vertical)
1292 int count;
1293 int vertical UNUSED; /* split windows vertically if TRUE */
1295 int maxcount;
1296 int todo;
1298 #ifdef FEAT_VERTSPLIT
1299 if (vertical)
1301 /* Each windows needs at least 'winminwidth' lines and a separator
1302 * column. */
1303 maxcount = (curwin->w_width + curwin->w_vsep_width
1304 - (p_wiw - p_wmw)) / (p_wmw + 1);
1306 else
1307 #endif
1309 /* Each window needs at least 'winminheight' lines and a status line. */
1310 maxcount = (curwin->w_height + curwin->w_status_height
1311 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1314 if (maxcount < 2)
1315 maxcount = 2;
1316 if (count > maxcount)
1317 count = maxcount;
1320 * add status line now, otherwise first window will be too big
1322 if (count > 1)
1323 last_status(TRUE);
1325 #ifdef FEAT_AUTOCMD
1327 * Don't execute autocommands while creating the windows. Must do that
1328 * when putting the buffers in the windows.
1330 block_autocmds();
1331 #endif
1333 /* todo is number of windows left to create */
1334 for (todo = count - 1; todo > 0; --todo)
1335 #ifdef FEAT_VERTSPLIT
1336 if (vertical)
1338 if (win_split(curwin->w_width - (curwin->w_width - todo)
1339 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1340 break;
1342 else
1343 #endif
1345 if (win_split(curwin->w_height - (curwin->w_height - todo
1346 * STATUS_HEIGHT) / (todo + 1)
1347 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1348 break;
1351 #ifdef FEAT_AUTOCMD
1352 unblock_autocmds();
1353 #endif
1355 /* return actual number of windows */
1356 return (count - todo);
1360 * Exchange current and next window
1362 static void
1363 win_exchange(Prenum)
1364 long Prenum;
1366 frame_T *frp;
1367 frame_T *frp2;
1368 win_T *wp;
1369 win_T *wp2;
1370 int temp;
1372 if (lastwin == firstwin) /* just one window */
1374 beep_flush();
1375 return;
1378 #ifdef FEAT_GUI
1379 need_mouse_correct = TRUE;
1380 #endif
1383 * find window to exchange with
1385 if (Prenum)
1387 frp = curwin->w_frame->fr_parent->fr_child;
1388 while (frp != NULL && --Prenum > 0)
1389 frp = frp->fr_next;
1391 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1392 frp = curwin->w_frame->fr_next;
1393 else /* Swap last window in row/col with previous */
1394 frp = curwin->w_frame->fr_prev;
1396 /* We can only exchange a window with another window, not with a frame
1397 * containing windows. */
1398 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1399 return;
1400 wp = frp->fr_win;
1403 * 1. remove curwin from the list. Remember after which window it was in wp2
1404 * 2. insert curwin before wp in the list
1405 * if wp != wp2
1406 * 3. remove wp from the list
1407 * 4. insert wp after wp2
1408 * 5. exchange the status line height and vsep width.
1410 wp2 = curwin->w_prev;
1411 frp2 = curwin->w_frame->fr_prev;
1412 if (wp->w_prev != curwin)
1414 win_remove(curwin, NULL);
1415 frame_remove(curwin->w_frame);
1416 win_append(wp->w_prev, curwin);
1417 frame_insert(frp, curwin->w_frame);
1419 if (wp != wp2)
1421 win_remove(wp, NULL);
1422 frame_remove(wp->w_frame);
1423 win_append(wp2, wp);
1424 if (frp2 == NULL)
1425 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1426 else
1427 frame_append(frp2, wp->w_frame);
1429 temp = curwin->w_status_height;
1430 curwin->w_status_height = wp->w_status_height;
1431 wp->w_status_height = temp;
1432 #ifdef FEAT_VERTSPLIT
1433 temp = curwin->w_vsep_width;
1434 curwin->w_vsep_width = wp->w_vsep_width;
1435 wp->w_vsep_width = temp;
1437 /* If the windows are not in the same frame, exchange the sizes to avoid
1438 * messing up the window layout. Otherwise fix the frame sizes. */
1439 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1441 temp = curwin->w_height;
1442 curwin->w_height = wp->w_height;
1443 wp->w_height = temp;
1444 temp = curwin->w_width;
1445 curwin->w_width = wp->w_width;
1446 wp->w_width = temp;
1448 else
1450 frame_fix_height(curwin);
1451 frame_fix_height(wp);
1452 frame_fix_width(curwin);
1453 frame_fix_width(wp);
1455 #endif
1457 (void)win_comp_pos(); /* recompute window positions */
1459 win_enter(wp, TRUE);
1460 redraw_later(CLEAR);
1464 * rotate windows: if upwards TRUE the second window becomes the first one
1465 * if upwards FALSE the first window becomes the second one
1467 static void
1468 win_rotate(upwards, count)
1469 int upwards;
1470 int count;
1472 win_T *wp1;
1473 win_T *wp2;
1474 frame_T *frp;
1475 int n;
1477 if (firstwin == lastwin) /* nothing to do */
1479 beep_flush();
1480 return;
1483 #ifdef FEAT_GUI
1484 need_mouse_correct = TRUE;
1485 #endif
1487 #ifdef FEAT_VERTSPLIT
1488 /* Check if all frames in this row/col have one window. */
1489 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1490 frp = frp->fr_next)
1491 if (frp->fr_win == NULL)
1493 EMSG(_("E443: Cannot rotate when another window is split"));
1494 return;
1496 #endif
1498 while (count--)
1500 if (upwards) /* first window becomes last window */
1502 /* remove first window/frame from the list */
1503 frp = curwin->w_frame->fr_parent->fr_child;
1504 wp1 = frp->fr_win;
1505 win_remove(wp1, NULL);
1506 frame_remove(frp);
1508 /* find last frame and append removed window/frame after it */
1509 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1511 win_append(frp->fr_win, wp1);
1512 frame_append(frp, wp1->w_frame);
1514 wp2 = frp->fr_win; /* previously last window */
1516 else /* last window becomes first window */
1518 /* find last window/frame in the list and remove it */
1519 for (frp = curwin->w_frame; frp->fr_next != NULL;
1520 frp = frp->fr_next)
1522 wp1 = frp->fr_win;
1523 wp2 = wp1->w_prev; /* will become last window */
1524 win_remove(wp1, NULL);
1525 frame_remove(frp);
1527 /* append the removed window/frame before the first in the list */
1528 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1529 frame_insert(frp->fr_parent->fr_child, frp);
1532 /* exchange status height and vsep width of old and new last window */
1533 n = wp2->w_status_height;
1534 wp2->w_status_height = wp1->w_status_height;
1535 wp1->w_status_height = n;
1536 frame_fix_height(wp1);
1537 frame_fix_height(wp2);
1538 #ifdef FEAT_VERTSPLIT
1539 n = wp2->w_vsep_width;
1540 wp2->w_vsep_width = wp1->w_vsep_width;
1541 wp1->w_vsep_width = n;
1542 frame_fix_width(wp1);
1543 frame_fix_width(wp2);
1544 #endif
1546 /* recompute w_winrow and w_wincol for all windows */
1547 (void)win_comp_pos();
1550 redraw_later(CLEAR);
1554 * Move the current window to the very top/bottom/left/right of the screen.
1556 static void
1557 win_totop(size, flags)
1558 int size;
1559 int flags;
1561 int dir;
1562 int height = curwin->w_height;
1564 if (lastwin == firstwin)
1566 beep_flush();
1567 return;
1570 /* Remove the window and frame from the tree of frames. */
1571 (void)winframe_remove(curwin, &dir, NULL);
1572 win_remove(curwin, NULL);
1573 last_status(FALSE); /* may need to remove last status line */
1574 (void)win_comp_pos(); /* recompute window positions */
1576 /* Split a window on the desired side and put the window there. */
1577 (void)win_split_ins(size, flags, curwin, dir);
1578 if (!(flags & WSP_VERT))
1580 win_setheight(height);
1581 if (p_ea)
1582 win_equal(curwin, TRUE, 'v');
1585 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1586 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1587 * scrollbars. Have to update them anyway. */
1588 gui_may_update_scrollbars();
1589 #endif
1593 * Move window "win1" to below/right of "win2" and make "win1" the current
1594 * window. Only works within the same frame!
1596 void
1597 win_move_after(win1, win2)
1598 win_T *win1, *win2;
1600 int height;
1602 /* check if the arguments are reasonable */
1603 if (win1 == win2)
1604 return;
1606 /* check if there is something to do */
1607 if (win2->w_next != win1)
1609 /* may need move the status line/vertical separator of the last window
1610 * */
1611 if (win1 == lastwin)
1613 height = win1->w_prev->w_status_height;
1614 win1->w_prev->w_status_height = win1->w_status_height;
1615 win1->w_status_height = height;
1616 #ifdef FEAT_VERTSPLIT
1617 if (win1->w_prev->w_vsep_width == 1)
1619 /* Remove the vertical separator from the last-but-one window,
1620 * add it to the last window. Adjust the frame widths. */
1621 win1->w_prev->w_vsep_width = 0;
1622 win1->w_prev->w_frame->fr_width -= 1;
1623 win1->w_vsep_width = 1;
1624 win1->w_frame->fr_width += 1;
1626 #endif
1628 else if (win2 == lastwin)
1630 height = win1->w_status_height;
1631 win1->w_status_height = win2->w_status_height;
1632 win2->w_status_height = height;
1633 #ifdef FEAT_VERTSPLIT
1634 if (win1->w_vsep_width == 1)
1636 /* Remove the vertical separator from win1, add it to the last
1637 * window, win2. Adjust the frame widths. */
1638 win2->w_vsep_width = 1;
1639 win2->w_frame->fr_width += 1;
1640 win1->w_vsep_width = 0;
1641 win1->w_frame->fr_width -= 1;
1643 #endif
1645 win_remove(win1, NULL);
1646 frame_remove(win1->w_frame);
1647 win_append(win2, win1);
1648 frame_append(win2->w_frame, win1->w_frame);
1650 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1651 redraw_later(NOT_VALID);
1653 win_enter(win1, FALSE);
1657 * Make all windows the same height.
1658 * 'next_curwin' will soon be the current window, make sure it has enough
1659 * rows.
1661 void
1662 win_equal(next_curwin, current, dir)
1663 win_T *next_curwin; /* pointer to current window to be or NULL */
1664 int current; /* do only frame with current window */
1665 int dir; /* 'v' for vertically, 'h' for horizontally,
1666 'b' for both, 0 for using p_ead */
1668 if (dir == 0)
1669 #ifdef FEAT_VERTSPLIT
1670 dir = *p_ead;
1671 #else
1672 dir = 'b';
1673 #endif
1674 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1675 topframe, dir, 0, tabline_height(),
1676 (int)Columns, topframe->fr_height);
1680 * Set a frame to a new position and height, spreading the available room
1681 * equally over contained frames.
1682 * The window "next_curwin" (if not NULL) should at least get the size from
1683 * 'winheight' and 'winwidth' if possible.
1685 static void
1686 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1687 win_T *next_curwin; /* pointer to current window to be or NULL */
1688 int current; /* do only frame with current window */
1689 frame_T *topfr; /* frame to set size off */
1690 int dir; /* 'v', 'h' or 'b', see win_equal() */
1691 int col; /* horizontal position for frame */
1692 int row; /* vertical position for frame */
1693 int width; /* new width of frame */
1694 int height; /* new height of frame */
1696 int n, m;
1697 int extra_sep = 0;
1698 int wincount, totwincount = 0;
1699 frame_T *fr;
1700 int next_curwin_size = 0;
1701 int room = 0;
1702 int new_size;
1703 int has_next_curwin = 0;
1704 int hnc;
1706 if (topfr->fr_layout == FR_LEAF)
1708 /* Set the width/height of this frame.
1709 * Redraw when size or position changes */
1710 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1711 #ifdef FEAT_VERTSPLIT
1712 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1713 #endif
1716 topfr->fr_win->w_winrow = row;
1717 frame_new_height(topfr, height, FALSE, FALSE);
1718 #ifdef FEAT_VERTSPLIT
1719 topfr->fr_win->w_wincol = col;
1720 frame_new_width(topfr, width, FALSE, FALSE);
1721 #endif
1722 redraw_all_later(CLEAR);
1725 #ifdef FEAT_VERTSPLIT
1726 else if (topfr->fr_layout == FR_ROW)
1728 topfr->fr_width = width;
1729 topfr->fr_height = height;
1731 if (dir != 'v') /* equalize frame widths */
1733 /* Compute the maximum number of windows horizontally in this
1734 * frame. */
1735 n = frame_minwidth(topfr, NOWIN);
1736 /* add one for the rightmost window, it doesn't have a separator */
1737 if (col + width == Columns)
1738 extra_sep = 1;
1739 else
1740 extra_sep = 0;
1741 totwincount = (n + extra_sep) / (p_wmw + 1);
1742 has_next_curwin = frame_has_win(topfr, next_curwin);
1745 * Compute width for "next_curwin" window and room available for
1746 * other windows.
1747 * "m" is the minimal width when counting p_wiw for "next_curwin".
1749 m = frame_minwidth(topfr, next_curwin);
1750 room = width - m;
1751 if (room < 0)
1753 next_curwin_size = p_wiw + room;
1754 room = 0;
1756 else
1758 next_curwin_size = -1;
1759 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1761 /* If 'winfixwidth' set keep the window width if
1762 * possible.
1763 * Watch out for this window being the next_curwin. */
1764 if (frame_fixed_width(fr))
1766 n = frame_minwidth(fr, NOWIN);
1767 new_size = fr->fr_width;
1768 if (frame_has_win(fr, next_curwin))
1770 room += p_wiw - p_wmw;
1771 next_curwin_size = 0;
1772 if (new_size < p_wiw)
1773 new_size = p_wiw;
1775 else
1776 /* These windows don't use up room. */
1777 totwincount -= (n + (fr->fr_next == NULL
1778 ? extra_sep : 0)) / (p_wmw + 1);
1779 room -= new_size - n;
1780 if (room < 0)
1782 new_size += room;
1783 room = 0;
1785 fr->fr_newwidth = new_size;
1788 if (next_curwin_size == -1)
1790 if (!has_next_curwin)
1791 next_curwin_size = 0;
1792 else if (totwincount > 1
1793 && (room + (totwincount - 2))
1794 / (totwincount - 1) > p_wiw)
1796 /* Can make all windows wider than 'winwidth', spread
1797 * the room equally. */
1798 next_curwin_size = (room + p_wiw
1799 + (totwincount - 1) * p_wmw
1800 + (totwincount - 1)) / totwincount;
1801 room -= next_curwin_size - p_wiw;
1803 else
1804 next_curwin_size = p_wiw;
1808 if (has_next_curwin)
1809 --totwincount; /* don't count curwin */
1812 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1814 n = m = 0;
1815 wincount = 1;
1816 if (fr->fr_next == NULL)
1817 /* last frame gets all that remains (avoid roundoff error) */
1818 new_size = width;
1819 else if (dir == 'v')
1820 new_size = fr->fr_width;
1821 else if (frame_fixed_width(fr))
1823 new_size = fr->fr_newwidth;
1824 wincount = 0; /* doesn't count as a sizeable window */
1826 else
1828 /* Compute the maximum number of windows horiz. in "fr". */
1829 n = frame_minwidth(fr, NOWIN);
1830 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1831 / (p_wmw + 1);
1832 m = frame_minwidth(fr, next_curwin);
1833 if (has_next_curwin)
1834 hnc = frame_has_win(fr, next_curwin);
1835 else
1836 hnc = FALSE;
1837 if (hnc) /* don't count next_curwin */
1838 --wincount;
1839 if (totwincount == 0)
1840 new_size = room;
1841 else
1842 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1843 / totwincount;
1844 if (hnc) /* add next_curwin size */
1846 next_curwin_size -= p_wiw - (m - n);
1847 new_size += next_curwin_size;
1848 room -= new_size - next_curwin_size;
1850 else
1851 room -= new_size;
1852 new_size += n;
1855 /* Skip frame that is full width when splitting or closing a
1856 * window, unless equalizing all frames. */
1857 if (!current || dir != 'v' || topfr->fr_parent != NULL
1858 || (new_size != fr->fr_width)
1859 || frame_has_win(fr, next_curwin))
1860 win_equal_rec(next_curwin, current, fr, dir, col, row,
1861 new_size, height);
1862 col += new_size;
1863 width -= new_size;
1864 totwincount -= wincount;
1867 #endif
1868 else /* topfr->fr_layout == FR_COL */
1870 #ifdef FEAT_VERTSPLIT
1871 topfr->fr_width = width;
1872 #endif
1873 topfr->fr_height = height;
1875 if (dir != 'h') /* equalize frame heights */
1877 /* Compute maximum number of windows vertically in this frame. */
1878 n = frame_minheight(topfr, NOWIN);
1879 /* add one for the bottom window if it doesn't have a statusline */
1880 if (row + height == cmdline_row && p_ls == 0)
1881 extra_sep = 1;
1882 else
1883 extra_sep = 0;
1884 totwincount = (n + extra_sep) / (p_wmh + 1);
1885 has_next_curwin = frame_has_win(topfr, next_curwin);
1888 * Compute height for "next_curwin" window and room available for
1889 * other windows.
1890 * "m" is the minimal height when counting p_wh for "next_curwin".
1892 m = frame_minheight(topfr, next_curwin);
1893 room = height - m;
1894 if (room < 0)
1896 /* The room is less then 'winheight', use all space for the
1897 * current window. */
1898 next_curwin_size = p_wh + room;
1899 room = 0;
1901 else
1903 next_curwin_size = -1;
1904 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1906 /* If 'winfixheight' set keep the window height if
1907 * possible.
1908 * Watch out for this window being the next_curwin. */
1909 if (frame_fixed_height(fr))
1911 n = frame_minheight(fr, NOWIN);
1912 new_size = fr->fr_height;
1913 if (frame_has_win(fr, next_curwin))
1915 room += p_wh - p_wmh;
1916 next_curwin_size = 0;
1917 if (new_size < p_wh)
1918 new_size = p_wh;
1920 else
1921 /* These windows don't use up room. */
1922 totwincount -= (n + (fr->fr_next == NULL
1923 ? extra_sep : 0)) / (p_wmh + 1);
1924 room -= new_size - n;
1925 if (room < 0)
1927 new_size += room;
1928 room = 0;
1930 fr->fr_newheight = new_size;
1933 if (next_curwin_size == -1)
1935 if (!has_next_curwin)
1936 next_curwin_size = 0;
1937 else if (totwincount > 1
1938 && (room + (totwincount - 2))
1939 / (totwincount - 1) > p_wh)
1941 /* can make all windows higher than 'winheight',
1942 * spread the room equally. */
1943 next_curwin_size = (room + p_wh
1944 + (totwincount - 1) * p_wmh
1945 + (totwincount - 1)) / totwincount;
1946 room -= next_curwin_size - p_wh;
1948 else
1949 next_curwin_size = p_wh;
1953 if (has_next_curwin)
1954 --totwincount; /* don't count curwin */
1957 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1959 n = m = 0;
1960 wincount = 1;
1961 if (fr->fr_next == NULL)
1962 /* last frame gets all that remains (avoid roundoff error) */
1963 new_size = height;
1964 else if (dir == 'h')
1965 new_size = fr->fr_height;
1966 else if (frame_fixed_height(fr))
1968 new_size = fr->fr_newheight;
1969 wincount = 0; /* doesn't count as a sizeable window */
1971 else
1973 /* Compute the maximum number of windows vert. in "fr". */
1974 n = frame_minheight(fr, NOWIN);
1975 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1976 / (p_wmh + 1);
1977 m = frame_minheight(fr, next_curwin);
1978 if (has_next_curwin)
1979 hnc = frame_has_win(fr, next_curwin);
1980 else
1981 hnc = FALSE;
1982 if (hnc) /* don't count next_curwin */
1983 --wincount;
1984 if (totwincount == 0)
1985 new_size = room;
1986 else
1987 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1988 / totwincount;
1989 if (hnc) /* add next_curwin size */
1991 next_curwin_size -= p_wh - (m - n);
1992 new_size += next_curwin_size;
1993 room -= new_size - next_curwin_size;
1995 else
1996 room -= new_size;
1997 new_size += n;
1999 /* Skip frame that is full width when splitting or closing a
2000 * window, unless equalizing all frames. */
2001 if (!current || dir != 'h' || topfr->fr_parent != NULL
2002 || (new_size != fr->fr_height)
2003 || frame_has_win(fr, next_curwin))
2004 win_equal_rec(next_curwin, current, fr, dir, col, row,
2005 width, new_size);
2006 row += new_size;
2007 height -= new_size;
2008 totwincount -= wincount;
2014 * close all windows for buffer 'buf'
2016 void
2017 close_windows(buf, keep_curwin)
2018 buf_T *buf;
2019 int keep_curwin; /* don't close "curwin" */
2021 win_T *wp;
2022 tabpage_T *tp, *nexttp;
2023 int h = tabline_height();
2025 ++RedrawingDisabled;
2027 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2029 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2031 win_close(wp, FALSE);
2033 /* Start all over, autocommands may change the window layout. */
2034 wp = firstwin;
2036 else
2037 wp = wp->w_next;
2040 /* Also check windows in other tab pages. */
2041 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2043 nexttp = tp->tp_next;
2044 if (tp != curtab)
2045 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2046 if (wp->w_buffer == buf)
2048 win_close_othertab(wp, FALSE, tp);
2050 /* Start all over, the tab page may be closed and
2051 * autocommands may change the window layout. */
2052 nexttp = first_tabpage;
2053 break;
2057 --RedrawingDisabled;
2059 if (h != tabline_height())
2060 shell_new_rows();
2064 * Return TRUE if the current window is the only window that exists (ignoring
2065 * "aucmd_win").
2066 * Returns FALSE if there is a window, possibly in another tab page.
2068 static int
2069 last_window()
2071 return (one_window() && first_tabpage->tp_next == NULL);
2075 * Return TRUE if there is only one window other than "aucmd_win" in the
2076 * current tab page.
2078 static int
2079 one_window()
2081 #ifdef FEAT_AUTOCMD
2082 win_T *wp;
2083 int seen_one = FALSE;
2085 FOR_ALL_WINDOWS(wp)
2087 if (wp != aucmd_win)
2089 if (seen_one)
2090 return FALSE;
2091 seen_one = TRUE;
2094 return TRUE;
2095 #else
2096 return firstwin == lastwin;
2097 #endif
2101 * Close window "win". Only works for the current tab page.
2102 * If "free_buf" is TRUE related buffer may be unloaded.
2104 * called by :quit, :close, :xit, :wq and findtag()
2106 void
2107 win_close(win, free_buf)
2108 win_T *win;
2109 int free_buf;
2111 win_T *wp;
2112 #ifdef FEAT_AUTOCMD
2113 int other_buffer = FALSE;
2114 #endif
2115 int close_curwin = FALSE;
2116 int dir;
2117 int help_window = FALSE;
2118 tabpage_T *prev_curtab = curtab;
2120 if (last_window())
2122 EMSG(_("E444: Cannot close last window"));
2123 return;
2126 #ifdef FEAT_AUTOCMD
2127 if (win == aucmd_win)
2129 EMSG(_("E813: Cannot close autocmd window"));
2130 return;
2132 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2134 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2135 return;
2137 #endif
2140 * When closing the last window in a tab page first go to another tab
2141 * page and then close the window and the tab page. This avoids that
2142 * curwin and curtab are not invalid while we are freeing memory, they may
2143 * be used in GUI events.
2145 if (firstwin == lastwin)
2147 goto_tabpage_tp(alt_tabpage());
2148 redraw_tabline = TRUE;
2150 /* Safety check: Autocommands may have closed the window when jumping
2151 * to the other tab page. */
2152 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2154 int h = tabline_height();
2156 win_close_othertab(win, free_buf, prev_curtab);
2157 if (h != tabline_height())
2158 shell_new_rows();
2160 return;
2163 /* When closing the help window, try restoring a snapshot after closing
2164 * the window. Otherwise clear the snapshot, it's now invalid. */
2165 if (win->w_buffer->b_help)
2166 help_window = TRUE;
2167 else
2168 clear_snapshot(curtab, SNAP_HELP_IDX);
2170 #ifdef FEAT_AUTOCMD
2171 if (win == curwin)
2174 * Guess which window is going to be the new current window.
2175 * This may change because of the autocommands (sigh).
2177 wp = frame2win(win_altframe(win, NULL));
2180 * Be careful: If autocommands delete the window, return now.
2182 if (wp->w_buffer != curbuf)
2184 other_buffer = TRUE;
2185 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2186 if (!win_valid(win) || last_window())
2187 return;
2189 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2190 if (!win_valid(win) || last_window())
2191 return;
2192 # ifdef FEAT_EVAL
2193 /* autocmds may abort script processing */
2194 if (aborting())
2195 return;
2196 # endif
2198 #endif
2200 #ifdef FEAT_GUI
2201 /* Avoid trouble with scrollbars that are going to be deleted in
2202 * win_free(). */
2203 if (gui.in_use)
2204 out_flush();
2205 #endif
2208 * Close the link to the buffer.
2210 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2212 /* Autocommands may have closed the window already, or closed the only
2213 * other window or moved to another tab page. */
2214 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2215 return;
2217 /* Free the memory used for the window. */
2218 wp = win_free_mem(win, &dir, NULL);
2220 /* Make sure curwin isn't invalid. It can cause severe trouble when
2221 * printing an error message. For win_equal() curbuf needs to be valid
2222 * too. */
2223 if (win == curwin)
2225 curwin = wp;
2226 #ifdef FEAT_QUICKFIX
2227 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2230 * If the cursor goes to the preview or the quickfix window, try
2231 * finding another window to go to.
2233 for (;;)
2235 if (wp->w_next == NULL)
2236 wp = firstwin;
2237 else
2238 wp = wp->w_next;
2239 if (wp == curwin)
2240 break;
2241 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2243 curwin = wp;
2244 break;
2248 #endif
2249 curbuf = curwin->w_buffer;
2250 close_curwin = TRUE;
2252 if (p_ea
2253 #ifdef FEAT_VERTSPLIT
2254 && (*p_ead == 'b' || *p_ead == dir)
2255 #endif
2257 win_equal(curwin, TRUE,
2258 #ifdef FEAT_VERTSPLIT
2260 #else
2262 #endif
2264 else
2265 win_comp_pos();
2266 if (close_curwin)
2268 win_enter_ext(wp, FALSE, TRUE);
2269 #ifdef FEAT_AUTOCMD
2270 if (other_buffer)
2271 /* careful: after this wp and win may be invalid! */
2272 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2273 #endif
2277 * If last window has a status line now and we don't want one,
2278 * remove the status line.
2280 last_status(FALSE);
2282 /* After closing the help window, try restoring the window layout from
2283 * before it was opened. */
2284 if (help_window)
2285 restore_snapshot(SNAP_HELP_IDX, close_curwin);
2287 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2288 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2289 if (gui.in_use && !win_hasvertsplit())
2290 gui_init_which_components(NULL);
2291 #endif
2293 redraw_all_later(NOT_VALID);
2297 * Close window "win" in tab page "tp", which is not the current tab page.
2298 * This may be the last window ih that tab page and result in closing the tab,
2299 * thus "tp" may become invalid!
2300 * Caller must check if buffer is hidden and whether the tabline needs to be
2301 * updated.
2303 void
2304 win_close_othertab(win, free_buf, tp)
2305 win_T *win;
2306 int free_buf;
2307 tabpage_T *tp;
2309 win_T *wp;
2310 int dir;
2311 tabpage_T *ptp = NULL;
2313 /* Close the link to the buffer. */
2314 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2316 /* Careful: Autocommands may have closed the tab page or made it the
2317 * current tab page. */
2318 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2320 if (ptp == NULL || tp == curtab)
2321 return;
2323 /* Autocommands may have closed the window already. */
2324 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2326 if (wp == NULL)
2327 return;
2329 /* Free the memory used for the window. */
2330 wp = win_free_mem(win, &dir, tp);
2332 /* When closing the last window in a tab page remove the tab page. */
2333 if (wp == NULL)
2335 if (tp == first_tabpage)
2336 first_tabpage = tp->tp_next;
2337 else
2339 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2340 ptp = ptp->tp_next)
2342 if (ptp == NULL)
2344 EMSG2(_(e_intern2), "win_close_othertab()");
2345 return;
2347 ptp->tp_next = tp->tp_next;
2349 free_tabpage(tp);
2354 * Free the memory used for a window.
2355 * Returns a pointer to the window that got the freed up space.
2357 static win_T *
2358 win_free_mem(win, dirp, tp)
2359 win_T *win;
2360 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2361 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2363 frame_T *frp;
2364 win_T *wp;
2366 /* Remove the window and its frame from the tree of frames. */
2367 frp = win->w_frame;
2368 wp = winframe_remove(win, dirp, tp);
2369 vim_free(frp);
2370 win_free(win, tp);
2372 /* When deleting the current window of another tab page select a new
2373 * current window. */
2374 if (tp != NULL && win == tp->tp_curwin)
2375 tp->tp_curwin = wp;
2377 return wp;
2380 #if defined(EXITFREE) || defined(PROTO)
2381 void
2382 win_free_all()
2384 int dummy;
2386 # ifdef FEAT_WINDOWS
2387 while (first_tabpage->tp_next != NULL)
2388 tabpage_close(TRUE);
2389 # endif
2391 # ifdef FEAT_AUTOCMD
2392 if (aucmd_win != NULL)
2394 (void)win_free_mem(aucmd_win, &dummy, NULL);
2395 aucmd_win = NULL;
2397 # endif
2399 while (firstwin != NULL)
2400 (void)win_free_mem(firstwin, &dummy, NULL);
2402 #endif
2405 * Remove a window and its frame from the tree of frames.
2406 * Returns a pointer to the window that got the freed up space.
2408 win_T *
2409 winframe_remove(win, dirp, tp)
2410 win_T *win;
2411 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
2412 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2414 frame_T *frp, *frp2, *frp3;
2415 frame_T *frp_close = win->w_frame;
2416 win_T *wp;
2419 * If there is only one window there is nothing to remove.
2421 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2422 return NULL;
2425 * Remove the window from its frame.
2427 frp2 = win_altframe(win, tp);
2428 wp = frame2win(frp2);
2430 /* Remove this frame from the list of frames. */
2431 frame_remove(frp_close);
2433 #ifdef FEAT_VERTSPLIT
2434 if (frp_close->fr_parent->fr_layout == FR_COL)
2436 #endif
2437 /* When 'winfixheight' is set, try to find another frame in the column
2438 * (as close to the closed frame as possible) to distribute the height
2439 * to. */
2440 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2442 frp = frp_close->fr_prev;
2443 frp3 = frp_close->fr_next;
2444 while (frp != NULL || frp3 != NULL)
2446 if (frp != NULL)
2448 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2450 frp2 = frp;
2451 wp = frp->fr_win;
2452 break;
2454 frp = frp->fr_prev;
2456 if (frp3 != NULL)
2458 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2460 frp2 = frp3;
2461 wp = frp3->fr_win;
2462 break;
2464 frp3 = frp3->fr_next;
2468 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2469 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2470 #ifdef FEAT_VERTSPLIT
2471 *dirp = 'v';
2473 else
2475 /* When 'winfixwidth' is set, try to find another frame in the column
2476 * (as close to the closed frame as possible) to distribute the width
2477 * to. */
2478 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2480 frp = frp_close->fr_prev;
2481 frp3 = frp_close->fr_next;
2482 while (frp != NULL || frp3 != NULL)
2484 if (frp != NULL)
2486 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2488 frp2 = frp;
2489 wp = frp->fr_win;
2490 break;
2492 frp = frp->fr_prev;
2494 if (frp3 != NULL)
2496 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2498 frp2 = frp3;
2499 wp = frp3->fr_win;
2500 break;
2502 frp3 = frp3->fr_next;
2506 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2507 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2508 *dirp = 'h';
2510 #endif
2512 /* If rows/columns go to a window below/right its positions need to be
2513 * updated. Can only be done after the sizes have been updated. */
2514 if (frp2 == frp_close->fr_next)
2516 int row = win->w_winrow;
2517 int col = W_WINCOL(win);
2519 frame_comp_pos(frp2, &row, &col);
2522 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2524 /* There is no other frame in this list, move its info to the parent
2525 * and remove it. */
2526 frp2->fr_parent->fr_layout = frp2->fr_layout;
2527 frp2->fr_parent->fr_child = frp2->fr_child;
2528 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2529 frp->fr_parent = frp2->fr_parent;
2530 frp2->fr_parent->fr_win = frp2->fr_win;
2531 if (frp2->fr_win != NULL)
2532 frp2->fr_win->w_frame = frp2->fr_parent;
2533 frp = frp2->fr_parent;
2534 vim_free(frp2);
2536 frp2 = frp->fr_parent;
2537 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2539 /* The frame above the parent has the same layout, have to merge
2540 * the frames into this list. */
2541 if (frp2->fr_child == frp)
2542 frp2->fr_child = frp->fr_child;
2543 frp->fr_child->fr_prev = frp->fr_prev;
2544 if (frp->fr_prev != NULL)
2545 frp->fr_prev->fr_next = frp->fr_child;
2546 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2548 frp3->fr_parent = frp2;
2549 if (frp3->fr_next == NULL)
2551 frp3->fr_next = frp->fr_next;
2552 if (frp->fr_next != NULL)
2553 frp->fr_next->fr_prev = frp3;
2554 break;
2557 vim_free(frp);
2561 return wp;
2565 * Find out which frame is going to get the freed up space when "win" is
2566 * closed.
2567 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2568 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2569 * This makes opening a window and closing it immediately keep the same window
2570 * layout.
2572 static frame_T *
2573 win_altframe(win, tp)
2574 win_T *win;
2575 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2577 frame_T *frp;
2578 int b;
2580 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2581 /* Last window in this tab page, will go to next tab page. */
2582 return alt_tabpage()->tp_curwin->w_frame;
2584 frp = win->w_frame;
2585 #ifdef FEAT_VERTSPLIT
2586 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2587 b = p_spr;
2588 else
2589 #endif
2590 b = p_sb;
2591 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2592 return frp->fr_next;
2593 return frp->fr_prev;
2597 * Return the tabpage that will be used if the current one is closed.
2599 static tabpage_T *
2600 alt_tabpage()
2602 tabpage_T *tp;
2604 /* Use the next tab page if possible. */
2605 if (curtab->tp_next != NULL)
2606 return curtab->tp_next;
2608 /* Find the last but one tab page. */
2609 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2611 return tp;
2615 * Find the left-upper window in frame "frp".
2617 static win_T *
2618 frame2win(frp)
2619 frame_T *frp;
2621 while (frp->fr_win == NULL)
2622 frp = frp->fr_child;
2623 return frp->fr_win;
2627 * Return TRUE if frame "frp" contains window "wp".
2629 static int
2630 frame_has_win(frp, wp)
2631 frame_T *frp;
2632 win_T *wp;
2634 frame_T *p;
2636 if (frp->fr_layout == FR_LEAF)
2637 return frp->fr_win == wp;
2639 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2640 if (frame_has_win(p, wp))
2641 return TRUE;
2642 return FALSE;
2646 * Set a new height for a frame. Recursively sets the height for contained
2647 * frames and windows. Caller must take care of positions.
2649 static void
2650 frame_new_height(topfrp, height, topfirst, wfh)
2651 frame_T *topfrp;
2652 int height;
2653 int topfirst; /* resize topmost contained frame first */
2654 int wfh; /* obey 'winfixheight' when there is a choice;
2655 may cause the height not to be set */
2657 frame_T *frp;
2658 int extra_lines;
2659 int h;
2661 if (topfrp->fr_win != NULL)
2663 /* Simple case: just one window. */
2664 win_new_height(topfrp->fr_win,
2665 height - topfrp->fr_win->w_status_height);
2667 #ifdef FEAT_VERTSPLIT
2668 else if (topfrp->fr_layout == FR_ROW)
2672 /* All frames in this row get the same new height. */
2673 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2675 frame_new_height(frp, height, topfirst, wfh);
2676 if (frp->fr_height > height)
2678 /* Could not fit the windows, make the whole row higher. */
2679 height = frp->fr_height;
2680 break;
2684 while (frp != NULL);
2686 #endif
2687 else /* fr_layout == FR_COL */
2689 /* Complicated case: Resize a column of frames. Resize the bottom
2690 * frame first, frames above that when needed. */
2692 frp = topfrp->fr_child;
2693 if (wfh)
2694 /* Advance past frames with one window with 'wfh' set. */
2695 while (frame_fixed_height(frp))
2697 frp = frp->fr_next;
2698 if (frp == NULL)
2699 return; /* no frame without 'wfh', give up */
2701 if (!topfirst)
2703 /* Find the bottom frame of this column */
2704 while (frp->fr_next != NULL)
2705 frp = frp->fr_next;
2706 if (wfh)
2707 /* Advance back for frames with one window with 'wfh' set. */
2708 while (frame_fixed_height(frp))
2709 frp = frp->fr_prev;
2712 extra_lines = height - topfrp->fr_height;
2713 if (extra_lines < 0)
2715 /* reduce height of contained frames, bottom or top frame first */
2716 while (frp != NULL)
2718 h = frame_minheight(frp, NULL);
2719 if (frp->fr_height + extra_lines < h)
2721 extra_lines += frp->fr_height - h;
2722 frame_new_height(frp, h, topfirst, wfh);
2724 else
2726 frame_new_height(frp, frp->fr_height + extra_lines,
2727 topfirst, wfh);
2728 break;
2730 if (topfirst)
2733 frp = frp->fr_next;
2734 while (wfh && frp != NULL && frame_fixed_height(frp));
2736 else
2739 frp = frp->fr_prev;
2740 while (wfh && frp != NULL && frame_fixed_height(frp));
2742 /* Increase "height" if we could not reduce enough frames. */
2743 if (frp == NULL)
2744 height -= extra_lines;
2747 else if (extra_lines > 0)
2749 /* increase height of bottom or top frame */
2750 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2753 topfrp->fr_height = height;
2757 * Return TRUE if height of frame "frp" should not be changed because of
2758 * the 'winfixheight' option.
2760 static int
2761 frame_fixed_height(frp)
2762 frame_T *frp;
2764 /* frame with one window: fixed height if 'winfixheight' set. */
2765 if (frp->fr_win != NULL)
2766 return frp->fr_win->w_p_wfh;
2768 if (frp->fr_layout == FR_ROW)
2770 /* The frame is fixed height if one of the frames in the row is fixed
2771 * height. */
2772 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2773 if (frame_fixed_height(frp))
2774 return TRUE;
2775 return FALSE;
2778 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2779 * frames in the row are fixed height. */
2780 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2781 if (!frame_fixed_height(frp))
2782 return FALSE;
2783 return TRUE;
2786 #ifdef FEAT_VERTSPLIT
2788 * Return TRUE if width of frame "frp" should not be changed because of
2789 * the 'winfixwidth' option.
2791 static int
2792 frame_fixed_width(frp)
2793 frame_T *frp;
2795 /* frame with one window: fixed width if 'winfixwidth' set. */
2796 if (frp->fr_win != NULL)
2797 return frp->fr_win->w_p_wfw;
2799 if (frp->fr_layout == FR_COL)
2801 /* The frame is fixed width if one of the frames in the row is fixed
2802 * width. */
2803 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2804 if (frame_fixed_width(frp))
2805 return TRUE;
2806 return FALSE;
2809 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2810 * frames in the row are fixed width. */
2811 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2812 if (!frame_fixed_width(frp))
2813 return FALSE;
2814 return TRUE;
2818 * Add a status line to windows at the bottom of "frp".
2819 * Note: Does not check if there is room!
2821 static void
2822 frame_add_statusline(frp)
2823 frame_T *frp;
2825 win_T *wp;
2827 if (frp->fr_layout == FR_LEAF)
2829 wp = frp->fr_win;
2830 if (wp->w_status_height == 0)
2832 if (wp->w_height > 0) /* don't make it negative */
2833 --wp->w_height;
2834 wp->w_status_height = STATUS_HEIGHT;
2837 else if (frp->fr_layout == FR_ROW)
2839 /* Handle all the frames in the row. */
2840 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2841 frame_add_statusline(frp);
2843 else /* frp->fr_layout == FR_COL */
2845 /* Only need to handle the last frame in the column. */
2846 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2848 frame_add_statusline(frp);
2853 * Set width of a frame. Handles recursively going through contained frames.
2854 * May remove separator line for windows at the right side (for win_close()).
2856 static void
2857 frame_new_width(topfrp, width, leftfirst, wfw)
2858 frame_T *topfrp;
2859 int width;
2860 int leftfirst; /* resize leftmost contained frame first */
2861 int wfw; /* obey 'winfixwidth' when there is a choice;
2862 may cause the width not to be set */
2864 frame_T *frp;
2865 int extra_cols;
2866 int w;
2867 win_T *wp;
2869 if (topfrp->fr_layout == FR_LEAF)
2871 /* Simple case: just one window. */
2872 wp = topfrp->fr_win;
2873 /* Find out if there are any windows right of this one. */
2874 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2875 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2876 break;
2877 if (frp->fr_parent == NULL)
2878 wp->w_vsep_width = 0;
2879 win_new_width(wp, width - wp->w_vsep_width);
2881 else if (topfrp->fr_layout == FR_COL)
2885 /* All frames in this column get the same new width. */
2886 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2888 frame_new_width(frp, width, leftfirst, wfw);
2889 if (frp->fr_width > width)
2891 /* Could not fit the windows, make whole column wider. */
2892 width = frp->fr_width;
2893 break;
2896 } while (frp != NULL);
2898 else /* fr_layout == FR_ROW */
2900 /* Complicated case: Resize a row of frames. Resize the rightmost
2901 * frame first, frames left of it when needed. */
2903 frp = topfrp->fr_child;
2904 if (wfw)
2905 /* Advance past frames with one window with 'wfw' set. */
2906 while (frame_fixed_width(frp))
2908 frp = frp->fr_next;
2909 if (frp == NULL)
2910 return; /* no frame without 'wfw', give up */
2912 if (!leftfirst)
2914 /* Find the rightmost frame of this row */
2915 while (frp->fr_next != NULL)
2916 frp = frp->fr_next;
2917 if (wfw)
2918 /* Advance back for frames with one window with 'wfw' set. */
2919 while (frame_fixed_width(frp))
2920 frp = frp->fr_prev;
2923 extra_cols = width - topfrp->fr_width;
2924 if (extra_cols < 0)
2926 /* reduce frame width, rightmost frame first */
2927 while (frp != NULL)
2929 w = frame_minwidth(frp, NULL);
2930 if (frp->fr_width + extra_cols < w)
2932 extra_cols += frp->fr_width - w;
2933 frame_new_width(frp, w, leftfirst, wfw);
2935 else
2937 frame_new_width(frp, frp->fr_width + extra_cols,
2938 leftfirst, wfw);
2939 break;
2941 if (leftfirst)
2944 frp = frp->fr_next;
2945 while (wfw && frp != NULL && frame_fixed_width(frp));
2947 else
2950 frp = frp->fr_prev;
2951 while (wfw && frp != NULL && frame_fixed_width(frp));
2953 /* Increase "width" if we could not reduce enough frames. */
2954 if (frp == NULL)
2955 width -= extra_cols;
2958 else if (extra_cols > 0)
2960 /* increase width of rightmost frame */
2961 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2964 topfrp->fr_width = width;
2968 * Add the vertical separator to windows at the right side of "frp".
2969 * Note: Does not check if there is room!
2971 static void
2972 frame_add_vsep(frp)
2973 frame_T *frp;
2975 win_T *wp;
2977 if (frp->fr_layout == FR_LEAF)
2979 wp = frp->fr_win;
2980 if (wp->w_vsep_width == 0)
2982 if (wp->w_width > 0) /* don't make it negative */
2983 --wp->w_width;
2984 wp->w_vsep_width = 1;
2987 else if (frp->fr_layout == FR_COL)
2989 /* Handle all the frames in the column. */
2990 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2991 frame_add_vsep(frp);
2993 else /* frp->fr_layout == FR_ROW */
2995 /* Only need to handle the last frame in the row. */
2996 frp = frp->fr_child;
2997 while (frp->fr_next != NULL)
2998 frp = frp->fr_next;
2999 frame_add_vsep(frp);
3004 * Set frame width from the window it contains.
3006 static void
3007 frame_fix_width(wp)
3008 win_T *wp;
3010 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3012 #endif
3015 * Set frame height from the window it contains.
3017 static void
3018 frame_fix_height(wp)
3019 win_T *wp;
3021 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3025 * Compute the minimal height for frame "topfrp".
3026 * Uses the 'winminheight' option.
3027 * When "next_curwin" isn't NULL, use p_wh for this window.
3028 * When "next_curwin" is NOWIN, don't use at least one line for the current
3029 * window.
3031 static int
3032 frame_minheight(topfrp, next_curwin)
3033 frame_T *topfrp;
3034 win_T *next_curwin;
3036 frame_T *frp;
3037 int m;
3038 #ifdef FEAT_VERTSPLIT
3039 int n;
3040 #endif
3042 if (topfrp->fr_win != NULL)
3044 if (topfrp->fr_win == next_curwin)
3045 m = p_wh + topfrp->fr_win->w_status_height;
3046 else
3048 /* window: minimal height of the window plus status line */
3049 m = p_wmh + topfrp->fr_win->w_status_height;
3050 /* Current window is minimal one line high */
3051 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3052 ++m;
3055 #ifdef FEAT_VERTSPLIT
3056 else if (topfrp->fr_layout == FR_ROW)
3058 /* get the minimal height from each frame in this row */
3059 m = 0;
3060 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3062 n = frame_minheight(frp, next_curwin);
3063 if (n > m)
3064 m = n;
3067 #endif
3068 else
3070 /* Add up the minimal heights for all frames in this column. */
3071 m = 0;
3072 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3073 m += frame_minheight(frp, next_curwin);
3076 return m;
3079 #ifdef FEAT_VERTSPLIT
3081 * Compute the minimal width for frame "topfrp".
3082 * When "next_curwin" isn't NULL, use p_wiw for this window.
3083 * When "next_curwin" is NOWIN, don't use at least one column for the current
3084 * window.
3086 static int
3087 frame_minwidth(topfrp, next_curwin)
3088 frame_T *topfrp;
3089 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3091 frame_T *frp;
3092 int m, n;
3094 if (topfrp->fr_win != NULL)
3096 if (topfrp->fr_win == next_curwin)
3097 m = p_wiw + topfrp->fr_win->w_vsep_width;
3098 else
3100 /* window: minimal width of the window plus separator column */
3101 m = p_wmw + topfrp->fr_win->w_vsep_width;
3102 /* Current window is minimal one column wide */
3103 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3104 ++m;
3107 else if (topfrp->fr_layout == FR_COL)
3109 /* get the minimal width from each frame in this column */
3110 m = 0;
3111 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3113 n = frame_minwidth(frp, next_curwin);
3114 if (n > m)
3115 m = n;
3118 else
3120 /* Add up the minimal widths for all frames in this row. */
3121 m = 0;
3122 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3123 m += frame_minwidth(frp, next_curwin);
3126 return m;
3128 #endif
3132 * Try to close all windows except current one.
3133 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3134 * used and the buffer was modified.
3136 * Used by ":bdel" and ":only".
3138 void
3139 close_others(message, forceit)
3140 int message;
3141 int forceit; /* always hide all other windows */
3143 win_T *wp;
3144 win_T *nextwp;
3145 int r;
3147 if (one_window())
3149 if (message
3150 #ifdef FEAT_AUTOCMD
3151 && !autocmd_busy
3152 #endif
3154 MSG(_(m_onlyone));
3155 return;
3158 /* Be very careful here: autocommands may change the window layout. */
3159 for (wp = firstwin; win_valid(wp); wp = nextwp)
3161 nextwp = wp->w_next;
3162 if (wp != curwin) /* don't close current window */
3165 /* Check if it's allowed to abandon this window */
3166 r = can_abandon(wp->w_buffer, forceit);
3167 #ifdef FEAT_AUTOCMD
3168 if (!win_valid(wp)) /* autocommands messed wp up */
3170 nextwp = firstwin;
3171 continue;
3173 #endif
3174 if (!r)
3176 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3177 if (message && (p_confirm || cmdmod.confirm) && p_write)
3179 dialog_changed(wp->w_buffer, FALSE);
3180 # ifdef FEAT_AUTOCMD
3181 if (!win_valid(wp)) /* autocommands messed wp up */
3183 nextwp = firstwin;
3184 continue;
3186 # endif
3188 if (bufIsChanged(wp->w_buffer))
3189 #endif
3190 continue;
3192 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3196 if (message && lastwin != firstwin)
3197 EMSG(_("E445: Other window contains changes"));
3200 #endif /* FEAT_WINDOWS */
3203 * Init the current window "curwin".
3204 * Called when a new file is being edited.
3206 void
3207 curwin_init()
3209 win_init_empty(curwin);
3212 void
3213 win_init_empty(wp)
3214 win_T *wp;
3216 redraw_win_later(wp, NOT_VALID);
3217 wp->w_lines_valid = 0;
3218 wp->w_cursor.lnum = 1;
3219 wp->w_curswant = wp->w_cursor.col = 0;
3220 #ifdef FEAT_VIRTUALEDIT
3221 wp->w_cursor.coladd = 0;
3222 #endif
3223 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3224 wp->w_pcmark.col = 0;
3225 wp->w_prev_pcmark.lnum = 0;
3226 wp->w_prev_pcmark.col = 0;
3227 wp->w_topline = 1;
3228 #ifdef FEAT_DIFF
3229 wp->w_topfill = 0;
3230 #endif
3231 wp->w_botline = 2;
3232 #ifdef FEAT_FKMAP
3233 if (wp->w_p_rl)
3234 wp->w_farsi = W_CONV + W_R_L;
3235 else
3236 wp->w_farsi = W_CONV;
3237 #endif
3241 * Allocate the first window and put an empty buffer in it.
3242 * Called from main().
3243 * Return FAIL when something goes wrong (out of memory).
3246 win_alloc_first()
3248 if (win_alloc_firstwin(NULL) == FAIL)
3249 return FAIL;
3251 #ifdef FEAT_WINDOWS
3252 first_tabpage = alloc_tabpage();
3253 if (first_tabpage == NULL)
3254 return FAIL;
3255 first_tabpage->tp_topframe = topframe;
3256 curtab = first_tabpage;
3257 #endif
3259 return OK;
3262 #if defined(FEAT_AUTOCMD) || defined(PROTO)
3264 * Init "aucmd_win". This can only be done after the first
3265 * window is fully initialized, thus it can't be in win_alloc_first().
3267 void
3268 win_alloc_aucmd_win()
3270 aucmd_win = win_alloc(NULL, TRUE);
3271 if (aucmd_win != NULL)
3273 win_init_some(aucmd_win, curwin);
3274 # ifdef FEAT_SCROLLBIND
3275 aucmd_win->w_p_scb = FALSE;
3276 # endif
3277 new_frame(aucmd_win);
3280 #endif
3283 * Allocate the first window or the first window in a new tab page.
3284 * When "oldwin" is NULL create an empty buffer for it.
3285 * When "oldwin" is not NULL copy info from it to the new window (only with
3286 * FEAT_WINDOWS).
3287 * Return FAIL when something goes wrong (out of memory).
3289 static int
3290 win_alloc_firstwin(oldwin)
3291 win_T *oldwin;
3293 curwin = win_alloc(NULL, FALSE);
3294 if (oldwin == NULL)
3296 /* Very first window, need to create an empty buffer for it and
3297 * initialize from scratch. */
3298 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3299 if (curwin == NULL || curbuf == NULL)
3300 return FAIL;
3301 curwin->w_buffer = curbuf;
3302 curbuf->b_nwindows = 1; /* there is one window */
3303 #ifdef FEAT_WINDOWS
3304 curwin->w_alist = &global_alist;
3305 #endif
3306 curwin_init(); /* init current window */
3308 #ifdef FEAT_WINDOWS
3309 else
3311 /* First window in new tab page, initialize it from "oldwin". */
3312 win_init(curwin, oldwin, 0);
3314 # ifdef FEAT_SCROLLBIND
3315 /* We don't want scroll-binding in the first window. */
3316 curwin->w_p_scb = FALSE;
3317 # endif
3319 #endif
3321 new_frame(curwin);
3322 if (curwin->w_frame == NULL)
3323 return FAIL;
3324 topframe = curwin->w_frame;
3325 #ifdef FEAT_VERTSPLIT
3326 topframe->fr_width = Columns;
3327 #endif
3328 topframe->fr_height = Rows - p_ch;
3329 topframe->fr_win = curwin;
3331 return OK;
3335 * Create a frame for window "wp".
3337 static void
3338 new_frame(win_T *wp)
3340 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3342 wp->w_frame = frp;
3343 if (frp != NULL)
3345 frp->fr_layout = FR_LEAF;
3346 frp->fr_win = wp;
3351 * Initialize the window and frame size to the maximum.
3353 void
3354 win_init_size()
3356 firstwin->w_height = ROWS_AVAIL;
3357 topframe->fr_height = ROWS_AVAIL;
3358 #ifdef FEAT_VERTSPLIT
3359 firstwin->w_width = Columns;
3360 topframe->fr_width = Columns;
3361 #endif
3364 #if defined(FEAT_WINDOWS) || defined(PROTO)
3367 * Allocate a new tabpage_T and init the values.
3368 * Returns NULL when out of memory.
3370 static tabpage_T *
3371 alloc_tabpage()
3373 tabpage_T *tp;
3375 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3376 if (tp != NULL)
3378 # ifdef FEAT_GUI
3379 int i;
3381 for (i = 0; i < 3; i++)
3382 tp->tp_prev_which_scrollbars[i] = -1;
3383 # endif
3384 # ifdef FEAT_DIFF
3385 tp->tp_diff_invalid = TRUE;
3386 # endif
3387 #ifdef FEAT_EVAL
3388 /* init t: variables */
3389 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3390 #endif
3391 tp->tp_ch_used = p_ch;
3393 return tp;
3396 void
3397 free_tabpage(tp)
3398 tabpage_T *tp;
3400 int idx;
3402 # ifdef FEAT_DIFF
3403 diff_clear(tp);
3404 # endif
3405 for (idx = 0; idx < SNAP_COUNT; ++idx)
3406 clear_snapshot(tp, idx);
3407 #ifdef FEAT_EVAL
3408 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3409 #endif
3410 vim_free(tp);
3414 * Create a new Tab page with one window.
3415 * It will edit the current buffer, like after ":split".
3416 * When "after" is 0 put it just after the current Tab page.
3417 * Otherwise put it just before tab page "after".
3418 * Return FAIL or OK.
3421 win_new_tabpage(after)
3422 int after;
3424 tabpage_T *tp = curtab;
3425 tabpage_T *newtp;
3426 int n;
3428 newtp = alloc_tabpage();
3429 if (newtp == NULL)
3430 return FAIL;
3432 /* Remember the current windows in this Tab page. */
3433 if (leave_tabpage(curbuf) == FAIL)
3435 vim_free(newtp);
3436 return FAIL;
3438 curtab = newtp;
3440 /* Create a new empty window. */
3441 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3443 /* Make the new Tab page the new topframe. */
3444 if (after == 1)
3446 /* New tab page becomes the first one. */
3447 newtp->tp_next = first_tabpage;
3448 first_tabpage = newtp;
3450 else
3452 if (after > 0)
3454 /* Put new tab page before tab page "after". */
3455 n = 2;
3456 for (tp = first_tabpage; tp->tp_next != NULL
3457 && n < after; tp = tp->tp_next)
3458 ++n;
3460 newtp->tp_next = tp->tp_next;
3461 tp->tp_next = newtp;
3463 win_init_size();
3464 firstwin->w_winrow = tabline_height();
3465 win_comp_scroll(curwin);
3467 newtp->tp_topframe = topframe;
3468 last_status(FALSE);
3470 #if defined(FEAT_GUI)
3471 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3472 * scrollbars. Have to update them anyway. */
3473 gui_may_update_scrollbars();
3474 #endif
3476 redraw_all_later(CLEAR);
3477 #ifdef FEAT_AUTOCMD
3478 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3479 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3480 #endif
3481 return OK;
3484 /* Failed, get back the previous Tab page */
3485 enter_tabpage(curtab, curbuf);
3486 return FAIL;
3490 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3491 * like with ":split".
3492 * Returns OK if a new tab page was created, FAIL otherwise.
3495 may_open_tabpage()
3497 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3499 if (n != 0)
3501 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3502 postponed_split_tab = 0;
3503 return win_new_tabpage(n);
3505 return FAIL;
3509 * Create up to "maxcount" tabpages with empty windows.
3510 * Returns the number of resulting tab pages.
3513 make_tabpages(maxcount)
3514 int maxcount;
3516 int count = maxcount;
3517 int todo;
3519 /* Limit to 'tabpagemax' tabs. */
3520 if (count > p_tpm)
3521 count = p_tpm;
3523 #ifdef FEAT_AUTOCMD
3525 * Don't execute autocommands while creating the tab pages. Must do that
3526 * when putting the buffers in the windows.
3528 block_autocmds();
3529 #endif
3531 for (todo = count - 1; todo > 0; --todo)
3532 if (win_new_tabpage(0) == FAIL)
3533 break;
3535 #ifdef FEAT_AUTOCMD
3536 unblock_autocmds();
3537 #endif
3539 /* return actual number of tab pages */
3540 return (count - todo);
3544 * Return TRUE when "tpc" points to a valid tab page.
3547 valid_tabpage(tpc)
3548 tabpage_T *tpc;
3550 tabpage_T *tp;
3552 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3553 if (tp == tpc)
3554 return TRUE;
3555 return FALSE;
3559 * Find tab page "n" (first one is 1). Returns NULL when not found.
3561 tabpage_T *
3562 find_tabpage(n)
3563 int n;
3565 tabpage_T *tp;
3566 int i = 1;
3568 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3569 ++i;
3570 return tp;
3574 * Get index of tab page "tp". First one has index 1.
3575 * When not found returns number of tab pages plus one.
3578 tabpage_index(ftp)
3579 tabpage_T *ftp;
3581 int i = 1;
3582 tabpage_T *tp;
3584 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3585 ++i;
3586 return i;
3590 * Prepare for leaving the current tab page.
3591 * When autocomands change "curtab" we don't leave the tab page and return
3592 * FAIL.
3593 * Careful: When OK is returned need to get a new tab page very very soon!
3595 static int
3596 leave_tabpage(new_curbuf)
3597 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
3598 NULL if unknown */
3600 tabpage_T *tp = curtab;
3602 #ifdef FEAT_VISUAL
3603 reset_VIsual_and_resel(); /* stop Visual mode */
3604 #endif
3605 #ifdef FEAT_AUTOCMD
3606 if (new_curbuf != curbuf)
3608 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3609 if (curtab != tp)
3610 return FAIL;
3612 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3613 if (curtab != tp)
3614 return FAIL;
3615 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3616 if (curtab != tp)
3617 return FAIL;
3618 #endif
3619 #if defined(FEAT_GUI)
3620 /* Remove the scrollbars. They may be added back later. */
3621 if (gui.in_use)
3622 gui_remove_scrollbars();
3623 #endif
3624 tp->tp_curwin = curwin;
3625 tp->tp_prevwin = prevwin;
3626 tp->tp_firstwin = firstwin;
3627 tp->tp_lastwin = lastwin;
3628 tp->tp_old_Rows = Rows;
3629 tp->tp_old_Columns = Columns;
3630 firstwin = NULL;
3631 lastwin = NULL;
3632 return OK;
3636 * Start using tab page "tp".
3637 * Only to be used after leave_tabpage() or freeing the current tab page.
3639 static void
3640 enter_tabpage(tp, old_curbuf)
3641 tabpage_T *tp;
3642 buf_T *old_curbuf UNUSED;
3644 int old_off = tp->tp_firstwin->w_winrow;
3645 win_T *next_prevwin = tp->tp_prevwin;
3647 curtab = tp;
3648 firstwin = tp->tp_firstwin;
3649 lastwin = tp->tp_lastwin;
3650 topframe = tp->tp_topframe;
3652 /* We would like doing the TabEnter event first, but we don't have a
3653 * valid current window yet, which may break some commands.
3654 * This triggers autocommands, thus may make "tp" invalid. */
3655 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3656 prevwin = next_prevwin;
3658 #ifdef FEAT_AUTOCMD
3659 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3661 if (old_curbuf != curbuf)
3662 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3663 #endif
3665 last_status(FALSE); /* status line may appear or disappear */
3666 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3667 must_redraw = CLEAR; /* need to redraw everything */
3668 #ifdef FEAT_DIFF
3669 diff_need_scrollbind = TRUE;
3670 #endif
3672 /* The tabpage line may have appeared or disappeared, may need to resize
3673 * the frames for that. When the Vim window was resized need to update
3674 * frame sizes too. Use the stored value of p_ch, so that it can be
3675 * different for each tab page. */
3676 p_ch = curtab->tp_ch_used;
3677 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3678 #ifdef FEAT_GUI_TABLINE
3679 && !gui_use_tabline()
3680 #endif
3682 shell_new_rows();
3683 #ifdef FEAT_VERTSPLIT
3684 if (curtab->tp_old_Columns != Columns && starting == 0)
3685 shell_new_columns(); /* update window widths */
3686 #endif
3688 #if defined(FEAT_GUI)
3689 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3690 * scrollbars. Have to update them anyway. */
3691 gui_may_update_scrollbars();
3692 #endif
3694 redraw_all_later(CLEAR);
3698 * Go to tab page "n". For ":tab N" and "Ngt".
3699 * When "n" is 9999 go to the last tab page.
3701 void
3702 goto_tabpage(n)
3703 int n;
3705 tabpage_T *tp;
3706 tabpage_T *ttp;
3707 int i;
3709 if (text_locked())
3711 /* Not allowed when editing the command line. */
3712 #ifdef FEAT_CMDWIN
3713 if (cmdwin_type != 0)
3714 EMSG(_(e_cmdwin));
3715 else
3716 #endif
3717 EMSG(_(e_secure));
3718 return;
3721 /* If there is only one it can't work. */
3722 if (first_tabpage->tp_next == NULL)
3724 if (n > 1)
3725 beep_flush();
3726 return;
3729 if (n == 0)
3731 /* No count, go to next tab page, wrap around end. */
3732 if (curtab->tp_next == NULL)
3733 tp = first_tabpage;
3734 else
3735 tp = curtab->tp_next;
3737 else if (n < 0)
3739 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3740 * this N times. */
3741 ttp = curtab;
3742 for (i = n; i < 0; ++i)
3744 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3745 tp = tp->tp_next)
3747 ttp = tp;
3750 else if (n == 9999)
3752 /* Go to last tab page. */
3753 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3756 else
3758 /* Go to tab page "n". */
3759 tp = find_tabpage(n);
3760 if (tp == NULL)
3762 beep_flush();
3763 return;
3767 goto_tabpage_tp(tp);
3769 #ifdef FEAT_GUI_TABLINE
3770 if (gui_use_tabline())
3771 gui_mch_set_curtab(tabpage_index(curtab));
3772 #endif
3776 * Go to tabpage "tp".
3777 * Note: doesn't update the GUI tab.
3779 void
3780 goto_tabpage_tp(tp)
3781 tabpage_T *tp;
3783 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3785 if (valid_tabpage(tp))
3786 enter_tabpage(tp, curbuf);
3787 else
3788 enter_tabpage(curtab, curbuf);
3793 * Enter window "wp" in tab page "tp".
3794 * Also updates the GUI tab.
3796 void
3797 goto_tabpage_win(tp, wp)
3798 tabpage_T *tp;
3799 win_T *wp;
3801 goto_tabpage_tp(tp);
3802 if (curtab == tp && win_valid(wp))
3804 win_enter(wp, TRUE);
3805 # ifdef FEAT_GUI_TABLINE
3806 if (gui_use_tabline())
3807 gui_mch_set_curtab(tabpage_index(curtab));
3808 # endif
3813 * Move the current tab page to before tab page "nr".
3815 void
3816 tabpage_move(nr)
3817 int nr;
3819 int n = nr;
3820 tabpage_T *tp;
3822 if (first_tabpage->tp_next == NULL)
3823 return;
3825 /* Remove the current tab page from the list of tab pages. */
3826 if (curtab == first_tabpage)
3827 first_tabpage = curtab->tp_next;
3828 else
3830 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3831 if (tp->tp_next == curtab)
3832 break;
3833 if (tp == NULL) /* "cannot happen" */
3834 return;
3835 tp->tp_next = curtab->tp_next;
3838 /* Re-insert it at the specified position. */
3839 if (n == 0)
3841 curtab->tp_next = first_tabpage;
3842 first_tabpage = curtab;
3844 else
3846 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3847 --n;
3848 curtab->tp_next = tp->tp_next;
3849 tp->tp_next = curtab;
3852 /* Need to redraw the tabline. Tab page contents doesn't change. */
3853 redraw_tabline = TRUE;
3858 * Go to another window.
3859 * When jumping to another buffer, stop Visual mode. Do this before
3860 * changing windows so we can yank the selection into the '*' register.
3861 * When jumping to another window on the same buffer, adjust its cursor
3862 * position to keep the same Visual area.
3864 void
3865 win_goto(wp)
3866 win_T *wp;
3868 if (text_locked())
3870 beep_flush();
3871 text_locked_msg();
3872 return;
3874 #ifdef FEAT_AUTOCMD
3875 if (curbuf_locked())
3876 return;
3877 #endif
3879 #ifdef FEAT_VISUAL
3880 if (wp->w_buffer != curbuf)
3881 reset_VIsual_and_resel();
3882 else if (VIsual_active)
3883 wp->w_cursor = curwin->w_cursor;
3884 #endif
3886 #ifdef FEAT_GUI
3887 need_mouse_correct = TRUE;
3888 #endif
3889 win_enter(wp, TRUE);
3892 #if defined(FEAT_PERL) || defined(PROTO)
3894 * Find window number "winnr" (counting top to bottom).
3896 win_T *
3897 win_find_nr(winnr)
3898 int winnr;
3900 win_T *wp;
3902 # ifdef FEAT_WINDOWS
3903 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3904 if (--winnr == 0)
3905 break;
3906 return wp;
3907 # else
3908 return curwin;
3909 # endif
3911 #endif
3913 #ifdef FEAT_VERTSPLIT
3915 * Move to window above or below "count" times.
3917 static void
3918 win_goto_ver(up, count)
3919 int up; /* TRUE to go to win above */
3920 long count;
3922 frame_T *fr;
3923 frame_T *nfr;
3924 frame_T *foundfr;
3926 foundfr = curwin->w_frame;
3927 while (count--)
3930 * First go upwards in the tree of frames until we find a upwards or
3931 * downwards neighbor.
3933 fr = foundfr;
3934 for (;;)
3936 if (fr == topframe)
3937 goto end;
3938 if (up)
3939 nfr = fr->fr_prev;
3940 else
3941 nfr = fr->fr_next;
3942 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3943 break;
3944 fr = fr->fr_parent;
3948 * Now go downwards to find the bottom or top frame in it.
3950 for (;;)
3952 if (nfr->fr_layout == FR_LEAF)
3954 foundfr = nfr;
3955 break;
3957 fr = nfr->fr_child;
3958 if (nfr->fr_layout == FR_ROW)
3960 /* Find the frame at the cursor row. */
3961 while (fr->fr_next != NULL
3962 && frame2win(fr)->w_wincol + fr->fr_width
3963 <= curwin->w_wincol + curwin->w_wcol)
3964 fr = fr->fr_next;
3966 if (nfr->fr_layout == FR_COL && up)
3967 while (fr->fr_next != NULL)
3968 fr = fr->fr_next;
3969 nfr = fr;
3972 end:
3973 if (foundfr != NULL)
3974 win_goto(foundfr->fr_win);
3978 * Move to left or right window.
3980 static void
3981 win_goto_hor(left, count)
3982 int left; /* TRUE to go to left win */
3983 long count;
3985 frame_T *fr;
3986 frame_T *nfr;
3987 frame_T *foundfr;
3989 foundfr = curwin->w_frame;
3990 while (count--)
3993 * First go upwards in the tree of frames until we find a left or
3994 * right neighbor.
3996 fr = foundfr;
3997 for (;;)
3999 if (fr == topframe)
4000 goto end;
4001 if (left)
4002 nfr = fr->fr_prev;
4003 else
4004 nfr = fr->fr_next;
4005 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4006 break;
4007 fr = fr->fr_parent;
4011 * Now go downwards to find the leftmost or rightmost frame in it.
4013 for (;;)
4015 if (nfr->fr_layout == FR_LEAF)
4017 foundfr = nfr;
4018 break;
4020 fr = nfr->fr_child;
4021 if (nfr->fr_layout == FR_COL)
4023 /* Find the frame at the cursor row. */
4024 while (fr->fr_next != NULL
4025 && frame2win(fr)->w_winrow + fr->fr_height
4026 <= curwin->w_winrow + curwin->w_wrow)
4027 fr = fr->fr_next;
4029 if (nfr->fr_layout == FR_ROW && left)
4030 while (fr->fr_next != NULL)
4031 fr = fr->fr_next;
4032 nfr = fr;
4035 end:
4036 if (foundfr != NULL)
4037 win_goto(foundfr->fr_win);
4039 #endif
4042 * Make window "wp" the current window.
4044 void
4045 win_enter(wp, undo_sync)
4046 win_T *wp;
4047 int undo_sync;
4049 win_enter_ext(wp, undo_sync, FALSE);
4053 * Make window wp the current window.
4054 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4055 * been closed and isn't valid.
4057 static void
4058 win_enter_ext(wp, undo_sync, curwin_invalid)
4059 win_T *wp;
4060 int undo_sync;
4061 int curwin_invalid;
4063 #ifdef FEAT_AUTOCMD
4064 int other_buffer = FALSE;
4065 #endif
4067 if (wp == curwin && !curwin_invalid) /* nothing to do */
4068 return;
4070 #ifdef FEAT_AUTOCMD
4071 if (!curwin_invalid)
4074 * Be careful: If autocommands delete the window, return now.
4076 if (wp->w_buffer != curbuf)
4078 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4079 other_buffer = TRUE;
4080 if (!win_valid(wp))
4081 return;
4083 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4084 if (!win_valid(wp))
4085 return;
4086 # ifdef FEAT_EVAL
4087 /* autocmds may abort script processing */
4088 if (aborting())
4089 return;
4090 # endif
4092 #endif
4094 /* sync undo before leaving the current buffer */
4095 if (undo_sync && curbuf != wp->w_buffer)
4096 u_sync(FALSE);
4097 /* may have to copy the buffer options when 'cpo' contains 'S' */
4098 if (wp->w_buffer != curbuf)
4099 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4100 if (!curwin_invalid)
4102 prevwin = curwin; /* remember for CTRL-W p */
4103 curwin->w_redr_status = TRUE;
4105 curwin = wp;
4106 curbuf = wp->w_buffer;
4107 check_cursor();
4108 #ifdef FEAT_VIRTUALEDIT
4109 if (!virtual_active())
4110 curwin->w_cursor.coladd = 0;
4111 #endif
4112 changed_line_abv_curs(); /* assume cursor position needs updating */
4114 if (curwin->w_localdir != NULL)
4116 /* Window has a local directory: Save current directory as global
4117 * directory (unless that was done already) and change to the local
4118 * directory. */
4119 if (globaldir == NULL)
4121 char_u cwd[MAXPATHL];
4123 if (mch_dirname(cwd, MAXPATHL) == OK)
4124 globaldir = vim_strsave(cwd);
4126 if (mch_chdir((char *)curwin->w_localdir) == 0)
4127 shorten_fnames(TRUE);
4129 else if (globaldir != NULL)
4131 /* Window doesn't have a local directory and we are not in the global
4132 * directory: Change to the global directory. */
4133 ignored = mch_chdir((char *)globaldir);
4134 vim_free(globaldir);
4135 globaldir = NULL;
4136 shorten_fnames(TRUE);
4139 #ifdef FEAT_AUTOCMD
4140 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4141 if (other_buffer)
4142 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4143 #endif
4145 #ifdef FEAT_TITLE
4146 maketitle();
4147 #endif
4148 curwin->w_redr_status = TRUE;
4149 redraw_tabline = TRUE;
4150 if (restart_edit)
4151 redraw_later(VALID); /* causes status line redraw */
4153 /* set window height to desired minimal value */
4154 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4155 win_setheight((int)p_wh);
4156 else if (curwin->w_height == 0)
4157 win_setheight(1);
4159 #ifdef FEAT_VERTSPLIT
4160 /* set window width to desired minimal value */
4161 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4162 win_setwidth((int)p_wiw);
4163 #endif
4165 #ifdef FEAT_MOUSE
4166 setmouse(); /* in case jumped to/from help buffer */
4167 #endif
4169 /* Change directories when the 'acd' option is set. */
4170 DO_AUTOCHDIR
4173 #endif /* FEAT_WINDOWS */
4175 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4177 * Jump to the first open window that contains buffer "buf", if one exists.
4178 * Returns a pointer to the window found, otherwise NULL.
4180 win_T *
4181 buf_jump_open_win(buf)
4182 buf_T *buf;
4184 # ifdef FEAT_WINDOWS
4185 win_T *wp;
4187 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4188 if (wp->w_buffer == buf)
4189 break;
4190 if (wp != NULL)
4191 win_enter(wp, FALSE);
4192 return wp;
4193 # else
4194 if (curwin->w_buffer == buf)
4195 return curwin;
4196 return NULL;
4197 # endif
4201 * Jump to the first open window in any tab page that contains buffer "buf",
4202 * if one exists.
4203 * Returns a pointer to the window found, otherwise NULL.
4205 win_T *
4206 buf_jump_open_tab(buf)
4207 buf_T *buf;
4209 # ifdef FEAT_WINDOWS
4210 win_T *wp;
4211 tabpage_T *tp;
4213 /* First try the current tab page. */
4214 wp = buf_jump_open_win(buf);
4215 if (wp != NULL)
4216 return wp;
4218 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4219 if (tp != curtab)
4221 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4222 if (wp->w_buffer == buf)
4223 break;
4224 if (wp != NULL)
4226 goto_tabpage_win(tp, wp);
4227 if (curwin != wp)
4228 wp = NULL; /* something went wrong */
4229 break;
4233 return wp;
4234 # else
4235 if (curwin->w_buffer == buf)
4236 return curwin;
4237 return NULL;
4238 # endif
4240 #endif
4243 * Allocate a window structure and link it in the window list when "hidden" is
4244 * FALSE.
4246 static win_T *
4247 win_alloc(after, hidden)
4248 win_T *after UNUSED;
4249 int hidden UNUSED;
4251 win_T *newwin;
4254 * allocate window structure and linesizes arrays
4256 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4257 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4259 vim_free(newwin);
4260 newwin = NULL;
4263 if (newwin != NULL)
4265 #ifdef FEAT_AUTOCMD
4266 /* Don't execute autocommands while the window is not properly
4267 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4268 * event. */
4269 block_autocmds();
4270 #endif
4272 * link the window in the window list
4274 #ifdef FEAT_WINDOWS
4275 if (!hidden)
4276 win_append(after, newwin);
4277 #endif
4278 #ifdef FEAT_VERTSPLIT
4279 newwin->w_wincol = 0;
4280 newwin->w_width = Columns;
4281 #endif
4283 /* position the display and the cursor at the top of the file. */
4284 newwin->w_topline = 1;
4285 #ifdef FEAT_DIFF
4286 newwin->w_topfill = 0;
4287 #endif
4288 newwin->w_botline = 2;
4289 newwin->w_cursor.lnum = 1;
4290 #ifdef FEAT_SCROLLBIND
4291 newwin->w_scbind_pos = 1;
4292 #endif
4294 /* We won't calculate w_fraction until resizing the window */
4295 newwin->w_fraction = 0;
4296 newwin->w_prev_fraction_row = -1;
4298 #ifdef FEAT_GUI
4299 if (gui.in_use)
4301 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4302 SBAR_LEFT, newwin);
4303 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4304 SBAR_RIGHT, newwin);
4306 #endif
4307 #ifdef FEAT_EVAL
4308 /* init w: variables */
4309 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4310 #endif
4311 #ifdef FEAT_FOLDING
4312 foldInitWin(newwin);
4313 #endif
4314 #ifdef FEAT_AUTOCMD
4315 unblock_autocmds();
4316 #endif
4317 #ifdef FEAT_SEARCH_EXTRA
4318 newwin->w_match_head = NULL;
4319 newwin->w_next_match_id = 4;
4320 #endif
4322 return newwin;
4325 #if defined(FEAT_WINDOWS) || defined(PROTO)
4328 * remove window 'wp' from the window list and free the structure
4330 static void
4331 win_free(wp, tp)
4332 win_T *wp;
4333 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4335 int i;
4337 #ifdef FEAT_FOLDING
4338 clearFolding(wp);
4339 #endif
4341 /* reduce the reference count to the argument list. */
4342 alist_unlink(wp->w_alist);
4344 #ifdef FEAT_AUTOCMD
4345 /* Don't execute autocommands while the window is halfway being deleted.
4346 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4347 block_autocmds();
4348 #endif
4350 #ifdef FEAT_MZSCHEME
4351 mzscheme_window_free(wp);
4352 #endif
4354 #ifdef FEAT_PERL
4355 perl_win_free(wp);
4356 #endif
4358 #ifdef FEAT_PYTHON
4359 python_window_free(wp);
4360 #endif
4362 #ifdef FEAT_TCL
4363 tcl_window_free(wp);
4364 #endif
4366 #ifdef FEAT_RUBY
4367 ruby_window_free(wp);
4368 #endif
4370 clear_winopt(&wp->w_onebuf_opt);
4371 clear_winopt(&wp->w_allbuf_opt);
4373 #ifdef FEAT_EVAL
4374 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4375 #endif
4377 if (prevwin == wp)
4378 prevwin = NULL;
4379 win_free_lsize(wp);
4381 for (i = 0; i < wp->w_tagstacklen; ++i)
4382 vim_free(wp->w_tagstack[i].tagname);
4384 vim_free(wp->w_localdir);
4386 #ifdef FEAT_SEARCH_EXTRA
4387 clear_matches(wp);
4388 #endif
4390 #ifdef FEAT_JUMPLIST
4391 free_jumplist(wp);
4392 #endif
4394 #ifdef FEAT_QUICKFIX
4395 qf_free_all(wp);
4396 #endif
4398 #ifdef FEAT_GUI
4399 if (gui.in_use)
4401 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4402 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4404 #endif /* FEAT_GUI */
4406 #ifdef FEAT_AUTOCMD
4407 if (wp != aucmd_win)
4408 #endif
4409 win_remove(wp, tp);
4410 vim_free(wp);
4412 #ifdef FEAT_AUTOCMD
4413 unblock_autocmds();
4414 #endif
4418 * Append window "wp" in the window list after window "after".
4420 void
4421 win_append(after, wp)
4422 win_T *after, *wp;
4424 win_T *before;
4426 if (after == NULL) /* after NULL is in front of the first */
4427 before = firstwin;
4428 else
4429 before = after->w_next;
4431 wp->w_next = before;
4432 wp->w_prev = after;
4433 if (after == NULL)
4434 firstwin = wp;
4435 else
4436 after->w_next = wp;
4437 if (before == NULL)
4438 lastwin = wp;
4439 else
4440 before->w_prev = wp;
4444 * Remove a window from the window list.
4446 void
4447 win_remove(wp, tp)
4448 win_T *wp;
4449 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4451 if (wp->w_prev != NULL)
4452 wp->w_prev->w_next = wp->w_next;
4453 else if (tp == NULL)
4454 firstwin = wp->w_next;
4455 else
4456 tp->tp_firstwin = wp->w_next;
4457 if (wp->w_next != NULL)
4458 wp->w_next->w_prev = wp->w_prev;
4459 else if (tp == NULL)
4460 lastwin = wp->w_prev;
4461 else
4462 tp->tp_lastwin = wp->w_prev;
4466 * Append frame "frp" in a frame list after frame "after".
4468 static void
4469 frame_append(after, frp)
4470 frame_T *after, *frp;
4472 frp->fr_next = after->fr_next;
4473 after->fr_next = frp;
4474 if (frp->fr_next != NULL)
4475 frp->fr_next->fr_prev = frp;
4476 frp->fr_prev = after;
4480 * Insert frame "frp" in a frame list before frame "before".
4482 static void
4483 frame_insert(before, frp)
4484 frame_T *before, *frp;
4486 frp->fr_next = before;
4487 frp->fr_prev = before->fr_prev;
4488 before->fr_prev = frp;
4489 if (frp->fr_prev != NULL)
4490 frp->fr_prev->fr_next = frp;
4491 else
4492 frp->fr_parent->fr_child = frp;
4496 * Remove a frame from a frame list.
4498 static void
4499 frame_remove(frp)
4500 frame_T *frp;
4502 if (frp->fr_prev != NULL)
4503 frp->fr_prev->fr_next = frp->fr_next;
4504 else
4505 frp->fr_parent->fr_child = frp->fr_next;
4506 if (frp->fr_next != NULL)
4507 frp->fr_next->fr_prev = frp->fr_prev;
4510 #endif /* FEAT_WINDOWS */
4513 * Allocate w_lines[] for window "wp".
4514 * Return FAIL for failure, OK for success.
4517 win_alloc_lines(wp)
4518 win_T *wp;
4520 wp->w_lines_valid = 0;
4521 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4522 if (wp->w_lines == NULL)
4523 return FAIL;
4524 return OK;
4528 * free lsize arrays for a window
4530 void
4531 win_free_lsize(wp)
4532 win_T *wp;
4534 vim_free(wp->w_lines);
4535 wp->w_lines = NULL;
4539 * Called from win_new_shellsize() after Rows changed.
4540 * This only does the current tab page, others must be done when made active.
4542 void
4543 shell_new_rows()
4545 int h = (int)ROWS_AVAIL;
4547 if (firstwin == NULL) /* not initialized yet */
4548 return;
4549 #ifdef FEAT_WINDOWS
4550 if (h < frame_minheight(topframe, NULL))
4551 h = frame_minheight(topframe, NULL);
4553 /* First try setting the heights of windows with 'winfixheight'. If
4554 * that doesn't result in the right height, forget about that option. */
4555 frame_new_height(topframe, h, FALSE, TRUE);
4556 if (topframe->fr_height != h)
4557 frame_new_height(topframe, h, FALSE, FALSE);
4559 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4560 #else
4561 if (h < 1)
4562 h = 1;
4563 win_new_height(firstwin, h);
4564 #endif
4565 compute_cmdrow();
4566 #ifdef FEAT_WINDOWS
4567 curtab->tp_ch_used = p_ch;
4568 #endif
4570 #if 0
4571 /* Disabled: don't want making the screen smaller make a window larger. */
4572 if (p_ea)
4573 win_equal(curwin, FALSE, 'v');
4574 #endif
4577 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4579 * Called from win_new_shellsize() after Columns changed.
4581 void
4582 shell_new_columns()
4584 if (firstwin == NULL) /* not initialized yet */
4585 return;
4587 /* First try setting the widths of windows with 'winfixwidth'. If that
4588 * doesn't result in the right width, forget about that option. */
4589 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4590 if (topframe->fr_width != Columns)
4591 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4593 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4594 #if 0
4595 /* Disabled: don't want making the screen smaller make a window larger. */
4596 if (p_ea)
4597 win_equal(curwin, FALSE, 'h');
4598 #endif
4600 #endif
4602 #if defined(FEAT_CMDWIN) || defined(PROTO)
4604 * Save the size of all windows in "gap".
4606 void
4607 win_size_save(gap)
4608 garray_T *gap;
4611 win_T *wp;
4613 ga_init2(gap, (int)sizeof(int), 1);
4614 if (ga_grow(gap, win_count() * 2) == OK)
4615 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4617 ((int *)gap->ga_data)[gap->ga_len++] =
4618 wp->w_width + wp->w_vsep_width;
4619 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4624 * Restore window sizes, but only if the number of windows is still the same.
4625 * Does not free the growarray.
4627 void
4628 win_size_restore(gap)
4629 garray_T *gap;
4631 win_T *wp;
4632 int i;
4634 if (win_count() * 2 == gap->ga_len)
4636 i = 0;
4637 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4639 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4640 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4642 /* recompute the window positions */
4643 (void)win_comp_pos();
4646 #endif /* FEAT_CMDWIN */
4648 #if defined(FEAT_WINDOWS) || defined(PROTO)
4650 * Update the position for all windows, using the width and height of the
4651 * frames.
4652 * Returns the row just after the last window.
4655 win_comp_pos()
4657 int row = tabline_height();
4658 int col = 0;
4660 frame_comp_pos(topframe, &row, &col);
4661 return row;
4665 * Update the position of the windows in frame "topfrp", using the width and
4666 * height of the frames.
4667 * "*row" and "*col" are the top-left position of the frame. They are updated
4668 * to the bottom-right position plus one.
4670 static void
4671 frame_comp_pos(topfrp, row, col)
4672 frame_T *topfrp;
4673 int *row;
4674 int *col;
4676 win_T *wp;
4677 frame_T *frp;
4678 #ifdef FEAT_VERTSPLIT
4679 int startcol;
4680 int startrow;
4681 #endif
4683 wp = topfrp->fr_win;
4684 if (wp != NULL)
4686 if (wp->w_winrow != *row
4687 #ifdef FEAT_VERTSPLIT
4688 || wp->w_wincol != *col
4689 #endif
4692 /* position changed, redraw */
4693 wp->w_winrow = *row;
4694 #ifdef FEAT_VERTSPLIT
4695 wp->w_wincol = *col;
4696 #endif
4697 redraw_win_later(wp, NOT_VALID);
4698 wp->w_redr_status = TRUE;
4700 *row += wp->w_height + wp->w_status_height;
4701 #ifdef FEAT_VERTSPLIT
4702 *col += wp->w_width + wp->w_vsep_width;
4703 #endif
4705 else
4707 #ifdef FEAT_VERTSPLIT
4708 startrow = *row;
4709 startcol = *col;
4710 #endif
4711 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4713 #ifdef FEAT_VERTSPLIT
4714 if (topfrp->fr_layout == FR_ROW)
4715 *row = startrow; /* all frames are at the same row */
4716 else
4717 *col = startcol; /* all frames are at the same col */
4718 #endif
4719 frame_comp_pos(frp, row, col);
4724 #endif /* FEAT_WINDOWS */
4727 * Set current window height and take care of repositioning other windows to
4728 * fit around it.
4730 void
4731 win_setheight(height)
4732 int height;
4734 win_setheight_win(height, curwin);
4738 * Set the window height of window "win" and take care of repositioning other
4739 * windows to fit around it.
4741 void
4742 win_setheight_win(height, win)
4743 int height;
4744 win_T *win;
4746 int row;
4748 if (win == curwin)
4750 /* Always keep current window at least one line high, even when
4751 * 'winminheight' is zero. */
4752 #ifdef FEAT_WINDOWS
4753 if (height < p_wmh)
4754 height = p_wmh;
4755 #endif
4756 if (height == 0)
4757 height = 1;
4760 #ifdef FEAT_WINDOWS
4761 frame_setheight(win->w_frame, height + win->w_status_height);
4763 /* recompute the window positions */
4764 row = win_comp_pos();
4765 #else
4766 if (height > topframe->fr_height)
4767 height = topframe->fr_height;
4768 win->w_height = height;
4769 row = height;
4770 #endif
4773 * If there is extra space created between the last window and the command
4774 * line, clear it.
4776 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4777 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4778 cmdline_row = row;
4779 msg_row = row;
4780 msg_col = 0;
4782 redraw_all_later(NOT_VALID);
4785 #if defined(FEAT_WINDOWS) || defined(PROTO)
4788 * Set the height of a frame to "height" and take care that all frames and
4789 * windows inside it are resized. Also resize frames on the left and right if
4790 * the are in the same FR_ROW frame.
4792 * Strategy:
4793 * If the frame is part of a FR_COL frame, try fitting the frame in that
4794 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4795 * go to containing frames to resize them and make room.
4796 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4797 * Check for the minimal height of the FR_ROW frame.
4798 * At the top level we can also use change the command line height.
4800 static void
4801 frame_setheight(curfrp, height)
4802 frame_T *curfrp;
4803 int height;
4805 int room; /* total number of lines available */
4806 int take; /* number of lines taken from other windows */
4807 int room_cmdline; /* lines available from cmdline */
4808 int run;
4809 frame_T *frp;
4810 int h;
4811 int room_reserved;
4813 /* If the height already is the desired value, nothing to do. */
4814 if (curfrp->fr_height == height)
4815 return;
4817 if (curfrp->fr_parent == NULL)
4819 /* topframe: can only change the command line */
4820 if (height > ROWS_AVAIL)
4821 height = ROWS_AVAIL;
4822 if (height > 0)
4823 frame_new_height(curfrp, height, FALSE, FALSE);
4825 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4827 /* Row of frames: Also need to resize frames left and right of this
4828 * one. First check for the minimal height of these. */
4829 h = frame_minheight(curfrp->fr_parent, NULL);
4830 if (height < h)
4831 height = h;
4832 frame_setheight(curfrp->fr_parent, height);
4834 else
4837 * Column of frames: try to change only frames in this column.
4839 #ifdef FEAT_VERTSPLIT
4841 * Do this twice:
4842 * 1: compute room available, if it's not enough try resizing the
4843 * containing frame.
4844 * 2: compute the room available and adjust the height to it.
4845 * Try not to reduce the height of a window with 'winfixheight' set.
4847 for (run = 1; run <= 2; ++run)
4848 #else
4849 for (;;)
4850 #endif
4852 room = 0;
4853 room_reserved = 0;
4854 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4855 frp = frp->fr_next)
4857 if (frp != curfrp
4858 && frp->fr_win != NULL
4859 && frp->fr_win->w_p_wfh)
4860 room_reserved += frp->fr_height;
4861 room += frp->fr_height;
4862 if (frp != curfrp)
4863 room -= frame_minheight(frp, NULL);
4865 #ifdef FEAT_VERTSPLIT
4866 if (curfrp->fr_width != Columns)
4867 room_cmdline = 0;
4868 else
4869 #endif
4871 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4872 + lastwin->w_height + lastwin->w_status_height);
4873 if (room_cmdline < 0)
4874 room_cmdline = 0;
4877 if (height <= room + room_cmdline)
4878 break;
4879 #ifdef FEAT_VERTSPLIT
4880 if (run == 2 || curfrp->fr_width == Columns)
4881 #endif
4883 if (height > room + room_cmdline)
4884 height = room + room_cmdline;
4885 break;
4887 #ifdef FEAT_VERTSPLIT
4888 frame_setheight(curfrp->fr_parent, height
4889 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4890 #endif
4891 /*NOTREACHED*/
4895 * Compute the number of lines we will take from others frames (can be
4896 * negative!).
4898 take = height - curfrp->fr_height;
4900 /* If there is not enough room, also reduce the height of a window
4901 * with 'winfixheight' set. */
4902 if (height > room + room_cmdline - room_reserved)
4903 room_reserved = room + room_cmdline - height;
4904 /* If there is only a 'winfixheight' window and making the
4905 * window smaller, need to make the other window taller. */
4906 if (take < 0 && room - curfrp->fr_height < room_reserved)
4907 room_reserved = 0;
4909 if (take > 0 && room_cmdline > 0)
4911 /* use lines from cmdline first */
4912 if (take < room_cmdline)
4913 room_cmdline = take;
4914 take -= room_cmdline;
4915 topframe->fr_height += room_cmdline;
4919 * set the current frame to the new height
4921 frame_new_height(curfrp, height, FALSE, FALSE);
4924 * First take lines from the frames after the current frame. If
4925 * that is not enough, takes lines from frames above the current
4926 * frame.
4928 for (run = 0; run < 2; ++run)
4930 if (run == 0)
4931 frp = curfrp->fr_next; /* 1st run: start with next window */
4932 else
4933 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4934 while (frp != NULL && take != 0)
4936 h = frame_minheight(frp, NULL);
4937 if (room_reserved > 0
4938 && frp->fr_win != NULL
4939 && frp->fr_win->w_p_wfh)
4941 if (room_reserved >= frp->fr_height)
4942 room_reserved -= frp->fr_height;
4943 else
4945 if (frp->fr_height - room_reserved > take)
4946 room_reserved = frp->fr_height - take;
4947 take -= frp->fr_height - room_reserved;
4948 frame_new_height(frp, room_reserved, FALSE, FALSE);
4949 room_reserved = 0;
4952 else
4954 if (frp->fr_height - take < h)
4956 take -= frp->fr_height - h;
4957 frame_new_height(frp, h, FALSE, FALSE);
4959 else
4961 frame_new_height(frp, frp->fr_height - take,
4962 FALSE, FALSE);
4963 take = 0;
4966 if (run == 0)
4967 frp = frp->fr_next;
4968 else
4969 frp = frp->fr_prev;
4975 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4977 * Set current window width and take care of repositioning other windows to
4978 * fit around it.
4980 void
4981 win_setwidth(width)
4982 int width;
4984 win_setwidth_win(width, curwin);
4987 void
4988 win_setwidth_win(width, wp)
4989 int width;
4990 win_T *wp;
4992 /* Always keep current window at least one column wide, even when
4993 * 'winminwidth' is zero. */
4994 if (wp == curwin)
4996 if (width < p_wmw)
4997 width = p_wmw;
4998 if (width == 0)
4999 width = 1;
5002 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5004 /* recompute the window positions */
5005 (void)win_comp_pos();
5007 redraw_all_later(NOT_VALID);
5011 * Set the width of a frame to "width" and take care that all frames and
5012 * windows inside it are resized. Also resize frames above and below if the
5013 * are in the same FR_ROW frame.
5015 * Strategy is similar to frame_setheight().
5017 static void
5018 frame_setwidth(curfrp, width)
5019 frame_T *curfrp;
5020 int width;
5022 int room; /* total number of lines available */
5023 int take; /* number of lines taken from other windows */
5024 int run;
5025 frame_T *frp;
5026 int w;
5027 int room_reserved;
5029 /* If the width already is the desired value, nothing to do. */
5030 if (curfrp->fr_width == width)
5031 return;
5033 if (curfrp->fr_parent == NULL)
5034 /* topframe: can't change width */
5035 return;
5037 if (curfrp->fr_parent->fr_layout == FR_COL)
5039 /* Column of frames: Also need to resize frames above and below of
5040 * this one. First check for the minimal width of these. */
5041 w = frame_minwidth(curfrp->fr_parent, NULL);
5042 if (width < w)
5043 width = w;
5044 frame_setwidth(curfrp->fr_parent, width);
5046 else
5049 * Row of frames: try to change only frames in this row.
5051 * Do this twice:
5052 * 1: compute room available, if it's not enough try resizing the
5053 * containing frame.
5054 * 2: compute the room available and adjust the width to it.
5056 for (run = 1; run <= 2; ++run)
5058 room = 0;
5059 room_reserved = 0;
5060 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5061 frp = frp->fr_next)
5063 if (frp != curfrp
5064 && frp->fr_win != NULL
5065 && frp->fr_win->w_p_wfw)
5066 room_reserved += frp->fr_width;
5067 room += frp->fr_width;
5068 if (frp != curfrp)
5069 room -= frame_minwidth(frp, NULL);
5072 if (width <= room)
5073 break;
5074 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5076 if (width > room)
5077 width = room;
5078 break;
5080 frame_setwidth(curfrp->fr_parent, width
5081 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5085 * Compute the number of lines we will take from others frames (can be
5086 * negative!).
5088 take = width - curfrp->fr_width;
5090 /* If there is not enough room, also reduce the width of a window
5091 * with 'winfixwidth' set. */
5092 if (width > room - room_reserved)
5093 room_reserved = room - width;
5094 /* If there is only a 'winfixwidth' window and making the
5095 * window smaller, need to make the other window narrower. */
5096 if (take < 0 && room - curfrp->fr_width < room_reserved)
5097 room_reserved = 0;
5100 * set the current frame to the new width
5102 frame_new_width(curfrp, width, FALSE, FALSE);
5105 * First take lines from the frames right of the current frame. If
5106 * that is not enough, takes lines from frames left of the current
5107 * frame.
5109 for (run = 0; run < 2; ++run)
5111 if (run == 0)
5112 frp = curfrp->fr_next; /* 1st run: start with next window */
5113 else
5114 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5115 while (frp != NULL && take != 0)
5117 w = frame_minwidth(frp, NULL);
5118 if (room_reserved > 0
5119 && frp->fr_win != NULL
5120 && frp->fr_win->w_p_wfw)
5122 if (room_reserved >= frp->fr_width)
5123 room_reserved -= frp->fr_width;
5124 else
5126 if (frp->fr_width - room_reserved > take)
5127 room_reserved = frp->fr_width - take;
5128 take -= frp->fr_width - room_reserved;
5129 frame_new_width(frp, room_reserved, FALSE, FALSE);
5130 room_reserved = 0;
5133 else
5135 if (frp->fr_width - take < w)
5137 take -= frp->fr_width - w;
5138 frame_new_width(frp, w, FALSE, FALSE);
5140 else
5142 frame_new_width(frp, frp->fr_width - take,
5143 FALSE, FALSE);
5144 take = 0;
5147 if (run == 0)
5148 frp = frp->fr_next;
5149 else
5150 frp = frp->fr_prev;
5155 #endif /* FEAT_VERTSPLIT */
5158 * Check 'winminheight' for a valid value.
5160 void
5161 win_setminheight()
5163 int room;
5164 int first = TRUE;
5165 win_T *wp;
5167 /* loop until there is a 'winminheight' that is possible */
5168 while (p_wmh > 0)
5170 /* TODO: handle vertical splits */
5171 room = -p_wh;
5172 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5173 room += wp->w_height - p_wmh;
5174 if (room >= 0)
5175 break;
5176 --p_wmh;
5177 if (first)
5179 EMSG(_(e_noroom));
5180 first = FALSE;
5185 #ifdef FEAT_MOUSE
5188 * Status line of dragwin is dragged "offset" lines down (negative is up).
5190 void
5191 win_drag_status_line(dragwin, offset)
5192 win_T *dragwin;
5193 int offset;
5195 frame_T *curfr;
5196 frame_T *fr;
5197 int room;
5198 int row;
5199 int up; /* if TRUE, drag status line up, otherwise down */
5200 int n;
5202 fr = dragwin->w_frame;
5203 curfr = fr;
5204 if (fr != topframe) /* more than one window */
5206 fr = fr->fr_parent;
5207 /* When the parent frame is not a column of frames, its parent should
5208 * be. */
5209 if (fr->fr_layout != FR_COL)
5211 curfr = fr;
5212 if (fr != topframe) /* only a row of windows, may drag statusline */
5213 fr = fr->fr_parent;
5217 /* If this is the last frame in a column, may want to resize the parent
5218 * frame instead (go two up to skip a row of frames). */
5219 while (curfr != topframe && curfr->fr_next == NULL)
5221 if (fr != topframe)
5222 fr = fr->fr_parent;
5223 curfr = fr;
5224 if (fr != topframe)
5225 fr = fr->fr_parent;
5228 if (offset < 0) /* drag up */
5230 up = TRUE;
5231 offset = -offset;
5232 /* sum up the room of the current frame and above it */
5233 if (fr == curfr)
5235 /* only one window */
5236 room = fr->fr_height - frame_minheight(fr, NULL);
5238 else
5240 room = 0;
5241 for (fr = fr->fr_child; ; fr = fr->fr_next)
5243 room += fr->fr_height - frame_minheight(fr, NULL);
5244 if (fr == curfr)
5245 break;
5248 fr = curfr->fr_next; /* put fr at frame that grows */
5250 else /* drag down */
5252 up = FALSE;
5254 * Only dragging the last status line can reduce p_ch.
5256 room = Rows - cmdline_row;
5257 if (curfr->fr_next == NULL)
5258 room -= 1;
5259 else
5260 room -= p_ch;
5261 if (room < 0)
5262 room = 0;
5263 /* sum up the room of frames below of the current one */
5264 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5265 room += fr->fr_height - frame_minheight(fr, NULL);
5266 fr = curfr; /* put fr at window that grows */
5269 if (room < offset) /* Not enough room */
5270 offset = room; /* Move as far as we can */
5271 if (offset <= 0)
5272 return;
5275 * Grow frame fr by "offset" lines.
5276 * Doesn't happen when dragging the last status line up.
5278 if (fr != NULL)
5279 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5281 if (up)
5282 fr = curfr; /* current frame gets smaller */
5283 else
5284 fr = curfr->fr_next; /* next frame gets smaller */
5287 * Now make the other frames smaller.
5289 while (fr != NULL && offset > 0)
5291 n = frame_minheight(fr, NULL);
5292 if (fr->fr_height - offset <= n)
5294 offset -= fr->fr_height - n;
5295 frame_new_height(fr, n, !up, FALSE);
5297 else
5299 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5300 break;
5302 if (up)
5303 fr = fr->fr_prev;
5304 else
5305 fr = fr->fr_next;
5307 row = win_comp_pos();
5308 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5309 cmdline_row = row;
5310 p_ch = Rows - cmdline_row;
5311 if (p_ch < 1)
5312 p_ch = 1;
5313 curtab->tp_ch_used = p_ch;
5314 redraw_all_later(SOME_VALID);
5315 showmode();
5318 #ifdef FEAT_VERTSPLIT
5320 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5322 void
5323 win_drag_vsep_line(dragwin, offset)
5324 win_T *dragwin;
5325 int offset;
5327 frame_T *curfr;
5328 frame_T *fr;
5329 int room;
5330 int left; /* if TRUE, drag separator line left, otherwise right */
5331 int n;
5333 fr = dragwin->w_frame;
5334 if (fr == topframe) /* only one window (cannot happen?) */
5335 return;
5336 curfr = fr;
5337 fr = fr->fr_parent;
5338 /* When the parent frame is not a row of frames, its parent should be. */
5339 if (fr->fr_layout != FR_ROW)
5341 if (fr == topframe) /* only a column of windows (cannot happen?) */
5342 return;
5343 curfr = fr;
5344 fr = fr->fr_parent;
5347 /* If this is the last frame in a row, may want to resize a parent
5348 * frame instead. */
5349 while (curfr->fr_next == NULL)
5351 if (fr == topframe)
5352 break;
5353 curfr = fr;
5354 fr = fr->fr_parent;
5355 if (fr != topframe)
5357 curfr = fr;
5358 fr = fr->fr_parent;
5362 if (offset < 0) /* drag left */
5364 left = TRUE;
5365 offset = -offset;
5366 /* sum up the room of the current frame and left of it */
5367 room = 0;
5368 for (fr = fr->fr_child; ; fr = fr->fr_next)
5370 room += fr->fr_width - frame_minwidth(fr, NULL);
5371 if (fr == curfr)
5372 break;
5374 fr = curfr->fr_next; /* put fr at frame that grows */
5376 else /* drag right */
5378 left = FALSE;
5379 /* sum up the room of frames right of the current one */
5380 room = 0;
5381 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5382 room += fr->fr_width - frame_minwidth(fr, NULL);
5383 fr = curfr; /* put fr at window that grows */
5386 if (room < offset) /* Not enough room */
5387 offset = room; /* Move as far as we can */
5388 if (offset <= 0) /* No room at all, quit. */
5389 return;
5391 /* grow frame fr by offset lines */
5392 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5394 /* shrink other frames: current and at the left or at the right */
5395 if (left)
5396 fr = curfr; /* current frame gets smaller */
5397 else
5398 fr = curfr->fr_next; /* next frame gets smaller */
5400 while (fr != NULL && offset > 0)
5402 n = frame_minwidth(fr, NULL);
5403 if (fr->fr_width - offset <= n)
5405 offset -= fr->fr_width - n;
5406 frame_new_width(fr, n, !left, FALSE);
5408 else
5410 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5411 break;
5413 if (left)
5414 fr = fr->fr_prev;
5415 else
5416 fr = fr->fr_next;
5418 (void)win_comp_pos();
5419 redraw_all_later(NOT_VALID);
5421 #endif /* FEAT_VERTSPLIT */
5422 #endif /* FEAT_MOUSE */
5424 #endif /* FEAT_WINDOWS */
5427 * Set the height of a window.
5428 * This takes care of the things inside the window, not what happens to the
5429 * window position, the frame or to other windows.
5431 static void
5432 win_new_height(wp, height)
5433 win_T *wp;
5434 int height;
5436 linenr_T lnum;
5437 int sline, line_size;
5438 #define FRACTION_MULT 16384L
5440 /* Don't want a negative height. Happens when splitting a tiny window.
5441 * Will equalize heights soon to fix it. */
5442 if (height < 0)
5443 height = 0;
5444 if (wp->w_height == height)
5445 return; /* nothing to do */
5447 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5448 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5449 + FRACTION_MULT / 2) / (long)wp->w_height;
5451 wp->w_height = height;
5452 wp->w_skipcol = 0;
5454 /* Don't change w_topline when height is zero. Don't set w_topline when
5455 * 'scrollbind' is set and this isn't the current window. */
5456 if (height > 0
5457 #ifdef FEAT_SCROLLBIND
5458 && (!wp->w_p_scb || wp == curwin)
5459 #endif
5463 * Find a value for w_topline that shows the cursor at the same
5464 * relative position in the window as before (more or less).
5466 lnum = wp->w_cursor.lnum;
5467 if (lnum < 1) /* can happen when starting up */
5468 lnum = 1;
5469 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5470 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5471 sline = wp->w_wrow - line_size;
5473 if (sline >= 0)
5475 /* Make sure the whole cursor line is visible, if possible. */
5476 int rows = plines_win(wp, lnum, FALSE);
5478 if (sline > wp->w_height - rows)
5480 sline = wp->w_height - rows;
5481 wp->w_wrow -= rows - line_size;
5485 if (sline < 0)
5488 * Cursor line would go off top of screen if w_wrow was this high.
5489 * Make cursor line the first line in the window. If not enough
5490 * room use w_skipcol;
5492 wp->w_wrow = line_size;
5493 if (wp->w_wrow >= wp->w_height
5494 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5496 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5497 --wp->w_wrow;
5498 while (wp->w_wrow >= wp->w_height)
5500 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5501 + win_col_off2(wp);
5502 --wp->w_wrow;
5506 else
5508 while (sline > 0 && lnum > 1)
5510 #ifdef FEAT_FOLDING
5511 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5512 if (lnum == 1)
5514 /* first line in buffer is folded */
5515 line_size = 1;
5516 --sline;
5517 break;
5519 #endif
5520 --lnum;
5521 #ifdef FEAT_DIFF
5522 if (lnum == wp->w_topline)
5523 line_size = plines_win_nofill(wp, lnum, TRUE)
5524 + wp->w_topfill;
5525 else
5526 #endif
5527 line_size = plines_win(wp, lnum, TRUE);
5528 sline -= line_size;
5531 if (sline < 0)
5534 * Line we want at top would go off top of screen. Use next
5535 * line instead.
5537 #ifdef FEAT_FOLDING
5538 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5539 #endif
5540 lnum++;
5541 wp->w_wrow -= line_size + sline;
5543 else if (sline > 0)
5545 /* First line of file reached, use that as topline. */
5546 lnum = 1;
5547 wp->w_wrow -= sline;
5550 set_topline(wp, lnum);
5553 if (wp == curwin)
5555 if (p_so)
5556 update_topline();
5557 curs_columns(FALSE); /* validate w_wrow */
5559 wp->w_prev_fraction_row = wp->w_wrow;
5561 win_comp_scroll(wp);
5562 redraw_win_later(wp, SOME_VALID);
5563 #ifdef FEAT_WINDOWS
5564 wp->w_redr_status = TRUE;
5565 #endif
5566 invalidate_botline_win(wp);
5569 #ifdef FEAT_VERTSPLIT
5571 * Set the width of a window.
5573 static void
5574 win_new_width(wp, width)
5575 win_T *wp;
5576 int width;
5578 wp->w_width = width;
5579 wp->w_lines_valid = 0;
5580 changed_line_abv_curs_win(wp);
5581 invalidate_botline_win(wp);
5582 if (wp == curwin)
5584 update_topline();
5585 curs_columns(TRUE); /* validate w_wrow */
5587 #ifdef FEAT_GUI_MACVIM
5588 /* The view may have moved, so clear all or display may get corrupted. */
5589 redraw_win_later(wp, CLEAR);
5590 #else
5591 redraw_win_later(wp, NOT_VALID);
5592 #endif
5593 wp->w_redr_status = TRUE;
5595 #endif
5597 void
5598 win_comp_scroll(wp)
5599 win_T *wp;
5601 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5602 if (wp->w_p_scr == 0)
5603 wp->w_p_scr = 1;
5607 * command_height: called whenever p_ch has been changed
5609 void
5610 command_height()
5612 #ifdef FEAT_WINDOWS
5613 int h;
5614 frame_T *frp;
5615 int old_p_ch = curtab->tp_ch_used;
5617 /* Use the value of p_ch that we remembered. This is needed for when the
5618 * GUI starts up, we can't be sure in what order things happen. And when
5619 * p_ch was changed in another tab page. */
5620 curtab->tp_ch_used = p_ch;
5622 /* Find bottom frame with width of screen. */
5623 frp = lastwin->w_frame;
5624 # ifdef FEAT_VERTSPLIT
5625 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5626 frp = frp->fr_parent;
5627 # endif
5629 /* Avoid changing the height of a window with 'winfixheight' set. */
5630 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5631 && frp->fr_win->w_p_wfh)
5632 frp = frp->fr_prev;
5634 if (starting != NO_SCREEN)
5636 cmdline_row = Rows - p_ch;
5638 if (p_ch > old_p_ch) /* p_ch got bigger */
5640 while (p_ch > old_p_ch)
5642 if (frp == NULL)
5644 EMSG(_(e_noroom));
5645 p_ch = old_p_ch;
5646 curtab->tp_ch_used = p_ch;
5647 cmdline_row = Rows - p_ch;
5648 break;
5650 h = frp->fr_height - frame_minheight(frp, NULL);
5651 if (h > p_ch - old_p_ch)
5652 h = p_ch - old_p_ch;
5653 old_p_ch += h;
5654 frame_add_height(frp, -h);
5655 frp = frp->fr_prev;
5658 /* Recompute window positions. */
5659 (void)win_comp_pos();
5661 /* clear the lines added to cmdline */
5662 if (full_screen)
5663 screen_fill((int)(cmdline_row), (int)Rows, 0,
5664 (int)Columns, ' ', ' ', 0);
5665 msg_row = cmdline_row;
5666 redraw_cmdline = TRUE;
5667 return;
5670 if (msg_row < cmdline_row)
5671 msg_row = cmdline_row;
5672 redraw_cmdline = TRUE;
5674 frame_add_height(frp, (int)(old_p_ch - p_ch));
5676 /* Recompute window positions. */
5677 if (frp != lastwin->w_frame)
5678 (void)win_comp_pos();
5679 #else
5680 cmdline_row = Rows - p_ch;
5681 win_setheight(cmdline_row);
5682 #endif
5685 #if defined(FEAT_WINDOWS) || defined(PROTO)
5687 * Resize frame "frp" to be "n" lines higher (negative for less high).
5688 * Also resize the frames it is contained in.
5690 static void
5691 frame_add_height(frp, n)
5692 frame_T *frp;
5693 int n;
5695 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5696 for (;;)
5698 frp = frp->fr_parent;
5699 if (frp == NULL)
5700 break;
5701 frp->fr_height += n;
5706 * Add or remove a status line for the bottom window(s), according to the
5707 * value of 'laststatus'.
5709 void
5710 last_status(morewin)
5711 int morewin; /* pretend there are two or more windows */
5713 /* Don't make a difference between horizontal or vertical split. */
5714 last_status_rec(topframe, (p_ls == 2
5715 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5718 static void
5719 last_status_rec(fr, statusline)
5720 frame_T *fr;
5721 int statusline;
5723 frame_T *fp;
5724 win_T *wp;
5726 if (fr->fr_layout == FR_LEAF)
5728 wp = fr->fr_win;
5729 if (wp->w_status_height != 0 && !statusline)
5731 /* remove status line */
5732 win_new_height(wp, wp->w_height + 1);
5733 wp->w_status_height = 0;
5734 comp_col();
5736 else if (wp->w_status_height == 0 && statusline)
5738 /* Find a frame to take a line from. */
5739 fp = fr;
5740 while (fp->fr_height <= frame_minheight(fp, NULL))
5742 if (fp == topframe)
5744 EMSG(_(e_noroom));
5745 return;
5747 /* In a column of frames: go to frame above. If already at
5748 * the top or in a row of frames: go to parent. */
5749 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5750 fp = fp->fr_prev;
5751 else
5752 fp = fp->fr_parent;
5754 wp->w_status_height = 1;
5755 if (fp != fr)
5757 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5758 frame_fix_height(wp);
5759 (void)win_comp_pos();
5761 else
5762 win_new_height(wp, wp->w_height - 1);
5763 comp_col();
5764 redraw_all_later(SOME_VALID);
5767 #ifdef FEAT_VERTSPLIT
5768 else if (fr->fr_layout == FR_ROW)
5770 /* vertically split windows, set status line for each one */
5771 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5772 last_status_rec(fp, statusline);
5774 #endif
5775 else
5777 /* horizontally split window, set status line for last one */
5778 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5780 last_status_rec(fp, statusline);
5785 * Return the number of lines used by the tab page line.
5788 tabline_height()
5790 #ifdef FEAT_GUI_TABLINE
5791 /* When the GUI has the tabline then this always returns zero. */
5792 if (gui_use_tabline())
5793 return 0;
5794 #endif
5795 switch (p_stal)
5797 case 0: return 0;
5798 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5800 return 1;
5803 #endif /* FEAT_WINDOWS */
5805 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5807 * Get the file name at the cursor.
5808 * If Visual mode is active, use the selected text if it's in one line.
5809 * Returns the name in allocated memory, NULL for failure.
5811 char_u *
5812 grab_file_name(count, file_lnum)
5813 long count;
5814 linenr_T *file_lnum;
5816 # ifdef FEAT_VISUAL
5817 if (VIsual_active)
5819 int len;
5820 char_u *ptr;
5822 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5823 return NULL;
5824 return find_file_name_in_path(ptr, len,
5825 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5827 # endif
5828 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5829 file_lnum);
5834 * Return the file name under or after the cursor.
5836 * The 'path' option is searched if the file name is not absolute.
5837 * The string returned has been alloc'ed and should be freed by the caller.
5838 * NULL is returned if the file name or file is not found.
5840 * options:
5841 * FNAME_MESS give error messages
5842 * FNAME_EXP expand to path
5843 * FNAME_HYP check for hypertext link
5844 * FNAME_INCL apply "includeexpr"
5846 char_u *
5847 file_name_at_cursor(options, count, file_lnum)
5848 int options;
5849 long count;
5850 linenr_T *file_lnum;
5852 return file_name_in_line(ml_get_curline(),
5853 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5854 file_lnum);
5858 * Return the name of the file under or after ptr[col].
5859 * Otherwise like file_name_at_cursor().
5861 char_u *
5862 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5863 char_u *line;
5864 int col;
5865 int options;
5866 long count;
5867 char_u *rel_fname; /* file we are searching relative to */
5868 linenr_T *file_lnum; /* line number after the file name */
5870 char_u *ptr;
5871 int len;
5874 * search forward for what could be the start of a file name
5876 ptr = line + col;
5877 while (*ptr != NUL && !vim_isfilec(*ptr))
5878 mb_ptr_adv(ptr);
5879 if (*ptr == NUL) /* nothing found */
5881 if (options & FNAME_MESS)
5882 EMSG(_("E446: No file name under cursor"));
5883 return NULL;
5887 * Search backward for first char of the file name.
5888 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5890 while (ptr > line)
5892 #ifdef FEAT_MBYTE
5893 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5894 ptr -= len + 1;
5895 else
5896 #endif
5897 if (vim_isfilec(ptr[-1])
5898 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5899 --ptr;
5900 else
5901 break;
5905 * Search forward for the last char of the file name.
5906 * Also allow "://" when ':' is not in 'isfname'.
5908 len = 0;
5909 while (vim_isfilec(ptr[len])
5910 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5911 #ifdef FEAT_MBYTE
5912 if (has_mbyte)
5913 len += (*mb_ptr2len)(ptr + len);
5914 else
5915 #endif
5916 ++len;
5919 * If there is trailing punctuation, remove it.
5920 * But don't remove "..", could be a directory name.
5922 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5923 && ptr[len - 2] != '.')
5924 --len;
5926 if (file_lnum != NULL)
5928 char_u *p;
5930 /* Get the number after the file name and a separator character */
5931 p = ptr + len;
5932 p = skipwhite(p);
5933 if (*p != NUL)
5935 if (!isdigit(*p))
5936 ++p; /* skip the separator */
5937 p = skipwhite(p);
5938 if (isdigit(*p))
5939 *file_lnum = (int)getdigits(&p);
5943 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5946 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5947 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5949 static char_u *
5950 eval_includeexpr(ptr, len)
5951 char_u *ptr;
5952 int len;
5954 char_u *res;
5956 set_vim_var_string(VV_FNAME, ptr, len);
5957 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5958 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5959 set_vim_var_string(VV_FNAME, NULL, 0);
5960 return res;
5962 #endif
5965 * Return the name of the file ptr[len] in 'path'.
5966 * Otherwise like file_name_at_cursor().
5968 char_u *
5969 find_file_name_in_path(ptr, len, options, count, rel_fname)
5970 char_u *ptr;
5971 int len;
5972 int options;
5973 long count;
5974 char_u *rel_fname; /* file we are searching relative to */
5976 char_u *file_name;
5977 int c;
5978 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5979 char_u *tofree = NULL;
5981 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5983 tofree = eval_includeexpr(ptr, len);
5984 if (tofree != NULL)
5986 ptr = tofree;
5987 len = (int)STRLEN(ptr);
5990 # endif
5992 if (options & FNAME_EXP)
5994 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5995 TRUE, rel_fname);
5997 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5999 * If the file could not be found in a normal way, try applying
6000 * 'includeexpr' (unless done already).
6002 if (file_name == NULL
6003 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6005 tofree = eval_includeexpr(ptr, len);
6006 if (tofree != NULL)
6008 ptr = tofree;
6009 len = (int)STRLEN(ptr);
6010 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6011 TRUE, rel_fname);
6014 # endif
6015 if (file_name == NULL && (options & FNAME_MESS))
6017 c = ptr[len];
6018 ptr[len] = NUL;
6019 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6020 ptr[len] = c;
6023 /* Repeat finding the file "count" times. This matters when it
6024 * appears several times in the path. */
6025 while (file_name != NULL && --count > 0)
6027 vim_free(file_name);
6028 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6031 else
6032 file_name = vim_strnsave(ptr, len);
6034 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6035 vim_free(tofree);
6036 # endif
6038 return file_name;
6040 #endif /* FEAT_SEARCHPATH */
6043 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6044 * Also check for ":\\", which MS Internet Explorer accepts, return
6045 * URL_BACKSLASH.
6047 static int
6048 path_is_url(p)
6049 char_u *p;
6051 if (STRNCMP(p, "://", (size_t)3) == 0)
6052 return URL_SLASH;
6053 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6054 return URL_BACKSLASH;
6055 return 0;
6059 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6060 * Return URL_BACKSLASH for "name:\\".
6061 * Return zero otherwise.
6064 path_with_url(fname)
6065 char_u *fname;
6067 char_u *p;
6069 for (p = fname; isalpha(*p); ++p)
6071 return path_is_url(p);
6075 * Return TRUE if "name" is a full (absolute) path name or URL.
6078 vim_isAbsName(name)
6079 char_u *name;
6081 return (path_with_url(name) != 0 || mch_isFullName(name));
6085 * Get absolute file name into buffer "buf[len]".
6087 * return FAIL for failure, OK otherwise
6090 vim_FullName(fname, buf, len, force)
6091 char_u *fname, *buf;
6092 int len;
6093 int force; /* force expansion even when already absolute */
6095 int retval = OK;
6096 int url;
6098 *buf = NUL;
6099 if (fname == NULL)
6100 return FAIL;
6102 url = path_with_url(fname);
6103 if (!url)
6104 retval = mch_FullName(fname, buf, len, force);
6105 if (url || retval == FAIL)
6107 /* something failed; use the file name (truncate when too long) */
6108 vim_strncpy(buf, fname, len - 1);
6110 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6111 slash_adjust(buf);
6112 #endif
6113 return retval;
6117 * Return the minimal number of rows that is needed on the screen to display
6118 * the current number of windows.
6121 min_rows()
6123 int total;
6124 #ifdef FEAT_WINDOWS
6125 tabpage_T *tp;
6126 int n;
6127 #endif
6129 if (firstwin == NULL) /* not initialized yet */
6130 return MIN_LINES;
6132 #ifdef FEAT_WINDOWS
6133 total = 0;
6134 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6136 n = frame_minheight(tp->tp_topframe, NULL);
6137 if (total < n)
6138 total = n;
6140 total += tabline_height();
6141 #else
6142 total = 1; /* at least one window should have a line! */
6143 #endif
6144 total += 1; /* count the room for the command line */
6145 return total;
6149 * Return TRUE if there is only one window (in the current tab page), not
6150 * counting a help or preview window, unless it is the current window.
6151 * Does not count "aucmd_win".
6154 only_one_window()
6156 #ifdef FEAT_WINDOWS
6157 int count = 0;
6158 win_T *wp;
6160 /* If there is another tab page there always is another window. */
6161 if (first_tabpage->tp_next != NULL)
6162 return FALSE;
6164 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6165 if ((!((wp->w_buffer->b_help && !curbuf->b_help)
6166 # ifdef FEAT_QUICKFIX
6167 || wp->w_p_pvw
6168 # endif
6169 ) || wp == curwin)
6170 # ifdef FEAT_AUTOCMD
6171 && wp != aucmd_win
6172 # endif
6174 ++count;
6175 return (count <= 1);
6176 #else
6177 return TRUE;
6178 #endif
6181 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6183 * Correct the cursor line number in other windows. Used after changing the
6184 * current buffer, and before applying autocommands.
6185 * When "do_curwin" is TRUE, also check current window.
6187 void
6188 check_lnums(do_curwin)
6189 int do_curwin;
6191 win_T *wp;
6193 #ifdef FEAT_WINDOWS
6194 tabpage_T *tp;
6196 FOR_ALL_TAB_WINDOWS(tp, wp)
6197 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6198 #else
6199 wp = curwin;
6200 if (do_curwin)
6201 #endif
6203 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6204 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6205 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6206 wp->w_topline = curbuf->b_ml.ml_line_count;
6209 #endif
6211 #if defined(FEAT_WINDOWS) || defined(PROTO)
6214 * A snapshot of the window sizes, to restore them after closing the help
6215 * window.
6216 * Only these fields are used:
6217 * fr_layout
6218 * fr_width
6219 * fr_height
6220 * fr_next
6221 * fr_child
6222 * fr_win (only valid for the old curwin, NULL otherwise)
6226 * Create a snapshot of the current frame sizes.
6228 void
6229 make_snapshot(idx)
6230 int idx;
6232 clear_snapshot(curtab, idx);
6233 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6236 static void
6237 make_snapshot_rec(fr, frp)
6238 frame_T *fr;
6239 frame_T **frp;
6241 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6242 if (*frp == NULL)
6243 return;
6244 (*frp)->fr_layout = fr->fr_layout;
6245 # ifdef FEAT_VERTSPLIT
6246 (*frp)->fr_width = fr->fr_width;
6247 # endif
6248 (*frp)->fr_height = fr->fr_height;
6249 if (fr->fr_next != NULL)
6250 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6251 if (fr->fr_child != NULL)
6252 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6253 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6254 (*frp)->fr_win = curwin;
6258 * Remove any existing snapshot.
6260 static void
6261 clear_snapshot(tp, idx)
6262 tabpage_T *tp;
6263 int idx;
6265 clear_snapshot_rec(tp->tp_snapshot[idx]);
6266 tp->tp_snapshot[idx] = NULL;
6269 static void
6270 clear_snapshot_rec(fr)
6271 frame_T *fr;
6273 if (fr != NULL)
6275 clear_snapshot_rec(fr->fr_next);
6276 clear_snapshot_rec(fr->fr_child);
6277 vim_free(fr);
6282 * Restore a previously created snapshot, if there is any.
6283 * This is only done if the screen size didn't change and the window layout is
6284 * still the same.
6286 void
6287 restore_snapshot(idx, close_curwin)
6288 int idx;
6289 int close_curwin; /* closing current window */
6291 win_T *wp;
6293 if (curtab->tp_snapshot[idx] != NULL
6294 # ifdef FEAT_VERTSPLIT
6295 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6296 # endif
6297 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6298 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6300 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6301 win_comp_pos();
6302 if (wp != NULL && close_curwin)
6303 win_goto(wp);
6304 redraw_all_later(CLEAR);
6306 clear_snapshot(curtab, idx);
6310 * Check if frames "sn" and "fr" have the same layout, same following frames
6311 * and same children.
6313 static int
6314 check_snapshot_rec(sn, fr)
6315 frame_T *sn;
6316 frame_T *fr;
6318 if (sn->fr_layout != fr->fr_layout
6319 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6320 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6321 || (sn->fr_next != NULL
6322 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6323 || (sn->fr_child != NULL
6324 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6325 return FAIL;
6326 return OK;
6330 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6331 * following frames and children.
6332 * Returns a pointer to the old current window, or NULL.
6334 static win_T *
6335 restore_snapshot_rec(sn, fr)
6336 frame_T *sn;
6337 frame_T *fr;
6339 win_T *wp = NULL;
6340 win_T *wp2;
6342 fr->fr_height = sn->fr_height;
6343 # ifdef FEAT_VERTSPLIT
6344 fr->fr_width = sn->fr_width;
6345 # endif
6346 if (fr->fr_layout == FR_LEAF)
6348 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6349 # ifdef FEAT_VERTSPLIT
6350 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6351 # endif
6352 wp = sn->fr_win;
6354 if (sn->fr_next != NULL)
6356 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6357 if (wp2 != NULL)
6358 wp = wp2;
6360 if (sn->fr_child != NULL)
6362 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6363 if (wp2 != NULL)
6364 wp = wp2;
6366 return wp;
6369 #endif
6371 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6373 * Return TRUE if there is any vertically split window.
6376 win_hasvertsplit()
6378 frame_T *fr;
6380 if (topframe->fr_layout == FR_ROW)
6381 return TRUE;
6383 if (topframe->fr_layout == FR_COL)
6384 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6385 if (fr->fr_layout == FR_ROW)
6386 return TRUE;
6388 return FALSE;
6390 #endif
6392 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6394 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6395 * highlighted with the group 'grp' with priority 'prio'.
6396 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6397 * If no particular ID is desired, -1 must be specified for 'id'.
6398 * Return ID of added match, -1 on failure.
6401 match_add(wp, grp, pat, prio, id)
6402 win_T *wp;
6403 char_u *grp;
6404 char_u *pat;
6405 int prio;
6406 int id;
6408 matchitem_T *cur;
6409 matchitem_T *prev;
6410 matchitem_T *m;
6411 int hlg_id;
6412 regprog_T *regprog;
6414 if (*grp == NUL || *pat == NUL)
6415 return -1;
6416 if (id < -1 || id == 0)
6418 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6419 return -1;
6421 if (id != -1)
6423 cur = wp->w_match_head;
6424 while (cur != NULL)
6426 if (cur->id == id)
6428 EMSGN("E801: ID already taken: %ld", id);
6429 return -1;
6431 cur = cur->next;
6434 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6436 EMSG2(_(e_nogroup), grp);
6437 return -1;
6439 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6441 EMSG2(_(e_invarg2), pat);
6442 return -1;
6445 /* Find available match ID. */
6446 while (id == -1)
6448 cur = wp->w_match_head;
6449 while (cur != NULL && cur->id != wp->w_next_match_id)
6450 cur = cur->next;
6451 if (cur == NULL)
6452 id = wp->w_next_match_id;
6453 wp->w_next_match_id++;
6456 /* Build new match. */
6457 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6458 m->id = id;
6459 m->priority = prio;
6460 m->pattern = vim_strsave(pat);
6461 m->hlg_id = hlg_id;
6462 m->match.regprog = regprog;
6463 m->match.rmm_ic = FALSE;
6464 m->match.rmm_maxcol = 0;
6466 /* Insert new match. The match list is in ascending order with regard to
6467 * the match priorities. */
6468 cur = wp->w_match_head;
6469 prev = cur;
6470 while (cur != NULL && prio >= cur->priority)
6472 prev = cur;
6473 cur = cur->next;
6475 if (cur == prev)
6476 wp->w_match_head = m;
6477 else
6478 prev->next = m;
6479 m->next = cur;
6481 redraw_later(SOME_VALID);
6482 return id;
6486 * Delete match with ID 'id' in the match list of window 'wp'.
6487 * Print error messages if 'perr' is TRUE.
6490 match_delete(wp, id, perr)
6491 win_T *wp;
6492 int id;
6493 int perr;
6495 matchitem_T *cur = wp->w_match_head;
6496 matchitem_T *prev = cur;
6498 if (id < 1)
6500 if (perr == TRUE)
6501 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6502 id);
6503 return -1;
6505 while (cur != NULL && cur->id != id)
6507 prev = cur;
6508 cur = cur->next;
6510 if (cur == NULL)
6512 if (perr == TRUE)
6513 EMSGN("E803: ID not found: %ld", id);
6514 return -1;
6516 if (cur == prev)
6517 wp->w_match_head = cur->next;
6518 else
6519 prev->next = cur->next;
6520 vim_free(cur->match.regprog);
6521 vim_free(cur->pattern);
6522 vim_free(cur);
6523 redraw_later(SOME_VALID);
6524 return 0;
6528 * Delete all matches in the match list of window 'wp'.
6530 void
6531 clear_matches(wp)
6532 win_T *wp;
6534 matchitem_T *m;
6536 while (wp->w_match_head != NULL)
6538 m = wp->w_match_head->next;
6539 vim_free(wp->w_match_head->match.regprog);
6540 vim_free(wp->w_match_head->pattern);
6541 vim_free(wp->w_match_head);
6542 wp->w_match_head = m;
6544 redraw_later(SOME_VALID);
6548 * Get match from ID 'id' in window 'wp'.
6549 * Return NULL if match not found.
6551 matchitem_T *
6552 get_match(wp, id)
6553 win_T *wp;
6554 int id;
6556 matchitem_T *cur = wp->w_match_head;
6558 while (cur != NULL && cur->id != id)
6559 cur = cur->next;
6560 return cur;
6562 #endif