Merged from the latest developing branch.
[MacVim.git] / src / window.c
blob81caee932c4a1d228dbf5e807d6188b91c720890
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
10 #include "vim.h"
12 static int path_is_url __ARGS((char_u *p));
13 #if defined(FEAT_WINDOWS) || defined(PROTO)
14 static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
15 static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
16 static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17 static void frame_setheight __ARGS((frame_T *curfrp, int height));
18 #ifdef FEAT_VERTSPLIT
19 static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20 #endif
21 static void win_exchange __ARGS((long));
22 static void win_rotate __ARGS((int, int));
23 static void win_totop __ARGS((int size, int flags));
24 static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
25 static int last_window __ARGS((void));
26 static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
27 static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
28 static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
29 static tabpage_T *alt_tabpage __ARGS((void));
30 static win_T *frame2win __ARGS((frame_T *frp));
31 static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32 static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33 static int frame_fixed_height __ARGS((frame_T *frp));
34 #ifdef FEAT_VERTSPLIT
35 static int frame_fixed_width __ARGS((frame_T *frp));
36 static void frame_add_statusline __ARGS((frame_T *frp));
37 static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
38 static void frame_add_vsep __ARGS((frame_T *frp));
39 static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40 static void frame_fix_width __ARGS((win_T *wp));
41 #endif
42 #endif
43 static int win_alloc_firstwin __ARGS((win_T *oldwin));
44 #if defined(FEAT_WINDOWS) || defined(PROTO)
45 static tabpage_T *alloc_tabpage __ARGS((void));
46 static int leave_tabpage __ARGS((buf_T *new_curbuf));
47 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
48 static void frame_fix_height __ARGS((win_T *wp));
49 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
50 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
51 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
52 static void win_append __ARGS((win_T *, win_T *));
53 static void win_remove __ARGS((win_T *, tabpage_T *tp));
54 static void frame_append __ARGS((frame_T *after, frame_T *frp));
55 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56 static void frame_remove __ARGS((frame_T *frp));
57 #ifdef FEAT_VERTSPLIT
58 static void win_new_width __ARGS((win_T *wp, int width));
59 static void win_goto_ver __ARGS((int up, long count));
60 static void win_goto_hor __ARGS((int left, long count));
61 #endif
62 static void frame_add_height __ARGS((frame_T *frp, int n));
63 static void last_status_rec __ARGS((frame_T *fr, int statusline));
65 static void make_snapshot __ARGS((void));
66 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
67 static void clear_snapshot __ARGS((tabpage_T *tp));
68 static void clear_snapshot_rec __ARGS((frame_T *fr));
69 static void restore_snapshot __ARGS((int close_curwin));
70 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
71 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
73 #endif /* FEAT_WINDOWS */
75 static win_T *win_alloc __ARGS((win_T *after));
76 static void win_new_height __ARGS((win_T *, int));
78 #define URL_SLASH 1 /* path_is_url() has found "://" */
79 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81 #define NOWIN (win_T *)-1 /* non-existing window */
83 #ifdef FEAT_WINDOWS
84 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
85 #else
86 # define ROWS_AVAIL (Rows - p_ch)
87 #endif
89 #if defined(FEAT_WINDOWS) || defined(PROTO)
91 static char *m_onlyone = N_("Already only one window");
94 * all CTRL-W window commands are handled here, called from normal_cmd().
96 void
97 do_window(nchar, Prenum, xchar)
98 int nchar;
99 long Prenum;
100 int xchar; /* extra char from ":wincmd gx" or NUL */
102 long Prenum1;
103 win_T *wp;
104 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
105 char_u *ptr;
106 linenr_T lnum = -1;
107 #endif
108 #ifdef FEAT_FIND_ID
109 int type = FIND_DEFINE;
110 int len;
111 #endif
112 char_u cbuf[40];
114 if (Prenum == 0)
115 Prenum1 = 1;
116 else
117 Prenum1 = Prenum;
119 #ifdef FEAT_CMDWIN
120 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
121 #else
122 # define CHECK_CMDWIN
123 #endif
125 switch (nchar)
127 /* split current window in two parts, horizontally */
128 case 'S':
129 case Ctrl_S:
130 case 's':
131 CHECK_CMDWIN
132 #ifdef FEAT_VISUAL
133 reset_VIsual_and_resel(); /* stop Visual mode */
134 #endif
135 #ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140 #endif
141 #ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143 #endif
144 win_split((int)Prenum, 0);
145 break;
147 #ifdef FEAT_VERTSPLIT
148 /* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
152 # ifdef FEAT_VISUAL
153 reset_VIsual_and_resel(); /* stop Visual mode */
154 # endif
155 # ifdef FEAT_QUICKFIX
156 /* When splitting the quickfix window open a new buffer in it,
157 * don't replicate the quickfix buffer. */
158 if (bt_quickfix(curbuf))
159 goto newwindow;
160 # endif
161 # ifdef FEAT_GUI
162 need_mouse_correct = TRUE;
163 # endif
164 win_split((int)Prenum, WSP_VERT);
165 break;
166 #endif
168 /* split current window and edit alternate file */
169 case Ctrl_HAT:
170 case '^':
171 CHECK_CMDWIN
172 #ifdef FEAT_VISUAL
173 reset_VIsual_and_resel(); /* stop Visual mode */
174 #endif
175 STRCPY(cbuf, "split #");
176 if (Prenum)
177 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
178 "%ld", Prenum);
179 do_cmdline_cmd(cbuf);
180 break;
182 /* open new window */
183 case Ctrl_N:
184 case 'n':
185 CHECK_CMDWIN
186 #ifdef FEAT_VISUAL
187 reset_VIsual_and_resel(); /* stop Visual mode */
188 #endif
189 #ifdef FEAT_QUICKFIX
190 newwindow:
191 #endif
192 if (Prenum)
193 /* window height */
194 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
195 else
196 cbuf[0] = NUL;
197 #if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
198 if (nchar == 'v' || nchar == Ctrl_V)
199 STRCAT(cbuf, "v");
200 #endif
201 STRCAT(cbuf, "new");
202 do_cmdline_cmd(cbuf);
203 break;
205 /* quit current window */
206 case Ctrl_Q:
207 case 'q':
208 #ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210 #endif
211 do_cmdline_cmd((char_u *)"quit");
212 break;
214 /* close current window */
215 case Ctrl_C:
216 case 'c':
217 #ifdef FEAT_VISUAL
218 reset_VIsual_and_resel(); /* stop Visual mode */
219 #endif
220 do_cmdline_cmd((char_u *)"close");
221 break;
223 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
224 /* close preview window */
225 case Ctrl_Z:
226 case 'z':
227 CHECK_CMDWIN
228 #ifdef FEAT_VISUAL
229 reset_VIsual_and_resel(); /* stop Visual mode */
230 #endif
231 do_cmdline_cmd((char_u *)"pclose");
232 break;
234 /* cursor to preview window */
235 case 'P':
236 for (wp = firstwin; wp != NULL; wp = wp->w_next)
237 if (wp->w_p_pvw)
238 break;
239 if (wp == NULL)
240 EMSG(_("E441: There is no preview window"));
241 else
242 win_goto(wp);
243 break;
244 #endif
246 /* close all but current window */
247 case Ctrl_O:
248 case 'o':
249 CHECK_CMDWIN
250 #ifdef FEAT_VISUAL
251 reset_VIsual_and_resel(); /* stop Visual mode */
252 #endif
253 do_cmdline_cmd((char_u *)"only");
254 break;
256 /* cursor to next window with wrap around */
257 case Ctrl_W:
258 case 'w':
259 /* cursor to previous window with wrap around */
260 case 'W':
261 CHECK_CMDWIN
262 if (lastwin == firstwin && Prenum != 1) /* just one window */
263 beep_flush();
264 else
266 if (Prenum) /* go to specified window */
268 for (wp = firstwin; --Prenum > 0; )
270 if (wp->w_next == NULL)
271 break;
272 else
273 wp = wp->w_next;
276 else
278 if (nchar == 'W') /* go to previous window */
280 wp = curwin->w_prev;
281 if (wp == NULL)
282 wp = lastwin; /* wrap around */
284 else /* go to next window */
286 wp = curwin->w_next;
287 if (wp == NULL)
288 wp = firstwin; /* wrap around */
291 win_goto(wp);
293 break;
295 /* cursor to window below */
296 case 'j':
297 case K_DOWN:
298 case Ctrl_J:
299 CHECK_CMDWIN
300 #ifdef FEAT_VERTSPLIT
301 win_goto_ver(FALSE, Prenum1);
302 #else
303 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
304 wp = wp->w_next)
306 win_goto(wp);
307 #endif
308 break;
310 /* cursor to window above */
311 case 'k':
312 case K_UP:
313 case Ctrl_K:
314 CHECK_CMDWIN
315 #ifdef FEAT_VERTSPLIT
316 win_goto_ver(TRUE, Prenum1);
317 #else
318 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
319 wp = wp->w_prev)
321 win_goto(wp);
322 #endif
323 break;
325 #ifdef FEAT_VERTSPLIT
326 /* cursor to left window */
327 case 'h':
328 case K_LEFT:
329 case Ctrl_H:
330 case K_BS:
331 CHECK_CMDWIN
332 win_goto_hor(TRUE, Prenum1);
333 break;
335 /* cursor to right window */
336 case 'l':
337 case K_RIGHT:
338 case Ctrl_L:
339 CHECK_CMDWIN
340 win_goto_hor(FALSE, Prenum1);
341 break;
342 #endif
344 /* move window to new tab page */
345 case 'T':
346 if (firstwin == lastwin)
347 MSG(_(m_onlyone));
348 else
350 tabpage_T *oldtab = curtab;
351 tabpage_T *newtab;
353 /* First create a new tab with the window, then go back to
354 * the old tab and close the window there. */
355 wp = curwin;
356 if (win_new_tabpage((int)Prenum) == OK
357 && valid_tabpage(oldtab))
359 newtab = curtab;
360 goto_tabpage_tp(oldtab);
361 if (curwin == wp)
362 win_close(curwin, FALSE);
363 if (valid_tabpage(newtab))
364 goto_tabpage_tp(newtab);
367 break;
369 /* cursor to top-left window */
370 case 't':
371 case Ctrl_T:
372 win_goto(firstwin);
373 break;
375 /* cursor to bottom-right window */
376 case 'b':
377 case Ctrl_B:
378 win_goto(lastwin);
379 break;
381 /* cursor to last accessed (previous) window */
382 case 'p':
383 case Ctrl_P:
384 if (prevwin == NULL)
385 beep_flush();
386 else
387 win_goto(prevwin);
388 break;
390 /* exchange current and next window */
391 case 'x':
392 case Ctrl_X:
393 CHECK_CMDWIN
394 win_exchange(Prenum);
395 break;
397 /* rotate windows downwards */
398 case Ctrl_R:
399 case 'r':
400 CHECK_CMDWIN
401 #ifdef FEAT_VISUAL
402 reset_VIsual_and_resel(); /* stop Visual mode */
403 #endif
404 win_rotate(FALSE, (int)Prenum1); /* downwards */
405 break;
407 /* rotate windows upwards */
408 case 'R':
409 CHECK_CMDWIN
410 #ifdef FEAT_VISUAL
411 reset_VIsual_and_resel(); /* stop Visual mode */
412 #endif
413 win_rotate(TRUE, (int)Prenum1); /* upwards */
414 break;
416 /* move window to the very top/bottom/left/right */
417 case 'K':
418 case 'J':
419 #ifdef FEAT_VERTSPLIT
420 case 'H':
421 case 'L':
422 #endif
423 CHECK_CMDWIN
424 win_totop((int)Prenum,
425 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
426 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
427 break;
429 /* make all windows the same height */
430 case '=':
431 #ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433 #endif
434 win_equal(NULL, FALSE, 'b');
435 break;
437 /* increase current window height */
438 case '+':
439 #ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441 #endif
442 win_setheight(curwin->w_height + (int)Prenum1);
443 break;
445 /* decrease current window height */
446 case '-':
447 #ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449 #endif
450 win_setheight(curwin->w_height - (int)Prenum1);
451 break;
453 /* set current window height */
454 case Ctrl__:
455 case '_':
456 #ifdef FEAT_GUI
457 need_mouse_correct = TRUE;
458 #endif
459 win_setheight(Prenum ? (int)Prenum : 9999);
460 break;
462 #ifdef FEAT_VERTSPLIT
463 /* increase current window width */
464 case '>':
465 #ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467 #endif
468 win_setwidth(curwin->w_width + (int)Prenum1);
469 break;
471 /* decrease current window width */
472 case '<':
473 #ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475 #endif
476 win_setwidth(curwin->w_width - (int)Prenum1);
477 break;
479 /* set current window width */
480 case '|':
481 #ifdef FEAT_GUI
482 need_mouse_correct = TRUE;
483 #endif
484 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
485 break;
486 #endif
488 /* jump to tag and split window if tag exists (in preview window) */
489 #if defined(FEAT_QUICKFIX)
490 case '}':
491 CHECK_CMDWIN
492 if (Prenum)
493 g_do_tagpreview = Prenum;
494 else
495 g_do_tagpreview = p_pvh;
496 /*FALLTHROUGH*/
497 #endif
498 case ']':
499 case Ctrl_RSB:
500 CHECK_CMDWIN
501 #ifdef FEAT_VISUAL
502 reset_VIsual_and_resel(); /* stop Visual mode */
503 #endif
504 if (Prenum)
505 postponed_split = Prenum;
506 else
507 postponed_split = -1;
509 /* Execute the command right here, required when
510 * "wincmd ]" was used in a function. */
511 do_nv_ident(Ctrl_RSB, NUL);
512 break;
514 #ifdef FEAT_SEARCHPATH
515 /* edit file name under cursor in a new window */
516 case 'f':
517 case 'F':
518 case Ctrl_F:
519 wingotofile:
520 CHECK_CMDWIN
522 ptr = grab_file_name(Prenum1, &lnum);
523 if (ptr != NULL)
525 # ifdef FEAT_GUI
526 need_mouse_correct = TRUE;
527 # endif
528 setpcmark();
529 if (win_split(0, 0) == OK)
531 # ifdef FEAT_SCROLLBIND
532 curwin->w_p_scb = FALSE;
533 # endif
534 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
535 ECMD_HIDE, NULL);
536 if (nchar == 'F' && lnum >= 0)
538 curwin->w_cursor.lnum = lnum;
539 check_cursor_lnum();
540 beginline(BL_SOL | BL_FIX);
543 vim_free(ptr);
545 break;
546 #endif
548 #ifdef FEAT_FIND_ID
549 /* Go to the first occurrence of the identifier under cursor along path in a
550 * new window -- webb
552 case 'i': /* Go to any match */
553 case Ctrl_I:
554 type = FIND_ANY;
555 /* FALLTHROUGH */
556 case 'd': /* Go to definition, using 'define' */
557 case Ctrl_D:
558 CHECK_CMDWIN
559 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
560 break;
561 find_pattern_in_path(ptr, 0, len, TRUE,
562 Prenum == 0 ? TRUE : FALSE, type,
563 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
564 curwin->w_set_curswant = TRUE;
565 break;
566 #endif
568 case K_KENTER:
569 case CAR:
570 #if defined(FEAT_QUICKFIX)
572 * In a quickfix window a <CR> jumps to the error under the
573 * cursor in a new window.
575 if (bt_quickfix(curbuf))
577 sprintf((char *)cbuf, "split +%ld%s",
578 (long)curwin->w_cursor.lnum,
579 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
580 do_cmdline_cmd(cbuf);
582 #endif
583 break;
586 /* CTRL-W g extended commands */
587 case 'g':
588 case Ctrl_G:
589 CHECK_CMDWIN
590 #ifdef USE_ON_FLY_SCROLL
591 dont_scroll = TRUE; /* disallow scrolling here */
592 #endif
593 ++no_mapping;
594 ++allow_keys; /* no mapping for xchar, but allow key codes */
595 if (xchar == NUL)
596 xchar = plain_vgetc();
597 LANGMAP_ADJUST(xchar, TRUE);
598 --no_mapping;
599 --allow_keys;
600 #ifdef FEAT_CMDL_INFO
601 (void)add_to_showcmd(xchar);
602 #endif
603 switch (xchar)
605 #if defined(FEAT_QUICKFIX)
606 case '}':
607 xchar = Ctrl_RSB;
608 if (Prenum)
609 g_do_tagpreview = Prenum;
610 else
611 g_do_tagpreview = p_pvh;
612 /*FALLTHROUGH*/
613 #endif
614 case ']':
615 case Ctrl_RSB:
616 #ifdef FEAT_VISUAL
617 reset_VIsual_and_resel(); /* stop Visual mode */
618 #endif
619 if (Prenum)
620 postponed_split = Prenum;
621 else
622 postponed_split = -1;
624 /* Execute the command right here, required when
625 * "wincmd g}" was used in a function. */
626 do_nv_ident('g', xchar);
627 break;
629 #ifdef FEAT_SEARCHPATH
630 case 'f': /* CTRL-W gf: "gf" in a new tab page */
631 case 'F': /* CTRL-W gF: "gF" in a new tab page */
632 cmdmod.tab = TRUE;
633 nchar = xchar;
634 goto wingotofile;
635 #endif
636 default:
637 beep_flush();
638 break;
640 break;
642 default: beep_flush();
643 break;
648 * split the current window, implements CTRL-W s and :split
650 * "size" is the height or width for the new window, 0 to use half of current
651 * height or width.
653 * "flags":
654 * WSP_ROOM: require enough room for new window
655 * WSP_VERT: vertical split.
656 * WSP_TOP: open window at the top-left of the shell (help window).
657 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
658 * WSP_HELP: creating the help window, keep layout snapshot
660 * return FAIL for failure, OK otherwise
663 win_split(size, flags)
664 int size;
665 int flags;
667 /* When the ":tab" modifier was used open a new tab page instead. */
668 if (may_open_tabpage() == OK)
669 return OK;
671 /* Add flags from ":vertical", ":topleft" and ":botright". */
672 flags |= cmdmod.split;
673 if ((flags & WSP_TOP) && (flags & WSP_BOT))
675 EMSG(_("E442: Can't split topleft and botright at the same time"));
676 return FAIL;
679 /* When creating the help window make a snapshot of the window layout.
680 * Otherwise clear the snapshot, it's now invalid. */
681 if (flags & WSP_HELP)
682 make_snapshot();
683 else
684 clear_snapshot(curtab);
686 return win_split_ins(size, flags, NULL, 0);
690 * When "newwin" is NULL: split the current window in two.
691 * When "newwin" is not NULL: insert this window at the far
692 * top/left/right/bottom.
693 * return FAIL for failure, OK otherwise
695 static int
696 win_split_ins(size, flags, newwin, dir)
697 int size;
698 int flags;
699 win_T *newwin;
700 int dir;
702 win_T *wp = newwin;
703 win_T *oldwin;
704 int new_size = size;
705 int i;
706 int need_status = 0;
707 int do_equal = FALSE;
708 int needed;
709 int available;
710 int oldwin_height = 0;
711 int layout;
712 frame_T *frp, *curfrp;
713 int before;
715 if (flags & WSP_TOP)
716 oldwin = firstwin;
717 else if (flags & WSP_BOT)
718 oldwin = lastwin;
719 else
720 oldwin = curwin;
722 /* add a status line when p_ls == 1 and splitting the first window */
723 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
725 if (oldwin->w_height <= p_wmh && newwin == NULL)
727 EMSG(_(e_noroom));
728 return FAIL;
730 need_status = STATUS_HEIGHT;
733 #ifdef FEAT_GUI
734 /* May be needed for the scrollbars that are going to change. */
735 if (gui.in_use)
736 out_flush();
737 #endif
739 #ifdef FEAT_VERTSPLIT
740 if (flags & WSP_VERT)
742 layout = FR_ROW;
745 * Check if we are able to split the current window and compute its
746 * width.
748 needed = p_wmw + 1;
749 if (flags & WSP_ROOM)
750 needed += p_wiw - p_wmw;
751 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
753 available = topframe->fr_width;
754 needed += frame_minwidth(topframe, NULL);
756 else
757 available = oldwin->w_width;
758 if (available < needed && newwin == NULL)
760 EMSG(_(e_noroom));
761 return FAIL;
763 if (new_size == 0)
764 new_size = oldwin->w_width / 2;
765 if (new_size > oldwin->w_width - p_wmw - 1)
766 new_size = oldwin->w_width - p_wmw - 1;
767 if (new_size < p_wmw)
768 new_size = p_wmw;
770 /* if it doesn't fit in the current window, need win_equal() */
771 if (oldwin->w_width - new_size - 1 < p_wmw)
772 do_equal = TRUE;
774 /* We don't like to take lines for the new window from a
775 * 'winfixwidth' window. Take them from a window to the left or right
776 * instead, if possible. */
777 if (oldwin->w_p_wfw)
778 win_setwidth_win(oldwin->w_width + new_size, oldwin);
780 /* Only make all windows the same width if one of them (except oldwin)
781 * is wider than one of the split windows. */
782 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
783 && oldwin->w_frame->fr_parent != NULL)
785 frp = oldwin->w_frame->fr_parent->fr_child;
786 while (frp != NULL)
788 if (frp->fr_win != oldwin && frp->fr_win != NULL
789 && (frp->fr_win->w_width > new_size
790 || frp->fr_win->w_width > oldwin->w_width
791 - new_size - STATUS_HEIGHT))
793 do_equal = TRUE;
794 break;
796 frp = frp->fr_next;
800 else
801 #endif
803 layout = FR_COL;
806 * Check if we are able to split the current window and compute its
807 * height.
809 needed = p_wmh + STATUS_HEIGHT + need_status;
810 if (flags & WSP_ROOM)
811 needed += p_wh - p_wmh;
812 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
814 available = topframe->fr_height;
815 needed += frame_minheight(topframe, NULL);
817 else
819 available = oldwin->w_height;
820 needed += p_wmh;
822 if (available < needed && newwin == NULL)
824 EMSG(_(e_noroom));
825 return FAIL;
827 oldwin_height = oldwin->w_height;
828 if (need_status)
830 oldwin->w_status_height = STATUS_HEIGHT;
831 oldwin_height -= STATUS_HEIGHT;
833 if (new_size == 0)
834 new_size = oldwin_height / 2;
836 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
837 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
838 if (new_size < p_wmh)
839 new_size = p_wmh;
841 /* if it doesn't fit in the current window, need win_equal() */
842 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
843 do_equal = TRUE;
845 /* We don't like to take lines for the new window from a
846 * 'winfixheight' window. Take them from a window above or below
847 * instead, if possible. */
848 if (oldwin->w_p_wfh)
850 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
851 oldwin);
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 oldwin_height -= STATUS_HEIGHT;
857 /* Only make all windows the same height if one of them (except oldwin)
858 * is higher than one of the split windows. */
859 if (!do_equal && p_ea && size == 0
860 #ifdef FEAT_VERTSPLIT
861 && *p_ead != 'h'
862 #endif
863 && oldwin->w_frame->fr_parent != NULL)
865 frp = oldwin->w_frame->fr_parent->fr_child;
866 while (frp != NULL)
868 if (frp->fr_win != oldwin && frp->fr_win != NULL
869 && (frp->fr_win->w_height > new_size
870 || frp->fr_win->w_height > oldwin_height - new_size
871 - STATUS_HEIGHT))
873 do_equal = TRUE;
874 break;
876 frp = frp->fr_next;
882 * allocate new window structure and link it in the window list
884 if ((flags & WSP_TOP) == 0
885 && ((flags & WSP_BOT)
886 || (flags & WSP_BELOW)
887 || (!(flags & WSP_ABOVE)
888 && (
889 #ifdef FEAT_VERTSPLIT
890 (flags & WSP_VERT) ? p_spr :
891 #endif
892 p_sb))))
894 /* new window below/right of current one */
895 if (newwin == NULL)
896 wp = win_alloc(oldwin);
897 else
898 win_append(oldwin, wp);
900 else
902 if (newwin == NULL)
903 wp = win_alloc(oldwin->w_prev);
904 else
905 win_append(oldwin->w_prev, wp);
908 if (newwin == NULL)
910 if (wp == NULL)
911 return FAIL;
913 /* make the contents of the new window the same as the current one */
914 win_init(wp, curwin, flags);
918 * Reorganise the tree of frames to insert the new window.
920 if (flags & (WSP_TOP | WSP_BOT))
922 #ifdef FEAT_VERTSPLIT
923 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
924 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
925 #else
926 if (topframe->fr_layout == FR_COL)
927 #endif
929 curfrp = topframe->fr_child;
930 if (flags & WSP_BOT)
931 while (curfrp->fr_next != NULL)
932 curfrp = curfrp->fr_next;
934 else
935 curfrp = topframe;
936 before = (flags & WSP_TOP);
938 else
940 curfrp = oldwin->w_frame;
941 if (flags & WSP_BELOW)
942 before = FALSE;
943 else if (flags & WSP_ABOVE)
944 before = TRUE;
945 else
946 #ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
948 before = !p_spr;
949 else
950 #endif
951 before = !p_sb;
953 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
955 /* Need to create a new frame in the tree to make a branch. */
956 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
957 *frp = *curfrp;
958 curfrp->fr_layout = layout;
959 frp->fr_parent = curfrp;
960 frp->fr_next = NULL;
961 frp->fr_prev = NULL;
962 curfrp->fr_child = frp;
963 curfrp->fr_win = NULL;
964 curfrp = frp;
965 if (frp->fr_win != NULL)
966 oldwin->w_frame = frp;
967 else
968 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
969 frp->fr_parent = curfrp;
972 if (newwin == NULL)
974 /* Create a frame for the new window. */
975 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
976 frp->fr_layout = FR_LEAF;
977 frp->fr_win = wp;
978 wp->w_frame = frp;
980 else
981 frp = newwin->w_frame;
982 frp->fr_parent = curfrp->fr_parent;
984 /* Insert the new frame at the right place in the frame list. */
985 if (before)
986 frame_insert(curfrp, frp);
987 else
988 frame_append(curfrp, frp);
990 #ifdef FEAT_VERTSPLIT
991 if (flags & WSP_VERT)
993 wp->w_p_scr = curwin->w_p_scr;
994 if (need_status)
996 --oldwin->w_height;
997 oldwin->w_status_height = need_status;
999 if (flags & (WSP_TOP | WSP_BOT))
1001 /* set height and row of new window to full height */
1002 wp->w_winrow = tabline_height();
1003 wp->w_height = curfrp->fr_height - (p_ls > 0);
1004 wp->w_status_height = (p_ls > 0);
1006 else
1008 /* height and row of new window is same as current window */
1009 wp->w_winrow = oldwin->w_winrow;
1010 wp->w_height = oldwin->w_height;
1011 wp->w_status_height = oldwin->w_status_height;
1013 frp->fr_height = curfrp->fr_height;
1015 /* "new_size" of the current window goes to the new window, use
1016 * one column for the vertical separator */
1017 wp->w_width = new_size;
1018 if (before)
1019 wp->w_vsep_width = 1;
1020 else
1022 wp->w_vsep_width = oldwin->w_vsep_width;
1023 oldwin->w_vsep_width = 1;
1025 if (flags & (WSP_TOP | WSP_BOT))
1027 if (flags & WSP_BOT)
1028 frame_add_vsep(curfrp);
1029 /* Set width of neighbor frame */
1030 frame_new_width(curfrp, curfrp->fr_width
1031 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1032 FALSE);
1034 else
1035 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1036 if (before) /* new window left of current one */
1038 wp->w_wincol = oldwin->w_wincol;
1039 oldwin->w_wincol += new_size + 1;
1041 else /* new window right of current one */
1042 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1043 frame_fix_width(oldwin);
1044 frame_fix_width(wp);
1046 else
1047 #endif
1049 /* width and column of new window is same as current window */
1050 #ifdef FEAT_VERTSPLIT
1051 if (flags & (WSP_TOP | WSP_BOT))
1053 wp->w_wincol = 0;
1054 wp->w_width = Columns;
1055 wp->w_vsep_width = 0;
1057 else
1059 wp->w_wincol = oldwin->w_wincol;
1060 wp->w_width = oldwin->w_width;
1061 wp->w_vsep_width = oldwin->w_vsep_width;
1063 frp->fr_width = curfrp->fr_width;
1064 #endif
1066 /* "new_size" of the current window goes to the new window, use
1067 * one row for the status line */
1068 win_new_height(wp, new_size);
1069 if (flags & (WSP_TOP | WSP_BOT))
1070 frame_new_height(curfrp, curfrp->fr_height
1071 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1072 else
1073 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1074 if (before) /* new window above current one */
1076 wp->w_winrow = oldwin->w_winrow;
1077 wp->w_status_height = STATUS_HEIGHT;
1078 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1080 else /* new window below current one */
1082 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1083 wp->w_status_height = oldwin->w_status_height;
1084 oldwin->w_status_height = STATUS_HEIGHT;
1086 #ifdef FEAT_VERTSPLIT
1087 if (flags & WSP_BOT)
1088 frame_add_statusline(curfrp);
1089 #endif
1090 frame_fix_height(wp);
1091 frame_fix_height(oldwin);
1094 if (flags & (WSP_TOP | WSP_BOT))
1095 (void)win_comp_pos();
1098 * Both windows need redrawing
1100 redraw_win_later(wp, NOT_VALID);
1101 wp->w_redr_status = TRUE;
1102 redraw_win_later(oldwin, NOT_VALID);
1103 oldwin->w_redr_status = TRUE;
1105 if (need_status)
1107 msg_row = Rows - 1;
1108 msg_col = sc_col;
1109 msg_clr_eos_force(); /* Old command/ruler may still be there */
1110 comp_col();
1111 msg_row = Rows - 1;
1112 msg_col = 0; /* put position back at start of line */
1116 * make the new window the current window and redraw
1118 if (do_equal || dir != 0)
1119 win_equal(wp, TRUE,
1120 #ifdef FEAT_VERTSPLIT
1121 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1122 : dir == 'h' ? 'b' :
1123 #endif
1124 'v');
1126 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1127 * size was given. */
1128 #ifdef FEAT_VERTSPLIT
1129 if (flags & WSP_VERT)
1131 i = p_wiw;
1132 if (size != 0)
1133 p_wiw = size;
1135 # ifdef FEAT_GUI
1136 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1137 if (gui.in_use)
1138 gui_init_which_components(NULL);
1139 # endif
1141 else
1142 #endif
1144 i = p_wh;
1145 if (size != 0)
1146 p_wh = size;
1148 win_enter(wp, FALSE);
1149 #ifdef FEAT_VERTSPLIT
1150 if (flags & WSP_VERT)
1151 p_wiw = i;
1152 else
1153 #endif
1154 p_wh = i;
1156 return OK;
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
1163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
1166 /*ARGSUSED*/
1167 static void
1168 win_init(newp, oldp, flags)
1169 win_T *newp;
1170 win_T *oldp;
1171 int flags;
1173 int i;
1175 newp->w_buffer = oldp->w_buffer;
1176 oldp->w_buffer->b_nwindows++;
1177 newp->w_cursor = oldp->w_cursor;
1178 newp->w_valid = 0;
1179 newp->w_curswant = oldp->w_curswant;
1180 newp->w_set_curswant = oldp->w_set_curswant;
1181 newp->w_topline = oldp->w_topline;
1182 #ifdef FEAT_DIFF
1183 newp->w_topfill = oldp->w_topfill;
1184 #endif
1185 newp->w_leftcol = oldp->w_leftcol;
1186 newp->w_pcmark = oldp->w_pcmark;
1187 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1188 newp->w_alt_fnum = oldp->w_alt_fnum;
1189 newp->w_wrow = oldp->w_wrow;
1190 newp->w_fraction = oldp->w_fraction;
1191 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1192 #ifdef FEAT_JUMPLIST
1193 copy_jumplist(oldp, newp);
1194 #endif
1195 #ifdef FEAT_QUICKFIX
1196 if (flags & WSP_NEWLOC)
1198 /* Don't copy the location list. */
1199 newp->w_llist = NULL;
1200 newp->w_llist_ref = NULL;
1202 else
1203 copy_loclist(oldp, newp);
1204 #endif
1205 if (oldp->w_localdir != NULL)
1206 newp->w_localdir = vim_strsave(oldp->w_localdir);
1208 /* Use the same argument list. */
1209 newp->w_alist = oldp->w_alist;
1210 ++newp->w_alist->al_refcount;
1211 newp->w_arg_idx = oldp->w_arg_idx;
1214 * copy tagstack and options from existing window
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 win_copy_options(oldp, newp);
1226 # ifdef FEAT_FOLDING
1227 copyFoldingState(oldp, newp);
1228 # endif
1231 #endif /* FEAT_WINDOWS */
1233 #if defined(FEAT_WINDOWS) || defined(PROTO)
1235 * Check if "win" is a pointer to an existing window.
1238 win_valid(win)
1239 win_T *win;
1241 win_T *wp;
1243 if (win == NULL)
1244 return FALSE;
1245 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1246 if (wp == win)
1247 return TRUE;
1248 return FALSE;
1252 * Return the number of windows.
1255 win_count()
1257 win_T *wp;
1258 int count = 0;
1260 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1261 ++count;
1262 return count;
1266 * Make "count" windows on the screen.
1267 * Return actual number of windows on the screen.
1268 * Must be called when there is just one window, filling the whole screen
1269 * (excluding the command line).
1271 /*ARGSUSED*/
1273 make_windows(count, vertical)
1274 int count;
1275 int vertical; /* split windows vertically if TRUE */
1277 int maxcount;
1278 int todo;
1280 #ifdef FEAT_VERTSPLIT
1281 if (vertical)
1283 /* Each windows needs at least 'winminwidth' lines and a separator
1284 * column. */
1285 maxcount = (curwin->w_width + curwin->w_vsep_width
1286 - (p_wiw - p_wmw)) / (p_wmw + 1);
1288 else
1289 #endif
1291 /* Each window needs at least 'winminheight' lines and a status line. */
1292 maxcount = (curwin->w_height + curwin->w_status_height
1293 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1296 if (maxcount < 2)
1297 maxcount = 2;
1298 if (count > maxcount)
1299 count = maxcount;
1302 * add status line now, otherwise first window will be too big
1304 if (count > 1)
1305 last_status(TRUE);
1307 #ifdef FEAT_AUTOCMD
1309 * Don't execute autocommands while creating the windows. Must do that
1310 * when putting the buffers in the windows.
1312 block_autocmds();
1313 #endif
1315 /* todo is number of windows left to create */
1316 for (todo = count - 1; todo > 0; --todo)
1317 #ifdef FEAT_VERTSPLIT
1318 if (vertical)
1320 if (win_split(curwin->w_width - (curwin->w_width - todo)
1321 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1322 break;
1324 else
1325 #endif
1327 if (win_split(curwin->w_height - (curwin->w_height - todo
1328 * STATUS_HEIGHT) / (todo + 1)
1329 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1330 break;
1333 #ifdef FEAT_AUTOCMD
1334 unblock_autocmds();
1335 #endif
1337 /* return actual number of windows */
1338 return (count - todo);
1342 * Exchange current and next window
1344 static void
1345 win_exchange(Prenum)
1346 long Prenum;
1348 frame_T *frp;
1349 frame_T *frp2;
1350 win_T *wp;
1351 win_T *wp2;
1352 int temp;
1354 if (lastwin == firstwin) /* just one window */
1356 beep_flush();
1357 return;
1360 #ifdef FEAT_GUI
1361 need_mouse_correct = TRUE;
1362 #endif
1365 * find window to exchange with
1367 if (Prenum)
1369 frp = curwin->w_frame->fr_parent->fr_child;
1370 while (frp != NULL && --Prenum > 0)
1371 frp = frp->fr_next;
1373 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1374 frp = curwin->w_frame->fr_next;
1375 else /* Swap last window in row/col with previous */
1376 frp = curwin->w_frame->fr_prev;
1378 /* We can only exchange a window with another window, not with a frame
1379 * containing windows. */
1380 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1381 return;
1382 wp = frp->fr_win;
1385 * 1. remove curwin from the list. Remember after which window it was in wp2
1386 * 2. insert curwin before wp in the list
1387 * if wp != wp2
1388 * 3. remove wp from the list
1389 * 4. insert wp after wp2
1390 * 5. exchange the status line height and vsep width.
1392 wp2 = curwin->w_prev;
1393 frp2 = curwin->w_frame->fr_prev;
1394 if (wp->w_prev != curwin)
1396 win_remove(curwin, NULL);
1397 frame_remove(curwin->w_frame);
1398 win_append(wp->w_prev, curwin);
1399 frame_insert(frp, curwin->w_frame);
1401 if (wp != wp2)
1403 win_remove(wp, NULL);
1404 frame_remove(wp->w_frame);
1405 win_append(wp2, wp);
1406 if (frp2 == NULL)
1407 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1408 else
1409 frame_append(frp2, wp->w_frame);
1411 temp = curwin->w_status_height;
1412 curwin->w_status_height = wp->w_status_height;
1413 wp->w_status_height = temp;
1414 #ifdef FEAT_VERTSPLIT
1415 temp = curwin->w_vsep_width;
1416 curwin->w_vsep_width = wp->w_vsep_width;
1417 wp->w_vsep_width = temp;
1419 /* If the windows are not in the same frame, exchange the sizes to avoid
1420 * messing up the window layout. Otherwise fix the frame sizes. */
1421 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1423 temp = curwin->w_height;
1424 curwin->w_height = wp->w_height;
1425 wp->w_height = temp;
1426 temp = curwin->w_width;
1427 curwin->w_width = wp->w_width;
1428 wp->w_width = temp;
1430 else
1432 frame_fix_height(curwin);
1433 frame_fix_height(wp);
1434 frame_fix_width(curwin);
1435 frame_fix_width(wp);
1437 #endif
1439 (void)win_comp_pos(); /* recompute window positions */
1441 win_enter(wp, TRUE);
1442 redraw_later(CLEAR);
1446 * rotate windows: if upwards TRUE the second window becomes the first one
1447 * if upwards FALSE the first window becomes the second one
1449 static void
1450 win_rotate(upwards, count)
1451 int upwards;
1452 int count;
1454 win_T *wp1;
1455 win_T *wp2;
1456 frame_T *frp;
1457 int n;
1459 if (firstwin == lastwin) /* nothing to do */
1461 beep_flush();
1462 return;
1465 #ifdef FEAT_GUI
1466 need_mouse_correct = TRUE;
1467 #endif
1469 #ifdef FEAT_VERTSPLIT
1470 /* Check if all frames in this row/col have one window. */
1471 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1472 frp = frp->fr_next)
1473 if (frp->fr_win == NULL)
1475 EMSG(_("E443: Cannot rotate when another window is split"));
1476 return;
1478 #endif
1480 while (count--)
1482 if (upwards) /* first window becomes last window */
1484 /* remove first window/frame from the list */
1485 frp = curwin->w_frame->fr_parent->fr_child;
1486 wp1 = frp->fr_win;
1487 win_remove(wp1, NULL);
1488 frame_remove(frp);
1490 /* find last frame and append removed window/frame after it */
1491 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1493 win_append(frp->fr_win, wp1);
1494 frame_append(frp, wp1->w_frame);
1496 wp2 = frp->fr_win; /* previously last window */
1498 else /* last window becomes first window */
1500 /* find last window/frame in the list and remove it */
1501 for (frp = curwin->w_frame; frp->fr_next != NULL;
1502 frp = frp->fr_next)
1504 wp1 = frp->fr_win;
1505 wp2 = wp1->w_prev; /* will become last window */
1506 win_remove(wp1, NULL);
1507 frame_remove(frp);
1509 /* append the removed window/frame before the first in the list */
1510 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1511 frame_insert(frp->fr_parent->fr_child, frp);
1514 /* exchange status height and vsep width of old and new last window */
1515 n = wp2->w_status_height;
1516 wp2->w_status_height = wp1->w_status_height;
1517 wp1->w_status_height = n;
1518 frame_fix_height(wp1);
1519 frame_fix_height(wp2);
1520 #ifdef FEAT_VERTSPLIT
1521 n = wp2->w_vsep_width;
1522 wp2->w_vsep_width = wp1->w_vsep_width;
1523 wp1->w_vsep_width = n;
1524 frame_fix_width(wp1);
1525 frame_fix_width(wp2);
1526 #endif
1528 /* recompute w_winrow and w_wincol for all windows */
1529 (void)win_comp_pos();
1532 redraw_later(CLEAR);
1536 * Move the current window to the very top/bottom/left/right of the screen.
1538 static void
1539 win_totop(size, flags)
1540 int size;
1541 int flags;
1543 int dir;
1544 int height = curwin->w_height;
1546 if (lastwin == firstwin)
1548 beep_flush();
1549 return;
1552 /* Remove the window and frame from the tree of frames. */
1553 (void)winframe_remove(curwin, &dir, NULL);
1554 win_remove(curwin, NULL);
1555 last_status(FALSE); /* may need to remove last status line */
1556 (void)win_comp_pos(); /* recompute window positions */
1558 /* Split a window on the desired side and put the window there. */
1559 (void)win_split_ins(size, flags, curwin, dir);
1560 if (!(flags & WSP_VERT))
1562 win_setheight(height);
1563 if (p_ea)
1564 win_equal(curwin, TRUE, 'v');
1567 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1568 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1569 * scrollbars. Have to update them anyway. */
1570 if (gui.in_use)
1572 out_flush();
1573 gui_init_which_components(NULL);
1574 gui_update_scrollbars(TRUE);
1576 need_mouse_correct = TRUE;
1577 #endif
1582 * Move window "win1" to below/right of "win2" and make "win1" the current
1583 * window. Only works within the same frame!
1585 void
1586 win_move_after(win1, win2)
1587 win_T *win1, *win2;
1589 int height;
1591 /* check if the arguments are reasonable */
1592 if (win1 == win2)
1593 return;
1595 /* check if there is something to do */
1596 if (win2->w_next != win1)
1598 /* may need move the status line/vertical separator of the last window
1599 * */
1600 if (win1 == lastwin)
1602 height = win1->w_prev->w_status_height;
1603 win1->w_prev->w_status_height = win1->w_status_height;
1604 win1->w_status_height = height;
1605 #ifdef FEAT_VERTSPLIT
1606 if (win1->w_prev->w_vsep_width == 1)
1608 /* Remove the vertical separator from the last-but-one window,
1609 * add it to the last window. Adjust the frame widths. */
1610 win1->w_prev->w_vsep_width = 0;
1611 win1->w_prev->w_frame->fr_width -= 1;
1612 win1->w_vsep_width = 1;
1613 win1->w_frame->fr_width += 1;
1615 #endif
1617 else if (win2 == lastwin)
1619 height = win1->w_status_height;
1620 win1->w_status_height = win2->w_status_height;
1621 win2->w_status_height = height;
1622 #ifdef FEAT_VERTSPLIT
1623 if (win1->w_vsep_width == 1)
1625 /* Remove the vertical separator from win1, add it to the last
1626 * window, win2. Adjust the frame widths. */
1627 win2->w_vsep_width = 1;
1628 win2->w_frame->fr_width += 1;
1629 win1->w_vsep_width = 0;
1630 win1->w_frame->fr_width -= 1;
1632 #endif
1634 win_remove(win1, NULL);
1635 frame_remove(win1->w_frame);
1636 win_append(win2, win1);
1637 frame_append(win2->w_frame, win1->w_frame);
1639 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1640 redraw_later(NOT_VALID);
1642 win_enter(win1, FALSE);
1646 * Make all windows the same height.
1647 * 'next_curwin' will soon be the current window, make sure it has enough
1648 * rows.
1650 void
1651 win_equal(next_curwin, current, dir)
1652 win_T *next_curwin; /* pointer to current window to be or NULL */
1653 int current; /* do only frame with current window */
1654 int dir; /* 'v' for vertically, 'h' for horizontally,
1655 'b' for both, 0 for using p_ead */
1657 if (dir == 0)
1658 #ifdef FEAT_VERTSPLIT
1659 dir = *p_ead;
1660 #else
1661 dir = 'b';
1662 #endif
1663 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1664 topframe, dir, 0, tabline_height(),
1665 (int)Columns, topframe->fr_height);
1669 * Set a frame to a new position and height, spreading the available room
1670 * equally over contained frames.
1671 * The window "next_curwin" (if not NULL) should at least get the size from
1672 * 'winheight' and 'winwidth' if possible.
1674 static void
1675 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1676 win_T *next_curwin; /* pointer to current window to be or NULL */
1677 int current; /* do only frame with current window */
1678 frame_T *topfr; /* frame to set size off */
1679 int dir; /* 'v', 'h' or 'b', see win_equal() */
1680 int col; /* horizontal position for frame */
1681 int row; /* vertical position for frame */
1682 int width; /* new width of frame */
1683 int height; /* new height of frame */
1685 int n, m;
1686 int extra_sep = 0;
1687 int wincount, totwincount = 0;
1688 frame_T *fr;
1689 int next_curwin_size = 0;
1690 int room = 0;
1691 int new_size;
1692 int has_next_curwin = 0;
1693 int hnc;
1695 if (topfr->fr_layout == FR_LEAF)
1697 /* Set the width/height of this frame.
1698 * Redraw when size or position changes */
1699 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1700 #ifdef FEAT_VERTSPLIT
1701 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1702 #endif
1705 topfr->fr_win->w_winrow = row;
1706 frame_new_height(topfr, height, FALSE, FALSE);
1707 #ifdef FEAT_VERTSPLIT
1708 topfr->fr_win->w_wincol = col;
1709 frame_new_width(topfr, width, FALSE, FALSE);
1710 #endif
1711 redraw_all_later(CLEAR);
1714 #ifdef FEAT_VERTSPLIT
1715 else if (topfr->fr_layout == FR_ROW)
1717 topfr->fr_width = width;
1718 topfr->fr_height = height;
1720 if (dir != 'v') /* equalize frame widths */
1722 /* Compute the maximum number of windows horizontally in this
1723 * frame. */
1724 n = frame_minwidth(topfr, NOWIN);
1725 /* add one for the rightmost window, it doesn't have a separator */
1726 if (col + width == Columns)
1727 extra_sep = 1;
1728 else
1729 extra_sep = 0;
1730 totwincount = (n + extra_sep) / (p_wmw + 1);
1731 has_next_curwin = frame_has_win(topfr, next_curwin);
1734 * Compute width for "next_curwin" window and room available for
1735 * other windows.
1736 * "m" is the minimal width when counting p_wiw for "next_curwin".
1738 m = frame_minwidth(topfr, next_curwin);
1739 room = width - m;
1740 if (room < 0)
1742 next_curwin_size = p_wiw + room;
1743 room = 0;
1745 else
1747 next_curwin_size = -1;
1748 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1750 /* If 'winfixwidth' set keep the window width if
1751 * possible.
1752 * Watch out for this window being the next_curwin. */
1753 if (frame_fixed_width(fr))
1755 n = frame_minwidth(fr, NOWIN);
1756 new_size = fr->fr_width;
1757 if (frame_has_win(fr, next_curwin))
1759 room += p_wiw - p_wmw;
1760 next_curwin_size = 0;
1761 if (new_size < p_wiw)
1762 new_size = p_wiw;
1764 else
1765 /* These windows don't use up room. */
1766 totwincount -= (n + (fr->fr_next == NULL
1767 ? extra_sep : 0)) / (p_wmw + 1);
1768 room -= new_size - n;
1769 if (room < 0)
1771 new_size += room;
1772 room = 0;
1774 fr->fr_newwidth = new_size;
1777 if (next_curwin_size == -1)
1779 if (!has_next_curwin)
1780 next_curwin_size = 0;
1781 else if (totwincount > 1
1782 && (room + (totwincount - 2))
1783 / (totwincount - 1) > p_wiw)
1785 /* Can make all windows wider than 'winwidth', spread
1786 * the room equally. */
1787 next_curwin_size = (room + p_wiw
1788 + (totwincount - 1) * p_wmw
1789 + (totwincount - 1)) / totwincount;
1790 room -= next_curwin_size - p_wiw;
1792 else
1793 next_curwin_size = p_wiw;
1797 if (has_next_curwin)
1798 --totwincount; /* don't count curwin */
1801 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1803 n = m = 0;
1804 wincount = 1;
1805 if (fr->fr_next == NULL)
1806 /* last frame gets all that remains (avoid roundoff error) */
1807 new_size = width;
1808 else if (dir == 'v')
1809 new_size = fr->fr_width;
1810 else if (frame_fixed_width(fr))
1812 new_size = fr->fr_newwidth;
1813 wincount = 0; /* doesn't count as a sizeable window */
1815 else
1817 /* Compute the maximum number of windows horiz. in "fr". */
1818 n = frame_minwidth(fr, NOWIN);
1819 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1820 / (p_wmw + 1);
1821 m = frame_minwidth(fr, next_curwin);
1822 if (has_next_curwin)
1823 hnc = frame_has_win(fr, next_curwin);
1824 else
1825 hnc = FALSE;
1826 if (hnc) /* don't count next_curwin */
1827 --wincount;
1828 if (totwincount == 0)
1829 new_size = room;
1830 else
1831 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1832 / totwincount;
1833 if (hnc) /* add next_curwin size */
1835 next_curwin_size -= p_wiw - (m - n);
1836 new_size += next_curwin_size;
1837 room -= new_size - next_curwin_size;
1839 else
1840 room -= new_size;
1841 new_size += n;
1844 /* Skip frame that is full width when splitting or closing a
1845 * window, unless equalizing all frames. */
1846 if (!current || dir != 'v' || topfr->fr_parent != NULL
1847 || (new_size != fr->fr_width)
1848 || frame_has_win(fr, next_curwin))
1849 win_equal_rec(next_curwin, current, fr, dir, col, row,
1850 new_size, height);
1851 col += new_size;
1852 width -= new_size;
1853 totwincount -= wincount;
1856 #endif
1857 else /* topfr->fr_layout == FR_COL */
1859 #ifdef FEAT_VERTSPLIT
1860 topfr->fr_width = width;
1861 #endif
1862 topfr->fr_height = height;
1864 if (dir != 'h') /* equalize frame heights */
1866 /* Compute maximum number of windows vertically in this frame. */
1867 n = frame_minheight(topfr, NOWIN);
1868 /* add one for the bottom window if it doesn't have a statusline */
1869 if (row + height == cmdline_row && p_ls == 0)
1870 extra_sep = 1;
1871 else
1872 extra_sep = 0;
1873 totwincount = (n + extra_sep) / (p_wmh + 1);
1874 has_next_curwin = frame_has_win(topfr, next_curwin);
1877 * Compute height for "next_curwin" window and room available for
1878 * other windows.
1879 * "m" is the minimal height when counting p_wh for "next_curwin".
1881 m = frame_minheight(topfr, next_curwin);
1882 room = height - m;
1883 if (room < 0)
1885 /* The room is less then 'winheight', use all space for the
1886 * current window. */
1887 next_curwin_size = p_wh + room;
1888 room = 0;
1890 else
1892 next_curwin_size = -1;
1893 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1895 /* If 'winfixheight' set keep the window height if
1896 * possible.
1897 * Watch out for this window being the next_curwin. */
1898 if (frame_fixed_height(fr))
1900 n = frame_minheight(fr, NOWIN);
1901 new_size = fr->fr_height;
1902 if (frame_has_win(fr, next_curwin))
1904 room += p_wh - p_wmh;
1905 next_curwin_size = 0;
1906 if (new_size < p_wh)
1907 new_size = p_wh;
1909 else
1910 /* These windows don't use up room. */
1911 totwincount -= (n + (fr->fr_next == NULL
1912 ? extra_sep : 0)) / (p_wmh + 1);
1913 room -= new_size - n;
1914 if (room < 0)
1916 new_size += room;
1917 room = 0;
1919 fr->fr_newheight = new_size;
1922 if (next_curwin_size == -1)
1924 if (!has_next_curwin)
1925 next_curwin_size = 0;
1926 else if (totwincount > 1
1927 && (room + (totwincount - 2))
1928 / (totwincount - 1) > p_wh)
1930 /* can make all windows higher than 'winheight',
1931 * spread the room equally. */
1932 next_curwin_size = (room + p_wh
1933 + (totwincount - 1) * p_wmh
1934 + (totwincount - 1)) / totwincount;
1935 room -= next_curwin_size - p_wh;
1937 else
1938 next_curwin_size = p_wh;
1942 if (has_next_curwin)
1943 --totwincount; /* don't count curwin */
1946 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1948 n = m = 0;
1949 wincount = 1;
1950 if (fr->fr_next == NULL)
1951 /* last frame gets all that remains (avoid roundoff error) */
1952 new_size = height;
1953 else if (dir == 'h')
1954 new_size = fr->fr_height;
1955 else if (frame_fixed_height(fr))
1957 new_size = fr->fr_newheight;
1958 wincount = 0; /* doesn't count as a sizeable window */
1960 else
1962 /* Compute the maximum number of windows vert. in "fr". */
1963 n = frame_minheight(fr, NOWIN);
1964 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1965 / (p_wmh + 1);
1966 m = frame_minheight(fr, next_curwin);
1967 if (has_next_curwin)
1968 hnc = frame_has_win(fr, next_curwin);
1969 else
1970 hnc = FALSE;
1971 if (hnc) /* don't count next_curwin */
1972 --wincount;
1973 if (totwincount == 0)
1974 new_size = room;
1975 else
1976 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1977 / totwincount;
1978 if (hnc) /* add next_curwin size */
1980 next_curwin_size -= p_wh - (m - n);
1981 new_size += next_curwin_size;
1982 room -= new_size - next_curwin_size;
1984 else
1985 room -= new_size;
1986 new_size += n;
1988 /* Skip frame that is full width when splitting or closing a
1989 * window, unless equalizing all frames. */
1990 if (!current || dir != 'h' || topfr->fr_parent != NULL
1991 || (new_size != fr->fr_height)
1992 || frame_has_win(fr, next_curwin))
1993 win_equal_rec(next_curwin, current, fr, dir, col, row,
1994 width, new_size);
1995 row += new_size;
1996 height -= new_size;
1997 totwincount -= wincount;
2003 * close all windows for buffer 'buf'
2005 void
2006 close_windows(buf, keep_curwin)
2007 buf_T *buf;
2008 int keep_curwin; /* don't close "curwin" */
2010 win_T *wp;
2011 tabpage_T *tp, *nexttp;
2012 int h = tabline_height();
2014 ++RedrawingDisabled;
2016 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2018 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2020 win_close(wp, FALSE);
2022 /* Start all over, autocommands may change the window layout. */
2023 wp = firstwin;
2025 else
2026 wp = wp->w_next;
2029 /* Also check windows in other tab pages. */
2030 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2032 nexttp = tp->tp_next;
2033 if (tp != curtab)
2034 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2035 if (wp->w_buffer == buf)
2037 win_close_othertab(wp, FALSE, tp);
2039 /* Start all over, the tab page may be closed and
2040 * autocommands may change the window layout. */
2041 nexttp = first_tabpage;
2042 break;
2046 --RedrawingDisabled;
2048 if (h != tabline_height())
2049 shell_new_rows();
2053 * Return TRUE if the current window is the only window that exists.
2054 * Returns FALSE if there is a window, possibly in another tab page.
2056 static int
2057 last_window()
2059 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2063 * Close window "win". Only works for the current tab page.
2064 * If "free_buf" is TRUE related buffer may be unloaded.
2066 * called by :quit, :close, :xit, :wq and findtag()
2068 void
2069 win_close(win, free_buf)
2070 win_T *win;
2071 int free_buf;
2073 win_T *wp;
2074 #ifdef FEAT_AUTOCMD
2075 int other_buffer = FALSE;
2076 #endif
2077 int close_curwin = FALSE;
2078 int dir;
2079 int help_window = FALSE;
2080 tabpage_T *prev_curtab = curtab;
2082 if (last_window())
2084 EMSG(_("E444: Cannot close last window"));
2085 return;
2089 * When closing the last window in a tab page first go to another tab
2090 * page and then close the window and the tab page. This avoids that
2091 * curwin and curtab are not invalid while we are freeing memory, they may
2092 * be used in GUI events.
2094 if (firstwin == lastwin)
2096 goto_tabpage_tp(alt_tabpage());
2097 redraw_tabline = TRUE;
2099 /* Safety check: Autocommands may have closed the window when jumping
2100 * to the other tab page. */
2101 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2103 int h = tabline_height();
2105 win_close_othertab(win, free_buf, prev_curtab);
2106 if (h != tabline_height())
2107 shell_new_rows();
2109 return;
2112 /* When closing the help window, try restoring a snapshot after closing
2113 * the window. Otherwise clear the snapshot, it's now invalid. */
2114 if (win->w_buffer->b_help)
2115 help_window = TRUE;
2116 else
2117 clear_snapshot(curtab);
2119 #ifdef FEAT_AUTOCMD
2120 if (win == curwin)
2123 * Guess which window is going to be the new current window.
2124 * This may change because of the autocommands (sigh).
2126 wp = frame2win(win_altframe(win, NULL));
2129 * Be careful: If autocommands delete the window, return now.
2131 if (wp->w_buffer != curbuf)
2133 other_buffer = TRUE;
2134 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2135 if (!win_valid(win) || last_window())
2136 return;
2138 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2139 if (!win_valid(win) || last_window())
2140 return;
2141 # ifdef FEAT_EVAL
2142 /* autocmds may abort script processing */
2143 if (aborting())
2144 return;
2145 # endif
2147 #endif
2149 #ifdef FEAT_GUI
2150 /* Avoid trouble with scrollbars that are going to be deleted in
2151 * win_free(). */
2152 if (gui.in_use)
2153 out_flush();
2154 #endif
2157 * Close the link to the buffer.
2159 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2161 /* Autocommands may have closed the window already, or closed the only
2162 * other window or moved to another tab page. */
2163 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2164 return;
2166 /* Free the memory used for the window. */
2167 wp = win_free_mem(win, &dir, NULL);
2169 /* Make sure curwin isn't invalid. It can cause severe trouble when
2170 * printing an error message. For win_equal() curbuf needs to be valid
2171 * too. */
2172 if (win == curwin)
2174 curwin = wp;
2175 #ifdef FEAT_QUICKFIX
2176 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2179 * If the cursor goes to the preview or the quickfix window, try
2180 * finding another window to go to.
2182 for (;;)
2184 if (wp->w_next == NULL)
2185 wp = firstwin;
2186 else
2187 wp = wp->w_next;
2188 if (wp == curwin)
2189 break;
2190 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2192 curwin = wp;
2193 break;
2197 #endif
2198 curbuf = curwin->w_buffer;
2199 close_curwin = TRUE;
2201 if (p_ea
2202 #ifdef FEAT_VERTSPLIT
2203 && (*p_ead == 'b' || *p_ead == dir)
2204 #endif
2206 win_equal(curwin, TRUE,
2207 #ifdef FEAT_VERTSPLIT
2209 #else
2211 #endif
2213 else
2214 win_comp_pos();
2215 if (close_curwin)
2217 win_enter_ext(wp, FALSE, TRUE);
2218 #ifdef FEAT_AUTOCMD
2219 if (other_buffer)
2220 /* careful: after this wp and win may be invalid! */
2221 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2222 #endif
2226 * If last window has a status line now and we don't want one,
2227 * remove the status line.
2229 last_status(FALSE);
2231 /* After closing the help window, try restoring the window layout from
2232 * before it was opened. */
2233 if (help_window)
2234 restore_snapshot(close_curwin);
2236 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2237 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2238 if (gui.in_use && !win_hasvertsplit())
2239 gui_init_which_components(NULL);
2240 #endif
2242 redraw_all_later(NOT_VALID);
2246 * Close window "win" in tab page "tp", which is not the current tab page.
2247 * This may be the last window ih that tab page and result in closing the tab,
2248 * thus "tp" may become invalid!
2249 * Caller must check if buffer is hidden and whether the tabline needs to be
2250 * updated.
2252 void
2253 win_close_othertab(win, free_buf, tp)
2254 win_T *win;
2255 int free_buf;
2256 tabpage_T *tp;
2258 win_T *wp;
2259 int dir;
2260 tabpage_T *ptp = NULL;
2262 /* Close the link to the buffer. */
2263 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2265 /* Careful: Autocommands may have closed the tab page or made it the
2266 * current tab page. */
2267 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2269 if (ptp == NULL || tp == curtab)
2270 return;
2272 /* Autocommands may have closed the window already. */
2273 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2275 if (wp == NULL)
2276 return;
2278 /* Free the memory used for the window. */
2279 wp = win_free_mem(win, &dir, tp);
2281 /* When closing the last window in a tab page remove the tab page. */
2282 if (wp == NULL)
2284 if (tp == first_tabpage)
2285 first_tabpage = tp->tp_next;
2286 else
2288 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2289 ptp = ptp->tp_next)
2291 if (ptp == NULL)
2293 EMSG2(_(e_intern2), "win_close_othertab()");
2294 return;
2296 ptp->tp_next = tp->tp_next;
2298 free_tabpage(tp);
2303 * Free the memory used for a window.
2304 * Returns a pointer to the window that got the freed up space.
2306 static win_T *
2307 win_free_mem(win, dirp, tp)
2308 win_T *win;
2309 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2310 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2312 frame_T *frp;
2313 win_T *wp;
2315 #ifdef FEAT_FOLDING
2316 clearFolding(win);
2317 #endif
2319 /* reduce the reference count to the argument list. */
2320 alist_unlink(win->w_alist);
2322 /* Remove the window and its frame from the tree of frames. */
2323 frp = win->w_frame;
2324 wp = winframe_remove(win, dirp, tp);
2325 vim_free(frp);
2326 win_free(win, tp);
2328 /* When deleting the current window of another tab page select a new
2329 * current window. */
2330 if (tp != NULL && win == tp->tp_curwin)
2331 tp->tp_curwin = wp;
2333 return wp;
2336 #if defined(EXITFREE) || defined(PROTO)
2337 void
2338 win_free_all()
2340 int dummy;
2342 # ifdef FEAT_WINDOWS
2343 while (first_tabpage->tp_next != NULL)
2344 tabpage_close(TRUE);
2345 # endif
2347 while (firstwin != NULL)
2348 (void)win_free_mem(firstwin, &dummy, NULL);
2350 #endif
2353 * Remove a window and its frame from the tree of frames.
2354 * Returns a pointer to the window that got the freed up space.
2356 /*ARGSUSED*/
2357 static win_T *
2358 winframe_remove(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, *frp2, *frp3;
2364 frame_T *frp_close = win->w_frame;
2365 win_T *wp;
2368 * If there is only one window there is nothing to remove.
2370 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2371 return NULL;
2374 * Remove the window from its frame.
2376 frp2 = win_altframe(win, tp);
2377 wp = frame2win(frp2);
2379 /* Remove this frame from the list of frames. */
2380 frame_remove(frp_close);
2382 #ifdef FEAT_VERTSPLIT
2383 if (frp_close->fr_parent->fr_layout == FR_COL)
2385 #endif
2386 /* When 'winfixheight' is set, try to find another frame in the column
2387 * (as close to the closed frame as possible) to distribute the height
2388 * to. */
2389 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2391 frp = frp_close->fr_prev;
2392 frp3 = frp_close->fr_next;
2393 while (frp != NULL || frp3 != NULL)
2395 if (frp != NULL)
2397 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2399 frp2 = frp;
2400 wp = frp->fr_win;
2401 break;
2403 frp = frp->fr_prev;
2405 if (frp3 != NULL)
2407 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2409 frp2 = frp3;
2410 wp = frp3->fr_win;
2411 break;
2413 frp3 = frp3->fr_next;
2417 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2418 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2419 #ifdef FEAT_VERTSPLIT
2420 *dirp = 'v';
2422 else
2424 /* When 'winfixwidth' is set, try to find another frame in the column
2425 * (as close to the closed frame as possible) to distribute the width
2426 * to. */
2427 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2429 frp = frp_close->fr_prev;
2430 frp3 = frp_close->fr_next;
2431 while (frp != NULL || frp3 != NULL)
2433 if (frp != NULL)
2435 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2437 frp2 = frp;
2438 wp = frp->fr_win;
2439 break;
2441 frp = frp->fr_prev;
2443 if (frp3 != NULL)
2445 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2447 frp2 = frp3;
2448 wp = frp3->fr_win;
2449 break;
2451 frp3 = frp3->fr_next;
2455 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2456 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2457 *dirp = 'h';
2459 #endif
2461 /* If rows/columns go to a window below/right its positions need to be
2462 * updated. Can only be done after the sizes have been updated. */
2463 if (frp2 == frp_close->fr_next)
2465 int row = win->w_winrow;
2466 int col = W_WINCOL(win);
2468 frame_comp_pos(frp2, &row, &col);
2471 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2473 /* There is no other frame in this list, move its info to the parent
2474 * and remove it. */
2475 frp2->fr_parent->fr_layout = frp2->fr_layout;
2476 frp2->fr_parent->fr_child = frp2->fr_child;
2477 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2478 frp->fr_parent = frp2->fr_parent;
2479 frp2->fr_parent->fr_win = frp2->fr_win;
2480 if (frp2->fr_win != NULL)
2481 frp2->fr_win->w_frame = frp2->fr_parent;
2482 frp = frp2->fr_parent;
2483 vim_free(frp2);
2485 frp2 = frp->fr_parent;
2486 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2488 /* The frame above the parent has the same layout, have to merge
2489 * the frames into this list. */
2490 if (frp2->fr_child == frp)
2491 frp2->fr_child = frp->fr_child;
2492 frp->fr_child->fr_prev = frp->fr_prev;
2493 if (frp->fr_prev != NULL)
2494 frp->fr_prev->fr_next = frp->fr_child;
2495 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2497 frp3->fr_parent = frp2;
2498 if (frp3->fr_next == NULL)
2500 frp3->fr_next = frp->fr_next;
2501 if (frp->fr_next != NULL)
2502 frp->fr_next->fr_prev = frp3;
2503 break;
2506 vim_free(frp);
2510 return wp;
2514 * Find out which frame is going to get the freed up space when "win" is
2515 * closed.
2516 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2517 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2518 * This makes opening a window and closing it immediately keep the same window
2519 * layout.
2521 static frame_T *
2522 win_altframe(win, tp)
2523 win_T *win;
2524 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2526 frame_T *frp;
2527 int b;
2529 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2530 /* Last window in this tab page, will go to next tab page. */
2531 return alt_tabpage()->tp_curwin->w_frame;
2533 frp = win->w_frame;
2534 #ifdef FEAT_VERTSPLIT
2535 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2536 b = p_spr;
2537 else
2538 #endif
2539 b = p_sb;
2540 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2541 return frp->fr_next;
2542 return frp->fr_prev;
2546 * Return the tabpage that will be used if the current one is closed.
2548 static tabpage_T *
2549 alt_tabpage()
2551 tabpage_T *tp;
2553 /* Use the next tab page if possible. */
2554 if (curtab->tp_next != NULL)
2555 return curtab->tp_next;
2557 /* Find the last but one tab page. */
2558 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2560 return tp;
2564 * Find the left-upper window in frame "frp".
2566 static win_T *
2567 frame2win(frp)
2568 frame_T *frp;
2570 while (frp->fr_win == NULL)
2571 frp = frp->fr_child;
2572 return frp->fr_win;
2576 * Return TRUE if frame "frp" contains window "wp".
2578 static int
2579 frame_has_win(frp, wp)
2580 frame_T *frp;
2581 win_T *wp;
2583 frame_T *p;
2585 if (frp->fr_layout == FR_LEAF)
2586 return frp->fr_win == wp;
2588 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2589 if (frame_has_win(p, wp))
2590 return TRUE;
2591 return FALSE;
2595 * Set a new height for a frame. Recursively sets the height for contained
2596 * frames and windows. Caller must take care of positions.
2598 static void
2599 frame_new_height(topfrp, height, topfirst, wfh)
2600 frame_T *topfrp;
2601 int height;
2602 int topfirst; /* resize topmost contained frame first */
2603 int wfh; /* obey 'winfixheight' when there is a choice;
2604 may cause the height not to be set */
2606 frame_T *frp;
2607 int extra_lines;
2608 int h;
2610 if (topfrp->fr_win != NULL)
2612 /* Simple case: just one window. */
2613 win_new_height(topfrp->fr_win,
2614 height - topfrp->fr_win->w_status_height);
2616 #ifdef FEAT_VERTSPLIT
2617 else if (topfrp->fr_layout == FR_ROW)
2621 /* All frames in this row get the same new height. */
2622 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2624 frame_new_height(frp, height, topfirst, wfh);
2625 if (frp->fr_height > height)
2627 /* Could not fit the windows, make the whole row higher. */
2628 height = frp->fr_height;
2629 break;
2633 while (frp != NULL);
2635 #endif
2636 else /* fr_layout == FR_COL */
2638 /* Complicated case: Resize a column of frames. Resize the bottom
2639 * frame first, frames above that when needed. */
2641 frp = topfrp->fr_child;
2642 if (wfh)
2643 /* Advance past frames with one window with 'wfh' set. */
2644 while (frame_fixed_height(frp))
2646 frp = frp->fr_next;
2647 if (frp == NULL)
2648 return; /* no frame without 'wfh', give up */
2650 if (!topfirst)
2652 /* Find the bottom frame of this column */
2653 while (frp->fr_next != NULL)
2654 frp = frp->fr_next;
2655 if (wfh)
2656 /* Advance back for frames with one window with 'wfh' set. */
2657 while (frame_fixed_height(frp))
2658 frp = frp->fr_prev;
2661 extra_lines = height - topfrp->fr_height;
2662 if (extra_lines < 0)
2664 /* reduce height of contained frames, bottom or top frame first */
2665 while (frp != NULL)
2667 h = frame_minheight(frp, NULL);
2668 if (frp->fr_height + extra_lines < h)
2670 extra_lines += frp->fr_height - h;
2671 frame_new_height(frp, h, topfirst, wfh);
2673 else
2675 frame_new_height(frp, frp->fr_height + extra_lines,
2676 topfirst, wfh);
2677 break;
2679 if (topfirst)
2682 frp = frp->fr_next;
2683 while (wfh && frp != NULL && frame_fixed_height(frp));
2685 else
2688 frp = frp->fr_prev;
2689 while (wfh && frp != NULL && frame_fixed_height(frp));
2691 /* Increase "height" if we could not reduce enough frames. */
2692 if (frp == NULL)
2693 height -= extra_lines;
2696 else if (extra_lines > 0)
2698 /* increase height of bottom or top frame */
2699 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2702 topfrp->fr_height = height;
2706 * Return TRUE if height of frame "frp" should not be changed because of
2707 * the 'winfixheight' option.
2709 static int
2710 frame_fixed_height(frp)
2711 frame_T *frp;
2713 /* frame with one window: fixed height if 'winfixheight' set. */
2714 if (frp->fr_win != NULL)
2715 return frp->fr_win->w_p_wfh;
2717 if (frp->fr_layout == FR_ROW)
2719 /* The frame is fixed height if one of the frames in the row is fixed
2720 * height. */
2721 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2722 if (frame_fixed_height(frp))
2723 return TRUE;
2724 return FALSE;
2727 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2728 * frames in the row are fixed height. */
2729 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2730 if (!frame_fixed_height(frp))
2731 return FALSE;
2732 return TRUE;
2735 #ifdef FEAT_VERTSPLIT
2737 * Return TRUE if width of frame "frp" should not be changed because of
2738 * the 'winfixwidth' option.
2740 static int
2741 frame_fixed_width(frp)
2742 frame_T *frp;
2744 /* frame with one window: fixed width if 'winfixwidth' set. */
2745 if (frp->fr_win != NULL)
2746 return frp->fr_win->w_p_wfw;
2748 if (frp->fr_layout == FR_COL)
2750 /* The frame is fixed width if one of the frames in the row is fixed
2751 * width. */
2752 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2753 if (frame_fixed_width(frp))
2754 return TRUE;
2755 return FALSE;
2758 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2759 * frames in the row are fixed width. */
2760 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2761 if (!frame_fixed_width(frp))
2762 return FALSE;
2763 return TRUE;
2767 * Add a status line to windows at the bottom of "frp".
2768 * Note: Does not check if there is room!
2770 static void
2771 frame_add_statusline(frp)
2772 frame_T *frp;
2774 win_T *wp;
2776 if (frp->fr_layout == FR_LEAF)
2778 wp = frp->fr_win;
2779 if (wp->w_status_height == 0)
2781 if (wp->w_height > 0) /* don't make it negative */
2782 --wp->w_height;
2783 wp->w_status_height = STATUS_HEIGHT;
2786 else if (frp->fr_layout == FR_ROW)
2788 /* Handle all the frames in the row. */
2789 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2790 frame_add_statusline(frp);
2792 else /* frp->fr_layout == FR_COL */
2794 /* Only need to handle the last frame in the column. */
2795 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2797 frame_add_statusline(frp);
2802 * Set width of a frame. Handles recursively going through contained frames.
2803 * May remove separator line for windows at the right side (for win_close()).
2805 static void
2806 frame_new_width(topfrp, width, leftfirst, wfw)
2807 frame_T *topfrp;
2808 int width;
2809 int leftfirst; /* resize leftmost contained frame first */
2810 int wfw; /* obey 'winfixwidth' when there is a choice;
2811 may cause the width not to be set */
2813 frame_T *frp;
2814 int extra_cols;
2815 int w;
2816 win_T *wp;
2818 if (topfrp->fr_layout == FR_LEAF)
2820 /* Simple case: just one window. */
2821 wp = topfrp->fr_win;
2822 /* Find out if there are any windows right of this one. */
2823 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2824 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2825 break;
2826 if (frp->fr_parent == NULL)
2827 wp->w_vsep_width = 0;
2828 win_new_width(wp, width - wp->w_vsep_width);
2830 else if (topfrp->fr_layout == FR_COL)
2834 /* All frames in this column get the same new width. */
2835 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2837 frame_new_width(frp, width, leftfirst, wfw);
2838 if (frp->fr_width > width)
2840 /* Could not fit the windows, make whole column wider. */
2841 width = frp->fr_width;
2842 break;
2845 } while (frp != NULL);
2847 else /* fr_layout == FR_ROW */
2849 /* Complicated case: Resize a row of frames. Resize the rightmost
2850 * frame first, frames left of it when needed. */
2852 frp = topfrp->fr_child;
2853 if (wfw)
2854 /* Advance past frames with one window with 'wfw' set. */
2855 while (frame_fixed_width(frp))
2857 frp = frp->fr_next;
2858 if (frp == NULL)
2859 return; /* no frame without 'wfw', give up */
2861 if (!leftfirst)
2863 /* Find the rightmost frame of this row */
2864 while (frp->fr_next != NULL)
2865 frp = frp->fr_next;
2866 if (wfw)
2867 /* Advance back for frames with one window with 'wfw' set. */
2868 while (frame_fixed_width(frp))
2869 frp = frp->fr_prev;
2872 extra_cols = width - topfrp->fr_width;
2873 if (extra_cols < 0)
2875 /* reduce frame width, rightmost frame first */
2876 while (frp != NULL)
2878 w = frame_minwidth(frp, NULL);
2879 if (frp->fr_width + extra_cols < w)
2881 extra_cols += frp->fr_width - w;
2882 frame_new_width(frp, w, leftfirst, wfw);
2884 else
2886 frame_new_width(frp, frp->fr_width + extra_cols,
2887 leftfirst, wfw);
2888 break;
2890 if (leftfirst)
2893 frp = frp->fr_next;
2894 while (wfw && frp != NULL && frame_fixed_width(frp));
2896 else
2899 frp = frp->fr_prev;
2900 while (wfw && frp != NULL && frame_fixed_width(frp));
2902 /* Increase "width" if we could not reduce enough frames. */
2903 if (frp == NULL)
2904 width -= extra_cols;
2907 else if (extra_cols > 0)
2909 /* increase width of rightmost frame */
2910 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2913 topfrp->fr_width = width;
2917 * Add the vertical separator to windows at the right side of "frp".
2918 * Note: Does not check if there is room!
2920 static void
2921 frame_add_vsep(frp)
2922 frame_T *frp;
2924 win_T *wp;
2926 if (frp->fr_layout == FR_LEAF)
2928 wp = frp->fr_win;
2929 if (wp->w_vsep_width == 0)
2931 if (wp->w_width > 0) /* don't make it negative */
2932 --wp->w_width;
2933 wp->w_vsep_width = 1;
2936 else if (frp->fr_layout == FR_COL)
2938 /* Handle all the frames in the column. */
2939 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2940 frame_add_vsep(frp);
2942 else /* frp->fr_layout == FR_ROW */
2944 /* Only need to handle the last frame in the row. */
2945 frp = frp->fr_child;
2946 while (frp->fr_next != NULL)
2947 frp = frp->fr_next;
2948 frame_add_vsep(frp);
2953 * Set frame width from the window it contains.
2955 static void
2956 frame_fix_width(wp)
2957 win_T *wp;
2959 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2961 #endif
2964 * Set frame height from the window it contains.
2966 static void
2967 frame_fix_height(wp)
2968 win_T *wp;
2970 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2974 * Compute the minimal height for frame "topfrp".
2975 * Uses the 'winminheight' option.
2976 * When "next_curwin" isn't NULL, use p_wh for this window.
2977 * When "next_curwin" is NOWIN, don't use at least one line for the current
2978 * window.
2980 static int
2981 frame_minheight(topfrp, next_curwin)
2982 frame_T *topfrp;
2983 win_T *next_curwin;
2985 frame_T *frp;
2986 int m;
2987 #ifdef FEAT_VERTSPLIT
2988 int n;
2989 #endif
2991 if (topfrp->fr_win != NULL)
2993 if (topfrp->fr_win == next_curwin)
2994 m = p_wh + topfrp->fr_win->w_status_height;
2995 else
2997 /* window: minimal height of the window plus status line */
2998 m = p_wmh + topfrp->fr_win->w_status_height;
2999 /* Current window is minimal one line high */
3000 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3001 ++m;
3004 #ifdef FEAT_VERTSPLIT
3005 else if (topfrp->fr_layout == FR_ROW)
3007 /* get the minimal height from each frame in this row */
3008 m = 0;
3009 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3011 n = frame_minheight(frp, next_curwin);
3012 if (n > m)
3013 m = n;
3016 #endif
3017 else
3019 /* Add up the minimal heights for all frames in this column. */
3020 m = 0;
3021 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3022 m += frame_minheight(frp, next_curwin);
3025 return m;
3028 #ifdef FEAT_VERTSPLIT
3030 * Compute the minimal width for frame "topfrp".
3031 * When "next_curwin" isn't NULL, use p_wiw for this window.
3032 * When "next_curwin" is NOWIN, don't use at least one column for the current
3033 * window.
3035 static int
3036 frame_minwidth(topfrp, next_curwin)
3037 frame_T *topfrp;
3038 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3040 frame_T *frp;
3041 int m, n;
3043 if (topfrp->fr_win != NULL)
3045 if (topfrp->fr_win == next_curwin)
3046 m = p_wiw + topfrp->fr_win->w_vsep_width;
3047 else
3049 /* window: minimal width of the window plus separator column */
3050 m = p_wmw + topfrp->fr_win->w_vsep_width;
3051 /* Current window is minimal one column wide */
3052 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3053 ++m;
3056 else if (topfrp->fr_layout == FR_COL)
3058 /* get the minimal width from each frame in this column */
3059 m = 0;
3060 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3062 n = frame_minwidth(frp, next_curwin);
3063 if (n > m)
3064 m = n;
3067 else
3069 /* Add up the minimal widths for all frames in this row. */
3070 m = 0;
3071 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3072 m += frame_minwidth(frp, next_curwin);
3075 return m;
3077 #endif
3081 * Try to close all windows except current one.
3082 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3083 * used and the buffer was modified.
3085 * Used by ":bdel" and ":only".
3087 void
3088 close_others(message, forceit)
3089 int message;
3090 int forceit; /* always hide all other windows */
3092 win_T *wp;
3093 win_T *nextwp;
3094 int r;
3096 if (lastwin == firstwin)
3098 if (message
3099 #ifdef FEAT_AUTOCMD
3100 && !autocmd_busy
3101 #endif
3103 MSG(_(m_onlyone));
3104 return;
3107 /* Be very careful here: autocommands may change the window layout. */
3108 for (wp = firstwin; win_valid(wp); wp = nextwp)
3110 nextwp = wp->w_next;
3111 if (wp != curwin) /* don't close current window */
3114 /* Check if it's allowed to abandon this window */
3115 r = can_abandon(wp->w_buffer, forceit);
3116 #ifdef FEAT_AUTOCMD
3117 if (!win_valid(wp)) /* autocommands messed wp up */
3119 nextwp = firstwin;
3120 continue;
3122 #endif
3123 if (!r)
3125 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3126 if (message && (p_confirm || cmdmod.confirm) && p_write)
3128 dialog_changed(wp->w_buffer, FALSE);
3129 # ifdef FEAT_AUTOCMD
3130 if (!win_valid(wp)) /* autocommands messed wp up */
3132 nextwp = firstwin;
3133 continue;
3135 # endif
3137 if (bufIsChanged(wp->w_buffer))
3138 #endif
3139 continue;
3141 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3145 if (message && lastwin != firstwin)
3146 EMSG(_("E445: Other window contains changes"));
3149 #endif /* FEAT_WINDOWS */
3152 * Init the current window "curwin".
3153 * Called when a new file is being edited.
3155 void
3156 curwin_init()
3158 redraw_win_later(curwin, NOT_VALID);
3159 curwin->w_lines_valid = 0;
3160 curwin->w_cursor.lnum = 1;
3161 curwin->w_curswant = curwin->w_cursor.col = 0;
3162 #ifdef FEAT_VIRTUALEDIT
3163 curwin->w_cursor.coladd = 0;
3164 #endif
3165 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3166 curwin->w_pcmark.col = 0;
3167 curwin->w_prev_pcmark.lnum = 0;
3168 curwin->w_prev_pcmark.col = 0;
3169 curwin->w_topline = 1;
3170 #ifdef FEAT_DIFF
3171 curwin->w_topfill = 0;
3172 #endif
3173 curwin->w_botline = 2;
3174 #ifdef FEAT_FKMAP
3175 if (curwin->w_p_rl)
3176 curwin->w_farsi = W_CONV + W_R_L;
3177 else
3178 curwin->w_farsi = W_CONV;
3179 #endif
3183 * Allocate the first window and put an empty buffer in it.
3184 * Called from main().
3185 * Return FAIL when something goes wrong (out of memory).
3188 win_alloc_first()
3190 if (win_alloc_firstwin(NULL) == FAIL)
3191 return FAIL;
3193 #ifdef FEAT_WINDOWS
3194 first_tabpage = alloc_tabpage();
3195 if (first_tabpage == NULL)
3196 return FAIL;
3197 first_tabpage->tp_topframe = topframe;
3198 curtab = first_tabpage;
3199 #endif
3200 return OK;
3204 * Allocate the first window or the first window in a new tab page.
3205 * When "oldwin" is NULL create an empty buffer for it.
3206 * When "oldwin" is not NULL copy info from it to the new window (only with
3207 * FEAT_WINDOWS).
3208 * Return FAIL when something goes wrong (out of memory).
3210 static int
3211 win_alloc_firstwin(oldwin)
3212 win_T *oldwin;
3214 curwin = win_alloc(NULL);
3215 if (oldwin == NULL)
3217 /* Very first window, need to create an empty buffer for it and
3218 * initialize from scratch. */
3219 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3220 if (curwin == NULL || curbuf == NULL)
3221 return FAIL;
3222 curwin->w_buffer = curbuf;
3223 curbuf->b_nwindows = 1; /* there is one window */
3224 #ifdef FEAT_WINDOWS
3225 curwin->w_alist = &global_alist;
3226 #endif
3227 curwin_init(); /* init current window */
3229 #ifdef FEAT_WINDOWS
3230 else
3232 /* First window in new tab page, initialize it from "oldwin". */
3233 win_init(curwin, oldwin, 0);
3235 # ifdef FEAT_SCROLLBIND
3236 /* We don't want scroll-binding in the first window. */
3237 curwin->w_p_scb = FALSE;
3238 # endif
3240 #endif
3242 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3243 if (topframe == NULL)
3244 return FAIL;
3245 topframe->fr_layout = FR_LEAF;
3246 #ifdef FEAT_VERTSPLIT
3247 topframe->fr_width = Columns;
3248 #endif
3249 topframe->fr_height = Rows - p_ch;
3250 topframe->fr_win = curwin;
3251 curwin->w_frame = topframe;
3253 return OK;
3257 * Initialize the window and frame size to the maximum.
3259 void
3260 win_init_size()
3262 firstwin->w_height = ROWS_AVAIL;
3263 topframe->fr_height = ROWS_AVAIL;
3264 #ifdef FEAT_VERTSPLIT
3265 firstwin->w_width = Columns;
3266 topframe->fr_width = Columns;
3267 #endif
3270 #if defined(FEAT_WINDOWS) || defined(PROTO)
3273 * Allocate a new tabpage_T and init the values.
3274 * Returns NULL when out of memory.
3276 static tabpage_T *
3277 alloc_tabpage()
3279 tabpage_T *tp;
3281 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3282 if (tp != NULL)
3284 # ifdef FEAT_GUI
3285 int i;
3287 for (i = 0; i < 3; i++)
3288 tp->tp_prev_which_scrollbars[i] = -1;
3289 # endif
3290 # ifdef FEAT_DIFF
3291 tp->tp_diff_invalid = TRUE;
3292 # endif
3293 #ifdef FEAT_EVAL
3294 /* init t: variables */
3295 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3296 #endif
3297 tp->tp_ch_used = p_ch;
3299 return tp;
3302 void
3303 free_tabpage(tp)
3304 tabpage_T *tp;
3306 # ifdef FEAT_DIFF
3307 diff_clear(tp);
3308 # endif
3309 clear_snapshot(tp);
3310 #ifdef FEAT_EVAL
3311 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3312 #endif
3313 vim_free(tp);
3317 * Create a new Tab page with one window.
3318 * It will edit the current buffer, like after ":split".
3319 * When "after" is 0 put it just after the current Tab page.
3320 * Otherwise put it just before tab page "after".
3321 * Return FAIL or OK.
3324 win_new_tabpage(after)
3325 int after;
3327 tabpage_T *tp = curtab;
3328 tabpage_T *newtp;
3329 int n;
3331 newtp = alloc_tabpage();
3332 if (newtp == NULL)
3333 return FAIL;
3335 /* Remember the current windows in this Tab page. */
3336 if (leave_tabpage(curbuf) == FAIL)
3338 vim_free(newtp);
3339 return FAIL;
3341 curtab = newtp;
3343 /* Create a new empty window. */
3344 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3346 /* Make the new Tab page the new topframe. */
3347 if (after == 1)
3349 /* New tab page becomes the first one. */
3350 newtp->tp_next = first_tabpage;
3351 first_tabpage = newtp;
3353 else
3355 if (after > 0)
3357 /* Put new tab page before tab page "after". */
3358 n = 2;
3359 for (tp = first_tabpage; tp->tp_next != NULL
3360 && n < after; tp = tp->tp_next)
3361 ++n;
3363 newtp->tp_next = tp->tp_next;
3364 tp->tp_next = newtp;
3366 win_init_size();
3367 firstwin->w_winrow = tabline_height();
3368 win_comp_scroll(curwin);
3370 newtp->tp_topframe = topframe;
3371 last_status(FALSE);
3373 #if defined(FEAT_GUI)
3374 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3375 * scrollbars. Have to update them anyway. */
3376 if (gui.in_use && starting == 0)
3378 gui_init_which_components(NULL);
3379 gui_update_scrollbars(TRUE);
3381 need_mouse_correct = TRUE;
3382 #endif
3384 redraw_all_later(CLEAR);
3385 #ifdef FEAT_AUTOCMD
3386 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3387 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3388 #endif
3389 return OK;
3392 /* Failed, get back the previous Tab page */
3393 enter_tabpage(curtab, curbuf);
3394 return FAIL;
3398 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3399 * like with ":split".
3400 * Returns OK if a new tab page was created, FAIL otherwise.
3403 may_open_tabpage()
3405 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3407 if (n != 0)
3409 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3410 postponed_split_tab = 0;
3411 return win_new_tabpage(n);
3413 return FAIL;
3417 * Create up to "maxcount" tabpages with empty windows.
3418 * Returns the number of resulting tab pages.
3421 make_tabpages(maxcount)
3422 int maxcount;
3424 int count = maxcount;
3425 int todo;
3427 /* Limit to 'tabpagemax' tabs. */
3428 if (count > p_tpm)
3429 count = p_tpm;
3431 #ifdef FEAT_AUTOCMD
3433 * Don't execute autocommands while creating the tab pages. Must do that
3434 * when putting the buffers in the windows.
3436 block_autocmds();
3437 #endif
3439 for (todo = count - 1; todo > 0; --todo)
3440 if (win_new_tabpage(0) == FAIL)
3441 break;
3443 #ifdef FEAT_AUTOCMD
3444 unblock_autocmds();
3445 #endif
3447 /* return actual number of tab pages */
3448 return (count - todo);
3452 * Return TRUE when "tpc" points to a valid tab page.
3455 valid_tabpage(tpc)
3456 tabpage_T *tpc;
3458 tabpage_T *tp;
3460 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3461 if (tp == tpc)
3462 return TRUE;
3463 return FALSE;
3467 * Find tab page "n" (first one is 1). Returns NULL when not found.
3469 tabpage_T *
3470 find_tabpage(n)
3471 int n;
3473 tabpage_T *tp;
3474 int i = 1;
3476 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3477 ++i;
3478 return tp;
3482 * Get index of tab page "tp". First one has index 1.
3483 * When not found returns number of tab pages plus one.
3486 tabpage_index(ftp)
3487 tabpage_T *ftp;
3489 int i = 1;
3490 tabpage_T *tp;
3492 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3493 ++i;
3494 return i;
3498 * Prepare for leaving the current tab page.
3499 * When autocomands change "curtab" we don't leave the tab page and return
3500 * FAIL.
3501 * Careful: When OK is returned need to get a new tab page very very soon!
3503 /*ARGSUSED*/
3504 static int
3505 leave_tabpage(new_curbuf)
3506 buf_T *new_curbuf; /* what is going to be the new curbuf,
3507 NULL if unknown */
3509 tabpage_T *tp = curtab;
3511 #ifdef FEAT_VISUAL
3512 reset_VIsual_and_resel(); /* stop Visual mode */
3513 #endif
3514 #ifdef FEAT_AUTOCMD
3515 if (new_curbuf != curbuf)
3517 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3518 if (curtab != tp)
3519 return FAIL;
3521 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3522 if (curtab != tp)
3523 return FAIL;
3524 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3525 if (curtab != tp)
3526 return FAIL;
3527 #endif
3528 #if defined(FEAT_GUI)
3529 /* Remove the scrollbars. They may be added back later. */
3530 if (gui.in_use)
3531 gui_remove_scrollbars();
3532 #endif
3533 tp->tp_curwin = curwin;
3534 tp->tp_prevwin = prevwin;
3535 tp->tp_firstwin = firstwin;
3536 tp->tp_lastwin = lastwin;
3537 tp->tp_old_Rows = Rows;
3538 tp->tp_old_Columns = Columns;
3539 firstwin = NULL;
3540 lastwin = NULL;
3541 return OK;
3545 * Start using tab page "tp".
3546 * Only to be used after leave_tabpage() or freeing the current tab page.
3548 /*ARGSUSED*/
3549 static void
3550 enter_tabpage(tp, old_curbuf)
3551 tabpage_T *tp;
3552 buf_T *old_curbuf;
3554 int old_off = tp->tp_firstwin->w_winrow;
3555 win_T *next_prevwin = tp->tp_prevwin;
3557 curtab = tp;
3558 firstwin = tp->tp_firstwin;
3559 lastwin = tp->tp_lastwin;
3560 topframe = tp->tp_topframe;
3562 /* We would like doing the TabEnter event first, but we don't have a
3563 * valid current window yet, which may break some commands.
3564 * This triggers autocommands, thus may make "tp" invalid. */
3565 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3566 prevwin = next_prevwin;
3568 #ifdef FEAT_AUTOCMD
3569 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3571 if (old_curbuf != curbuf)
3572 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3573 #endif
3575 last_status(FALSE); /* status line may appear or disappear */
3576 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3577 must_redraw = CLEAR; /* need to redraw everything */
3578 #ifdef FEAT_DIFF
3579 diff_need_scrollbind = TRUE;
3580 #endif
3582 /* The tabpage line may have appeared or disappeared, may need to resize
3583 * the frames for that. When the Vim window was resized need to update
3584 * frame sizes too. Use the stored value of p_ch, so that it can be
3585 * different for each tab page. */
3586 p_ch = curtab->tp_ch_used;
3587 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3588 #ifdef FEAT_GUI_TABLINE
3589 && !gui_use_tabline()
3590 #endif
3592 shell_new_rows();
3593 #ifdef FEAT_VERTSPLIT
3594 if (curtab->tp_old_Columns != Columns && starting == 0)
3595 shell_new_columns(); /* update window widths */
3596 #endif
3598 #if defined(FEAT_GUI)
3599 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3600 * scrollbars. Have to update them anyway. */
3601 if (gui.in_use && starting == 0)
3603 gui_init_which_components(NULL);
3604 gui_update_scrollbars(TRUE);
3606 need_mouse_correct = TRUE;
3607 #endif
3609 redraw_all_later(CLEAR);
3613 * Go to tab page "n". For ":tab N" and "Ngt".
3614 * When "n" is 9999 go to the last tab page.
3616 void
3617 goto_tabpage(n)
3618 int n;
3620 tabpage_T *tp;
3621 tabpage_T *ttp;
3622 int i;
3624 if (text_locked())
3626 /* Not allowed when editing the command line. */
3627 #ifdef FEAT_CMDWIN
3628 if (cmdwin_type != 0)
3629 EMSG(_(e_cmdwin));
3630 else
3631 #endif
3632 EMSG(_(e_secure));
3633 return;
3636 /* If there is only one it can't work. */
3637 if (first_tabpage->tp_next == NULL)
3639 if (n > 1)
3640 beep_flush();
3641 return;
3644 if (n == 0)
3646 /* No count, go to next tab page, wrap around end. */
3647 if (curtab->tp_next == NULL)
3648 tp = first_tabpage;
3649 else
3650 tp = curtab->tp_next;
3652 else if (n < 0)
3654 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3655 * this N times. */
3656 ttp = curtab;
3657 for (i = n; i < 0; ++i)
3659 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3660 tp = tp->tp_next)
3662 ttp = tp;
3665 else if (n == 9999)
3667 /* Go to last tab page. */
3668 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3671 else
3673 /* Go to tab page "n". */
3674 tp = find_tabpage(n);
3675 if (tp == NULL)
3677 beep_flush();
3678 return;
3682 goto_tabpage_tp(tp);
3684 #ifdef FEAT_GUI_TABLINE
3685 if (gui_use_tabline())
3686 gui_mch_set_curtab(tabpage_index(curtab));
3687 #endif
3691 * Go to tabpage "tp".
3692 * Note: doesn't update the GUI tab.
3694 void
3695 goto_tabpage_tp(tp)
3696 tabpage_T *tp;
3698 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3700 if (valid_tabpage(tp))
3701 enter_tabpage(tp, curbuf);
3702 else
3703 enter_tabpage(curtab, curbuf);
3708 * Enter window "wp" in tab page "tp".
3709 * Also updates the GUI tab.
3711 void
3712 goto_tabpage_win(tp, wp)
3713 tabpage_T *tp;
3714 win_T *wp;
3716 goto_tabpage_tp(tp);
3717 if (curtab == tp && win_valid(wp))
3719 win_enter(wp, TRUE);
3720 # ifdef FEAT_GUI_TABLINE
3721 if (gui_use_tabline())
3722 gui_mch_set_curtab(tabpage_index(curtab));
3723 # endif
3728 * Move the current tab page to before tab page "nr".
3730 void
3731 tabpage_move(nr)
3732 int nr;
3734 int n = nr;
3735 tabpage_T *tp;
3737 if (first_tabpage->tp_next == NULL)
3738 return;
3740 /* Remove the current tab page from the list of tab pages. */
3741 if (curtab == first_tabpage)
3742 first_tabpage = curtab->tp_next;
3743 else
3745 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3746 if (tp->tp_next == curtab)
3747 break;
3748 if (tp == NULL) /* "cannot happen" */
3749 return;
3750 tp->tp_next = curtab->tp_next;
3753 /* Re-insert it at the specified position. */
3754 if (n == 0)
3756 curtab->tp_next = first_tabpage;
3757 first_tabpage = curtab;
3759 else
3761 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3762 --n;
3763 curtab->tp_next = tp->tp_next;
3764 tp->tp_next = curtab;
3767 /* Need to redraw the tabline. Tab page contents doesn't change. */
3768 redraw_tabline = TRUE;
3773 * Go to another window.
3774 * When jumping to another buffer, stop Visual mode. Do this before
3775 * changing windows so we can yank the selection into the '*' register.
3776 * When jumping to another window on the same buffer, adjust its cursor
3777 * position to keep the same Visual area.
3779 void
3780 win_goto(wp)
3781 win_T *wp;
3783 if (text_locked())
3785 beep_flush();
3786 text_locked_msg();
3787 return;
3789 #ifdef FEAT_AUTOCMD
3790 if (curbuf_locked())
3791 return;
3792 #endif
3794 #ifdef FEAT_VISUAL
3795 if (wp->w_buffer != curbuf)
3796 reset_VIsual_and_resel();
3797 else if (VIsual_active)
3798 wp->w_cursor = curwin->w_cursor;
3799 #endif
3801 #ifdef FEAT_GUI
3802 need_mouse_correct = TRUE;
3803 #endif
3804 win_enter(wp, TRUE);
3807 #if defined(FEAT_PERL) || defined(PROTO)
3809 * Find window number "winnr" (counting top to bottom).
3811 win_T *
3812 win_find_nr(winnr)
3813 int winnr;
3815 win_T *wp;
3817 # ifdef FEAT_WINDOWS
3818 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3819 if (--winnr == 0)
3820 break;
3821 return wp;
3822 # else
3823 return curwin;
3824 # endif
3826 #endif
3828 #ifdef FEAT_VERTSPLIT
3830 * Move to window above or below "count" times.
3832 static void
3833 win_goto_ver(up, count)
3834 int up; /* TRUE to go to win above */
3835 long count;
3837 frame_T *fr;
3838 frame_T *nfr;
3839 frame_T *foundfr;
3841 foundfr = curwin->w_frame;
3842 while (count--)
3845 * First go upwards in the tree of frames until we find a upwards or
3846 * downwards neighbor.
3848 fr = foundfr;
3849 for (;;)
3851 if (fr == topframe)
3852 goto end;
3853 if (up)
3854 nfr = fr->fr_prev;
3855 else
3856 nfr = fr->fr_next;
3857 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3858 break;
3859 fr = fr->fr_parent;
3863 * Now go downwards to find the bottom or top frame in it.
3865 for (;;)
3867 if (nfr->fr_layout == FR_LEAF)
3869 foundfr = nfr;
3870 break;
3872 fr = nfr->fr_child;
3873 if (nfr->fr_layout == FR_ROW)
3875 /* Find the frame at the cursor row. */
3876 while (fr->fr_next != NULL
3877 && frame2win(fr)->w_wincol + fr->fr_width
3878 <= curwin->w_wincol + curwin->w_wcol)
3879 fr = fr->fr_next;
3881 if (nfr->fr_layout == FR_COL && up)
3882 while (fr->fr_next != NULL)
3883 fr = fr->fr_next;
3884 nfr = fr;
3887 end:
3888 if (foundfr != NULL)
3889 win_goto(foundfr->fr_win);
3893 * Move to left or right window.
3895 static void
3896 win_goto_hor(left, count)
3897 int left; /* TRUE to go to left win */
3898 long count;
3900 frame_T *fr;
3901 frame_T *nfr;
3902 frame_T *foundfr;
3904 foundfr = curwin->w_frame;
3905 while (count--)
3908 * First go upwards in the tree of frames until we find a left or
3909 * right neighbor.
3911 fr = foundfr;
3912 for (;;)
3914 if (fr == topframe)
3915 goto end;
3916 if (left)
3917 nfr = fr->fr_prev;
3918 else
3919 nfr = fr->fr_next;
3920 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3921 break;
3922 fr = fr->fr_parent;
3926 * Now go downwards to find the leftmost or rightmost frame in it.
3928 for (;;)
3930 if (nfr->fr_layout == FR_LEAF)
3932 foundfr = nfr;
3933 break;
3935 fr = nfr->fr_child;
3936 if (nfr->fr_layout == FR_COL)
3938 /* Find the frame at the cursor row. */
3939 while (fr->fr_next != NULL
3940 && frame2win(fr)->w_winrow + fr->fr_height
3941 <= curwin->w_winrow + curwin->w_wrow)
3942 fr = fr->fr_next;
3944 if (nfr->fr_layout == FR_ROW && left)
3945 while (fr->fr_next != NULL)
3946 fr = fr->fr_next;
3947 nfr = fr;
3950 end:
3951 if (foundfr != NULL)
3952 win_goto(foundfr->fr_win);
3954 #endif
3957 * Make window "wp" the current window.
3959 void
3960 win_enter(wp, undo_sync)
3961 win_T *wp;
3962 int undo_sync;
3964 win_enter_ext(wp, undo_sync, FALSE);
3968 * Make window wp the current window.
3969 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3970 * been closed and isn't valid.
3972 static void
3973 win_enter_ext(wp, undo_sync, curwin_invalid)
3974 win_T *wp;
3975 int undo_sync;
3976 int curwin_invalid;
3978 #ifdef FEAT_AUTOCMD
3979 int other_buffer = FALSE;
3980 #endif
3982 if (wp == curwin && !curwin_invalid) /* nothing to do */
3983 return;
3985 #ifdef FEAT_AUTOCMD
3986 if (!curwin_invalid)
3989 * Be careful: If autocommands delete the window, return now.
3991 if (wp->w_buffer != curbuf)
3993 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3994 other_buffer = TRUE;
3995 if (!win_valid(wp))
3996 return;
3998 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3999 if (!win_valid(wp))
4000 return;
4001 # ifdef FEAT_EVAL
4002 /* autocmds may abort script processing */
4003 if (aborting())
4004 return;
4005 # endif
4007 #endif
4009 /* sync undo before leaving the current buffer */
4010 if (undo_sync && curbuf != wp->w_buffer)
4011 u_sync(FALSE);
4012 /* may have to copy the buffer options when 'cpo' contains 'S' */
4013 if (wp->w_buffer != curbuf)
4014 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4015 if (!curwin_invalid)
4017 prevwin = curwin; /* remember for CTRL-W p */
4018 curwin->w_redr_status = TRUE;
4020 curwin = wp;
4021 curbuf = wp->w_buffer;
4022 check_cursor();
4023 #ifdef FEAT_VIRTUALEDIT
4024 if (!virtual_active())
4025 curwin->w_cursor.coladd = 0;
4026 #endif
4027 changed_line_abv_curs(); /* assume cursor position needs updating */
4029 if (curwin->w_localdir != NULL)
4031 /* Window has a local directory: Save current directory as global
4032 * directory (unless that was done already) and change to the local
4033 * directory. */
4034 if (globaldir == NULL)
4036 char_u cwd[MAXPATHL];
4038 if (mch_dirname(cwd, MAXPATHL) == OK)
4039 globaldir = vim_strsave(cwd);
4041 if (mch_chdir((char *)curwin->w_localdir) == 0)
4042 shorten_fnames(TRUE);
4044 else if (globaldir != NULL)
4046 /* Window doesn't have a local directory and we are not in the global
4047 * directory: Change to the global directory. */
4048 ignored = mch_chdir((char *)globaldir);
4049 vim_free(globaldir);
4050 globaldir = NULL;
4051 shorten_fnames(TRUE);
4054 #ifdef FEAT_AUTOCMD
4055 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4056 if (other_buffer)
4057 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4058 #endif
4060 #ifdef FEAT_TITLE
4061 maketitle();
4062 #endif
4063 curwin->w_redr_status = TRUE;
4064 redraw_tabline = TRUE;
4065 if (restart_edit)
4066 redraw_later(VALID); /* causes status line redraw */
4068 /* set window height to desired minimal value */
4069 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4070 win_setheight((int)p_wh);
4071 else if (curwin->w_height == 0)
4072 win_setheight(1);
4074 #ifdef FEAT_VERTSPLIT
4075 /* set window width to desired minimal value */
4076 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4077 win_setwidth((int)p_wiw);
4078 #endif
4080 #ifdef FEAT_MOUSE
4081 setmouse(); /* in case jumped to/from help buffer */
4082 #endif
4084 /* Change directories when the 'acd' option is set. */
4085 DO_AUTOCHDIR
4088 #endif /* FEAT_WINDOWS */
4090 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4092 * Jump to the first open window that contains buffer "buf", if one exists.
4093 * Returns a pointer to the window found, otherwise NULL.
4095 win_T *
4096 buf_jump_open_win(buf)
4097 buf_T *buf;
4099 # ifdef FEAT_WINDOWS
4100 win_T *wp;
4102 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4103 if (wp->w_buffer == buf)
4104 break;
4105 if (wp != NULL)
4106 win_enter(wp, FALSE);
4107 return wp;
4108 # else
4109 if (curwin->w_buffer == buf)
4110 return curwin;
4111 return NULL;
4112 # endif
4116 * Jump to the first open window in any tab page that contains buffer "buf",
4117 * if one exists.
4118 * Returns a pointer to the window found, otherwise NULL.
4120 win_T *
4121 buf_jump_open_tab(buf)
4122 buf_T *buf;
4124 # ifdef FEAT_WINDOWS
4125 win_T *wp;
4126 tabpage_T *tp;
4128 /* First try the current tab page. */
4129 wp = buf_jump_open_win(buf);
4130 if (wp != NULL)
4131 return wp;
4133 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4134 if (tp != curtab)
4136 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4137 if (wp->w_buffer == buf)
4138 break;
4139 if (wp != NULL)
4141 goto_tabpage_win(tp, wp);
4142 if (curwin != wp)
4143 wp = NULL; /* something went wrong */
4144 break;
4148 return wp;
4149 # else
4150 if (curwin->w_buffer == buf)
4151 return curwin;
4152 return NULL;
4153 # endif
4155 #endif
4158 * allocate a window structure and link it in the window list
4160 /*ARGSUSED*/
4161 static win_T *
4162 win_alloc(after)
4163 win_T *after;
4165 win_T *newwin;
4168 * allocate window structure and linesizes arrays
4170 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4171 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4173 vim_free(newwin);
4174 newwin = NULL;
4177 if (newwin != NULL)
4179 #ifdef FEAT_AUTOCMD
4180 /* Don't execute autocommands while the window is not properly
4181 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4182 * event. */
4183 block_autocmds();
4184 #endif
4186 * link the window in the window list
4188 #ifdef FEAT_WINDOWS
4189 win_append(after, newwin);
4190 #endif
4191 #ifdef FEAT_VERTSPLIT
4192 newwin->w_wincol = 0;
4193 newwin->w_width = Columns;
4194 #endif
4196 /* position the display and the cursor at the top of the file. */
4197 newwin->w_topline = 1;
4198 #ifdef FEAT_DIFF
4199 newwin->w_topfill = 0;
4200 #endif
4201 newwin->w_botline = 2;
4202 newwin->w_cursor.lnum = 1;
4203 #ifdef FEAT_SCROLLBIND
4204 newwin->w_scbind_pos = 1;
4205 #endif
4207 /* We won't calculate w_fraction until resizing the window */
4208 newwin->w_fraction = 0;
4209 newwin->w_prev_fraction_row = -1;
4211 #ifdef FEAT_GUI
4212 if (gui.in_use)
4214 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4215 SBAR_LEFT, newwin);
4216 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4217 SBAR_RIGHT, newwin);
4219 #endif
4220 #ifdef FEAT_EVAL
4221 /* init w: variables */
4222 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4223 #endif
4224 #ifdef FEAT_FOLDING
4225 foldInitWin(newwin);
4226 #endif
4227 #ifdef FEAT_AUTOCMD
4228 unblock_autocmds();
4229 #endif
4230 #ifdef FEAT_SEARCH_EXTRA
4231 newwin->w_match_head = NULL;
4232 newwin->w_next_match_id = 4;
4233 #endif
4235 return newwin;
4238 #if defined(FEAT_WINDOWS) || defined(PROTO)
4241 * remove window 'wp' from the window list and free the structure
4243 static void
4244 win_free(wp, tp)
4245 win_T *wp;
4246 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4248 int i;
4250 #ifdef FEAT_AUTOCMD
4251 /* Don't execute autocommands while the window is halfway being deleted.
4252 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4253 block_autocmds();
4254 #endif
4256 #ifdef FEAT_MZSCHEME
4257 mzscheme_window_free(wp);
4258 #endif
4260 #ifdef FEAT_PERL
4261 perl_win_free(wp);
4262 #endif
4264 #ifdef FEAT_PYTHON
4265 python_window_free(wp);
4266 #endif
4268 #ifdef FEAT_TCL
4269 tcl_window_free(wp);
4270 #endif
4272 #ifdef FEAT_RUBY
4273 ruby_window_free(wp);
4274 #endif
4276 clear_winopt(&wp->w_onebuf_opt);
4277 clear_winopt(&wp->w_allbuf_opt);
4279 #ifdef FEAT_EVAL
4280 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4281 #endif
4283 if (prevwin == wp)
4284 prevwin = NULL;
4285 win_free_lsize(wp);
4287 for (i = 0; i < wp->w_tagstacklen; ++i)
4288 vim_free(wp->w_tagstack[i].tagname);
4290 vim_free(wp->w_localdir);
4292 #ifdef FEAT_SEARCH_EXTRA
4293 clear_matches(wp);
4294 #endif
4296 #ifdef FEAT_JUMPLIST
4297 free_jumplist(wp);
4298 #endif
4300 #ifdef FEAT_QUICKFIX
4301 qf_free_all(wp);
4302 #endif
4304 #ifdef FEAT_GUI
4305 if (gui.in_use)
4307 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4308 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4310 #endif /* FEAT_GUI */
4312 win_remove(wp, tp);
4313 vim_free(wp);
4315 #ifdef FEAT_AUTOCMD
4316 unblock_autocmds();
4317 #endif
4321 * Append window "wp" in the window list after window "after".
4323 static void
4324 win_append(after, wp)
4325 win_T *after, *wp;
4327 win_T *before;
4329 if (after == NULL) /* after NULL is in front of the first */
4330 before = firstwin;
4331 else
4332 before = after->w_next;
4334 wp->w_next = before;
4335 wp->w_prev = after;
4336 if (after == NULL)
4337 firstwin = wp;
4338 else
4339 after->w_next = wp;
4340 if (before == NULL)
4341 lastwin = wp;
4342 else
4343 before->w_prev = wp;
4347 * Remove a window from the window list.
4349 static void
4350 win_remove(wp, tp)
4351 win_T *wp;
4352 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4354 if (wp->w_prev != NULL)
4355 wp->w_prev->w_next = wp->w_next;
4356 else if (tp == NULL)
4357 firstwin = wp->w_next;
4358 else
4359 tp->tp_firstwin = wp->w_next;
4360 if (wp->w_next != NULL)
4361 wp->w_next->w_prev = wp->w_prev;
4362 else if (tp == NULL)
4363 lastwin = wp->w_prev;
4364 else
4365 tp->tp_lastwin = wp->w_prev;
4369 * Append frame "frp" in a frame list after frame "after".
4371 static void
4372 frame_append(after, frp)
4373 frame_T *after, *frp;
4375 frp->fr_next = after->fr_next;
4376 after->fr_next = frp;
4377 if (frp->fr_next != NULL)
4378 frp->fr_next->fr_prev = frp;
4379 frp->fr_prev = after;
4383 * Insert frame "frp" in a frame list before frame "before".
4385 static void
4386 frame_insert(before, frp)
4387 frame_T *before, *frp;
4389 frp->fr_next = before;
4390 frp->fr_prev = before->fr_prev;
4391 before->fr_prev = frp;
4392 if (frp->fr_prev != NULL)
4393 frp->fr_prev->fr_next = frp;
4394 else
4395 frp->fr_parent->fr_child = frp;
4399 * Remove a frame from a frame list.
4401 static void
4402 frame_remove(frp)
4403 frame_T *frp;
4405 if (frp->fr_prev != NULL)
4406 frp->fr_prev->fr_next = frp->fr_next;
4407 else
4408 frp->fr_parent->fr_child = frp->fr_next;
4409 if (frp->fr_next != NULL)
4410 frp->fr_next->fr_prev = frp->fr_prev;
4413 #endif /* FEAT_WINDOWS */
4416 * Allocate w_lines[] for window "wp".
4417 * Return FAIL for failure, OK for success.
4420 win_alloc_lines(wp)
4421 win_T *wp;
4423 wp->w_lines_valid = 0;
4424 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4425 if (wp->w_lines == NULL)
4426 return FAIL;
4427 return OK;
4431 * free lsize arrays for a window
4433 void
4434 win_free_lsize(wp)
4435 win_T *wp;
4437 vim_free(wp->w_lines);
4438 wp->w_lines = NULL;
4442 * Called from win_new_shellsize() after Rows changed.
4443 * This only does the current tab page, others must be done when made active.
4445 void
4446 shell_new_rows()
4448 int h = (int)ROWS_AVAIL;
4450 if (firstwin == NULL) /* not initialized yet */
4451 return;
4452 #ifdef FEAT_WINDOWS
4453 if (h < frame_minheight(topframe, NULL))
4454 h = frame_minheight(topframe, NULL);
4456 /* First try setting the heights of windows with 'winfixheight'. If
4457 * that doesn't result in the right height, forget about that option. */
4458 frame_new_height(topframe, h, FALSE, TRUE);
4459 if (topframe->fr_height != h)
4460 frame_new_height(topframe, h, FALSE, FALSE);
4462 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4463 #else
4464 if (h < 1)
4465 h = 1;
4466 win_new_height(firstwin, h);
4467 #endif
4468 compute_cmdrow();
4469 #ifdef FEAT_WINDOWS
4470 curtab->tp_ch_used = p_ch;
4471 #endif
4473 #if 0
4474 /* Disabled: don't want making the screen smaller make a window larger. */
4475 if (p_ea)
4476 win_equal(curwin, FALSE, 'v');
4477 #endif
4480 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4482 * Called from win_new_shellsize() after Columns changed.
4484 void
4485 shell_new_columns()
4487 if (firstwin == NULL) /* not initialized yet */
4488 return;
4490 /* First try setting the widths of windows with 'winfixwidth'. If that
4491 * doesn't result in the right width, forget about that option. */
4492 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4493 if (topframe->fr_width != Columns)
4494 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4496 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4497 #if 0
4498 /* Disabled: don't want making the screen smaller make a window larger. */
4499 if (p_ea)
4500 win_equal(curwin, FALSE, 'h');
4501 #endif
4503 #endif
4505 #if defined(FEAT_CMDWIN) || defined(PROTO)
4507 * Save the size of all windows in "gap".
4509 void
4510 win_size_save(gap)
4511 garray_T *gap;
4514 win_T *wp;
4516 ga_init2(gap, (int)sizeof(int), 1);
4517 if (ga_grow(gap, win_count() * 2) == OK)
4518 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4520 ((int *)gap->ga_data)[gap->ga_len++] =
4521 wp->w_width + wp->w_vsep_width;
4522 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4527 * Restore window sizes, but only if the number of windows is still the same.
4528 * Does not free the growarray.
4530 void
4531 win_size_restore(gap)
4532 garray_T *gap;
4534 win_T *wp;
4535 int i;
4537 if (win_count() * 2 == gap->ga_len)
4539 i = 0;
4540 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4542 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4543 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4545 /* recompute the window positions */
4546 (void)win_comp_pos();
4549 #endif /* FEAT_CMDWIN */
4551 #if defined(FEAT_WINDOWS) || defined(PROTO)
4553 * Update the position for all windows, using the width and height of the
4554 * frames.
4555 * Returns the row just after the last window.
4558 win_comp_pos()
4560 int row = tabline_height();
4561 int col = 0;
4563 frame_comp_pos(topframe, &row, &col);
4564 return row;
4568 * Update the position of the windows in frame "topfrp", using the width and
4569 * height of the frames.
4570 * "*row" and "*col" are the top-left position of the frame. They are updated
4571 * to the bottom-right position plus one.
4573 static void
4574 frame_comp_pos(topfrp, row, col)
4575 frame_T *topfrp;
4576 int *row;
4577 int *col;
4579 win_T *wp;
4580 frame_T *frp;
4581 #ifdef FEAT_VERTSPLIT
4582 int startcol;
4583 int startrow;
4584 #endif
4586 wp = topfrp->fr_win;
4587 if (wp != NULL)
4589 if (wp->w_winrow != *row
4590 #ifdef FEAT_VERTSPLIT
4591 || wp->w_wincol != *col
4592 #endif
4595 /* position changed, redraw */
4596 wp->w_winrow = *row;
4597 #ifdef FEAT_VERTSPLIT
4598 wp->w_wincol = *col;
4599 #endif
4600 redraw_win_later(wp, NOT_VALID);
4601 wp->w_redr_status = TRUE;
4603 *row += wp->w_height + wp->w_status_height;
4604 #ifdef FEAT_VERTSPLIT
4605 *col += wp->w_width + wp->w_vsep_width;
4606 #endif
4608 else
4610 #ifdef FEAT_VERTSPLIT
4611 startrow = *row;
4612 startcol = *col;
4613 #endif
4614 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4616 #ifdef FEAT_VERTSPLIT
4617 if (topfrp->fr_layout == FR_ROW)
4618 *row = startrow; /* all frames are at the same row */
4619 else
4620 *col = startcol; /* all frames are at the same col */
4621 #endif
4622 frame_comp_pos(frp, row, col);
4627 #endif /* FEAT_WINDOWS */
4630 * Set current window height and take care of repositioning other windows to
4631 * fit around it.
4633 void
4634 win_setheight(height)
4635 int height;
4637 win_setheight_win(height, curwin);
4641 * Set the window height of window "win" and take care of repositioning other
4642 * windows to fit around it.
4644 void
4645 win_setheight_win(height, win)
4646 int height;
4647 win_T *win;
4649 int row;
4651 if (win == curwin)
4653 /* Always keep current window at least one line high, even when
4654 * 'winminheight' is zero. */
4655 #ifdef FEAT_WINDOWS
4656 if (height < p_wmh)
4657 height = p_wmh;
4658 #endif
4659 if (height == 0)
4660 height = 1;
4663 #ifdef FEAT_WINDOWS
4664 frame_setheight(win->w_frame, height + win->w_status_height);
4666 /* recompute the window positions */
4667 row = win_comp_pos();
4668 #else
4669 if (height > topframe->fr_height)
4670 height = topframe->fr_height;
4671 win->w_height = height;
4672 row = height;
4673 #endif
4676 * If there is extra space created between the last window and the command
4677 * line, clear it.
4679 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4680 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4681 cmdline_row = row;
4682 msg_row = row;
4683 msg_col = 0;
4685 redraw_all_later(NOT_VALID);
4688 #if defined(FEAT_WINDOWS) || defined(PROTO)
4691 * Set the height of a frame to "height" and take care that all frames and
4692 * windows inside it are resized. Also resize frames on the left and right if
4693 * the are in the same FR_ROW frame.
4695 * Strategy:
4696 * If the frame is part of a FR_COL frame, try fitting the frame in that
4697 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4698 * go to containing frames to resize them and make room.
4699 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4700 * Check for the minimal height of the FR_ROW frame.
4701 * At the top level we can also use change the command line height.
4703 static void
4704 frame_setheight(curfrp, height)
4705 frame_T *curfrp;
4706 int height;
4708 int room; /* total number of lines available */
4709 int take; /* number of lines taken from other windows */
4710 int room_cmdline; /* lines available from cmdline */
4711 int run;
4712 frame_T *frp;
4713 int h;
4714 int room_reserved;
4716 /* If the height already is the desired value, nothing to do. */
4717 if (curfrp->fr_height == height)
4718 return;
4720 if (curfrp->fr_parent == NULL)
4722 /* topframe: can only change the command line */
4723 if (height > ROWS_AVAIL)
4724 height = ROWS_AVAIL;
4725 if (height > 0)
4726 frame_new_height(curfrp, height, FALSE, FALSE);
4728 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4730 /* Row of frames: Also need to resize frames left and right of this
4731 * one. First check for the minimal height of these. */
4732 h = frame_minheight(curfrp->fr_parent, NULL);
4733 if (height < h)
4734 height = h;
4735 frame_setheight(curfrp->fr_parent, height);
4737 else
4740 * Column of frames: try to change only frames in this column.
4742 #ifdef FEAT_VERTSPLIT
4744 * Do this twice:
4745 * 1: compute room available, if it's not enough try resizing the
4746 * containing frame.
4747 * 2: compute the room available and adjust the height to it.
4748 * Try not to reduce the height of a window with 'winfixheight' set.
4750 for (run = 1; run <= 2; ++run)
4751 #else
4752 for (;;)
4753 #endif
4755 room = 0;
4756 room_reserved = 0;
4757 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4758 frp = frp->fr_next)
4760 if (frp != curfrp
4761 && frp->fr_win != NULL
4762 && frp->fr_win->w_p_wfh)
4763 room_reserved += frp->fr_height;
4764 room += frp->fr_height;
4765 if (frp != curfrp)
4766 room -= frame_minheight(frp, NULL);
4768 #ifdef FEAT_VERTSPLIT
4769 if (curfrp->fr_width != Columns)
4770 room_cmdline = 0;
4771 else
4772 #endif
4774 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4775 + lastwin->w_height + lastwin->w_status_height);
4776 if (room_cmdline < 0)
4777 room_cmdline = 0;
4780 if (height <= room + room_cmdline)
4781 break;
4782 #ifdef FEAT_VERTSPLIT
4783 if (run == 2 || curfrp->fr_width == Columns)
4784 #endif
4786 if (height > room + room_cmdline)
4787 height = room + room_cmdline;
4788 break;
4790 #ifdef FEAT_VERTSPLIT
4791 frame_setheight(curfrp->fr_parent, height
4792 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4793 #endif
4794 /*NOTREACHED*/
4798 * Compute the number of lines we will take from others frames (can be
4799 * negative!).
4801 take = height - curfrp->fr_height;
4803 /* If there is not enough room, also reduce the height of a window
4804 * with 'winfixheight' set. */
4805 if (height > room + room_cmdline - room_reserved)
4806 room_reserved = room + room_cmdline - height;
4807 /* If there is only a 'winfixheight' window and making the
4808 * window smaller, need to make the other window taller. */
4809 if (take < 0 && room - curfrp->fr_height < room_reserved)
4810 room_reserved = 0;
4812 if (take > 0 && room_cmdline > 0)
4814 /* use lines from cmdline first */
4815 if (take < room_cmdline)
4816 room_cmdline = take;
4817 take -= room_cmdline;
4818 topframe->fr_height += room_cmdline;
4822 * set the current frame to the new height
4824 frame_new_height(curfrp, height, FALSE, FALSE);
4827 * First take lines from the frames after the current frame. If
4828 * that is not enough, takes lines from frames above the current
4829 * frame.
4831 for (run = 0; run < 2; ++run)
4833 if (run == 0)
4834 frp = curfrp->fr_next; /* 1st run: start with next window */
4835 else
4836 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4837 while (frp != NULL && take != 0)
4839 h = frame_minheight(frp, NULL);
4840 if (room_reserved > 0
4841 && frp->fr_win != NULL
4842 && frp->fr_win->w_p_wfh)
4844 if (room_reserved >= frp->fr_height)
4845 room_reserved -= frp->fr_height;
4846 else
4848 if (frp->fr_height - room_reserved > take)
4849 room_reserved = frp->fr_height - take;
4850 take -= frp->fr_height - room_reserved;
4851 frame_new_height(frp, room_reserved, FALSE, FALSE);
4852 room_reserved = 0;
4855 else
4857 if (frp->fr_height - take < h)
4859 take -= frp->fr_height - h;
4860 frame_new_height(frp, h, FALSE, FALSE);
4862 else
4864 frame_new_height(frp, frp->fr_height - take,
4865 FALSE, FALSE);
4866 take = 0;
4869 if (run == 0)
4870 frp = frp->fr_next;
4871 else
4872 frp = frp->fr_prev;
4878 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4880 * Set current window width and take care of repositioning other windows to
4881 * fit around it.
4883 void
4884 win_setwidth(width)
4885 int width;
4887 win_setwidth_win(width, curwin);
4890 void
4891 win_setwidth_win(width, wp)
4892 int width;
4893 win_T *wp;
4895 /* Always keep current window at least one column wide, even when
4896 * 'winminwidth' is zero. */
4897 if (wp == curwin)
4899 if (width < p_wmw)
4900 width = p_wmw;
4901 if (width == 0)
4902 width = 1;
4905 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4907 /* recompute the window positions */
4908 (void)win_comp_pos();
4910 redraw_all_later(NOT_VALID);
4914 * Set the width of a frame to "width" and take care that all frames and
4915 * windows inside it are resized. Also resize frames above and below if the
4916 * are in the same FR_ROW frame.
4918 * Strategy is similar to frame_setheight().
4920 static void
4921 frame_setwidth(curfrp, width)
4922 frame_T *curfrp;
4923 int width;
4925 int room; /* total number of lines available */
4926 int take; /* number of lines taken from other windows */
4927 int run;
4928 frame_T *frp;
4929 int w;
4930 int room_reserved;
4932 /* If the width already is the desired value, nothing to do. */
4933 if (curfrp->fr_width == width)
4934 return;
4936 if (curfrp->fr_parent == NULL)
4937 /* topframe: can't change width */
4938 return;
4940 if (curfrp->fr_parent->fr_layout == FR_COL)
4942 /* Column of frames: Also need to resize frames above and below of
4943 * this one. First check for the minimal width of these. */
4944 w = frame_minwidth(curfrp->fr_parent, NULL);
4945 if (width < w)
4946 width = w;
4947 frame_setwidth(curfrp->fr_parent, width);
4949 else
4952 * Row of frames: try to change only frames in this row.
4954 * Do this twice:
4955 * 1: compute room available, if it's not enough try resizing the
4956 * containing frame.
4957 * 2: compute the room available and adjust the width to it.
4959 for (run = 1; run <= 2; ++run)
4961 room = 0;
4962 room_reserved = 0;
4963 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4964 frp = frp->fr_next)
4966 if (frp != curfrp
4967 && frp->fr_win != NULL
4968 && frp->fr_win->w_p_wfw)
4969 room_reserved += frp->fr_width;
4970 room += frp->fr_width;
4971 if (frp != curfrp)
4972 room -= frame_minwidth(frp, NULL);
4975 if (width <= room)
4976 break;
4977 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4979 if (width > room)
4980 width = room;
4981 break;
4983 frame_setwidth(curfrp->fr_parent, width
4984 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4988 * Compute the number of lines we will take from others frames (can be
4989 * negative!).
4991 take = width - curfrp->fr_width;
4993 /* If there is not enough room, also reduce the width of a window
4994 * with 'winfixwidth' set. */
4995 if (width > room - room_reserved)
4996 room_reserved = room - width;
4997 /* If there is only a 'winfixwidth' window and making the
4998 * window smaller, need to make the other window narrower. */
4999 if (take < 0 && room - curfrp->fr_width < room_reserved)
5000 room_reserved = 0;
5003 * set the current frame to the new width
5005 frame_new_width(curfrp, width, FALSE, FALSE);
5008 * First take lines from the frames right of the current frame. If
5009 * that is not enough, takes lines from frames left of the current
5010 * frame.
5012 for (run = 0; run < 2; ++run)
5014 if (run == 0)
5015 frp = curfrp->fr_next; /* 1st run: start with next window */
5016 else
5017 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5018 while (frp != NULL && take != 0)
5020 w = frame_minwidth(frp, NULL);
5021 if (room_reserved > 0
5022 && frp->fr_win != NULL
5023 && frp->fr_win->w_p_wfw)
5025 if (room_reserved >= frp->fr_width)
5026 room_reserved -= frp->fr_width;
5027 else
5029 if (frp->fr_width - room_reserved > take)
5030 room_reserved = frp->fr_width - take;
5031 take -= frp->fr_width - room_reserved;
5032 frame_new_width(frp, room_reserved, FALSE, FALSE);
5033 room_reserved = 0;
5036 else
5038 if (frp->fr_width - take < w)
5040 take -= frp->fr_width - w;
5041 frame_new_width(frp, w, FALSE, FALSE);
5043 else
5045 frame_new_width(frp, frp->fr_width - take,
5046 FALSE, FALSE);
5047 take = 0;
5050 if (run == 0)
5051 frp = frp->fr_next;
5052 else
5053 frp = frp->fr_prev;
5058 #endif /* FEAT_VERTSPLIT */
5061 * Check 'winminheight' for a valid value.
5063 void
5064 win_setminheight()
5066 int room;
5067 int first = TRUE;
5068 win_T *wp;
5070 /* loop until there is a 'winminheight' that is possible */
5071 while (p_wmh > 0)
5073 /* TODO: handle vertical splits */
5074 room = -p_wh;
5075 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5076 room += wp->w_height - p_wmh;
5077 if (room >= 0)
5078 break;
5079 --p_wmh;
5080 if (first)
5082 EMSG(_(e_noroom));
5083 first = FALSE;
5088 #ifdef FEAT_MOUSE
5091 * Status line of dragwin is dragged "offset" lines down (negative is up).
5093 void
5094 win_drag_status_line(dragwin, offset)
5095 win_T *dragwin;
5096 int offset;
5098 frame_T *curfr;
5099 frame_T *fr;
5100 int room;
5101 int row;
5102 int up; /* if TRUE, drag status line up, otherwise down */
5103 int n;
5105 fr = dragwin->w_frame;
5106 curfr = fr;
5107 if (fr != topframe) /* more than one window */
5109 fr = fr->fr_parent;
5110 /* When the parent frame is not a column of frames, its parent should
5111 * be. */
5112 if (fr->fr_layout != FR_COL)
5114 curfr = fr;
5115 if (fr != topframe) /* only a row of windows, may drag statusline */
5116 fr = fr->fr_parent;
5120 /* If this is the last frame in a column, may want to resize the parent
5121 * frame instead (go two up to skip a row of frames). */
5122 while (curfr != topframe && curfr->fr_next == NULL)
5124 if (fr != topframe)
5125 fr = fr->fr_parent;
5126 curfr = fr;
5127 if (fr != topframe)
5128 fr = fr->fr_parent;
5131 if (offset < 0) /* drag up */
5133 up = TRUE;
5134 offset = -offset;
5135 /* sum up the room of the current frame and above it */
5136 if (fr == curfr)
5138 /* only one window */
5139 room = fr->fr_height - frame_minheight(fr, NULL);
5141 else
5143 room = 0;
5144 for (fr = fr->fr_child; ; fr = fr->fr_next)
5146 room += fr->fr_height - frame_minheight(fr, NULL);
5147 if (fr == curfr)
5148 break;
5151 fr = curfr->fr_next; /* put fr at frame that grows */
5153 else /* drag down */
5155 up = FALSE;
5157 * Only dragging the last status line can reduce p_ch.
5159 room = Rows - cmdline_row;
5160 if (curfr->fr_next == NULL)
5161 room -= 1;
5162 else
5163 room -= p_ch;
5164 if (room < 0)
5165 room = 0;
5166 /* sum up the room of frames below of the current one */
5167 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5168 room += fr->fr_height - frame_minheight(fr, NULL);
5169 fr = curfr; /* put fr at window that grows */
5172 if (room < offset) /* Not enough room */
5173 offset = room; /* Move as far as we can */
5174 if (offset <= 0)
5175 return;
5178 * Grow frame fr by "offset" lines.
5179 * Doesn't happen when dragging the last status line up.
5181 if (fr != NULL)
5182 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5184 if (up)
5185 fr = curfr; /* current frame gets smaller */
5186 else
5187 fr = curfr->fr_next; /* next frame gets smaller */
5190 * Now make the other frames smaller.
5192 while (fr != NULL && offset > 0)
5194 n = frame_minheight(fr, NULL);
5195 if (fr->fr_height - offset <= n)
5197 offset -= fr->fr_height - n;
5198 frame_new_height(fr, n, !up, FALSE);
5200 else
5202 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5203 break;
5205 if (up)
5206 fr = fr->fr_prev;
5207 else
5208 fr = fr->fr_next;
5210 row = win_comp_pos();
5211 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5212 cmdline_row = row;
5213 p_ch = Rows - cmdline_row;
5214 if (p_ch < 1)
5215 p_ch = 1;
5216 curtab->tp_ch_used = p_ch;
5217 redraw_all_later(SOME_VALID);
5218 showmode();
5221 #ifdef FEAT_VERTSPLIT
5223 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5225 void
5226 win_drag_vsep_line(dragwin, offset)
5227 win_T *dragwin;
5228 int offset;
5230 frame_T *curfr;
5231 frame_T *fr;
5232 int room;
5233 int left; /* if TRUE, drag separator line left, otherwise right */
5234 int n;
5236 fr = dragwin->w_frame;
5237 if (fr == topframe) /* only one window (cannot happen?) */
5238 return;
5239 curfr = fr;
5240 fr = fr->fr_parent;
5241 /* When the parent frame is not a row of frames, its parent should be. */
5242 if (fr->fr_layout != FR_ROW)
5244 if (fr == topframe) /* only a column of windows (cannot happen?) */
5245 return;
5246 curfr = fr;
5247 fr = fr->fr_parent;
5250 /* If this is the last frame in a row, may want to resize a parent
5251 * frame instead. */
5252 while (curfr->fr_next == NULL)
5254 if (fr == topframe)
5255 break;
5256 curfr = fr;
5257 fr = fr->fr_parent;
5258 if (fr != topframe)
5260 curfr = fr;
5261 fr = fr->fr_parent;
5265 if (offset < 0) /* drag left */
5267 left = TRUE;
5268 offset = -offset;
5269 /* sum up the room of the current frame and left of it */
5270 room = 0;
5271 for (fr = fr->fr_child; ; fr = fr->fr_next)
5273 room += fr->fr_width - frame_minwidth(fr, NULL);
5274 if (fr == curfr)
5275 break;
5277 fr = curfr->fr_next; /* put fr at frame that grows */
5279 else /* drag right */
5281 left = FALSE;
5282 /* sum up the room of frames right of the current one */
5283 room = 0;
5284 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5285 room += fr->fr_width - frame_minwidth(fr, NULL);
5286 fr = curfr; /* put fr at window that grows */
5289 if (room < offset) /* Not enough room */
5290 offset = room; /* Move as far as we can */
5291 if (offset <= 0) /* No room at all, quit. */
5292 return;
5294 /* grow frame fr by offset lines */
5295 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5297 /* shrink other frames: current and at the left or at the right */
5298 if (left)
5299 fr = curfr; /* current frame gets smaller */
5300 else
5301 fr = curfr->fr_next; /* next frame gets smaller */
5303 while (fr != NULL && offset > 0)
5305 n = frame_minwidth(fr, NULL);
5306 if (fr->fr_width - offset <= n)
5308 offset -= fr->fr_width - n;
5309 frame_new_width(fr, n, !left, FALSE);
5311 else
5313 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5314 break;
5316 if (left)
5317 fr = fr->fr_prev;
5318 else
5319 fr = fr->fr_next;
5321 (void)win_comp_pos();
5322 redraw_all_later(NOT_VALID);
5324 #endif /* FEAT_VERTSPLIT */
5325 #endif /* FEAT_MOUSE */
5327 #endif /* FEAT_WINDOWS */
5330 * Set the height of a window.
5331 * This takes care of the things inside the window, not what happens to the
5332 * window position, the frame or to other windows.
5334 static void
5335 win_new_height(wp, height)
5336 win_T *wp;
5337 int height;
5339 linenr_T lnum;
5340 int sline, line_size;
5341 #define FRACTION_MULT 16384L
5343 /* Don't want a negative height. Happens when splitting a tiny window.
5344 * Will equalize heights soon to fix it. */
5345 if (height < 0)
5346 height = 0;
5347 if (wp->w_height == height)
5348 return; /* nothing to do */
5350 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5351 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5352 + FRACTION_MULT / 2) / (long)wp->w_height;
5354 wp->w_height = height;
5355 wp->w_skipcol = 0;
5357 /* Don't change w_topline when height is zero. Don't set w_topline when
5358 * 'scrollbind' is set and this isn't the current window. */
5359 if (height > 0
5360 #ifdef FEAT_SCROLLBIND
5361 && (!wp->w_p_scb || wp == curwin)
5362 #endif
5366 * Find a value for w_topline that shows the cursor at the same
5367 * relative position in the window as before (more or less).
5369 lnum = wp->w_cursor.lnum;
5370 if (lnum < 1) /* can happen when starting up */
5371 lnum = 1;
5372 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5373 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5374 sline = wp->w_wrow - line_size;
5376 if (sline >= 0)
5378 /* Make sure the whole cursor line is visible, if possible. */
5379 int rows = plines_win(wp, lnum, FALSE);
5381 if (sline > wp->w_height - rows)
5383 sline = wp->w_height - rows;
5384 wp->w_wrow -= rows - line_size;
5388 if (sline < 0)
5391 * Cursor line would go off top of screen if w_wrow was this high.
5392 * Make cursor line the first line in the window. If not enough
5393 * room use w_skipcol;
5395 wp->w_wrow = line_size;
5396 if (wp->w_wrow >= wp->w_height
5397 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5399 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5400 --wp->w_wrow;
5401 while (wp->w_wrow >= wp->w_height)
5403 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5404 + win_col_off2(wp);
5405 --wp->w_wrow;
5409 else
5411 while (sline > 0 && lnum > 1)
5413 #ifdef FEAT_FOLDING
5414 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5415 if (lnum == 1)
5417 /* first line in buffer is folded */
5418 line_size = 1;
5419 --sline;
5420 break;
5422 #endif
5423 --lnum;
5424 #ifdef FEAT_DIFF
5425 if (lnum == wp->w_topline)
5426 line_size = plines_win_nofill(wp, lnum, TRUE)
5427 + wp->w_topfill;
5428 else
5429 #endif
5430 line_size = plines_win(wp, lnum, TRUE);
5431 sline -= line_size;
5434 if (sline < 0)
5437 * Line we want at top would go off top of screen. Use next
5438 * line instead.
5440 #ifdef FEAT_FOLDING
5441 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5442 #endif
5443 lnum++;
5444 wp->w_wrow -= line_size + sline;
5446 else if (sline > 0)
5448 /* First line of file reached, use that as topline. */
5449 lnum = 1;
5450 wp->w_wrow -= sline;
5453 set_topline(wp, lnum);
5456 if (wp == curwin)
5458 if (p_so)
5459 update_topline();
5460 curs_columns(FALSE); /* validate w_wrow */
5462 wp->w_prev_fraction_row = wp->w_wrow;
5464 win_comp_scroll(wp);
5465 redraw_win_later(wp, SOME_VALID);
5466 #ifdef FEAT_WINDOWS
5467 wp->w_redr_status = TRUE;
5468 #endif
5469 invalidate_botline_win(wp);
5472 #ifdef FEAT_VERTSPLIT
5474 * Set the width of a window.
5476 static void
5477 win_new_width(wp, width)
5478 win_T *wp;
5479 int width;
5481 wp->w_width = width;
5482 wp->w_lines_valid = 0;
5483 changed_line_abv_curs_win(wp);
5484 invalidate_botline_win(wp);
5485 if (wp == curwin)
5487 update_topline();
5488 curs_columns(TRUE); /* validate w_wrow */
5490 redraw_win_later(wp, NOT_VALID);
5491 wp->w_redr_status = TRUE;
5493 #endif
5495 void
5496 win_comp_scroll(wp)
5497 win_T *wp;
5499 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5500 if (wp->w_p_scr == 0)
5501 wp->w_p_scr = 1;
5505 * command_height: called whenever p_ch has been changed
5507 void
5508 command_height()
5510 #ifdef FEAT_WINDOWS
5511 int h;
5512 frame_T *frp;
5513 int old_p_ch = curtab->tp_ch_used;
5515 /* Use the value of p_ch that we remembered. This is needed for when the
5516 * GUI starts up, we can't be sure in what order things happen. And when
5517 * p_ch was changed in another tab page. */
5518 curtab->tp_ch_used = p_ch;
5520 /* Find bottom frame with width of screen. */
5521 frp = lastwin->w_frame;
5522 # ifdef FEAT_VERTSPLIT
5523 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5524 frp = frp->fr_parent;
5525 # endif
5527 /* Avoid changing the height of a window with 'winfixheight' set. */
5528 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5529 && frp->fr_win->w_p_wfh)
5530 frp = frp->fr_prev;
5532 if (starting != NO_SCREEN)
5534 cmdline_row = Rows - p_ch;
5536 if (p_ch > old_p_ch) /* p_ch got bigger */
5538 while (p_ch > old_p_ch)
5540 if (frp == NULL)
5542 EMSG(_(e_noroom));
5543 p_ch = old_p_ch;
5544 curtab->tp_ch_used = p_ch;
5545 cmdline_row = Rows - p_ch;
5546 break;
5548 h = frp->fr_height - frame_minheight(frp, NULL);
5549 if (h > p_ch - old_p_ch)
5550 h = p_ch - old_p_ch;
5551 old_p_ch += h;
5552 frame_add_height(frp, -h);
5553 frp = frp->fr_prev;
5556 /* Recompute window positions. */
5557 (void)win_comp_pos();
5559 /* clear the lines added to cmdline */
5560 if (full_screen)
5561 screen_fill((int)(cmdline_row), (int)Rows, 0,
5562 (int)Columns, ' ', ' ', 0);
5563 msg_row = cmdline_row;
5564 redraw_cmdline = TRUE;
5565 return;
5568 if (msg_row < cmdline_row)
5569 msg_row = cmdline_row;
5570 redraw_cmdline = TRUE;
5572 frame_add_height(frp, (int)(old_p_ch - p_ch));
5574 /* Recompute window positions. */
5575 if (frp != lastwin->w_frame)
5576 (void)win_comp_pos();
5577 #else
5578 cmdline_row = Rows - p_ch;
5579 win_setheight(cmdline_row);
5580 #endif
5583 #if defined(FEAT_WINDOWS) || defined(PROTO)
5585 * Resize frame "frp" to be "n" lines higher (negative for less high).
5586 * Also resize the frames it is contained in.
5588 static void
5589 frame_add_height(frp, n)
5590 frame_T *frp;
5591 int n;
5593 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5594 for (;;)
5596 frp = frp->fr_parent;
5597 if (frp == NULL)
5598 break;
5599 frp->fr_height += n;
5604 * Add or remove a status line for the bottom window(s), according to the
5605 * value of 'laststatus'.
5607 void
5608 last_status(morewin)
5609 int morewin; /* pretend there are two or more windows */
5611 /* Don't make a difference between horizontal or vertical split. */
5612 last_status_rec(topframe, (p_ls == 2
5613 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5616 static void
5617 last_status_rec(fr, statusline)
5618 frame_T *fr;
5619 int statusline;
5621 frame_T *fp;
5622 win_T *wp;
5624 if (fr->fr_layout == FR_LEAF)
5626 wp = fr->fr_win;
5627 if (wp->w_status_height != 0 && !statusline)
5629 /* remove status line */
5630 win_new_height(wp, wp->w_height + 1);
5631 wp->w_status_height = 0;
5632 comp_col();
5634 else if (wp->w_status_height == 0 && statusline)
5636 /* Find a frame to take a line from. */
5637 fp = fr;
5638 while (fp->fr_height <= frame_minheight(fp, NULL))
5640 if (fp == topframe)
5642 EMSG(_(e_noroom));
5643 return;
5645 /* In a column of frames: go to frame above. If already at
5646 * the top or in a row of frames: go to parent. */
5647 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5648 fp = fp->fr_prev;
5649 else
5650 fp = fp->fr_parent;
5652 wp->w_status_height = 1;
5653 if (fp != fr)
5655 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5656 frame_fix_height(wp);
5657 (void)win_comp_pos();
5659 else
5660 win_new_height(wp, wp->w_height - 1);
5661 comp_col();
5662 redraw_all_later(SOME_VALID);
5665 #ifdef FEAT_VERTSPLIT
5666 else if (fr->fr_layout == FR_ROW)
5668 /* vertically split windows, set status line for each one */
5669 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5670 last_status_rec(fp, statusline);
5672 #endif
5673 else
5675 /* horizontally split window, set status line for last one */
5676 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5678 last_status_rec(fp, statusline);
5683 * Return the number of lines used by the tab page line.
5686 tabline_height()
5688 #ifdef FEAT_GUI_TABLINE
5689 /* When the GUI has the tabline then this always returns zero. */
5690 if (gui_use_tabline())
5691 return 0;
5692 #endif
5693 switch (p_stal)
5695 case 0: return 0;
5696 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5698 return 1;
5701 #endif /* FEAT_WINDOWS */
5703 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5705 * Get the file name at the cursor.
5706 * If Visual mode is active, use the selected text if it's in one line.
5707 * Returns the name in allocated memory, NULL for failure.
5709 char_u *
5710 grab_file_name(count, file_lnum)
5711 long count;
5712 linenr_T *file_lnum;
5714 # ifdef FEAT_VISUAL
5715 if (VIsual_active)
5717 int len;
5718 char_u *ptr;
5720 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5721 return NULL;
5722 return find_file_name_in_path(ptr, len,
5723 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5725 # endif
5726 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5727 file_lnum);
5732 * Return the file name under or after the cursor.
5734 * The 'path' option is searched if the file name is not absolute.
5735 * The string returned has been alloc'ed and should be freed by the caller.
5736 * NULL is returned if the file name or file is not found.
5738 * options:
5739 * FNAME_MESS give error messages
5740 * FNAME_EXP expand to path
5741 * FNAME_HYP check for hypertext link
5742 * FNAME_INCL apply "includeexpr"
5744 char_u *
5745 file_name_at_cursor(options, count, file_lnum)
5746 int options;
5747 long count;
5748 linenr_T *file_lnum;
5750 return file_name_in_line(ml_get_curline(),
5751 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5752 file_lnum);
5756 * Return the name of the file under or after ptr[col].
5757 * Otherwise like file_name_at_cursor().
5759 char_u *
5760 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5761 char_u *line;
5762 int col;
5763 int options;
5764 long count;
5765 char_u *rel_fname; /* file we are searching relative to */
5766 linenr_T *file_lnum; /* line number after the file name */
5768 char_u *ptr;
5769 int len;
5772 * search forward for what could be the start of a file name
5774 ptr = line + col;
5775 while (*ptr != NUL && !vim_isfilec(*ptr))
5776 mb_ptr_adv(ptr);
5777 if (*ptr == NUL) /* nothing found */
5779 if (options & FNAME_MESS)
5780 EMSG(_("E446: No file name under cursor"));
5781 return NULL;
5785 * Search backward for first char of the file name.
5786 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5788 while (ptr > line)
5790 #ifdef FEAT_MBYTE
5791 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5792 ptr -= len + 1;
5793 else
5794 #endif
5795 if (vim_isfilec(ptr[-1])
5796 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5797 --ptr;
5798 else
5799 break;
5803 * Search forward for the last char of the file name.
5804 * Also allow "://" when ':' is not in 'isfname'.
5806 len = 0;
5807 while (vim_isfilec(ptr[len])
5808 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5809 #ifdef FEAT_MBYTE
5810 if (has_mbyte)
5811 len += (*mb_ptr2len)(ptr + len);
5812 else
5813 #endif
5814 ++len;
5817 * If there is trailing punctuation, remove it.
5818 * But don't remove "..", could be a directory name.
5820 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5821 && ptr[len - 2] != '.')
5822 --len;
5824 if (file_lnum != NULL)
5826 char_u *p;
5828 /* Get the number after the file name and a separator character */
5829 p = ptr + len;
5830 p = skipwhite(p);
5831 if (*p != NUL)
5833 if (!isdigit(*p))
5834 ++p; /* skip the separator */
5835 p = skipwhite(p);
5836 if (isdigit(*p))
5837 *file_lnum = (int)getdigits(&p);
5841 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5844 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5845 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5847 static char_u *
5848 eval_includeexpr(ptr, len)
5849 char_u *ptr;
5850 int len;
5852 char_u *res;
5854 set_vim_var_string(VV_FNAME, ptr, len);
5855 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5856 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5857 set_vim_var_string(VV_FNAME, NULL, 0);
5858 return res;
5860 #endif
5863 * Return the name of the file ptr[len] in 'path'.
5864 * Otherwise like file_name_at_cursor().
5866 char_u *
5867 find_file_name_in_path(ptr, len, options, count, rel_fname)
5868 char_u *ptr;
5869 int len;
5870 int options;
5871 long count;
5872 char_u *rel_fname; /* file we are searching relative to */
5874 char_u *file_name;
5875 int c;
5876 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5877 char_u *tofree = NULL;
5879 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5881 tofree = eval_includeexpr(ptr, len);
5882 if (tofree != NULL)
5884 ptr = tofree;
5885 len = (int)STRLEN(ptr);
5888 # endif
5890 if (options & FNAME_EXP)
5892 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5893 TRUE, rel_fname);
5895 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5897 * If the file could not be found in a normal way, try applying
5898 * 'includeexpr' (unless done already).
5900 if (file_name == NULL
5901 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5903 tofree = eval_includeexpr(ptr, len);
5904 if (tofree != NULL)
5906 ptr = tofree;
5907 len = (int)STRLEN(ptr);
5908 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5909 TRUE, rel_fname);
5912 # endif
5913 if (file_name == NULL && (options & FNAME_MESS))
5915 c = ptr[len];
5916 ptr[len] = NUL;
5917 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5918 ptr[len] = c;
5921 /* Repeat finding the file "count" times. This matters when it
5922 * appears several times in the path. */
5923 while (file_name != NULL && --count > 0)
5925 vim_free(file_name);
5926 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5929 else
5930 file_name = vim_strnsave(ptr, len);
5932 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5933 vim_free(tofree);
5934 # endif
5936 return file_name;
5938 #endif /* FEAT_SEARCHPATH */
5941 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5942 * Also check for ":\\", which MS Internet Explorer accepts, return
5943 * URL_BACKSLASH.
5945 static int
5946 path_is_url(p)
5947 char_u *p;
5949 if (STRNCMP(p, "://", (size_t)3) == 0)
5950 return URL_SLASH;
5951 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5952 return URL_BACKSLASH;
5953 return 0;
5957 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5958 * Return URL_BACKSLASH for "name:\\".
5959 * Return zero otherwise.
5962 path_with_url(fname)
5963 char_u *fname;
5965 char_u *p;
5967 for (p = fname; isalpha(*p); ++p)
5969 return path_is_url(p);
5973 * Return TRUE if "name" is a full (absolute) path name or URL.
5976 vim_isAbsName(name)
5977 char_u *name;
5979 return (path_with_url(name) != 0 || mch_isFullName(name));
5983 * Get absolute file name into buffer "buf[len]".
5985 * return FAIL for failure, OK otherwise
5988 vim_FullName(fname, buf, len, force)
5989 char_u *fname, *buf;
5990 int len;
5991 int force; /* force expansion even when already absolute */
5993 int retval = OK;
5994 int url;
5996 *buf = NUL;
5997 if (fname == NULL)
5998 return FAIL;
6000 url = path_with_url(fname);
6001 if (!url)
6002 retval = mch_FullName(fname, buf, len, force);
6003 if (url || retval == FAIL)
6005 /* something failed; use the file name (truncate when too long) */
6006 vim_strncpy(buf, fname, len - 1);
6008 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6009 slash_adjust(buf);
6010 #endif
6011 return retval;
6015 * Return the minimal number of rows that is needed on the screen to display
6016 * the current number of windows.
6019 min_rows()
6021 int total;
6022 #ifdef FEAT_WINDOWS
6023 tabpage_T *tp;
6024 int n;
6025 #endif
6027 if (firstwin == NULL) /* not initialized yet */
6028 return MIN_LINES;
6030 #ifdef FEAT_WINDOWS
6031 total = 0;
6032 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6034 n = frame_minheight(tp->tp_topframe, NULL);
6035 if (total < n)
6036 total = n;
6038 total += tabline_height();
6039 #else
6040 total = 1; /* at least one window should have a line! */
6041 #endif
6042 total += 1; /* count the room for the command line */
6043 return total;
6047 * Return TRUE if there is only one window (in the current tab page), not
6048 * counting a help or preview window, unless it is the current window.
6051 only_one_window()
6053 #ifdef FEAT_WINDOWS
6054 int count = 0;
6055 win_T *wp;
6057 /* If there is another tab page there always is another window. */
6058 if (first_tabpage->tp_next != NULL)
6059 return FALSE;
6061 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6062 if (!((wp->w_buffer->b_help && !curbuf->b_help)
6063 # ifdef FEAT_QUICKFIX
6064 || wp->w_p_pvw
6065 # endif
6066 ) || wp == curwin)
6067 ++count;
6068 return (count <= 1);
6069 #else
6070 return TRUE;
6071 #endif
6074 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6076 * Correct the cursor line number in other windows. Used after changing the
6077 * current buffer, and before applying autocommands.
6078 * When "do_curwin" is TRUE, also check current window.
6080 void
6081 check_lnums(do_curwin)
6082 int do_curwin;
6084 win_T *wp;
6086 #ifdef FEAT_WINDOWS
6087 tabpage_T *tp;
6089 FOR_ALL_TAB_WINDOWS(tp, wp)
6090 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6091 #else
6092 wp = curwin;
6093 if (do_curwin)
6094 #endif
6096 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6097 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6098 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6099 wp->w_topline = curbuf->b_ml.ml_line_count;
6102 #endif
6104 #if defined(FEAT_WINDOWS) || defined(PROTO)
6107 * A snapshot of the window sizes, to restore them after closing the help
6108 * window.
6109 * Only these fields are used:
6110 * fr_layout
6111 * fr_width
6112 * fr_height
6113 * fr_next
6114 * fr_child
6115 * fr_win (only valid for the old curwin, NULL otherwise)
6119 * Create a snapshot of the current frame sizes.
6121 static void
6122 make_snapshot()
6124 clear_snapshot(curtab);
6125 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6128 static void
6129 make_snapshot_rec(fr, frp)
6130 frame_T *fr;
6131 frame_T **frp;
6133 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6134 if (*frp == NULL)
6135 return;
6136 (*frp)->fr_layout = fr->fr_layout;
6137 # ifdef FEAT_VERTSPLIT
6138 (*frp)->fr_width = fr->fr_width;
6139 # endif
6140 (*frp)->fr_height = fr->fr_height;
6141 if (fr->fr_next != NULL)
6142 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6143 if (fr->fr_child != NULL)
6144 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6145 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6146 (*frp)->fr_win = curwin;
6150 * Remove any existing snapshot.
6152 static void
6153 clear_snapshot(tp)
6154 tabpage_T *tp;
6156 clear_snapshot_rec(tp->tp_snapshot);
6157 tp->tp_snapshot = NULL;
6160 static void
6161 clear_snapshot_rec(fr)
6162 frame_T *fr;
6164 if (fr != NULL)
6166 clear_snapshot_rec(fr->fr_next);
6167 clear_snapshot_rec(fr->fr_child);
6168 vim_free(fr);
6173 * Restore a previously created snapshot, if there is any.
6174 * This is only done if the screen size didn't change and the window layout is
6175 * still the same.
6177 static void
6178 restore_snapshot(close_curwin)
6179 int close_curwin; /* closing current window */
6181 win_T *wp;
6183 if (curtab->tp_snapshot != NULL
6184 # ifdef FEAT_VERTSPLIT
6185 && curtab->tp_snapshot->fr_width == topframe->fr_width
6186 # endif
6187 && curtab->tp_snapshot->fr_height == topframe->fr_height
6188 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6190 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6191 win_comp_pos();
6192 if (wp != NULL && close_curwin)
6193 win_goto(wp);
6194 redraw_all_later(CLEAR);
6196 clear_snapshot(curtab);
6200 * Check if frames "sn" and "fr" have the same layout, same following frames
6201 * and same children.
6203 static int
6204 check_snapshot_rec(sn, fr)
6205 frame_T *sn;
6206 frame_T *fr;
6208 if (sn->fr_layout != fr->fr_layout
6209 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6210 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6211 || (sn->fr_next != NULL
6212 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6213 || (sn->fr_child != NULL
6214 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6215 return FAIL;
6216 return OK;
6220 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6221 * following frames and children.
6222 * Returns a pointer to the old current window, or NULL.
6224 static win_T *
6225 restore_snapshot_rec(sn, fr)
6226 frame_T *sn;
6227 frame_T *fr;
6229 win_T *wp = NULL;
6230 win_T *wp2;
6232 fr->fr_height = sn->fr_height;
6233 # ifdef FEAT_VERTSPLIT
6234 fr->fr_width = sn->fr_width;
6235 # endif
6236 if (fr->fr_layout == FR_LEAF)
6238 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6239 # ifdef FEAT_VERTSPLIT
6240 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6241 # endif
6242 wp = sn->fr_win;
6244 if (sn->fr_next != NULL)
6246 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6247 if (wp2 != NULL)
6248 wp = wp2;
6250 if (sn->fr_child != NULL)
6252 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6253 if (wp2 != NULL)
6254 wp = wp2;
6256 return wp;
6259 #endif
6261 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6263 * Return TRUE if there is any vertically split window.
6266 win_hasvertsplit()
6268 frame_T *fr;
6270 if (topframe->fr_layout == FR_ROW)
6271 return TRUE;
6273 if (topframe->fr_layout == FR_COL)
6274 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6275 if (fr->fr_layout == FR_ROW)
6276 return TRUE;
6278 return FALSE;
6280 #endif
6282 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6284 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6285 * highlighted with the group 'grp' with priority 'prio'.
6286 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6287 * If no particular ID is desired, -1 must be specified for 'id'.
6288 * Return ID of added match, -1 on failure.
6291 match_add(wp, grp, pat, prio, id)
6292 win_T *wp;
6293 char_u *grp;
6294 char_u *pat;
6295 int prio;
6296 int id;
6298 matchitem_T *cur;
6299 matchitem_T *prev;
6300 matchitem_T *m;
6301 int hlg_id;
6302 regprog_T *regprog;
6304 if (*grp == NUL || *pat == NUL)
6305 return -1;
6306 if (id < -1 || id == 0)
6308 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6309 return -1;
6311 if (id != -1)
6313 cur = wp->w_match_head;
6314 while (cur != NULL)
6316 if (cur->id == id)
6318 EMSGN("E801: ID already taken: %ld", id);
6319 return -1;
6321 cur = cur->next;
6324 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6326 EMSG2(_(e_nogroup), grp);
6327 return -1;
6329 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6331 EMSG2(_(e_invarg2), pat);
6332 return -1;
6335 /* Find available match ID. */
6336 while (id == -1)
6338 cur = wp->w_match_head;
6339 while (cur != NULL && cur->id != wp->w_next_match_id)
6340 cur = cur->next;
6341 if (cur == NULL)
6342 id = wp->w_next_match_id;
6343 wp->w_next_match_id++;
6346 /* Build new match. */
6347 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6348 m->id = id;
6349 m->priority = prio;
6350 m->pattern = vim_strsave(pat);
6351 m->hlg_id = hlg_id;
6352 m->match.regprog = regprog;
6353 m->match.rmm_ic = FALSE;
6354 m->match.rmm_maxcol = 0;
6356 /* Insert new match. The match list is in ascending order with regard to
6357 * the match priorities. */
6358 cur = wp->w_match_head;
6359 prev = cur;
6360 while (cur != NULL && prio >= cur->priority)
6362 prev = cur;
6363 cur = cur->next;
6365 if (cur == prev)
6366 wp->w_match_head = m;
6367 else
6368 prev->next = m;
6369 m->next = cur;
6371 redraw_later(SOME_VALID);
6372 return id;
6376 * Delete match with ID 'id' in the match list of window 'wp'.
6377 * Print error messages if 'perr' is TRUE.
6380 match_delete(wp, id, perr)
6381 win_T *wp;
6382 int id;
6383 int perr;
6385 matchitem_T *cur = wp->w_match_head;
6386 matchitem_T *prev = cur;
6388 if (id < 1)
6390 if (perr == TRUE)
6391 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6392 id);
6393 return -1;
6395 while (cur != NULL && cur->id != id)
6397 prev = cur;
6398 cur = cur->next;
6400 if (cur == NULL)
6402 if (perr == TRUE)
6403 EMSGN("E803: ID not found: %ld", id);
6404 return -1;
6406 if (cur == prev)
6407 wp->w_match_head = cur->next;
6408 else
6409 prev->next = cur->next;
6410 vim_free(cur->match.regprog);
6411 vim_free(cur->pattern);
6412 vim_free(cur);
6413 redraw_later(SOME_VALID);
6414 return 0;
6418 * Delete all matches in the match list of window 'wp'.
6420 void
6421 clear_matches(wp)
6422 win_T *wp;
6424 matchitem_T *m;
6426 while (wp->w_match_head != NULL)
6428 m = wp->w_match_head->next;
6429 vim_free(wp->w_match_head->match.regprog);
6430 vim_free(wp->w_match_head->pattern);
6431 vim_free(wp->w_match_head);
6432 wp->w_match_head = m;
6434 redraw_later(SOME_VALID);
6438 * Get match from ID 'id' in window 'wp'.
6439 * Return NULL if match not found.
6441 matchitem_T *
6442 get_match(wp, id)
6443 win_T *wp;
6444 int id;
6446 matchitem_T *cur = wp->w_match_head;
6448 while (cur != NULL && cur->id != id)
6449 cur = cur->next;
6450 return cur;
6452 #endif