Enable trackpad scrolling over the black area in full-screen
[MacVim/jjgod.git] / src / window.c
blob74e36468728d4e86be52d54ae9061d5abdfc21c3
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 #ifdef HAVE_FCNTL_H
13 # include <fcntl.h> /* for chdir() */
14 #endif
16 static int path_is_url __ARGS((char_u *p));
17 #if defined(FEAT_WINDOWS) || defined(PROTO)
18 static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
19 static void win_init __ARGS((win_T *newp, win_T *oldp));
20 static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21 static void frame_setheight __ARGS((frame_T *curfrp, int height));
22 #ifdef FEAT_VERTSPLIT
23 static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24 #endif
25 static void win_exchange __ARGS((long));
26 static void win_rotate __ARGS((int, int));
27 static void win_totop __ARGS((int size, int flags));
28 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));
29 static int last_window __ARGS((void));
30 static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31 static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32 static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
33 static tabpage_T *alt_tabpage __ARGS((void));
34 static win_T *frame2win __ARGS((frame_T *frp));
35 static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36 static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37 static int frame_fixed_height __ARGS((frame_T *frp));
38 #ifdef FEAT_VERTSPLIT
39 static int frame_fixed_width __ARGS((frame_T *frp));
40 static void frame_add_statusline __ARGS((frame_T *frp));
41 static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
42 static void frame_add_vsep __ARGS((frame_T *frp));
43 static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44 static void frame_fix_width __ARGS((win_T *wp));
45 #endif
46 #endif
47 static int win_alloc_firstwin __ARGS((win_T *oldwin));
48 #if defined(FEAT_WINDOWS) || defined(PROTO)
49 static tabpage_T *alloc_tabpage __ARGS((void));
50 static int leave_tabpage __ARGS((buf_T *new_curbuf));
51 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
52 static void frame_fix_height __ARGS((win_T *wp));
53 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
55 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
56 static void win_append __ARGS((win_T *, win_T *));
57 static void win_remove __ARGS((win_T *, tabpage_T *tp));
58 static void frame_append __ARGS((frame_T *after, frame_T *frp));
59 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60 static void frame_remove __ARGS((frame_T *frp));
61 #ifdef FEAT_VERTSPLIT
62 static void win_new_width __ARGS((win_T *wp, int width));
63 static void win_goto_ver __ARGS((int up, long count));
64 static void win_goto_hor __ARGS((int left, long count));
65 #endif
66 static void frame_add_height __ARGS((frame_T *frp, int n));
67 static void last_status_rec __ARGS((frame_T *fr, int statusline));
69 static void make_snapshot __ARGS((void));
70 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
71 static void clear_snapshot __ARGS((tabpage_T *tp));
72 static void clear_snapshot_rec __ARGS((frame_T *fr));
73 static void restore_snapshot __ARGS((int close_curwin));
74 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
77 #endif /* FEAT_WINDOWS */
79 static win_T *win_alloc __ARGS((win_T *after));
80 static void win_new_height __ARGS((win_T *, int));
82 #define URL_SLASH 1 /* path_is_url() has found "://" */
83 #define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
85 #define NOWIN (win_T *)-1 /* non-existing window */
87 #ifdef FEAT_WINDOWS
88 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
89 #else
90 # define ROWS_AVAIL (Rows - p_ch)
91 #endif
93 #if defined(FEAT_WINDOWS) || defined(PROTO)
95 static char *m_onlyone = N_("Already only one window");
98 * all CTRL-W window commands are handled here, called from normal_cmd().
100 void
101 do_window(nchar, Prenum, xchar)
102 int nchar;
103 long Prenum;
104 int xchar; /* extra char from ":wincmd gx" or NUL */
106 long Prenum1;
107 win_T *wp;
108 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
109 char_u *ptr;
110 linenr_T lnum = -1;
111 #endif
112 #ifdef FEAT_FIND_ID
113 int type = FIND_DEFINE;
114 int len;
115 #endif
116 char_u cbuf[40];
118 if (Prenum == 0)
119 Prenum1 = 1;
120 else
121 Prenum1 = Prenum;
123 #ifdef FEAT_CMDWIN
124 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
125 #else
126 # define CHECK_CMDWIN
127 #endif
129 switch (nchar)
131 /* split current window in two parts, horizontally */
132 case 'S':
133 case Ctrl_S:
134 case 's':
135 CHECK_CMDWIN
136 #ifdef FEAT_VISUAL
137 reset_VIsual_and_resel(); /* stop Visual mode */
138 #endif
139 #ifdef FEAT_QUICKFIX
140 /* When splitting the quickfix window open a new buffer in it,
141 * don't replicate the quickfix buffer. */
142 if (bt_quickfix(curbuf))
143 goto newwindow;
144 #endif
145 #ifdef FEAT_GUI
146 need_mouse_correct = TRUE;
147 #endif
148 win_split((int)Prenum, 0);
149 break;
151 #ifdef FEAT_VERTSPLIT
152 /* split current window in two parts, vertically */
153 case Ctrl_V:
154 case 'v':
155 CHECK_CMDWIN
156 #ifdef FEAT_VISUAL
157 reset_VIsual_and_resel(); /* stop Visual mode */
158 #endif
159 #ifdef FEAT_GUI
160 need_mouse_correct = TRUE;
161 #endif
162 win_split((int)Prenum, WSP_VERT);
163 break;
164 #endif
166 /* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170 #ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172 #endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
175 sprintf((char *)cbuf + 7, "%ld", Prenum);
176 do_cmdline_cmd(cbuf);
177 break;
179 /* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183 #ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185 #endif
186 #ifdef FEAT_QUICKFIX
187 newwindow:
188 #endif
189 if (Prenum)
190 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
191 else
192 cbuf[0] = NUL;
193 STRCAT(cbuf, "new");
194 do_cmdline_cmd(cbuf);
195 break;
197 /* quit current window */
198 case Ctrl_Q:
199 case 'q':
200 #ifdef FEAT_VISUAL
201 reset_VIsual_and_resel(); /* stop Visual mode */
202 #endif
203 do_cmdline_cmd((char_u *)"quit");
204 break;
206 /* close current window */
207 case Ctrl_C:
208 case 'c':
209 #ifdef FEAT_VISUAL
210 reset_VIsual_and_resel(); /* stop Visual mode */
211 #endif
212 do_cmdline_cmd((char_u *)"close");
213 break;
215 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
216 /* close preview window */
217 case Ctrl_Z:
218 case 'z':
219 CHECK_CMDWIN
220 #ifdef FEAT_VISUAL
221 reset_VIsual_and_resel(); /* stop Visual mode */
222 #endif
223 do_cmdline_cmd((char_u *)"pclose");
224 break;
226 /* cursor to preview window */
227 case 'P':
228 for (wp = firstwin; wp != NULL; wp = wp->w_next)
229 if (wp->w_p_pvw)
230 break;
231 if (wp == NULL)
232 EMSG(_("E441: There is no preview window"));
233 else
234 win_goto(wp);
235 break;
236 #endif
238 /* close all but current window */
239 case Ctrl_O:
240 case 'o':
241 CHECK_CMDWIN
242 #ifdef FEAT_VISUAL
243 reset_VIsual_and_resel(); /* stop Visual mode */
244 #endif
245 do_cmdline_cmd((char_u *)"only");
246 break;
248 /* cursor to next window with wrap around */
249 case Ctrl_W:
250 case 'w':
251 /* cursor to previous window with wrap around */
252 case 'W':
253 CHECK_CMDWIN
254 if (lastwin == firstwin && Prenum != 1) /* just one window */
255 beep_flush();
256 else
258 if (Prenum) /* go to specified window */
260 for (wp = firstwin; --Prenum > 0; )
262 if (wp->w_next == NULL)
263 break;
264 else
265 wp = wp->w_next;
268 else
270 if (nchar == 'W') /* go to previous window */
272 wp = curwin->w_prev;
273 if (wp == NULL)
274 wp = lastwin; /* wrap around */
276 else /* go to next window */
278 wp = curwin->w_next;
279 if (wp == NULL)
280 wp = firstwin; /* wrap around */
283 win_goto(wp);
285 break;
287 /* cursor to window below */
288 case 'j':
289 case K_DOWN:
290 case Ctrl_J:
291 CHECK_CMDWIN
292 #ifdef FEAT_VERTSPLIT
293 win_goto_ver(FALSE, Prenum1);
294 #else
295 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
296 wp = wp->w_next)
298 win_goto(wp);
299 #endif
300 break;
302 /* cursor to window above */
303 case 'k':
304 case K_UP:
305 case Ctrl_K:
306 CHECK_CMDWIN
307 #ifdef FEAT_VERTSPLIT
308 win_goto_ver(TRUE, Prenum1);
309 #else
310 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
311 wp = wp->w_prev)
313 win_goto(wp);
314 #endif
315 break;
317 #ifdef FEAT_VERTSPLIT
318 /* cursor to left window */
319 case 'h':
320 case K_LEFT:
321 case Ctrl_H:
322 case K_BS:
323 CHECK_CMDWIN
324 win_goto_hor(TRUE, Prenum1);
325 break;
327 /* cursor to right window */
328 case 'l':
329 case K_RIGHT:
330 case Ctrl_L:
331 CHECK_CMDWIN
332 win_goto_hor(FALSE, Prenum1);
333 break;
334 #endif
336 /* move window to new tab page */
337 case 'T':
338 if (firstwin == lastwin)
339 MSG(_(m_onlyone));
340 else
342 tabpage_T *oldtab = curtab;
343 tabpage_T *newtab;
345 /* First create a new tab with the window, then go back to
346 * the old tab and close the window there. */
347 wp = curwin;
348 if (win_new_tabpage((int)Prenum) == OK
349 && valid_tabpage(oldtab))
351 newtab = curtab;
352 goto_tabpage_tp(oldtab);
353 if (curwin == wp)
354 win_close(curwin, FALSE);
355 if (valid_tabpage(newtab))
356 goto_tabpage_tp(newtab);
359 break;
361 /* cursor to top-left window */
362 case 't':
363 case Ctrl_T:
364 win_goto(firstwin);
365 break;
367 /* cursor to bottom-right window */
368 case 'b':
369 case Ctrl_B:
370 win_goto(lastwin);
371 break;
373 /* cursor to last accessed (previous) window */
374 case 'p':
375 case Ctrl_P:
376 if (prevwin == NULL)
377 beep_flush();
378 else
379 win_goto(prevwin);
380 break;
382 /* exchange current and next window */
383 case 'x':
384 case Ctrl_X:
385 CHECK_CMDWIN
386 win_exchange(Prenum);
387 break;
389 /* rotate windows downwards */
390 case Ctrl_R:
391 case 'r':
392 CHECK_CMDWIN
393 #ifdef FEAT_VISUAL
394 reset_VIsual_and_resel(); /* stop Visual mode */
395 #endif
396 win_rotate(FALSE, (int)Prenum1); /* downwards */
397 break;
399 /* rotate windows upwards */
400 case 'R':
401 CHECK_CMDWIN
402 #ifdef FEAT_VISUAL
403 reset_VIsual_and_resel(); /* stop Visual mode */
404 #endif
405 win_rotate(TRUE, (int)Prenum1); /* upwards */
406 break;
408 /* move window to the very top/bottom/left/right */
409 case 'K':
410 case 'J':
411 #ifdef FEAT_VERTSPLIT
412 case 'H':
413 case 'L':
414 #endif
415 CHECK_CMDWIN
416 win_totop((int)Prenum,
417 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
418 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
419 break;
421 /* make all windows the same height */
422 case '=':
423 #ifdef FEAT_GUI
424 need_mouse_correct = TRUE;
425 #endif
426 win_equal(NULL, FALSE, 'b');
427 break;
429 /* increase current window height */
430 case '+':
431 #ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433 #endif
434 win_setheight(curwin->w_height + (int)Prenum1);
435 break;
437 /* decrease 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 /* set current window height */
446 case Ctrl__:
447 case '_':
448 #ifdef FEAT_GUI
449 need_mouse_correct = TRUE;
450 #endif
451 win_setheight(Prenum ? (int)Prenum : 9999);
452 break;
454 #ifdef FEAT_VERTSPLIT
455 /* increase current window width */
456 case '>':
457 #ifdef FEAT_GUI
458 need_mouse_correct = TRUE;
459 #endif
460 win_setwidth(curwin->w_width + (int)Prenum1);
461 break;
463 /* decrease 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 /* set current window width */
472 case '|':
473 #ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475 #endif
476 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
477 break;
478 #endif
480 /* jump to tag and split window if tag exists (in preview window) */
481 #if defined(FEAT_QUICKFIX)
482 case '}':
483 CHECK_CMDWIN
484 if (Prenum)
485 g_do_tagpreview = Prenum;
486 else
487 g_do_tagpreview = p_pvh;
488 /*FALLTHROUGH*/
489 #endif
490 case ']':
491 case Ctrl_RSB:
492 CHECK_CMDWIN
493 #ifdef FEAT_VISUAL
494 reset_VIsual_and_resel(); /* stop Visual mode */
495 #endif
496 if (Prenum)
497 postponed_split = Prenum;
498 else
499 postponed_split = -1;
501 /* Execute the command right here, required when
502 * "wincmd ]" was used in a function. */
503 do_nv_ident(Ctrl_RSB, NUL);
504 break;
506 #ifdef FEAT_SEARCHPATH
507 /* edit file name under cursor in a new window */
508 case 'f':
509 case 'F':
510 case Ctrl_F:
511 wingotofile:
512 CHECK_CMDWIN
514 ptr = grab_file_name(Prenum1, &lnum);
515 if (ptr != NULL)
517 # ifdef FEAT_GUI
518 need_mouse_correct = TRUE;
519 # endif
520 setpcmark();
521 if (win_split(0, 0) == OK)
523 # ifdef FEAT_SCROLLBIND
524 curwin->w_p_scb = FALSE;
525 # endif
526 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
527 if (nchar == 'F' && lnum >= 0)
529 curwin->w_cursor.lnum = lnum;
530 check_cursor_lnum();
531 beginline(BL_SOL | BL_FIX);
534 vim_free(ptr);
536 break;
537 #endif
539 #ifdef FEAT_FIND_ID
540 /* Go to the first occurrence of the identifier under cursor along path in a
541 * new window -- webb
543 case 'i': /* Go to any match */
544 case Ctrl_I:
545 type = FIND_ANY;
546 /* FALLTHROUGH */
547 case 'd': /* Go to definition, using 'define' */
548 case Ctrl_D:
549 CHECK_CMDWIN
550 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
551 break;
552 find_pattern_in_path(ptr, 0, len, TRUE,
553 Prenum == 0 ? TRUE : FALSE, type,
554 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
555 curwin->w_set_curswant = TRUE;
556 break;
557 #endif
559 case K_KENTER:
560 case CAR:
561 #if defined(FEAT_QUICKFIX)
563 * In a quickfix window a <CR> jumps to the error under the
564 * cursor in a new window.
566 if (bt_quickfix(curbuf))
568 sprintf((char *)cbuf, "split +%ld%s",
569 (long)curwin->w_cursor.lnum,
570 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
571 do_cmdline_cmd(cbuf);
573 #endif
574 break;
577 /* CTRL-W g extended commands */
578 case 'g':
579 case Ctrl_G:
580 CHECK_CMDWIN
581 #ifdef USE_ON_FLY_SCROLL
582 dont_scroll = TRUE; /* disallow scrolling here */
583 #endif
584 ++no_mapping;
585 ++allow_keys; /* no mapping for xchar, but allow key codes */
586 if (xchar == NUL)
587 xchar = plain_vgetc();
588 #ifdef FEAT_LANGMAP
589 LANGMAP_ADJUST(xchar, TRUE);
590 #endif
591 --no_mapping;
592 --allow_keys;
593 #ifdef FEAT_CMDL_INFO
594 (void)add_to_showcmd(xchar);
595 #endif
596 switch (xchar)
598 #if defined(FEAT_QUICKFIX)
599 case '}':
600 xchar = Ctrl_RSB;
601 if (Prenum)
602 g_do_tagpreview = Prenum;
603 else
604 g_do_tagpreview = p_pvh;
605 /*FALLTHROUGH*/
606 #endif
607 case ']':
608 case Ctrl_RSB:
609 #ifdef FEAT_VISUAL
610 reset_VIsual_and_resel(); /* stop Visual mode */
611 #endif
612 if (Prenum)
613 postponed_split = Prenum;
614 else
615 postponed_split = -1;
617 /* Execute the command right here, required when
618 * "wincmd g}" was used in a function. */
619 do_nv_ident('g', xchar);
620 break;
622 #ifdef FEAT_SEARCHPATH
623 case 'f': /* CTRL-W gf: "gf" in a new tab page */
624 case 'F': /* CTRL-W gF: "gF" in a new tab page */
625 cmdmod.tab = TRUE;
626 nchar = xchar;
627 goto wingotofile;
628 #endif
629 default:
630 beep_flush();
631 break;
633 break;
635 default: beep_flush();
636 break;
641 * split the current window, implements CTRL-W s and :split
643 * "size" is the height or width for the new window, 0 to use half of current
644 * height or width.
646 * "flags":
647 * WSP_ROOM: require enough room for new window
648 * WSP_VERT: vertical split.
649 * WSP_TOP: open window at the top-left of the shell (help window).
650 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
651 * WSP_HELP: creating the help window, keep layout snapshot
653 * return FAIL for failure, OK otherwise
656 win_split(size, flags)
657 int size;
658 int flags;
660 /* When the ":tab" modifier was used open a new tab page instead. */
661 if (may_open_tabpage() == OK)
662 return OK;
664 /* Add flags from ":vertical", ":topleft" and ":botright". */
665 flags |= cmdmod.split;
666 if ((flags & WSP_TOP) && (flags & WSP_BOT))
668 EMSG(_("E442: Can't split topleft and botright at the same time"));
669 return FAIL;
672 /* When creating the help window make a snapshot of the window layout.
673 * Otherwise clear the snapshot, it's now invalid. */
674 if (flags & WSP_HELP)
675 make_snapshot();
676 else
677 clear_snapshot(curtab);
679 return win_split_ins(size, flags, NULL, 0);
683 * When "newwin" is NULL: split the current window in two.
684 * When "newwin" is not NULL: insert this window at the far
685 * top/left/right/bottom.
686 * return FAIL for failure, OK otherwise
688 static int
689 win_split_ins(size, flags, newwin, dir)
690 int size;
691 int flags;
692 win_T *newwin;
693 int dir;
695 win_T *wp = newwin;
696 win_T *oldwin;
697 int new_size = size;
698 int i;
699 int need_status = 0;
700 int do_equal = FALSE;
701 int needed;
702 int available;
703 int oldwin_height = 0;
704 int layout;
705 frame_T *frp, *curfrp;
706 int before;
708 if (flags & WSP_TOP)
709 oldwin = firstwin;
710 else if (flags & WSP_BOT)
711 oldwin = lastwin;
712 else
713 oldwin = curwin;
715 /* add a status line when p_ls == 1 and splitting the first window */
716 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
718 if (oldwin->w_height <= p_wmh && newwin == NULL)
720 EMSG(_(e_noroom));
721 return FAIL;
723 need_status = STATUS_HEIGHT;
726 #ifdef FEAT_GUI
727 /* May be needed for the scrollbars that are going to change. */
728 if (gui.in_use)
729 out_flush();
730 #endif
732 #ifdef FEAT_VERTSPLIT
733 if (flags & WSP_VERT)
735 layout = FR_ROW;
738 * Check if we are able to split the current window and compute its
739 * width.
741 needed = p_wmw + 1;
742 if (flags & WSP_ROOM)
743 needed += p_wiw - p_wmw;
744 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
746 available = topframe->fr_width;
747 needed += frame_minwidth(topframe, NULL);
749 else
750 available = oldwin->w_width;
751 if (available < needed && newwin == NULL)
753 EMSG(_(e_noroom));
754 return FAIL;
756 if (new_size == 0)
757 new_size = oldwin->w_width / 2;
758 if (new_size > oldwin->w_width - p_wmw - 1)
759 new_size = oldwin->w_width - p_wmw - 1;
760 if (new_size < p_wmw)
761 new_size = p_wmw;
763 /* if it doesn't fit in the current window, need win_equal() */
764 if (oldwin->w_width - new_size - 1 < p_wmw)
765 do_equal = TRUE;
767 /* We don't like to take lines for the new window from a
768 * 'winfixwidth' window. Take them from a window to the left or right
769 * instead, if possible. */
770 if (oldwin->w_p_wfw)
771 win_setwidth_win(oldwin->w_width + new_size, oldwin);
773 /* Only make all windows the same width if one of them (except oldwin)
774 * is wider than one of the split windows. */
775 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
776 && oldwin->w_frame->fr_parent != NULL)
778 frp = oldwin->w_frame->fr_parent->fr_child;
779 while (frp != NULL)
781 if (frp->fr_win != oldwin && frp->fr_win != NULL
782 && (frp->fr_win->w_width > new_size
783 || frp->fr_win->w_width > oldwin->w_width
784 - new_size - STATUS_HEIGHT))
786 do_equal = TRUE;
787 break;
789 frp = frp->fr_next;
793 else
794 #endif
796 layout = FR_COL;
799 * Check if we are able to split the current window and compute its
800 * height.
802 needed = p_wmh + STATUS_HEIGHT + need_status;
803 if (flags & WSP_ROOM)
804 needed += p_wh - p_wmh;
805 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
807 available = topframe->fr_height;
808 needed += frame_minheight(topframe, NULL);
810 else
812 available = oldwin->w_height;
813 needed += p_wmh;
815 if (available < needed && newwin == NULL)
817 EMSG(_(e_noroom));
818 return FAIL;
820 oldwin_height = oldwin->w_height;
821 if (need_status)
823 oldwin->w_status_height = STATUS_HEIGHT;
824 oldwin_height -= STATUS_HEIGHT;
826 if (new_size == 0)
827 new_size = oldwin_height / 2;
829 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
830 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
831 if (new_size < p_wmh)
832 new_size = p_wmh;
834 /* if it doesn't fit in the current window, need win_equal() */
835 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
836 do_equal = TRUE;
838 /* We don't like to take lines for the new window from a
839 * 'winfixheight' window. Take them from a window above or below
840 * instead, if possible. */
841 if (oldwin->w_p_wfh)
843 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
844 oldwin);
845 oldwin_height = oldwin->w_height;
846 if (need_status)
847 oldwin_height -= STATUS_HEIGHT;
850 /* Only make all windows the same height if one of them (except oldwin)
851 * is higher than one of the split windows. */
852 if (!do_equal && p_ea && size == 0
853 #ifdef FEAT_VERTSPLIT
854 && *p_ead != 'h'
855 #endif
856 && oldwin->w_frame->fr_parent != NULL)
858 frp = oldwin->w_frame->fr_parent->fr_child;
859 while (frp != NULL)
861 if (frp->fr_win != oldwin && frp->fr_win != NULL
862 && (frp->fr_win->w_height > new_size
863 || frp->fr_win->w_height > oldwin_height - new_size
864 - STATUS_HEIGHT))
866 do_equal = TRUE;
867 break;
869 frp = frp->fr_next;
875 * allocate new window structure and link it in the window list
877 if ((flags & WSP_TOP) == 0
878 && ((flags & WSP_BOT)
879 || (flags & WSP_BELOW)
880 || (!(flags & WSP_ABOVE)
881 && (
882 #ifdef FEAT_VERTSPLIT
883 (flags & WSP_VERT) ? p_spr :
884 #endif
885 p_sb))))
887 /* new window below/right of current one */
888 if (newwin == NULL)
889 wp = win_alloc(oldwin);
890 else
891 win_append(oldwin, wp);
893 else
895 if (newwin == NULL)
896 wp = win_alloc(oldwin->w_prev);
897 else
898 win_append(oldwin->w_prev, wp);
901 if (newwin == NULL)
903 if (wp == NULL)
904 return FAIL;
906 /* make the contents of the new window the same as the current one */
907 win_init(wp, curwin);
911 * Reorganise the tree of frames to insert the new window.
913 if (flags & (WSP_TOP | WSP_BOT))
915 #ifdef FEAT_VERTSPLIT
916 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
917 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
918 #else
919 if (topframe->fr_layout == FR_COL)
920 #endif
922 curfrp = topframe->fr_child;
923 if (flags & WSP_BOT)
924 while (curfrp->fr_next != NULL)
925 curfrp = curfrp->fr_next;
927 else
928 curfrp = topframe;
929 before = (flags & WSP_TOP);
931 else
933 curfrp = oldwin->w_frame;
934 if (flags & WSP_BELOW)
935 before = FALSE;
936 else if (flags & WSP_ABOVE)
937 before = TRUE;
938 else
939 #ifdef FEAT_VERTSPLIT
940 if (flags & WSP_VERT)
941 before = !p_spr;
942 else
943 #endif
944 before = !p_sb;
946 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
948 /* Need to create a new frame in the tree to make a branch. */
949 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
950 *frp = *curfrp;
951 curfrp->fr_layout = layout;
952 frp->fr_parent = curfrp;
953 frp->fr_next = NULL;
954 frp->fr_prev = NULL;
955 curfrp->fr_child = frp;
956 curfrp->fr_win = NULL;
957 curfrp = frp;
958 if (frp->fr_win != NULL)
959 oldwin->w_frame = frp;
960 else
961 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
962 frp->fr_parent = curfrp;
965 if (newwin == NULL)
967 /* Create a frame for the new window. */
968 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
969 frp->fr_layout = FR_LEAF;
970 frp->fr_win = wp;
971 wp->w_frame = frp;
973 else
974 frp = newwin->w_frame;
975 frp->fr_parent = curfrp->fr_parent;
977 /* Insert the new frame at the right place in the frame list. */
978 if (before)
979 frame_insert(curfrp, frp);
980 else
981 frame_append(curfrp, frp);
983 #ifdef FEAT_VERTSPLIT
984 if (flags & WSP_VERT)
986 wp->w_p_scr = curwin->w_p_scr;
987 if (need_status)
989 --oldwin->w_height;
990 oldwin->w_status_height = need_status;
992 if (flags & (WSP_TOP | WSP_BOT))
994 /* set height and row of new window to full height */
995 wp->w_winrow = tabline_height();
996 wp->w_height = curfrp->fr_height - (p_ls > 0);
997 wp->w_status_height = (p_ls > 0);
999 else
1001 /* height and row of new window is same as current window */
1002 wp->w_winrow = oldwin->w_winrow;
1003 wp->w_height = oldwin->w_height;
1004 wp->w_status_height = oldwin->w_status_height;
1006 frp->fr_height = curfrp->fr_height;
1008 /* "new_size" of the current window goes to the new window, use
1009 * one column for the vertical separator */
1010 wp->w_width = new_size;
1011 if (before)
1012 wp->w_vsep_width = 1;
1013 else
1015 wp->w_vsep_width = oldwin->w_vsep_width;
1016 oldwin->w_vsep_width = 1;
1018 if (flags & (WSP_TOP | WSP_BOT))
1020 if (flags & WSP_BOT)
1021 frame_add_vsep(curfrp);
1022 /* Set width of neighbor frame */
1023 frame_new_width(curfrp, curfrp->fr_width
1024 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1025 FALSE);
1027 else
1028 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1029 if (before) /* new window left of current one */
1031 wp->w_wincol = oldwin->w_wincol;
1032 oldwin->w_wincol += new_size + 1;
1034 else /* new window right of current one */
1035 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1036 frame_fix_width(oldwin);
1037 frame_fix_width(wp);
1039 else
1040 #endif
1042 /* width and column of new window is same as current window */
1043 #ifdef FEAT_VERTSPLIT
1044 if (flags & (WSP_TOP | WSP_BOT))
1046 wp->w_wincol = 0;
1047 wp->w_width = Columns;
1048 wp->w_vsep_width = 0;
1050 else
1052 wp->w_wincol = oldwin->w_wincol;
1053 wp->w_width = oldwin->w_width;
1054 wp->w_vsep_width = oldwin->w_vsep_width;
1056 frp->fr_width = curfrp->fr_width;
1057 #endif
1059 /* "new_size" of the current window goes to the new window, use
1060 * one row for the status line */
1061 win_new_height(wp, new_size);
1062 if (flags & (WSP_TOP | WSP_BOT))
1063 frame_new_height(curfrp, curfrp->fr_height
1064 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1065 else
1066 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1067 if (before) /* new window above current one */
1069 wp->w_winrow = oldwin->w_winrow;
1070 wp->w_status_height = STATUS_HEIGHT;
1071 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1073 else /* new window below current one */
1075 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1076 wp->w_status_height = oldwin->w_status_height;
1077 oldwin->w_status_height = STATUS_HEIGHT;
1079 #ifdef FEAT_VERTSPLIT
1080 if (flags & WSP_BOT)
1081 frame_add_statusline(curfrp);
1082 #endif
1083 frame_fix_height(wp);
1084 frame_fix_height(oldwin);
1087 if (flags & (WSP_TOP | WSP_BOT))
1088 (void)win_comp_pos();
1091 * Both windows need redrawing
1093 redraw_win_later(wp, NOT_VALID);
1094 wp->w_redr_status = TRUE;
1095 redraw_win_later(oldwin, NOT_VALID);
1096 oldwin->w_redr_status = TRUE;
1098 if (need_status)
1100 msg_row = Rows - 1;
1101 msg_col = sc_col;
1102 msg_clr_eos_force(); /* Old command/ruler may still be there */
1103 comp_col();
1104 msg_row = Rows - 1;
1105 msg_col = 0; /* put position back at start of line */
1109 * make the new window the current window and redraw
1111 if (do_equal || dir != 0)
1112 win_equal(wp, TRUE,
1113 #ifdef FEAT_VERTSPLIT
1114 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1115 : dir == 'h' ? 'b' :
1116 #endif
1117 'v');
1119 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1120 * size was given. */
1121 #ifdef FEAT_VERTSPLIT
1122 if (flags & WSP_VERT)
1124 i = p_wiw;
1125 if (size != 0)
1126 p_wiw = size;
1128 # ifdef FEAT_GUI
1129 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1130 if (gui.in_use)
1131 gui_init_which_components(NULL);
1132 # endif
1134 else
1135 #endif
1137 i = p_wh;
1138 if (size != 0)
1139 p_wh = size;
1141 win_enter(wp, FALSE);
1142 #ifdef FEAT_VERTSPLIT
1143 if (flags & WSP_VERT)
1144 p_wiw = i;
1145 else
1146 #endif
1147 p_wh = i;
1149 return OK;
1153 * Initialize window "newp" from window "oldp".
1154 * Used when splitting a window and when creating a new tab page.
1155 * The windows will both edit the same buffer.
1157 static void
1158 win_init(newp, oldp)
1159 win_T *newp;
1160 win_T *oldp;
1162 int i;
1164 newp->w_buffer = oldp->w_buffer;
1165 oldp->w_buffer->b_nwindows++;
1166 newp->w_cursor = oldp->w_cursor;
1167 newp->w_valid = 0;
1168 newp->w_curswant = oldp->w_curswant;
1169 newp->w_set_curswant = oldp->w_set_curswant;
1170 newp->w_topline = oldp->w_topline;
1171 #ifdef FEAT_DIFF
1172 newp->w_topfill = oldp->w_topfill;
1173 #endif
1174 newp->w_leftcol = oldp->w_leftcol;
1175 newp->w_pcmark = oldp->w_pcmark;
1176 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1177 newp->w_alt_fnum = oldp->w_alt_fnum;
1178 newp->w_wrow = oldp->w_wrow;
1179 newp->w_fraction = oldp->w_fraction;
1180 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1181 #ifdef FEAT_JUMPLIST
1182 copy_jumplist(oldp, newp);
1183 #endif
1184 #ifdef FEAT_QUICKFIX
1185 copy_loclist(oldp, newp);
1186 #endif
1187 if (oldp->w_localdir != NULL)
1188 newp->w_localdir = vim_strsave(oldp->w_localdir);
1190 /* Use the same argument list. */
1191 newp->w_alist = oldp->w_alist;
1192 ++newp->w_alist->al_refcount;
1193 newp->w_arg_idx = oldp->w_arg_idx;
1196 * copy tagstack and options from existing window
1198 for (i = 0; i < oldp->w_tagstacklen; i++)
1200 newp->w_tagstack[i] = oldp->w_tagstack[i];
1201 if (newp->w_tagstack[i].tagname != NULL)
1202 newp->w_tagstack[i].tagname =
1203 vim_strsave(newp->w_tagstack[i].tagname);
1205 newp->w_tagstackidx = oldp->w_tagstackidx;
1206 newp->w_tagstacklen = oldp->w_tagstacklen;
1207 win_copy_options(oldp, newp);
1208 # ifdef FEAT_FOLDING
1209 copyFoldingState(oldp, newp);
1210 # endif
1213 #endif /* FEAT_WINDOWS */
1215 #if defined(FEAT_WINDOWS) || defined(PROTO)
1217 * Check if "win" is a pointer to an existing window.
1220 win_valid(win)
1221 win_T *win;
1223 win_T *wp;
1225 if (win == NULL)
1226 return FALSE;
1227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1228 if (wp == win)
1229 return TRUE;
1230 return FALSE;
1234 * Return the number of windows.
1237 win_count()
1239 win_T *wp;
1240 int count = 0;
1242 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1243 ++count;
1244 return count;
1248 * Make "count" windows on the screen.
1249 * Return actual number of windows on the screen.
1250 * Must be called when there is just one window, filling the whole screen
1251 * (excluding the command line).
1253 /*ARGSUSED*/
1255 make_windows(count, vertical)
1256 int count;
1257 int vertical; /* split windows vertically if TRUE */
1259 int maxcount;
1260 int todo;
1262 #ifdef FEAT_VERTSPLIT
1263 if (vertical)
1265 /* Each windows needs at least 'winminwidth' lines and a separator
1266 * column. */
1267 maxcount = (curwin->w_width + curwin->w_vsep_width
1268 - (p_wiw - p_wmw)) / (p_wmw + 1);
1270 else
1271 #endif
1273 /* Each window needs at least 'winminheight' lines and a status line. */
1274 maxcount = (curwin->w_height + curwin->w_status_height
1275 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1278 if (maxcount < 2)
1279 maxcount = 2;
1280 if (count > maxcount)
1281 count = maxcount;
1284 * add status line now, otherwise first window will be too big
1286 if (count > 1)
1287 last_status(TRUE);
1289 #ifdef FEAT_AUTOCMD
1291 * Don't execute autocommands while creating the windows. Must do that
1292 * when putting the buffers in the windows.
1294 block_autocmds();
1295 #endif
1297 /* todo is number of windows left to create */
1298 for (todo = count - 1; todo > 0; --todo)
1299 #ifdef FEAT_VERTSPLIT
1300 if (vertical)
1302 if (win_split(curwin->w_width - (curwin->w_width - todo)
1303 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1304 break;
1306 else
1307 #endif
1309 if (win_split(curwin->w_height - (curwin->w_height - todo
1310 * STATUS_HEIGHT) / (todo + 1)
1311 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1312 break;
1315 #ifdef FEAT_AUTOCMD
1316 unblock_autocmds();
1317 #endif
1319 /* return actual number of windows */
1320 return (count - todo);
1324 * Exchange current and next window
1326 static void
1327 win_exchange(Prenum)
1328 long Prenum;
1330 frame_T *frp;
1331 frame_T *frp2;
1332 win_T *wp;
1333 win_T *wp2;
1334 int temp;
1336 if (lastwin == firstwin) /* just one window */
1338 beep_flush();
1339 return;
1342 #ifdef FEAT_GUI
1343 need_mouse_correct = TRUE;
1344 #endif
1347 * find window to exchange with
1349 if (Prenum)
1351 frp = curwin->w_frame->fr_parent->fr_child;
1352 while (frp != NULL && --Prenum > 0)
1353 frp = frp->fr_next;
1355 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1356 frp = curwin->w_frame->fr_next;
1357 else /* Swap last window in row/col with previous */
1358 frp = curwin->w_frame->fr_prev;
1360 /* We can only exchange a window with another window, not with a frame
1361 * containing windows. */
1362 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1363 return;
1364 wp = frp->fr_win;
1367 * 1. remove curwin from the list. Remember after which window it was in wp2
1368 * 2. insert curwin before wp in the list
1369 * if wp != wp2
1370 * 3. remove wp from the list
1371 * 4. insert wp after wp2
1372 * 5. exchange the status line height and vsep width.
1374 wp2 = curwin->w_prev;
1375 frp2 = curwin->w_frame->fr_prev;
1376 if (wp->w_prev != curwin)
1378 win_remove(curwin, NULL);
1379 frame_remove(curwin->w_frame);
1380 win_append(wp->w_prev, curwin);
1381 frame_insert(frp, curwin->w_frame);
1383 if (wp != wp2)
1385 win_remove(wp, NULL);
1386 frame_remove(wp->w_frame);
1387 win_append(wp2, wp);
1388 if (frp2 == NULL)
1389 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1390 else
1391 frame_append(frp2, wp->w_frame);
1393 temp = curwin->w_status_height;
1394 curwin->w_status_height = wp->w_status_height;
1395 wp->w_status_height = temp;
1396 #ifdef FEAT_VERTSPLIT
1397 temp = curwin->w_vsep_width;
1398 curwin->w_vsep_width = wp->w_vsep_width;
1399 wp->w_vsep_width = temp;
1401 /* If the windows are not in the same frame, exchange the sizes to avoid
1402 * messing up the window layout. Otherwise fix the frame sizes. */
1403 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1405 temp = curwin->w_height;
1406 curwin->w_height = wp->w_height;
1407 wp->w_height = temp;
1408 temp = curwin->w_width;
1409 curwin->w_width = wp->w_width;
1410 wp->w_width = temp;
1412 else
1414 frame_fix_height(curwin);
1415 frame_fix_height(wp);
1416 frame_fix_width(curwin);
1417 frame_fix_width(wp);
1419 #endif
1421 (void)win_comp_pos(); /* recompute window positions */
1423 win_enter(wp, TRUE);
1424 redraw_later(CLEAR);
1428 * rotate windows: if upwards TRUE the second window becomes the first one
1429 * if upwards FALSE the first window becomes the second one
1431 static void
1432 win_rotate(upwards, count)
1433 int upwards;
1434 int count;
1436 win_T *wp1;
1437 win_T *wp2;
1438 frame_T *frp;
1439 int n;
1441 if (firstwin == lastwin) /* nothing to do */
1443 beep_flush();
1444 return;
1447 #ifdef FEAT_GUI
1448 need_mouse_correct = TRUE;
1449 #endif
1451 #ifdef FEAT_VERTSPLIT
1452 /* Check if all frames in this row/col have one window. */
1453 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1454 frp = frp->fr_next)
1455 if (frp->fr_win == NULL)
1457 EMSG(_("E443: Cannot rotate when another window is split"));
1458 return;
1460 #endif
1462 while (count--)
1464 if (upwards) /* first window becomes last window */
1466 /* remove first window/frame from the list */
1467 frp = curwin->w_frame->fr_parent->fr_child;
1468 wp1 = frp->fr_win;
1469 win_remove(wp1, NULL);
1470 frame_remove(frp);
1472 /* find last frame and append removed window/frame after it */
1473 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1475 win_append(frp->fr_win, wp1);
1476 frame_append(frp, wp1->w_frame);
1478 wp2 = frp->fr_win; /* previously last window */
1480 else /* last window becomes first window */
1482 /* find last window/frame in the list and remove it */
1483 for (frp = curwin->w_frame; frp->fr_next != NULL;
1484 frp = frp->fr_next)
1486 wp1 = frp->fr_win;
1487 wp2 = wp1->w_prev; /* will become last window */
1488 win_remove(wp1, NULL);
1489 frame_remove(frp);
1491 /* append the removed window/frame before the first in the list */
1492 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1493 frame_insert(frp->fr_parent->fr_child, frp);
1496 /* exchange status height and vsep width of old and new last window */
1497 n = wp2->w_status_height;
1498 wp2->w_status_height = wp1->w_status_height;
1499 wp1->w_status_height = n;
1500 frame_fix_height(wp1);
1501 frame_fix_height(wp2);
1502 #ifdef FEAT_VERTSPLIT
1503 n = wp2->w_vsep_width;
1504 wp2->w_vsep_width = wp1->w_vsep_width;
1505 wp1->w_vsep_width = n;
1506 frame_fix_width(wp1);
1507 frame_fix_width(wp2);
1508 #endif
1510 /* recompute w_winrow and w_wincol for all windows */
1511 (void)win_comp_pos();
1514 redraw_later(CLEAR);
1518 * Move the current window to the very top/bottom/left/right of the screen.
1520 static void
1521 win_totop(size, flags)
1522 int size;
1523 int flags;
1525 int dir;
1526 int height = curwin->w_height;
1528 if (lastwin == firstwin)
1530 beep_flush();
1531 return;
1534 /* Remove the window and frame from the tree of frames. */
1535 (void)winframe_remove(curwin, &dir, NULL);
1536 win_remove(curwin, NULL);
1537 last_status(FALSE); /* may need to remove last status line */
1538 (void)win_comp_pos(); /* recompute window positions */
1540 /* Split a window on the desired side and put the window there. */
1541 (void)win_split_ins(size, flags, curwin, dir);
1542 if (!(flags & WSP_VERT))
1544 win_setheight(height);
1545 if (p_ea)
1546 win_equal(curwin, TRUE, 'v');
1549 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1550 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1551 * scrollbars. Have to update them anyway. */
1552 if (gui.in_use)
1554 out_flush();
1555 gui_init_which_components(NULL);
1556 gui_update_scrollbars(TRUE);
1558 need_mouse_correct = TRUE;
1559 #endif
1564 * Move window "win1" to below/right of "win2" and make "win1" the current
1565 * window. Only works within the same frame!
1567 void
1568 win_move_after(win1, win2)
1569 win_T *win1, *win2;
1571 int height;
1573 /* check if the arguments are reasonable */
1574 if (win1 == win2)
1575 return;
1577 /* check if there is something to do */
1578 if (win2->w_next != win1)
1580 /* may need move the status line/vertical separator of the last window
1581 * */
1582 if (win1 == lastwin)
1584 height = win1->w_prev->w_status_height;
1585 win1->w_prev->w_status_height = win1->w_status_height;
1586 win1->w_status_height = height;
1587 #ifdef FEAT_VERTSPLIT
1588 if (win1->w_prev->w_vsep_width == 1)
1590 /* Remove the vertical separator from the last-but-one window,
1591 * add it to the last window. Adjust the frame widths. */
1592 win1->w_prev->w_vsep_width = 0;
1593 win1->w_prev->w_frame->fr_width -= 1;
1594 win1->w_vsep_width = 1;
1595 win1->w_frame->fr_width += 1;
1597 #endif
1599 else if (win2 == lastwin)
1601 height = win1->w_status_height;
1602 win1->w_status_height = win2->w_status_height;
1603 win2->w_status_height = height;
1604 #ifdef FEAT_VERTSPLIT
1605 if (win1->w_vsep_width == 1)
1607 /* Remove the vertical separator from win1, add it to the last
1608 * window, win2. Adjust the frame widths. */
1609 win2->w_vsep_width = 1;
1610 win2->w_frame->fr_width += 1;
1611 win1->w_vsep_width = 0;
1612 win1->w_frame->fr_width -= 1;
1614 #endif
1616 win_remove(win1, NULL);
1617 frame_remove(win1->w_frame);
1618 win_append(win2, win1);
1619 frame_append(win2->w_frame, win1->w_frame);
1621 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1622 redraw_later(NOT_VALID);
1624 win_enter(win1, FALSE);
1628 * Make all windows the same height.
1629 * 'next_curwin' will soon be the current window, make sure it has enough
1630 * rows.
1632 void
1633 win_equal(next_curwin, current, dir)
1634 win_T *next_curwin; /* pointer to current window to be or NULL */
1635 int current; /* do only frame with current window */
1636 int dir; /* 'v' for vertically, 'h' for horizontally,
1637 'b' for both, 0 for using p_ead */
1639 if (dir == 0)
1640 #ifdef FEAT_VERTSPLIT
1641 dir = *p_ead;
1642 #else
1643 dir = 'b';
1644 #endif
1645 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1646 topframe, dir, 0, tabline_height(),
1647 (int)Columns, topframe->fr_height);
1651 * Set a frame to a new position and height, spreading the available room
1652 * equally over contained frames.
1653 * The window "next_curwin" (if not NULL) should at least get the size from
1654 * 'winheight' and 'winwidth' if possible.
1656 static void
1657 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1658 win_T *next_curwin; /* pointer to current window to be or NULL */
1659 int current; /* do only frame with current window */
1660 frame_T *topfr; /* frame to set size off */
1661 int dir; /* 'v', 'h' or 'b', see win_equal() */
1662 int col; /* horizontal position for frame */
1663 int row; /* vertical position for frame */
1664 int width; /* new width of frame */
1665 int height; /* new height of frame */
1667 int n, m;
1668 int extra_sep = 0;
1669 int wincount, totwincount = 0;
1670 frame_T *fr;
1671 int next_curwin_size = 0;
1672 int room = 0;
1673 int new_size;
1674 int has_next_curwin = 0;
1675 int hnc;
1677 if (topfr->fr_layout == FR_LEAF)
1679 /* Set the width/height of this frame.
1680 * Redraw when size or position changes */
1681 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1682 #ifdef FEAT_VERTSPLIT
1683 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1684 #endif
1687 topfr->fr_win->w_winrow = row;
1688 frame_new_height(topfr, height, FALSE, FALSE);
1689 #ifdef FEAT_VERTSPLIT
1690 topfr->fr_win->w_wincol = col;
1691 frame_new_width(topfr, width, FALSE, FALSE);
1692 #endif
1693 redraw_all_later(CLEAR);
1696 #ifdef FEAT_VERTSPLIT
1697 else if (topfr->fr_layout == FR_ROW)
1699 topfr->fr_width = width;
1700 topfr->fr_height = height;
1702 if (dir != 'v') /* equalize frame widths */
1704 /* Compute the maximum number of windows horizontally in this
1705 * frame. */
1706 n = frame_minwidth(topfr, NOWIN);
1707 /* add one for the rightmost window, it doesn't have a separator */
1708 if (col + width == Columns)
1709 extra_sep = 1;
1710 else
1711 extra_sep = 0;
1712 totwincount = (n + extra_sep) / (p_wmw + 1);
1713 has_next_curwin = frame_has_win(topfr, next_curwin);
1716 * Compute width for "next_curwin" window and room available for
1717 * other windows.
1718 * "m" is the minimal width when counting p_wiw for "next_curwin".
1720 m = frame_minwidth(topfr, next_curwin);
1721 room = width - m;
1722 if (room < 0)
1724 next_curwin_size = p_wiw + room;
1725 room = 0;
1727 else
1729 next_curwin_size = -1;
1730 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1732 /* If 'winfixwidth' set keep the window width if
1733 * possible.
1734 * Watch out for this window being the next_curwin. */
1735 if (frame_fixed_width(fr))
1737 n = frame_minwidth(fr, NOWIN);
1738 new_size = fr->fr_width;
1739 if (frame_has_win(fr, next_curwin))
1741 room += p_wiw - p_wmw;
1742 next_curwin_size = 0;
1743 if (new_size < p_wiw)
1744 new_size = p_wiw;
1746 else
1747 /* These windows don't use up room. */
1748 totwincount -= (n + (fr->fr_next == NULL
1749 ? extra_sep : 0)) / (p_wmw + 1);
1750 room -= new_size - n;
1751 if (room < 0)
1753 new_size += room;
1754 room = 0;
1756 fr->fr_newwidth = new_size;
1759 if (next_curwin_size == -1)
1761 if (!has_next_curwin)
1762 next_curwin_size = 0;
1763 else if (totwincount > 1
1764 && (room + (totwincount - 2))
1765 / (totwincount - 1) > p_wiw)
1767 /* Can make all windows wider than 'winwidth', spread
1768 * the room equally. */
1769 next_curwin_size = (room + p_wiw
1770 + (totwincount - 1) * p_wmw
1771 + (totwincount - 1)) / totwincount;
1772 room -= next_curwin_size - p_wiw;
1774 else
1775 next_curwin_size = p_wiw;
1779 if (has_next_curwin)
1780 --totwincount; /* don't count curwin */
1783 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1785 n = m = 0;
1786 wincount = 1;
1787 if (fr->fr_next == NULL)
1788 /* last frame gets all that remains (avoid roundoff error) */
1789 new_size = width;
1790 else if (dir == 'v')
1791 new_size = fr->fr_width;
1792 else if (frame_fixed_width(fr))
1794 new_size = fr->fr_newwidth;
1795 wincount = 0; /* doesn't count as a sizeable window */
1797 else
1799 /* Compute the maximum number of windows horiz. in "fr". */
1800 n = frame_minwidth(fr, NOWIN);
1801 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1802 / (p_wmw + 1);
1803 m = frame_minwidth(fr, next_curwin);
1804 if (has_next_curwin)
1805 hnc = frame_has_win(fr, next_curwin);
1806 else
1807 hnc = FALSE;
1808 if (hnc) /* don't count next_curwin */
1809 --wincount;
1810 if (totwincount == 0)
1811 new_size = room;
1812 else
1813 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1814 / totwincount;
1815 if (hnc) /* add next_curwin size */
1817 next_curwin_size -= p_wiw - (m - n);
1818 new_size += next_curwin_size;
1819 room -= new_size - next_curwin_size;
1821 else
1822 room -= new_size;
1823 new_size += n;
1826 /* Skip frame that is full width when splitting or closing a
1827 * window, unless equalizing all frames. */
1828 if (!current || dir != 'v' || topfr->fr_parent != NULL
1829 || (new_size != fr->fr_width)
1830 || frame_has_win(fr, next_curwin))
1831 win_equal_rec(next_curwin, current, fr, dir, col, row,
1832 new_size, height);
1833 col += new_size;
1834 width -= new_size;
1835 totwincount -= wincount;
1838 #endif
1839 else /* topfr->fr_layout == FR_COL */
1841 #ifdef FEAT_VERTSPLIT
1842 topfr->fr_width = width;
1843 #endif
1844 topfr->fr_height = height;
1846 if (dir != 'h') /* equalize frame heights */
1848 /* Compute maximum number of windows vertically in this frame. */
1849 n = frame_minheight(topfr, NOWIN);
1850 /* add one for the bottom window if it doesn't have a statusline */
1851 if (row + height == cmdline_row && p_ls == 0)
1852 extra_sep = 1;
1853 else
1854 extra_sep = 0;
1855 totwincount = (n + extra_sep) / (p_wmh + 1);
1856 has_next_curwin = frame_has_win(topfr, next_curwin);
1859 * Compute height for "next_curwin" window and room available for
1860 * other windows.
1861 * "m" is the minimal height when counting p_wh for "next_curwin".
1863 m = frame_minheight(topfr, next_curwin);
1864 room = height - m;
1865 if (room < 0)
1867 /* The room is less then 'winheight', use all space for the
1868 * current window. */
1869 next_curwin_size = p_wh + room;
1870 room = 0;
1872 else
1874 next_curwin_size = -1;
1875 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1877 /* If 'winfixheight' set keep the window height if
1878 * possible.
1879 * Watch out for this window being the next_curwin. */
1880 if (frame_fixed_height(fr))
1882 n = frame_minheight(fr, NOWIN);
1883 new_size = fr->fr_height;
1884 if (frame_has_win(fr, next_curwin))
1886 room += p_wh - p_wmh;
1887 next_curwin_size = 0;
1888 if (new_size < p_wh)
1889 new_size = p_wh;
1891 else
1892 /* These windows don't use up room. */
1893 totwincount -= (n + (fr->fr_next == NULL
1894 ? extra_sep : 0)) / (p_wmh + 1);
1895 room -= new_size - n;
1896 if (room < 0)
1898 new_size += room;
1899 room = 0;
1901 fr->fr_newheight = new_size;
1904 if (next_curwin_size == -1)
1906 if (!has_next_curwin)
1907 next_curwin_size = 0;
1908 else if (totwincount > 1
1909 && (room + (totwincount - 2))
1910 / (totwincount - 1) > p_wh)
1912 /* can make all windows higher than 'winheight',
1913 * spread the room equally. */
1914 next_curwin_size = (room + p_wh
1915 + (totwincount - 1) * p_wmh
1916 + (totwincount - 1)) / totwincount;
1917 room -= next_curwin_size - p_wh;
1919 else
1920 next_curwin_size = p_wh;
1924 if (has_next_curwin)
1925 --totwincount; /* don't count curwin */
1928 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1930 n = m = 0;
1931 wincount = 1;
1932 if (fr->fr_next == NULL)
1933 /* last frame gets all that remains (avoid roundoff error) */
1934 new_size = height;
1935 else if (dir == 'h')
1936 new_size = fr->fr_height;
1937 else if (frame_fixed_height(fr))
1939 new_size = fr->fr_newheight;
1940 wincount = 0; /* doesn't count as a sizeable window */
1942 else
1944 /* Compute the maximum number of windows vert. in "fr". */
1945 n = frame_minheight(fr, NOWIN);
1946 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1947 / (p_wmh + 1);
1948 m = frame_minheight(fr, next_curwin);
1949 if (has_next_curwin)
1950 hnc = frame_has_win(fr, next_curwin);
1951 else
1952 hnc = FALSE;
1953 if (hnc) /* don't count next_curwin */
1954 --wincount;
1955 if (totwincount == 0)
1956 new_size = room;
1957 else
1958 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1959 / totwincount;
1960 if (hnc) /* add next_curwin size */
1962 next_curwin_size -= p_wh - (m - n);
1963 new_size += next_curwin_size;
1964 room -= new_size - next_curwin_size;
1966 else
1967 room -= new_size;
1968 new_size += n;
1970 /* Skip frame that is full width when splitting or closing a
1971 * window, unless equalizing all frames. */
1972 if (!current || dir != 'h' || topfr->fr_parent != NULL
1973 || (new_size != fr->fr_height)
1974 || frame_has_win(fr, next_curwin))
1975 win_equal_rec(next_curwin, current, fr, dir, col, row,
1976 width, new_size);
1977 row += new_size;
1978 height -= new_size;
1979 totwincount -= wincount;
1985 * close all windows for buffer 'buf'
1987 void
1988 close_windows(buf, keep_curwin)
1989 buf_T *buf;
1990 int keep_curwin; /* don't close "curwin" */
1992 win_T *wp;
1993 tabpage_T *tp, *nexttp;
1994 int h = tabline_height();
1996 ++RedrawingDisabled;
1998 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
2000 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
2002 win_close(wp, FALSE);
2004 /* Start all over, autocommands may change the window layout. */
2005 wp = firstwin;
2007 else
2008 wp = wp->w_next;
2011 /* Also check windows in other tab pages. */
2012 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2014 nexttp = tp->tp_next;
2015 if (tp != curtab)
2016 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2017 if (wp->w_buffer == buf)
2019 win_close_othertab(wp, FALSE, tp);
2021 /* Start all over, the tab page may be closed and
2022 * autocommands may change the window layout. */
2023 nexttp = first_tabpage;
2024 break;
2028 --RedrawingDisabled;
2030 if (h != tabline_height())
2031 shell_new_rows();
2035 * Return TRUE if the current window is the only window that exists.
2036 * Returns FALSE if there is a window, possibly in another tab page.
2038 static int
2039 last_window()
2041 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2045 * Close window "win". Only works for the current tab page.
2046 * If "free_buf" is TRUE related buffer may be unloaded.
2048 * called by :quit, :close, :xit, :wq and findtag()
2050 void
2051 win_close(win, free_buf)
2052 win_T *win;
2053 int free_buf;
2055 win_T *wp;
2056 #ifdef FEAT_AUTOCMD
2057 int other_buffer = FALSE;
2058 #endif
2059 int close_curwin = FALSE;
2060 int dir;
2061 int help_window = FALSE;
2062 tabpage_T *prev_curtab = curtab;
2064 if (last_window())
2066 EMSG(_("E444: Cannot close last window"));
2067 return;
2071 * When closing the last window in a tab page first go to another tab
2072 * page and then close the window and the tab page. This avoids that
2073 * curwin and curtab are not invalid while we are freeing memory, they may
2074 * be used in GUI events.
2076 if (firstwin == lastwin)
2078 goto_tabpage_tp(alt_tabpage());
2079 redraw_tabline = TRUE;
2081 /* Safety check: Autocommands may have closed the window when jumping
2082 * to the other tab page. */
2083 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2085 int h = tabline_height();
2087 win_close_othertab(win, free_buf, prev_curtab);
2088 if (h != tabline_height())
2089 shell_new_rows();
2091 return;
2094 /* When closing the help window, try restoring a snapshot after closing
2095 * the window. Otherwise clear the snapshot, it's now invalid. */
2096 if (win->w_buffer->b_help)
2097 help_window = TRUE;
2098 else
2099 clear_snapshot(curtab);
2101 #ifdef FEAT_AUTOCMD
2102 if (win == curwin)
2105 * Guess which window is going to be the new current window.
2106 * This may change because of the autocommands (sigh).
2108 wp = frame2win(win_altframe(win, NULL));
2111 * Be careful: If autocommands delete the window, return now.
2113 if (wp->w_buffer != curbuf)
2115 other_buffer = TRUE;
2116 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2117 if (!win_valid(win) || last_window())
2118 return;
2120 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2121 if (!win_valid(win) || last_window())
2122 return;
2123 # ifdef FEAT_EVAL
2124 /* autocmds may abort script processing */
2125 if (aborting())
2126 return;
2127 # endif
2129 #endif
2131 #ifdef FEAT_GUI
2132 /* Avoid trouble with scrollbars that are going to be deleted in
2133 * win_free(). */
2134 if (gui.in_use)
2135 out_flush();
2136 #endif
2139 * Close the link to the buffer.
2141 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2143 /* Autocommands may have closed the window already, or closed the only
2144 * other window or moved to another tab page. */
2145 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2146 return;
2148 /* Free the memory used for the window. */
2149 wp = win_free_mem(win, &dir, NULL);
2151 /* Make sure curwin isn't invalid. It can cause severe trouble when
2152 * printing an error message. For win_equal() curbuf needs to be valid
2153 * too. */
2154 if (win == curwin)
2156 curwin = wp;
2157 #ifdef FEAT_QUICKFIX
2158 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2161 * If the cursor goes to the preview or the quickfix window, try
2162 * finding another window to go to.
2164 for (;;)
2166 if (wp->w_next == NULL)
2167 wp = firstwin;
2168 else
2169 wp = wp->w_next;
2170 if (wp == curwin)
2171 break;
2172 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2174 curwin = wp;
2175 break;
2179 #endif
2180 curbuf = curwin->w_buffer;
2181 close_curwin = TRUE;
2183 if (p_ea
2184 #ifdef FEAT_VERTSPLIT
2185 && (*p_ead == 'b' || *p_ead == dir)
2186 #endif
2188 win_equal(curwin, TRUE,
2189 #ifdef FEAT_VERTSPLIT
2191 #else
2193 #endif
2195 else
2196 win_comp_pos();
2197 if (close_curwin)
2199 win_enter_ext(wp, FALSE, TRUE);
2200 #ifdef FEAT_AUTOCMD
2201 if (other_buffer)
2202 /* careful: after this wp and win may be invalid! */
2203 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2204 #endif
2208 * If last window has a status line now and we don't want one,
2209 * remove the status line.
2211 last_status(FALSE);
2213 /* After closing the help window, try restoring the window layout from
2214 * before it was opened. */
2215 if (help_window)
2216 restore_snapshot(close_curwin);
2218 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2219 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2220 if (gui.in_use && !win_hasvertsplit())
2221 gui_init_which_components(NULL);
2222 #endif
2224 redraw_all_later(NOT_VALID);
2228 * Close window "win" in tab page "tp", which is not the current tab page.
2229 * This may be the last window ih that tab page and result in closing the tab,
2230 * thus "tp" may become invalid!
2231 * Caller must check if buffer is hidden and whether the tabline needs to be
2232 * updated.
2234 void
2235 win_close_othertab(win, free_buf, tp)
2236 win_T *win;
2237 int free_buf;
2238 tabpage_T *tp;
2240 win_T *wp;
2241 int dir;
2242 tabpage_T *ptp = NULL;
2244 /* Close the link to the buffer. */
2245 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2247 /* Careful: Autocommands may have closed the tab page or made it the
2248 * current tab page. */
2249 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2251 if (ptp == NULL || tp == curtab)
2252 return;
2254 /* Autocommands may have closed the window already. */
2255 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2257 if (wp == NULL)
2258 return;
2260 /* Free the memory used for the window. */
2261 wp = win_free_mem(win, &dir, tp);
2263 /* When closing the last window in a tab page remove the tab page. */
2264 if (wp == NULL)
2266 if (tp == first_tabpage)
2267 first_tabpage = tp->tp_next;
2268 else
2270 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2271 ptp = ptp->tp_next)
2273 if (ptp == NULL)
2275 EMSG2(_(e_intern2), "win_close_othertab()");
2276 return;
2278 ptp->tp_next = tp->tp_next;
2280 free_tabpage(tp);
2285 * Free the memory used for a window.
2286 * Returns a pointer to the window that got the freed up space.
2288 static win_T *
2289 win_free_mem(win, dirp, tp)
2290 win_T *win;
2291 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2292 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2294 frame_T *frp;
2295 win_T *wp;
2297 #ifdef FEAT_FOLDING
2298 clearFolding(win);
2299 #endif
2301 /* reduce the reference count to the argument list. */
2302 alist_unlink(win->w_alist);
2304 /* Remove the window and its frame from the tree of frames. */
2305 frp = win->w_frame;
2306 wp = winframe_remove(win, dirp, tp);
2307 vim_free(frp);
2308 win_free(win, tp);
2310 /* When deleting the current window of another tab page select a new
2311 * current window. */
2312 if (tp != NULL && win == tp->tp_curwin)
2313 tp->tp_curwin = wp;
2315 return wp;
2318 #if defined(EXITFREE) || defined(PROTO)
2319 void
2320 win_free_all()
2322 int dummy;
2324 # ifdef FEAT_WINDOWS
2325 while (first_tabpage->tp_next != NULL)
2326 tabpage_close(TRUE);
2327 # endif
2329 while (firstwin != NULL)
2330 (void)win_free_mem(firstwin, &dummy, NULL);
2332 #endif
2335 * Remove a window and its frame from the tree of frames.
2336 * Returns a pointer to the window that got the freed up space.
2338 /*ARGSUSED*/
2339 static win_T *
2340 winframe_remove(win, dirp, tp)
2341 win_T *win;
2342 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2343 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2345 frame_T *frp, *frp2, *frp3;
2346 frame_T *frp_close = win->w_frame;
2347 win_T *wp;
2350 * If there is only one window there is nothing to remove.
2352 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2353 return NULL;
2356 * Remove the window from its frame.
2358 frp2 = win_altframe(win, tp);
2359 wp = frame2win(frp2);
2361 /* Remove this frame from the list of frames. */
2362 frame_remove(frp_close);
2364 #ifdef FEAT_VERTSPLIT
2365 if (frp_close->fr_parent->fr_layout == FR_COL)
2367 #endif
2368 /* When 'winfixheight' is set, try to find another frame in the column
2369 * (as close to the closed frame as possible) to distribute the height
2370 * to. */
2371 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2373 frp = frp_close->fr_prev;
2374 frp3 = frp_close->fr_next;
2375 while (frp != NULL || frp3 != NULL)
2377 if (frp != NULL)
2379 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2381 frp2 = frp;
2382 wp = frp->fr_win;
2383 break;
2385 frp = frp->fr_prev;
2387 if (frp3 != NULL)
2389 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2391 frp2 = frp3;
2392 wp = frp3->fr_win;
2393 break;
2395 frp3 = frp3->fr_next;
2399 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2400 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2401 #ifdef FEAT_VERTSPLIT
2402 *dirp = 'v';
2404 else
2406 /* When 'winfixwidth' is set, try to find another frame in the column
2407 * (as close to the closed frame as possible) to distribute the width
2408 * to. */
2409 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2411 frp = frp_close->fr_prev;
2412 frp3 = frp_close->fr_next;
2413 while (frp != NULL || frp3 != NULL)
2415 if (frp != NULL)
2417 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2419 frp2 = frp;
2420 wp = frp->fr_win;
2421 break;
2423 frp = frp->fr_prev;
2425 if (frp3 != NULL)
2427 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2429 frp2 = frp3;
2430 wp = frp3->fr_win;
2431 break;
2433 frp3 = frp3->fr_next;
2437 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2438 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2439 *dirp = 'h';
2441 #endif
2443 /* If rows/columns go to a window below/right its positions need to be
2444 * updated. Can only be done after the sizes have been updated. */
2445 if (frp2 == frp_close->fr_next)
2447 int row = win->w_winrow;
2448 int col = W_WINCOL(win);
2450 frame_comp_pos(frp2, &row, &col);
2453 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2455 /* There is no other frame in this list, move its info to the parent
2456 * and remove it. */
2457 frp2->fr_parent->fr_layout = frp2->fr_layout;
2458 frp2->fr_parent->fr_child = frp2->fr_child;
2459 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2460 frp->fr_parent = frp2->fr_parent;
2461 frp2->fr_parent->fr_win = frp2->fr_win;
2462 if (frp2->fr_win != NULL)
2463 frp2->fr_win->w_frame = frp2->fr_parent;
2464 frp = frp2->fr_parent;
2465 vim_free(frp2);
2467 frp2 = frp->fr_parent;
2468 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2470 /* The frame above the parent has the same layout, have to merge
2471 * the frames into this list. */
2472 if (frp2->fr_child == frp)
2473 frp2->fr_child = frp->fr_child;
2474 frp->fr_child->fr_prev = frp->fr_prev;
2475 if (frp->fr_prev != NULL)
2476 frp->fr_prev->fr_next = frp->fr_child;
2477 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2479 frp3->fr_parent = frp2;
2480 if (frp3->fr_next == NULL)
2482 frp3->fr_next = frp->fr_next;
2483 if (frp->fr_next != NULL)
2484 frp->fr_next->fr_prev = frp3;
2485 break;
2488 vim_free(frp);
2492 return wp;
2496 * Find out which frame is going to get the freed up space when "win" is
2497 * closed.
2498 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2499 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2500 * This makes opening a window and closing it immediately keep the same window
2501 * layout.
2503 static frame_T *
2504 win_altframe(win, tp)
2505 win_T *win;
2506 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2508 frame_T *frp;
2509 int b;
2511 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2512 /* Last window in this tab page, will go to next tab page. */
2513 return alt_tabpage()->tp_curwin->w_frame;
2515 frp = win->w_frame;
2516 #ifdef FEAT_VERTSPLIT
2517 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2518 b = p_spr;
2519 else
2520 #endif
2521 b = p_sb;
2522 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2523 return frp->fr_next;
2524 return frp->fr_prev;
2528 * Return the tabpage that will be used if the current one is closed.
2530 static tabpage_T *
2531 alt_tabpage()
2533 tabpage_T *tp;
2535 /* Use the next tab page if possible. */
2536 if (curtab->tp_next != NULL)
2537 return curtab->tp_next;
2539 /* Find the last but one tab page. */
2540 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2542 return tp;
2546 * Find the left-upper window in frame "frp".
2548 static win_T *
2549 frame2win(frp)
2550 frame_T *frp;
2552 while (frp->fr_win == NULL)
2553 frp = frp->fr_child;
2554 return frp->fr_win;
2558 * Return TRUE if frame "frp" contains window "wp".
2560 static int
2561 frame_has_win(frp, wp)
2562 frame_T *frp;
2563 win_T *wp;
2565 frame_T *p;
2567 if (frp->fr_layout == FR_LEAF)
2568 return frp->fr_win == wp;
2570 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2571 if (frame_has_win(p, wp))
2572 return TRUE;
2573 return FALSE;
2577 * Set a new height for a frame. Recursively sets the height for contained
2578 * frames and windows. Caller must take care of positions.
2580 static void
2581 frame_new_height(topfrp, height, topfirst, wfh)
2582 frame_T *topfrp;
2583 int height;
2584 int topfirst; /* resize topmost contained frame first */
2585 int wfh; /* obey 'winfixheight' when there is a choice;
2586 may cause the height not to be set */
2588 frame_T *frp;
2589 int extra_lines;
2590 int h;
2592 if (topfrp->fr_win != NULL)
2594 /* Simple case: just one window. */
2595 win_new_height(topfrp->fr_win,
2596 height - topfrp->fr_win->w_status_height);
2598 #ifdef FEAT_VERTSPLIT
2599 else if (topfrp->fr_layout == FR_ROW)
2603 /* All frames in this row get the same new height. */
2604 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2606 frame_new_height(frp, height, topfirst, wfh);
2607 if (frp->fr_height > height)
2609 /* Could not fit the windows, make the whole row higher. */
2610 height = frp->fr_height;
2611 break;
2615 while (frp != NULL);
2617 #endif
2618 else /* fr_layout == FR_COL */
2620 /* Complicated case: Resize a column of frames. Resize the bottom
2621 * frame first, frames above that when needed. */
2623 frp = topfrp->fr_child;
2624 if (wfh)
2625 /* Advance past frames with one window with 'wfh' set. */
2626 while (frame_fixed_height(frp))
2628 frp = frp->fr_next;
2629 if (frp == NULL)
2630 return; /* no frame without 'wfh', give up */
2632 if (!topfirst)
2634 /* Find the bottom frame of this column */
2635 while (frp->fr_next != NULL)
2636 frp = frp->fr_next;
2637 if (wfh)
2638 /* Advance back for frames with one window with 'wfh' set. */
2639 while (frame_fixed_height(frp))
2640 frp = frp->fr_prev;
2643 extra_lines = height - topfrp->fr_height;
2644 if (extra_lines < 0)
2646 /* reduce height of contained frames, bottom or top frame first */
2647 while (frp != NULL)
2649 h = frame_minheight(frp, NULL);
2650 if (frp->fr_height + extra_lines < h)
2652 extra_lines += frp->fr_height - h;
2653 frame_new_height(frp, h, topfirst, wfh);
2655 else
2657 frame_new_height(frp, frp->fr_height + extra_lines,
2658 topfirst, wfh);
2659 break;
2661 if (topfirst)
2664 frp = frp->fr_next;
2665 while (wfh && frp != NULL && frame_fixed_height(frp));
2667 else
2670 frp = frp->fr_prev;
2671 while (wfh && frp != NULL && frame_fixed_height(frp));
2673 /* Increase "height" if we could not reduce enough frames. */
2674 if (frp == NULL)
2675 height -= extra_lines;
2678 else if (extra_lines > 0)
2680 /* increase height of bottom or top frame */
2681 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2684 topfrp->fr_height = height;
2688 * Return TRUE if height of frame "frp" should not be changed because of
2689 * the 'winfixheight' option.
2691 static int
2692 frame_fixed_height(frp)
2693 frame_T *frp;
2695 /* frame with one window: fixed height if 'winfixheight' set. */
2696 if (frp->fr_win != NULL)
2697 return frp->fr_win->w_p_wfh;
2699 if (frp->fr_layout == FR_ROW)
2701 /* The frame is fixed height if one of the frames in the row is fixed
2702 * height. */
2703 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2704 if (frame_fixed_height(frp))
2705 return TRUE;
2706 return FALSE;
2709 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2710 * frames in the row are fixed height. */
2711 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2712 if (!frame_fixed_height(frp))
2713 return FALSE;
2714 return TRUE;
2717 #ifdef FEAT_VERTSPLIT
2719 * Return TRUE if width of frame "frp" should not be changed because of
2720 * the 'winfixwidth' option.
2722 static int
2723 frame_fixed_width(frp)
2724 frame_T *frp;
2726 /* frame with one window: fixed width if 'winfixwidth' set. */
2727 if (frp->fr_win != NULL)
2728 return frp->fr_win->w_p_wfw;
2730 if (frp->fr_layout == FR_COL)
2732 /* The frame is fixed width if one of the frames in the row is fixed
2733 * width. */
2734 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2735 if (frame_fixed_width(frp))
2736 return TRUE;
2737 return FALSE;
2740 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2741 * frames in the row are fixed width. */
2742 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2743 if (!frame_fixed_width(frp))
2744 return FALSE;
2745 return TRUE;
2749 * Add a status line to windows at the bottom of "frp".
2750 * Note: Does not check if there is room!
2752 static void
2753 frame_add_statusline(frp)
2754 frame_T *frp;
2756 win_T *wp;
2758 if (frp->fr_layout == FR_LEAF)
2760 wp = frp->fr_win;
2761 if (wp->w_status_height == 0)
2763 if (wp->w_height > 0) /* don't make it negative */
2764 --wp->w_height;
2765 wp->w_status_height = STATUS_HEIGHT;
2768 else if (frp->fr_layout == FR_ROW)
2770 /* Handle all the frames in the row. */
2771 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2772 frame_add_statusline(frp);
2774 else /* frp->fr_layout == FR_COL */
2776 /* Only need to handle the last frame in the column. */
2777 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2779 frame_add_statusline(frp);
2784 * Set width of a frame. Handles recursively going through contained frames.
2785 * May remove separator line for windows at the right side (for win_close()).
2787 static void
2788 frame_new_width(topfrp, width, leftfirst, wfw)
2789 frame_T *topfrp;
2790 int width;
2791 int leftfirst; /* resize leftmost contained frame first */
2792 int wfw; /* obey 'winfixwidth' when there is a choice;
2793 may cause the width not to be set */
2795 frame_T *frp;
2796 int extra_cols;
2797 int w;
2798 win_T *wp;
2800 if (topfrp->fr_layout == FR_LEAF)
2802 /* Simple case: just one window. */
2803 wp = topfrp->fr_win;
2804 /* Find out if there are any windows right of this one. */
2805 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2806 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2807 break;
2808 if (frp->fr_parent == NULL)
2809 wp->w_vsep_width = 0;
2810 win_new_width(wp, width - wp->w_vsep_width);
2812 else if (topfrp->fr_layout == FR_COL)
2816 /* All frames in this column get the same new width. */
2817 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2819 frame_new_width(frp, width, leftfirst, wfw);
2820 if (frp->fr_width > width)
2822 /* Could not fit the windows, make whole column wider. */
2823 width = frp->fr_width;
2824 break;
2827 } while (frp != NULL);
2829 else /* fr_layout == FR_ROW */
2831 /* Complicated case: Resize a row of frames. Resize the rightmost
2832 * frame first, frames left of it when needed. */
2834 frp = topfrp->fr_child;
2835 if (wfw)
2836 /* Advance past frames with one window with 'wfw' set. */
2837 while (frame_fixed_width(frp))
2839 frp = frp->fr_next;
2840 if (frp == NULL)
2841 return; /* no frame without 'wfw', give up */
2843 if (!leftfirst)
2845 /* Find the rightmost frame of this row */
2846 while (frp->fr_next != NULL)
2847 frp = frp->fr_next;
2848 if (wfw)
2849 /* Advance back for frames with one window with 'wfw' set. */
2850 while (frame_fixed_width(frp))
2851 frp = frp->fr_prev;
2854 extra_cols = width - topfrp->fr_width;
2855 if (extra_cols < 0)
2857 /* reduce frame width, rightmost frame first */
2858 while (frp != NULL)
2860 w = frame_minwidth(frp, NULL);
2861 if (frp->fr_width + extra_cols < w)
2863 extra_cols += frp->fr_width - w;
2864 frame_new_width(frp, w, leftfirst, wfw);
2866 else
2868 frame_new_width(frp, frp->fr_width + extra_cols,
2869 leftfirst, wfw);
2870 break;
2872 if (leftfirst)
2875 frp = frp->fr_next;
2876 while (wfw && frp != NULL && frame_fixed_width(frp));
2878 else
2881 frp = frp->fr_prev;
2882 while (wfw && frp != NULL && frame_fixed_width(frp));
2884 /* Increase "width" if we could not reduce enough frames. */
2885 if (frp == NULL)
2886 width -= extra_cols;
2889 else if (extra_cols > 0)
2891 /* increase width of rightmost frame */
2892 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2895 topfrp->fr_width = width;
2899 * Add the vertical separator to windows at the right side of "frp".
2900 * Note: Does not check if there is room!
2902 static void
2903 frame_add_vsep(frp)
2904 frame_T *frp;
2906 win_T *wp;
2908 if (frp->fr_layout == FR_LEAF)
2910 wp = frp->fr_win;
2911 if (wp->w_vsep_width == 0)
2913 if (wp->w_width > 0) /* don't make it negative */
2914 --wp->w_width;
2915 wp->w_vsep_width = 1;
2918 else if (frp->fr_layout == FR_COL)
2920 /* Handle all the frames in the column. */
2921 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2922 frame_add_vsep(frp);
2924 else /* frp->fr_layout == FR_ROW */
2926 /* Only need to handle the last frame in the row. */
2927 frp = frp->fr_child;
2928 while (frp->fr_next != NULL)
2929 frp = frp->fr_next;
2930 frame_add_vsep(frp);
2935 * Set frame width from the window it contains.
2937 static void
2938 frame_fix_width(wp)
2939 win_T *wp;
2941 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2943 #endif
2946 * Set frame height from the window it contains.
2948 static void
2949 frame_fix_height(wp)
2950 win_T *wp;
2952 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2956 * Compute the minimal height for frame "topfrp".
2957 * Uses the 'winminheight' option.
2958 * When "next_curwin" isn't NULL, use p_wh for this window.
2959 * When "next_curwin" is NOWIN, don't use at least one line for the current
2960 * window.
2962 static int
2963 frame_minheight(topfrp, next_curwin)
2964 frame_T *topfrp;
2965 win_T *next_curwin;
2967 frame_T *frp;
2968 int m;
2969 #ifdef FEAT_VERTSPLIT
2970 int n;
2971 #endif
2973 if (topfrp->fr_win != NULL)
2975 if (topfrp->fr_win == next_curwin)
2976 m = p_wh + topfrp->fr_win->w_status_height;
2977 else
2979 /* window: minimal height of the window plus status line */
2980 m = p_wmh + topfrp->fr_win->w_status_height;
2981 /* Current window is minimal one line high */
2982 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2983 ++m;
2986 #ifdef FEAT_VERTSPLIT
2987 else if (topfrp->fr_layout == FR_ROW)
2989 /* get the minimal height from each frame in this row */
2990 m = 0;
2991 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2993 n = frame_minheight(frp, next_curwin);
2994 if (n > m)
2995 m = n;
2998 #endif
2999 else
3001 /* Add up the minimal heights for all frames in this column. */
3002 m = 0;
3003 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3004 m += frame_minheight(frp, next_curwin);
3007 return m;
3010 #ifdef FEAT_VERTSPLIT
3012 * Compute the minimal width for frame "topfrp".
3013 * When "next_curwin" isn't NULL, use p_wiw for this window.
3014 * When "next_curwin" is NOWIN, don't use at least one column for the current
3015 * window.
3017 static int
3018 frame_minwidth(topfrp, next_curwin)
3019 frame_T *topfrp;
3020 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3022 frame_T *frp;
3023 int m, n;
3025 if (topfrp->fr_win != NULL)
3027 if (topfrp->fr_win == next_curwin)
3028 m = p_wiw + topfrp->fr_win->w_vsep_width;
3029 else
3031 /* window: minimal width of the window plus separator column */
3032 m = p_wmw + topfrp->fr_win->w_vsep_width;
3033 /* Current window is minimal one column wide */
3034 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3035 ++m;
3038 else if (topfrp->fr_layout == FR_COL)
3040 /* get the minimal width from each frame in this column */
3041 m = 0;
3042 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3044 n = frame_minwidth(frp, next_curwin);
3045 if (n > m)
3046 m = n;
3049 else
3051 /* Add up the minimal widths for all frames in this row. */
3052 m = 0;
3053 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3054 m += frame_minwidth(frp, next_curwin);
3057 return m;
3059 #endif
3063 * Try to close all windows except current one.
3064 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3065 * used and the buffer was modified.
3067 * Used by ":bdel" and ":only".
3069 void
3070 close_others(message, forceit)
3071 int message;
3072 int forceit; /* always hide all other windows */
3074 win_T *wp;
3075 win_T *nextwp;
3076 int r;
3078 if (lastwin == firstwin)
3080 if (message
3081 #ifdef FEAT_AUTOCMD
3082 && !autocmd_busy
3083 #endif
3085 MSG(_(m_onlyone));
3086 return;
3089 /* Be very careful here: autocommands may change the window layout. */
3090 for (wp = firstwin; win_valid(wp); wp = nextwp)
3092 nextwp = wp->w_next;
3093 if (wp != curwin) /* don't close current window */
3096 /* Check if it's allowed to abandon this window */
3097 r = can_abandon(wp->w_buffer, forceit);
3098 #ifdef FEAT_AUTOCMD
3099 if (!win_valid(wp)) /* autocommands messed wp up */
3101 nextwp = firstwin;
3102 continue;
3104 #endif
3105 if (!r)
3107 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3108 if (message && (p_confirm || cmdmod.confirm) && p_write)
3110 dialog_changed(wp->w_buffer, FALSE);
3111 # ifdef FEAT_AUTOCMD
3112 if (!win_valid(wp)) /* autocommands messed wp up */
3114 nextwp = firstwin;
3115 continue;
3117 # endif
3119 if (bufIsChanged(wp->w_buffer))
3120 #endif
3121 continue;
3123 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3127 if (message && lastwin != firstwin)
3128 EMSG(_("E445: Other window contains changes"));
3131 #endif /* FEAT_WINDOWS */
3134 * Init the current window "curwin".
3135 * Called when a new file is being edited.
3137 void
3138 curwin_init()
3140 redraw_win_later(curwin, NOT_VALID);
3141 curwin->w_lines_valid = 0;
3142 curwin->w_cursor.lnum = 1;
3143 curwin->w_curswant = curwin->w_cursor.col = 0;
3144 #ifdef FEAT_VIRTUALEDIT
3145 curwin->w_cursor.coladd = 0;
3146 #endif
3147 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3148 curwin->w_pcmark.col = 0;
3149 curwin->w_prev_pcmark.lnum = 0;
3150 curwin->w_prev_pcmark.col = 0;
3151 curwin->w_topline = 1;
3152 #ifdef FEAT_DIFF
3153 curwin->w_topfill = 0;
3154 #endif
3155 curwin->w_botline = 2;
3156 #ifdef FEAT_FKMAP
3157 if (curwin->w_p_rl)
3158 curwin->w_farsi = W_CONV + W_R_L;
3159 else
3160 curwin->w_farsi = W_CONV;
3161 #endif
3165 * Allocate the first window and put an empty buffer in it.
3166 * Called from main().
3167 * Return FAIL when something goes wrong (out of memory).
3170 win_alloc_first()
3172 if (win_alloc_firstwin(NULL) == FAIL)
3173 return FAIL;
3175 #ifdef FEAT_WINDOWS
3176 first_tabpage = alloc_tabpage();
3177 if (first_tabpage == NULL)
3178 return FAIL;
3179 first_tabpage->tp_topframe = topframe;
3180 curtab = first_tabpage;
3181 #endif
3182 return OK;
3186 * Allocate the first window or the first window in a new tab page.
3187 * When "oldwin" is NULL create an empty buffer for it.
3188 * When "oldwin" is not NULL copy info from it to the new window (only with
3189 * FEAT_WINDOWS).
3190 * Return FAIL when something goes wrong (out of memory).
3192 static int
3193 win_alloc_firstwin(oldwin)
3194 win_T *oldwin;
3196 curwin = win_alloc(NULL);
3197 if (oldwin == NULL)
3199 /* Very first window, need to create an empty buffer for it and
3200 * initialize from scratch. */
3201 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3202 if (curwin == NULL || curbuf == NULL)
3203 return FAIL;
3204 curwin->w_buffer = curbuf;
3205 curbuf->b_nwindows = 1; /* there is one window */
3206 #ifdef FEAT_WINDOWS
3207 curwin->w_alist = &global_alist;
3208 #endif
3209 curwin_init(); /* init current window */
3211 #ifdef FEAT_WINDOWS
3212 else
3214 /* First window in new tab page, initialize it from "oldwin". */
3215 win_init(curwin, oldwin);
3217 # ifdef FEAT_SCROLLBIND
3218 /* We don't want scroll-binding in the first window. */
3219 curwin->w_p_scb = FALSE;
3220 # endif
3222 #endif
3224 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3225 if (topframe == NULL)
3226 return FAIL;
3227 topframe->fr_layout = FR_LEAF;
3228 #ifdef FEAT_VERTSPLIT
3229 topframe->fr_width = Columns;
3230 #endif
3231 topframe->fr_height = Rows - p_ch;
3232 topframe->fr_win = curwin;
3233 curwin->w_frame = topframe;
3235 return OK;
3239 * Initialize the window and frame size to the maximum.
3241 void
3242 win_init_size()
3244 firstwin->w_height = ROWS_AVAIL;
3245 topframe->fr_height = ROWS_AVAIL;
3246 #ifdef FEAT_VERTSPLIT
3247 firstwin->w_width = Columns;
3248 topframe->fr_width = Columns;
3249 #endif
3252 #if defined(FEAT_WINDOWS) || defined(PROTO)
3255 * Allocate a new tabpage_T and init the values.
3256 * Returns NULL when out of memory.
3258 static tabpage_T *
3259 alloc_tabpage()
3261 tabpage_T *tp;
3263 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3264 if (tp != NULL)
3266 # ifdef FEAT_GUI
3267 int i;
3269 for (i = 0; i < 3; i++)
3270 tp->tp_prev_which_scrollbars[i] = -1;
3271 # endif
3272 # ifdef FEAT_DIFF
3273 tp->tp_diff_invalid = TRUE;
3274 # endif
3275 #ifdef FEAT_EVAL
3276 /* init t: variables */
3277 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3278 #endif
3279 tp->tp_ch_used = p_ch;
3281 return tp;
3284 void
3285 free_tabpage(tp)
3286 tabpage_T *tp;
3288 # ifdef FEAT_DIFF
3289 diff_clear(tp);
3290 # endif
3291 clear_snapshot(tp);
3292 #ifdef FEAT_EVAL
3293 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3294 #endif
3295 vim_free(tp);
3299 * Create a new Tab page with one window.
3300 * It will edit the current buffer, like after ":split".
3301 * When "after" is 0 put it just after the current Tab page.
3302 * Otherwise put it just before tab page "after".
3303 * Return FAIL or OK.
3306 win_new_tabpage(after)
3307 int after;
3309 tabpage_T *tp = curtab;
3310 tabpage_T *newtp;
3311 int n;
3313 newtp = alloc_tabpage();
3314 if (newtp == NULL)
3315 return FAIL;
3317 /* Remember the current windows in this Tab page. */
3318 if (leave_tabpage(curbuf) == FAIL)
3320 vim_free(newtp);
3321 return FAIL;
3323 curtab = newtp;
3325 /* Create a new empty window. */
3326 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3328 /* Make the new Tab page the new topframe. */
3329 if (after == 1)
3331 /* New tab page becomes the first one. */
3332 newtp->tp_next = first_tabpage;
3333 first_tabpage = newtp;
3335 else
3337 if (after > 0)
3339 /* Put new tab page before tab page "after". */
3340 n = 2;
3341 for (tp = first_tabpage; tp->tp_next != NULL
3342 && n < after; tp = tp->tp_next)
3343 ++n;
3345 newtp->tp_next = tp->tp_next;
3346 tp->tp_next = newtp;
3348 win_init_size();
3349 firstwin->w_winrow = tabline_height();
3350 win_comp_scroll(curwin);
3352 newtp->tp_topframe = topframe;
3353 last_status(FALSE);
3355 #if defined(FEAT_GUI)
3356 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3357 * scrollbars. Have to update them anyway. */
3358 if (gui.in_use && starting == 0)
3360 gui_init_which_components(NULL);
3361 gui_update_scrollbars(TRUE);
3363 need_mouse_correct = TRUE;
3364 #endif
3366 redraw_all_later(CLEAR);
3367 #ifdef FEAT_AUTOCMD
3368 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3369 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3370 #endif
3371 return OK;
3374 /* Failed, get back the previous Tab page */
3375 enter_tabpage(curtab, curbuf);
3376 return FAIL;
3380 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3381 * like with ":split".
3382 * Returns OK if a new tab page was created, FAIL otherwise.
3385 may_open_tabpage()
3387 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3389 if (n != 0)
3391 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3392 postponed_split_tab = 0;
3393 return win_new_tabpage(n);
3395 return FAIL;
3399 * Create up to "maxcount" tabpages with empty windows.
3400 * Returns the number of resulting tab pages.
3403 make_tabpages(maxcount)
3404 int maxcount;
3406 int count = maxcount;
3407 int todo;
3409 /* Limit to 'tabpagemax' tabs. */
3410 if (count > p_tpm)
3411 count = p_tpm;
3413 #ifdef FEAT_AUTOCMD
3415 * Don't execute autocommands while creating the tab pages. Must do that
3416 * when putting the buffers in the windows.
3418 block_autocmds();
3419 #endif
3421 for (todo = count - 1; todo > 0; --todo)
3422 if (win_new_tabpage(0) == FAIL)
3423 break;
3425 #ifdef FEAT_AUTOCMD
3426 unblock_autocmds();
3427 #endif
3429 /* return actual number of tab pages */
3430 return (count - todo);
3434 * Return TRUE when "tpc" points to a valid tab page.
3437 valid_tabpage(tpc)
3438 tabpage_T *tpc;
3440 tabpage_T *tp;
3442 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3443 if (tp == tpc)
3444 return TRUE;
3445 return FALSE;
3449 * Find tab page "n" (first one is 1). Returns NULL when not found.
3451 tabpage_T *
3452 find_tabpage(n)
3453 int n;
3455 tabpage_T *tp;
3456 int i = 1;
3458 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3459 ++i;
3460 return tp;
3464 * Get index of tab page "tp". First one has index 1.
3465 * When not found returns number of tab pages plus one.
3468 tabpage_index(ftp)
3469 tabpage_T *ftp;
3471 int i = 1;
3472 tabpage_T *tp;
3474 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3475 ++i;
3476 return i;
3480 * Prepare for leaving the current tab page.
3481 * When autocomands change "curtab" we don't leave the tab page and return
3482 * FAIL.
3483 * Careful: When OK is returned need to get a new tab page very very soon!
3485 /*ARGSUSED*/
3486 static int
3487 leave_tabpage(new_curbuf)
3488 buf_T *new_curbuf; /* what is going to be the new curbuf,
3489 NULL if unknown */
3491 tabpage_T *tp = curtab;
3493 #ifdef FEAT_VISUAL
3494 reset_VIsual_and_resel(); /* stop Visual mode */
3495 #endif
3496 #ifdef FEAT_AUTOCMD
3497 if (new_curbuf != curbuf)
3499 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3500 if (curtab != tp)
3501 return FAIL;
3503 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3504 if (curtab != tp)
3505 return FAIL;
3506 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3507 if (curtab != tp)
3508 return FAIL;
3509 #endif
3510 #if defined(FEAT_GUI)
3511 /* Remove the scrollbars. They may be added back later. */
3512 if (gui.in_use)
3513 gui_remove_scrollbars();
3514 #endif
3515 tp->tp_curwin = curwin;
3516 tp->tp_prevwin = prevwin;
3517 tp->tp_firstwin = firstwin;
3518 tp->tp_lastwin = lastwin;
3519 tp->tp_old_Rows = Rows;
3520 tp->tp_old_Columns = Columns;
3521 firstwin = NULL;
3522 lastwin = NULL;
3523 return OK;
3527 * Start using tab page "tp".
3528 * Only to be used after leave_tabpage() or freeing the current tab page.
3530 /*ARGSUSED*/
3531 static void
3532 enter_tabpage(tp, old_curbuf)
3533 tabpage_T *tp;
3534 buf_T *old_curbuf;
3536 int old_off = tp->tp_firstwin->w_winrow;
3537 win_T *next_prevwin = tp->tp_prevwin;
3539 curtab = tp;
3540 firstwin = tp->tp_firstwin;
3541 lastwin = tp->tp_lastwin;
3542 topframe = tp->tp_topframe;
3544 /* We would like doing the TabEnter event first, but we don't have a
3545 * valid current window yet, which may break some commands.
3546 * This triggers autocommands, thus may make "tp" invalid. */
3547 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3548 prevwin = next_prevwin;
3550 #ifdef FEAT_AUTOCMD
3551 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3553 if (old_curbuf != curbuf)
3554 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3555 #endif
3557 last_status(FALSE); /* status line may appear or disappear */
3558 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3559 must_redraw = CLEAR; /* need to redraw everything */
3560 #ifdef FEAT_DIFF
3561 diff_need_scrollbind = TRUE;
3562 #endif
3564 /* The tabpage line may have appeared or disappeared, may need to resize
3565 * the frames for that. When the Vim window was resized need to update
3566 * frame sizes too. Use the stored value of p_ch, so that it can be
3567 * different for each tab page. */
3568 p_ch = curtab->tp_ch_used;
3569 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3570 #ifdef FEAT_GUI_TABLINE
3571 && !gui_use_tabline()
3572 #endif
3574 shell_new_rows();
3575 #ifdef FEAT_VERTSPLIT
3576 if (curtab->tp_old_Columns != Columns && starting == 0)
3577 shell_new_columns(); /* update window widths */
3578 #endif
3580 #if defined(FEAT_GUI)
3581 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3582 * scrollbars. Have to update them anyway. */
3583 if (gui.in_use && starting == 0)
3585 gui_init_which_components(NULL);
3586 gui_update_scrollbars(TRUE);
3588 need_mouse_correct = TRUE;
3589 #endif
3591 redraw_all_later(CLEAR);
3595 * Go to tab page "n". For ":tab N" and "Ngt".
3596 * When "n" is 9999 go to the last tab page.
3598 void
3599 goto_tabpage(n)
3600 int n;
3602 tabpage_T *tp;
3603 tabpage_T *ttp;
3604 int i;
3606 if (text_locked())
3608 /* Not allowed when editing the command line. */
3609 #ifdef FEAT_CMDWIN
3610 if (cmdwin_type != 0)
3611 EMSG(_(e_cmdwin));
3612 else
3613 #endif
3614 EMSG(_(e_secure));
3615 return;
3618 /* If there is only one it can't work. */
3619 if (first_tabpage->tp_next == NULL)
3621 if (n > 1)
3622 beep_flush();
3623 return;
3626 if (n == 0)
3628 /* No count, go to next tab page, wrap around end. */
3629 if (curtab->tp_next == NULL)
3630 tp = first_tabpage;
3631 else
3632 tp = curtab->tp_next;
3634 else if (n < 0)
3636 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3637 * this N times. */
3638 ttp = curtab;
3639 for (i = n; i < 0; ++i)
3641 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3642 tp = tp->tp_next)
3644 ttp = tp;
3647 else if (n == 9999)
3649 /* Go to last tab page. */
3650 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3653 else
3655 /* Go to tab page "n". */
3656 tp = find_tabpage(n);
3657 if (tp == NULL)
3659 beep_flush();
3660 return;
3664 goto_tabpage_tp(tp);
3666 #ifdef FEAT_GUI_TABLINE
3667 if (gui_use_tabline())
3668 gui_mch_set_curtab(tabpage_index(curtab));
3669 #endif
3673 * Go to tabpage "tp".
3674 * Note: doesn't update the GUI tab.
3676 void
3677 goto_tabpage_tp(tp)
3678 tabpage_T *tp;
3680 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3682 if (valid_tabpage(tp))
3683 enter_tabpage(tp, curbuf);
3684 else
3685 enter_tabpage(curtab, curbuf);
3690 * Enter window "wp" in tab page "tp".
3691 * Also updates the GUI tab.
3693 void
3694 goto_tabpage_win(tp, wp)
3695 tabpage_T *tp;
3696 win_T *wp;
3698 goto_tabpage_tp(tp);
3699 if (curtab == tp && win_valid(wp))
3701 win_enter(wp, TRUE);
3702 # ifdef FEAT_GUI_TABLINE
3703 if (gui_use_tabline())
3704 gui_mch_set_curtab(tabpage_index(curtab));
3705 # endif
3710 * Move the current tab page to before tab page "nr".
3712 void
3713 tabpage_move(nr)
3714 int nr;
3716 int n = nr;
3717 tabpage_T *tp;
3719 if (first_tabpage->tp_next == NULL)
3720 return;
3722 /* Remove the current tab page from the list of tab pages. */
3723 if (curtab == first_tabpage)
3724 first_tabpage = curtab->tp_next;
3725 else
3727 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3728 if (tp->tp_next == curtab)
3729 break;
3730 if (tp == NULL) /* "cannot happen" */
3731 return;
3732 tp->tp_next = curtab->tp_next;
3735 /* Re-insert it at the specified position. */
3736 if (n == 0)
3738 curtab->tp_next = first_tabpage;
3739 first_tabpage = curtab;
3741 else
3743 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3744 --n;
3745 curtab->tp_next = tp->tp_next;
3746 tp->tp_next = curtab;
3749 /* Need to redraw the tabline. Tab page contents doesn't change. */
3750 redraw_tabline = TRUE;
3755 * Go to another window.
3756 * When jumping to another buffer, stop Visual mode. Do this before
3757 * changing windows so we can yank the selection into the '*' register.
3758 * When jumping to another window on the same buffer, adjust its cursor
3759 * position to keep the same Visual area.
3761 void
3762 win_goto(wp)
3763 win_T *wp;
3765 if (text_locked())
3767 beep_flush();
3768 text_locked_msg();
3769 return;
3771 #ifdef FEAT_AUTOCMD
3772 if (curbuf_locked())
3773 return;
3774 #endif
3776 #ifdef FEAT_VISUAL
3777 if (wp->w_buffer != curbuf)
3778 reset_VIsual_and_resel();
3779 else if (VIsual_active)
3780 wp->w_cursor = curwin->w_cursor;
3781 #endif
3783 #ifdef FEAT_GUI
3784 need_mouse_correct = TRUE;
3785 #endif
3786 win_enter(wp, TRUE);
3789 #if defined(FEAT_PERL) || defined(PROTO)
3791 * Find window number "winnr" (counting top to bottom).
3793 win_T *
3794 win_find_nr(winnr)
3795 int winnr;
3797 win_T *wp;
3799 # ifdef FEAT_WINDOWS
3800 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3801 if (--winnr == 0)
3802 break;
3803 return wp;
3804 # else
3805 return curwin;
3806 # endif
3808 #endif
3810 #ifdef FEAT_VERTSPLIT
3812 * Move to window above or below "count" times.
3814 static void
3815 win_goto_ver(up, count)
3816 int up; /* TRUE to go to win above */
3817 long count;
3819 frame_T *fr;
3820 frame_T *nfr;
3821 frame_T *foundfr;
3823 foundfr = curwin->w_frame;
3824 while (count--)
3827 * First go upwards in the tree of frames until we find a upwards or
3828 * downwards neighbor.
3830 fr = foundfr;
3831 for (;;)
3833 if (fr == topframe)
3834 goto end;
3835 if (up)
3836 nfr = fr->fr_prev;
3837 else
3838 nfr = fr->fr_next;
3839 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3840 break;
3841 fr = fr->fr_parent;
3845 * Now go downwards to find the bottom or top frame in it.
3847 for (;;)
3849 if (nfr->fr_layout == FR_LEAF)
3851 foundfr = nfr;
3852 break;
3854 fr = nfr->fr_child;
3855 if (nfr->fr_layout == FR_ROW)
3857 /* Find the frame at the cursor row. */
3858 while (fr->fr_next != NULL
3859 && frame2win(fr)->w_wincol + fr->fr_width
3860 <= curwin->w_wincol + curwin->w_wcol)
3861 fr = fr->fr_next;
3863 if (nfr->fr_layout == FR_COL && up)
3864 while (fr->fr_next != NULL)
3865 fr = fr->fr_next;
3866 nfr = fr;
3869 end:
3870 if (foundfr != NULL)
3871 win_goto(foundfr->fr_win);
3875 * Move to left or right window.
3877 static void
3878 win_goto_hor(left, count)
3879 int left; /* TRUE to go to left win */
3880 long count;
3882 frame_T *fr;
3883 frame_T *nfr;
3884 frame_T *foundfr;
3886 foundfr = curwin->w_frame;
3887 while (count--)
3890 * First go upwards in the tree of frames until we find a left or
3891 * right neighbor.
3893 fr = foundfr;
3894 for (;;)
3896 if (fr == topframe)
3897 goto end;
3898 if (left)
3899 nfr = fr->fr_prev;
3900 else
3901 nfr = fr->fr_next;
3902 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3903 break;
3904 fr = fr->fr_parent;
3908 * Now go downwards to find the leftmost or rightmost frame in it.
3910 for (;;)
3912 if (nfr->fr_layout == FR_LEAF)
3914 foundfr = nfr;
3915 break;
3917 fr = nfr->fr_child;
3918 if (nfr->fr_layout == FR_COL)
3920 /* Find the frame at the cursor row. */
3921 while (fr->fr_next != NULL
3922 && frame2win(fr)->w_winrow + fr->fr_height
3923 <= curwin->w_winrow + curwin->w_wrow)
3924 fr = fr->fr_next;
3926 if (nfr->fr_layout == FR_ROW && left)
3927 while (fr->fr_next != NULL)
3928 fr = fr->fr_next;
3929 nfr = fr;
3932 end:
3933 if (foundfr != NULL)
3934 win_goto(foundfr->fr_win);
3936 #endif
3939 * Make window "wp" the current window.
3941 void
3942 win_enter(wp, undo_sync)
3943 win_T *wp;
3944 int undo_sync;
3946 win_enter_ext(wp, undo_sync, FALSE);
3950 * Make window wp the current window.
3951 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3952 * been closed and isn't valid.
3954 static void
3955 win_enter_ext(wp, undo_sync, curwin_invalid)
3956 win_T *wp;
3957 int undo_sync;
3958 int curwin_invalid;
3960 #ifdef FEAT_AUTOCMD
3961 int other_buffer = FALSE;
3962 #endif
3964 if (wp == curwin && !curwin_invalid) /* nothing to do */
3965 return;
3967 #ifdef FEAT_AUTOCMD
3968 if (!curwin_invalid)
3971 * Be careful: If autocommands delete the window, return now.
3973 if (wp->w_buffer != curbuf)
3975 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3976 other_buffer = TRUE;
3977 if (!win_valid(wp))
3978 return;
3980 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3981 if (!win_valid(wp))
3982 return;
3983 # ifdef FEAT_EVAL
3984 /* autocmds may abort script processing */
3985 if (aborting())
3986 return;
3987 # endif
3989 #endif
3991 /* sync undo before leaving the current buffer */
3992 if (undo_sync && curbuf != wp->w_buffer)
3993 u_sync(FALSE);
3994 /* may have to copy the buffer options when 'cpo' contains 'S' */
3995 if (wp->w_buffer != curbuf)
3996 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3997 if (!curwin_invalid)
3999 prevwin = curwin; /* remember for CTRL-W p */
4000 curwin->w_redr_status = TRUE;
4002 curwin = wp;
4003 curbuf = wp->w_buffer;
4004 check_cursor();
4005 #ifdef FEAT_VIRTUALEDIT
4006 if (!virtual_active())
4007 curwin->w_cursor.coladd = 0;
4008 #endif
4009 changed_line_abv_curs(); /* assume cursor position needs updating */
4011 if (curwin->w_localdir != NULL)
4013 /* Window has a local directory: Save current directory as global
4014 * directory (unless that was done already) and change to the local
4015 * directory. */
4016 if (globaldir == NULL)
4018 char_u cwd[MAXPATHL];
4020 if (mch_dirname(cwd, MAXPATHL) == OK)
4021 globaldir = vim_strsave(cwd);
4023 mch_chdir((char *)curwin->w_localdir);
4024 shorten_fnames(TRUE);
4026 else if (globaldir != NULL)
4028 /* Window doesn't have a local directory and we are not in the global
4029 * directory: Change to the global directory. */
4030 mch_chdir((char *)globaldir);
4031 vim_free(globaldir);
4032 globaldir = NULL;
4033 shorten_fnames(TRUE);
4036 #ifdef FEAT_AUTOCMD
4037 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4038 if (other_buffer)
4039 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4040 #endif
4042 #ifdef FEAT_TITLE
4043 maketitle();
4044 #endif
4045 curwin->w_redr_status = TRUE;
4046 redraw_tabline = TRUE;
4047 if (restart_edit)
4048 redraw_later(VALID); /* causes status line redraw */
4050 /* set window height to desired minimal value */
4051 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4052 win_setheight((int)p_wh);
4053 else if (curwin->w_height == 0)
4054 win_setheight(1);
4056 #ifdef FEAT_VERTSPLIT
4057 /* set window width to desired minimal value */
4058 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4059 win_setwidth((int)p_wiw);
4060 #endif
4062 #ifdef FEAT_MOUSE
4063 setmouse(); /* in case jumped to/from help buffer */
4064 #endif
4066 /* Change directories when the 'acd' option is set. */
4067 DO_AUTOCHDIR
4070 #endif /* FEAT_WINDOWS */
4072 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4074 * Jump to the first open window that contains buffer "buf", if one exists.
4075 * Returns a pointer to the window found, otherwise NULL.
4077 win_T *
4078 buf_jump_open_win(buf)
4079 buf_T *buf;
4081 # ifdef FEAT_WINDOWS
4082 win_T *wp;
4084 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4085 if (wp->w_buffer == buf)
4086 break;
4087 if (wp != NULL)
4088 win_enter(wp, FALSE);
4089 return wp;
4090 # else
4091 if (curwin->w_buffer == buf)
4092 return curwin;
4093 return NULL;
4094 # endif
4098 * Jump to the first open window in any tab page that contains buffer "buf",
4099 * if one exists.
4100 * Returns a pointer to the window found, otherwise NULL.
4102 win_T *
4103 buf_jump_open_tab(buf)
4104 buf_T *buf;
4106 # ifdef FEAT_WINDOWS
4107 win_T *wp;
4108 tabpage_T *tp;
4110 /* First try the current tab page. */
4111 wp = buf_jump_open_win(buf);
4112 if (wp != NULL)
4113 return wp;
4115 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4116 if (tp != curtab)
4118 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4119 if (wp->w_buffer == buf)
4120 break;
4121 if (wp != NULL)
4123 goto_tabpage_win(tp, wp);
4124 if (curwin != wp)
4125 wp = NULL; /* something went wrong */
4126 break;
4130 return wp;
4131 # else
4132 if (curwin->w_buffer == buf)
4133 return curwin;
4134 return NULL;
4135 # endif
4137 #endif
4140 * allocate a window structure and link it in the window list
4142 /*ARGSUSED*/
4143 static win_T *
4144 win_alloc(after)
4145 win_T *after;
4147 win_T *newwin;
4150 * allocate window structure and linesizes arrays
4152 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4153 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4155 vim_free(newwin);
4156 newwin = NULL;
4159 if (newwin != NULL)
4161 #ifdef FEAT_AUTOCMD
4162 /* Don't execute autocommands while the window is not properly
4163 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4164 * event. */
4165 block_autocmds();
4166 #endif
4168 * link the window in the window list
4170 #ifdef FEAT_WINDOWS
4171 win_append(after, newwin);
4172 #endif
4173 #ifdef FEAT_VERTSPLIT
4174 newwin->w_wincol = 0;
4175 newwin->w_width = Columns;
4176 #endif
4178 /* position the display and the cursor at the top of the file. */
4179 newwin->w_topline = 1;
4180 #ifdef FEAT_DIFF
4181 newwin->w_topfill = 0;
4182 #endif
4183 newwin->w_botline = 2;
4184 newwin->w_cursor.lnum = 1;
4185 #ifdef FEAT_SCROLLBIND
4186 newwin->w_scbind_pos = 1;
4187 #endif
4189 /* We won't calculate w_fraction until resizing the window */
4190 newwin->w_fraction = 0;
4191 newwin->w_prev_fraction_row = -1;
4193 #ifdef FEAT_GUI
4194 if (gui.in_use)
4196 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4197 SBAR_LEFT, newwin);
4198 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4199 SBAR_RIGHT, newwin);
4201 #endif
4202 #ifdef FEAT_EVAL
4203 /* init w: variables */
4204 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4205 #endif
4206 #ifdef FEAT_FOLDING
4207 foldInitWin(newwin);
4208 #endif
4209 #ifdef FEAT_AUTOCMD
4210 unblock_autocmds();
4211 #endif
4212 #ifdef FEAT_SEARCH_EXTRA
4213 newwin->w_match_head = NULL;
4214 newwin->w_next_match_id = 4;
4215 #endif
4217 return newwin;
4220 #if defined(FEAT_WINDOWS) || defined(PROTO)
4223 * remove window 'wp' from the window list and free the structure
4225 static void
4226 win_free(wp, tp)
4227 win_T *wp;
4228 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4230 int i;
4232 #ifdef FEAT_AUTOCMD
4233 /* Don't execute autocommands while the window is halfway being deleted.
4234 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4235 block_autocmds();
4236 #endif
4238 #ifdef FEAT_MZSCHEME
4239 mzscheme_window_free(wp);
4240 #endif
4242 #ifdef FEAT_PERL
4243 perl_win_free(wp);
4244 #endif
4246 #ifdef FEAT_PYTHON
4247 python_window_free(wp);
4248 #endif
4250 #ifdef FEAT_TCL
4251 tcl_window_free(wp);
4252 #endif
4254 #ifdef FEAT_RUBY
4255 ruby_window_free(wp);
4256 #endif
4258 clear_winopt(&wp->w_onebuf_opt);
4259 clear_winopt(&wp->w_allbuf_opt);
4261 #ifdef FEAT_EVAL
4262 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4263 #endif
4265 if (prevwin == wp)
4266 prevwin = NULL;
4267 win_free_lsize(wp);
4269 for (i = 0; i < wp->w_tagstacklen; ++i)
4270 vim_free(wp->w_tagstack[i].tagname);
4272 vim_free(wp->w_localdir);
4274 #ifdef FEAT_SEARCH_EXTRA
4275 clear_matches(wp);
4276 #endif
4278 #ifdef FEAT_JUMPLIST
4279 free_jumplist(wp);
4280 #endif
4282 #ifdef FEAT_QUICKFIX
4283 qf_free_all(wp);
4284 #endif
4286 #ifdef FEAT_GUI
4287 if (gui.in_use)
4289 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4290 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4292 #endif /* FEAT_GUI */
4294 win_remove(wp, tp);
4295 vim_free(wp);
4297 #ifdef FEAT_AUTOCMD
4298 unblock_autocmds();
4299 #endif
4303 * Append window "wp" in the window list after window "after".
4305 static void
4306 win_append(after, wp)
4307 win_T *after, *wp;
4309 win_T *before;
4311 if (after == NULL) /* after NULL is in front of the first */
4312 before = firstwin;
4313 else
4314 before = after->w_next;
4316 wp->w_next = before;
4317 wp->w_prev = after;
4318 if (after == NULL)
4319 firstwin = wp;
4320 else
4321 after->w_next = wp;
4322 if (before == NULL)
4323 lastwin = wp;
4324 else
4325 before->w_prev = wp;
4329 * Remove a window from the window list.
4331 static void
4332 win_remove(wp, tp)
4333 win_T *wp;
4334 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4336 if (wp->w_prev != NULL)
4337 wp->w_prev->w_next = wp->w_next;
4338 else if (tp == NULL)
4339 firstwin = wp->w_next;
4340 else
4341 tp->tp_firstwin = wp->w_next;
4342 if (wp->w_next != NULL)
4343 wp->w_next->w_prev = wp->w_prev;
4344 else if (tp == NULL)
4345 lastwin = wp->w_prev;
4346 else
4347 tp->tp_lastwin = wp->w_prev;
4351 * Append frame "frp" in a frame list after frame "after".
4353 static void
4354 frame_append(after, frp)
4355 frame_T *after, *frp;
4357 frp->fr_next = after->fr_next;
4358 after->fr_next = frp;
4359 if (frp->fr_next != NULL)
4360 frp->fr_next->fr_prev = frp;
4361 frp->fr_prev = after;
4365 * Insert frame "frp" in a frame list before frame "before".
4367 static void
4368 frame_insert(before, frp)
4369 frame_T *before, *frp;
4371 frp->fr_next = before;
4372 frp->fr_prev = before->fr_prev;
4373 before->fr_prev = frp;
4374 if (frp->fr_prev != NULL)
4375 frp->fr_prev->fr_next = frp;
4376 else
4377 frp->fr_parent->fr_child = frp;
4381 * Remove a frame from a frame list.
4383 static void
4384 frame_remove(frp)
4385 frame_T *frp;
4387 if (frp->fr_prev != NULL)
4388 frp->fr_prev->fr_next = frp->fr_next;
4389 else
4390 frp->fr_parent->fr_child = frp->fr_next;
4391 if (frp->fr_next != NULL)
4392 frp->fr_next->fr_prev = frp->fr_prev;
4395 #endif /* FEAT_WINDOWS */
4398 * Allocate w_lines[] for window "wp".
4399 * Return FAIL for failure, OK for success.
4402 win_alloc_lines(wp)
4403 win_T *wp;
4405 wp->w_lines_valid = 0;
4406 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4407 if (wp->w_lines == NULL)
4408 return FAIL;
4409 return OK;
4413 * free lsize arrays for a window
4415 void
4416 win_free_lsize(wp)
4417 win_T *wp;
4419 vim_free(wp->w_lines);
4420 wp->w_lines = NULL;
4424 * Called from win_new_shellsize() after Rows changed.
4425 * This only does the current tab page, others must be done when made active.
4427 void
4428 shell_new_rows()
4430 int h = (int)ROWS_AVAIL;
4432 if (firstwin == NULL) /* not initialized yet */
4433 return;
4434 #ifdef FEAT_WINDOWS
4435 if (h < frame_minheight(topframe, NULL))
4436 h = frame_minheight(topframe, NULL);
4438 /* First try setting the heights of windows with 'winfixheight'. If
4439 * that doesn't result in the right height, forget about that option. */
4440 frame_new_height(topframe, h, FALSE, TRUE);
4441 if (topframe->fr_height != h)
4442 frame_new_height(topframe, h, FALSE, FALSE);
4444 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4445 #else
4446 if (h < 1)
4447 h = 1;
4448 win_new_height(firstwin, h);
4449 #endif
4450 compute_cmdrow();
4451 #ifdef FEAT_WINDOWS
4452 curtab->tp_ch_used = p_ch;
4453 #endif
4455 #if 0
4456 /* Disabled: don't want making the screen smaller make a window larger. */
4457 if (p_ea)
4458 win_equal(curwin, FALSE, 'v');
4459 #endif
4462 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4464 * Called from win_new_shellsize() after Columns changed.
4466 void
4467 shell_new_columns()
4469 if (firstwin == NULL) /* not initialized yet */
4470 return;
4472 /* First try setting the widths of windows with 'winfixwidth'. If that
4473 * doesn't result in the right width, forget about that option. */
4474 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4475 if (topframe->fr_width != Columns)
4476 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4478 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4479 #if 0
4480 /* Disabled: don't want making the screen smaller make a window larger. */
4481 if (p_ea)
4482 win_equal(curwin, FALSE, 'h');
4483 #endif
4485 #endif
4487 #if defined(FEAT_CMDWIN) || defined(PROTO)
4489 * Save the size of all windows in "gap".
4491 void
4492 win_size_save(gap)
4493 garray_T *gap;
4496 win_T *wp;
4498 ga_init2(gap, (int)sizeof(int), 1);
4499 if (ga_grow(gap, win_count() * 2) == OK)
4500 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4502 ((int *)gap->ga_data)[gap->ga_len++] =
4503 wp->w_width + wp->w_vsep_width;
4504 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4509 * Restore window sizes, but only if the number of windows is still the same.
4510 * Does not free the growarray.
4512 void
4513 win_size_restore(gap)
4514 garray_T *gap;
4516 win_T *wp;
4517 int i;
4519 if (win_count() * 2 == gap->ga_len)
4521 i = 0;
4522 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4524 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4525 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4527 /* recompute the window positions */
4528 (void)win_comp_pos();
4531 #endif /* FEAT_CMDWIN */
4533 #if defined(FEAT_WINDOWS) || defined(PROTO)
4535 * Update the position for all windows, using the width and height of the
4536 * frames.
4537 * Returns the row just after the last window.
4540 win_comp_pos()
4542 int row = tabline_height();
4543 int col = 0;
4545 frame_comp_pos(topframe, &row, &col);
4546 return row;
4550 * Update the position of the windows in frame "topfrp", using the width and
4551 * height of the frames.
4552 * "*row" and "*col" are the top-left position of the frame. They are updated
4553 * to the bottom-right position plus one.
4555 static void
4556 frame_comp_pos(topfrp, row, col)
4557 frame_T *topfrp;
4558 int *row;
4559 int *col;
4561 win_T *wp;
4562 frame_T *frp;
4563 #ifdef FEAT_VERTSPLIT
4564 int startcol;
4565 int startrow;
4566 #endif
4568 wp = topfrp->fr_win;
4569 if (wp != NULL)
4571 if (wp->w_winrow != *row
4572 #ifdef FEAT_VERTSPLIT
4573 || wp->w_wincol != *col
4574 #endif
4577 /* position changed, redraw */
4578 wp->w_winrow = *row;
4579 #ifdef FEAT_VERTSPLIT
4580 wp->w_wincol = *col;
4581 #endif
4582 redraw_win_later(wp, NOT_VALID);
4583 wp->w_redr_status = TRUE;
4585 *row += wp->w_height + wp->w_status_height;
4586 #ifdef FEAT_VERTSPLIT
4587 *col += wp->w_width + wp->w_vsep_width;
4588 #endif
4590 else
4592 #ifdef FEAT_VERTSPLIT
4593 startrow = *row;
4594 startcol = *col;
4595 #endif
4596 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4598 #ifdef FEAT_VERTSPLIT
4599 if (topfrp->fr_layout == FR_ROW)
4600 *row = startrow; /* all frames are at the same row */
4601 else
4602 *col = startcol; /* all frames are at the same col */
4603 #endif
4604 frame_comp_pos(frp, row, col);
4609 #endif /* FEAT_WINDOWS */
4612 * Set current window height and take care of repositioning other windows to
4613 * fit around it.
4615 void
4616 win_setheight(height)
4617 int height;
4619 win_setheight_win(height, curwin);
4623 * Set the window height of window "win" and take care of repositioning other
4624 * windows to fit around it.
4626 void
4627 win_setheight_win(height, win)
4628 int height;
4629 win_T *win;
4631 int row;
4633 if (win == curwin)
4635 /* Always keep current window at least one line high, even when
4636 * 'winminheight' is zero. */
4637 #ifdef FEAT_WINDOWS
4638 if (height < p_wmh)
4639 height = p_wmh;
4640 #endif
4641 if (height == 0)
4642 height = 1;
4645 #ifdef FEAT_WINDOWS
4646 frame_setheight(win->w_frame, height + win->w_status_height);
4648 /* recompute the window positions */
4649 row = win_comp_pos();
4650 #else
4651 if (height > topframe->fr_height)
4652 height = topframe->fr_height;
4653 win->w_height = height;
4654 row = height;
4655 #endif
4658 * If there is extra space created between the last window and the command
4659 * line, clear it.
4661 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4662 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4663 cmdline_row = row;
4664 msg_row = row;
4665 msg_col = 0;
4667 redraw_all_later(NOT_VALID);
4670 #if defined(FEAT_WINDOWS) || defined(PROTO)
4673 * Set the height of a frame to "height" and take care that all frames and
4674 * windows inside it are resized. Also resize frames on the left and right if
4675 * the are in the same FR_ROW frame.
4677 * Strategy:
4678 * If the frame is part of a FR_COL frame, try fitting the frame in that
4679 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4680 * go to containing frames to resize them and make room.
4681 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4682 * Check for the minimal height of the FR_ROW frame.
4683 * At the top level we can also use change the command line height.
4685 static void
4686 frame_setheight(curfrp, height)
4687 frame_T *curfrp;
4688 int height;
4690 int room; /* total number of lines available */
4691 int take; /* number of lines taken from other windows */
4692 int room_cmdline; /* lines available from cmdline */
4693 int run;
4694 frame_T *frp;
4695 int h;
4696 int room_reserved;
4698 /* If the height already is the desired value, nothing to do. */
4699 if (curfrp->fr_height == height)
4700 return;
4702 if (curfrp->fr_parent == NULL)
4704 /* topframe: can only change the command line */
4705 if (height > ROWS_AVAIL)
4706 height = ROWS_AVAIL;
4707 if (height > 0)
4708 frame_new_height(curfrp, height, FALSE, FALSE);
4710 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4712 /* Row of frames: Also need to resize frames left and right of this
4713 * one. First check for the minimal height of these. */
4714 h = frame_minheight(curfrp->fr_parent, NULL);
4715 if (height < h)
4716 height = h;
4717 frame_setheight(curfrp->fr_parent, height);
4719 else
4722 * Column of frames: try to change only frames in this column.
4724 #ifdef FEAT_VERTSPLIT
4726 * Do this twice:
4727 * 1: compute room available, if it's not enough try resizing the
4728 * containing frame.
4729 * 2: compute the room available and adjust the height to it.
4730 * Try not to reduce the height of a window with 'winfixheight' set.
4732 for (run = 1; run <= 2; ++run)
4733 #else
4734 for (;;)
4735 #endif
4737 room = 0;
4738 room_reserved = 0;
4739 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4740 frp = frp->fr_next)
4742 if (frp != curfrp
4743 && frp->fr_win != NULL
4744 && frp->fr_win->w_p_wfh)
4745 room_reserved += frp->fr_height;
4746 room += frp->fr_height;
4747 if (frp != curfrp)
4748 room -= frame_minheight(frp, NULL);
4750 #ifdef FEAT_VERTSPLIT
4751 if (curfrp->fr_width != Columns)
4752 room_cmdline = 0;
4753 else
4754 #endif
4756 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4757 + lastwin->w_height + lastwin->w_status_height);
4758 if (room_cmdline < 0)
4759 room_cmdline = 0;
4762 if (height <= room + room_cmdline)
4763 break;
4764 #ifdef FEAT_VERTSPLIT
4765 if (run == 2 || curfrp->fr_width == Columns)
4766 #endif
4768 if (height > room + room_cmdline)
4769 height = room + room_cmdline;
4770 break;
4772 #ifdef FEAT_VERTSPLIT
4773 frame_setheight(curfrp->fr_parent, height
4774 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4775 #endif
4776 /*NOTREACHED*/
4780 * Compute the number of lines we will take from others frames (can be
4781 * negative!).
4783 take = height - curfrp->fr_height;
4785 /* If there is not enough room, also reduce the height of a window
4786 * with 'winfixheight' set. */
4787 if (height > room + room_cmdline - room_reserved)
4788 room_reserved = room + room_cmdline - height;
4789 /* If there is only a 'winfixheight' window and making the
4790 * window smaller, need to make the other window taller. */
4791 if (take < 0 && room - curfrp->fr_height < room_reserved)
4792 room_reserved = 0;
4794 if (take > 0 && room_cmdline > 0)
4796 /* use lines from cmdline first */
4797 if (take < room_cmdline)
4798 room_cmdline = take;
4799 take -= room_cmdline;
4800 topframe->fr_height += room_cmdline;
4804 * set the current frame to the new height
4806 frame_new_height(curfrp, height, FALSE, FALSE);
4809 * First take lines from the frames after the current frame. If
4810 * that is not enough, takes lines from frames above the current
4811 * frame.
4813 for (run = 0; run < 2; ++run)
4815 if (run == 0)
4816 frp = curfrp->fr_next; /* 1st run: start with next window */
4817 else
4818 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4819 while (frp != NULL && take != 0)
4821 h = frame_minheight(frp, NULL);
4822 if (room_reserved > 0
4823 && frp->fr_win != NULL
4824 && frp->fr_win->w_p_wfh)
4826 if (room_reserved >= frp->fr_height)
4827 room_reserved -= frp->fr_height;
4828 else
4830 if (frp->fr_height - room_reserved > take)
4831 room_reserved = frp->fr_height - take;
4832 take -= frp->fr_height - room_reserved;
4833 frame_new_height(frp, room_reserved, FALSE, FALSE);
4834 room_reserved = 0;
4837 else
4839 if (frp->fr_height - take < h)
4841 take -= frp->fr_height - h;
4842 frame_new_height(frp, h, FALSE, FALSE);
4844 else
4846 frame_new_height(frp, frp->fr_height - take,
4847 FALSE, FALSE);
4848 take = 0;
4851 if (run == 0)
4852 frp = frp->fr_next;
4853 else
4854 frp = frp->fr_prev;
4860 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4862 * Set current window width and take care of repositioning other windows to
4863 * fit around it.
4865 void
4866 win_setwidth(width)
4867 int width;
4869 win_setwidth_win(width, curwin);
4872 void
4873 win_setwidth_win(width, wp)
4874 int width;
4875 win_T *wp;
4877 /* Always keep current window at least one column wide, even when
4878 * 'winminwidth' is zero. */
4879 if (wp == curwin)
4881 if (width < p_wmw)
4882 width = p_wmw;
4883 if (width == 0)
4884 width = 1;
4887 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4889 /* recompute the window positions */
4890 (void)win_comp_pos();
4892 redraw_all_later(NOT_VALID);
4896 * Set the width of a frame to "width" and take care that all frames and
4897 * windows inside it are resized. Also resize frames above and below if the
4898 * are in the same FR_ROW frame.
4900 * Strategy is similar to frame_setheight().
4902 static void
4903 frame_setwidth(curfrp, width)
4904 frame_T *curfrp;
4905 int width;
4907 int room; /* total number of lines available */
4908 int take; /* number of lines taken from other windows */
4909 int run;
4910 frame_T *frp;
4911 int w;
4912 int room_reserved;
4914 /* If the width already is the desired value, nothing to do. */
4915 if (curfrp->fr_width == width)
4916 return;
4918 if (curfrp->fr_parent == NULL)
4919 /* topframe: can't change width */
4920 return;
4922 if (curfrp->fr_parent->fr_layout == FR_COL)
4924 /* Column of frames: Also need to resize frames above and below of
4925 * this one. First check for the minimal width of these. */
4926 w = frame_minwidth(curfrp->fr_parent, NULL);
4927 if (width < w)
4928 width = w;
4929 frame_setwidth(curfrp->fr_parent, width);
4931 else
4934 * Row of frames: try to change only frames in this row.
4936 * Do this twice:
4937 * 1: compute room available, if it's not enough try resizing the
4938 * containing frame.
4939 * 2: compute the room available and adjust the width to it.
4941 for (run = 1; run <= 2; ++run)
4943 room = 0;
4944 room_reserved = 0;
4945 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4946 frp = frp->fr_next)
4948 if (frp != curfrp
4949 && frp->fr_win != NULL
4950 && frp->fr_win->w_p_wfw)
4951 room_reserved += frp->fr_width;
4952 room += frp->fr_width;
4953 if (frp != curfrp)
4954 room -= frame_minwidth(frp, NULL);
4957 if (width <= room)
4958 break;
4959 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4961 if (width > room)
4962 width = room;
4963 break;
4965 frame_setwidth(curfrp->fr_parent, width
4966 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4970 * Compute the number of lines we will take from others frames (can be
4971 * negative!).
4973 take = width - curfrp->fr_width;
4975 /* If there is not enough room, also reduce the width of a window
4976 * with 'winfixwidth' set. */
4977 if (width > room - room_reserved)
4978 room_reserved = room - width;
4979 /* If there is only a 'winfixwidth' window and making the
4980 * window smaller, need to make the other window narrower. */
4981 if (take < 0 && room - curfrp->fr_width < room_reserved)
4982 room_reserved = 0;
4985 * set the current frame to the new width
4987 frame_new_width(curfrp, width, FALSE, FALSE);
4990 * First take lines from the frames right of the current frame. If
4991 * that is not enough, takes lines from frames left of the current
4992 * frame.
4994 for (run = 0; run < 2; ++run)
4996 if (run == 0)
4997 frp = curfrp->fr_next; /* 1st run: start with next window */
4998 else
4999 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5000 while (frp != NULL && take != 0)
5002 w = frame_minwidth(frp, NULL);
5003 if (room_reserved > 0
5004 && frp->fr_win != NULL
5005 && frp->fr_win->w_p_wfw)
5007 if (room_reserved >= frp->fr_width)
5008 room_reserved -= frp->fr_width;
5009 else
5011 if (frp->fr_width - room_reserved > take)
5012 room_reserved = frp->fr_width - take;
5013 take -= frp->fr_width - room_reserved;
5014 frame_new_width(frp, room_reserved, FALSE, FALSE);
5015 room_reserved = 0;
5018 else
5020 if (frp->fr_width - take < w)
5022 take -= frp->fr_width - w;
5023 frame_new_width(frp, w, FALSE, FALSE);
5025 else
5027 frame_new_width(frp, frp->fr_width - take,
5028 FALSE, FALSE);
5029 take = 0;
5032 if (run == 0)
5033 frp = frp->fr_next;
5034 else
5035 frp = frp->fr_prev;
5040 #endif /* FEAT_VERTSPLIT */
5043 * Check 'winminheight' for a valid value.
5045 void
5046 win_setminheight()
5048 int room;
5049 int first = TRUE;
5050 win_T *wp;
5052 /* loop until there is a 'winminheight' that is possible */
5053 while (p_wmh > 0)
5055 /* TODO: handle vertical splits */
5056 room = -p_wh;
5057 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5058 room += wp->w_height - p_wmh;
5059 if (room >= 0)
5060 break;
5061 --p_wmh;
5062 if (first)
5064 EMSG(_(e_noroom));
5065 first = FALSE;
5070 #ifdef FEAT_MOUSE
5073 * Status line of dragwin is dragged "offset" lines down (negative is up).
5075 void
5076 win_drag_status_line(dragwin, offset)
5077 win_T *dragwin;
5078 int offset;
5080 frame_T *curfr;
5081 frame_T *fr;
5082 int room;
5083 int row;
5084 int up; /* if TRUE, drag status line up, otherwise down */
5085 int n;
5087 fr = dragwin->w_frame;
5088 curfr = fr;
5089 if (fr != topframe) /* more than one window */
5091 fr = fr->fr_parent;
5092 /* When the parent frame is not a column of frames, its parent should
5093 * be. */
5094 if (fr->fr_layout != FR_COL)
5096 curfr = fr;
5097 if (fr != topframe) /* only a row of windows, may drag statusline */
5098 fr = fr->fr_parent;
5102 /* If this is the last frame in a column, may want to resize the parent
5103 * frame instead (go two up to skip a row of frames). */
5104 while (curfr != topframe && curfr->fr_next == NULL)
5106 if (fr != topframe)
5107 fr = fr->fr_parent;
5108 curfr = fr;
5109 if (fr != topframe)
5110 fr = fr->fr_parent;
5113 if (offset < 0) /* drag up */
5115 up = TRUE;
5116 offset = -offset;
5117 /* sum up the room of the current frame and above it */
5118 if (fr == curfr)
5120 /* only one window */
5121 room = fr->fr_height - frame_minheight(fr, NULL);
5123 else
5125 room = 0;
5126 for (fr = fr->fr_child; ; fr = fr->fr_next)
5128 room += fr->fr_height - frame_minheight(fr, NULL);
5129 if (fr == curfr)
5130 break;
5133 fr = curfr->fr_next; /* put fr at frame that grows */
5135 else /* drag down */
5137 up = FALSE;
5139 * Only dragging the last status line can reduce p_ch.
5141 room = Rows - cmdline_row;
5142 if (curfr->fr_next == NULL)
5143 room -= 1;
5144 else
5145 room -= p_ch;
5146 if (room < 0)
5147 room = 0;
5148 /* sum up the room of frames below of the current one */
5149 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5150 room += fr->fr_height - frame_minheight(fr, NULL);
5151 fr = curfr; /* put fr at window that grows */
5154 if (room < offset) /* Not enough room */
5155 offset = room; /* Move as far as we can */
5156 if (offset <= 0)
5157 return;
5160 * Grow frame fr by "offset" lines.
5161 * Doesn't happen when dragging the last status line up.
5163 if (fr != NULL)
5164 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5166 if (up)
5167 fr = curfr; /* current frame gets smaller */
5168 else
5169 fr = curfr->fr_next; /* next frame gets smaller */
5172 * Now make the other frames smaller.
5174 while (fr != NULL && offset > 0)
5176 n = frame_minheight(fr, NULL);
5177 if (fr->fr_height - offset <= n)
5179 offset -= fr->fr_height - n;
5180 frame_new_height(fr, n, !up, FALSE);
5182 else
5184 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5185 break;
5187 if (up)
5188 fr = fr->fr_prev;
5189 else
5190 fr = fr->fr_next;
5192 row = win_comp_pos();
5193 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5194 cmdline_row = row;
5195 p_ch = Rows - cmdline_row;
5196 if (p_ch < 1)
5197 p_ch = 1;
5198 curtab->tp_ch_used = p_ch;
5199 redraw_all_later(SOME_VALID);
5200 showmode();
5203 #ifdef FEAT_VERTSPLIT
5205 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5207 void
5208 win_drag_vsep_line(dragwin, offset)
5209 win_T *dragwin;
5210 int offset;
5212 frame_T *curfr;
5213 frame_T *fr;
5214 int room;
5215 int left; /* if TRUE, drag separator line left, otherwise right */
5216 int n;
5218 fr = dragwin->w_frame;
5219 if (fr == topframe) /* only one window (cannot happen?) */
5220 return;
5221 curfr = fr;
5222 fr = fr->fr_parent;
5223 /* When the parent frame is not a row of frames, its parent should be. */
5224 if (fr->fr_layout != FR_ROW)
5226 if (fr == topframe) /* only a column of windows (cannot happen?) */
5227 return;
5228 curfr = fr;
5229 fr = fr->fr_parent;
5232 /* If this is the last frame in a row, may want to resize a parent
5233 * frame instead. */
5234 while (curfr->fr_next == NULL)
5236 if (fr == topframe)
5237 break;
5238 curfr = fr;
5239 fr = fr->fr_parent;
5240 if (fr != topframe)
5242 curfr = fr;
5243 fr = fr->fr_parent;
5247 if (offset < 0) /* drag left */
5249 left = TRUE;
5250 offset = -offset;
5251 /* sum up the room of the current frame and left of it */
5252 room = 0;
5253 for (fr = fr->fr_child; ; fr = fr->fr_next)
5255 room += fr->fr_width - frame_minwidth(fr, NULL);
5256 if (fr == curfr)
5257 break;
5259 fr = curfr->fr_next; /* put fr at frame that grows */
5261 else /* drag right */
5263 left = FALSE;
5264 /* sum up the room of frames right of the current one */
5265 room = 0;
5266 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5267 room += fr->fr_width - frame_minwidth(fr, NULL);
5268 fr = curfr; /* put fr at window that grows */
5271 if (room < offset) /* Not enough room */
5272 offset = room; /* Move as far as we can */
5273 if (offset <= 0) /* No room at all, quit. */
5274 return;
5276 /* grow frame fr by offset lines */
5277 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5279 /* shrink other frames: current and at the left or at the right */
5280 if (left)
5281 fr = curfr; /* current frame gets smaller */
5282 else
5283 fr = curfr->fr_next; /* next frame gets smaller */
5285 while (fr != NULL && offset > 0)
5287 n = frame_minwidth(fr, NULL);
5288 if (fr->fr_width - offset <= n)
5290 offset -= fr->fr_width - n;
5291 frame_new_width(fr, n, !left, FALSE);
5293 else
5295 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5296 break;
5298 if (left)
5299 fr = fr->fr_prev;
5300 else
5301 fr = fr->fr_next;
5303 (void)win_comp_pos();
5304 redraw_all_later(NOT_VALID);
5306 #endif /* FEAT_VERTSPLIT */
5307 #endif /* FEAT_MOUSE */
5309 #endif /* FEAT_WINDOWS */
5312 * Set the height of a window.
5313 * This takes care of the things inside the window, not what happens to the
5314 * window position, the frame or to other windows.
5316 static void
5317 win_new_height(wp, height)
5318 win_T *wp;
5319 int height;
5321 linenr_T lnum;
5322 int sline, line_size;
5323 #define FRACTION_MULT 16384L
5325 /* Don't want a negative height. Happens when splitting a tiny window.
5326 * Will equalize heights soon to fix it. */
5327 if (height < 0)
5328 height = 0;
5329 if (wp->w_height == height)
5330 return; /* nothing to do */
5332 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5333 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5334 + FRACTION_MULT / 2) / (long)wp->w_height;
5336 wp->w_height = height;
5337 wp->w_skipcol = 0;
5339 /* Don't change w_topline when height is zero. Don't set w_topline when
5340 * 'scrollbind' is set and this isn't the current window. */
5341 if (height > 0
5342 #ifdef FEAT_SCROLLBIND
5343 && (!wp->w_p_scb || wp == curwin)
5344 #endif
5348 * Find a value for w_topline that shows the cursor at the same
5349 * relative position in the window as before (more or less).
5351 lnum = wp->w_cursor.lnum;
5352 if (lnum < 1) /* can happen when starting up */
5353 lnum = 1;
5354 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5355 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5356 sline = wp->w_wrow - line_size;
5358 if (sline >= 0)
5360 /* Make sure the whole cursor line is visible, if possible. */
5361 int rows = plines_win(wp, lnum, FALSE);
5363 if (sline > wp->w_height - rows)
5365 sline = wp->w_height - rows;
5366 wp->w_wrow -= rows - line_size;
5370 if (sline < 0)
5373 * Cursor line would go off top of screen if w_wrow was this high.
5374 * Make cursor line the first line in the window. If not enough
5375 * room use w_skipcol;
5377 wp->w_wrow = line_size;
5378 if (wp->w_wrow >= wp->w_height
5379 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5381 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5382 --wp->w_wrow;
5383 while (wp->w_wrow >= wp->w_height)
5385 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5386 + win_col_off2(wp);
5387 --wp->w_wrow;
5391 else
5393 while (sline > 0 && lnum > 1)
5395 #ifdef FEAT_FOLDING
5396 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5397 if (lnum == 1)
5399 /* first line in buffer is folded */
5400 line_size = 1;
5401 --sline;
5402 break;
5404 #endif
5405 --lnum;
5406 #ifdef FEAT_DIFF
5407 if (lnum == wp->w_topline)
5408 line_size = plines_win_nofill(wp, lnum, TRUE)
5409 + wp->w_topfill;
5410 else
5411 #endif
5412 line_size = plines_win(wp, lnum, TRUE);
5413 sline -= line_size;
5416 if (sline < 0)
5419 * Line we want at top would go off top of screen. Use next
5420 * line instead.
5422 #ifdef FEAT_FOLDING
5423 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5424 #endif
5425 lnum++;
5426 wp->w_wrow -= line_size + sline;
5428 else if (sline > 0)
5430 /* First line of file reached, use that as topline. */
5431 lnum = 1;
5432 wp->w_wrow -= sline;
5435 set_topline(wp, lnum);
5438 if (wp == curwin)
5440 if (p_so)
5441 update_topline();
5442 curs_columns(FALSE); /* validate w_wrow */
5444 wp->w_prev_fraction_row = wp->w_wrow;
5446 win_comp_scroll(wp);
5447 redraw_win_later(wp, SOME_VALID);
5448 #ifdef FEAT_WINDOWS
5449 wp->w_redr_status = TRUE;
5450 #endif
5451 invalidate_botline_win(wp);
5454 #ifdef FEAT_VERTSPLIT
5456 * Set the width of a window.
5458 static void
5459 win_new_width(wp, width)
5460 win_T *wp;
5461 int width;
5463 wp->w_width = width;
5464 wp->w_lines_valid = 0;
5465 changed_line_abv_curs_win(wp);
5466 invalidate_botline_win(wp);
5467 if (wp == curwin)
5469 update_topline();
5470 curs_columns(TRUE); /* validate w_wrow */
5472 redraw_win_later(wp, NOT_VALID);
5473 wp->w_redr_status = TRUE;
5475 #endif
5477 void
5478 win_comp_scroll(wp)
5479 win_T *wp;
5481 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5482 if (wp->w_p_scr == 0)
5483 wp->w_p_scr = 1;
5487 * command_height: called whenever p_ch has been changed
5489 void
5490 command_height()
5492 #ifdef FEAT_WINDOWS
5493 int h;
5494 frame_T *frp;
5495 int old_p_ch = curtab->tp_ch_used;
5497 /* Use the value of p_ch that we remembered. This is needed for when the
5498 * GUI starts up, we can't be sure in what order things happen. And when
5499 * p_ch was changed in another tab page. */
5500 curtab->tp_ch_used = p_ch;
5502 /* Find bottom frame with width of screen. */
5503 frp = lastwin->w_frame;
5504 # ifdef FEAT_VERTSPLIT
5505 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5506 frp = frp->fr_parent;
5507 # endif
5509 /* Avoid changing the height of a window with 'winfixheight' set. */
5510 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5511 && frp->fr_win->w_p_wfh)
5512 frp = frp->fr_prev;
5514 if (starting != NO_SCREEN)
5516 cmdline_row = Rows - p_ch;
5518 if (p_ch > old_p_ch) /* p_ch got bigger */
5520 while (p_ch > old_p_ch)
5522 if (frp == NULL)
5524 EMSG(_(e_noroom));
5525 p_ch = old_p_ch;
5526 curtab->tp_ch_used = p_ch;
5527 cmdline_row = Rows - p_ch;
5528 break;
5530 h = frp->fr_height - frame_minheight(frp, NULL);
5531 if (h > p_ch - old_p_ch)
5532 h = p_ch - old_p_ch;
5533 old_p_ch += h;
5534 frame_add_height(frp, -h);
5535 frp = frp->fr_prev;
5538 /* Recompute window positions. */
5539 (void)win_comp_pos();
5541 /* clear the lines added to cmdline */
5542 if (full_screen)
5543 screen_fill((int)(cmdline_row), (int)Rows, 0,
5544 (int)Columns, ' ', ' ', 0);
5545 msg_row = cmdline_row;
5546 redraw_cmdline = TRUE;
5547 return;
5550 if (msg_row < cmdline_row)
5551 msg_row = cmdline_row;
5552 redraw_cmdline = TRUE;
5554 frame_add_height(frp, (int)(old_p_ch - p_ch));
5556 /* Recompute window positions. */
5557 if (frp != lastwin->w_frame)
5558 (void)win_comp_pos();
5559 #else
5560 cmdline_row = Rows - p_ch;
5561 win_setheight(cmdline_row);
5562 #endif
5565 #if defined(FEAT_WINDOWS) || defined(PROTO)
5567 * Resize frame "frp" to be "n" lines higher (negative for less high).
5568 * Also resize the frames it is contained in.
5570 static void
5571 frame_add_height(frp, n)
5572 frame_T *frp;
5573 int n;
5575 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5576 for (;;)
5578 frp = frp->fr_parent;
5579 if (frp == NULL)
5580 break;
5581 frp->fr_height += n;
5586 * Add or remove a status line for the bottom window(s), according to the
5587 * value of 'laststatus'.
5589 void
5590 last_status(morewin)
5591 int morewin; /* pretend there are two or more windows */
5593 /* Don't make a difference between horizontal or vertical split. */
5594 last_status_rec(topframe, (p_ls == 2
5595 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5598 static void
5599 last_status_rec(fr, statusline)
5600 frame_T *fr;
5601 int statusline;
5603 frame_T *fp;
5604 win_T *wp;
5606 if (fr->fr_layout == FR_LEAF)
5608 wp = fr->fr_win;
5609 if (wp->w_status_height != 0 && !statusline)
5611 /* remove status line */
5612 win_new_height(wp, wp->w_height + 1);
5613 wp->w_status_height = 0;
5614 comp_col();
5616 else if (wp->w_status_height == 0 && statusline)
5618 /* Find a frame to take a line from. */
5619 fp = fr;
5620 while (fp->fr_height <= frame_minheight(fp, NULL))
5622 if (fp == topframe)
5624 EMSG(_(e_noroom));
5625 return;
5627 /* In a column of frames: go to frame above. If already at
5628 * the top or in a row of frames: go to parent. */
5629 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5630 fp = fp->fr_prev;
5631 else
5632 fp = fp->fr_parent;
5634 wp->w_status_height = 1;
5635 if (fp != fr)
5637 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5638 frame_fix_height(wp);
5639 (void)win_comp_pos();
5641 else
5642 win_new_height(wp, wp->w_height - 1);
5643 comp_col();
5644 redraw_all_later(SOME_VALID);
5647 #ifdef FEAT_VERTSPLIT
5648 else if (fr->fr_layout == FR_ROW)
5650 /* vertically split windows, set status line for each one */
5651 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5652 last_status_rec(fp, statusline);
5654 #endif
5655 else
5657 /* horizontally split window, set status line for last one */
5658 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5660 last_status_rec(fp, statusline);
5665 * Return the number of lines used by the tab page line.
5668 tabline_height()
5670 #ifdef FEAT_GUI_TABLINE
5671 /* When the GUI has the tabline then this always returns zero. */
5672 if (gui_use_tabline())
5673 return 0;
5674 #endif
5675 switch (p_stal)
5677 case 0: return 0;
5678 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5680 return 1;
5683 #endif /* FEAT_WINDOWS */
5685 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5687 * Get the file name at the cursor.
5688 * If Visual mode is active, use the selected text if it's in one line.
5689 * Returns the name in allocated memory, NULL for failure.
5691 char_u *
5692 grab_file_name(count, file_lnum)
5693 long count;
5694 linenr_T *file_lnum;
5696 # ifdef FEAT_VISUAL
5697 if (VIsual_active)
5699 int len;
5700 char_u *ptr;
5702 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5703 return NULL;
5704 return find_file_name_in_path(ptr, len,
5705 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5707 # endif
5708 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5709 file_lnum);
5714 * Return the file name under or after the cursor.
5716 * The 'path' option is searched if the file name is not absolute.
5717 * The string returned has been alloc'ed and should be freed by the caller.
5718 * NULL is returned if the file name or file is not found.
5720 * options:
5721 * FNAME_MESS give error messages
5722 * FNAME_EXP expand to path
5723 * FNAME_HYP check for hypertext link
5724 * FNAME_INCL apply "includeexpr"
5726 char_u *
5727 file_name_at_cursor(options, count, file_lnum)
5728 int options;
5729 long count;
5730 linenr_T *file_lnum;
5732 return file_name_in_line(ml_get_curline(),
5733 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5734 file_lnum);
5738 * Return the name of the file under or after ptr[col].
5739 * Otherwise like file_name_at_cursor().
5741 char_u *
5742 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5743 char_u *line;
5744 int col;
5745 int options;
5746 long count;
5747 char_u *rel_fname; /* file we are searching relative to */
5748 linenr_T *file_lnum; /* line number after the file name */
5750 char_u *ptr;
5751 int len;
5754 * search forward for what could be the start of a file name
5756 ptr = line + col;
5757 while (*ptr != NUL && !vim_isfilec(*ptr))
5758 mb_ptr_adv(ptr);
5759 if (*ptr == NUL) /* nothing found */
5761 if (options & FNAME_MESS)
5762 EMSG(_("E446: No file name under cursor"));
5763 return NULL;
5767 * Search backward for first char of the file name.
5768 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5770 while (ptr > line)
5772 #ifdef FEAT_MBYTE
5773 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5774 ptr -= len + 1;
5775 else
5776 #endif
5777 if (vim_isfilec(ptr[-1])
5778 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5779 --ptr;
5780 else
5781 break;
5785 * Search forward for the last char of the file name.
5786 * Also allow "://" when ':' is not in 'isfname'.
5788 len = 0;
5789 while (vim_isfilec(ptr[len])
5790 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5791 #ifdef FEAT_MBYTE
5792 if (has_mbyte)
5793 len += (*mb_ptr2len)(ptr + len);
5794 else
5795 #endif
5796 ++len;
5799 * If there is trailing punctuation, remove it.
5800 * But don't remove "..", could be a directory name.
5802 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5803 && ptr[len - 2] != '.')
5804 --len;
5806 if (file_lnum != NULL)
5808 char_u *p;
5810 /* Get the number after the file name and a separator character */
5811 p = ptr + len;
5812 p = skipwhite(p);
5813 if (*p != NUL)
5815 if (!isdigit(*p))
5816 ++p; /* skip the separator */
5817 p = skipwhite(p);
5818 if (isdigit(*p))
5819 *file_lnum = (int)getdigits(&p);
5823 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5826 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5827 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5829 static char_u *
5830 eval_includeexpr(ptr, len)
5831 char_u *ptr;
5832 int len;
5834 char_u *res;
5836 set_vim_var_string(VV_FNAME, ptr, len);
5837 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5838 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5839 set_vim_var_string(VV_FNAME, NULL, 0);
5840 return res;
5842 #endif
5845 * Return the name of the file ptr[len] in 'path'.
5846 * Otherwise like file_name_at_cursor().
5848 char_u *
5849 find_file_name_in_path(ptr, len, options, count, rel_fname)
5850 char_u *ptr;
5851 int len;
5852 int options;
5853 long count;
5854 char_u *rel_fname; /* file we are searching relative to */
5856 char_u *file_name;
5857 int c;
5858 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5859 char_u *tofree = NULL;
5861 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5863 tofree = eval_includeexpr(ptr, len);
5864 if (tofree != NULL)
5866 ptr = tofree;
5867 len = (int)STRLEN(ptr);
5870 # endif
5872 if (options & FNAME_EXP)
5874 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5875 TRUE, rel_fname);
5877 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5879 * If the file could not be found in a normal way, try applying
5880 * 'includeexpr' (unless done already).
5882 if (file_name == NULL
5883 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5885 tofree = eval_includeexpr(ptr, len);
5886 if (tofree != NULL)
5888 ptr = tofree;
5889 len = (int)STRLEN(ptr);
5890 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5891 TRUE, rel_fname);
5894 # endif
5895 if (file_name == NULL && (options & FNAME_MESS))
5897 c = ptr[len];
5898 ptr[len] = NUL;
5899 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5900 ptr[len] = c;
5903 /* Repeat finding the file "count" times. This matters when it
5904 * appears several times in the path. */
5905 while (file_name != NULL && --count > 0)
5907 vim_free(file_name);
5908 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5911 else
5912 file_name = vim_strnsave(ptr, len);
5914 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5915 vim_free(tofree);
5916 # endif
5918 return file_name;
5920 #endif /* FEAT_SEARCHPATH */
5923 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5924 * Also check for ":\\", which MS Internet Explorer accepts, return
5925 * URL_BACKSLASH.
5927 static int
5928 path_is_url(p)
5929 char_u *p;
5931 if (STRNCMP(p, "://", (size_t)3) == 0)
5932 return URL_SLASH;
5933 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5934 return URL_BACKSLASH;
5935 return 0;
5939 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5940 * Return URL_BACKSLASH for "name:\\".
5941 * Return zero otherwise.
5944 path_with_url(fname)
5945 char_u *fname;
5947 char_u *p;
5949 for (p = fname; isalpha(*p); ++p)
5951 return path_is_url(p);
5955 * Return TRUE if "name" is a full (absolute) path name or URL.
5958 vim_isAbsName(name)
5959 char_u *name;
5961 return (path_with_url(name) != 0 || mch_isFullName(name));
5965 * Get absolute file name into buffer "buf[len]".
5967 * return FAIL for failure, OK otherwise
5970 vim_FullName(fname, buf, len, force)
5971 char_u *fname, *buf;
5972 int len;
5973 int force; /* force expansion even when already absolute */
5975 int retval = OK;
5976 int url;
5978 *buf = NUL;
5979 if (fname == NULL)
5980 return FAIL;
5982 url = path_with_url(fname);
5983 if (!url)
5984 retval = mch_FullName(fname, buf, len, force);
5985 if (url || retval == FAIL)
5987 /* something failed; use the file name (truncate when too long) */
5988 vim_strncpy(buf, fname, len - 1);
5990 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5991 slash_adjust(buf);
5992 #endif
5993 return retval;
5997 * Return the minimal number of rows that is needed on the screen to display
5998 * the current number of windows.
6001 min_rows()
6003 int total;
6004 #ifdef FEAT_WINDOWS
6005 tabpage_T *tp;
6006 int n;
6007 #endif
6009 if (firstwin == NULL) /* not initialized yet */
6010 return MIN_LINES;
6012 #ifdef FEAT_WINDOWS
6013 total = 0;
6014 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6016 n = frame_minheight(tp->tp_topframe, NULL);
6017 if (total < n)
6018 total = n;
6020 total += tabline_height();
6021 #else
6022 total = 1; /* at least one window should have a line! */
6023 #endif
6024 total += 1; /* count the room for the command line */
6025 return total;
6029 * Return TRUE if there is only one window (in the current tab page), not
6030 * counting a help or preview window, unless it is the current window.
6033 only_one_window()
6035 #ifdef FEAT_WINDOWS
6036 int count = 0;
6037 win_T *wp;
6039 /* If there is another tab page there always is another window. */
6040 if (first_tabpage->tp_next != NULL)
6041 return FALSE;
6043 for (wp = firstwin; wp != NULL; wp = wp->w_next)
6044 if (!((wp->w_buffer->b_help && !curbuf->b_help)
6045 # ifdef FEAT_QUICKFIX
6046 || wp->w_p_pvw
6047 # endif
6048 ) || wp == curwin)
6049 ++count;
6050 return (count <= 1);
6051 #else
6052 return TRUE;
6053 #endif
6056 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6058 * Correct the cursor line number in other windows. Used after changing the
6059 * current buffer, and before applying autocommands.
6060 * When "do_curwin" is TRUE, also check current window.
6062 void
6063 check_lnums(do_curwin)
6064 int do_curwin;
6066 win_T *wp;
6068 #ifdef FEAT_WINDOWS
6069 tabpage_T *tp;
6071 FOR_ALL_TAB_WINDOWS(tp, wp)
6072 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6073 #else
6074 wp = curwin;
6075 if (do_curwin)
6076 #endif
6078 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6079 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6080 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6081 wp->w_topline = curbuf->b_ml.ml_line_count;
6084 #endif
6086 #if defined(FEAT_WINDOWS) || defined(PROTO)
6089 * A snapshot of the window sizes, to restore them after closing the help
6090 * window.
6091 * Only these fields are used:
6092 * fr_layout
6093 * fr_width
6094 * fr_height
6095 * fr_next
6096 * fr_child
6097 * fr_win (only valid for the old curwin, NULL otherwise)
6101 * Create a snapshot of the current frame sizes.
6103 static void
6104 make_snapshot()
6106 clear_snapshot(curtab);
6107 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6110 static void
6111 make_snapshot_rec(fr, frp)
6112 frame_T *fr;
6113 frame_T **frp;
6115 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6116 if (*frp == NULL)
6117 return;
6118 (*frp)->fr_layout = fr->fr_layout;
6119 # ifdef FEAT_VERTSPLIT
6120 (*frp)->fr_width = fr->fr_width;
6121 # endif
6122 (*frp)->fr_height = fr->fr_height;
6123 if (fr->fr_next != NULL)
6124 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6125 if (fr->fr_child != NULL)
6126 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6127 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6128 (*frp)->fr_win = curwin;
6132 * Remove any existing snapshot.
6134 static void
6135 clear_snapshot(tp)
6136 tabpage_T *tp;
6138 clear_snapshot_rec(tp->tp_snapshot);
6139 tp->tp_snapshot = NULL;
6142 static void
6143 clear_snapshot_rec(fr)
6144 frame_T *fr;
6146 if (fr != NULL)
6148 clear_snapshot_rec(fr->fr_next);
6149 clear_snapshot_rec(fr->fr_child);
6150 vim_free(fr);
6155 * Restore a previously created snapshot, if there is any.
6156 * This is only done if the screen size didn't change and the window layout is
6157 * still the same.
6159 static void
6160 restore_snapshot(close_curwin)
6161 int close_curwin; /* closing current window */
6163 win_T *wp;
6165 if (curtab->tp_snapshot != NULL
6166 # ifdef FEAT_VERTSPLIT
6167 && curtab->tp_snapshot->fr_width == topframe->fr_width
6168 # endif
6169 && curtab->tp_snapshot->fr_height == topframe->fr_height
6170 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6172 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6173 win_comp_pos();
6174 if (wp != NULL && close_curwin)
6175 win_goto(wp);
6176 redraw_all_later(CLEAR);
6178 clear_snapshot(curtab);
6182 * Check if frames "sn" and "fr" have the same layout, same following frames
6183 * and same children.
6185 static int
6186 check_snapshot_rec(sn, fr)
6187 frame_T *sn;
6188 frame_T *fr;
6190 if (sn->fr_layout != fr->fr_layout
6191 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6192 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6193 || (sn->fr_next != NULL
6194 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6195 || (sn->fr_child != NULL
6196 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6197 return FAIL;
6198 return OK;
6202 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6203 * following frames and children.
6204 * Returns a pointer to the old current window, or NULL.
6206 static win_T *
6207 restore_snapshot_rec(sn, fr)
6208 frame_T *sn;
6209 frame_T *fr;
6211 win_T *wp = NULL;
6212 win_T *wp2;
6214 fr->fr_height = sn->fr_height;
6215 # ifdef FEAT_VERTSPLIT
6216 fr->fr_width = sn->fr_width;
6217 # endif
6218 if (fr->fr_layout == FR_LEAF)
6220 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6221 # ifdef FEAT_VERTSPLIT
6222 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6223 # endif
6224 wp = sn->fr_win;
6226 if (sn->fr_next != NULL)
6228 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6229 if (wp2 != NULL)
6230 wp = wp2;
6232 if (sn->fr_child != NULL)
6234 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6235 if (wp2 != NULL)
6236 wp = wp2;
6238 return wp;
6241 #endif
6243 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6245 * Return TRUE if there is any vertically split window.
6248 win_hasvertsplit()
6250 frame_T *fr;
6252 if (topframe->fr_layout == FR_ROW)
6253 return TRUE;
6255 if (topframe->fr_layout == FR_COL)
6256 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6257 if (fr->fr_layout == FR_ROW)
6258 return TRUE;
6260 return FALSE;
6262 #endif
6264 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6266 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6267 * highligted with the group 'grp' with priority 'prio'.
6268 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6269 * If no particular ID is desired, -1 must be specified for 'id'.
6270 * Return ID of added match, -1 on failure.
6273 match_add(wp, grp, pat, prio, id)
6274 win_T *wp;
6275 char_u *grp;
6276 char_u *pat;
6277 int prio;
6278 int id;
6280 matchitem_T *cur;
6281 matchitem_T *prev;
6282 matchitem_T *m;
6283 int hlg_id;
6284 regprog_T *regprog;
6286 if (*grp == NUL || *pat == NUL)
6287 return -1;
6288 if (id < -1 || id == 0)
6290 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6291 return -1;
6293 if (id != -1)
6295 cur = wp->w_match_head;
6296 while (cur != NULL)
6298 if (cur->id == id)
6300 EMSGN("E801: ID already taken: %ld", id);
6301 return -1;
6303 cur = cur->next;
6306 if ((hlg_id = syn_namen2id(grp, STRLEN(grp))) == 0)
6308 EMSG2(_(e_nogroup), grp);
6309 return -1;
6311 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6313 EMSG2(_(e_invarg2), pat);
6314 return -1;
6317 /* Find available match ID. */
6318 while (id == -1)
6320 cur = wp->w_match_head;
6321 while (cur != NULL && cur->id != wp->w_next_match_id)
6322 cur = cur->next;
6323 if (cur == NULL)
6324 id = wp->w_next_match_id;
6325 wp->w_next_match_id++;
6328 /* Build new match. */
6329 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6330 m->id = id;
6331 m->priority = prio;
6332 m->pattern = vim_strsave(pat);
6333 m->hlg_id = hlg_id;
6334 m->match.regprog = regprog;
6335 m->match.rmm_ic = FALSE;
6336 m->match.rmm_maxcol = 0;
6338 /* Insert new match. The match list is in ascending order with regard to
6339 * the match priorities. */
6340 cur = wp->w_match_head;
6341 prev = cur;
6342 while (cur != NULL && prio >= cur->priority)
6344 prev = cur;
6345 cur = cur->next;
6347 if (cur == prev)
6348 wp->w_match_head = m;
6349 else
6350 prev->next = m;
6351 m->next = cur;
6353 redraw_later(SOME_VALID);
6354 return id;
6358 * Delete match with ID 'id' in the match list of window 'wp'.
6359 * Print error messages if 'perr' is TRUE.
6362 match_delete(wp, id, perr)
6363 win_T *wp;
6364 int id;
6365 int perr;
6367 matchitem_T *cur = wp->w_match_head;
6368 matchitem_T *prev = cur;
6370 if (id < 1)
6372 if (perr == TRUE)
6373 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6374 id);
6375 return -1;
6377 while (cur != NULL && cur->id != id)
6379 prev = cur;
6380 cur = cur->next;
6382 if (cur == NULL)
6384 if (perr == TRUE)
6385 EMSGN("E803: ID not found: %ld", id);
6386 return -1;
6388 if (cur == prev)
6389 wp->w_match_head = cur->next;
6390 else
6391 prev->next = cur->next;
6392 vim_free(cur->match.regprog);
6393 vim_free(cur->pattern);
6394 vim_free(cur);
6395 redraw_later(SOME_VALID);
6396 return 0;
6400 * Delete all matches in the match list of window 'wp'.
6402 void
6403 clear_matches(wp)
6404 win_T *wp;
6406 matchitem_T *m;
6408 while (wp->w_match_head != NULL)
6410 m = wp->w_match_head->next;
6411 vim_free(wp->w_match_head->match.regprog);
6412 vim_free(wp->w_match_head->pattern);
6413 vim_free(wp->w_match_head);
6414 wp->w_match_head = m;
6416 redraw_later(SOME_VALID);
6420 * Get match from ID 'id' in window 'wp'.
6421 * Return NULL if match not found.
6423 matchitem_T *
6424 get_match(wp, id)
6425 win_T *wp;
6426 int id;
6428 matchitem_T *cur = wp->w_match_head;
6430 while (cur != NULL && cur->id != id)
6431 cur = cur->next;
6432 return cur;
6434 #endif