blob: 47da663b5f57f81d9f33d59faf7f02c05966b4bd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 */
9
10#include "vim.h"
11
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
19static int win_comp_pos __ARGS((void));
20static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
29static win_T *winframe_remove __ARGS((win_T *win, int *dirp));
30static frame_T *win_altframe __ARGS((win_T *win));
31static win_T *frame2win __ARGS((frame_T *frp));
32static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
33static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
34static int frame_fixed_height __ARGS((frame_T *frp));
35#ifdef FEAT_VERTSPLIT
36static void frame_add_statusline __ARGS((frame_T *frp));
37static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst));
38static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
42static void frame_fix_height __ARGS((win_T *wp));
43static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
45static void win_free __ARGS((win_T *wp));
46static void win_append __ARGS((win_T *, win_T *));
47static void win_remove __ARGS((win_T *));
48static void frame_append __ARGS((frame_T *after, frame_T *frp));
49static void frame_insert __ARGS((frame_T *before, frame_T *frp));
50static void frame_remove __ARGS((frame_T *frp));
51#ifdef FEAT_VERTSPLIT
52static void win_new_width __ARGS((win_T *wp, int width));
53static int win_minheight __ARGS((win_T *wp));
54static void win_goto_ver __ARGS((int up, long count));
55static void win_goto_hor __ARGS((int left, long count));
56#endif
57static void frame_add_height __ARGS((frame_T *frp, int n));
58static void last_status_rec __ARGS((frame_T *fr, int statusline));
59
60static void make_snapshot __ARGS((void));
61static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
62static void clear_snapshot __ARGS((void));
63static void clear_snapshot_rec __ARGS((frame_T *fr));
64static void restore_snapshot __ARGS((int close_curwin));
65static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
66static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
67
68#endif /* FEAT_WINDOWS */
69static win_T *win_alloc __ARGS((win_T *after));
70static void win_new_height __ARGS((win_T *, int));
71
72#define URL_SLASH 1 /* path_is_url() has found "://" */
73#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
74
75#define NOWIN (win_T *)-1 /* non-exisiting window */
76
77#if defined(FEAT_WINDOWS) || defined(PROTO)
78/*
79 * all CTRL-W window commands are handled here, called from normal_cmd().
80 */
81 void
82do_window(nchar, Prenum, xchar)
83 int nchar;
84 long Prenum;
85 int xchar; /* extra char from ":wincmd gx" or NUL */
86{
87 long Prenum1;
88 win_T *wp;
89#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
90 char_u *ptr;
91#endif
92#ifdef FEAT_FIND_ID
93 int type = FIND_DEFINE;
94 int len;
95#endif
96 char_u cbuf[40];
97
98 if (Prenum == 0)
99 Prenum1 = 1;
100 else
101 Prenum1 = Prenum;
102
103#ifdef FEAT_CMDWIN
104# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
105#else
106# define CHECK_CMDWIN
107#endif
108
109 switch (nchar)
110 {
111/* split current window in two parts, horizontally */
112 case 'S':
113 case Ctrl_S:
114 case 's':
115 CHECK_CMDWIN
116#ifdef FEAT_VISUAL
117 reset_VIsual_and_resel(); /* stop Visual mode */
118#endif
119#ifdef FEAT_GUI
120 need_mouse_correct = TRUE;
121#endif
122 win_split((int)Prenum, 0);
123 break;
124
125#ifdef FEAT_VERTSPLIT
126/* split current window in two parts, vertically */
127 case Ctrl_V:
128 case 'v':
129 CHECK_CMDWIN
130#ifdef FEAT_VISUAL
131 reset_VIsual_and_resel(); /* stop Visual mode */
132#endif
133#ifdef FEAT_GUI
134 need_mouse_correct = TRUE;
135#endif
136 win_split((int)Prenum, WSP_VERT);
137 break;
138#endif
139
140/* split current window and edit alternate file */
141 case Ctrl_HAT:
142 case '^':
143 CHECK_CMDWIN
144#ifdef FEAT_VISUAL
145 reset_VIsual_and_resel(); /* stop Visual mode */
146#endif
147 STRCPY(cbuf, "split #");
148 if (Prenum)
149 sprintf((char *)cbuf + 7, "%ld", Prenum);
150 do_cmdline_cmd(cbuf);
151 break;
152
153/* open new window */
154 case Ctrl_N:
155 case 'n':
156 CHECK_CMDWIN
157#ifdef FEAT_VISUAL
158 reset_VIsual_and_resel(); /* stop Visual mode */
159#endif
160 if (Prenum)
161 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
162 else
163 cbuf[0] = NUL;
164 STRCAT(cbuf, "new");
165 do_cmdline_cmd(cbuf);
166 break;
167
168/* quit current window */
169 case Ctrl_Q:
170 case 'q':
171#ifdef FEAT_VISUAL
172 reset_VIsual_and_resel(); /* stop Visual mode */
173#endif
174 do_cmdline_cmd((char_u *)"quit");
175 break;
176
177/* close current window */
178 case Ctrl_C:
179 case 'c':
180#ifdef FEAT_VISUAL
181 reset_VIsual_and_resel(); /* stop Visual mode */
182#endif
183 do_cmdline_cmd((char_u *)"close");
184 break;
185
186#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
187/* close preview window */
188 case Ctrl_Z:
189 case 'z':
190 CHECK_CMDWIN
191#ifdef FEAT_VISUAL
192 reset_VIsual_and_resel(); /* stop Visual mode */
193#endif
194 do_cmdline_cmd((char_u *)"pclose");
195 break;
196
197/* cursor to preview window */
198 case 'P':
199 for (wp = firstwin; wp != NULL; wp = wp->w_next)
200 if (wp->w_p_pvw)
201 break;
202 if (wp == NULL)
203 EMSG(_("E441: There is no preview window"));
204 else
205 win_goto(wp);
206 break;
207#endif
208
209/* close all but current window */
210 case Ctrl_O:
211 case 'o':
212 CHECK_CMDWIN
213#ifdef FEAT_VISUAL
214 reset_VIsual_and_resel(); /* stop Visual mode */
215#endif
216 do_cmdline_cmd((char_u *)"only");
217 break;
218
219/* cursor to next window with wrap around */
220 case Ctrl_W:
221 case 'w':
222/* cursor to previous window with wrap around */
223 case 'W':
224 CHECK_CMDWIN
225 if (lastwin == firstwin && Prenum != 1) /* just one window */
226 beep_flush();
227 else
228 {
229 if (Prenum) /* go to specified window */
230 {
231 for (wp = firstwin; --Prenum > 0; )
232 {
233 if (wp->w_next == NULL)
234 break;
235 else
236 wp = wp->w_next;
237 }
238 }
239 else
240 {
241 if (nchar == 'W') /* go to previous window */
242 {
243 wp = curwin->w_prev;
244 if (wp == NULL)
245 wp = lastwin; /* wrap around */
246 }
247 else /* go to next window */
248 {
249 wp = curwin->w_next;
250 if (wp == NULL)
251 wp = firstwin; /* wrap around */
252 }
253 }
254 win_goto(wp);
255 }
256 break;
257
258/* cursor to window below */
259 case 'j':
260 case K_DOWN:
261 case Ctrl_J:
262 CHECK_CMDWIN
263#ifdef FEAT_VERTSPLIT
264 win_goto_ver(FALSE, Prenum1);
265#else
266 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
267 wp = wp->w_next)
268 ;
269 win_goto(wp);
270#endif
271 break;
272
273/* cursor to window above */
274 case 'k':
275 case K_UP:
276 case Ctrl_K:
277 CHECK_CMDWIN
278#ifdef FEAT_VERTSPLIT
279 win_goto_ver(TRUE, Prenum1);
280#else
281 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
282 wp = wp->w_prev)
283 ;
284 win_goto(wp);
285#endif
286 break;
287
288#ifdef FEAT_VERTSPLIT
289/* cursor to left window */
290 case 'h':
291 case K_LEFT:
292 case Ctrl_H:
293 case K_BS:
294 CHECK_CMDWIN
295 win_goto_hor(TRUE, Prenum1);
296 break;
297
298/* cursor to right window */
299 case 'l':
300 case K_RIGHT:
301 case Ctrl_L:
302 CHECK_CMDWIN
303 win_goto_hor(FALSE, Prenum1);
304 break;
305#endif
306
307/* cursor to top-left window */
308 case 't':
309 case Ctrl_T:
310 win_goto(firstwin);
311 break;
312
313/* cursor to bottom-right window */
314 case 'b':
315 case Ctrl_B:
316 win_goto(lastwin);
317 break;
318
319/* cursor to last accessed (previous) window */
320 case 'p':
321 case Ctrl_P:
322 if (prevwin == NULL)
323 beep_flush();
324 else
325 win_goto(prevwin);
326 break;
327
328/* exchange current and next window */
329 case 'x':
330 case Ctrl_X:
331 CHECK_CMDWIN
332 win_exchange(Prenum);
333 break;
334
335/* rotate windows downwards */
336 case Ctrl_R:
337 case 'r':
338 CHECK_CMDWIN
339#ifdef FEAT_VISUAL
340 reset_VIsual_and_resel(); /* stop Visual mode */
341#endif
342 win_rotate(FALSE, (int)Prenum1); /* downwards */
343 break;
344
345/* rotate windows upwards */
346 case 'R':
347 CHECK_CMDWIN
348#ifdef FEAT_VISUAL
349 reset_VIsual_and_resel(); /* stop Visual mode */
350#endif
351 win_rotate(TRUE, (int)Prenum1); /* upwards */
352 break;
353
354/* move window to the very top/bottom/left/right */
355 case 'K':
356 case 'J':
357#ifdef FEAT_VERTSPLIT
358 case 'H':
359 case 'L':
360#endif
361 CHECK_CMDWIN
362 win_totop((int)Prenum,
363 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
364 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
365 break;
366
367/* make all windows the same height */
368 case '=':
369#ifdef FEAT_GUI
370 need_mouse_correct = TRUE;
371#endif
372 win_equal(NULL, FALSE, 'b');
373 break;
374
375/* increase current window height */
376 case '+':
377#ifdef FEAT_GUI
378 need_mouse_correct = TRUE;
379#endif
380 win_setheight(curwin->w_height + (int)Prenum1);
381 break;
382
383/* decrease current window height */
384 case '-':
385#ifdef FEAT_GUI
386 need_mouse_correct = TRUE;
387#endif
388 win_setheight(curwin->w_height - (int)Prenum1);
389 break;
390
391/* set current window height */
392 case Ctrl__:
393 case '_':
394#ifdef FEAT_GUI
395 need_mouse_correct = TRUE;
396#endif
397 win_setheight(Prenum ? (int)Prenum : 9999);
398 break;
399
400#ifdef FEAT_VERTSPLIT
401/* increase current window width */
402 case '>':
403#ifdef FEAT_GUI
404 need_mouse_correct = TRUE;
405#endif
406 win_setwidth(curwin->w_width + (int)Prenum1);
407 break;
408
409/* decrease current window width */
410 case '<':
411#ifdef FEAT_GUI
412 need_mouse_correct = TRUE;
413#endif
414 win_setwidth(curwin->w_width - (int)Prenum1);
415 break;
416
417/* set current window width */
418 case '|':
419#ifdef FEAT_GUI
420 need_mouse_correct = TRUE;
421#endif
422 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
423 break;
424#endif
425
426/* jump to tag and split window if tag exists (in preview window) */
427#if defined(FEAT_QUICKFIX)
428 case '}':
429 CHECK_CMDWIN
430 if (Prenum)
431 g_do_tagpreview = Prenum;
432 else
433 g_do_tagpreview = p_pvh;
434 /*FALLTHROUGH*/
435#endif
436 case ']':
437 case Ctrl_RSB:
438 CHECK_CMDWIN
439#ifdef FEAT_VISUAL
440 reset_VIsual_and_resel(); /* stop Visual mode */
441#endif
442 if (Prenum)
443 postponed_split = Prenum;
444 else
445 postponed_split = -1;
446
447 /* Execute the command right here, required when
448 * "wincmd ]" was used in a function. */
449 do_nv_ident(Ctrl_RSB, NUL);
450 break;
451
452#ifdef FEAT_SEARCHPATH
453/* edit file name under cursor in a new window */
454 case 'f':
455 case Ctrl_F:
456 CHECK_CMDWIN
457#ifdef FEAT_VISUAL
458 reset_VIsual_and_resel(); /* stop Visual mode */
459#endif
460 ptr = file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP,
461 Prenum1);
462 if (ptr != NULL)
463 {
464#ifdef FEAT_GUI
465 need_mouse_correct = TRUE;
466#endif
467 setpcmark();
468 if (win_split(0, 0) == OK)
469 {
470# ifdef FEAT_SCROLLBIND
471 curwin->w_p_scb = FALSE;
472# endif
473 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
474 ECMD_HIDE);
475 }
476 vim_free(ptr);
477 }
478 break;
479#endif
480
481#ifdef FEAT_FIND_ID
482/* Go to the first occurence of the identifier under cursor along path in a
483 * new window -- webb
484 */
485 case 'i': /* Go to any match */
486 case Ctrl_I:
487 type = FIND_ANY;
488 /* FALLTHROUGH */
489 case 'd': /* Go to definition, using 'define' */
490 case Ctrl_D:
491 CHECK_CMDWIN
492 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
493 break;
494 find_pattern_in_path(ptr, 0, len, TRUE,
495 Prenum == 0 ? TRUE : FALSE, type,
496 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
497 curwin->w_set_curswant = TRUE;
498 break;
499#endif
500
501/* CTRL-W g extended commands */
502 case 'g':
503 case Ctrl_G:
504 CHECK_CMDWIN
505#ifdef USE_ON_FLY_SCROLL
506 dont_scroll = TRUE; /* disallow scrolling here */
507#endif
508 ++no_mapping;
509 ++allow_keys; /* no mapping for xchar, but allow key codes */
510 if (xchar == NUL)
511 xchar = safe_vgetc();
512#ifdef FEAT_LANGMAP
513 LANGMAP_ADJUST(xchar, TRUE);
514#endif
515 --no_mapping;
516 --allow_keys;
517#ifdef FEAT_CMDL_INFO
518 (void)add_to_showcmd(xchar);
519#endif
520 switch (xchar)
521 {
522#if defined(FEAT_QUICKFIX)
523 case '}':
524 xchar = Ctrl_RSB;
525 if (Prenum)
526 g_do_tagpreview = Prenum;
527 else
528 g_do_tagpreview = p_pvh;
529 /*FALLTHROUGH*/
530#endif
531 case ']':
532 case Ctrl_RSB:
533#ifdef FEAT_VISUAL
534 reset_VIsual_and_resel(); /* stop Visual mode */
535#endif
536 if (Prenum)
537 postponed_split = Prenum;
538 else
539 postponed_split = -1;
540
541 /* Execute the command right here, required when
542 * "wincmd g}" was used in a function. */
543 do_nv_ident('g', xchar);
544 break;
545
546 default:
547 beep_flush();
548 break;
549 }
550 break;
551
552 default: beep_flush();
553 break;
554 }
555}
556
557/*
558 * split the current window, implements CTRL-W s and :split
559 *
560 * "size" is the height or width for the new window, 0 to use half of current
561 * height or width.
562 *
563 * "flags":
564 * WSP_ROOM: require enough room for new window
565 * WSP_VERT: vertical split.
566 * WSP_TOP: open window at the top-left of the shell (help window).
567 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
568 * WSP_HELP: creating the help window, keep layout snapshot
569 *
570 * return FAIL for failure, OK otherwise
571 */
572 int
573win_split(size, flags)
574 int size;
575 int flags;
576{
577 /* Add flags from ":vertical", ":topleft" and ":botright". */
578 flags |= cmdmod.split;
579 if ((flags & WSP_TOP) && (flags & WSP_BOT))
580 {
581 EMSG(_("E442: Can't split topleft and botright at the same time"));
582 return FAIL;
583 }
584
585 /* When creating the help window make a snapshot of the window layout.
586 * Otherwise clear the snapshot, it's now invalid. */
587 if (flags & WSP_HELP)
588 make_snapshot();
589 else
590 clear_snapshot();
591
592 return win_split_ins(size, flags, NULL, 0);
593}
594
595/*
596 * When "newwin" is NULL: split a window in two.
597 * When "newwin" is not NULL: insert this window at the far
598 * top/left/right/bottom.
599 * return FAIL for failure, OK otherwise
600 */
601 static int
602win_split_ins(size, flags, newwin, dir)
603 int size;
604 int flags;
605 win_T *newwin;
606 int dir;
607{
608 win_T *wp = newwin;
609 win_T *oldwin;
610 int new_size = size;
611 int i;
612 int need_status = 0;
613 int do_equal = FALSE;
614 int needed;
615 int available;
616 int oldwin_height = 0;
617 int layout;
618 frame_T *frp, *curfrp;
619 int before;
620
621 if (flags & WSP_TOP)
622 oldwin = firstwin;
623 else if (flags & WSP_BOT)
624 oldwin = lastwin;
625 else
626 oldwin = curwin;
627
628 /* add a status line when p_ls == 1 and splitting the first window */
629 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
630 {
631 if (oldwin->w_height <= p_wmh && newwin == NULL)
632 {
633 EMSG(_(e_noroom));
634 return FAIL;
635 }
636 need_status = STATUS_HEIGHT;
637 }
638
639#ifdef FEAT_VERTSPLIT
640 if (flags & WSP_VERT)
641 {
642 layout = FR_ROW;
643 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
644
645 /*
646 * Check if we are able to split the current window and compute its
647 * width.
648 */
649 needed = p_wmw + 1;
650 if (flags & WSP_ROOM)
651 needed += p_wiw - p_wmw;
652 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
653 {
654 available = topframe->fr_width;
655 needed += frame_minwidth(topframe, NULL);
656 }
657 else
658 available = oldwin->w_width;
659 if (available < needed && newwin == NULL)
660 {
661 EMSG(_(e_noroom));
662 return FAIL;
663 }
664 if (new_size == 0)
665 new_size = oldwin->w_width / 2;
666 if (new_size > oldwin->w_width - p_wmw - 1)
667 new_size = oldwin->w_width - p_wmw - 1;
668 if (new_size < p_wmw)
669 new_size = p_wmw;
670
671 /* if it doesn't fit in the current window, need win_equal() */
672 if (oldwin->w_width - new_size - 1 < p_wmw)
673 do_equal = TRUE;
674 }
675 else
676#endif
677 {
678 layout = FR_COL;
679 do_equal = (p_ea && new_size == 0
680#ifdef FEAT_VERTSPLIT
681 && *p_ead != 'h'
682#endif
683 );
684
685 /*
686 * Check if we are able to split the current window and compute its
687 * height.
688 */
689 needed = p_wmh + STATUS_HEIGHT + need_status;
690 if (flags & WSP_ROOM)
691 needed += p_wh - p_wmh;
692 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
693 {
694 available = topframe->fr_height;
695 needed += frame_minheight(topframe, NULL);
696 }
697 else
698 {
699 available = oldwin->w_height;
700 needed += p_wmh;
701 }
702 if (available < needed && newwin == NULL)
703 {
704 EMSG(_(e_noroom));
705 return FAIL;
706 }
707 oldwin_height = oldwin->w_height;
708 if (need_status)
709 {
710 oldwin->w_status_height = STATUS_HEIGHT;
711 oldwin_height -= STATUS_HEIGHT;
712 }
713 if (new_size == 0)
714 new_size = oldwin_height / 2;
715
716 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
717 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
718 if (new_size < p_wmh)
719 new_size = p_wmh;
720
721 /* if it doesn't fit in the current window, need win_equal() */
722 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
723 do_equal = TRUE;
724
725 /* We don't like to take lines for the new window from a
726 * 'winfixheight' window. Take them from a window above or below
727 * instead, if possible. */
728 if (oldwin->w_p_wfh)
729 {
730 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
731 oldwin);
732 oldwin_height = oldwin->w_height;
733 if (need_status)
734 oldwin_height -= STATUS_HEIGHT;
735 }
736 }
737
738 /*
739 * allocate new window structure and link it in the window list
740 */
741 if ((flags & WSP_TOP) == 0
742 && ((flags & WSP_BOT)
743 || (flags & WSP_BELOW)
744 || (!(flags & WSP_ABOVE)
745 && (
746#ifdef FEAT_VERTSPLIT
747 (flags & WSP_VERT) ? p_spr :
748#endif
749 p_sb))))
750 {
751 /* new window below/right of current one */
752 if (newwin == NULL)
753 wp = win_alloc(oldwin);
754 else
755 win_append(oldwin, wp);
756 }
757 else
758 {
759 if (newwin == NULL)
760 wp = win_alloc(oldwin->w_prev);
761 else
762 win_append(oldwin->w_prev, wp);
763 }
764
765 if (newwin == NULL)
766 {
767 if (wp == NULL)
768 return FAIL;
769
770 /*
771 * make the contents of the new window the same as the current one
772 */
773 wp->w_buffer = curbuf;
774 curbuf->b_nwindows++;
775 wp->w_cursor = curwin->w_cursor;
776 wp->w_valid = 0;
777 wp->w_curswant = curwin->w_curswant;
778 wp->w_set_curswant = curwin->w_set_curswant;
779 wp->w_topline = curwin->w_topline;
780#ifdef FEAT_DIFF
781 wp->w_topfill = curwin->w_topfill;
782#endif
783 wp->w_leftcol = curwin->w_leftcol;
784 wp->w_pcmark = curwin->w_pcmark;
785 wp->w_prev_pcmark = curwin->w_prev_pcmark;
786 wp->w_alt_fnum = curwin->w_alt_fnum;
787 wp->w_fraction = curwin->w_fraction;
788 wp->w_prev_fraction_row = curwin->w_prev_fraction_row;
789#ifdef FEAT_JUMPLIST
790 copy_jumplist(curwin, wp);
791#endif
792 if (curwin->w_localdir != NULL)
793 wp->w_localdir = vim_strsave(curwin->w_localdir);
794
795 /* Use the same argument list. */
796 wp->w_alist = curwin->w_alist;
797 ++wp->w_alist->al_refcount;
798 wp->w_arg_idx = curwin->w_arg_idx;
799
800 /*
801 * copy tagstack and options from existing window
802 */
803 for (i = 0; i < curwin->w_tagstacklen; i++)
804 {
805 wp->w_tagstack[i] = curwin->w_tagstack[i];
806 if (wp->w_tagstack[i].tagname != NULL)
807 wp->w_tagstack[i].tagname =
808 vim_strsave(wp->w_tagstack[i].tagname);
809 }
810 wp->w_tagstackidx = curwin->w_tagstackidx;
811 wp->w_tagstacklen = curwin->w_tagstacklen;
812 win_copy_options(curwin, wp);
813#ifdef FEAT_FOLDING
814 copyFoldingState(curwin, wp);
815#endif
816 }
817
818 /*
819 * Reorganise the tree of frames to insert the new window.
820 */
821 if (flags & (WSP_TOP | WSP_BOT))
822 {
823#ifdef FEAT_VERTSPLIT
824 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
825 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
826#else
827 if (topframe->fr_layout == FR_COL)
828#endif
829 {
830 curfrp = topframe->fr_child;
831 if (flags & WSP_BOT)
832 while (curfrp->fr_next != NULL)
833 curfrp = curfrp->fr_next;
834 }
835 else
836 curfrp = topframe;
837 before = (flags & WSP_TOP);
838 }
839 else
840 {
841 curfrp = oldwin->w_frame;
842 if (flags & WSP_BELOW)
843 before = FALSE;
844 else if (flags & WSP_ABOVE)
845 before = TRUE;
846 else
847#ifdef FEAT_VERTSPLIT
848 if (flags & WSP_VERT)
849 before = !p_spr;
850 else
851#endif
852 before = !p_sb;
853 }
854 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
855 {
856 /* Need to create a new frame in the tree to make a branch. */
857 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
858 *frp = *curfrp;
859 curfrp->fr_layout = layout;
860 frp->fr_parent = curfrp;
861 frp->fr_next = NULL;
862 frp->fr_prev = NULL;
863 curfrp->fr_child = frp;
864 curfrp->fr_win = NULL;
865 curfrp = frp;
866 if (frp->fr_win != NULL)
867 oldwin->w_frame = frp;
868 else
869 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
870 frp->fr_parent = curfrp;
871 }
872
873 if (newwin == NULL)
874 {
875 /* Create a frame for the new window. */
876 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
877 frp->fr_layout = FR_LEAF;
878 frp->fr_win = wp;
879 wp->w_frame = frp;
880 }
881 else
882 frp = newwin->w_frame;
883 frp->fr_parent = curfrp->fr_parent;
884
885 /* Insert the new frame at the right place in the frame list. */
886 if (before)
887 frame_insert(curfrp, frp);
888 else
889 frame_append(curfrp, frp);
890
891#ifdef FEAT_VERTSPLIT
892 if (flags & WSP_VERT)
893 {
894 wp->w_p_scr = curwin->w_p_scr;
895 if (need_status)
896 {
897 --oldwin->w_height;
898 oldwin->w_status_height = need_status;
899 }
900 if (flags & (WSP_TOP | WSP_BOT))
901 {
902 /* set height and row of new window to full height */
903 wp->w_winrow = 0;
904 wp->w_height = curfrp->fr_height - (p_ls > 0);
905 wp->w_status_height = (p_ls > 0);
906 }
907 else
908 {
909 /* height and row of new window is same as current window */
910 wp->w_winrow = oldwin->w_winrow;
911 wp->w_height = oldwin->w_height;
912 wp->w_status_height = oldwin->w_status_height;
913 }
914 frp->fr_height = curfrp->fr_height;
915
916 /* "new_size" of the current window goes to the new window, use
917 * one column for the vertical separator */
918 wp->w_width = new_size;
919 if (before)
920 wp->w_vsep_width = 1;
921 else
922 {
923 wp->w_vsep_width = oldwin->w_vsep_width;
924 oldwin->w_vsep_width = 1;
925 }
926 if (flags & (WSP_TOP | WSP_BOT))
927 {
928 if (flags & WSP_BOT)
929 frame_add_vsep(curfrp);
930 /* Set width of neighbor frame */
931 frame_new_width(curfrp, curfrp->fr_width
932 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP);
933 }
934 else
935 oldwin->w_width -= new_size + 1;
936 if (before) /* new window left of current one */
937 {
938 wp->w_wincol = oldwin->w_wincol;
939 oldwin->w_wincol += new_size + 1;
940 }
941 else /* new window right of current one */
942 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
943 frame_fix_width(oldwin);
944 frame_fix_width(wp);
945 }
946 else
947#endif
948 {
949 /* width and column of new window is same as current window */
950#ifdef FEAT_VERTSPLIT
951 if (flags & (WSP_TOP | WSP_BOT))
952 {
953 wp->w_wincol = 0;
954 wp->w_width = Columns;
955 wp->w_vsep_width = 0;
956 }
957 else
958 {
959 wp->w_wincol = oldwin->w_wincol;
960 wp->w_width = oldwin->w_width;
961 wp->w_vsep_width = oldwin->w_vsep_width;
962 }
963 frp->fr_width = curfrp->fr_width;
964#endif
965
966 /* "new_size" of the current window goes to the new window, use
967 * one row for the status line */
968 win_new_height(wp, new_size);
969 if (flags & (WSP_TOP | WSP_BOT))
970 frame_new_height(curfrp, curfrp->fr_height
971 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
972 else
973 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
974 if (before) /* new window above current one */
975 {
976 wp->w_winrow = oldwin->w_winrow;
977 wp->w_status_height = STATUS_HEIGHT;
978 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
979 }
980 else /* new window below current one */
981 {
982 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
983 wp->w_status_height = oldwin->w_status_height;
984 oldwin->w_status_height = STATUS_HEIGHT;
985 }
986#ifdef FEAT_VERTSPLIT
987 if (flags & WSP_BOT)
988 frame_add_statusline(curfrp);
989#endif
990 frame_fix_height(wp);
991 frame_fix_height(oldwin);
992 }
993
994 if (flags & (WSP_TOP | WSP_BOT))
995 (void)win_comp_pos();
996
997 /*
998 * Both windows need redrawing
999 */
1000 redraw_win_later(wp, NOT_VALID);
1001 wp->w_redr_status = TRUE;
1002 redraw_win_later(oldwin, NOT_VALID);
1003 oldwin->w_redr_status = TRUE;
1004
1005 if (need_status)
1006 {
1007 msg_row = Rows - 1;
1008 msg_col = sc_col;
1009 msg_clr_eos_force(); /* Old command/ruler may still be there */
1010 comp_col();
1011 msg_row = Rows - 1;
1012 msg_col = 0; /* put position back at start of line */
1013 }
1014
1015 /*
1016 * make the new window the current window and redraw
1017 */
1018 if (do_equal || dir != 0)
1019 win_equal(wp, TRUE,
1020#ifdef FEAT_VERTSPLIT
1021 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1022 : dir == 'h' ? 'b' :
1023#endif
1024 'v');
1025
1026 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1027 * size was given. */
1028#ifdef FEAT_VERTSPLIT
1029 if (flags & WSP_VERT)
1030 {
1031 i = p_wiw;
1032 if (size != 0)
1033 p_wiw = size;
1034
1035# ifdef FEAT_GUI
1036 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1037 if (gui.in_use)
1038 gui_init_which_components(NULL);
1039# endif
1040 }
1041 else
1042#endif
1043 {
1044 i = p_wh;
1045 if (size != 0)
1046 p_wh = size;
1047 }
1048 win_enter(wp, FALSE);
1049#ifdef FEAT_VERTSPLIT
1050 if (flags & WSP_VERT)
1051 p_wiw = i;
1052 else
1053#endif
1054 p_wh = i;
1055
1056 return OK;
1057}
1058
1059#endif /* FEAT_WINDOWS */
1060
1061#ifdef FEAT_VERTSPLIT
1062/*
1063 * Return minimal height for window "wp" and windows east of it.
1064 * Takes into account the eastbound windws can be split, each of them
1065 * requireing p_wmh lines. Doesn't count status lines.
1066 */
1067 static int
1068win_minheight(wp)
1069 win_T *wp;
1070{
1071 int minheight = p_wmh;
1072 int n;
1073 win_T *wp1, *wp2;
1074
1075 wp1 = wp;
1076 for (;;)
1077 {
1078 wp1 = wp1->w_next;
1079 if (wp1 == NULL)
1080 break;
1081 n = p_wmh;
1082 wp2 = wp1;
1083 for (;;)
1084 {
1085 wp2 = wp2->w_next;
1086 if (wp2 == NULL)
1087 break;
1088 n += win_minheight(wp2);
1089 }
1090 if (n > minheight)
1091 minheight = n;
1092 }
1093 return minheight;
1094}
1095
1096#endif
1097
1098#if defined(FEAT_WINDOWS) || defined(PROTO)
1099/*
1100 * Check if "win" is a pointer to an existing window.
1101 */
1102 int
1103win_valid(win)
1104 win_T *win;
1105{
1106 win_T *wp;
1107
1108 if (win == NULL)
1109 return FALSE;
1110 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1111 if (wp == win)
1112 return TRUE;
1113 return FALSE;
1114}
1115
1116/*
1117 * Return the number of windows.
1118 */
1119 int
1120win_count()
1121{
1122 win_T *wp;
1123 int count = 0;
1124
1125 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1126 ++count;
1127 return count;
1128}
1129
1130/*
1131 * Make "count" windows on the screen.
1132 * Return actual number of windows on the screen.
1133 * Must be called when there is just one window, filling the whole screen
1134 * (excluding the command line).
1135 */
1136/*ARGSUSED*/
1137 int
1138make_windows(count, vertical)
1139 int count;
1140 int vertical; /* split windows vertically if TRUE */
1141{
1142 int maxcount;
1143 int todo;
1144
1145#ifdef FEAT_VERTSPLIT
1146 if (vertical)
1147 {
1148 /* Each windows needs at least 'winminwidth' lines and a separator
1149 * column. */
1150 maxcount = (curwin->w_width + curwin->w_vsep_width
1151 - (p_wiw - p_wmw)) / (p_wmw + 1);
1152 }
1153 else
1154#endif
1155 {
1156 /* Each window needs at least 'winminheight' lines and a status line. */
1157 maxcount = (curwin->w_height + curwin->w_status_height
1158 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1159 }
1160
1161 if (maxcount < 2)
1162 maxcount = 2;
1163 if (count > maxcount)
1164 count = maxcount;
1165
1166 /*
1167 * add status line now, otherwise first window will be too big
1168 */
1169 if (count > 1)
1170 last_status(TRUE);
1171
1172#ifdef FEAT_AUTOCMD
1173 /*
1174 * Don't execute autocommands while creating the windows. Must do that
1175 * when putting the buffers in the windows.
1176 */
1177 ++autocmd_block;
1178#endif
1179
1180 /* todo is number of windows left to create */
1181 for (todo = count - 1; todo > 0; --todo)
1182#ifdef FEAT_VERTSPLIT
1183 if (vertical)
1184 {
1185 if (win_split(curwin->w_width - (curwin->w_width - todo)
1186 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1187 break;
1188 }
1189 else
1190#endif
1191 {
1192 if (win_split(curwin->w_height - (curwin->w_height - todo
1193 * STATUS_HEIGHT) / (todo + 1)
1194 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1195 break;
1196 }
1197
1198#ifdef FEAT_AUTOCMD
1199 --autocmd_block;
1200#endif
1201
1202 /* return actual number of windows */
1203 return (count - todo);
1204}
1205
1206/*
1207 * Exchange current and next window
1208 */
1209 static void
1210win_exchange(Prenum)
1211 long Prenum;
1212{
1213 frame_T *frp;
1214 frame_T *frp2;
1215 win_T *wp;
1216 win_T *wp2;
1217 int temp;
1218
1219 if (lastwin == firstwin) /* just one window */
1220 {
1221 beep_flush();
1222 return;
1223 }
1224
1225#ifdef FEAT_GUI
1226 need_mouse_correct = TRUE;
1227#endif
1228
1229 /*
1230 * find window to exchange with
1231 */
1232 if (Prenum)
1233 {
1234 frp = curwin->w_frame->fr_parent->fr_child;
1235 while (frp != NULL && --Prenum > 0)
1236 frp = frp->fr_next;
1237 }
1238 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1239 frp = curwin->w_frame->fr_next;
1240 else /* Swap last window in row/col with previous */
1241 frp = curwin->w_frame->fr_prev;
1242
1243 /* We can only exchange a window with another window, not with a frame
1244 * containing windows. */
1245 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1246 return;
1247 wp = frp->fr_win;
1248
1249/*
1250 * 1. remove curwin from the list. Remember after which window it was in wp2
1251 * 2. insert curwin before wp in the list
1252 * if wp != wp2
1253 * 3. remove wp from the list
1254 * 4. insert wp after wp2
1255 * 5. exchange the status line height and vsep width.
1256 */
1257 wp2 = curwin->w_prev;
1258 frp2 = curwin->w_frame->fr_prev;
1259 if (wp->w_prev != curwin)
1260 {
1261 win_remove(curwin);
1262 frame_remove(curwin->w_frame);
1263 win_append(wp->w_prev, curwin);
1264 frame_insert(frp, curwin->w_frame);
1265 }
1266 if (wp != wp2)
1267 {
1268 win_remove(wp);
1269 frame_remove(wp->w_frame);
1270 win_append(wp2, wp);
1271 if (frp2 == NULL)
1272 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1273 else
1274 frame_append(frp2, wp->w_frame);
1275 }
1276 temp = curwin->w_status_height;
1277 curwin->w_status_height = wp->w_status_height;
1278 wp->w_status_height = temp;
1279#ifdef FEAT_VERTSPLIT
1280 temp = curwin->w_vsep_width;
1281 curwin->w_vsep_width = wp->w_vsep_width;
1282 wp->w_vsep_width = temp;
1283
1284 /* If the windows are not in the same frame, exchange the sizes to avoid
1285 * messing up the window layout. Otherwise fix the frame sizes. */
1286 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1287 {
1288 temp = curwin->w_height;
1289 curwin->w_height = wp->w_height;
1290 wp->w_height = temp;
1291 temp = curwin->w_width;
1292 curwin->w_width = wp->w_width;
1293 wp->w_width = temp;
1294 }
1295 else
1296 {
1297 frame_fix_height(curwin);
1298 frame_fix_height(wp);
1299 frame_fix_width(curwin);
1300 frame_fix_width(wp);
1301 }
1302#endif
1303
1304 (void)win_comp_pos(); /* recompute window positions */
1305
1306 win_enter(wp, TRUE);
1307 redraw_later(CLEAR);
1308}
1309
1310/*
1311 * rotate windows: if upwards TRUE the second window becomes the first one
1312 * if upwards FALSE the first window becomes the second one
1313 */
1314 static void
1315win_rotate(upwards, count)
1316 int upwards;
1317 int count;
1318{
1319 win_T *wp1;
1320 win_T *wp2;
1321 frame_T *frp;
1322 int n;
1323
1324 if (firstwin == lastwin) /* nothing to do */
1325 {
1326 beep_flush();
1327 return;
1328 }
1329
1330#ifdef FEAT_GUI
1331 need_mouse_correct = TRUE;
1332#endif
1333
1334#ifdef FEAT_VERTSPLIT
1335 /* Check if all frames in this row/col have one window. */
1336 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1337 frp = frp->fr_next)
1338 if (frp->fr_win == NULL)
1339 {
1340 EMSG(_("E443: Cannot rotate when another window is split"));
1341 return;
1342 }
1343#endif
1344
1345 while (count--)
1346 {
1347 if (upwards) /* first window becomes last window */
1348 {
1349 /* remove first window/frame from the list */
1350 frp = curwin->w_frame->fr_parent->fr_child;
1351 wp1 = frp->fr_win;
1352 win_remove(wp1);
1353 frame_remove(frp);
1354
1355 /* find last frame and append removed window/frame after it */
1356 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1357 ;
1358 win_append(frp->fr_win, wp1);
1359 frame_append(frp, wp1->w_frame);
1360
1361 wp2 = frp->fr_win; /* previously last window */
1362 }
1363 else /* last window becomes first window */
1364 {
1365 /* find last window/frame in the list and remove it */
1366 for (frp = curwin->w_frame; frp->fr_next != NULL;
1367 frp = frp->fr_next)
1368 ;
1369 wp1 = frp->fr_win;
1370 wp2 = wp1->w_prev; /* will become last window */
1371 win_remove(wp1);
1372 frame_remove(frp);
1373
1374 /* append the removed window/frame before the first in the list */
1375 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1376 frame_insert(frp->fr_parent->fr_child, frp);
1377 }
1378
1379 /* exchange status height and vsep width of old and new last window */
1380 n = wp2->w_status_height;
1381 wp2->w_status_height = wp1->w_status_height;
1382 wp1->w_status_height = n;
1383 frame_fix_height(wp1);
1384 frame_fix_height(wp2);
1385#ifdef FEAT_VERTSPLIT
1386 n = wp2->w_vsep_width;
1387 wp2->w_vsep_width = wp1->w_vsep_width;
1388 wp1->w_vsep_width = n;
1389 frame_fix_width(wp1);
1390 frame_fix_width(wp2);
1391#endif
1392
1393 /* recompute w_winrow and w_wincol for all windows */
1394 (void)win_comp_pos();
1395 }
1396
1397 redraw_later(CLEAR);
1398}
1399
1400/*
1401 * Move the current window to the very top/bottom/left/right of the screen.
1402 */
1403 static void
1404win_totop(size, flags)
1405 int size;
1406 int flags;
1407{
1408 int dir;
1409 int height = curwin->w_height;
1410
1411 if (lastwin == firstwin)
1412 {
1413 beep_flush();
1414 return;
1415 }
1416
1417 /* Remove the window and frame from the tree of frames. */
1418 (void)winframe_remove(curwin, &dir);
1419 win_remove(curwin);
1420 last_status(FALSE); /* may need to remove last status line */
1421 (void)win_comp_pos(); /* recompute window positions */
1422
1423 /* Split a window on the desired side and put the window there. */
1424 (void)win_split_ins(size, flags, curwin, dir);
1425 if (!(flags & WSP_VERT))
1426 {
1427 win_setheight(height);
1428 if (p_ea)
1429 win_equal(curwin, TRUE, 'v');
1430 }
1431
1432#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1433 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1434 * scrollbars. Have to update them anyway. */
1435 if (gui.in_use)
1436 {
1437 out_flush();
1438 gui_init_which_components(NULL);
1439 gui_update_scrollbars(TRUE);
1440 }
1441 need_mouse_correct = TRUE;
1442#endif
1443
1444}
1445
1446/*
1447 * Move window "win1" to below/right of "win2" and make "win1" the current
1448 * window. Only works within the same frame!
1449 */
1450 void
1451win_move_after(win1, win2)
1452 win_T *win1, *win2;
1453{
1454 int height;
1455
1456 /* check if the arguments are reasonable */
1457 if (win1 == win2)
1458 return;
1459
1460 /* check if there is something to do */
1461 if (win2->w_next != win1)
1462 {
1463 /* may need move the status line/vertical separator of the last window
1464 * */
1465 if (win1 == lastwin)
1466 {
1467 height = win1->w_prev->w_status_height;
1468 win1->w_prev->w_status_height = win1->w_status_height;
1469 win1->w_status_height = height;
1470#ifdef FEAT_VERTSPLIT
1471 win1->w_prev->w_vsep_width = 0;
1472 win1->w_vsep_width = 1;
1473#endif
1474 }
1475 else if (win2 == lastwin)
1476 {
1477 height = win1->w_status_height;
1478 win1->w_status_height = win2->w_status_height;
1479 win2->w_status_height = height;
1480#ifdef FEAT_VERTSPLIT
1481 win2->w_vsep_width = 1;
1482 win1->w_vsep_width = 0;
1483#endif
1484 }
1485 win_remove(win1);
1486 frame_remove(win1->w_frame);
1487 win_append(win2, win1);
1488 frame_append(win2->w_frame, win1->w_frame);
1489
1490 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1491 redraw_later(NOT_VALID);
1492 }
1493 win_enter(win1, FALSE);
1494}
1495
1496/*
1497 * Make all windows the same height.
1498 * 'next_curwin' will soon be the current window, make sure it has enough
1499 * rows.
1500 */
1501 void
1502win_equal(next_curwin, current, dir)
1503 win_T *next_curwin; /* pointer to current window to be or NULL */
1504 int current; /* do only frame with current window */
1505 int dir; /* 'v' for vertically, 'h' for horizontally,
1506 'b' for both, 0 for using p_ead */
1507{
1508 if (dir == 0)
1509#ifdef FEAT_VERTSPLIT
1510 dir = *p_ead;
1511#else
1512 dir = 'b';
1513#endif
1514 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1515 topframe, dir, 0, 0, (int)Columns, topframe->fr_height);
1516}
1517
1518/*
1519 * Set a frame to a new position and height, spreading the available room
1520 * equally over contained frames.
1521 * The window "next_curwin" (if not NULL) should at least get the size from
1522 * 'winheight' and 'winwidth' if possible.
1523 */
1524 static void
1525win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1526 win_T *next_curwin; /* pointer to current window to be or NULL */
1527 int current; /* do only frame with current window */
1528 frame_T *topfr; /* frame to set size off */
1529 int dir; /* 'v', 'h' or 'b', see win_equal() */
1530 int col; /* horizontal position for frame */
1531 int row; /* vertical position for frame */
1532 int width; /* new width of frame */
1533 int height; /* new height of frame */
1534{
1535 int n, m;
1536 int extra_sep = 0;
1537 int wincount, totwincount = 0;
1538 frame_T *fr;
1539 int next_curwin_size = 0;
1540 int room = 0;
1541 int new_size;
1542 int has_next_curwin = 0;
1543 int hnc;
1544
1545 if (topfr->fr_layout == FR_LEAF)
1546 {
1547 /* Set the width/height of this frame.
1548 * Redraw when size or position changes */
1549 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1550#ifdef FEAT_VERTSPLIT
1551 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1552#endif
1553 )
1554 {
1555 topfr->fr_win->w_winrow = row;
1556 frame_new_height(topfr, height, FALSE, FALSE);
1557#ifdef FEAT_VERTSPLIT
1558 topfr->fr_win->w_wincol = col;
1559 frame_new_width(topfr, width, FALSE);
1560#endif
1561 redraw_all_later(CLEAR);
1562 }
1563 }
1564#ifdef FEAT_VERTSPLIT
1565 else if (topfr->fr_layout == FR_ROW)
1566 {
1567 topfr->fr_width = width;
1568 topfr->fr_height = height;
1569
1570 if (dir != 'v') /* equalize frame widths */
1571 {
1572 /* Compute the maximum number of windows horizontally in this
1573 * frame. */
1574 n = frame_minwidth(topfr, NOWIN);
1575 /* add one for the rightmost window, it doesn't have a separator */
1576 if (col + width == Columns)
1577 extra_sep = 1;
1578 else
1579 extra_sep = 0;
1580 totwincount = (n + extra_sep) / (p_wmw + 1);
1581
1582 /* Compute room available for windows other than "next_curwin" */
1583 m = frame_minwidth(topfr, next_curwin);
1584 room = width - m;
1585 if (room < 0)
1586 {
1587 next_curwin_size = p_wiw + room;
1588 room = 0;
1589 }
1590 else if (n == m) /* doesn't contain curwin */
1591 next_curwin_size = 0;
1592 else
1593 {
1594 next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw
1595 + (totwincount - 1)) / totwincount;
1596 if (next_curwin_size > p_wiw)
1597 room -= next_curwin_size - p_wiw;
1598 else
1599 next_curwin_size = p_wiw;
1600 }
1601 if (n != m)
1602 --totwincount; /* don't count curwin */
1603 }
1604
1605 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1606 {
1607 n = m = 0;
1608 wincount = 1;
1609 if (fr->fr_next == NULL)
1610 /* last frame gets all that remains (avoid roundoff error) */
1611 new_size = width;
1612 else if (dir == 'v')
1613 new_size = fr->fr_width;
1614 else
1615 {
1616 /* Compute the maximum number of windows horiz. in "fr". */
1617 n = frame_minwidth(fr, NOWIN);
1618 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1619 / (p_wmw + 1);
1620 m = frame_minwidth(fr, next_curwin);
1621 if (n != m) /* don't count next_curwin */
1622 --wincount;
1623 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1624 / totwincount;
1625 if (n != m) /* add next_curwin size */
1626 {
1627 next_curwin_size -= p_wiw - (m - n);
1628 new_size += next_curwin_size;
1629 }
1630 }
1631
1632 /* Skip frame that is full height when splitting or closing a
1633 * window, unless equalizing all frames. */
1634 if (!current || dir != 'v' || topfr->fr_parent != NULL
1635 || (new_size != fr->fr_width)
1636 || frame_has_win(fr, next_curwin))
1637 win_equal_rec(next_curwin, current, fr, dir, col, row,
1638 new_size + n, height);
1639 col += new_size + n;
1640 width -= new_size + n;
1641 if (n != m) /* contains curwin */
1642 room -= new_size - next_curwin_size;
1643 else
1644 room -= new_size;
1645 totwincount -= wincount;
1646 }
1647 }
1648#endif
1649 else /* topfr->fr_layout == FR_COL */
1650 {
1651#ifdef FEAT_VERTSPLIT
1652 topfr->fr_width = width;
1653#endif
1654 topfr->fr_height = height;
1655
1656 if (dir != 'h') /* equalize frame heights */
1657 {
1658 /* Compute maximum number of windows vertically in this frame. */
1659 n = frame_minheight(topfr, NOWIN);
1660 /* add one for the bottom window if it doesn't have a statusline */
1661 if (row + height == cmdline_row && p_ls == 0)
1662 extra_sep = 1;
1663 else
1664 extra_sep = 0;
1665 totwincount = (n + extra_sep) / (p_wmh + 1);
1666 has_next_curwin = frame_has_win(topfr, next_curwin);
1667
1668 /*
1669 * Compute height for "next_curwin" window and room available for
1670 * other windows.
1671 * "m" is the minimal height when counting p_wh for "next_curwin".
1672 */
1673 m = frame_minheight(topfr, next_curwin);
1674 room = height - m;
1675 if (room < 0)
1676 {
1677 /* The room is less then 'winheight', use all space for the
1678 * current window. */
1679 next_curwin_size = p_wh + room;
1680 room = 0;
1681 }
1682 else
1683 {
1684 next_curwin_size = -1;
1685 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1686 {
1687 /* If 'winfixheight' set keep the window height if
1688 * possible.
1689 * Watch out for this window being the next_curwin. */
1690 if (frame_fixed_height(fr))
1691 {
1692 n = frame_minheight(fr, NOWIN);
1693 new_size = fr->fr_height;
1694 if (frame_has_win(fr, next_curwin))
1695 {
1696 room += p_wh - p_wmh;
1697 next_curwin_size = 0;
1698 if (new_size < p_wh)
1699 new_size = p_wh;
1700 }
1701 else
1702 /* These windows don't use up room. */
1703 totwincount -= (n + (fr->fr_next == NULL
1704 ? extra_sep : 0)) / (p_wmh + 1);
1705 room -= new_size - n;
1706 if (room < 0)
1707 {
1708 new_size += room;
1709 room = 0;
1710 }
1711 fr->fr_newheight = new_size;
1712 }
1713 }
1714 if (next_curwin_size == -1)
1715 {
1716 if (!has_next_curwin)
1717 next_curwin_size = 0;
1718 else if (totwincount > 1
1719 && (room + (totwincount - 2))
1720 / (totwincount - 1) > p_wh)
1721 {
1722 next_curwin_size = (room + p_wh + totwincount * p_wmh
1723 + (totwincount - 1)) / totwincount;
1724 room -= next_curwin_size - p_wh;
1725 }
1726 else
1727 next_curwin_size = p_wh;
1728 }
1729 }
1730
1731 if (has_next_curwin)
1732 --totwincount; /* don't count curwin */
1733 }
1734
1735 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1736 {
1737 n = m = 0;
1738 wincount = 1;
1739 if (fr->fr_next == NULL)
1740 /* last frame gets all that remains (avoid roundoff error) */
1741 new_size = height;
1742 else if (dir == 'h')
1743 new_size = fr->fr_height;
1744 else if (frame_fixed_height(fr))
1745 {
1746 new_size = fr->fr_newheight;
1747 wincount = 0; /* doesn't count as a sizeable window */
1748 }
1749 else
1750 {
1751 /* Compute the maximum number of windows vert. in "fr". */
1752 n = frame_minheight(fr, NOWIN);
1753 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1754 / (p_wmh + 1);
1755 m = frame_minheight(fr, next_curwin);
1756 if (has_next_curwin)
1757 hnc = frame_has_win(fr, next_curwin);
1758 else
1759 hnc = FALSE;
1760 if (hnc) /* don't count next_curwin */
1761 --wincount;
1762 if (totwincount == 0)
1763 new_size = room;
1764 else
1765 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1766 / totwincount;
1767 if (hnc) /* add next_curwin size */
1768 {
1769 next_curwin_size -= p_wh - (m - n);
1770 new_size += next_curwin_size;
1771 room -= new_size - next_curwin_size;
1772 }
1773 else
1774 room -= new_size;
1775 new_size += n;
1776 }
1777 /* Skip frame that is full width when splitting or closing a
1778 * window, unless equalizing all frames. */
1779 if (!current || dir != 'h' || topfr->fr_parent != NULL
1780 || (new_size != fr->fr_height)
1781 || frame_has_win(fr, next_curwin))
1782 win_equal_rec(next_curwin, current, fr, dir, col, row,
1783 width, new_size);
1784 row += new_size;
1785 height -= new_size;
1786 totwincount -= wincount;
1787 }
1788 }
1789}
1790
1791/*
1792 * close all windows for buffer 'buf'
1793 */
1794 void
1795close_windows(buf)
1796 buf_T *buf;
1797{
1798 win_T *win;
1799
1800 ++RedrawingDisabled;
1801 for (win = firstwin; win != NULL && lastwin != firstwin; )
1802 {
1803 if (win->w_buffer == buf)
1804 {
1805 win_close(win, FALSE);
1806 win = firstwin; /* go back to the start */
1807 }
1808 else
1809 win = win->w_next;
1810 }
1811 --RedrawingDisabled;
1812}
1813
1814/*
1815 * close window "win"
1816 * If "free_buf" is TRUE related buffer may be unloaded.
1817 *
1818 * called by :quit, :close, :xit, :wq and findtag()
1819 */
1820 void
1821win_close(win, free_buf)
1822 win_T *win;
1823 int free_buf;
1824{
1825 win_T *wp;
1826#ifdef FEAT_AUTOCMD
1827 int other_buffer = FALSE;
1828#endif
1829 int close_curwin = FALSE;
1830 frame_T *frp;
1831 int dir;
1832 int help_window = FALSE;
1833
1834 if (lastwin == firstwin)
1835 {
1836 EMSG(_("E444: Cannot close last window"));
1837 return;
1838 }
1839
1840 /* When closing the help window, try restoring a snapshot after closing
1841 * the window. Otherwise clear the snapshot, it's now invalid. */
1842 if (win->w_buffer->b_help)
1843 help_window = TRUE;
1844 else
1845 clear_snapshot();
1846
1847#ifdef FEAT_AUTOCMD
1848 if (win == curwin)
1849 {
1850 /*
1851 * Guess which window is going to be the new current window.
1852 * This may change because of the autocommands (sigh).
1853 */
1854 wp = frame2win(win_altframe(win));
1855
1856 /*
1857 * Be careful: If autocommands delete the window, return now.
1858 */
1859 if (wp->w_buffer != curbuf)
1860 {
1861 other_buffer = TRUE;
1862 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1863 if (!win_valid(win) || firstwin == lastwin)
1864 return;
1865 }
1866 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
1867 if (!win_valid(win) || firstwin == lastwin)
1868 return;
1869# ifdef FEAT_EVAL
1870 /* autocmds may abort script processing */
1871 if (aborting())
1872 return;
1873# endif
1874 }
1875#endif
1876
1877 /*
1878 * Close the link to the buffer.
1879 */
1880 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1881 /* Autocommands may have closed the window already, or closed the only
1882 * other window. */
1883 if (!win_valid(win) || firstwin == lastwin)
1884 return;
1885
1886 /* reduce the reference count to the argument list. */
1887 alist_unlink(win->w_alist);
1888
1889 /* remove the window and its frame from the tree of frames. */
1890 frp = win->w_frame;
1891 wp = winframe_remove(win, &dir);
1892 vim_free(frp);
1893 win_free(win);
1894
1895 /* Make sure curwin isn't invalid. It can cause severe trouble when
1896 * printing an error message. For win_equal() curbuf needs to be valid
1897 * too. */
1898 if (win == curwin)
1899 {
1900 curwin = wp;
1901#ifdef FEAT_QUICKFIX
1902 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1903 {
1904 /*
1905 * The cursor goes to the preview or the quickfix window, try
1906 * finding another window to go to.
1907 */
1908 for (;;)
1909 {
1910 if (wp->w_next == NULL)
1911 wp = firstwin;
1912 else
1913 wp = wp->w_next;
1914 if (wp == curwin)
1915 break;
1916 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
1917 {
1918 curwin = wp;
1919 break;
1920 }
1921 }
1922 }
1923#endif
1924 curbuf = curwin->w_buffer;
1925 close_curwin = TRUE;
1926 }
1927 if (p_ea)
1928 win_equal(curwin, TRUE,
1929#ifdef FEAT_VERTSPLIT
1930 dir
1931#else
1932 0
1933#endif
1934 );
1935 else
1936 win_comp_pos();
1937 if (close_curwin)
1938 {
1939 win_enter_ext(wp, FALSE, TRUE);
1940#ifdef FEAT_AUTOCMD
1941 if (other_buffer)
1942 /* careful: after this wp and win may be invalid! */
1943 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1944#endif
1945 }
1946
1947 /*
1948 * if last window has a status line now and we don't want one,
1949 * remove the status line
1950 */
1951 last_status(FALSE);
1952
1953 /* After closing the help window, try restoring the window layout from
1954 * before it was opened. */
1955 if (help_window)
1956 restore_snapshot(close_curwin);
1957
1958#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1959 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
1960 if (gui.in_use && !win_hasvertsplit())
1961 gui_init_which_components(NULL);
1962#endif
1963
1964 redraw_all_later(NOT_VALID);
1965}
1966
1967/*
1968 * Remove a window and its frame from the tree of frames.
1969 * Returns a pointer to the window that got the freed up space.
1970 */
1971/*ARGSUSED*/
1972 static win_T *
1973winframe_remove(win, dirp)
1974 win_T *win;
1975 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
1976{
1977 frame_T *frp, *frp2, *frp3;
1978 frame_T *frp_close = win->w_frame;
1979 win_T *wp;
1980 int old_height = 0;
1981
1982 /*
1983 * Remove the window from its frame.
1984 */
1985 frp2 = win_altframe(win);
1986 wp = frame2win(frp2);
1987
1988 /* Remove this frame from the list of frames. */
1989 frame_remove(frp_close);
1990
1991#ifdef FEAT_VERTSPLIT
1992 if (frp_close->fr_parent->fr_layout == FR_COL)
1993 {
1994#endif
1995 /* When 'winfixheight' is set, remember its old size and restore
1996 * it later (it's a simplistic solution...). Don't do this if the
1997 * window will occupy the full height of the screen. */
1998 if (frp2->fr_win != NULL
1999 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2000 && frp2->fr_win->w_p_wfh)
2001 old_height = frp2->fr_win->w_height;
2002 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2003 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2004 if (old_height != 0)
2005 win_setheight_win(old_height, frp2->fr_win);
2006#ifdef FEAT_VERTSPLIT
2007 *dirp = 'v';
2008 }
2009 else
2010 {
2011 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2012 frp2 == frp_close->fr_next ? TRUE : FALSE);
2013 *dirp = 'h';
2014 }
2015#endif
2016
2017 /* If rows/columns go to a window below/right its positions need to be
2018 * updated. Can only be done after the sizes have been updated. */
2019 if (frp2 == frp_close->fr_next)
2020 {
2021 int row = win->w_winrow;
2022 int col = W_WINCOL(win);
2023
2024 frame_comp_pos(frp2, &row, &col);
2025 }
2026
2027 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2028 {
2029 /* There is no other frame in this list, move its info to the parent
2030 * and remove it. */
2031 frp2->fr_parent->fr_layout = frp2->fr_layout;
2032 frp2->fr_parent->fr_child = frp2->fr_child;
2033 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2034 frp->fr_parent = frp2->fr_parent;
2035 frp2->fr_parent->fr_win = frp2->fr_win;
2036 if (frp2->fr_win != NULL)
2037 frp2->fr_win->w_frame = frp2->fr_parent;
2038 frp = frp2->fr_parent;
2039 vim_free(frp2);
2040
2041 frp2 = frp->fr_parent;
2042 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2043 {
2044 /* The frame above the parent has the same layout, have to merge
2045 * the frames into this list. */
2046 if (frp2->fr_child == frp)
2047 frp2->fr_child = frp->fr_child;
2048 frp->fr_child->fr_prev = frp->fr_prev;
2049 if (frp->fr_prev != NULL)
2050 frp->fr_prev->fr_next = frp->fr_child;
2051 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2052 {
2053 frp3->fr_parent = frp2;
2054 if (frp3->fr_next == NULL)
2055 {
2056 frp3->fr_next = frp->fr_next;
2057 if (frp->fr_next != NULL)
2058 frp->fr_next->fr_prev = frp3;
2059 break;
2060 }
2061 }
2062 vim_free(frp);
2063 }
2064 }
2065
2066 return wp;
2067}
2068
2069/*
2070 * Find out which frame is going to get the freed up space when "win" is
2071 * closed.
2072 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2073 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2074 * This makes opening a window and closing it immediately keep the same window
2075 * layout.
2076 */
2077 static frame_T *
2078win_altframe(win)
2079 win_T *win;
2080{
2081 frame_T *frp;
2082 int b;
2083
2084 frp = win->w_frame;
2085#ifdef FEAT_VERTSPLIT
2086 if (frp->fr_parent->fr_layout == FR_ROW)
2087 b = p_spr;
2088 else
2089#endif
2090 b = p_sb;
2091 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2092 return frp->fr_next;
2093 return frp->fr_prev;
2094}
2095
2096/*
2097 * Find the left-upper window in frame "frp".
2098 */
2099 static win_T *
2100frame2win(frp)
2101 frame_T *frp;
2102{
2103 while (frp->fr_win == NULL)
2104 frp = frp->fr_child;
2105 return frp->fr_win;
2106}
2107
2108/*
2109 * Return TRUE if frame "frp" contains window "wp".
2110 */
2111 static int
2112frame_has_win(frp, wp)
2113 frame_T *frp;
2114 win_T *wp;
2115{
2116 frame_T *p;
2117
2118 if (frp->fr_layout == FR_LEAF)
2119 return frp->fr_win == wp;
2120
2121 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2122 if (frame_has_win(p, wp))
2123 return TRUE;
2124 return FALSE;
2125}
2126
2127/*
2128 * Set a new height for a frame. Recursively sets the height for contained
2129 * frames and windows. Caller must take care of positions.
2130 */
2131 static void
2132frame_new_height(topfrp, height, topfirst, wfh)
2133 frame_T *topfrp;
2134 int height;
2135 int topfirst; /* resize topmost contained frame first */
2136 int wfh; /* obey 'winfixheight' when there is a choice;
2137 may cause the height not to be set */
2138{
2139 frame_T *frp;
2140 int extra_lines;
2141 int h;
2142
2143 if (topfrp->fr_win != NULL)
2144 {
2145 /* Simple case: just one window. */
2146 win_new_height(topfrp->fr_win,
2147 height - topfrp->fr_win->w_status_height);
2148 }
2149#ifdef FEAT_VERTSPLIT
2150 else if (topfrp->fr_layout == FR_ROW)
2151 {
2152 do
2153 {
2154 /* All frames in this row get the same new height. */
2155 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2156 {
2157 frame_new_height(frp, height, topfirst, wfh);
2158 if (frp->fr_height > height)
2159 {
2160 /* Could not fit the windows, make the whole row higher. */
2161 height = frp->fr_height;
2162 break;
2163 }
2164 }
2165 }
2166 while (frp != NULL);
2167 }
2168#endif
2169 else
2170 {
2171 /* Complicated case: Resize a column of frames. Resize the bottom
2172 * frame first, frames above that when needed. */
2173
2174 frp = topfrp->fr_child;
2175 if (wfh)
2176 /* Advance past frames with one window with 'wfh' set. */
2177 while (frame_fixed_height(frp))
2178 {
2179 frp = frp->fr_next;
2180 if (frp == NULL)
2181 return; /* no frame without 'wfh', give up */
2182 }
2183 if (!topfirst)
2184 {
2185 /* Find the bottom frame of this column */
2186 while (frp->fr_next != NULL)
2187 frp = frp->fr_next;
2188 if (wfh)
2189 /* Advance back for frames with one window with 'wfh' set. */
2190 while (frame_fixed_height(frp))
2191 frp = frp->fr_prev;
2192 }
2193
2194 extra_lines = height - topfrp->fr_height;
2195 if (extra_lines < 0)
2196 {
2197 /* reduce height of contained frames, bottom or top frame first */
2198 while (frp != NULL)
2199 {
2200 h = frame_minheight(frp, NULL);
2201 if (frp->fr_height + extra_lines < h)
2202 {
2203 extra_lines += frp->fr_height - h;
2204 frame_new_height(frp, h, topfirst, wfh);
2205 }
2206 else
2207 {
2208 frame_new_height(frp, frp->fr_height + extra_lines,
2209 topfirst, wfh);
2210 break;
2211 }
2212 if (topfirst)
2213 {
2214 do
2215 frp = frp->fr_next;
2216 while (wfh && frp != NULL && frame_fixed_height(frp));
2217 }
2218 else
2219 {
2220 do
2221 frp = frp->fr_prev;
2222 while (wfh && frp != NULL && frame_fixed_height(frp));
2223 }
2224 /* Increase "height" if we could not reduce enough frames. */
2225 if (frp == NULL)
2226 height -= extra_lines;
2227 }
2228 }
2229 else if (extra_lines > 0)
2230 {
2231 /* increase height of bottom or top frame */
2232 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2233 }
2234 }
2235 topfrp->fr_height = height;
2236}
2237
2238/*
2239 * Return TRUE if height of frame "frp" should not be changed because of
2240 * the 'winfixheight' option.
2241 */
2242 static int
2243frame_fixed_height(frp)
2244 frame_T *frp;
2245{
2246 /* frame with one window: fixed height if 'winfixheight' set. */
2247 if (frp->fr_win != NULL)
2248 return frp->fr_win->w_p_wfh;
2249
2250 if (frp->fr_layout == FR_ROW)
2251 {
2252 /* The frame is fixed height if one of the frames in the row is fixed
2253 * height. */
2254 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2255 if (frame_fixed_height(frp))
2256 return TRUE;
2257 return FALSE;
2258 }
2259
2260 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2261 * frames in the row are fixed height. */
2262 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2263 if (!frame_fixed_height(frp))
2264 return FALSE;
2265 return TRUE;
2266}
2267
2268#ifdef FEAT_VERTSPLIT
2269/*
2270 * Add a status line to windows at the bottom of "frp".
2271 * Note: Does not check if there is room!
2272 */
2273 static void
2274frame_add_statusline(frp)
2275 frame_T *frp;
2276{
2277 win_T *wp;
2278
2279 if (frp->fr_layout == FR_LEAF)
2280 {
2281 wp = frp->fr_win;
2282 if (wp->w_status_height == 0)
2283 {
2284 if (wp->w_height > 0) /* don't make it negative */
2285 --wp->w_height;
2286 wp->w_status_height = STATUS_HEIGHT;
2287 }
2288 }
2289 else if (frp->fr_layout == FR_ROW)
2290 {
2291 /* Handle all the frames in the row. */
2292 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2293 frame_add_statusline(frp);
2294 }
2295 else /* frp->fr_layout == FR_COL */
2296 {
2297 /* Only need to handle the last frame in the column. */
2298 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2299 ;
2300 frame_add_statusline(frp);
2301 }
2302}
2303
2304/*
2305 * Set width of a frame. Handles recursively going through contained frames.
2306 * May remove separator line for windows at the right side (for win_close()).
2307 */
2308 static void
2309frame_new_width(topfrp, width, leftfirst)
2310 frame_T *topfrp;
2311 int width;
2312 int leftfirst; /* resize leftmost contained frame first */
2313{
2314 frame_T *frp;
2315 int extra_cols;
2316 int w;
2317 win_T *wp;
2318
2319 if (topfrp->fr_layout == FR_LEAF)
2320 {
2321 /* Simple case: just one window. */
2322 wp = topfrp->fr_win;
2323 /* Find out if there are any windows right of this one. */
2324 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2325 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2326 break;
2327 if (frp->fr_parent == NULL)
2328 wp->w_vsep_width = 0;
2329 win_new_width(wp, width - wp->w_vsep_width);
2330 }
2331 else if (topfrp->fr_layout == FR_COL)
2332 {
2333 /* All frames in this column get the same new width. */
2334 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2335 frame_new_width(frp, width, leftfirst);
2336 }
2337 else /* fr_layout == FR_ROW */
2338 {
2339 /* Complicated case: Resize a row of frames. Resize the rightmost
2340 * frame first, frames left of it when needed. */
2341
2342 /* Find the rightmost frame of this row */
2343 frp = topfrp->fr_child;
2344 if (!leftfirst)
2345 while (frp->fr_next != NULL)
2346 frp = frp->fr_next;
2347
2348 extra_cols = width - topfrp->fr_width;
2349 if (extra_cols < 0)
2350 {
2351 /* reduce frame width, rightmost frame first */
2352 while (frp != NULL)
2353 {
2354 w = frame_minwidth(frp, NULL);
2355 if (frp->fr_width + extra_cols < w)
2356 {
2357 extra_cols += frp->fr_width - w;
2358 frame_new_width(frp, w, leftfirst);
2359 }
2360 else
2361 {
2362 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2363 break;
2364 }
2365 if (leftfirst)
2366 frp = frp->fr_next;
2367 else
2368 frp = frp->fr_prev;
2369 }
2370 }
2371 else if (extra_cols > 0)
2372 {
2373 /* increase width of rightmost frame */
2374 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2375 }
2376 }
2377 topfrp->fr_width = width;
2378}
2379
2380/*
2381 * Add the vertical separator to windows at the right side of "frp".
2382 * Note: Does not check if there is room!
2383 */
2384 static void
2385frame_add_vsep(frp)
2386 frame_T *frp;
2387{
2388 win_T *wp;
2389
2390 if (frp->fr_layout == FR_LEAF)
2391 {
2392 wp = frp->fr_win;
2393 if (wp->w_vsep_width == 0)
2394 {
2395 if (wp->w_width > 0) /* don't make it negative */
2396 --wp->w_width;
2397 wp->w_vsep_width = 1;
2398 }
2399 }
2400 else if (frp->fr_layout == FR_COL)
2401 {
2402 /* Handle all the frames in the column. */
2403 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2404 frame_add_vsep(frp);
2405 }
2406 else /* frp->fr_layout == FR_ROW */
2407 {
2408 /* Only need to handle the last frame in the row. */
2409 frp = frp->fr_child;
2410 while (frp->fr_next != NULL)
2411 frp = frp->fr_next;
2412 frame_add_vsep(frp);
2413 }
2414}
2415
2416/*
2417 * Set frame width from the window it contains.
2418 */
2419 static void
2420frame_fix_width(wp)
2421 win_T *wp;
2422{
2423 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2424}
2425#endif
2426
2427/*
2428 * Set frame height from the window it contains.
2429 */
2430 static void
2431frame_fix_height(wp)
2432 win_T *wp;
2433{
2434 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2435}
2436
2437/*
2438 * Compute the minimal height for frame "topfrp".
2439 * Uses the 'winminheight' option.
2440 * When "next_curwin" isn't NULL, use p_wh for this window.
2441 * When "next_curwin" is NOWIN, don't use at least one line for the current
2442 * window.
2443 */
2444 static int
2445frame_minheight(topfrp, next_curwin)
2446 frame_T *topfrp;
2447 win_T *next_curwin;
2448{
2449 frame_T *frp;
2450 int m;
2451#ifdef FEAT_VERTSPLIT
2452 int n;
2453#endif
2454
2455 if (topfrp->fr_win != NULL)
2456 {
2457 if (topfrp->fr_win == next_curwin)
2458 m = p_wh + topfrp->fr_win->w_status_height;
2459 else
2460 {
2461 /* window: minimal height of the window plus status line */
2462 m = p_wmh + topfrp->fr_win->w_status_height;
2463 /* Current window is minimal one line high */
2464 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2465 ++m;
2466 }
2467 }
2468#ifdef FEAT_VERTSPLIT
2469 else if (topfrp->fr_layout == FR_ROW)
2470 {
2471 /* get the minimal height from each frame in this row */
2472 m = 0;
2473 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2474 {
2475 n = frame_minheight(frp, next_curwin);
2476 if (n > m)
2477 m = n;
2478 }
2479 }
2480#endif
2481 else
2482 {
2483 /* Add up the minimal heights for all frames in this column. */
2484 m = 0;
2485 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2486 m += frame_minheight(frp, next_curwin);
2487 }
2488
2489 return m;
2490}
2491
2492#ifdef FEAT_VERTSPLIT
2493/*
2494 * Compute the minimal width for frame "topfrp".
2495 * When "next_curwin" isn't NULL, use p_wiw for this window.
2496 * When "next_curwin" is NOWIN, don't use at least one column for the current
2497 * window.
2498 */
2499 static int
2500frame_minwidth(topfrp, next_curwin)
2501 frame_T *topfrp;
2502 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2503{
2504 frame_T *frp;
2505 int m, n;
2506
2507 if (topfrp->fr_win != NULL)
2508 {
2509 if (topfrp->fr_win == next_curwin)
2510 m = p_wiw + topfrp->fr_win->w_vsep_width;
2511 else
2512 {
2513 /* window: minimal width of the window plus separator column */
2514 m = p_wmw + topfrp->fr_win->w_vsep_width;
2515 /* Current window is minimal one column wide */
2516 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2517 ++m;
2518 }
2519 }
2520 else if (topfrp->fr_layout == FR_COL)
2521 {
2522 /* get the minimal width from each frame in this column */
2523 m = 0;
2524 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2525 {
2526 n = frame_minwidth(frp, next_curwin);
2527 if (n > m)
2528 m = n;
2529 }
2530 }
2531 else
2532 {
2533 /* Add up the minimal widths for all frames in this row. */
2534 m = 0;
2535 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2536 m += frame_minwidth(frp, next_curwin);
2537 }
2538
2539 return m;
2540}
2541#endif
2542
2543
2544/*
2545 * Try to close all windows except current one.
2546 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2547 * used and the buffer was modified.
2548 *
2549 * Used by ":bdel" and ":only".
2550 */
2551 void
2552close_others(message, forceit)
2553 int message;
2554 int forceit; /* always hide all other windows */
2555{
2556 win_T *wp;
2557 win_T *nextwp;
2558 int r;
2559
2560 if (lastwin == firstwin)
2561 {
2562 if (message
2563#ifdef FEAT_AUTOCMD
2564 && !autocmd_busy
2565#endif
2566 )
2567 MSG(_("Already only one window"));
2568 return;
2569 }
2570
2571 /* Be very careful here: autocommands may change the window layout. */
2572 for (wp = firstwin; win_valid(wp); wp = nextwp)
2573 {
2574 nextwp = wp->w_next;
2575 if (wp != curwin) /* don't close current window */
2576 {
2577
2578 /* Check if it's allowed to abandon this window */
2579 r = can_abandon(wp->w_buffer, forceit);
2580#ifdef FEAT_AUTOCMD
2581 if (!win_valid(wp)) /* autocommands messed wp up */
2582 {
2583 nextwp = firstwin;
2584 continue;
2585 }
2586#endif
2587 if (!r)
2588 {
2589#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2590 if (message && (p_confirm || cmdmod.confirm) && p_write)
2591 {
2592 dialog_changed(wp->w_buffer, FALSE);
2593# ifdef FEAT_AUTOCMD
2594 if (!win_valid(wp)) /* autocommands messed wp up */
2595 {
2596 nextwp = firstwin;
2597 continue;
2598 }
2599# endif
2600 }
2601 if (bufIsChanged(wp->w_buffer))
2602#endif
2603 continue;
2604 }
2605 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2606 }
2607 }
2608
2609 /*
2610 * If current window has a status line and we don't want one,
2611 * remove the status line.
2612 */
2613 if (lastwin != firstwin)
2614 EMSG(_("E445: Other window contains changes"));
2615}
2616
2617#endif /* FEAT_WINDOWS */
2618
2619/*
2620 * init the cursor in the window
2621 *
2622 * called when a new file is being edited
2623 */
2624 void
2625win_init(wp)
2626 win_T *wp;
2627{
2628 redraw_win_later(wp, NOT_VALID);
2629 wp->w_lines_valid = 0;
2630 wp->w_cursor.lnum = 1;
2631 wp->w_curswant = wp->w_cursor.col = 0;
2632#ifdef FEAT_VIRTUALEDIT
2633 wp->w_cursor.coladd = 0;
2634#endif
2635 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2636 wp->w_pcmark.col = 0;
2637 wp->w_prev_pcmark.lnum = 0;
2638 wp->w_prev_pcmark.col = 0;
2639 wp->w_topline = 1;
2640#ifdef FEAT_DIFF
2641 wp->w_topfill = 0;
2642#endif
2643 wp->w_botline = 2;
2644#ifdef FEAT_FKMAP
2645 if (curwin->w_p_rl)
2646 wp->w_farsi = W_CONV + W_R_L;
2647 else
2648 wp->w_farsi = W_CONV;
2649#endif
2650}
2651
2652/*
2653 * Allocate the first window and put an empty buffer in it.
2654 * Called from main().
2655 * When this fails we can't do anything: exit.
2656 */
2657 void
2658win_alloc_first()
2659{
2660 curwin = win_alloc(NULL);
2661 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2662 if (curwin == NULL || curbuf == NULL)
2663 mch_exit(0);
2664 curwin->w_buffer = curbuf;
2665 curbuf->b_nwindows = 1; /* there is one window */
2666#ifdef FEAT_WINDOWS
2667 curwin->w_alist = &global_alist;
2668#endif
2669 win_init(curwin); /* init current window */
2670
2671 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2672 if (topframe == NULL)
2673 mch_exit(0);
2674 topframe->fr_layout = FR_LEAF;
2675#ifdef FEAT_VERTSPLIT
2676 topframe->fr_width = Columns;
2677#endif
2678 topframe->fr_height = Rows - p_ch;
2679 topframe->fr_win = curwin;
2680 curwin->w_frame = topframe;
2681}
2682
2683#if defined(FEAT_WINDOWS) || defined(PROTO)
2684
2685/*
2686 * Go to another window.
2687 * When jumping to another buffer, stop Visual mode. Do this before
2688 * changing windows so we can yank the selection into the '*' register.
2689 * When jumping to another window on the same buffer, adjust its cursor
2690 * position to keep the same Visual area.
2691 */
2692 void
2693win_goto(wp)
2694 win_T *wp;
2695{
2696#ifdef FEAT_CMDWIN
2697 if (cmdwin_type != 0)
2698 {
2699 beep_flush();
2700 return;
2701 }
2702#endif
2703#ifdef FEAT_VISUAL
2704 if (wp->w_buffer != curbuf)
2705 reset_VIsual_and_resel();
2706 else if (VIsual_active)
2707 wp->w_cursor = curwin->w_cursor;
2708#endif
2709
2710#ifdef FEAT_GUI
2711 need_mouse_correct = TRUE;
2712#endif
2713 win_enter(wp, TRUE);
2714}
2715
2716#if defined(FEAT_PERL) || defined(PROTO)
2717/*
2718 * Find window number "winnr" (counting top to bottom).
2719 */
2720 win_T *
2721win_find_nr(winnr)
2722 int winnr;
2723{
2724 win_T *wp;
2725
2726# ifdef FEAT_WINDOWS
2727 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2728 if (--winnr == 0)
2729 break;
2730 return wp;
2731# else
2732 return curwin;
2733# endif
2734}
2735#endif
2736
2737#ifdef FEAT_VERTSPLIT
2738/*
2739 * Move to window above or below "count" times.
2740 */
2741 static void
2742win_goto_ver(up, count)
2743 int up; /* TRUE to go to win above */
2744 long count;
2745{
2746 frame_T *fr;
2747 frame_T *nfr;
2748 frame_T *foundfr;
2749
2750 foundfr = curwin->w_frame;
2751 while (count--)
2752 {
2753 /*
2754 * First go upwards in the tree of frames until we find a upwards or
2755 * downwards neighbor.
2756 */
2757 fr = foundfr;
2758 for (;;)
2759 {
2760 if (fr == topframe)
2761 goto end;
2762 if (up)
2763 nfr = fr->fr_prev;
2764 else
2765 nfr = fr->fr_next;
2766 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
2767 break;
2768 fr = fr->fr_parent;
2769 }
2770
2771 /*
2772 * Now go downwards to find the bottom or top frame in it.
2773 */
2774 for (;;)
2775 {
2776 if (nfr->fr_layout == FR_LEAF)
2777 {
2778 foundfr = nfr;
2779 break;
2780 }
2781 fr = nfr->fr_child;
2782 if (nfr->fr_layout == FR_ROW)
2783 {
2784 /* Find the frame at the cursor row. */
2785 while (fr->fr_next != NULL
2786 && frame2win(fr)->w_wincol + fr->fr_width
2787 <= curwin->w_wincol + curwin->w_wcol)
2788 fr = fr->fr_next;
2789 }
2790 if (nfr->fr_layout == FR_COL && up)
2791 while (fr->fr_next != NULL)
2792 fr = fr->fr_next;
2793 nfr = fr;
2794 }
2795 }
2796end:
2797 if (foundfr != NULL)
2798 win_goto(foundfr->fr_win);
2799}
2800
2801/*
2802 * Move to left or right window.
2803 */
2804 static void
2805win_goto_hor(left, count)
2806 int left; /* TRUE to go to left win */
2807 long count;
2808{
2809 frame_T *fr;
2810 frame_T *nfr;
2811 frame_T *foundfr;
2812
2813 foundfr = curwin->w_frame;
2814 while (count--)
2815 {
2816 /*
2817 * First go upwards in the tree of frames until we find a left or
2818 * right neighbor.
2819 */
2820 fr = foundfr;
2821 for (;;)
2822 {
2823 if (fr == topframe)
2824 goto end;
2825 if (left)
2826 nfr = fr->fr_prev;
2827 else
2828 nfr = fr->fr_next;
2829 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
2830 break;
2831 fr = fr->fr_parent;
2832 }
2833
2834 /*
2835 * Now go downwards to find the leftmost or rightmost frame in it.
2836 */
2837 for (;;)
2838 {
2839 if (nfr->fr_layout == FR_LEAF)
2840 {
2841 foundfr = nfr;
2842 break;
2843 }
2844 fr = nfr->fr_child;
2845 if (nfr->fr_layout == FR_COL)
2846 {
2847 /* Find the frame at the cursor row. */
2848 while (fr->fr_next != NULL
2849 && frame2win(fr)->w_winrow + fr->fr_height
2850 <= curwin->w_winrow + curwin->w_wrow)
2851 fr = fr->fr_next;
2852 }
2853 if (nfr->fr_layout == FR_ROW && left)
2854 while (fr->fr_next != NULL)
2855 fr = fr->fr_next;
2856 nfr = fr;
2857 }
2858 }
2859end:
2860 if (foundfr != NULL)
2861 win_goto(foundfr->fr_win);
2862}
2863#endif
2864
2865/*
2866 * Make window "wp" the current window.
2867 */
2868 void
2869win_enter(wp, undo_sync)
2870 win_T *wp;
2871 int undo_sync;
2872{
2873 win_enter_ext(wp, undo_sync, FALSE);
2874}
2875
2876/*
2877 * Make window wp the current window.
2878 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
2879 * been closed and isn't valid.
2880 */
2881 static void
2882win_enter_ext(wp, undo_sync, curwin_invalid)
2883 win_T *wp;
2884 int undo_sync;
2885 int curwin_invalid;
2886{
2887#ifdef FEAT_AUTOCMD
2888 int other_buffer = FALSE;
2889#endif
2890
2891 if (wp == curwin && !curwin_invalid) /* nothing to do */
2892 return;
2893
2894#ifdef FEAT_AUTOCMD
2895 if (!curwin_invalid)
2896 {
2897 /*
2898 * Be careful: If autocommands delete the window, return now.
2899 */
2900 if (wp->w_buffer != curbuf)
2901 {
2902 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2903 other_buffer = TRUE;
2904 if (!win_valid(wp))
2905 return;
2906 }
2907 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2908 if (!win_valid(wp))
2909 return;
2910# ifdef FEAT_EVAL
2911 /* autocmds may abort script processing */
2912 if (aborting())
2913 return;
2914# endif
2915 }
2916#endif
2917
2918 /* sync undo before leaving the current buffer */
2919 if (undo_sync && curbuf != wp->w_buffer)
2920 u_sync();
2921 /* may have to copy the buffer options when 'cpo' contains 'S' */
2922 if (wp->w_buffer != curbuf)
2923 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
2924 if (!curwin_invalid)
2925 {
2926 prevwin = curwin; /* remember for CTRL-W p */
2927 curwin->w_redr_status = TRUE;
2928 }
2929 curwin = wp;
2930 curbuf = wp->w_buffer;
2931 check_cursor();
2932#ifdef FEAT_VIRTUALEDIT
2933 if (!virtual_active())
2934 curwin->w_cursor.coladd = 0;
2935#endif
2936 changed_line_abv_curs(); /* assume cursor position needs updating */
2937
2938 if (curwin->w_localdir != NULL)
2939 {
2940 /* Window has a local directory: Save current directory as global
2941 * directory (unless that was done already) and change to the local
2942 * directory. */
2943 if (globaldir == NULL)
2944 {
2945 char_u cwd[MAXPATHL];
2946
2947 if (mch_dirname(cwd, MAXPATHL) == OK)
2948 globaldir = vim_strsave(cwd);
2949 }
2950 mch_chdir((char *)curwin->w_localdir);
2951 shorten_fnames(TRUE);
2952 }
2953 else if (globaldir != NULL)
2954 {
2955 /* Window doesn't have a local directory and we are not in the global
2956 * directory: Change to the global directory. */
2957 mch_chdir((char *)globaldir);
2958 vim_free(globaldir);
2959 globaldir = NULL;
2960 shorten_fnames(TRUE);
2961 }
2962
2963#ifdef FEAT_AUTOCMD
2964 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2965 if (other_buffer)
2966 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2967#endif
2968
2969#ifdef FEAT_TITLE
2970 maketitle();
2971#endif
2972 curwin->w_redr_status = TRUE;
2973 if (restart_edit)
2974 redraw_later(VALID); /* causes status line redraw */
2975
2976 /* set window height to desired minimal value */
2977 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
2978 win_setheight((int)p_wh);
2979 else if (curwin->w_height == 0)
2980 win_setheight(1);
2981
2982#ifdef FEAT_VERTSPLIT
2983 /* set window width to desired minimal value */
2984 if (curwin->w_width < p_wiw)
2985 win_setwidth((int)p_wiw);
2986#endif
2987
2988#ifdef FEAT_MOUSE
2989 setmouse(); /* in case jumped to/from help buffer */
2990#endif
2991
2992#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
2993 /* Change directories when the acd option is set on and after
2994 * switching windows. */
2995 if (p_acd && curbuf->b_ffname != NULL
2996 && vim_chdirfile(curbuf->b_ffname) == OK)
2997 shorten_fnames(TRUE);
2998#endif
2999}
3000
3001#endif /* FEAT_WINDOWS */
3002
3003#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3004/*
3005 * Jump to the first open window that contains buffer buf if one exists
3006 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3007 * Returns pointer to window if it exists, otherwise NULL.
3008 */
3009 win_T *
3010buf_jump_open_win(buf)
3011 buf_T *buf;
3012{
3013# ifdef FEAT_WINDOWS
3014 win_T *wp;
3015
3016 for (wp = firstwin; wp; wp = wp->w_next)
3017 if (wp->w_buffer == buf)
3018 break;
3019 if (wp != NULL)
3020 win_enter(wp, FALSE);
3021 return wp;
3022# else
3023 if (curwin->w_buffer == buf)
3024 return curwin;
3025 return NULL;
3026# endif
3027}
3028#endif
3029
3030/*
3031 * allocate a window structure and link it in the window list
3032 */
3033/*ARGSUSED*/
3034 static win_T *
3035win_alloc(after)
3036 win_T *after;
3037{
3038 win_T *newwin;
3039
3040 /*
3041 * allocate window structure and linesizes arrays
3042 */
3043 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3044 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3045 {
3046 vim_free(newwin);
3047 newwin = NULL;
3048 }
3049
3050 if (newwin != NULL)
3051 {
3052 /*
3053 * link the window in the window list
3054 */
3055#ifdef FEAT_WINDOWS
3056 win_append(after, newwin);
3057#endif
3058#ifdef FEAT_VERTSPLIT
3059 newwin->w_wincol = 0;
3060 newwin->w_width = Columns;
3061#endif
3062
3063 /* position the display and the cursor at the top of the file. */
3064 newwin->w_topline = 1;
3065#ifdef FEAT_DIFF
3066 newwin->w_topfill = 0;
3067#endif
3068 newwin->w_botline = 2;
3069 newwin->w_cursor.lnum = 1;
3070#ifdef FEAT_SCROLLBIND
3071 newwin->w_scbind_pos = 1;
3072#endif
3073
3074 /* We won't calculate w_fraction until resizing the window */
3075 newwin->w_fraction = 0;
3076 newwin->w_prev_fraction_row = -1;
3077
3078#ifdef FEAT_GUI
3079 if (gui.in_use)
3080 {
3081 out_flush();
3082 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3083 SBAR_LEFT, newwin);
3084 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3085 SBAR_RIGHT, newwin);
3086 }
3087#endif
3088#ifdef FEAT_EVAL
3089 var_init(&newwin->w_vars); /* init internal variables */
3090#endif
3091#ifdef FEAT_FOLDING
3092 foldInitWin(newwin);
3093#endif
3094 }
3095 return newwin;
3096}
3097
3098#if defined(FEAT_WINDOWS) || defined(PROTO)
3099
3100/*
3101 * remove window 'wp' from the window list and free the structure
3102 */
3103 static void
3104win_free(wp)
3105 win_T *wp;
3106{
3107 int i;
3108
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003109#ifdef FEAT_MZSCHEME
3110 mzscheme_window_free(wp);
3111#endif
3112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef FEAT_PERL
3114 perl_win_free(wp);
3115#endif
3116
3117#ifdef FEAT_PYTHON
3118 python_window_free(wp);
3119#endif
3120
3121#ifdef FEAT_TCL
3122 tcl_window_free(wp);
3123#endif
3124
3125#ifdef FEAT_RUBY
3126 ruby_window_free(wp);
3127#endif
3128
3129 clear_winopt(&wp->w_onebuf_opt);
3130 clear_winopt(&wp->w_allbuf_opt);
3131
3132#ifdef FEAT_EVAL
3133 var_clear(&wp->w_vars); /* free all internal variables */
3134#endif
3135
3136 if (prevwin == wp)
3137 prevwin = NULL;
3138 win_free_lsize(wp);
3139
3140 for (i = 0; i < wp->w_tagstacklen; ++i)
3141 vim_free(wp->w_tagstack[i].tagname);
3142
3143 vim_free(wp->w_localdir);
3144#ifdef FEAT_SEARCH_EXTRA
3145 vim_free(wp->w_match.regprog);
3146#endif
3147#ifdef FEAT_JUMPLIST
3148 free_jumplist(wp);
3149#endif
3150
3151#ifdef FEAT_GUI
3152 if (gui.in_use)
3153 {
3154 out_flush();
3155 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3156 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3157 }
3158#endif /* FEAT_GUI */
3159
3160 win_remove(wp);
3161 vim_free(wp);
3162}
3163
3164/*
3165 * Append window "wp" in the window list after window "after".
3166 */
3167 static void
3168win_append(after, wp)
3169 win_T *after, *wp;
3170{
3171 win_T *before;
3172
3173 if (after == NULL) /* after NULL is in front of the first */
3174 before = firstwin;
3175 else
3176 before = after->w_next;
3177
3178 wp->w_next = before;
3179 wp->w_prev = after;
3180 if (after == NULL)
3181 firstwin = wp;
3182 else
3183 after->w_next = wp;
3184 if (before == NULL)
3185 lastwin = wp;
3186 else
3187 before->w_prev = wp;
3188}
3189
3190/*
3191 * Remove a window from the window list.
3192 */
3193 static void
3194win_remove(wp)
3195 win_T *wp;
3196{
3197 if (wp->w_prev != NULL)
3198 wp->w_prev->w_next = wp->w_next;
3199 else
3200 firstwin = wp->w_next;
3201 if (wp->w_next != NULL)
3202 wp->w_next->w_prev = wp->w_prev;
3203 else
3204 lastwin = wp->w_prev;
3205}
3206
3207/*
3208 * Append frame "frp" in a frame list after frame "after".
3209 */
3210 static void
3211frame_append(after, frp)
3212 frame_T *after, *frp;
3213{
3214 frp->fr_next = after->fr_next;
3215 after->fr_next = frp;
3216 if (frp->fr_next != NULL)
3217 frp->fr_next->fr_prev = frp;
3218 frp->fr_prev = after;
3219}
3220
3221/*
3222 * Insert frame "frp" in a frame list before frame "before".
3223 */
3224 static void
3225frame_insert(before, frp)
3226 frame_T *before, *frp;
3227{
3228 frp->fr_next = before;
3229 frp->fr_prev = before->fr_prev;
3230 before->fr_prev = frp;
3231 if (frp->fr_prev != NULL)
3232 frp->fr_prev->fr_next = frp;
3233 else
3234 frp->fr_parent->fr_child = frp;
3235}
3236
3237/*
3238 * Remove a frame from a frame list.
3239 */
3240 static void
3241frame_remove(frp)
3242 frame_T *frp;
3243{
3244 if (frp->fr_prev != NULL)
3245 frp->fr_prev->fr_next = frp->fr_next;
3246 else
3247 frp->fr_parent->fr_child = frp->fr_next;
3248 if (frp->fr_next != NULL)
3249 frp->fr_next->fr_prev = frp->fr_prev;
3250}
3251
3252#endif /* FEAT_WINDOWS */
3253
3254/*
3255 * Allocate w_lines[] for window "wp".
3256 * Return FAIL for failure, OK for success.
3257 */
3258 int
3259win_alloc_lines(wp)
3260 win_T *wp;
3261{
3262 wp->w_lines_valid = 0;
3263 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3264 if (wp->w_lines == NULL)
3265 return FAIL;
3266 return OK;
3267}
3268
3269/*
3270 * free lsize arrays for a window
3271 */
3272 void
3273win_free_lsize(wp)
3274 win_T *wp;
3275{
3276 vim_free(wp->w_lines);
3277 wp->w_lines = NULL;
3278}
3279
3280/*
3281 * Called from win_new_shellsize() after Rows changed.
3282 */
3283 void
3284shell_new_rows()
3285{
3286 int h = (int)(Rows - p_ch);
3287
3288 if (firstwin == NULL) /* not initialized yet */
3289 return;
3290#ifdef FEAT_WINDOWS
3291 if (h < frame_minheight(topframe, NULL))
3292 h = frame_minheight(topframe, NULL);
3293 /* First try setting the heights of windows without 'winfixheight'. If
3294 * that doesn't result in the right height, forget about that option. */
3295 frame_new_height(topframe, h, FALSE, TRUE);
3296 if (topframe->fr_height != h)
3297 frame_new_height(topframe, h, FALSE, FALSE);
3298
3299 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3300#else
3301 if (h < 1)
3302 h = 1;
3303 win_new_height(firstwin, h);
3304#endif
3305 compute_cmdrow();
3306#if 0
3307 /* Disabled: don't want making the screen smaller make a window larger. */
3308 if (p_ea)
3309 win_equal(curwin, FALSE, 'v');
3310#endif
3311}
3312
3313#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3314/*
3315 * Called from win_new_shellsize() after Columns changed.
3316 */
3317 void
3318shell_new_columns()
3319{
3320 if (firstwin == NULL) /* not initialized yet */
3321 return;
3322 frame_new_width(topframe, (int)Columns, FALSE);
3323 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3324#if 0
3325 /* Disabled: don't want making the screen smaller make a window larger. */
3326 if (p_ea)
3327 win_equal(curwin, FALSE, 'h');
3328#endif
3329}
3330#endif
3331
3332#if defined(FEAT_CMDWIN) || defined(PROTO)
3333/*
3334 * Save the size of all windows in "gap".
3335 */
3336 void
3337win_size_save(gap)
3338 garray_T *gap;
3339
3340{
3341 win_T *wp;
3342
3343 ga_init2(gap, (int)sizeof(int), 1);
3344 if (ga_grow(gap, win_count() * 2) == OK)
3345 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3346 {
3347 ((int *)gap->ga_data)[gap->ga_len++] =
3348 wp->w_width + wp->w_vsep_width;
3349 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3350 }
3351}
3352
3353/*
3354 * Restore window sizes, but only if the number of windows is still the same.
3355 * Does not free the growarray.
3356 */
3357 void
3358win_size_restore(gap)
3359 garray_T *gap;
3360{
3361 win_T *wp;
3362 int i;
3363
3364 if (win_count() * 2 == gap->ga_len)
3365 {
3366 i = 0;
3367 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3368 {
3369 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3370 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3371 }
3372 /* recompute the window positions */
3373 (void)win_comp_pos();
3374 }
3375}
3376#endif /* FEAT_CMDWIN */
3377
3378#if defined(FEAT_WINDOWS) || defined(PROTO)
3379/*
3380 * Update the position for all windows, using the width and height of the
3381 * frames.
3382 * Returns the row just after the last window.
3383 */
3384 static int
3385win_comp_pos()
3386{
3387 int row = 0;
3388 int col = 0;
3389
3390 frame_comp_pos(topframe, &row, &col);
3391 return row;
3392}
3393
3394/*
3395 * Update the position of the windows in frame "topfrp", using the width and
3396 * height of the frames.
3397 * "*row" and "*col" are the top-left position of the frame. They are updated
3398 * to the bottom-right position plus one.
3399 */
3400 static void
3401frame_comp_pos(topfrp, row, col)
3402 frame_T *topfrp;
3403 int *row;
3404 int *col;
3405{
3406 win_T *wp;
3407 frame_T *frp;
3408#ifdef FEAT_VERTSPLIT
3409 int startcol;
3410 int startrow;
3411#endif
3412
3413 wp = topfrp->fr_win;
3414 if (wp != NULL)
3415 {
3416 if (wp->w_winrow != *row
3417#ifdef FEAT_VERTSPLIT
3418 || wp->w_wincol != *col
3419#endif
3420 )
3421 {
3422 /* position changed, redraw */
3423 wp->w_winrow = *row;
3424#ifdef FEAT_VERTSPLIT
3425 wp->w_wincol = *col;
3426#endif
3427 redraw_win_later(wp, NOT_VALID);
3428 wp->w_redr_status = TRUE;
3429 }
3430 *row += wp->w_height + wp->w_status_height;
3431#ifdef FEAT_VERTSPLIT
3432 *col += wp->w_width + wp->w_vsep_width;
3433#endif
3434 }
3435 else
3436 {
3437#ifdef FEAT_VERTSPLIT
3438 startrow = *row;
3439 startcol = *col;
3440#endif
3441 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3442 {
3443#ifdef FEAT_VERTSPLIT
3444 if (topfrp->fr_layout == FR_ROW)
3445 *row = startrow; /* all frames are at the same row */
3446 else
3447 *col = startcol; /* all frames are at the same col */
3448#endif
3449 frame_comp_pos(frp, row, col);
3450 }
3451 }
3452}
3453
3454#endif /* FEAT_WINDOWS */
3455
3456/*
3457 * Set current window height and take care of repositioning other windows to
3458 * fit around it.
3459 */
3460 void
3461win_setheight(height)
3462 int height;
3463{
3464 win_setheight_win(height, curwin);
3465}
3466
3467/*
3468 * Set the window height of window "win" and take care of repositioning other
3469 * windows to fit around it.
3470 */
3471 void
3472win_setheight_win(height, win)
3473 int height;
3474 win_T *win;
3475{
3476 int row;
3477
3478 if (win == curwin)
3479 {
3480 /* Always keep current window at least one line high, even when
3481 * 'winminheight' is zero. */
3482#ifdef FEAT_WINDOWS
3483 if (height < p_wmh)
3484 height = p_wmh;
3485#endif
3486 if (height == 0)
3487 height = 1;
3488 }
3489
3490#ifdef FEAT_WINDOWS
3491 frame_setheight(win->w_frame, height + win->w_status_height);
3492
3493 /* recompute the window positions */
3494 row = win_comp_pos();
3495#else
3496 if (height > topframe->fr_height)
3497 height = topframe->fr_height;
3498 win->w_height = height;
3499 row = height;
3500#endif
3501
3502 /*
3503 * If there is extra space created between the last window and the command
3504 * line, clear it.
3505 */
3506 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
3507 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
3508 cmdline_row = row;
3509 msg_row = row;
3510 msg_col = 0;
3511
3512 redraw_all_later(NOT_VALID);
3513}
3514
3515#if defined(FEAT_WINDOWS) || defined(PROTO)
3516
3517/*
3518 * Set the height of a frame to "height" and take care that all frames and
3519 * windows inside it are resized. Also resize frames on the left and right if
3520 * the are in the same FR_ROW frame.
3521 *
3522 * Strategy:
3523 * If the frame is part of a FR_COL frame, try fitting the frame in that
3524 * frame. If that doesn't work (the FR_COL frame is too small), recursively
3525 * go to containing frames to resize them and make room.
3526 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
3527 * Check for the minimal height of the FR_ROW frame.
3528 * At the top level we can also use change the command line height.
3529 */
3530 static void
3531frame_setheight(curfrp, height)
3532 frame_T *curfrp;
3533 int height;
3534{
3535 int room; /* total number of lines available */
3536 int take; /* number of lines taken from other windows */
3537 int room_cmdline; /* lines available from cmdline */
3538 int run;
3539 frame_T *frp;
3540 int h;
3541 int room_reserved;
3542
3543 /* If the height already is the desired value, nothing to do. */
3544 if (curfrp->fr_height == height)
3545 return;
3546
3547 if (curfrp->fr_parent == NULL)
3548 {
3549 /* topframe: can only change the command line */
3550 if (height > Rows - p_ch)
3551 height = Rows - p_ch;
3552 if (height > 0)
3553 frame_new_height(curfrp, height, FALSE, FALSE);
3554 }
3555 else if (curfrp->fr_parent->fr_layout == FR_ROW)
3556 {
3557 /* Row of frames: Also need to resize frames left and right of this
3558 * one. First check for the minimal height of these. */
3559 h = frame_minheight(curfrp->fr_parent, NULL);
3560 if (height < h)
3561 height = h;
3562 frame_setheight(curfrp->fr_parent, height);
3563 }
3564 else
3565 {
3566 /*
3567 * Column of frames: try to change only frames in this column.
3568 */
3569#ifdef FEAT_VERTSPLIT
3570 /*
3571 * Do this twice:
3572 * 1: compute room available, if it's not enough try resizing the
3573 * containing frame.
3574 * 2: compute the room available and adjust the height to it.
3575 * Try not to reduce the height of a window with 'winfixheight' set.
3576 */
3577 for (run = 1; run <= 2; ++run)
3578#else
3579 for (;;)
3580#endif
3581 {
3582 room = 0;
3583 room_reserved = 0;
3584 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3585 frp = frp->fr_next)
3586 {
3587 if (frp != curfrp
3588 && frp->fr_win != NULL
3589 && frp->fr_win->w_p_wfh)
3590 room_reserved += frp->fr_height;
3591 room += frp->fr_height;
3592 if (frp != curfrp)
3593 room -= frame_minheight(frp, NULL);
3594 }
3595#ifdef FEAT_VERTSPLIT
3596 if (curfrp->fr_width != Columns)
3597 room_cmdline = 0;
3598 else
3599#endif
3600 {
3601 room_cmdline = Rows - p_ch - (lastwin->w_winrow
3602 + lastwin->w_height + lastwin->w_status_height);
3603 if (room_cmdline < 0)
3604 room_cmdline = 0;
3605 }
3606
3607 if (height <= room + room_cmdline)
3608 break;
3609#ifdef FEAT_VERTSPLIT
3610 if (run == 2 || curfrp->fr_width == Columns)
3611#endif
3612 {
3613 if (height > room + room_cmdline)
3614 height = room + room_cmdline;
3615 break;
3616 }
3617#ifdef FEAT_VERTSPLIT
3618 frame_setheight(curfrp->fr_parent, height
3619 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
3620#endif
3621 /*NOTREACHED*/
3622 }
3623
3624 /*
3625 * Compute the number of lines we will take from others frames (can be
3626 * negative!).
3627 */
3628 take = height - curfrp->fr_height;
3629
3630 /* If there is not enough room, also reduce the height of a window
3631 * with 'winfixheight' set. */
3632 if (height > room + room_cmdline - room_reserved)
3633 room_reserved = room + room_cmdline - height;
3634 /* If there is only a 'winfixheight' window and making the
3635 * window smaller, need to make the other window taller. */
3636 if (take < 0 && room - curfrp->fr_height < room_reserved)
3637 room_reserved = 0;
3638
3639 if (take > 0 && room_cmdline > 0)
3640 {
3641 /* use lines from cmdline first */
3642 if (take < room_cmdline)
3643 room_cmdline = take;
3644 take -= room_cmdline;
3645 topframe->fr_height += room_cmdline;
3646 }
3647
3648 /*
3649 * set the current frame to the new height
3650 */
3651 frame_new_height(curfrp, height, FALSE, FALSE);
3652
3653 /*
3654 * First take lines from the frames after the current frame. If
3655 * that is not enough, takes lines from frames above the current
3656 * frame.
3657 */
3658 for (run = 0; run < 2; ++run)
3659 {
3660 if (run == 0)
3661 frp = curfrp->fr_next; /* 1st run: start with next window */
3662 else
3663 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3664 while (frp != NULL && take != 0)
3665 {
3666 h = frame_minheight(frp, NULL);
3667 if (room_reserved > 0
3668 && frp->fr_win != NULL
3669 && frp->fr_win->w_p_wfh)
3670 {
3671 if (room_reserved >= frp->fr_height)
3672 room_reserved -= frp->fr_height;
3673 else
3674 {
3675 if (frp->fr_height - room_reserved > take)
3676 room_reserved = frp->fr_height - take;
3677 take -= frp->fr_height - room_reserved;
3678 frame_new_height(frp, room_reserved, FALSE, FALSE);
3679 room_reserved = 0;
3680 }
3681 }
3682 else
3683 {
3684 if (frp->fr_height - take < h)
3685 {
3686 take -= frp->fr_height - h;
3687 frame_new_height(frp, h, FALSE, FALSE);
3688 }
3689 else
3690 {
3691 frame_new_height(frp, frp->fr_height - take,
3692 FALSE, FALSE);
3693 take = 0;
3694 }
3695 }
3696 if (run == 0)
3697 frp = frp->fr_next;
3698 else
3699 frp = frp->fr_prev;
3700 }
3701 }
3702 }
3703}
3704
3705#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3706/*
3707 * Set current window width and take care of repositioning other windows to
3708 * fit around it.
3709 */
3710 void
3711win_setwidth(width)
3712 int width;
3713{
3714 win_setwidth_win(width, curwin);
3715}
3716
3717 void
3718win_setwidth_win(width, wp)
3719 int width;
3720 win_T *wp;
3721{
3722 /* Always keep current window at least one column wide, even when
3723 * 'winminwidth' is zero. */
3724 if (wp == curwin)
3725 {
3726 if (width < p_wmw)
3727 width = p_wmw;
3728 if (width == 0)
3729 width = 1;
3730 }
3731
3732 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
3733
3734 /* recompute the window positions */
3735 (void)win_comp_pos();
3736
3737 redraw_all_later(NOT_VALID);
3738}
3739
3740/*
3741 * Set the width of a frame to "width" and take care that all frames and
3742 * windows inside it are resized. Also resize frames above and below if the
3743 * are in the same FR_ROW frame.
3744 *
3745 * Strategy is similar to frame_setheight().
3746 */
3747 static void
3748frame_setwidth(curfrp, width)
3749 frame_T *curfrp;
3750 int width;
3751{
3752 int room; /* total number of lines available */
3753 int take; /* number of lines taken from other windows */
3754 int run;
3755 frame_T *frp;
3756 int w;
3757
3758 /* If the width already is the desired value, nothing to do. */
3759 if (curfrp->fr_width == width)
3760 return;
3761
3762 if (curfrp->fr_parent == NULL)
3763 /* topframe: can't change width */
3764 return;
3765
3766 if (curfrp->fr_parent->fr_layout == FR_COL)
3767 {
3768 /* Column of frames: Also need to resize frames above and below of
3769 * this one. First check for the minimal width of these. */
3770 w = frame_minwidth(curfrp->fr_parent, NULL);
3771 if (width < w)
3772 width = w;
3773 frame_setwidth(curfrp->fr_parent, width);
3774 }
3775 else
3776 {
3777 /*
3778 * Row of frames: try to change only frames in this row.
3779 *
3780 * Do this twice:
3781 * 1: compute room available, if it's not enough try resizing the
3782 * containing frame.
3783 * 2: compute the room available and adjust the width to it.
3784 */
3785 for (run = 1; run <= 2; ++run)
3786 {
3787 room = 0;
3788 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3789 frp = frp->fr_next)
3790 {
3791 room += frp->fr_width;
3792 if (frp != curfrp)
3793 room -= frame_minwidth(frp, NULL);
3794 }
3795
3796 if (width <= room)
3797 break;
3798 if (run == 2 || curfrp->fr_height >= Rows - p_ch)
3799 {
3800 if (width > room)
3801 width = room;
3802 break;
3803 }
3804 frame_setwidth(curfrp->fr_parent, width
3805 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
3806 }
3807
3808
3809 /*
3810 * Compute the number of lines we will take from others frames (can be
3811 * negative!).
3812 */
3813 take = width - curfrp->fr_width;
3814
3815 /*
3816 * set the current frame to the new width
3817 */
3818 frame_new_width(curfrp, width, FALSE);
3819
3820 /*
3821 * First take lines from the frames right of the current frame. If
3822 * that is not enough, takes lines from frames left of the current
3823 * frame.
3824 */
3825 for (run = 0; run < 2; ++run)
3826 {
3827 if (run == 0)
3828 frp = curfrp->fr_next; /* 1st run: start with next window */
3829 else
3830 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3831 while (frp != NULL && take != 0)
3832 {
3833 w = frame_minwidth(frp, NULL);
3834 if (frp->fr_width - take < w)
3835 {
3836 take -= frp->fr_width - w;
3837 frame_new_width(frp, w, FALSE);
3838 }
3839 else
3840 {
3841 frame_new_width(frp, frp->fr_width - take, FALSE);
3842 take = 0;
3843 }
3844 if (run == 0)
3845 frp = frp->fr_next;
3846 else
3847 frp = frp->fr_prev;
3848 }
3849 }
3850 }
3851}
3852#endif /* FEAT_VERTSPLIT */
3853
3854/*
3855 * Check 'winminheight' for a valid value.
3856 */
3857 void
3858win_setminheight()
3859{
3860 int room;
3861 int first = TRUE;
3862 win_T *wp;
3863
3864 /* loop until there is a 'winminheight' that is possible */
3865 while (p_wmh > 0)
3866 {
3867 /* TODO: handle vertical splits */
3868 room = -p_wh;
3869 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3870 room += wp->w_height - p_wmh;
3871 if (room >= 0)
3872 break;
3873 --p_wmh;
3874 if (first)
3875 {
3876 EMSG(_(e_noroom));
3877 first = FALSE;
3878 }
3879 }
3880}
3881
3882#ifdef FEAT_MOUSE
3883
3884/*
3885 * Status line of dragwin is dragged "offset" lines down (negative is up).
3886 */
3887 void
3888win_drag_status_line(dragwin, offset)
3889 win_T *dragwin;
3890 int offset;
3891{
3892 frame_T *curfr;
3893 frame_T *fr;
3894 int room;
3895 int row;
3896 int up; /* if TRUE, drag status line up, otherwise down */
3897 int n;
3898
3899 fr = dragwin->w_frame;
3900 curfr = fr;
3901 if (fr != topframe) /* more than one window */
3902 {
3903 fr = fr->fr_parent;
3904 /* When the parent frame is not a column of frames, its parent should
3905 * be. */
3906 if (fr->fr_layout != FR_COL)
3907 {
3908 curfr = fr;
3909 if (fr != topframe) /* only a row of windows, may drag statusline */
3910 fr = fr->fr_parent;
3911 }
3912 }
3913
3914 /* If this is the last frame in a column, may want to resize the parent
3915 * frame instead (go two up to skip a row of frames). */
3916 while (curfr != topframe && curfr->fr_next == NULL)
3917 {
3918 if (fr != topframe)
3919 fr = fr->fr_parent;
3920 curfr = fr;
3921 if (fr != topframe)
3922 fr = fr->fr_parent;
3923 }
3924
3925 if (offset < 0) /* drag up */
3926 {
3927 up = TRUE;
3928 offset = -offset;
3929 /* sum up the room of the current frame and above it */
3930 if (fr == curfr)
3931 {
3932 /* only one window */
3933 room = fr->fr_height - frame_minheight(fr, NULL);
3934 }
3935 else
3936 {
3937 room = 0;
3938 for (fr = fr->fr_child; ; fr = fr->fr_next)
3939 {
3940 room += fr->fr_height - frame_minheight(fr, NULL);
3941 if (fr == curfr)
3942 break;
3943 }
3944 }
3945 fr = curfr->fr_next; /* put fr at frame that grows */
3946 }
3947 else /* drag down */
3948 {
3949 up = FALSE;
3950 /*
3951 * Only dragging the last status line can reduce p_ch.
3952 */
3953 room = Rows - cmdline_row;
3954 if (curfr->fr_next == NULL)
3955 room -= 1;
3956 else
3957 room -= p_ch;
3958 if (room < 0)
3959 room = 0;
3960 /* sum up the room of frames below of the current one */
3961 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
3962 room += fr->fr_height - frame_minheight(fr, NULL);
3963 fr = curfr; /* put fr at window that grows */
3964 }
3965
3966 if (room < offset) /* Not enough room */
3967 offset = room; /* Move as far as we can */
3968 if (offset <= 0)
3969 return;
3970
3971 /*
3972 * Grow frame fr by "offset" lines.
3973 * Doesn't happen when dragging the last status line up.
3974 */
3975 if (fr != NULL)
3976 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
3977
3978 if (up)
3979 fr = curfr; /* current frame gets smaller */
3980 else
3981 fr = curfr->fr_next; /* next frame gets smaller */
3982
3983 /*
3984 * Now make the other frames smaller.
3985 */
3986 while (fr != NULL && offset > 0)
3987 {
3988 n = frame_minheight(fr, NULL);
3989 if (fr->fr_height - offset <= n)
3990 {
3991 offset -= fr->fr_height - n;
3992 frame_new_height(fr, n, !up, FALSE);
3993 }
3994 else
3995 {
3996 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
3997 break;
3998 }
3999 if (up)
4000 fr = fr->fr_prev;
4001 else
4002 fr = fr->fr_next;
4003 }
4004 row = win_comp_pos();
4005 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4006 cmdline_row = row;
4007 p_ch = Rows - cmdline_row;
4008 if (p_ch < 1)
4009 p_ch = 1;
4010 redraw_all_later(NOT_VALID);
4011 showmode();
4012}
4013
4014#ifdef FEAT_VERTSPLIT
4015/*
4016 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4017 */
4018 void
4019win_drag_vsep_line(dragwin, offset)
4020 win_T *dragwin;
4021 int offset;
4022{
4023 frame_T *curfr;
4024 frame_T *fr;
4025 int room;
4026 int left; /* if TRUE, drag separator line left, otherwise right */
4027 int n;
4028
4029 fr = dragwin->w_frame;
4030 if (fr == topframe) /* only one window (cannot happe?) */
4031 return;
4032 curfr = fr;
4033 fr = fr->fr_parent;
4034 /* When the parent frame is not a row of frames, its parent should be. */
4035 if (fr->fr_layout != FR_ROW)
4036 {
4037 if (fr == topframe) /* only a column of windows (cannot happen?) */
4038 return;
4039 curfr = fr;
4040 fr = fr->fr_parent;
4041 }
4042
4043 /* If this is the last frame in a row, may want to resize a parent
4044 * frame instead. */
4045 while (curfr->fr_next == NULL)
4046 {
4047 if (fr == topframe)
4048 break;
4049 curfr = fr;
4050 fr = fr->fr_parent;
4051 if (fr != topframe)
4052 {
4053 curfr = fr;
4054 fr = fr->fr_parent;
4055 }
4056 }
4057
4058 if (offset < 0) /* drag left */
4059 {
4060 left = TRUE;
4061 offset = -offset;
4062 /* sum up the room of the current frame and left of it */
4063 room = 0;
4064 for (fr = fr->fr_child; ; fr = fr->fr_next)
4065 {
4066 room += fr->fr_width - frame_minwidth(fr, NULL);
4067 if (fr == curfr)
4068 break;
4069 }
4070 fr = curfr->fr_next; /* put fr at frame that grows */
4071 }
4072 else /* drag right */
4073 {
4074 left = FALSE;
4075 /* sum up the room of frames right of the current one */
4076 room = 0;
4077 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4078 room += fr->fr_width - frame_minwidth(fr, NULL);
4079 fr = curfr; /* put fr at window that grows */
4080 }
4081
4082 if (room < offset) /* Not enough room */
4083 offset = room; /* Move as far as we can */
4084 if (offset <= 0) /* No room at all, quit. */
4085 return;
4086
4087 /* grow frame fr by offset lines */
4088 frame_new_width(fr, fr->fr_width + offset, left);
4089
4090 /* shrink other frames: current and at the left or at the right */
4091 if (left)
4092 fr = curfr; /* current frame gets smaller */
4093 else
4094 fr = curfr->fr_next; /* next frame gets smaller */
4095
4096 while (fr != NULL && offset > 0)
4097 {
4098 n = frame_minwidth(fr, NULL);
4099 if (fr->fr_width - offset <= n)
4100 {
4101 offset -= fr->fr_width - n;
4102 frame_new_width(fr, n, !left);
4103 }
4104 else
4105 {
4106 frame_new_width(fr, fr->fr_width - offset, !left);
4107 break;
4108 }
4109 if (left)
4110 fr = fr->fr_prev;
4111 else
4112 fr = fr->fr_next;
4113 }
4114 (void)win_comp_pos();
4115 redraw_all_later(NOT_VALID);
4116}
4117#endif /* FEAT_VERTSPLIT */
4118#endif /* FEAT_MOUSE */
4119
4120#endif /* FEAT_WINDOWS */
4121
4122/*
4123 * Set the height of a window.
4124 * This takes care of the things inside the window, not what happens to the
4125 * window position, the frame or to other windows.
4126 */
4127 static void
4128win_new_height(wp, height)
4129 win_T *wp;
4130 int height;
4131{
4132 linenr_T lnum;
4133 int sline, line_size;
4134#define FRACTION_MULT 16384L
4135
4136 /* Don't want a negative height. Happens when splitting a tiny window.
4137 * Will equalize heights soon to fix it. */
4138 if (height < 0)
4139 height = 0;
4140
4141 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4142 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4143 + FRACTION_MULT / 2) / (long)wp->w_height;
4144
4145 wp->w_height = height;
4146 wp->w_skipcol = 0;
4147
4148 /* Don't change w_topline when height is zero. Don't set w_topline when
4149 * 'scrollbind' is set and this isn't the current window. */
4150 if (height > 0
4151#ifdef FEAT_SCROLLBIND
4152 && (!wp->w_p_scb || wp == curwin)
4153#endif
4154 )
4155 {
4156 lnum = wp->w_cursor.lnum;
4157 if (lnum < 1) /* can happen when starting up */
4158 lnum = 1;
4159 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4160 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4161 sline = wp->w_wrow - line_size;
4162 if (sline < 0)
4163 {
4164 /*
4165 * Cursor line would go off top of screen if w_wrow was this high.
4166 */
4167 wp->w_wrow = line_size;
4168 }
4169 else
4170 {
4171 while (sline > 0 && lnum > 1)
4172 {
4173#ifdef FEAT_FOLDING
4174 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4175 if (lnum == 1)
4176 {
4177 /* first line in buffer is folded */
4178 line_size = 1;
4179 --sline;
4180 break;
4181 }
4182#endif
4183 --lnum;
4184#ifdef FEAT_DIFF
4185 if (lnum == wp->w_topline)
4186 line_size = plines_win_nofill(wp, lnum, TRUE)
4187 + wp->w_topfill;
4188 else
4189#endif
4190 line_size = plines_win(wp, lnum, TRUE);
4191 sline -= line_size;
4192 }
4193 if (sline < 0)
4194 {
4195 /*
4196 * Line we want at top would go off top of screen. Use next
4197 * line instead.
4198 */
4199#ifdef FEAT_FOLDING
4200 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4201#endif
4202 lnum++;
4203 wp->w_wrow -= line_size + sline;
4204 }
4205 else if (sline > 0)
4206 {
4207 /* First line of file reached, use that as topline. */
4208 lnum = 1;
4209 wp->w_wrow -= sline;
4210 }
4211 }
4212 set_topline(wp, lnum);
4213 }
4214
4215 if (wp == curwin)
4216 {
4217 if (p_so)
4218 update_topline();
4219 curs_columns(FALSE); /* validate w_wrow */
4220 }
4221 wp->w_prev_fraction_row = wp->w_wrow;
4222
4223 win_comp_scroll(wp);
4224 redraw_win_later(wp, NOT_VALID);
4225#ifdef FEAT_WINDOWS
4226 wp->w_redr_status = TRUE;
4227#endif
4228 invalidate_botline_win(wp);
4229}
4230
4231#ifdef FEAT_VERTSPLIT
4232/*
4233 * Set the width of a window.
4234 */
4235 static void
4236win_new_width(wp, width)
4237 win_T *wp;
4238 int width;
4239{
4240 wp->w_width = width;
4241 wp->w_lines_valid = 0;
4242 changed_line_abv_curs_win(wp);
4243 invalidate_botline_win(wp);
4244 if (wp == curwin)
4245 {
4246 update_topline();
4247 curs_columns(TRUE); /* validate w_wrow */
4248 }
4249 redraw_win_later(wp, NOT_VALID);
4250 wp->w_redr_status = TRUE;
4251}
4252#endif
4253
4254 void
4255win_comp_scroll(wp)
4256 win_T *wp;
4257{
4258 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4259 if (wp->w_p_scr == 0)
4260 wp->w_p_scr = 1;
4261}
4262
4263/*
4264 * command_height: called whenever p_ch has been changed
4265 */
4266 void
4267command_height(old_p_ch)
4268 long old_p_ch;
4269{
4270#ifdef FEAT_WINDOWS
4271 int h;
4272 frame_T *frp;
4273
4274 /* Find bottom frame with width of screen. */
4275 frp = lastwin->w_frame;
4276# ifdef FEAT_VERTSPLIT
4277 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4278 frp = frp->fr_parent;
4279# endif
4280
4281 /* Avoid changing the height of a window with 'winfixheight' set. */
4282 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4283 && frp->fr_win->w_p_wfh)
4284 frp = frp->fr_prev;
4285
4286 if (starting != NO_SCREEN)
4287 {
4288 cmdline_row = Rows - p_ch;
4289
4290 if (p_ch > old_p_ch) /* p_ch got bigger */
4291 {
4292 while (p_ch > old_p_ch)
4293 {
4294 if (frp == NULL)
4295 {
4296 EMSG(_(e_noroom));
4297 p_ch = old_p_ch;
4298 cmdline_row = Rows - p_ch;
4299 break;
4300 }
4301 h = frp->fr_height - frame_minheight(frp, NULL);
4302 if (h > p_ch - old_p_ch)
4303 h = p_ch - old_p_ch;
4304 old_p_ch += h;
4305 frame_add_height(frp, -h);
4306 frp = frp->fr_prev;
4307 }
4308
4309 /* Recompute window positions. */
4310 (void)win_comp_pos();
4311
4312 /* clear the lines added to cmdline */
4313 if (full_screen)
4314 screen_fill((int)(cmdline_row), (int)Rows, 0,
4315 (int)Columns, ' ', ' ', 0);
4316 msg_row = cmdline_row;
4317 redraw_cmdline = TRUE;
4318 return;
4319 }
4320
4321 if (msg_row < cmdline_row)
4322 msg_row = cmdline_row;
4323 redraw_cmdline = TRUE;
4324 }
4325 frame_add_height(frp, (int)(old_p_ch - p_ch));
4326
4327 /* Recompute window positions. */
4328 if (frp != lastwin->w_frame)
4329 (void)win_comp_pos();
4330#else
4331 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4332 cmdline_row = Rows - p_ch;
4333#endif
4334}
4335
4336#if defined(FEAT_WINDOWS) || defined(PROTO)
4337/*
4338 * Resize frame "frp" to be "n" lines higher (negative for less high).
4339 * Also resize the frames it is contained in.
4340 */
4341 static void
4342frame_add_height(frp, n)
4343 frame_T *frp;
4344 int n;
4345{
4346 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4347 for (;;)
4348 {
4349 frp = frp->fr_parent;
4350 if (frp == NULL)
4351 break;
4352 frp->fr_height += n;
4353 }
4354}
4355
4356/*
4357 * Add or remove a status line for the bottom window(s), according to the
4358 * value of 'laststatus'.
4359 */
4360 void
4361last_status(morewin)
4362 int morewin; /* pretend there are two or more windows */
4363{
4364 /* Don't make a difference between horizontal or vertical split. */
4365 last_status_rec(topframe, (p_ls == 2
4366 || (p_ls == 1 && (morewin || lastwin != firstwin))));
4367}
4368
4369 static void
4370last_status_rec(fr, statusline)
4371 frame_T *fr;
4372 int statusline;
4373{
4374 frame_T *fp;
4375 win_T *wp;
4376
4377 if (fr->fr_layout == FR_LEAF)
4378 {
4379 wp = fr->fr_win;
4380 if (wp->w_status_height != 0 && !statusline)
4381 {
4382 /* remove status line */
4383 win_new_height(wp, wp->w_height + 1);
4384 wp->w_status_height = 0;
4385 comp_col();
4386 }
4387 else if (wp->w_status_height == 0 && statusline)
4388 {
4389 /* Find a frame to take a line from. */
4390 fp = fr;
4391 while (fp->fr_height <= frame_minheight(fp, NULL))
4392 {
4393 if (fp == topframe)
4394 {
4395 EMSG(_(e_noroom));
4396 return;
4397 }
4398 /* In a column of frames: go to frame above. If already at
4399 * the top or in a row of frames: go to parent. */
4400 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
4401 fp = fp->fr_prev;
4402 else
4403 fp = fp->fr_parent;
4404 }
4405 wp->w_status_height = 1;
4406 if (fp != fr)
4407 {
4408 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
4409 frame_fix_height(wp);
4410 (void)win_comp_pos();
4411 }
4412 else
4413 win_new_height(wp, wp->w_height - 1);
4414 comp_col();
4415 redraw_all_later(NOT_VALID);
4416 }
4417 }
4418#ifdef FEAT_VERTSPLIT
4419 else if (fr->fr_layout == FR_ROW)
4420 {
4421 /* vertically split windows, set status line for each one */
4422 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
4423 last_status_rec(fp, statusline);
4424 }
4425#endif
4426 else
4427 {
4428 /* horizontally split window, set status line for last one */
4429 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
4430 ;
4431 last_status_rec(fp, statusline);
4432 }
4433}
4434
4435#endif /* FEAT_WINDOWS */
4436
4437#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4438/*
4439 * Return the file name under or after the cursor.
4440 *
4441 * The 'path' option is searched if the file name is not absolute.
4442 * The string returned has been alloc'ed and should be freed by the caller.
4443 * NULL is returned if the file name or file is not found.
4444 *
4445 * options:
4446 * FNAME_MESS give error messages
4447 * FNAME_EXP expand to path
4448 * FNAME_HYP check for hypertext link
4449 * FNAME_INCL apply "includeexpr"
4450 */
4451 char_u *
4452file_name_at_cursor(options, count)
4453 int options;
4454 long count;
4455{
4456 return file_name_in_line(ml_get_curline(),
4457 curwin->w_cursor.col, options, count, curbuf->b_ffname);
4458}
4459
4460/*
4461 * Return the name of the file under or after ptr[col].
4462 * Otherwise like file_name_at_cursor().
4463 */
4464 char_u *
4465file_name_in_line(line, col, options, count, rel_fname)
4466 char_u *line;
4467 int col;
4468 int options;
4469 long count;
4470 char_u *rel_fname; /* file we are searching relative to */
4471{
4472 char_u *ptr;
4473 int len;
4474
4475 /*
4476 * search forward for what could be the start of a file name
4477 */
4478 ptr = line + col;
4479 while (*ptr != NUL && !vim_isfilec(*ptr))
4480 ++ptr;
4481 if (*ptr == NUL) /* nothing found */
4482 {
4483 if (options & FNAME_MESS)
4484 EMSG(_("E446: No file name under cursor"));
4485 return NULL;
4486 }
4487
4488 /*
4489 * Search backward for first char of the file name.
4490 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
4491 */
4492 while (ptr > line)
4493 {
4494#ifdef FEAT_MBYTE
4495 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
4496 ptr -= len + 1;
4497 else
4498#endif
4499 if (vim_isfilec(ptr[-1])
4500 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
4501 --ptr;
4502 else
4503 break;
4504 }
4505
4506 /*
4507 * Search forward for the last char of the file name.
4508 * Also allow "://" when ':' is not in 'isfname'.
4509 */
4510 len = 0;
4511 while (vim_isfilec(ptr[len])
4512 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
4513#ifdef FEAT_MBYTE
4514 if (has_mbyte)
4515 len += (*mb_ptr2len_check)(ptr + len);
4516 else
4517#endif
4518 ++len;
4519
4520 /*
4521 * If there is trailing punctuation, remove it.
4522 * But don't remove "..", could be a directory name.
4523 */
4524 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
4525 && ptr[len - 2] != '.')
4526 --len;
4527
4528 return find_file_name_in_path(ptr, len, options, count, rel_fname);
4529}
4530
4531# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4532static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
4533
4534 static char_u *
4535eval_includeexpr(ptr, len)
4536 char_u *ptr;
4537 int len;
4538{
4539 char_u *res;
4540
4541 set_vim_var_string(VV_FNAME, ptr, len);
4542 res = eval_to_string_safe(curbuf->b_p_inex, NULL);
4543 set_vim_var_string(VV_FNAME, NULL, 0);
4544 return res;
4545}
4546#endif
4547
4548/*
4549 * Return the name of the file ptr[len] in 'path'.
4550 * Otherwise like file_name_at_cursor().
4551 */
4552 char_u *
4553find_file_name_in_path(ptr, len, options, count, rel_fname)
4554 char_u *ptr;
4555 int len;
4556 int options;
4557 long count;
4558 char_u *rel_fname; /* file we are searching relative to */
4559{
4560 char_u *file_name;
4561 int c;
4562# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4563 char_u *tofree = NULL;
4564
4565 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4566 {
4567 tofree = eval_includeexpr(ptr, len);
4568 if (tofree != NULL)
4569 {
4570 ptr = tofree;
4571 len = (int)STRLEN(ptr);
4572 }
4573 }
4574# endif
4575
4576 if (options & FNAME_EXP)
4577 {
4578 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4579 TRUE, rel_fname);
4580
4581# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4582 /*
4583 * If the file could not be found in a normal way, try applying
4584 * 'includeexpr' (unless done already).
4585 */
4586 if (file_name == NULL
4587 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4588 {
4589 tofree = eval_includeexpr(ptr, len);
4590 if (tofree != NULL)
4591 {
4592 ptr = tofree;
4593 len = (int)STRLEN(ptr);
4594 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4595 TRUE, rel_fname);
4596 }
4597 }
4598# endif
4599 if (file_name == NULL && (options & FNAME_MESS))
4600 {
4601 c = ptr[len];
4602 ptr[len] = NUL;
4603 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
4604 ptr[len] = c;
4605 }
4606
4607 /* Repeat finding the file "count" times. This matters when it
4608 * appears several times in the path. */
4609 while (file_name != NULL && --count > 0)
4610 {
4611 vim_free(file_name);
4612 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
4613 }
4614 }
4615 else
4616 file_name = vim_strnsave(ptr, len);
4617
4618# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4619 vim_free(tofree);
4620# endif
4621
4622 return file_name;
4623}
4624#endif /* FEAT_SEARCHPATH */
4625
4626/*
4627 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4628 * Also check for ":\\", which MS Internet Explorer accepts, return
4629 * URL_BACKSLASH.
4630 */
4631 static int
4632path_is_url(p)
4633 char_u *p;
4634{
4635 if (STRNCMP(p, "://", (size_t)3) == 0)
4636 return URL_SLASH;
4637 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4638 return URL_BACKSLASH;
4639 return 0;
4640}
4641
4642/*
4643 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4644 * Return URL_BACKSLASH for "name:\\".
4645 * Return zero otherwise.
4646 */
4647 int
4648path_with_url(fname)
4649 char_u *fname;
4650{
4651 char_u *p;
4652
4653 for (p = fname; isalpha(*p); ++p)
4654 ;
4655 return path_is_url(p);
4656}
4657
4658/*
4659 * Return TRUE if "name" is a full (absolute) path name or URL.
4660 */
4661 int
4662vim_isAbsName(name)
4663 char_u *name;
4664{
4665 return (path_with_url(name) != 0 || mch_isFullName(name));
4666}
4667
4668/*
4669 * Get absolute file name into buffer 'buf' of length 'len' bytes.
4670 *
4671 * return FAIL for failure, OK otherwise
4672 */
4673 int
4674vim_FullName(fname, buf, len, force)
4675 char_u *fname, *buf;
4676 int len;
4677 int force;
4678{
4679 int retval = OK;
4680 int url;
4681
4682 *buf = NUL;
4683 if (fname == NULL)
4684 return FAIL;
4685
4686 url = path_with_url(fname);
4687 if (!url)
4688 retval = mch_FullName(fname, buf, len, force);
4689 if (url || retval == FAIL)
4690 {
4691 /* something failed; use the file name (truncate when too long) */
4692 STRNCPY(buf, fname, len);
4693 buf[len - 1] = NUL;
4694 }
4695#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
4696 slash_adjust(buf);
4697#endif
4698 return retval;
4699}
4700
4701/*
4702 * Return the minimal number of rows that is needed on the screen to display
4703 * the current number of windows.
4704 */
4705 int
4706min_rows()
4707{
4708 int total;
4709
4710 if (firstwin == NULL) /* not initialized yet */
4711 return MIN_LINES;
4712
4713 total = 1; /* count the room for the command line */
4714#ifdef FEAT_WINDOWS
4715 total += frame_minheight(topframe, NULL);
4716#else
4717 total += 1; /* at least one window should have a line! */
4718#endif
4719 return total;
4720}
4721
4722/*
4723 * Return TRUE if there is only one window, not counting a help or preview
4724 * window, unless it is the current window.
4725 */
4726 int
4727only_one_window()
4728{
4729#ifdef FEAT_WINDOWS
4730 int count = 0;
4731 win_T *wp;
4732
4733 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4734 if (!(wp->w_buffer->b_help
4735# ifdef FEAT_QUICKFIX
4736 || wp->w_p_pvw
4737# endif
4738 ) || wp == curwin)
4739 ++count;
4740 return (count <= 1);
4741#else
4742 return TRUE;
4743#endif
4744}
4745
4746#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
4747/*
4748 * Correct the cursor line number in other windows. Used after changing the
4749 * current buffer, and before applying autocommands.
4750 * When "do_curwin" is TRUE, also check current window.
4751 */
4752 void
4753check_lnums(do_curwin)
4754 int do_curwin;
4755{
4756 win_T *wp;
4757
4758#ifdef FEAT_WINDOWS
4759 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4760 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
4761#else
4762 wp = curwin;
4763 if (do_curwin)
4764#endif
4765 {
4766 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4767 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4768 if (wp->w_topline > curbuf->b_ml.ml_line_count)
4769 wp->w_topline = curbuf->b_ml.ml_line_count;
4770 }
4771}
4772#endif
4773
4774#if defined(FEAT_WINDOWS) || defined(PROTO)
4775
4776/*
4777 * A snapshot of the window sizes, to restore them after closing the help
4778 * window.
4779 * Only these fields are used:
4780 * fr_layout
4781 * fr_width
4782 * fr_height
4783 * fr_next
4784 * fr_child
4785 * fr_win (only valid for the old curwin, NULL otherwise)
4786 */
4787static frame_T *snapshot = NULL;
4788
4789/*
4790 * Create a snapshot of the current frame sizes.
4791 */
4792 static void
4793make_snapshot()
4794{
4795 clear_snapshot();
4796 make_snapshot_rec(topframe, &snapshot);
4797}
4798
4799 static void
4800make_snapshot_rec(fr, frp)
4801 frame_T *fr;
4802 frame_T **frp;
4803{
4804 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
4805 if (*frp == NULL)
4806 return;
4807 (*frp)->fr_layout = fr->fr_layout;
4808# ifdef FEAT_VERTSPLIT
4809 (*frp)->fr_width = fr->fr_width;
4810# endif
4811 (*frp)->fr_height = fr->fr_height;
4812 if (fr->fr_next != NULL)
4813 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
4814 if (fr->fr_child != NULL)
4815 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
4816 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
4817 (*frp)->fr_win = curwin;
4818}
4819
4820/*
4821 * Remove any existing snapshot.
4822 */
4823 static void
4824clear_snapshot()
4825{
4826 clear_snapshot_rec(snapshot);
4827 snapshot = NULL;
4828}
4829
4830 static void
4831clear_snapshot_rec(fr)
4832 frame_T *fr;
4833{
4834 if (fr != NULL)
4835 {
4836 clear_snapshot_rec(fr->fr_next);
4837 clear_snapshot_rec(fr->fr_child);
4838 vim_free(fr);
4839 }
4840}
4841
4842/*
4843 * Restore a previously created snapshot, if there is any.
4844 * This is only done if the screen size didn't change and the window layout is
4845 * still the same.
4846 */
4847 static void
4848restore_snapshot(close_curwin)
4849 int close_curwin; /* closing current window */
4850{
4851 win_T *wp;
4852
4853 if (snapshot != NULL
4854# ifdef FEAT_VERTSPLIT
4855 && snapshot->fr_width == topframe->fr_width
4856# endif
4857 && snapshot->fr_height == topframe->fr_height
4858 && check_snapshot_rec(snapshot, topframe) == OK)
4859 {
4860 wp = restore_snapshot_rec(snapshot, topframe);
4861 win_comp_pos();
4862 if (wp != NULL && close_curwin)
4863 win_goto(wp);
4864 redraw_all_later(CLEAR);
4865 }
4866 clear_snapshot();
4867}
4868
4869/*
4870 * Check if frames "sn" and "fr" have the same layout, same following frames
4871 * and same children.
4872 */
4873 static int
4874check_snapshot_rec(sn, fr)
4875 frame_T *sn;
4876 frame_T *fr;
4877{
4878 if (sn->fr_layout != fr->fr_layout
4879 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
4880 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
4881 || (sn->fr_next != NULL
4882 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
4883 || (sn->fr_child != NULL
4884 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
4885 return FAIL;
4886 return OK;
4887}
4888
4889/*
4890 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
4891 * following frames and children.
4892 * Returns a pointer to the old current window, or NULL.
4893 */
4894 static win_T *
4895restore_snapshot_rec(sn, fr)
4896 frame_T *sn;
4897 frame_T *fr;
4898{
4899 win_T *wp = NULL;
4900 win_T *wp2;
4901
4902 fr->fr_height = sn->fr_height;
4903# ifdef FEAT_VERTSPLIT
4904 fr->fr_width = sn->fr_width;
4905# endif
4906 if (fr->fr_layout == FR_LEAF)
4907 {
4908 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
4909# ifdef FEAT_VERTSPLIT
4910 frame_new_width(fr, fr->fr_width, FALSE);
4911# endif
4912 wp = sn->fr_win;
4913 }
4914 if (sn->fr_next != NULL)
4915 {
4916 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
4917 if (wp2 != NULL)
4918 wp = wp2;
4919 }
4920 if (sn->fr_child != NULL)
4921 {
4922 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
4923 if (wp2 != NULL)
4924 wp = wp2;
4925 }
4926 return wp;
4927}
4928
4929#endif
4930
4931#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
4932/*
4933 * Return TRUE if there is any vertically split window.
4934 */
4935 int
4936win_hasvertsplit()
4937{
4938 frame_T *fr;
4939
4940 if (topframe->fr_layout == FR_ROW)
4941 return TRUE;
4942
4943 if (topframe->fr_layout == FR_COL)
4944 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
4945 if (fr->fr_layout == FR_ROW)
4946 return TRUE;
4947
4948 return FALSE;
4949}
4950#endif