Merged from the latest developing branch.
[MacVim.git] / src / window.c
blobb4ed10a5151bef89e5353e325c5a57999f06a732
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 = safe_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;
736 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
739 * Check if we are able to split the current window and compute its
740 * width.
742 needed = p_wmw + 1;
743 if (flags & WSP_ROOM)
744 needed += p_wiw - p_wmw;
745 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
747 available = topframe->fr_width;
748 needed += frame_minwidth(topframe, NULL);
750 else
751 available = oldwin->w_width;
752 if (available < needed && newwin == NULL)
754 EMSG(_(e_noroom));
755 return FAIL;
757 if (new_size == 0)
758 new_size = oldwin->w_width / 2;
759 if (new_size > oldwin->w_width - p_wmw - 1)
760 new_size = oldwin->w_width - p_wmw - 1;
761 if (new_size < p_wmw)
762 new_size = p_wmw;
764 /* if it doesn't fit in the current window, need win_equal() */
765 if (oldwin->w_width - new_size - 1 < p_wmw)
766 do_equal = TRUE;
768 /* We don't like to take lines for the new window from a
769 * 'winfixwidth' window. Take them from a window to the left or right
770 * instead, if possible. */
771 if (oldwin->w_p_wfw)
772 win_setwidth_win(oldwin->w_width + new_size, oldwin);
774 else
775 #endif
777 layout = FR_COL;
778 do_equal = (p_ea && new_size == 0
779 #ifdef FEAT_VERTSPLIT
780 && *p_ead != 'h'
781 #endif
785 * Check if we are able to split the current window and compute its
786 * height.
788 needed = p_wmh + STATUS_HEIGHT + need_status;
789 if (flags & WSP_ROOM)
790 needed += p_wh - p_wmh;
791 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
793 available = topframe->fr_height;
794 needed += frame_minheight(topframe, NULL);
796 else
798 available = oldwin->w_height;
799 needed += p_wmh;
801 if (available < needed && newwin == NULL)
803 EMSG(_(e_noroom));
804 return FAIL;
806 oldwin_height = oldwin->w_height;
807 if (need_status)
809 oldwin->w_status_height = STATUS_HEIGHT;
810 oldwin_height -= STATUS_HEIGHT;
812 if (new_size == 0)
813 new_size = oldwin_height / 2;
815 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
816 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
817 if (new_size < p_wmh)
818 new_size = p_wmh;
820 /* if it doesn't fit in the current window, need win_equal() */
821 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
822 do_equal = TRUE;
824 /* We don't like to take lines for the new window from a
825 * 'winfixheight' window. Take them from a window above or below
826 * instead, if possible. */
827 if (oldwin->w_p_wfh)
829 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
830 oldwin);
831 oldwin_height = oldwin->w_height;
832 if (need_status)
833 oldwin_height -= STATUS_HEIGHT;
838 * allocate new window structure and link it in the window list
840 if ((flags & WSP_TOP) == 0
841 && ((flags & WSP_BOT)
842 || (flags & WSP_BELOW)
843 || (!(flags & WSP_ABOVE)
844 && (
845 #ifdef FEAT_VERTSPLIT
846 (flags & WSP_VERT) ? p_spr :
847 #endif
848 p_sb))))
850 /* new window below/right of current one */
851 if (newwin == NULL)
852 wp = win_alloc(oldwin);
853 else
854 win_append(oldwin, wp);
856 else
858 if (newwin == NULL)
859 wp = win_alloc(oldwin->w_prev);
860 else
861 win_append(oldwin->w_prev, wp);
864 if (newwin == NULL)
866 if (wp == NULL)
867 return FAIL;
869 /* make the contents of the new window the same as the current one */
870 win_init(wp, curwin);
874 * Reorganise the tree of frames to insert the new window.
876 if (flags & (WSP_TOP | WSP_BOT))
878 #ifdef FEAT_VERTSPLIT
879 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
880 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
881 #else
882 if (topframe->fr_layout == FR_COL)
883 #endif
885 curfrp = topframe->fr_child;
886 if (flags & WSP_BOT)
887 while (curfrp->fr_next != NULL)
888 curfrp = curfrp->fr_next;
890 else
891 curfrp = topframe;
892 before = (flags & WSP_TOP);
894 else
896 curfrp = oldwin->w_frame;
897 if (flags & WSP_BELOW)
898 before = FALSE;
899 else if (flags & WSP_ABOVE)
900 before = TRUE;
901 else
902 #ifdef FEAT_VERTSPLIT
903 if (flags & WSP_VERT)
904 before = !p_spr;
905 else
906 #endif
907 before = !p_sb;
909 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
911 /* Need to create a new frame in the tree to make a branch. */
912 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
913 *frp = *curfrp;
914 curfrp->fr_layout = layout;
915 frp->fr_parent = curfrp;
916 frp->fr_next = NULL;
917 frp->fr_prev = NULL;
918 curfrp->fr_child = frp;
919 curfrp->fr_win = NULL;
920 curfrp = frp;
921 if (frp->fr_win != NULL)
922 oldwin->w_frame = frp;
923 else
924 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
925 frp->fr_parent = curfrp;
928 if (newwin == NULL)
930 /* Create a frame for the new window. */
931 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
932 frp->fr_layout = FR_LEAF;
933 frp->fr_win = wp;
934 wp->w_frame = frp;
936 else
937 frp = newwin->w_frame;
938 frp->fr_parent = curfrp->fr_parent;
940 /* Insert the new frame at the right place in the frame list. */
941 if (before)
942 frame_insert(curfrp, frp);
943 else
944 frame_append(curfrp, frp);
946 #ifdef FEAT_VERTSPLIT
947 if (flags & WSP_VERT)
949 wp->w_p_scr = curwin->w_p_scr;
950 if (need_status)
952 --oldwin->w_height;
953 oldwin->w_status_height = need_status;
955 if (flags & (WSP_TOP | WSP_BOT))
957 /* set height and row of new window to full height */
958 wp->w_winrow = tabline_height();
959 wp->w_height = curfrp->fr_height - (p_ls > 0);
960 wp->w_status_height = (p_ls > 0);
962 else
964 /* height and row of new window is same as current window */
965 wp->w_winrow = oldwin->w_winrow;
966 wp->w_height = oldwin->w_height;
967 wp->w_status_height = oldwin->w_status_height;
969 frp->fr_height = curfrp->fr_height;
971 /* "new_size" of the current window goes to the new window, use
972 * one column for the vertical separator */
973 wp->w_width = new_size;
974 if (before)
975 wp->w_vsep_width = 1;
976 else
978 wp->w_vsep_width = oldwin->w_vsep_width;
979 oldwin->w_vsep_width = 1;
981 if (flags & (WSP_TOP | WSP_BOT))
983 if (flags & WSP_BOT)
984 frame_add_vsep(curfrp);
985 /* Set width of neighbor frame */
986 frame_new_width(curfrp, curfrp->fr_width
987 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
988 FALSE);
990 else
991 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
992 if (before) /* new window left of current one */
994 wp->w_wincol = oldwin->w_wincol;
995 oldwin->w_wincol += new_size + 1;
997 else /* new window right of current one */
998 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
999 frame_fix_width(oldwin);
1000 frame_fix_width(wp);
1002 else
1003 #endif
1005 /* width and column of new window is same as current window */
1006 #ifdef FEAT_VERTSPLIT
1007 if (flags & (WSP_TOP | WSP_BOT))
1009 wp->w_wincol = 0;
1010 wp->w_width = Columns;
1011 wp->w_vsep_width = 0;
1013 else
1015 wp->w_wincol = oldwin->w_wincol;
1016 wp->w_width = oldwin->w_width;
1017 wp->w_vsep_width = oldwin->w_vsep_width;
1019 frp->fr_width = curfrp->fr_width;
1020 #endif
1022 /* "new_size" of the current window goes to the new window, use
1023 * one row for the status line */
1024 win_new_height(wp, new_size);
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 frame_new_height(curfrp, curfrp->fr_height
1027 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1028 else
1029 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1030 if (before) /* new window above current one */
1032 wp->w_winrow = oldwin->w_winrow;
1033 wp->w_status_height = STATUS_HEIGHT;
1034 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1036 else /* new window below current one */
1038 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1039 wp->w_status_height = oldwin->w_status_height;
1040 oldwin->w_status_height = STATUS_HEIGHT;
1042 #ifdef FEAT_VERTSPLIT
1043 if (flags & WSP_BOT)
1044 frame_add_statusline(curfrp);
1045 #endif
1046 frame_fix_height(wp);
1047 frame_fix_height(oldwin);
1050 if (flags & (WSP_TOP | WSP_BOT))
1051 (void)win_comp_pos();
1054 * Both windows need redrawing
1056 redraw_win_later(wp, NOT_VALID);
1057 wp->w_redr_status = TRUE;
1058 redraw_win_later(oldwin, NOT_VALID);
1059 oldwin->w_redr_status = TRUE;
1061 if (need_status)
1063 msg_row = Rows - 1;
1064 msg_col = sc_col;
1065 msg_clr_eos_force(); /* Old command/ruler may still be there */
1066 comp_col();
1067 msg_row = Rows - 1;
1068 msg_col = 0; /* put position back at start of line */
1072 * make the new window the current window and redraw
1074 if (do_equal || dir != 0)
1075 win_equal(wp, TRUE,
1076 #ifdef FEAT_VERTSPLIT
1077 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1078 : dir == 'h' ? 'b' :
1079 #endif
1080 'v');
1082 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1083 * size was given. */
1084 #ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_VERT)
1087 i = p_wiw;
1088 if (size != 0)
1089 p_wiw = size;
1091 # ifdef FEAT_GUI
1092 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1093 if (gui.in_use)
1094 gui_init_which_components(NULL);
1095 # endif
1097 else
1098 #endif
1100 i = p_wh;
1101 if (size != 0)
1102 p_wh = size;
1104 win_enter(wp, FALSE);
1105 #ifdef FEAT_VERTSPLIT
1106 if (flags & WSP_VERT)
1107 p_wiw = i;
1108 else
1109 #endif
1110 p_wh = i;
1112 return OK;
1116 * Initialize window "newp" from window "oldp".
1117 * Used when splitting a window and when creating a new tab page.
1118 * The windows will both edit the same buffer.
1120 static void
1121 win_init(newp, oldp)
1122 win_T *newp;
1123 win_T *oldp;
1125 int i;
1127 newp->w_buffer = oldp->w_buffer;
1128 oldp->w_buffer->b_nwindows++;
1129 newp->w_cursor = oldp->w_cursor;
1130 newp->w_valid = 0;
1131 newp->w_curswant = oldp->w_curswant;
1132 newp->w_set_curswant = oldp->w_set_curswant;
1133 newp->w_topline = oldp->w_topline;
1134 #ifdef FEAT_DIFF
1135 newp->w_topfill = oldp->w_topfill;
1136 #endif
1137 newp->w_leftcol = oldp->w_leftcol;
1138 newp->w_pcmark = oldp->w_pcmark;
1139 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1140 newp->w_alt_fnum = oldp->w_alt_fnum;
1141 newp->w_wrow = oldp->w_wrow;
1142 newp->w_fraction = oldp->w_fraction;
1143 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1144 #ifdef FEAT_JUMPLIST
1145 copy_jumplist(oldp, newp);
1146 #endif
1147 #ifdef FEAT_QUICKFIX
1148 copy_loclist(oldp, newp);
1149 #endif
1150 if (oldp->w_localdir != NULL)
1151 newp->w_localdir = vim_strsave(oldp->w_localdir);
1153 /* Use the same argument list. */
1154 newp->w_alist = oldp->w_alist;
1155 ++newp->w_alist->al_refcount;
1156 newp->w_arg_idx = oldp->w_arg_idx;
1159 * copy tagstack and options from existing window
1161 for (i = 0; i < oldp->w_tagstacklen; i++)
1163 newp->w_tagstack[i] = oldp->w_tagstack[i];
1164 if (newp->w_tagstack[i].tagname != NULL)
1165 newp->w_tagstack[i].tagname =
1166 vim_strsave(newp->w_tagstack[i].tagname);
1168 newp->w_tagstackidx = oldp->w_tagstackidx;
1169 newp->w_tagstacklen = oldp->w_tagstacklen;
1170 win_copy_options(oldp, newp);
1171 # ifdef FEAT_FOLDING
1172 copyFoldingState(oldp, newp);
1173 # endif
1176 #endif /* FEAT_WINDOWS */
1178 #if defined(FEAT_WINDOWS) || defined(PROTO)
1180 * Check if "win" is a pointer to an existing window.
1183 win_valid(win)
1184 win_T *win;
1186 win_T *wp;
1188 if (win == NULL)
1189 return FALSE;
1190 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1191 if (wp == win)
1192 return TRUE;
1193 return FALSE;
1197 * Return the number of windows.
1200 win_count()
1202 win_T *wp;
1203 int count = 0;
1205 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1206 ++count;
1207 return count;
1211 * Make "count" windows on the screen.
1212 * Return actual number of windows on the screen.
1213 * Must be called when there is just one window, filling the whole screen
1214 * (excluding the command line).
1216 /*ARGSUSED*/
1218 make_windows(count, vertical)
1219 int count;
1220 int vertical; /* split windows vertically if TRUE */
1222 int maxcount;
1223 int todo;
1225 #ifdef FEAT_VERTSPLIT
1226 if (vertical)
1228 /* Each windows needs at least 'winminwidth' lines and a separator
1229 * column. */
1230 maxcount = (curwin->w_width + curwin->w_vsep_width
1231 - (p_wiw - p_wmw)) / (p_wmw + 1);
1233 else
1234 #endif
1236 /* Each window needs at least 'winminheight' lines and a status line. */
1237 maxcount = (curwin->w_height + curwin->w_status_height
1238 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1241 if (maxcount < 2)
1242 maxcount = 2;
1243 if (count > maxcount)
1244 count = maxcount;
1247 * add status line now, otherwise first window will be too big
1249 if (count > 1)
1250 last_status(TRUE);
1252 #ifdef FEAT_AUTOCMD
1254 * Don't execute autocommands while creating the windows. Must do that
1255 * when putting the buffers in the windows.
1257 ++autocmd_block;
1258 #endif
1260 /* todo is number of windows left to create */
1261 for (todo = count - 1; todo > 0; --todo)
1262 #ifdef FEAT_VERTSPLIT
1263 if (vertical)
1265 if (win_split(curwin->w_width - (curwin->w_width - todo)
1266 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1267 break;
1269 else
1270 #endif
1272 if (win_split(curwin->w_height - (curwin->w_height - todo
1273 * STATUS_HEIGHT) / (todo + 1)
1274 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1275 break;
1278 #ifdef FEAT_AUTOCMD
1279 --autocmd_block;
1280 #endif
1282 /* return actual number of windows */
1283 return (count - todo);
1287 * Exchange current and next window
1289 static void
1290 win_exchange(Prenum)
1291 long Prenum;
1293 frame_T *frp;
1294 frame_T *frp2;
1295 win_T *wp;
1296 win_T *wp2;
1297 int temp;
1299 if (lastwin == firstwin) /* just one window */
1301 beep_flush();
1302 return;
1305 #ifdef FEAT_GUI
1306 need_mouse_correct = TRUE;
1307 #endif
1310 * find window to exchange with
1312 if (Prenum)
1314 frp = curwin->w_frame->fr_parent->fr_child;
1315 while (frp != NULL && --Prenum > 0)
1316 frp = frp->fr_next;
1318 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1319 frp = curwin->w_frame->fr_next;
1320 else /* Swap last window in row/col with previous */
1321 frp = curwin->w_frame->fr_prev;
1323 /* We can only exchange a window with another window, not with a frame
1324 * containing windows. */
1325 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1326 return;
1327 wp = frp->fr_win;
1330 * 1. remove curwin from the list. Remember after which window it was in wp2
1331 * 2. insert curwin before wp in the list
1332 * if wp != wp2
1333 * 3. remove wp from the list
1334 * 4. insert wp after wp2
1335 * 5. exchange the status line height and vsep width.
1337 wp2 = curwin->w_prev;
1338 frp2 = curwin->w_frame->fr_prev;
1339 if (wp->w_prev != curwin)
1341 win_remove(curwin, NULL);
1342 frame_remove(curwin->w_frame);
1343 win_append(wp->w_prev, curwin);
1344 frame_insert(frp, curwin->w_frame);
1346 if (wp != wp2)
1348 win_remove(wp, NULL);
1349 frame_remove(wp->w_frame);
1350 win_append(wp2, wp);
1351 if (frp2 == NULL)
1352 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1353 else
1354 frame_append(frp2, wp->w_frame);
1356 temp = curwin->w_status_height;
1357 curwin->w_status_height = wp->w_status_height;
1358 wp->w_status_height = temp;
1359 #ifdef FEAT_VERTSPLIT
1360 temp = curwin->w_vsep_width;
1361 curwin->w_vsep_width = wp->w_vsep_width;
1362 wp->w_vsep_width = temp;
1364 /* If the windows are not in the same frame, exchange the sizes to avoid
1365 * messing up the window layout. Otherwise fix the frame sizes. */
1366 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1368 temp = curwin->w_height;
1369 curwin->w_height = wp->w_height;
1370 wp->w_height = temp;
1371 temp = curwin->w_width;
1372 curwin->w_width = wp->w_width;
1373 wp->w_width = temp;
1375 else
1377 frame_fix_height(curwin);
1378 frame_fix_height(wp);
1379 frame_fix_width(curwin);
1380 frame_fix_width(wp);
1382 #endif
1384 (void)win_comp_pos(); /* recompute window positions */
1386 win_enter(wp, TRUE);
1387 redraw_later(CLEAR);
1391 * rotate windows: if upwards TRUE the second window becomes the first one
1392 * if upwards FALSE the first window becomes the second one
1394 static void
1395 win_rotate(upwards, count)
1396 int upwards;
1397 int count;
1399 win_T *wp1;
1400 win_T *wp2;
1401 frame_T *frp;
1402 int n;
1404 if (firstwin == lastwin) /* nothing to do */
1406 beep_flush();
1407 return;
1410 #ifdef FEAT_GUI
1411 need_mouse_correct = TRUE;
1412 #endif
1414 #ifdef FEAT_VERTSPLIT
1415 /* Check if all frames in this row/col have one window. */
1416 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1417 frp = frp->fr_next)
1418 if (frp->fr_win == NULL)
1420 EMSG(_("E443: Cannot rotate when another window is split"));
1421 return;
1423 #endif
1425 while (count--)
1427 if (upwards) /* first window becomes last window */
1429 /* remove first window/frame from the list */
1430 frp = curwin->w_frame->fr_parent->fr_child;
1431 wp1 = frp->fr_win;
1432 win_remove(wp1, NULL);
1433 frame_remove(frp);
1435 /* find last frame and append removed window/frame after it */
1436 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1438 win_append(frp->fr_win, wp1);
1439 frame_append(frp, wp1->w_frame);
1441 wp2 = frp->fr_win; /* previously last window */
1443 else /* last window becomes first window */
1445 /* find last window/frame in the list and remove it */
1446 for (frp = curwin->w_frame; frp->fr_next != NULL;
1447 frp = frp->fr_next)
1449 wp1 = frp->fr_win;
1450 wp2 = wp1->w_prev; /* will become last window */
1451 win_remove(wp1, NULL);
1452 frame_remove(frp);
1454 /* append the removed window/frame before the first in the list */
1455 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1456 frame_insert(frp->fr_parent->fr_child, frp);
1459 /* exchange status height and vsep width of old and new last window */
1460 n = wp2->w_status_height;
1461 wp2->w_status_height = wp1->w_status_height;
1462 wp1->w_status_height = n;
1463 frame_fix_height(wp1);
1464 frame_fix_height(wp2);
1465 #ifdef FEAT_VERTSPLIT
1466 n = wp2->w_vsep_width;
1467 wp2->w_vsep_width = wp1->w_vsep_width;
1468 wp1->w_vsep_width = n;
1469 frame_fix_width(wp1);
1470 frame_fix_width(wp2);
1471 #endif
1473 /* recompute w_winrow and w_wincol for all windows */
1474 (void)win_comp_pos();
1477 redraw_later(CLEAR);
1481 * Move the current window to the very top/bottom/left/right of the screen.
1483 static void
1484 win_totop(size, flags)
1485 int size;
1486 int flags;
1488 int dir;
1489 int height = curwin->w_height;
1491 if (lastwin == firstwin)
1493 beep_flush();
1494 return;
1497 /* Remove the window and frame from the tree of frames. */
1498 (void)winframe_remove(curwin, &dir, NULL);
1499 win_remove(curwin, NULL);
1500 last_status(FALSE); /* may need to remove last status line */
1501 (void)win_comp_pos(); /* recompute window positions */
1503 /* Split a window on the desired side and put the window there. */
1504 (void)win_split_ins(size, flags, curwin, dir);
1505 if (!(flags & WSP_VERT))
1507 win_setheight(height);
1508 if (p_ea)
1509 win_equal(curwin, TRUE, 'v');
1512 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1513 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1514 * scrollbars. Have to update them anyway. */
1515 if (gui.in_use)
1517 out_flush();
1518 gui_init_which_components(NULL);
1519 gui_update_scrollbars(TRUE);
1521 need_mouse_correct = TRUE;
1522 #endif
1527 * Move window "win1" to below/right of "win2" and make "win1" the current
1528 * window. Only works within the same frame!
1530 void
1531 win_move_after(win1, win2)
1532 win_T *win1, *win2;
1534 int height;
1536 /* check if the arguments are reasonable */
1537 if (win1 == win2)
1538 return;
1540 /* check if there is something to do */
1541 if (win2->w_next != win1)
1543 /* may need move the status line/vertical separator of the last window
1544 * */
1545 if (win1 == lastwin)
1547 height = win1->w_prev->w_status_height;
1548 win1->w_prev->w_status_height = win1->w_status_height;
1549 win1->w_status_height = height;
1550 #ifdef FEAT_VERTSPLIT
1551 if (win1->w_prev->w_vsep_width == 1)
1553 /* Remove the vertical separator from the last-but-one window,
1554 * add it to the last window. Adjust the frame widths. */
1555 win1->w_prev->w_vsep_width = 0;
1556 win1->w_prev->w_frame->fr_width -= 1;
1557 win1->w_vsep_width = 1;
1558 win1->w_frame->fr_width += 1;
1560 #endif
1562 else if (win2 == lastwin)
1564 height = win1->w_status_height;
1565 win1->w_status_height = win2->w_status_height;
1566 win2->w_status_height = height;
1567 #ifdef FEAT_VERTSPLIT
1568 if (win1->w_vsep_width == 1)
1570 /* Remove the vertical separator from win1, add it to the last
1571 * window, win2. Adjust the frame widths. */
1572 win2->w_vsep_width = 1;
1573 win2->w_frame->fr_width += 1;
1574 win1->w_vsep_width = 0;
1575 win1->w_frame->fr_width -= 1;
1577 #endif
1579 win_remove(win1, NULL);
1580 frame_remove(win1->w_frame);
1581 win_append(win2, win1);
1582 frame_append(win2->w_frame, win1->w_frame);
1584 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1585 redraw_later(NOT_VALID);
1587 win_enter(win1, FALSE);
1591 * Make all windows the same height.
1592 * 'next_curwin' will soon be the current window, make sure it has enough
1593 * rows.
1595 void
1596 win_equal(next_curwin, current, dir)
1597 win_T *next_curwin; /* pointer to current window to be or NULL */
1598 int current; /* do only frame with current window */
1599 int dir; /* 'v' for vertically, 'h' for horizontally,
1600 'b' for both, 0 for using p_ead */
1602 if (dir == 0)
1603 #ifdef FEAT_VERTSPLIT
1604 dir = *p_ead;
1605 #else
1606 dir = 'b';
1607 #endif
1608 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1609 topframe, dir, 0, tabline_height(),
1610 (int)Columns, topframe->fr_height);
1614 * Set a frame to a new position and height, spreading the available room
1615 * equally over contained frames.
1616 * The window "next_curwin" (if not NULL) should at least get the size from
1617 * 'winheight' and 'winwidth' if possible.
1619 static void
1620 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1621 win_T *next_curwin; /* pointer to current window to be or NULL */
1622 int current; /* do only frame with current window */
1623 frame_T *topfr; /* frame to set size off */
1624 int dir; /* 'v', 'h' or 'b', see win_equal() */
1625 int col; /* horizontal position for frame */
1626 int row; /* vertical position for frame */
1627 int width; /* new width of frame */
1628 int height; /* new height of frame */
1630 int n, m;
1631 int extra_sep = 0;
1632 int wincount, totwincount = 0;
1633 frame_T *fr;
1634 int next_curwin_size = 0;
1635 int room = 0;
1636 int new_size;
1637 int has_next_curwin = 0;
1638 int hnc;
1640 if (topfr->fr_layout == FR_LEAF)
1642 /* Set the width/height of this frame.
1643 * Redraw when size or position changes */
1644 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1645 #ifdef FEAT_VERTSPLIT
1646 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1647 #endif
1650 topfr->fr_win->w_winrow = row;
1651 frame_new_height(topfr, height, FALSE, FALSE);
1652 #ifdef FEAT_VERTSPLIT
1653 topfr->fr_win->w_wincol = col;
1654 frame_new_width(topfr, width, FALSE, FALSE);
1655 #endif
1656 redraw_all_later(CLEAR);
1659 #ifdef FEAT_VERTSPLIT
1660 else if (topfr->fr_layout == FR_ROW)
1662 topfr->fr_width = width;
1663 topfr->fr_height = height;
1665 if (dir != 'v') /* equalize frame widths */
1667 /* Compute the maximum number of windows horizontally in this
1668 * frame. */
1669 n = frame_minwidth(topfr, NOWIN);
1670 /* add one for the rightmost window, it doesn't have a separator */
1671 if (col + width == Columns)
1672 extra_sep = 1;
1673 else
1674 extra_sep = 0;
1675 totwincount = (n + extra_sep) / (p_wmw + 1);
1676 has_next_curwin = frame_has_win(topfr, next_curwin);
1679 * Compute width for "next_curwin" window and room available for
1680 * other windows.
1681 * "m" is the minimal width when counting p_wiw for "next_curwin".
1683 m = frame_minwidth(topfr, next_curwin);
1684 room = width - m;
1685 if (room < 0)
1687 next_curwin_size = p_wiw + room;
1688 room = 0;
1690 else
1692 next_curwin_size = -1;
1693 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1695 /* If 'winfixwidth' set keep the window width if
1696 * possible.
1697 * Watch out for this window being the next_curwin. */
1698 if (frame_fixed_width(fr))
1700 n = frame_minwidth(fr, NOWIN);
1701 new_size = fr->fr_width;
1702 if (frame_has_win(fr, next_curwin))
1704 room += p_wiw - p_wmw;
1705 next_curwin_size = 0;
1706 if (new_size < p_wiw)
1707 new_size = p_wiw;
1709 else
1710 /* These windows don't use up room. */
1711 totwincount -= (n + (fr->fr_next == NULL
1712 ? extra_sep : 0)) / (p_wmw + 1);
1713 room -= new_size - n;
1714 if (room < 0)
1716 new_size += room;
1717 room = 0;
1719 fr->fr_newwidth = new_size;
1722 if (next_curwin_size == -1)
1724 if (!has_next_curwin)
1725 next_curwin_size = 0;
1726 else if (totwincount > 1
1727 && (room + (totwincount - 2))
1728 / (totwincount - 1) > p_wiw)
1730 /* Can make all windows wider than 'winwidth', spread
1731 * the room equally. */
1732 next_curwin_size = (room + p_wiw
1733 + (totwincount - 1) * p_wmw
1734 + (totwincount - 1)) / totwincount;
1735 room -= next_curwin_size - p_wiw;
1737 else
1738 next_curwin_size = p_wiw;
1742 if (has_next_curwin)
1743 --totwincount; /* don't count curwin */
1746 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1748 n = m = 0;
1749 wincount = 1;
1750 if (fr->fr_next == NULL)
1751 /* last frame gets all that remains (avoid roundoff error) */
1752 new_size = width;
1753 else if (dir == 'v')
1754 new_size = fr->fr_width;
1755 else if (frame_fixed_width(fr))
1757 new_size = fr->fr_newwidth;
1758 wincount = 0; /* doesn't count as a sizeable window */
1760 else
1762 /* Compute the maximum number of windows horiz. in "fr". */
1763 n = frame_minwidth(fr, NOWIN);
1764 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1765 / (p_wmw + 1);
1766 m = frame_minwidth(fr, next_curwin);
1767 if (has_next_curwin)
1768 hnc = frame_has_win(fr, next_curwin);
1769 else
1770 hnc = FALSE;
1771 if (hnc) /* don't count next_curwin */
1772 --wincount;
1773 if (totwincount == 0)
1774 new_size = room;
1775 else
1776 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1777 / totwincount;
1778 if (hnc) /* add next_curwin size */
1780 next_curwin_size -= p_wiw - (m - n);
1781 new_size += next_curwin_size;
1782 room -= new_size - next_curwin_size;
1784 else
1785 room -= new_size;
1786 new_size += n;
1789 /* Skip frame that is full width when splitting or closing a
1790 * window, unless equalizing all frames. */
1791 if (!current || dir != 'v' || topfr->fr_parent != NULL
1792 || (new_size != fr->fr_width)
1793 || frame_has_win(fr, next_curwin))
1794 win_equal_rec(next_curwin, current, fr, dir, col, row,
1795 new_size, height);
1796 col += new_size;
1797 width -= new_size;
1798 totwincount -= wincount;
1801 #endif
1802 else /* topfr->fr_layout == FR_COL */
1804 #ifdef FEAT_VERTSPLIT
1805 topfr->fr_width = width;
1806 #endif
1807 topfr->fr_height = height;
1809 if (dir != 'h') /* equalize frame heights */
1811 /* Compute maximum number of windows vertically in this frame. */
1812 n = frame_minheight(topfr, NOWIN);
1813 /* add one for the bottom window if it doesn't have a statusline */
1814 if (row + height == cmdline_row && p_ls == 0)
1815 extra_sep = 1;
1816 else
1817 extra_sep = 0;
1818 totwincount = (n + extra_sep) / (p_wmh + 1);
1819 has_next_curwin = frame_has_win(topfr, next_curwin);
1822 * Compute height for "next_curwin" window and room available for
1823 * other windows.
1824 * "m" is the minimal height when counting p_wh for "next_curwin".
1826 m = frame_minheight(topfr, next_curwin);
1827 room = height - m;
1828 if (room < 0)
1830 /* The room is less then 'winheight', use all space for the
1831 * current window. */
1832 next_curwin_size = p_wh + room;
1833 room = 0;
1835 else
1837 next_curwin_size = -1;
1838 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1840 /* If 'winfixheight' set keep the window height if
1841 * possible.
1842 * Watch out for this window being the next_curwin. */
1843 if (frame_fixed_height(fr))
1845 n = frame_minheight(fr, NOWIN);
1846 new_size = fr->fr_height;
1847 if (frame_has_win(fr, next_curwin))
1849 room += p_wh - p_wmh;
1850 next_curwin_size = 0;
1851 if (new_size < p_wh)
1852 new_size = p_wh;
1854 else
1855 /* These windows don't use up room. */
1856 totwincount -= (n + (fr->fr_next == NULL
1857 ? extra_sep : 0)) / (p_wmh + 1);
1858 room -= new_size - n;
1859 if (room < 0)
1861 new_size += room;
1862 room = 0;
1864 fr->fr_newheight = new_size;
1867 if (next_curwin_size == -1)
1869 if (!has_next_curwin)
1870 next_curwin_size = 0;
1871 else if (totwincount > 1
1872 && (room + (totwincount - 2))
1873 / (totwincount - 1) > p_wh)
1875 /* can make all windows higher than 'winheight',
1876 * spread the room equally. */
1877 next_curwin_size = (room + p_wh
1878 + (totwincount - 1) * p_wmh
1879 + (totwincount - 1)) / totwincount;
1880 room -= next_curwin_size - p_wh;
1882 else
1883 next_curwin_size = p_wh;
1887 if (has_next_curwin)
1888 --totwincount; /* don't count curwin */
1891 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1893 n = m = 0;
1894 wincount = 1;
1895 if (fr->fr_next == NULL)
1896 /* last frame gets all that remains (avoid roundoff error) */
1897 new_size = height;
1898 else if (dir == 'h')
1899 new_size = fr->fr_height;
1900 else if (frame_fixed_height(fr))
1902 new_size = fr->fr_newheight;
1903 wincount = 0; /* doesn't count as a sizeable window */
1905 else
1907 /* Compute the maximum number of windows vert. in "fr". */
1908 n = frame_minheight(fr, NOWIN);
1909 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1910 / (p_wmh + 1);
1911 m = frame_minheight(fr, next_curwin);
1912 if (has_next_curwin)
1913 hnc = frame_has_win(fr, next_curwin);
1914 else
1915 hnc = FALSE;
1916 if (hnc) /* don't count next_curwin */
1917 --wincount;
1918 if (totwincount == 0)
1919 new_size = room;
1920 else
1921 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1922 / totwincount;
1923 if (hnc) /* add next_curwin size */
1925 next_curwin_size -= p_wh - (m - n);
1926 new_size += next_curwin_size;
1927 room -= new_size - next_curwin_size;
1929 else
1930 room -= new_size;
1931 new_size += n;
1933 /* Skip frame that is full width when splitting or closing a
1934 * window, unless equalizing all frames. */
1935 if (!current || dir != 'h' || topfr->fr_parent != NULL
1936 || (new_size != fr->fr_height)
1937 || frame_has_win(fr, next_curwin))
1938 win_equal_rec(next_curwin, current, fr, dir, col, row,
1939 width, new_size);
1940 row += new_size;
1941 height -= new_size;
1942 totwincount -= wincount;
1948 * close all windows for buffer 'buf'
1950 void
1951 close_windows(buf, keep_curwin)
1952 buf_T *buf;
1953 int keep_curwin; /* don't close "curwin" */
1955 win_T *wp;
1956 tabpage_T *tp, *nexttp;
1957 int h = tabline_height();
1959 ++RedrawingDisabled;
1961 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
1963 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
1965 win_close(wp, FALSE);
1967 /* Start all over, autocommands may change the window layout. */
1968 wp = firstwin;
1970 else
1971 wp = wp->w_next;
1974 /* Also check windows in other tab pages. */
1975 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1977 nexttp = tp->tp_next;
1978 if (tp != curtab)
1979 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1980 if (wp->w_buffer == buf)
1982 win_close_othertab(wp, FALSE, tp);
1984 /* Start all over, the tab page may be closed and
1985 * autocommands may change the window layout. */
1986 nexttp = first_tabpage;
1987 break;
1991 --RedrawingDisabled;
1993 if (h != tabline_height())
1994 shell_new_rows();
1998 * Return TRUE if the current window is the only window that exists.
1999 * Returns FALSE if there is a window, possibly in another tab page.
2001 static int
2002 last_window()
2004 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
2008 * Close window "win". Only works for the current tab page.
2009 * If "free_buf" is TRUE related buffer may be unloaded.
2011 * called by :quit, :close, :xit, :wq and findtag()
2013 void
2014 win_close(win, free_buf)
2015 win_T *win;
2016 int free_buf;
2018 win_T *wp;
2019 #ifdef FEAT_AUTOCMD
2020 int other_buffer = FALSE;
2021 #endif
2022 int close_curwin = FALSE;
2023 int dir;
2024 int help_window = FALSE;
2025 tabpage_T *prev_curtab = curtab;
2027 if (last_window())
2029 EMSG(_("E444: Cannot close last window"));
2030 return;
2034 * When closing the last window in a tab page first go to another tab
2035 * page and then close the window and the tab page. This avoids that
2036 * curwin and curtab are not invalid while we are freeing memory, they may
2037 * be used in GUI events.
2039 if (firstwin == lastwin)
2041 goto_tabpage_tp(alt_tabpage());
2042 redraw_tabline = TRUE;
2044 /* Safety check: Autocommands may have closed the window when jumping
2045 * to the other tab page. */
2046 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2048 int h = tabline_height();
2050 win_close_othertab(win, free_buf, prev_curtab);
2051 if (h != tabline_height())
2052 shell_new_rows();
2054 return;
2057 /* When closing the help window, try restoring a snapshot after closing
2058 * the window. Otherwise clear the snapshot, it's now invalid. */
2059 if (win->w_buffer->b_help)
2060 help_window = TRUE;
2061 else
2062 clear_snapshot(curtab);
2064 #ifdef FEAT_AUTOCMD
2065 if (win == curwin)
2068 * Guess which window is going to be the new current window.
2069 * This may change because of the autocommands (sigh).
2071 wp = frame2win(win_altframe(win, NULL));
2074 * Be careful: If autocommands delete the window, return now.
2076 if (wp->w_buffer != curbuf)
2078 other_buffer = TRUE;
2079 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2080 if (!win_valid(win) || last_window())
2081 return;
2083 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2084 if (!win_valid(win) || last_window())
2085 return;
2086 # ifdef FEAT_EVAL
2087 /* autocmds may abort script processing */
2088 if (aborting())
2089 return;
2090 # endif
2092 #endif
2094 #ifdef FEAT_GUI
2095 /* Avoid trouble with scrollbars that are going to be deleted in
2096 * win_free(). */
2097 if (gui.in_use)
2098 out_flush();
2099 #endif
2102 * Close the link to the buffer.
2104 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2106 /* Autocommands may have closed the window already, or closed the only
2107 * other window or moved to another tab page. */
2108 if (!win_valid(win) || last_window() || curtab != prev_curtab)
2109 return;
2111 /* Free the memory used for the window. */
2112 wp = win_free_mem(win, &dir, NULL);
2114 /* Make sure curwin isn't invalid. It can cause severe trouble when
2115 * printing an error message. For win_equal() curbuf needs to be valid
2116 * too. */
2117 if (win == curwin)
2119 curwin = wp;
2120 #ifdef FEAT_QUICKFIX
2121 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2124 * The cursor goes to the preview or the quickfix window, try
2125 * finding another window to go to.
2127 for (;;)
2129 if (wp->w_next == NULL)
2130 wp = firstwin;
2131 else
2132 wp = wp->w_next;
2133 if (wp == curwin)
2134 break;
2135 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2137 curwin = wp;
2138 break;
2142 #endif
2143 curbuf = curwin->w_buffer;
2144 close_curwin = TRUE;
2146 if (p_ea
2147 #ifdef FEAT_VERTSPLIT
2148 && (*p_ead == 'b' || *p_ead == dir)
2149 #endif
2151 win_equal(curwin, TRUE,
2152 #ifdef FEAT_VERTSPLIT
2154 #else
2156 #endif
2158 else
2159 win_comp_pos();
2160 if (close_curwin)
2162 win_enter_ext(wp, FALSE, TRUE);
2163 #ifdef FEAT_AUTOCMD
2164 if (other_buffer)
2165 /* careful: after this wp and win may be invalid! */
2166 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2167 #endif
2171 * If last window has a status line now and we don't want one,
2172 * remove the status line.
2174 last_status(FALSE);
2176 /* After closing the help window, try restoring the window layout from
2177 * before it was opened. */
2178 if (help_window)
2179 restore_snapshot(close_curwin);
2181 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2182 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2183 if (gui.in_use && !win_hasvertsplit())
2184 gui_init_which_components(NULL);
2185 #endif
2187 redraw_all_later(NOT_VALID);
2191 * Close window "win" in tab page "tp", which is not the current tab page.
2192 * This may be the last window ih that tab page and result in closing the tab,
2193 * thus "tp" may become invalid!
2194 * Caller must check if buffer is hidden and whether the tabline needs to be
2195 * updated.
2197 void
2198 win_close_othertab(win, free_buf, tp)
2199 win_T *win;
2200 int free_buf;
2201 tabpage_T *tp;
2203 win_T *wp;
2204 int dir;
2205 tabpage_T *ptp = NULL;
2207 /* Close the link to the buffer. */
2208 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2210 /* Careful: Autocommands may have closed the tab page or made it the
2211 * current tab page. */
2212 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2214 if (ptp == NULL || tp == curtab)
2215 return;
2217 /* Autocommands may have closed the window already. */
2218 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2220 if (wp == NULL)
2221 return;
2223 /* Free the memory used for the window. */
2224 wp = win_free_mem(win, &dir, tp);
2226 /* When closing the last window in a tab page remove the tab page. */
2227 if (wp == NULL)
2229 if (tp == first_tabpage)
2230 first_tabpage = tp->tp_next;
2231 else
2233 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2234 ptp = ptp->tp_next)
2236 if (ptp == NULL)
2238 EMSG2(_(e_intern2), "win_close_othertab()");
2239 return;
2241 ptp->tp_next = tp->tp_next;
2243 free_tabpage(tp);
2248 * Free the memory used for a window.
2249 * Returns a pointer to the window that got the freed up space.
2251 static win_T *
2252 win_free_mem(win, dirp, tp)
2253 win_T *win;
2254 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2255 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2257 frame_T *frp;
2258 win_T *wp;
2260 #ifdef FEAT_FOLDING
2261 clearFolding(win);
2262 #endif
2264 /* reduce the reference count to the argument list. */
2265 alist_unlink(win->w_alist);
2267 /* Remove the window and its frame from the tree of frames. */
2268 frp = win->w_frame;
2269 wp = winframe_remove(win, dirp, tp);
2270 vim_free(frp);
2271 win_free(win, tp);
2273 /* When deleting the current window of another tab page select a new
2274 * current window. */
2275 if (tp != NULL && win == tp->tp_curwin)
2276 tp->tp_curwin = wp;
2278 return wp;
2281 #if defined(EXITFREE) || defined(PROTO)
2282 void
2283 win_free_all()
2285 int dummy;
2287 # ifdef FEAT_WINDOWS
2288 while (first_tabpage->tp_next != NULL)
2289 tabpage_close(TRUE);
2290 # endif
2292 while (firstwin != NULL)
2293 (void)win_free_mem(firstwin, &dummy, NULL);
2295 #endif
2298 * Remove a window and its frame from the tree of frames.
2299 * Returns a pointer to the window that got the freed up space.
2301 /*ARGSUSED*/
2302 static win_T *
2303 winframe_remove(win, dirp, tp)
2304 win_T *win;
2305 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
2306 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2308 frame_T *frp, *frp2, *frp3;
2309 frame_T *frp_close = win->w_frame;
2310 win_T *wp;
2311 int old_size = 0;
2314 * If there is only one window there is nothing to remove.
2316 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2317 return NULL;
2320 * Remove the window from its frame.
2322 frp2 = win_altframe(win, tp);
2323 wp = frame2win(frp2);
2325 /* Remove this frame from the list of frames. */
2326 frame_remove(frp_close);
2328 #ifdef FEAT_VERTSPLIT
2329 if (frp_close->fr_parent->fr_layout == FR_COL)
2331 #endif
2332 /* When 'winfixheight' is set, remember its old size and restore
2333 * it later (it's a simplistic solution...). Don't do this if the
2334 * window will occupy the full height of the screen. */
2335 if (frp2->fr_win != NULL
2336 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2337 && frp2->fr_win->w_p_wfh)
2338 old_size = frp2->fr_win->w_height;
2339 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2340 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2341 if (old_size != 0)
2342 win_setheight_win(old_size, frp2->fr_win);
2343 #ifdef FEAT_VERTSPLIT
2344 *dirp = 'v';
2346 else
2348 /* When 'winfixwidth' is set, remember its old size and restore
2349 * it later (it's a simplistic solution...). Don't do this if the
2350 * window will occupy the full width of the screen. */
2351 if (frp2->fr_win != NULL
2352 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2353 && frp2->fr_win->w_p_wfw)
2354 old_size = frp2->fr_win->w_width;
2355 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2356 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2357 if (old_size != 0)
2358 win_setwidth_win(old_size, frp2->fr_win);
2359 *dirp = 'h';
2361 #endif
2363 /* If rows/columns go to a window below/right its positions need to be
2364 * updated. Can only be done after the sizes have been updated. */
2365 if (frp2 == frp_close->fr_next)
2367 int row = win->w_winrow;
2368 int col = W_WINCOL(win);
2370 frame_comp_pos(frp2, &row, &col);
2373 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2375 /* There is no other frame in this list, move its info to the parent
2376 * and remove it. */
2377 frp2->fr_parent->fr_layout = frp2->fr_layout;
2378 frp2->fr_parent->fr_child = frp2->fr_child;
2379 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2380 frp->fr_parent = frp2->fr_parent;
2381 frp2->fr_parent->fr_win = frp2->fr_win;
2382 if (frp2->fr_win != NULL)
2383 frp2->fr_win->w_frame = frp2->fr_parent;
2384 frp = frp2->fr_parent;
2385 vim_free(frp2);
2387 frp2 = frp->fr_parent;
2388 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2390 /* The frame above the parent has the same layout, have to merge
2391 * the frames into this list. */
2392 if (frp2->fr_child == frp)
2393 frp2->fr_child = frp->fr_child;
2394 frp->fr_child->fr_prev = frp->fr_prev;
2395 if (frp->fr_prev != NULL)
2396 frp->fr_prev->fr_next = frp->fr_child;
2397 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2399 frp3->fr_parent = frp2;
2400 if (frp3->fr_next == NULL)
2402 frp3->fr_next = frp->fr_next;
2403 if (frp->fr_next != NULL)
2404 frp->fr_next->fr_prev = frp3;
2405 break;
2408 vim_free(frp);
2412 return wp;
2416 * Find out which frame is going to get the freed up space when "win" is
2417 * closed.
2418 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2419 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2420 * This makes opening a window and closing it immediately keep the same window
2421 * layout.
2423 static frame_T *
2424 win_altframe(win, tp)
2425 win_T *win;
2426 tabpage_T *tp; /* tab page "win" is in, NULL for current */
2428 frame_T *frp;
2429 int b;
2431 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2432 /* Last window in this tab page, will go to next tab page. */
2433 return alt_tabpage()->tp_curwin->w_frame;
2435 frp = win->w_frame;
2436 #ifdef FEAT_VERTSPLIT
2437 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2438 b = p_spr;
2439 else
2440 #endif
2441 b = p_sb;
2442 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2443 return frp->fr_next;
2444 return frp->fr_prev;
2448 * Return the tabpage that will be used if the current one is closed.
2450 static tabpage_T *
2451 alt_tabpage()
2453 tabpage_T *tp;
2455 /* Use the next tab page if possible. */
2456 if (curtab->tp_next != NULL)
2457 return curtab->tp_next;
2459 /* Find the last but one tab page. */
2460 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2462 return tp;
2466 * Find the left-upper window in frame "frp".
2468 static win_T *
2469 frame2win(frp)
2470 frame_T *frp;
2472 while (frp->fr_win == NULL)
2473 frp = frp->fr_child;
2474 return frp->fr_win;
2478 * Return TRUE if frame "frp" contains window "wp".
2480 static int
2481 frame_has_win(frp, wp)
2482 frame_T *frp;
2483 win_T *wp;
2485 frame_T *p;
2487 if (frp->fr_layout == FR_LEAF)
2488 return frp->fr_win == wp;
2490 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2491 if (frame_has_win(p, wp))
2492 return TRUE;
2493 return FALSE;
2497 * Set a new height for a frame. Recursively sets the height for contained
2498 * frames and windows. Caller must take care of positions.
2500 static void
2501 frame_new_height(topfrp, height, topfirst, wfh)
2502 frame_T *topfrp;
2503 int height;
2504 int topfirst; /* resize topmost contained frame first */
2505 int wfh; /* obey 'winfixheight' when there is a choice;
2506 may cause the height not to be set */
2508 frame_T *frp;
2509 int extra_lines;
2510 int h;
2512 if (topfrp->fr_win != NULL)
2514 /* Simple case: just one window. */
2515 win_new_height(topfrp->fr_win,
2516 height - topfrp->fr_win->w_status_height);
2518 #ifdef FEAT_VERTSPLIT
2519 else if (topfrp->fr_layout == FR_ROW)
2523 /* All frames in this row get the same new height. */
2524 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2526 frame_new_height(frp, height, topfirst, wfh);
2527 if (frp->fr_height > height)
2529 /* Could not fit the windows, make the whole row higher. */
2530 height = frp->fr_height;
2531 break;
2535 while (frp != NULL);
2537 #endif
2538 else /* fr_layout == FR_COL */
2540 /* Complicated case: Resize a column of frames. Resize the bottom
2541 * frame first, frames above that when needed. */
2543 frp = topfrp->fr_child;
2544 if (wfh)
2545 /* Advance past frames with one window with 'wfh' set. */
2546 while (frame_fixed_height(frp))
2548 frp = frp->fr_next;
2549 if (frp == NULL)
2550 return; /* no frame without 'wfh', give up */
2552 if (!topfirst)
2554 /* Find the bottom frame of this column */
2555 while (frp->fr_next != NULL)
2556 frp = frp->fr_next;
2557 if (wfh)
2558 /* Advance back for frames with one window with 'wfh' set. */
2559 while (frame_fixed_height(frp))
2560 frp = frp->fr_prev;
2563 extra_lines = height - topfrp->fr_height;
2564 if (extra_lines < 0)
2566 /* reduce height of contained frames, bottom or top frame first */
2567 while (frp != NULL)
2569 h = frame_minheight(frp, NULL);
2570 if (frp->fr_height + extra_lines < h)
2572 extra_lines += frp->fr_height - h;
2573 frame_new_height(frp, h, topfirst, wfh);
2575 else
2577 frame_new_height(frp, frp->fr_height + extra_lines,
2578 topfirst, wfh);
2579 break;
2581 if (topfirst)
2584 frp = frp->fr_next;
2585 while (wfh && frp != NULL && frame_fixed_height(frp));
2587 else
2590 frp = frp->fr_prev;
2591 while (wfh && frp != NULL && frame_fixed_height(frp));
2593 /* Increase "height" if we could not reduce enough frames. */
2594 if (frp == NULL)
2595 height -= extra_lines;
2598 else if (extra_lines > 0)
2600 /* increase height of bottom or top frame */
2601 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2604 topfrp->fr_height = height;
2608 * Return TRUE if height of frame "frp" should not be changed because of
2609 * the 'winfixheight' option.
2611 static int
2612 frame_fixed_height(frp)
2613 frame_T *frp;
2615 /* frame with one window: fixed height if 'winfixheight' set. */
2616 if (frp->fr_win != NULL)
2617 return frp->fr_win->w_p_wfh;
2619 if (frp->fr_layout == FR_ROW)
2621 /* The frame is fixed height if one of the frames in the row is fixed
2622 * height. */
2623 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2624 if (frame_fixed_height(frp))
2625 return TRUE;
2626 return FALSE;
2629 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2630 * frames in the row are fixed height. */
2631 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2632 if (!frame_fixed_height(frp))
2633 return FALSE;
2634 return TRUE;
2637 #ifdef FEAT_VERTSPLIT
2639 * Return TRUE if width of frame "frp" should not be changed because of
2640 * the 'winfixwidth' option.
2642 static int
2643 frame_fixed_width(frp)
2644 frame_T *frp;
2646 /* frame with one window: fixed width if 'winfixwidth' set. */
2647 if (frp->fr_win != NULL)
2648 return frp->fr_win->w_p_wfw;
2650 if (frp->fr_layout == FR_COL)
2652 /* The frame is fixed width if one of the frames in the row is fixed
2653 * width. */
2654 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2655 if (frame_fixed_width(frp))
2656 return TRUE;
2657 return FALSE;
2660 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2661 * frames in the row are fixed width. */
2662 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2663 if (!frame_fixed_width(frp))
2664 return FALSE;
2665 return TRUE;
2669 * Add a status line to windows at the bottom of "frp".
2670 * Note: Does not check if there is room!
2672 static void
2673 frame_add_statusline(frp)
2674 frame_T *frp;
2676 win_T *wp;
2678 if (frp->fr_layout == FR_LEAF)
2680 wp = frp->fr_win;
2681 if (wp->w_status_height == 0)
2683 if (wp->w_height > 0) /* don't make it negative */
2684 --wp->w_height;
2685 wp->w_status_height = STATUS_HEIGHT;
2688 else if (frp->fr_layout == FR_ROW)
2690 /* Handle all the frames in the row. */
2691 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2692 frame_add_statusline(frp);
2694 else /* frp->fr_layout == FR_COL */
2696 /* Only need to handle the last frame in the column. */
2697 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2699 frame_add_statusline(frp);
2704 * Set width of a frame. Handles recursively going through contained frames.
2705 * May remove separator line for windows at the right side (for win_close()).
2707 static void
2708 frame_new_width(topfrp, width, leftfirst, wfw)
2709 frame_T *topfrp;
2710 int width;
2711 int leftfirst; /* resize leftmost contained frame first */
2712 int wfw; /* obey 'winfixwidth' when there is a choice;
2713 may cause the width not to be set */
2715 frame_T *frp;
2716 int extra_cols;
2717 int w;
2718 win_T *wp;
2720 if (topfrp->fr_layout == FR_LEAF)
2722 /* Simple case: just one window. */
2723 wp = topfrp->fr_win;
2724 /* Find out if there are any windows right of this one. */
2725 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2726 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2727 break;
2728 if (frp->fr_parent == NULL)
2729 wp->w_vsep_width = 0;
2730 win_new_width(wp, width - wp->w_vsep_width);
2732 else if (topfrp->fr_layout == FR_COL)
2736 /* All frames in this column get the same new width. */
2737 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2739 frame_new_width(frp, width, leftfirst, wfw);
2740 if (frp->fr_width > width)
2742 /* Could not fit the windows, make whole column wider. */
2743 width = frp->fr_width;
2744 break;
2747 } while (frp != NULL);
2749 else /* fr_layout == FR_ROW */
2751 /* Complicated case: Resize a row of frames. Resize the rightmost
2752 * frame first, frames left of it when needed. */
2754 frp = topfrp->fr_child;
2755 if (wfw)
2756 /* Advance past frames with one window with 'wfw' set. */
2757 while (frame_fixed_width(frp))
2759 frp = frp->fr_next;
2760 if (frp == NULL)
2761 return; /* no frame without 'wfw', give up */
2763 if (!leftfirst)
2765 /* Find the rightmost frame of this row */
2766 while (frp->fr_next != NULL)
2767 frp = frp->fr_next;
2768 if (wfw)
2769 /* Advance back for frames with one window with 'wfw' set. */
2770 while (frame_fixed_width(frp))
2771 frp = frp->fr_prev;
2774 extra_cols = width - topfrp->fr_width;
2775 if (extra_cols < 0)
2777 /* reduce frame width, rightmost frame first */
2778 while (frp != NULL)
2780 w = frame_minwidth(frp, NULL);
2781 if (frp->fr_width + extra_cols < w)
2783 extra_cols += frp->fr_width - w;
2784 frame_new_width(frp, w, leftfirst, wfw);
2786 else
2788 frame_new_width(frp, frp->fr_width + extra_cols,
2789 leftfirst, wfw);
2790 break;
2792 if (leftfirst)
2795 frp = frp->fr_next;
2796 while (wfw && frp != NULL && frame_fixed_width(frp));
2798 else
2801 frp = frp->fr_prev;
2802 while (wfw && frp != NULL && frame_fixed_width(frp));
2804 /* Increase "width" if we could not reduce enough frames. */
2805 if (frp == NULL)
2806 width -= extra_cols;
2809 else if (extra_cols > 0)
2811 /* increase width of rightmost frame */
2812 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2815 topfrp->fr_width = width;
2819 * Add the vertical separator to windows at the right side of "frp".
2820 * Note: Does not check if there is room!
2822 static void
2823 frame_add_vsep(frp)
2824 frame_T *frp;
2826 win_T *wp;
2828 if (frp->fr_layout == FR_LEAF)
2830 wp = frp->fr_win;
2831 if (wp->w_vsep_width == 0)
2833 if (wp->w_width > 0) /* don't make it negative */
2834 --wp->w_width;
2835 wp->w_vsep_width = 1;
2838 else if (frp->fr_layout == FR_COL)
2840 /* Handle all the frames in the column. */
2841 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2842 frame_add_vsep(frp);
2844 else /* frp->fr_layout == FR_ROW */
2846 /* Only need to handle the last frame in the row. */
2847 frp = frp->fr_child;
2848 while (frp->fr_next != NULL)
2849 frp = frp->fr_next;
2850 frame_add_vsep(frp);
2855 * Set frame width from the window it contains.
2857 static void
2858 frame_fix_width(wp)
2859 win_T *wp;
2861 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2863 #endif
2866 * Set frame height from the window it contains.
2868 static void
2869 frame_fix_height(wp)
2870 win_T *wp;
2872 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2876 * Compute the minimal height for frame "topfrp".
2877 * Uses the 'winminheight' option.
2878 * When "next_curwin" isn't NULL, use p_wh for this window.
2879 * When "next_curwin" is NOWIN, don't use at least one line for the current
2880 * window.
2882 static int
2883 frame_minheight(topfrp, next_curwin)
2884 frame_T *topfrp;
2885 win_T *next_curwin;
2887 frame_T *frp;
2888 int m;
2889 #ifdef FEAT_VERTSPLIT
2890 int n;
2891 #endif
2893 if (topfrp->fr_win != NULL)
2895 if (topfrp->fr_win == next_curwin)
2896 m = p_wh + topfrp->fr_win->w_status_height;
2897 else
2899 /* window: minimal height of the window plus status line */
2900 m = p_wmh + topfrp->fr_win->w_status_height;
2901 /* Current window is minimal one line high */
2902 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2903 ++m;
2906 #ifdef FEAT_VERTSPLIT
2907 else if (topfrp->fr_layout == FR_ROW)
2909 /* get the minimal height from each frame in this row */
2910 m = 0;
2911 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2913 n = frame_minheight(frp, next_curwin);
2914 if (n > m)
2915 m = n;
2918 #endif
2919 else
2921 /* Add up the minimal heights for all frames in this column. */
2922 m = 0;
2923 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2924 m += frame_minheight(frp, next_curwin);
2927 return m;
2930 #ifdef FEAT_VERTSPLIT
2932 * Compute the minimal width for frame "topfrp".
2933 * When "next_curwin" isn't NULL, use p_wiw for this window.
2934 * When "next_curwin" is NOWIN, don't use at least one column for the current
2935 * window.
2937 static int
2938 frame_minwidth(topfrp, next_curwin)
2939 frame_T *topfrp;
2940 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2942 frame_T *frp;
2943 int m, n;
2945 if (topfrp->fr_win != NULL)
2947 if (topfrp->fr_win == next_curwin)
2948 m = p_wiw + topfrp->fr_win->w_vsep_width;
2949 else
2951 /* window: minimal width of the window plus separator column */
2952 m = p_wmw + topfrp->fr_win->w_vsep_width;
2953 /* Current window is minimal one column wide */
2954 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2955 ++m;
2958 else if (topfrp->fr_layout == FR_COL)
2960 /* get the minimal width from each frame in this column */
2961 m = 0;
2962 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2964 n = frame_minwidth(frp, next_curwin);
2965 if (n > m)
2966 m = n;
2969 else
2971 /* Add up the minimal widths for all frames in this row. */
2972 m = 0;
2973 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2974 m += frame_minwidth(frp, next_curwin);
2977 return m;
2979 #endif
2983 * Try to close all windows except current one.
2984 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2985 * used and the buffer was modified.
2987 * Used by ":bdel" and ":only".
2989 void
2990 close_others(message, forceit)
2991 int message;
2992 int forceit; /* always hide all other windows */
2994 win_T *wp;
2995 win_T *nextwp;
2996 int r;
2998 if (lastwin == firstwin)
3000 if (message
3001 #ifdef FEAT_AUTOCMD
3002 && !autocmd_busy
3003 #endif
3005 MSG(_(m_onlyone));
3006 return;
3009 /* Be very careful here: autocommands may change the window layout. */
3010 for (wp = firstwin; win_valid(wp); wp = nextwp)
3012 nextwp = wp->w_next;
3013 if (wp != curwin) /* don't close current window */
3016 /* Check if it's allowed to abandon this window */
3017 r = can_abandon(wp->w_buffer, forceit);
3018 #ifdef FEAT_AUTOCMD
3019 if (!win_valid(wp)) /* autocommands messed wp up */
3021 nextwp = firstwin;
3022 continue;
3024 #endif
3025 if (!r)
3027 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3028 if (message && (p_confirm || cmdmod.confirm) && p_write)
3030 dialog_changed(wp->w_buffer, FALSE);
3031 # ifdef FEAT_AUTOCMD
3032 if (!win_valid(wp)) /* autocommands messed wp up */
3034 nextwp = firstwin;
3035 continue;
3037 # endif
3039 if (bufIsChanged(wp->w_buffer))
3040 #endif
3041 continue;
3043 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3047 if (message && lastwin != firstwin)
3048 EMSG(_("E445: Other window contains changes"));
3051 #endif /* FEAT_WINDOWS */
3054 * Init the current window "curwin".
3055 * Called when a new file is being edited.
3057 void
3058 curwin_init()
3060 redraw_win_later(curwin, NOT_VALID);
3061 curwin->w_lines_valid = 0;
3062 curwin->w_cursor.lnum = 1;
3063 curwin->w_curswant = curwin->w_cursor.col = 0;
3064 #ifdef FEAT_VIRTUALEDIT
3065 curwin->w_cursor.coladd = 0;
3066 #endif
3067 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3068 curwin->w_pcmark.col = 0;
3069 curwin->w_prev_pcmark.lnum = 0;
3070 curwin->w_prev_pcmark.col = 0;
3071 curwin->w_topline = 1;
3072 #ifdef FEAT_DIFF
3073 curwin->w_topfill = 0;
3074 #endif
3075 curwin->w_botline = 2;
3076 #ifdef FEAT_FKMAP
3077 if (curwin->w_p_rl)
3078 curwin->w_farsi = W_CONV + W_R_L;
3079 else
3080 curwin->w_farsi = W_CONV;
3081 #endif
3085 * Allocate the first window and put an empty buffer in it.
3086 * Called from main().
3087 * Return FAIL when something goes wrong (out of memory).
3090 win_alloc_first()
3092 if (win_alloc_firstwin(NULL) == FAIL)
3093 return FAIL;
3095 #ifdef FEAT_WINDOWS
3096 first_tabpage = alloc_tabpage();
3097 if (first_tabpage == NULL)
3098 return FAIL;
3099 first_tabpage->tp_topframe = topframe;
3100 curtab = first_tabpage;
3101 #endif
3102 return OK;
3106 * Allocate the first window or the first window in a new tab page.
3107 * When "oldwin" is NULL create an empty buffer for it.
3108 * When "oldwin" is not NULL copy info from it to the new window (only with
3109 * FEAT_WINDOWS).
3110 * Return FAIL when something goes wrong (out of memory).
3112 static int
3113 win_alloc_firstwin(oldwin)
3114 win_T *oldwin;
3116 curwin = win_alloc(NULL);
3117 if (oldwin == NULL)
3119 /* Very first window, need to create an empty buffer for it and
3120 * initialize from scratch. */
3121 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3122 if (curwin == NULL || curbuf == NULL)
3123 return FAIL;
3124 curwin->w_buffer = curbuf;
3125 curbuf->b_nwindows = 1; /* there is one window */
3126 #ifdef FEAT_WINDOWS
3127 curwin->w_alist = &global_alist;
3128 #endif
3129 curwin_init(); /* init current window */
3131 #ifdef FEAT_WINDOWS
3132 else
3134 /* First window in new tab page, initialize it from "oldwin". */
3135 win_init(curwin, oldwin);
3137 # ifdef FEAT_SCROLLBIND
3138 /* We don't want scroll-binding in the first window. */
3139 curwin->w_p_scb = FALSE;
3140 # endif
3142 #endif
3144 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3145 if (topframe == NULL)
3146 return FAIL;
3147 topframe->fr_layout = FR_LEAF;
3148 #ifdef FEAT_VERTSPLIT
3149 topframe->fr_width = Columns;
3150 #endif
3151 topframe->fr_height = Rows - p_ch;
3152 topframe->fr_win = curwin;
3153 curwin->w_frame = topframe;
3155 return OK;
3159 * Initialize the window and frame size to the maximum.
3161 void
3162 win_init_size()
3164 firstwin->w_height = ROWS_AVAIL;
3165 topframe->fr_height = ROWS_AVAIL;
3166 #ifdef FEAT_VERTSPLIT
3167 firstwin->w_width = Columns;
3168 topframe->fr_width = Columns;
3169 #endif
3172 #if defined(FEAT_WINDOWS) || defined(PROTO)
3175 * Allocate a new tabpage_T and init the values.
3176 * Returns NULL when out of memory.
3178 static tabpage_T *
3179 alloc_tabpage()
3181 tabpage_T *tp;
3183 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3184 if (tp != NULL)
3186 # ifdef FEAT_GUI
3187 int i;
3189 for (i = 0; i < 3; i++)
3190 tp->tp_prev_which_scrollbars[i] = -1;
3191 # endif
3192 # ifdef FEAT_DIFF
3193 tp->tp_diff_invalid = TRUE;
3194 # endif
3195 #ifdef FEAT_EVAL
3196 /* init t: variables */
3197 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3198 #endif
3199 tp->tp_ch_used = p_ch;
3201 return tp;
3204 void
3205 free_tabpage(tp)
3206 tabpage_T *tp;
3208 # ifdef FEAT_DIFF
3209 diff_clear(tp);
3210 # endif
3211 clear_snapshot(tp);
3212 #ifdef FEAT_EVAL
3213 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3214 #endif
3215 vim_free(tp);
3219 * Create a new Tab page with one window.
3220 * It will edit the current buffer, like after ":split".
3221 * When "after" is 0 put it just after the current Tab page.
3222 * Otherwise put it just before tab page "after".
3223 * Return FAIL or OK.
3226 win_new_tabpage(after)
3227 int after;
3229 tabpage_T *tp = curtab;
3230 tabpage_T *newtp;
3231 int n;
3233 newtp = alloc_tabpage();
3234 if (newtp == NULL)
3235 return FAIL;
3237 /* Remember the current windows in this Tab page. */
3238 if (leave_tabpage(curbuf) == FAIL)
3240 vim_free(newtp);
3241 return FAIL;
3243 curtab = newtp;
3245 /* Create a new empty window. */
3246 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3248 /* Make the new Tab page the new topframe. */
3249 if (after == 1)
3251 /* New tab page becomes the first one. */
3252 newtp->tp_next = first_tabpage;
3253 first_tabpage = newtp;
3255 else
3257 if (after > 0)
3259 /* Put new tab page before tab page "after". */
3260 n = 2;
3261 for (tp = first_tabpage; tp->tp_next != NULL
3262 && n < after; tp = tp->tp_next)
3263 ++n;
3265 newtp->tp_next = tp->tp_next;
3266 tp->tp_next = newtp;
3268 win_init_size();
3269 firstwin->w_winrow = tabline_height();
3270 win_comp_scroll(curwin);
3272 newtp->tp_topframe = topframe;
3273 last_status(FALSE);
3275 #if defined(FEAT_GUI)
3276 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3277 * scrollbars. Have to update them anyway. */
3278 if (gui.in_use && starting == 0)
3280 gui_init_which_components(NULL);
3281 gui_update_scrollbars(TRUE);
3283 need_mouse_correct = TRUE;
3284 #endif
3286 redraw_all_later(CLEAR);
3287 #ifdef FEAT_AUTOCMD
3288 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3289 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3290 #endif
3291 return OK;
3294 /* Failed, get back the previous Tab page */
3295 enter_tabpage(curtab, curbuf);
3296 return FAIL;
3300 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3301 * like with ":split".
3302 * Returns OK if a new tab page was created, FAIL otherwise.
3305 may_open_tabpage()
3307 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3309 if (n != 0)
3311 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3312 postponed_split_tab = 0;
3313 return win_new_tabpage(n);
3315 return FAIL;
3319 * Create up to "maxcount" tabpages with empty windows.
3320 * Returns the number of resulting tab pages.
3323 make_tabpages(maxcount)
3324 int maxcount;
3326 int count = maxcount;
3327 int todo;
3329 /* Limit to 'tabpagemax' tabs. */
3330 if (count > p_tpm)
3331 count = p_tpm;
3333 #ifdef FEAT_AUTOCMD
3335 * Don't execute autocommands while creating the tab pages. Must do that
3336 * when putting the buffers in the windows.
3338 ++autocmd_block;
3339 #endif
3341 for (todo = count - 1; todo > 0; --todo)
3342 if (win_new_tabpage(0) == FAIL)
3343 break;
3345 #ifdef FEAT_AUTOCMD
3346 --autocmd_block;
3347 #endif
3349 /* return actual number of tab pages */
3350 return (count - todo);
3354 * Return TRUE when "tpc" points to a valid tab page.
3357 valid_tabpage(tpc)
3358 tabpage_T *tpc;
3360 tabpage_T *tp;
3362 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3363 if (tp == tpc)
3364 return TRUE;
3365 return FALSE;
3369 * Find tab page "n" (first one is 1). Returns NULL when not found.
3371 tabpage_T *
3372 find_tabpage(n)
3373 int n;
3375 tabpage_T *tp;
3376 int i = 1;
3378 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3379 ++i;
3380 return tp;
3384 * Get index of tab page "tp". First one has index 1.
3385 * When not found returns number of tab pages plus one.
3388 tabpage_index(ftp)
3389 tabpage_T *ftp;
3391 int i = 1;
3392 tabpage_T *tp;
3394 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3395 ++i;
3396 return i;
3400 * Prepare for leaving the current tab page.
3401 * When autocomands change "curtab" we don't leave the tab page and return
3402 * FAIL.
3403 * Careful: When OK is returned need to get a new tab page very very soon!
3405 /*ARGSUSED*/
3406 static int
3407 leave_tabpage(new_curbuf)
3408 buf_T *new_curbuf; /* what is going to be the new curbuf,
3409 NULL if unknown */
3411 tabpage_T *tp = curtab;
3413 #ifdef FEAT_VISUAL
3414 reset_VIsual_and_resel(); /* stop Visual mode */
3415 #endif
3416 #ifdef FEAT_AUTOCMD
3417 if (new_curbuf != curbuf)
3419 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3420 if (curtab != tp)
3421 return FAIL;
3423 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3424 if (curtab != tp)
3425 return FAIL;
3426 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3427 if (curtab != tp)
3428 return FAIL;
3429 #endif
3430 #if defined(FEAT_GUI)
3431 /* Remove the scrollbars. They may be added back later. */
3432 if (gui.in_use)
3433 gui_remove_scrollbars();
3434 #endif
3435 tp->tp_curwin = curwin;
3436 tp->tp_prevwin = prevwin;
3437 tp->tp_firstwin = firstwin;
3438 tp->tp_lastwin = lastwin;
3439 tp->tp_old_Rows = Rows;
3440 tp->tp_old_Columns = Columns;
3441 firstwin = NULL;
3442 lastwin = NULL;
3443 return OK;
3447 * Start using tab page "tp".
3448 * Only to be used after leave_tabpage() or freeing the current tab page.
3450 /*ARGSUSED*/
3451 static void
3452 enter_tabpage(tp, old_curbuf)
3453 tabpage_T *tp;
3454 buf_T *old_curbuf;
3456 int old_off = tp->tp_firstwin->w_winrow;
3457 win_T *next_prevwin = tp->tp_prevwin;
3459 curtab = tp;
3460 firstwin = tp->tp_firstwin;
3461 lastwin = tp->tp_lastwin;
3462 topframe = tp->tp_topframe;
3464 /* We would like doing the TabEnter event first, but we don't have a
3465 * valid current window yet, which may break some commands.
3466 * This triggers autocommands, thus may make "tp" invalid. */
3467 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3468 prevwin = next_prevwin;
3470 #ifdef FEAT_AUTOCMD
3471 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3473 if (old_curbuf != curbuf)
3474 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3475 #endif
3477 last_status(FALSE); /* status line may appear or disappear */
3478 (void)win_comp_pos(); /* recompute w_winrow for all windows */
3479 must_redraw = CLEAR; /* need to redraw everything */
3480 #ifdef FEAT_DIFF
3481 diff_need_scrollbind = TRUE;
3482 #endif
3484 /* The tabpage line may have appeared or disappeared, may need to resize
3485 * the frames for that. When the Vim window was resized need to update
3486 * frame sizes too. Use the stored value of p_ch, so that it can be
3487 * different for each tab page. */
3488 p_ch = curtab->tp_ch_used;
3489 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3490 #ifdef FEAT_GUI_TABLINE
3491 && !gui_use_tabline()
3492 #endif
3494 shell_new_rows();
3495 #ifdef FEAT_VERTSPLIT
3496 if (curtab->tp_old_Columns != Columns && starting == 0)
3497 shell_new_columns(); /* update window widths */
3498 #endif
3500 #if defined(FEAT_GUI)
3501 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3502 * scrollbars. Have to update them anyway. */
3503 if (gui.in_use && starting == 0)
3505 gui_init_which_components(NULL);
3506 gui_update_scrollbars(TRUE);
3508 need_mouse_correct = TRUE;
3509 #endif
3511 redraw_all_later(CLEAR);
3515 * Go to tab page "n". For ":tab N" and "Ngt".
3516 * When "n" is 9999 go to the last tab page.
3518 void
3519 goto_tabpage(n)
3520 int n;
3522 tabpage_T *tp;
3523 tabpage_T *ttp;
3524 int i;
3526 if (text_locked())
3528 /* Not allowed when editing the command line. */
3529 #ifdef FEAT_CMDWIN
3530 if (cmdwin_type != 0)
3531 EMSG(_(e_cmdwin));
3532 else
3533 #endif
3534 EMSG(_(e_secure));
3535 return;
3538 /* If there is only one it can't work. */
3539 if (first_tabpage->tp_next == NULL)
3541 if (n > 1)
3542 beep_flush();
3543 return;
3546 if (n == 0)
3548 /* No count, go to next tab page, wrap around end. */
3549 if (curtab->tp_next == NULL)
3550 tp = first_tabpage;
3551 else
3552 tp = curtab->tp_next;
3554 else if (n < 0)
3556 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3557 * this N times. */
3558 ttp = curtab;
3559 for (i = n; i < 0; ++i)
3561 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3562 tp = tp->tp_next)
3564 ttp = tp;
3567 else if (n == 9999)
3569 /* Go to last tab page. */
3570 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3573 else
3575 /* Go to tab page "n". */
3576 tp = find_tabpage(n);
3577 if (tp == NULL)
3579 beep_flush();
3580 return;
3584 goto_tabpage_tp(tp);
3586 #ifdef FEAT_GUI_TABLINE
3587 if (gui_use_tabline())
3588 gui_mch_set_curtab(tabpage_index(curtab));
3589 #endif
3593 * Go to tabpage "tp".
3594 * Note: doesn't update the GUI tab.
3596 void
3597 goto_tabpage_tp(tp)
3598 tabpage_T *tp;
3600 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3602 if (valid_tabpage(tp))
3603 enter_tabpage(tp, curbuf);
3604 else
3605 enter_tabpage(curtab, curbuf);
3610 * Enter window "wp" in tab page "tp".
3611 * Also updates the GUI tab.
3613 void
3614 goto_tabpage_win(tp, wp)
3615 tabpage_T *tp;
3616 win_T *wp;
3618 goto_tabpage_tp(tp);
3619 if (curtab == tp && win_valid(wp))
3621 win_enter(wp, TRUE);
3622 # ifdef FEAT_GUI_TABLINE
3623 if (gui_use_tabline())
3624 gui_mch_set_curtab(tabpage_index(curtab));
3625 # endif
3630 * Move the current tab page to before tab page "nr".
3632 void
3633 tabpage_move(nr)
3634 int nr;
3636 int n = nr;
3637 tabpage_T *tp;
3639 if (first_tabpage->tp_next == NULL)
3640 return;
3642 /* Remove the current tab page from the list of tab pages. */
3643 if (curtab == first_tabpage)
3644 first_tabpage = curtab->tp_next;
3645 else
3647 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3648 if (tp->tp_next == curtab)
3649 break;
3650 if (tp == NULL) /* "cannot happen" */
3651 return;
3652 tp->tp_next = curtab->tp_next;
3655 /* Re-insert it at the specified position. */
3656 if (n == 0)
3658 curtab->tp_next = first_tabpage;
3659 first_tabpage = curtab;
3661 else
3663 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3664 --n;
3665 curtab->tp_next = tp->tp_next;
3666 tp->tp_next = curtab;
3669 /* Need to redraw the tabline. Tab page contents doesn't change. */
3670 redraw_tabline = TRUE;
3675 * Go to another window.
3676 * When jumping to another buffer, stop Visual mode. Do this before
3677 * changing windows so we can yank the selection into the '*' register.
3678 * When jumping to another window on the same buffer, adjust its cursor
3679 * position to keep the same Visual area.
3681 void
3682 win_goto(wp)
3683 win_T *wp;
3685 if (text_locked())
3687 beep_flush();
3688 text_locked_msg();
3689 return;
3691 #ifdef FEAT_AUTOCMD
3692 if (curbuf_locked())
3693 return;
3694 #endif
3696 #ifdef FEAT_VISUAL
3697 if (wp->w_buffer != curbuf)
3698 reset_VIsual_and_resel();
3699 else if (VIsual_active)
3700 wp->w_cursor = curwin->w_cursor;
3701 #endif
3703 #ifdef FEAT_GUI
3704 need_mouse_correct = TRUE;
3705 #endif
3706 win_enter(wp, TRUE);
3709 #if defined(FEAT_PERL) || defined(PROTO)
3711 * Find window number "winnr" (counting top to bottom).
3713 win_T *
3714 win_find_nr(winnr)
3715 int winnr;
3717 win_T *wp;
3719 # ifdef FEAT_WINDOWS
3720 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3721 if (--winnr == 0)
3722 break;
3723 return wp;
3724 # else
3725 return curwin;
3726 # endif
3728 #endif
3730 #ifdef FEAT_VERTSPLIT
3732 * Move to window above or below "count" times.
3734 static void
3735 win_goto_ver(up, count)
3736 int up; /* TRUE to go to win above */
3737 long count;
3739 frame_T *fr;
3740 frame_T *nfr;
3741 frame_T *foundfr;
3743 foundfr = curwin->w_frame;
3744 while (count--)
3747 * First go upwards in the tree of frames until we find a upwards or
3748 * downwards neighbor.
3750 fr = foundfr;
3751 for (;;)
3753 if (fr == topframe)
3754 goto end;
3755 if (up)
3756 nfr = fr->fr_prev;
3757 else
3758 nfr = fr->fr_next;
3759 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3760 break;
3761 fr = fr->fr_parent;
3765 * Now go downwards to find the bottom or top frame in it.
3767 for (;;)
3769 if (nfr->fr_layout == FR_LEAF)
3771 foundfr = nfr;
3772 break;
3774 fr = nfr->fr_child;
3775 if (nfr->fr_layout == FR_ROW)
3777 /* Find the frame at the cursor row. */
3778 while (fr->fr_next != NULL
3779 && frame2win(fr)->w_wincol + fr->fr_width
3780 <= curwin->w_wincol + curwin->w_wcol)
3781 fr = fr->fr_next;
3783 if (nfr->fr_layout == FR_COL && up)
3784 while (fr->fr_next != NULL)
3785 fr = fr->fr_next;
3786 nfr = fr;
3789 end:
3790 if (foundfr != NULL)
3791 win_goto(foundfr->fr_win);
3795 * Move to left or right window.
3797 static void
3798 win_goto_hor(left, count)
3799 int left; /* TRUE to go to left win */
3800 long count;
3802 frame_T *fr;
3803 frame_T *nfr;
3804 frame_T *foundfr;
3806 foundfr = curwin->w_frame;
3807 while (count--)
3810 * First go upwards in the tree of frames until we find a left or
3811 * right neighbor.
3813 fr = foundfr;
3814 for (;;)
3816 if (fr == topframe)
3817 goto end;
3818 if (left)
3819 nfr = fr->fr_prev;
3820 else
3821 nfr = fr->fr_next;
3822 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3823 break;
3824 fr = fr->fr_parent;
3828 * Now go downwards to find the leftmost or rightmost frame in it.
3830 for (;;)
3832 if (nfr->fr_layout == FR_LEAF)
3834 foundfr = nfr;
3835 break;
3837 fr = nfr->fr_child;
3838 if (nfr->fr_layout == FR_COL)
3840 /* Find the frame at the cursor row. */
3841 while (fr->fr_next != NULL
3842 && frame2win(fr)->w_winrow + fr->fr_height
3843 <= curwin->w_winrow + curwin->w_wrow)
3844 fr = fr->fr_next;
3846 if (nfr->fr_layout == FR_ROW && left)
3847 while (fr->fr_next != NULL)
3848 fr = fr->fr_next;
3849 nfr = fr;
3852 end:
3853 if (foundfr != NULL)
3854 win_goto(foundfr->fr_win);
3856 #endif
3859 * Make window "wp" the current window.
3861 void
3862 win_enter(wp, undo_sync)
3863 win_T *wp;
3864 int undo_sync;
3866 win_enter_ext(wp, undo_sync, FALSE);
3870 * Make window wp the current window.
3871 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3872 * been closed and isn't valid.
3874 static void
3875 win_enter_ext(wp, undo_sync, curwin_invalid)
3876 win_T *wp;
3877 int undo_sync;
3878 int curwin_invalid;
3880 #ifdef FEAT_AUTOCMD
3881 int other_buffer = FALSE;
3882 #endif
3884 if (wp == curwin && !curwin_invalid) /* nothing to do */
3885 return;
3887 #ifdef FEAT_AUTOCMD
3888 if (!curwin_invalid)
3891 * Be careful: If autocommands delete the window, return now.
3893 if (wp->w_buffer != curbuf)
3895 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3896 other_buffer = TRUE;
3897 if (!win_valid(wp))
3898 return;
3900 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3901 if (!win_valid(wp))
3902 return;
3903 # ifdef FEAT_EVAL
3904 /* autocmds may abort script processing */
3905 if (aborting())
3906 return;
3907 # endif
3909 #endif
3911 /* sync undo before leaving the current buffer */
3912 if (undo_sync && curbuf != wp->w_buffer)
3913 u_sync(FALSE);
3914 /* may have to copy the buffer options when 'cpo' contains 'S' */
3915 if (wp->w_buffer != curbuf)
3916 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3917 if (!curwin_invalid)
3919 prevwin = curwin; /* remember for CTRL-W p */
3920 curwin->w_redr_status = TRUE;
3922 curwin = wp;
3923 curbuf = wp->w_buffer;
3924 check_cursor();
3925 #ifdef FEAT_VIRTUALEDIT
3926 if (!virtual_active())
3927 curwin->w_cursor.coladd = 0;
3928 #endif
3929 changed_line_abv_curs(); /* assume cursor position needs updating */
3931 if (curwin->w_localdir != NULL)
3933 /* Window has a local directory: Save current directory as global
3934 * directory (unless that was done already) and change to the local
3935 * directory. */
3936 if (globaldir == NULL)
3938 char_u cwd[MAXPATHL];
3940 if (mch_dirname(cwd, MAXPATHL) == OK)
3941 globaldir = vim_strsave(cwd);
3943 mch_chdir((char *)curwin->w_localdir);
3944 shorten_fnames(TRUE);
3946 else if (globaldir != NULL)
3948 /* Window doesn't have a local directory and we are not in the global
3949 * directory: Change to the global directory. */
3950 mch_chdir((char *)globaldir);
3951 vim_free(globaldir);
3952 globaldir = NULL;
3953 shorten_fnames(TRUE);
3956 #ifdef FEAT_AUTOCMD
3957 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3958 if (other_buffer)
3959 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3960 #endif
3962 #ifdef FEAT_TITLE
3963 maketitle();
3964 #endif
3965 curwin->w_redr_status = TRUE;
3966 redraw_tabline = TRUE;
3967 if (restart_edit)
3968 redraw_later(VALID); /* causes status line redraw */
3970 /* set window height to desired minimal value */
3971 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3972 win_setheight((int)p_wh);
3973 else if (curwin->w_height == 0)
3974 win_setheight(1);
3976 #ifdef FEAT_VERTSPLIT
3977 /* set window width to desired minimal value */
3978 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
3979 win_setwidth((int)p_wiw);
3980 #endif
3982 #ifdef FEAT_MOUSE
3983 setmouse(); /* in case jumped to/from help buffer */
3984 #endif
3986 /* Change directories when the 'acd' option is set. */
3987 DO_AUTOCHDIR
3990 #endif /* FEAT_WINDOWS */
3992 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3994 * Jump to the first open window that contains buffer "buf", if one exists.
3995 * Returns a pointer to the window found, otherwise NULL.
3997 win_T *
3998 buf_jump_open_win(buf)
3999 buf_T *buf;
4001 # ifdef FEAT_WINDOWS
4002 win_T *wp;
4004 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4005 if (wp->w_buffer == buf)
4006 break;
4007 if (wp != NULL)
4008 win_enter(wp, FALSE);
4009 return wp;
4010 # else
4011 if (curwin->w_buffer == buf)
4012 return curwin;
4013 return NULL;
4014 # endif
4018 * Jump to the first open window in any tab page that contains buffer "buf",
4019 * if one exists.
4020 * Returns a pointer to the window found, otherwise NULL.
4022 win_T *
4023 buf_jump_open_tab(buf)
4024 buf_T *buf;
4026 # ifdef FEAT_WINDOWS
4027 win_T *wp;
4028 tabpage_T *tp;
4030 /* First try the current tab page. */
4031 wp = buf_jump_open_win(buf);
4032 if (wp != NULL)
4033 return wp;
4035 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4036 if (tp != curtab)
4038 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4039 if (wp->w_buffer == buf)
4040 break;
4041 if (wp != NULL)
4043 goto_tabpage_win(tp, wp);
4044 if (curwin != wp)
4045 wp = NULL; /* something went wrong */
4046 break;
4050 return wp;
4051 # else
4052 if (curwin->w_buffer == buf)
4053 return curwin;
4054 return NULL;
4055 # endif
4057 #endif
4060 * allocate a window structure and link it in the window list
4062 /*ARGSUSED*/
4063 static win_T *
4064 win_alloc(after)
4065 win_T *after;
4067 win_T *newwin;
4070 * allocate window structure and linesizes arrays
4072 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4073 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4075 vim_free(newwin);
4076 newwin = NULL;
4079 if (newwin != NULL)
4081 #ifdef FEAT_AUTOCMD
4082 /* Don't execute autocommands while the window is not properly
4083 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4084 * event. */
4085 ++autocmd_block;
4086 #endif
4088 * link the window in the window list
4090 #ifdef FEAT_WINDOWS
4091 win_append(after, newwin);
4092 #endif
4093 #ifdef FEAT_VERTSPLIT
4094 newwin->w_wincol = 0;
4095 newwin->w_width = Columns;
4096 #endif
4098 /* position the display and the cursor at the top of the file. */
4099 newwin->w_topline = 1;
4100 #ifdef FEAT_DIFF
4101 newwin->w_topfill = 0;
4102 #endif
4103 newwin->w_botline = 2;
4104 newwin->w_cursor.lnum = 1;
4105 #ifdef FEAT_SCROLLBIND
4106 newwin->w_scbind_pos = 1;
4107 #endif
4109 /* We won't calculate w_fraction until resizing the window */
4110 newwin->w_fraction = 0;
4111 newwin->w_prev_fraction_row = -1;
4113 #ifdef FEAT_GUI
4114 if (gui.in_use)
4116 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4117 SBAR_LEFT, newwin);
4118 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4119 SBAR_RIGHT, newwin);
4121 #endif
4122 #ifdef FEAT_EVAL
4123 /* init w: variables */
4124 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
4125 #endif
4126 #ifdef FEAT_FOLDING
4127 foldInitWin(newwin);
4128 #endif
4129 #ifdef FEAT_AUTOCMD
4130 --autocmd_block;
4131 #endif
4132 #ifdef FEAT_SEARCH_EXTRA
4133 newwin->w_match_head = NULL;
4134 newwin->w_next_match_id = 4;
4135 #endif
4137 return newwin;
4140 #if defined(FEAT_WINDOWS) || defined(PROTO)
4143 * remove window 'wp' from the window list and free the structure
4145 static void
4146 win_free(wp, tp)
4147 win_T *wp;
4148 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4150 int i;
4152 #ifdef FEAT_AUTOCMD
4153 /* Don't execute autocommands while the window is halfway being deleted.
4154 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4155 ++autocmd_block;
4156 #endif
4158 #ifdef FEAT_MZSCHEME
4159 mzscheme_window_free(wp);
4160 #endif
4162 #ifdef FEAT_PERL
4163 perl_win_free(wp);
4164 #endif
4166 #ifdef FEAT_PYTHON
4167 python_window_free(wp);
4168 #endif
4170 #ifdef FEAT_TCL
4171 tcl_window_free(wp);
4172 #endif
4174 #ifdef FEAT_RUBY
4175 ruby_window_free(wp);
4176 #endif
4178 clear_winopt(&wp->w_onebuf_opt);
4179 clear_winopt(&wp->w_allbuf_opt);
4181 #ifdef FEAT_EVAL
4182 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
4183 #endif
4185 if (prevwin == wp)
4186 prevwin = NULL;
4187 win_free_lsize(wp);
4189 for (i = 0; i < wp->w_tagstacklen; ++i)
4190 vim_free(wp->w_tagstack[i].tagname);
4192 vim_free(wp->w_localdir);
4194 #ifdef FEAT_SEARCH_EXTRA
4195 clear_matches(wp);
4196 #endif
4198 #ifdef FEAT_JUMPLIST
4199 free_jumplist(wp);
4200 #endif
4202 #ifdef FEAT_QUICKFIX
4203 qf_free_all(wp);
4204 #endif
4206 #ifdef FEAT_GUI
4207 if (gui.in_use)
4209 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4210 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4212 #endif /* FEAT_GUI */
4214 win_remove(wp, tp);
4215 vim_free(wp);
4217 #ifdef FEAT_AUTOCMD
4218 --autocmd_block;
4219 #endif
4223 * Append window "wp" in the window list after window "after".
4225 static void
4226 win_append(after, wp)
4227 win_T *after, *wp;
4229 win_T *before;
4231 if (after == NULL) /* after NULL is in front of the first */
4232 before = firstwin;
4233 else
4234 before = after->w_next;
4236 wp->w_next = before;
4237 wp->w_prev = after;
4238 if (after == NULL)
4239 firstwin = wp;
4240 else
4241 after->w_next = wp;
4242 if (before == NULL)
4243 lastwin = wp;
4244 else
4245 before->w_prev = wp;
4249 * Remove a window from the window list.
4251 static void
4252 win_remove(wp, tp)
4253 win_T *wp;
4254 tabpage_T *tp; /* tab page "win" is in, NULL for current */
4256 if (wp->w_prev != NULL)
4257 wp->w_prev->w_next = wp->w_next;
4258 else if (tp == NULL)
4259 firstwin = wp->w_next;
4260 else
4261 tp->tp_firstwin = wp->w_next;
4262 if (wp->w_next != NULL)
4263 wp->w_next->w_prev = wp->w_prev;
4264 else if (tp == NULL)
4265 lastwin = wp->w_prev;
4266 else
4267 tp->tp_lastwin = wp->w_prev;
4271 * Append frame "frp" in a frame list after frame "after".
4273 static void
4274 frame_append(after, frp)
4275 frame_T *after, *frp;
4277 frp->fr_next = after->fr_next;
4278 after->fr_next = frp;
4279 if (frp->fr_next != NULL)
4280 frp->fr_next->fr_prev = frp;
4281 frp->fr_prev = after;
4285 * Insert frame "frp" in a frame list before frame "before".
4287 static void
4288 frame_insert(before, frp)
4289 frame_T *before, *frp;
4291 frp->fr_next = before;
4292 frp->fr_prev = before->fr_prev;
4293 before->fr_prev = frp;
4294 if (frp->fr_prev != NULL)
4295 frp->fr_prev->fr_next = frp;
4296 else
4297 frp->fr_parent->fr_child = frp;
4301 * Remove a frame from a frame list.
4303 static void
4304 frame_remove(frp)
4305 frame_T *frp;
4307 if (frp->fr_prev != NULL)
4308 frp->fr_prev->fr_next = frp->fr_next;
4309 else
4310 frp->fr_parent->fr_child = frp->fr_next;
4311 if (frp->fr_next != NULL)
4312 frp->fr_next->fr_prev = frp->fr_prev;
4315 #endif /* FEAT_WINDOWS */
4318 * Allocate w_lines[] for window "wp".
4319 * Return FAIL for failure, OK for success.
4322 win_alloc_lines(wp)
4323 win_T *wp;
4325 wp->w_lines_valid = 0;
4326 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4327 if (wp->w_lines == NULL)
4328 return FAIL;
4329 return OK;
4333 * free lsize arrays for a window
4335 void
4336 win_free_lsize(wp)
4337 win_T *wp;
4339 vim_free(wp->w_lines);
4340 wp->w_lines = NULL;
4344 * Called from win_new_shellsize() after Rows changed.
4345 * This only does the current tab page, others must be done when made active.
4347 void
4348 shell_new_rows()
4350 int h = (int)ROWS_AVAIL;
4352 if (firstwin == NULL) /* not initialized yet */
4353 return;
4354 #ifdef FEAT_WINDOWS
4355 if (h < frame_minheight(topframe, NULL))
4356 h = frame_minheight(topframe, NULL);
4358 /* First try setting the heights of windows with 'winfixheight'. If
4359 * that doesn't result in the right height, forget about that option. */
4360 frame_new_height(topframe, h, FALSE, TRUE);
4361 if (topframe->fr_height != h)
4362 frame_new_height(topframe, h, FALSE, FALSE);
4364 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4365 #else
4366 if (h < 1)
4367 h = 1;
4368 win_new_height(firstwin, h);
4369 #endif
4370 compute_cmdrow();
4371 #ifdef FEAT_WINDOWS
4372 curtab->tp_ch_used = p_ch;
4373 #endif
4375 #if 0
4376 /* Disabled: don't want making the screen smaller make a window larger. */
4377 if (p_ea)
4378 win_equal(curwin, FALSE, 'v');
4379 #endif
4382 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4384 * Called from win_new_shellsize() after Columns changed.
4386 void
4387 shell_new_columns()
4389 if (firstwin == NULL) /* not initialized yet */
4390 return;
4392 /* First try setting the widths of windows with 'winfixwidth'. If that
4393 * doesn't result in the right width, forget about that option. */
4394 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4395 if (topframe->fr_width != Columns)
4396 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4398 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4399 #if 0
4400 /* Disabled: don't want making the screen smaller make a window larger. */
4401 if (p_ea)
4402 win_equal(curwin, FALSE, 'h');
4403 #endif
4405 #endif
4407 #if defined(FEAT_CMDWIN) || defined(PROTO)
4409 * Save the size of all windows in "gap".
4411 void
4412 win_size_save(gap)
4413 garray_T *gap;
4416 win_T *wp;
4418 ga_init2(gap, (int)sizeof(int), 1);
4419 if (ga_grow(gap, win_count() * 2) == OK)
4420 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4422 ((int *)gap->ga_data)[gap->ga_len++] =
4423 wp->w_width + wp->w_vsep_width;
4424 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4429 * Restore window sizes, but only if the number of windows is still the same.
4430 * Does not free the growarray.
4432 void
4433 win_size_restore(gap)
4434 garray_T *gap;
4436 win_T *wp;
4437 int i;
4439 if (win_count() * 2 == gap->ga_len)
4441 i = 0;
4442 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4444 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4445 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4447 /* recompute the window positions */
4448 (void)win_comp_pos();
4451 #endif /* FEAT_CMDWIN */
4453 #if defined(FEAT_WINDOWS) || defined(PROTO)
4455 * Update the position for all windows, using the width and height of the
4456 * frames.
4457 * Returns the row just after the last window.
4460 win_comp_pos()
4462 int row = tabline_height();
4463 int col = 0;
4465 frame_comp_pos(topframe, &row, &col);
4466 return row;
4470 * Update the position of the windows in frame "topfrp", using the width and
4471 * height of the frames.
4472 * "*row" and "*col" are the top-left position of the frame. They are updated
4473 * to the bottom-right position plus one.
4475 static void
4476 frame_comp_pos(topfrp, row, col)
4477 frame_T *topfrp;
4478 int *row;
4479 int *col;
4481 win_T *wp;
4482 frame_T *frp;
4483 #ifdef FEAT_VERTSPLIT
4484 int startcol;
4485 int startrow;
4486 #endif
4488 wp = topfrp->fr_win;
4489 if (wp != NULL)
4491 if (wp->w_winrow != *row
4492 #ifdef FEAT_VERTSPLIT
4493 || wp->w_wincol != *col
4494 #endif
4497 /* position changed, redraw */
4498 wp->w_winrow = *row;
4499 #ifdef FEAT_VERTSPLIT
4500 wp->w_wincol = *col;
4501 #endif
4502 redraw_win_later(wp, NOT_VALID);
4503 wp->w_redr_status = TRUE;
4505 *row += wp->w_height + wp->w_status_height;
4506 #ifdef FEAT_VERTSPLIT
4507 *col += wp->w_width + wp->w_vsep_width;
4508 #endif
4510 else
4512 #ifdef FEAT_VERTSPLIT
4513 startrow = *row;
4514 startcol = *col;
4515 #endif
4516 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4518 #ifdef FEAT_VERTSPLIT
4519 if (topfrp->fr_layout == FR_ROW)
4520 *row = startrow; /* all frames are at the same row */
4521 else
4522 *col = startcol; /* all frames are at the same col */
4523 #endif
4524 frame_comp_pos(frp, row, col);
4529 #endif /* FEAT_WINDOWS */
4532 * Set current window height and take care of repositioning other windows to
4533 * fit around it.
4535 void
4536 win_setheight(height)
4537 int height;
4539 win_setheight_win(height, curwin);
4543 * Set the window height of window "win" and take care of repositioning other
4544 * windows to fit around it.
4546 void
4547 win_setheight_win(height, win)
4548 int height;
4549 win_T *win;
4551 int row;
4553 if (win == curwin)
4555 /* Always keep current window at least one line high, even when
4556 * 'winminheight' is zero. */
4557 #ifdef FEAT_WINDOWS
4558 if (height < p_wmh)
4559 height = p_wmh;
4560 #endif
4561 if (height == 0)
4562 height = 1;
4565 #ifdef FEAT_WINDOWS
4566 frame_setheight(win->w_frame, height + win->w_status_height);
4568 /* recompute the window positions */
4569 row = win_comp_pos();
4570 #else
4571 if (height > topframe->fr_height)
4572 height = topframe->fr_height;
4573 win->w_height = height;
4574 row = height;
4575 #endif
4578 * If there is extra space created between the last window and the command
4579 * line, clear it.
4581 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4582 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4583 cmdline_row = row;
4584 msg_row = row;
4585 msg_col = 0;
4587 redraw_all_later(NOT_VALID);
4590 #if defined(FEAT_WINDOWS) || defined(PROTO)
4593 * Set the height of a frame to "height" and take care that all frames and
4594 * windows inside it are resized. Also resize frames on the left and right if
4595 * the are in the same FR_ROW frame.
4597 * Strategy:
4598 * If the frame is part of a FR_COL frame, try fitting the frame in that
4599 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4600 * go to containing frames to resize them and make room.
4601 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4602 * Check for the minimal height of the FR_ROW frame.
4603 * At the top level we can also use change the command line height.
4605 static void
4606 frame_setheight(curfrp, height)
4607 frame_T *curfrp;
4608 int height;
4610 int room; /* total number of lines available */
4611 int take; /* number of lines taken from other windows */
4612 int room_cmdline; /* lines available from cmdline */
4613 int run;
4614 frame_T *frp;
4615 int h;
4616 int room_reserved;
4618 /* If the height already is the desired value, nothing to do. */
4619 if (curfrp->fr_height == height)
4620 return;
4622 if (curfrp->fr_parent == NULL)
4624 /* topframe: can only change the command line */
4625 if (height > ROWS_AVAIL)
4626 height = ROWS_AVAIL;
4627 if (height > 0)
4628 frame_new_height(curfrp, height, FALSE, FALSE);
4630 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4632 /* Row of frames: Also need to resize frames left and right of this
4633 * one. First check for the minimal height of these. */
4634 h = frame_minheight(curfrp->fr_parent, NULL);
4635 if (height < h)
4636 height = h;
4637 frame_setheight(curfrp->fr_parent, height);
4639 else
4642 * Column of frames: try to change only frames in this column.
4644 #ifdef FEAT_VERTSPLIT
4646 * Do this twice:
4647 * 1: compute room available, if it's not enough try resizing the
4648 * containing frame.
4649 * 2: compute the room available and adjust the height to it.
4650 * Try not to reduce the height of a window with 'winfixheight' set.
4652 for (run = 1; run <= 2; ++run)
4653 #else
4654 for (;;)
4655 #endif
4657 room = 0;
4658 room_reserved = 0;
4659 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4660 frp = frp->fr_next)
4662 if (frp != curfrp
4663 && frp->fr_win != NULL
4664 && frp->fr_win->w_p_wfh)
4665 room_reserved += frp->fr_height;
4666 room += frp->fr_height;
4667 if (frp != curfrp)
4668 room -= frame_minheight(frp, NULL);
4670 #ifdef FEAT_VERTSPLIT
4671 if (curfrp->fr_width != Columns)
4672 room_cmdline = 0;
4673 else
4674 #endif
4676 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4677 + lastwin->w_height + lastwin->w_status_height);
4678 if (room_cmdline < 0)
4679 room_cmdline = 0;
4682 if (height <= room + room_cmdline)
4683 break;
4684 #ifdef FEAT_VERTSPLIT
4685 if (run == 2 || curfrp->fr_width == Columns)
4686 #endif
4688 if (height > room + room_cmdline)
4689 height = room + room_cmdline;
4690 break;
4692 #ifdef FEAT_VERTSPLIT
4693 frame_setheight(curfrp->fr_parent, height
4694 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4695 #endif
4696 /*NOTREACHED*/
4700 * Compute the number of lines we will take from others frames (can be
4701 * negative!).
4703 take = height - curfrp->fr_height;
4705 /* If there is not enough room, also reduce the height of a window
4706 * with 'winfixheight' set. */
4707 if (height > room + room_cmdline - room_reserved)
4708 room_reserved = room + room_cmdline - height;
4709 /* If there is only a 'winfixheight' window and making the
4710 * window smaller, need to make the other window taller. */
4711 if (take < 0 && room - curfrp->fr_height < room_reserved)
4712 room_reserved = 0;
4714 if (take > 0 && room_cmdline > 0)
4716 /* use lines from cmdline first */
4717 if (take < room_cmdline)
4718 room_cmdline = take;
4719 take -= room_cmdline;
4720 topframe->fr_height += room_cmdline;
4724 * set the current frame to the new height
4726 frame_new_height(curfrp, height, FALSE, FALSE);
4729 * First take lines from the frames after the current frame. If
4730 * that is not enough, takes lines from frames above the current
4731 * frame.
4733 for (run = 0; run < 2; ++run)
4735 if (run == 0)
4736 frp = curfrp->fr_next; /* 1st run: start with next window */
4737 else
4738 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4739 while (frp != NULL && take != 0)
4741 h = frame_minheight(frp, NULL);
4742 if (room_reserved > 0
4743 && frp->fr_win != NULL
4744 && frp->fr_win->w_p_wfh)
4746 if (room_reserved >= frp->fr_height)
4747 room_reserved -= frp->fr_height;
4748 else
4750 if (frp->fr_height - room_reserved > take)
4751 room_reserved = frp->fr_height - take;
4752 take -= frp->fr_height - room_reserved;
4753 frame_new_height(frp, room_reserved, FALSE, FALSE);
4754 room_reserved = 0;
4757 else
4759 if (frp->fr_height - take < h)
4761 take -= frp->fr_height - h;
4762 frame_new_height(frp, h, FALSE, FALSE);
4764 else
4766 frame_new_height(frp, frp->fr_height - take,
4767 FALSE, FALSE);
4768 take = 0;
4771 if (run == 0)
4772 frp = frp->fr_next;
4773 else
4774 frp = frp->fr_prev;
4780 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4782 * Set current window width and take care of repositioning other windows to
4783 * fit around it.
4785 void
4786 win_setwidth(width)
4787 int width;
4789 win_setwidth_win(width, curwin);
4792 void
4793 win_setwidth_win(width, wp)
4794 int width;
4795 win_T *wp;
4797 /* Always keep current window at least one column wide, even when
4798 * 'winminwidth' is zero. */
4799 if (wp == curwin)
4801 if (width < p_wmw)
4802 width = p_wmw;
4803 if (width == 0)
4804 width = 1;
4807 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4809 /* recompute the window positions */
4810 (void)win_comp_pos();
4812 redraw_all_later(NOT_VALID);
4816 * Set the width of a frame to "width" and take care that all frames and
4817 * windows inside it are resized. Also resize frames above and below if the
4818 * are in the same FR_ROW frame.
4820 * Strategy is similar to frame_setheight().
4822 static void
4823 frame_setwidth(curfrp, width)
4824 frame_T *curfrp;
4825 int width;
4827 int room; /* total number of lines available */
4828 int take; /* number of lines taken from other windows */
4829 int run;
4830 frame_T *frp;
4831 int w;
4832 int room_reserved;
4834 /* If the width already is the desired value, nothing to do. */
4835 if (curfrp->fr_width == width)
4836 return;
4838 if (curfrp->fr_parent == NULL)
4839 /* topframe: can't change width */
4840 return;
4842 if (curfrp->fr_parent->fr_layout == FR_COL)
4844 /* Column of frames: Also need to resize frames above and below of
4845 * this one. First check for the minimal width of these. */
4846 w = frame_minwidth(curfrp->fr_parent, NULL);
4847 if (width < w)
4848 width = w;
4849 frame_setwidth(curfrp->fr_parent, width);
4851 else
4854 * Row of frames: try to change only frames in this row.
4856 * Do this twice:
4857 * 1: compute room available, if it's not enough try resizing the
4858 * containing frame.
4859 * 2: compute the room available and adjust the width to it.
4861 for (run = 1; run <= 2; ++run)
4863 room = 0;
4864 room_reserved = 0;
4865 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4866 frp = frp->fr_next)
4868 if (frp != curfrp
4869 && frp->fr_win != NULL
4870 && frp->fr_win->w_p_wfw)
4871 room_reserved += frp->fr_width;
4872 room += frp->fr_width;
4873 if (frp != curfrp)
4874 room -= frame_minwidth(frp, NULL);
4877 if (width <= room)
4878 break;
4879 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4881 if (width > room)
4882 width = room;
4883 break;
4885 frame_setwidth(curfrp->fr_parent, width
4886 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4890 * Compute the number of lines we will take from others frames (can be
4891 * negative!).
4893 take = width - curfrp->fr_width;
4895 /* If there is not enough room, also reduce the width of a window
4896 * with 'winfixwidth' set. */
4897 if (width > room - room_reserved)
4898 room_reserved = room - width;
4899 /* If there is only a 'winfixwidth' window and making the
4900 * window smaller, need to make the other window narrower. */
4901 if (take < 0 && room - curfrp->fr_width < room_reserved)
4902 room_reserved = 0;
4905 * set the current frame to the new width
4907 frame_new_width(curfrp, width, FALSE, FALSE);
4910 * First take lines from the frames right of the current frame. If
4911 * that is not enough, takes lines from frames left of the current
4912 * frame.
4914 for (run = 0; run < 2; ++run)
4916 if (run == 0)
4917 frp = curfrp->fr_next; /* 1st run: start with next window */
4918 else
4919 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4920 while (frp != NULL && take != 0)
4922 w = frame_minwidth(frp, NULL);
4923 if (room_reserved > 0
4924 && frp->fr_win != NULL
4925 && frp->fr_win->w_p_wfw)
4927 if (room_reserved >= frp->fr_width)
4928 room_reserved -= frp->fr_width;
4929 else
4931 if (frp->fr_width - room_reserved > take)
4932 room_reserved = frp->fr_width - take;
4933 take -= frp->fr_width - room_reserved;
4934 frame_new_width(frp, room_reserved, FALSE, FALSE);
4935 room_reserved = 0;
4938 else
4940 if (frp->fr_width - take < w)
4942 take -= frp->fr_width - w;
4943 frame_new_width(frp, w, FALSE, FALSE);
4945 else
4947 frame_new_width(frp, frp->fr_width - take,
4948 FALSE, FALSE);
4949 take = 0;
4952 if (run == 0)
4953 frp = frp->fr_next;
4954 else
4955 frp = frp->fr_prev;
4960 #endif /* FEAT_VERTSPLIT */
4963 * Check 'winminheight' for a valid value.
4965 void
4966 win_setminheight()
4968 int room;
4969 int first = TRUE;
4970 win_T *wp;
4972 /* loop until there is a 'winminheight' that is possible */
4973 while (p_wmh > 0)
4975 /* TODO: handle vertical splits */
4976 room = -p_wh;
4977 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4978 room += wp->w_height - p_wmh;
4979 if (room >= 0)
4980 break;
4981 --p_wmh;
4982 if (first)
4984 EMSG(_(e_noroom));
4985 first = FALSE;
4990 #ifdef FEAT_MOUSE
4993 * Status line of dragwin is dragged "offset" lines down (negative is up).
4995 void
4996 win_drag_status_line(dragwin, offset)
4997 win_T *dragwin;
4998 int offset;
5000 frame_T *curfr;
5001 frame_T *fr;
5002 int room;
5003 int row;
5004 int up; /* if TRUE, drag status line up, otherwise down */
5005 int n;
5007 fr = dragwin->w_frame;
5008 curfr = fr;
5009 if (fr != topframe) /* more than one window */
5011 fr = fr->fr_parent;
5012 /* When the parent frame is not a column of frames, its parent should
5013 * be. */
5014 if (fr->fr_layout != FR_COL)
5016 curfr = fr;
5017 if (fr != topframe) /* only a row of windows, may drag statusline */
5018 fr = fr->fr_parent;
5022 /* If this is the last frame in a column, may want to resize the parent
5023 * frame instead (go two up to skip a row of frames). */
5024 while (curfr != topframe && curfr->fr_next == NULL)
5026 if (fr != topframe)
5027 fr = fr->fr_parent;
5028 curfr = fr;
5029 if (fr != topframe)
5030 fr = fr->fr_parent;
5033 if (offset < 0) /* drag up */
5035 up = TRUE;
5036 offset = -offset;
5037 /* sum up the room of the current frame and above it */
5038 if (fr == curfr)
5040 /* only one window */
5041 room = fr->fr_height - frame_minheight(fr, NULL);
5043 else
5045 room = 0;
5046 for (fr = fr->fr_child; ; fr = fr->fr_next)
5048 room += fr->fr_height - frame_minheight(fr, NULL);
5049 if (fr == curfr)
5050 break;
5053 fr = curfr->fr_next; /* put fr at frame that grows */
5055 else /* drag down */
5057 up = FALSE;
5059 * Only dragging the last status line can reduce p_ch.
5061 room = Rows - cmdline_row;
5062 if (curfr->fr_next == NULL)
5063 room -= 1;
5064 else
5065 room -= p_ch;
5066 if (room < 0)
5067 room = 0;
5068 /* sum up the room of frames below of the current one */
5069 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5070 room += fr->fr_height - frame_minheight(fr, NULL);
5071 fr = curfr; /* put fr at window that grows */
5074 if (room < offset) /* Not enough room */
5075 offset = room; /* Move as far as we can */
5076 if (offset <= 0)
5077 return;
5080 * Grow frame fr by "offset" lines.
5081 * Doesn't happen when dragging the last status line up.
5083 if (fr != NULL)
5084 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5086 if (up)
5087 fr = curfr; /* current frame gets smaller */
5088 else
5089 fr = curfr->fr_next; /* next frame gets smaller */
5092 * Now make the other frames smaller.
5094 while (fr != NULL && offset > 0)
5096 n = frame_minheight(fr, NULL);
5097 if (fr->fr_height - offset <= n)
5099 offset -= fr->fr_height - n;
5100 frame_new_height(fr, n, !up, FALSE);
5102 else
5104 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5105 break;
5107 if (up)
5108 fr = fr->fr_prev;
5109 else
5110 fr = fr->fr_next;
5112 row = win_comp_pos();
5113 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5114 cmdline_row = row;
5115 p_ch = Rows - cmdline_row;
5116 if (p_ch < 1)
5117 p_ch = 1;
5118 curtab->tp_ch_used = p_ch;
5119 redraw_all_later(SOME_VALID);
5120 showmode();
5123 #ifdef FEAT_VERTSPLIT
5125 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5127 void
5128 win_drag_vsep_line(dragwin, offset)
5129 win_T *dragwin;
5130 int offset;
5132 frame_T *curfr;
5133 frame_T *fr;
5134 int room;
5135 int left; /* if TRUE, drag separator line left, otherwise right */
5136 int n;
5138 fr = dragwin->w_frame;
5139 if (fr == topframe) /* only one window (cannot happen?) */
5140 return;
5141 curfr = fr;
5142 fr = fr->fr_parent;
5143 /* When the parent frame is not a row of frames, its parent should be. */
5144 if (fr->fr_layout != FR_ROW)
5146 if (fr == topframe) /* only a column of windows (cannot happen?) */
5147 return;
5148 curfr = fr;
5149 fr = fr->fr_parent;
5152 /* If this is the last frame in a row, may want to resize a parent
5153 * frame instead. */
5154 while (curfr->fr_next == NULL)
5156 if (fr == topframe)
5157 break;
5158 curfr = fr;
5159 fr = fr->fr_parent;
5160 if (fr != topframe)
5162 curfr = fr;
5163 fr = fr->fr_parent;
5167 if (offset < 0) /* drag left */
5169 left = TRUE;
5170 offset = -offset;
5171 /* sum up the room of the current frame and left of it */
5172 room = 0;
5173 for (fr = fr->fr_child; ; fr = fr->fr_next)
5175 room += fr->fr_width - frame_minwidth(fr, NULL);
5176 if (fr == curfr)
5177 break;
5179 fr = curfr->fr_next; /* put fr at frame that grows */
5181 else /* drag right */
5183 left = FALSE;
5184 /* sum up the room of frames right of the current one */
5185 room = 0;
5186 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5187 room += fr->fr_width - frame_minwidth(fr, NULL);
5188 fr = curfr; /* put fr at window that grows */
5191 if (room < offset) /* Not enough room */
5192 offset = room; /* Move as far as we can */
5193 if (offset <= 0) /* No room at all, quit. */
5194 return;
5196 /* grow frame fr by offset lines */
5197 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5199 /* shrink other frames: current and at the left or at the right */
5200 if (left)
5201 fr = curfr; /* current frame gets smaller */
5202 else
5203 fr = curfr->fr_next; /* next frame gets smaller */
5205 while (fr != NULL && offset > 0)
5207 n = frame_minwidth(fr, NULL);
5208 if (fr->fr_width - offset <= n)
5210 offset -= fr->fr_width - n;
5211 frame_new_width(fr, n, !left, FALSE);
5213 else
5215 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5216 break;
5218 if (left)
5219 fr = fr->fr_prev;
5220 else
5221 fr = fr->fr_next;
5223 (void)win_comp_pos();
5224 redraw_all_later(NOT_VALID);
5226 #endif /* FEAT_VERTSPLIT */
5227 #endif /* FEAT_MOUSE */
5229 #endif /* FEAT_WINDOWS */
5232 * Set the height of a window.
5233 * This takes care of the things inside the window, not what happens to the
5234 * window position, the frame or to other windows.
5236 static void
5237 win_new_height(wp, height)
5238 win_T *wp;
5239 int height;
5241 linenr_T lnum;
5242 int sline, line_size;
5243 #define FRACTION_MULT 16384L
5245 /* Don't want a negative height. Happens when splitting a tiny window.
5246 * Will equalize heights soon to fix it. */
5247 if (height < 0)
5248 height = 0;
5249 if (wp->w_height == height)
5250 return; /* nothing to do */
5252 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5253 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5254 + FRACTION_MULT / 2) / (long)wp->w_height;
5256 wp->w_height = height;
5257 wp->w_skipcol = 0;
5259 /* Don't change w_topline when height is zero. Don't set w_topline when
5260 * 'scrollbind' is set and this isn't the current window. */
5261 if (height > 0
5262 #ifdef FEAT_SCROLLBIND
5263 && (!wp->w_p_scb || wp == curwin)
5264 #endif
5268 * Find a value for w_topline that shows the cursor at the same
5269 * relative position in the window as before (more or less).
5271 lnum = wp->w_cursor.lnum;
5272 if (lnum < 1) /* can happen when starting up */
5273 lnum = 1;
5274 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5275 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5276 sline = wp->w_wrow - line_size;
5278 if (sline >= 0)
5280 /* Make sure the whole cursor line is visible, if possible. */
5281 int rows = plines_win(wp, lnum, FALSE);
5283 if (sline > wp->w_height - rows)
5285 sline = wp->w_height - rows;
5286 wp->w_wrow -= rows - line_size;
5290 if (sline < 0)
5293 * Cursor line would go off top of screen if w_wrow was this high.
5294 * Make cursor line the first line in the window. If not enough
5295 * room use w_skipcol;
5297 wp->w_wrow = line_size;
5298 if (wp->w_wrow >= wp->w_height
5299 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5301 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5302 --wp->w_wrow;
5303 while (wp->w_wrow >= wp->w_height)
5305 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5306 + win_col_off2(wp);
5307 --wp->w_wrow;
5311 else
5313 while (sline > 0 && lnum > 1)
5315 #ifdef FEAT_FOLDING
5316 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5317 if (lnum == 1)
5319 /* first line in buffer is folded */
5320 line_size = 1;
5321 --sline;
5322 break;
5324 #endif
5325 --lnum;
5326 #ifdef FEAT_DIFF
5327 if (lnum == wp->w_topline)
5328 line_size = plines_win_nofill(wp, lnum, TRUE)
5329 + wp->w_topfill;
5330 else
5331 #endif
5332 line_size = plines_win(wp, lnum, TRUE);
5333 sline -= line_size;
5336 if (sline < 0)
5339 * Line we want at top would go off top of screen. Use next
5340 * line instead.
5342 #ifdef FEAT_FOLDING
5343 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5344 #endif
5345 lnum++;
5346 wp->w_wrow -= line_size + sline;
5348 else if (sline > 0)
5350 /* First line of file reached, use that as topline. */
5351 lnum = 1;
5352 wp->w_wrow -= sline;
5355 set_topline(wp, lnum);
5358 if (wp == curwin)
5360 if (p_so)
5361 update_topline();
5362 curs_columns(FALSE); /* validate w_wrow */
5364 wp->w_prev_fraction_row = wp->w_wrow;
5366 win_comp_scroll(wp);
5367 redraw_win_later(wp, SOME_VALID);
5368 #ifdef FEAT_WINDOWS
5369 wp->w_redr_status = TRUE;
5370 #endif
5371 invalidate_botline_win(wp);
5374 #ifdef FEAT_VERTSPLIT
5376 * Set the width of a window.
5378 static void
5379 win_new_width(wp, width)
5380 win_T *wp;
5381 int width;
5383 wp->w_width = width;
5384 wp->w_lines_valid = 0;
5385 changed_line_abv_curs_win(wp);
5386 invalidate_botline_win(wp);
5387 if (wp == curwin)
5389 update_topline();
5390 curs_columns(TRUE); /* validate w_wrow */
5392 redraw_win_later(wp, NOT_VALID);
5393 wp->w_redr_status = TRUE;
5395 #endif
5397 void
5398 win_comp_scroll(wp)
5399 win_T *wp;
5401 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5402 if (wp->w_p_scr == 0)
5403 wp->w_p_scr = 1;
5407 * command_height: called whenever p_ch has been changed
5409 void
5410 command_height()
5412 #ifdef FEAT_WINDOWS
5413 int h;
5414 frame_T *frp;
5415 int old_p_ch = curtab->tp_ch_used;
5417 /* Use the value of p_ch that we remembered. This is needed for when the
5418 * GUI starts up, we can't be sure in what order things happen. And when
5419 * p_ch was changed in another tab page. */
5420 curtab->tp_ch_used = p_ch;
5422 /* Find bottom frame with width of screen. */
5423 frp = lastwin->w_frame;
5424 # ifdef FEAT_VERTSPLIT
5425 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5426 frp = frp->fr_parent;
5427 # endif
5429 /* Avoid changing the height of a window with 'winfixheight' set. */
5430 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5431 && frp->fr_win->w_p_wfh)
5432 frp = frp->fr_prev;
5434 if (starting != NO_SCREEN)
5436 cmdline_row = Rows - p_ch;
5438 if (p_ch > old_p_ch) /* p_ch got bigger */
5440 while (p_ch > old_p_ch)
5442 if (frp == NULL)
5444 EMSG(_(e_noroom));
5445 p_ch = old_p_ch;
5446 cmdline_row = Rows - p_ch;
5447 break;
5449 h = frp->fr_height - frame_minheight(frp, NULL);
5450 if (h > p_ch - old_p_ch)
5451 h = p_ch - old_p_ch;
5452 old_p_ch += h;
5453 frame_add_height(frp, -h);
5454 frp = frp->fr_prev;
5457 /* Recompute window positions. */
5458 (void)win_comp_pos();
5460 /* clear the lines added to cmdline */
5461 if (full_screen)
5462 screen_fill((int)(cmdline_row), (int)Rows, 0,
5463 (int)Columns, ' ', ' ', 0);
5464 msg_row = cmdline_row;
5465 redraw_cmdline = TRUE;
5466 return;
5469 if (msg_row < cmdline_row)
5470 msg_row = cmdline_row;
5471 redraw_cmdline = TRUE;
5473 frame_add_height(frp, (int)(old_p_ch - p_ch));
5475 /* Recompute window positions. */
5476 if (frp != lastwin->w_frame)
5477 (void)win_comp_pos();
5478 #else
5479 cmdline_row = Rows - p_ch;
5480 win_setheight(cmdline_row);
5481 #endif
5484 #if defined(FEAT_WINDOWS) || defined(PROTO)
5486 * Resize frame "frp" to be "n" lines higher (negative for less high).
5487 * Also resize the frames it is contained in.
5489 static void
5490 frame_add_height(frp, n)
5491 frame_T *frp;
5492 int n;
5494 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5495 for (;;)
5497 frp = frp->fr_parent;
5498 if (frp == NULL)
5499 break;
5500 frp->fr_height += n;
5505 * Add or remove a status line for the bottom window(s), according to the
5506 * value of 'laststatus'.
5508 void
5509 last_status(morewin)
5510 int morewin; /* pretend there are two or more windows */
5512 /* Don't make a difference between horizontal or vertical split. */
5513 last_status_rec(topframe, (p_ls == 2
5514 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5517 static void
5518 last_status_rec(fr, statusline)
5519 frame_T *fr;
5520 int statusline;
5522 frame_T *fp;
5523 win_T *wp;
5525 if (fr->fr_layout == FR_LEAF)
5527 wp = fr->fr_win;
5528 if (wp->w_status_height != 0 && !statusline)
5530 /* remove status line */
5531 win_new_height(wp, wp->w_height + 1);
5532 wp->w_status_height = 0;
5533 comp_col();
5535 else if (wp->w_status_height == 0 && statusline)
5537 /* Find a frame to take a line from. */
5538 fp = fr;
5539 while (fp->fr_height <= frame_minheight(fp, NULL))
5541 if (fp == topframe)
5543 EMSG(_(e_noroom));
5544 return;
5546 /* In a column of frames: go to frame above. If already at
5547 * the top or in a row of frames: go to parent. */
5548 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5549 fp = fp->fr_prev;
5550 else
5551 fp = fp->fr_parent;
5553 wp->w_status_height = 1;
5554 if (fp != fr)
5556 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5557 frame_fix_height(wp);
5558 (void)win_comp_pos();
5560 else
5561 win_new_height(wp, wp->w_height - 1);
5562 comp_col();
5563 redraw_all_later(SOME_VALID);
5566 #ifdef FEAT_VERTSPLIT
5567 else if (fr->fr_layout == FR_ROW)
5569 /* vertically split windows, set status line for each one */
5570 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5571 last_status_rec(fp, statusline);
5573 #endif
5574 else
5576 /* horizontally split window, set status line for last one */
5577 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5579 last_status_rec(fp, statusline);
5584 * Return the number of lines used by the tab page line.
5587 tabline_height()
5589 #ifdef FEAT_GUI_TABLINE
5590 /* When the GUI has the tabline then this always returns zero. */
5591 if (gui_use_tabline())
5592 return 0;
5593 #endif
5594 switch (p_stal)
5596 case 0: return 0;
5597 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5599 return 1;
5602 #endif /* FEAT_WINDOWS */
5604 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5606 * Get the file name at the cursor.
5607 * If Visual mode is active, use the selected text if it's in one line.
5608 * Returns the name in allocated memory, NULL for failure.
5610 char_u *
5611 grab_file_name(count, file_lnum)
5612 long count;
5613 linenr_T *file_lnum;
5615 # ifdef FEAT_VISUAL
5616 if (VIsual_active)
5618 int len;
5619 char_u *ptr;
5621 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5622 return NULL;
5623 return find_file_name_in_path(ptr, len,
5624 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5626 # endif
5627 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5628 file_lnum);
5633 * Return the file name under or after the cursor.
5635 * The 'path' option is searched if the file name is not absolute.
5636 * The string returned has been alloc'ed and should be freed by the caller.
5637 * NULL is returned if the file name or file is not found.
5639 * options:
5640 * FNAME_MESS give error messages
5641 * FNAME_EXP expand to path
5642 * FNAME_HYP check for hypertext link
5643 * FNAME_INCL apply "includeexpr"
5645 char_u *
5646 file_name_at_cursor(options, count, file_lnum)
5647 int options;
5648 long count;
5649 linenr_T *file_lnum;
5651 return file_name_in_line(ml_get_curline(),
5652 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5653 file_lnum);
5657 * Return the name of the file under or after ptr[col].
5658 * Otherwise like file_name_at_cursor().
5660 char_u *
5661 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5662 char_u *line;
5663 int col;
5664 int options;
5665 long count;
5666 char_u *rel_fname; /* file we are searching relative to */
5667 linenr_T *file_lnum; /* line number after the file name */
5669 char_u *ptr;
5670 int len;
5673 * search forward for what could be the start of a file name
5675 ptr = line + col;
5676 while (*ptr != NUL && !vim_isfilec(*ptr))
5677 mb_ptr_adv(ptr);
5678 if (*ptr == NUL) /* nothing found */
5680 if (options & FNAME_MESS)
5681 EMSG(_("E446: No file name under cursor"));
5682 return NULL;
5686 * Search backward for first char of the file name.
5687 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5689 while (ptr > line)
5691 #ifdef FEAT_MBYTE
5692 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5693 ptr -= len + 1;
5694 else
5695 #endif
5696 if (vim_isfilec(ptr[-1])
5697 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5698 --ptr;
5699 else
5700 break;
5704 * Search forward for the last char of the file name.
5705 * Also allow "://" when ':' is not in 'isfname'.
5707 len = 0;
5708 while (vim_isfilec(ptr[len])
5709 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5710 #ifdef FEAT_MBYTE
5711 if (has_mbyte)
5712 len += (*mb_ptr2len)(ptr + len);
5713 else
5714 #endif
5715 ++len;
5718 * If there is trailing punctuation, remove it.
5719 * But don't remove "..", could be a directory name.
5721 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5722 && ptr[len - 2] != '.')
5723 --len;
5725 if (file_lnum != NULL)
5727 char_u *p;
5729 /* Get the number after the file name and a separator character */
5730 p = ptr + len;
5731 p = skipwhite(p);
5732 if (*p != NUL)
5734 if (!isdigit(*p))
5735 ++p; /* skip the separator */
5736 p = skipwhite(p);
5737 if (isdigit(*p))
5738 *file_lnum = (int)getdigits(&p);
5742 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5745 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5746 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5748 static char_u *
5749 eval_includeexpr(ptr, len)
5750 char_u *ptr;
5751 int len;
5753 char_u *res;
5755 set_vim_var_string(VV_FNAME, ptr, len);
5756 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5757 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5758 set_vim_var_string(VV_FNAME, NULL, 0);
5759 return res;
5761 #endif
5764 * Return the name of the file ptr[len] in 'path'.
5765 * Otherwise like file_name_at_cursor().
5767 char_u *
5768 find_file_name_in_path(ptr, len, options, count, rel_fname)
5769 char_u *ptr;
5770 int len;
5771 int options;
5772 long count;
5773 char_u *rel_fname; /* file we are searching relative to */
5775 char_u *file_name;
5776 int c;
5777 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5778 char_u *tofree = NULL;
5780 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5782 tofree = eval_includeexpr(ptr, len);
5783 if (tofree != NULL)
5785 ptr = tofree;
5786 len = (int)STRLEN(ptr);
5789 # endif
5791 if (options & FNAME_EXP)
5793 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5794 TRUE, rel_fname);
5796 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5798 * If the file could not be found in a normal way, try applying
5799 * 'includeexpr' (unless done already).
5801 if (file_name == NULL
5802 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5804 tofree = eval_includeexpr(ptr, len);
5805 if (tofree != NULL)
5807 ptr = tofree;
5808 len = (int)STRLEN(ptr);
5809 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5810 TRUE, rel_fname);
5813 # endif
5814 if (file_name == NULL && (options & FNAME_MESS))
5816 c = ptr[len];
5817 ptr[len] = NUL;
5818 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5819 ptr[len] = c;
5822 /* Repeat finding the file "count" times. This matters when it
5823 * appears several times in the path. */
5824 while (file_name != NULL && --count > 0)
5826 vim_free(file_name);
5827 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5830 else
5831 file_name = vim_strnsave(ptr, len);
5833 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5834 vim_free(tofree);
5835 # endif
5837 return file_name;
5839 #endif /* FEAT_SEARCHPATH */
5842 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5843 * Also check for ":\\", which MS Internet Explorer accepts, return
5844 * URL_BACKSLASH.
5846 static int
5847 path_is_url(p)
5848 char_u *p;
5850 if (STRNCMP(p, "://", (size_t)3) == 0)
5851 return URL_SLASH;
5852 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5853 return URL_BACKSLASH;
5854 return 0;
5858 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5859 * Return URL_BACKSLASH for "name:\\".
5860 * Return zero otherwise.
5863 path_with_url(fname)
5864 char_u *fname;
5866 char_u *p;
5868 for (p = fname; isalpha(*p); ++p)
5870 return path_is_url(p);
5874 * Return TRUE if "name" is a full (absolute) path name or URL.
5877 vim_isAbsName(name)
5878 char_u *name;
5880 return (path_with_url(name) != 0 || mch_isFullName(name));
5884 * Get absolute file name into buffer "buf[len]".
5886 * return FAIL for failure, OK otherwise
5889 vim_FullName(fname, buf, len, force)
5890 char_u *fname, *buf;
5891 int len;
5892 int force; /* force expansion even when already absolute */
5894 int retval = OK;
5895 int url;
5897 *buf = NUL;
5898 if (fname == NULL)
5899 return FAIL;
5901 url = path_with_url(fname);
5902 if (!url)
5903 retval = mch_FullName(fname, buf, len, force);
5904 if (url || retval == FAIL)
5906 /* something failed; use the file name (truncate when too long) */
5907 vim_strncpy(buf, fname, len - 1);
5909 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5910 slash_adjust(buf);
5911 #endif
5912 return retval;
5916 * Return the minimal number of rows that is needed on the screen to display
5917 * the current number of windows.
5920 min_rows()
5922 int total;
5923 #ifdef FEAT_WINDOWS
5924 tabpage_T *tp;
5925 int n;
5926 #endif
5928 if (firstwin == NULL) /* not initialized yet */
5929 return MIN_LINES;
5931 #ifdef FEAT_WINDOWS
5932 total = 0;
5933 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5935 n = frame_minheight(tp->tp_topframe, NULL);
5936 if (total < n)
5937 total = n;
5939 total += tabline_height();
5940 #else
5941 total = 1; /* at least one window should have a line! */
5942 #endif
5943 total += 1; /* count the room for the command line */
5944 return total;
5948 * Return TRUE if there is only one window (in the current tab page), not
5949 * counting a help or preview window, unless it is the current window.
5952 only_one_window()
5954 #ifdef FEAT_WINDOWS
5955 int count = 0;
5956 win_T *wp;
5958 /* If there is another tab page there always is another window. */
5959 if (first_tabpage->tp_next != NULL)
5960 return FALSE;
5962 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5963 if (!((wp->w_buffer->b_help && !curbuf->b_help)
5964 # ifdef FEAT_QUICKFIX
5965 || wp->w_p_pvw
5966 # endif
5967 ) || wp == curwin)
5968 ++count;
5969 return (count <= 1);
5970 #else
5971 return TRUE;
5972 #endif
5975 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5977 * Correct the cursor line number in other windows. Used after changing the
5978 * current buffer, and before applying autocommands.
5979 * When "do_curwin" is TRUE, also check current window.
5981 void
5982 check_lnums(do_curwin)
5983 int do_curwin;
5985 win_T *wp;
5987 #ifdef FEAT_WINDOWS
5988 tabpage_T *tp;
5990 FOR_ALL_TAB_WINDOWS(tp, wp)
5991 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5992 #else
5993 wp = curwin;
5994 if (do_curwin)
5995 #endif
5997 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5998 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5999 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6000 wp->w_topline = curbuf->b_ml.ml_line_count;
6003 #endif
6005 #if defined(FEAT_WINDOWS) || defined(PROTO)
6008 * A snapshot of the window sizes, to restore them after closing the help
6009 * window.
6010 * Only these fields are used:
6011 * fr_layout
6012 * fr_width
6013 * fr_height
6014 * fr_next
6015 * fr_child
6016 * fr_win (only valid for the old curwin, NULL otherwise)
6020 * Create a snapshot of the current frame sizes.
6022 static void
6023 make_snapshot()
6025 clear_snapshot(curtab);
6026 make_snapshot_rec(topframe, &curtab->tp_snapshot);
6029 static void
6030 make_snapshot_rec(fr, frp)
6031 frame_T *fr;
6032 frame_T **frp;
6034 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6035 if (*frp == NULL)
6036 return;
6037 (*frp)->fr_layout = fr->fr_layout;
6038 # ifdef FEAT_VERTSPLIT
6039 (*frp)->fr_width = fr->fr_width;
6040 # endif
6041 (*frp)->fr_height = fr->fr_height;
6042 if (fr->fr_next != NULL)
6043 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6044 if (fr->fr_child != NULL)
6045 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6046 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6047 (*frp)->fr_win = curwin;
6051 * Remove any existing snapshot.
6053 static void
6054 clear_snapshot(tp)
6055 tabpage_T *tp;
6057 clear_snapshot_rec(tp->tp_snapshot);
6058 tp->tp_snapshot = NULL;
6061 static void
6062 clear_snapshot_rec(fr)
6063 frame_T *fr;
6065 if (fr != NULL)
6067 clear_snapshot_rec(fr->fr_next);
6068 clear_snapshot_rec(fr->fr_child);
6069 vim_free(fr);
6074 * Restore a previously created snapshot, if there is any.
6075 * This is only done if the screen size didn't change and the window layout is
6076 * still the same.
6078 static void
6079 restore_snapshot(close_curwin)
6080 int close_curwin; /* closing current window */
6082 win_T *wp;
6084 if (curtab->tp_snapshot != NULL
6085 # ifdef FEAT_VERTSPLIT
6086 && curtab->tp_snapshot->fr_width == topframe->fr_width
6087 # endif
6088 && curtab->tp_snapshot->fr_height == topframe->fr_height
6089 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
6091 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
6092 win_comp_pos();
6093 if (wp != NULL && close_curwin)
6094 win_goto(wp);
6095 redraw_all_later(CLEAR);
6097 clear_snapshot(curtab);
6101 * Check if frames "sn" and "fr" have the same layout, same following frames
6102 * and same children.
6104 static int
6105 check_snapshot_rec(sn, fr)
6106 frame_T *sn;
6107 frame_T *fr;
6109 if (sn->fr_layout != fr->fr_layout
6110 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6111 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6112 || (sn->fr_next != NULL
6113 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6114 || (sn->fr_child != NULL
6115 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6116 return FAIL;
6117 return OK;
6121 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6122 * following frames and children.
6123 * Returns a pointer to the old current window, or NULL.
6125 static win_T *
6126 restore_snapshot_rec(sn, fr)
6127 frame_T *sn;
6128 frame_T *fr;
6130 win_T *wp = NULL;
6131 win_T *wp2;
6133 fr->fr_height = sn->fr_height;
6134 # ifdef FEAT_VERTSPLIT
6135 fr->fr_width = sn->fr_width;
6136 # endif
6137 if (fr->fr_layout == FR_LEAF)
6139 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6140 # ifdef FEAT_VERTSPLIT
6141 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6142 # endif
6143 wp = sn->fr_win;
6145 if (sn->fr_next != NULL)
6147 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6148 if (wp2 != NULL)
6149 wp = wp2;
6151 if (sn->fr_child != NULL)
6153 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6154 if (wp2 != NULL)
6155 wp = wp2;
6157 return wp;
6160 #endif
6162 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6164 * Return TRUE if there is any vertically split window.
6167 win_hasvertsplit()
6169 frame_T *fr;
6171 if (topframe->fr_layout == FR_ROW)
6172 return TRUE;
6174 if (topframe->fr_layout == FR_COL)
6175 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6176 if (fr->fr_layout == FR_ROW)
6177 return TRUE;
6179 return FALSE;
6181 #endif
6183 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6185 * Add match to the match list of window 'wp'. The pattern 'pat' will be
6186 * highligted with the group 'grp' with priority 'prio'.
6187 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6188 * If no particular ID is desired, -1 must be specified for 'id'.
6189 * Return ID of added match, -1 on failure.
6192 match_add(wp, grp, pat, prio, id)
6193 win_T *wp;
6194 char_u *grp;
6195 char_u *pat;
6196 int prio;
6197 int id;
6199 matchitem_T *cur;
6200 matchitem_T *prev;
6201 matchitem_T *m;
6202 int hlg_id;
6203 regprog_T *regprog;
6205 if (*grp == NUL || *pat == NUL)
6206 return -1;
6207 if (id < -1 || id == 0)
6209 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6210 return -1;
6212 if (id != -1)
6214 cur = wp->w_match_head;
6215 while (cur != NULL)
6217 if (cur->id == id)
6219 EMSGN("E801: ID already taken: %ld", id);
6220 return -1;
6222 cur = cur->next;
6225 if ((hlg_id = syn_namen2id(grp, STRLEN(grp))) == 0)
6227 EMSG2(_(e_nogroup), grp);
6228 return -1;
6230 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6232 EMSG2(_(e_invarg2), pat);
6233 return -1;
6236 /* Find available match ID. */
6237 while (id == -1)
6239 cur = wp->w_match_head;
6240 while (cur != NULL && cur->id != wp->w_next_match_id)
6241 cur = cur->next;
6242 if (cur == NULL)
6243 id = wp->w_next_match_id;
6244 wp->w_next_match_id++;
6247 /* Build new match. */
6248 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6249 m->id = id;
6250 m->priority = prio;
6251 m->pattern = vim_strsave(pat);
6252 m->hlg_id = hlg_id;
6253 m->match.regprog = regprog;
6254 m->match.rmm_ic = FALSE;
6255 m->match.rmm_maxcol = 0;
6257 /* Insert new match. The match list is in ascending order with regard to
6258 * the match priorities. */
6259 cur = wp->w_match_head;
6260 prev = cur;
6261 while (cur != NULL && prio >= cur->priority)
6263 prev = cur;
6264 cur = cur->next;
6266 if (cur == prev)
6267 wp->w_match_head = m;
6268 else
6269 prev->next = m;
6270 m->next = cur;
6272 redraw_later(SOME_VALID);
6273 return id;
6277 * Delete match with ID 'id' in the match list of window 'wp'.
6278 * Print error messages if 'perr' is TRUE.
6281 match_delete(wp, id, perr)
6282 win_T *wp;
6283 int id;
6284 int perr;
6286 matchitem_T *cur = wp->w_match_head;
6287 matchitem_T *prev = cur;
6289 if (id < 1)
6291 if (perr == TRUE)
6292 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6293 id);
6294 return -1;
6296 while (cur != NULL && cur->id != id)
6298 prev = cur;
6299 cur = cur->next;
6301 if (cur == NULL)
6303 if (perr == TRUE)
6304 EMSGN("E803: ID not found: %ld", id);
6305 return -1;
6307 if (cur == prev)
6308 wp->w_match_head = cur->next;
6309 else
6310 prev->next = cur->next;
6311 vim_free(cur->match.regprog);
6312 vim_free(cur->pattern);
6313 vim_free(cur);
6314 redraw_later(SOME_VALID);
6315 return 0;
6319 * Delete all matches in the match list of window 'wp'.
6321 void
6322 clear_matches(wp)
6323 win_T *wp;
6325 matchitem_T *m;
6327 while (wp->w_match_head != NULL)
6329 m = wp->w_match_head->next;
6330 vim_free(wp->w_match_head->match.regprog);
6331 vim_free(wp->w_match_head->pattern);
6332 vim_free(wp->w_match_head);
6333 wp->w_match_head = m;
6335 redraw_later(SOME_VALID);
6339 * Get match from ID 'id' in window 'wp'.
6340 * Return NULL if match not found.
6342 matchitem_T *
6343 get_match(wp, id)
6344 win_T *wp;
6345 int id;
6347 matchitem_T *cur = wp->w_match_head;
6349 while (cur != NULL && cur->id != id)
6350 cur = cur->next;
6351 return cur;
6353 #endif