blob: dc63e09d412de458dfe0353c4356bd850328d9fb [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
3109#ifdef FEAT_PERL
3110 perl_win_free(wp);
3111#endif
3112
3113#ifdef FEAT_PYTHON
3114 python_window_free(wp);
3115#endif
3116
3117#ifdef FEAT_TCL
3118 tcl_window_free(wp);
3119#endif
3120
3121#ifdef FEAT_RUBY
3122 ruby_window_free(wp);
3123#endif
3124
3125 clear_winopt(&wp->w_onebuf_opt);
3126 clear_winopt(&wp->w_allbuf_opt);
3127
3128#ifdef FEAT_EVAL
3129 var_clear(&wp->w_vars); /* free all internal variables */
3130#endif
3131
3132 if (prevwin == wp)
3133 prevwin = NULL;
3134 win_free_lsize(wp);
3135
3136 for (i = 0; i < wp->w_tagstacklen; ++i)
3137 vim_free(wp->w_tagstack[i].tagname);
3138
3139 vim_free(wp->w_localdir);
3140#ifdef FEAT_SEARCH_EXTRA
3141 vim_free(wp->w_match.regprog);
3142#endif
3143#ifdef FEAT_JUMPLIST
3144 free_jumplist(wp);
3145#endif
3146
3147#ifdef FEAT_GUI
3148 if (gui.in_use)
3149 {
3150 out_flush();
3151 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3152 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3153 }
3154#endif /* FEAT_GUI */
3155
3156 win_remove(wp);
3157 vim_free(wp);
3158}
3159
3160/*
3161 * Append window "wp" in the window list after window "after".
3162 */
3163 static void
3164win_append(after, wp)
3165 win_T *after, *wp;
3166{
3167 win_T *before;
3168
3169 if (after == NULL) /* after NULL is in front of the first */
3170 before = firstwin;
3171 else
3172 before = after->w_next;
3173
3174 wp->w_next = before;
3175 wp->w_prev = after;
3176 if (after == NULL)
3177 firstwin = wp;
3178 else
3179 after->w_next = wp;
3180 if (before == NULL)
3181 lastwin = wp;
3182 else
3183 before->w_prev = wp;
3184}
3185
3186/*
3187 * Remove a window from the window list.
3188 */
3189 static void
3190win_remove(wp)
3191 win_T *wp;
3192{
3193 if (wp->w_prev != NULL)
3194 wp->w_prev->w_next = wp->w_next;
3195 else
3196 firstwin = wp->w_next;
3197 if (wp->w_next != NULL)
3198 wp->w_next->w_prev = wp->w_prev;
3199 else
3200 lastwin = wp->w_prev;
3201}
3202
3203/*
3204 * Append frame "frp" in a frame list after frame "after".
3205 */
3206 static void
3207frame_append(after, frp)
3208 frame_T *after, *frp;
3209{
3210 frp->fr_next = after->fr_next;
3211 after->fr_next = frp;
3212 if (frp->fr_next != NULL)
3213 frp->fr_next->fr_prev = frp;
3214 frp->fr_prev = after;
3215}
3216
3217/*
3218 * Insert frame "frp" in a frame list before frame "before".
3219 */
3220 static void
3221frame_insert(before, frp)
3222 frame_T *before, *frp;
3223{
3224 frp->fr_next = before;
3225 frp->fr_prev = before->fr_prev;
3226 before->fr_prev = frp;
3227 if (frp->fr_prev != NULL)
3228 frp->fr_prev->fr_next = frp;
3229 else
3230 frp->fr_parent->fr_child = frp;
3231}
3232
3233/*
3234 * Remove a frame from a frame list.
3235 */
3236 static void
3237frame_remove(frp)
3238 frame_T *frp;
3239{
3240 if (frp->fr_prev != NULL)
3241 frp->fr_prev->fr_next = frp->fr_next;
3242 else
3243 frp->fr_parent->fr_child = frp->fr_next;
3244 if (frp->fr_next != NULL)
3245 frp->fr_next->fr_prev = frp->fr_prev;
3246}
3247
3248#endif /* FEAT_WINDOWS */
3249
3250/*
3251 * Allocate w_lines[] for window "wp".
3252 * Return FAIL for failure, OK for success.
3253 */
3254 int
3255win_alloc_lines(wp)
3256 win_T *wp;
3257{
3258 wp->w_lines_valid = 0;
3259 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3260 if (wp->w_lines == NULL)
3261 return FAIL;
3262 return OK;
3263}
3264
3265/*
3266 * free lsize arrays for a window
3267 */
3268 void
3269win_free_lsize(wp)
3270 win_T *wp;
3271{
3272 vim_free(wp->w_lines);
3273 wp->w_lines = NULL;
3274}
3275
3276/*
3277 * Called from win_new_shellsize() after Rows changed.
3278 */
3279 void
3280shell_new_rows()
3281{
3282 int h = (int)(Rows - p_ch);
3283
3284 if (firstwin == NULL) /* not initialized yet */
3285 return;
3286#ifdef FEAT_WINDOWS
3287 if (h < frame_minheight(topframe, NULL))
3288 h = frame_minheight(topframe, NULL);
3289 /* First try setting the heights of windows without 'winfixheight'. If
3290 * that doesn't result in the right height, forget about that option. */
3291 frame_new_height(topframe, h, FALSE, TRUE);
3292 if (topframe->fr_height != h)
3293 frame_new_height(topframe, h, FALSE, FALSE);
3294
3295 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3296#else
3297 if (h < 1)
3298 h = 1;
3299 win_new_height(firstwin, h);
3300#endif
3301 compute_cmdrow();
3302#if 0
3303 /* Disabled: don't want making the screen smaller make a window larger. */
3304 if (p_ea)
3305 win_equal(curwin, FALSE, 'v');
3306#endif
3307}
3308
3309#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3310/*
3311 * Called from win_new_shellsize() after Columns changed.
3312 */
3313 void
3314shell_new_columns()
3315{
3316 if (firstwin == NULL) /* not initialized yet */
3317 return;
3318 frame_new_width(topframe, (int)Columns, FALSE);
3319 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3320#if 0
3321 /* Disabled: don't want making the screen smaller make a window larger. */
3322 if (p_ea)
3323 win_equal(curwin, FALSE, 'h');
3324#endif
3325}
3326#endif
3327
3328#if defined(FEAT_CMDWIN) || defined(PROTO)
3329/*
3330 * Save the size of all windows in "gap".
3331 */
3332 void
3333win_size_save(gap)
3334 garray_T *gap;
3335
3336{
3337 win_T *wp;
3338
3339 ga_init2(gap, (int)sizeof(int), 1);
3340 if (ga_grow(gap, win_count() * 2) == OK)
3341 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3342 {
3343 ((int *)gap->ga_data)[gap->ga_len++] =
3344 wp->w_width + wp->w_vsep_width;
3345 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3346 }
3347}
3348
3349/*
3350 * Restore window sizes, but only if the number of windows is still the same.
3351 * Does not free the growarray.
3352 */
3353 void
3354win_size_restore(gap)
3355 garray_T *gap;
3356{
3357 win_T *wp;
3358 int i;
3359
3360 if (win_count() * 2 == gap->ga_len)
3361 {
3362 i = 0;
3363 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3364 {
3365 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3366 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3367 }
3368 /* recompute the window positions */
3369 (void)win_comp_pos();
3370 }
3371}
3372#endif /* FEAT_CMDWIN */
3373
3374#if defined(FEAT_WINDOWS) || defined(PROTO)
3375/*
3376 * Update the position for all windows, using the width and height of the
3377 * frames.
3378 * Returns the row just after the last window.
3379 */
3380 static int
3381win_comp_pos()
3382{
3383 int row = 0;
3384 int col = 0;
3385
3386 frame_comp_pos(topframe, &row, &col);
3387 return row;
3388}
3389
3390/*
3391 * Update the position of the windows in frame "topfrp", using the width and
3392 * height of the frames.
3393 * "*row" and "*col" are the top-left position of the frame. They are updated
3394 * to the bottom-right position plus one.
3395 */
3396 static void
3397frame_comp_pos(topfrp, row, col)
3398 frame_T *topfrp;
3399 int *row;
3400 int *col;
3401{
3402 win_T *wp;
3403 frame_T *frp;
3404#ifdef FEAT_VERTSPLIT
3405 int startcol;
3406 int startrow;
3407#endif
3408
3409 wp = topfrp->fr_win;
3410 if (wp != NULL)
3411 {
3412 if (wp->w_winrow != *row
3413#ifdef FEAT_VERTSPLIT
3414 || wp->w_wincol != *col
3415#endif
3416 )
3417 {
3418 /* position changed, redraw */
3419 wp->w_winrow = *row;
3420#ifdef FEAT_VERTSPLIT
3421 wp->w_wincol = *col;
3422#endif
3423 redraw_win_later(wp, NOT_VALID);
3424 wp->w_redr_status = TRUE;
3425 }
3426 *row += wp->w_height + wp->w_status_height;
3427#ifdef FEAT_VERTSPLIT
3428 *col += wp->w_width + wp->w_vsep_width;
3429#endif
3430 }
3431 else
3432 {
3433#ifdef FEAT_VERTSPLIT
3434 startrow = *row;
3435 startcol = *col;
3436#endif
3437 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3438 {
3439#ifdef FEAT_VERTSPLIT
3440 if (topfrp->fr_layout == FR_ROW)
3441 *row = startrow; /* all frames are at the same row */
3442 else
3443 *col = startcol; /* all frames are at the same col */
3444#endif
3445 frame_comp_pos(frp, row, col);
3446 }
3447 }
3448}
3449
3450#endif /* FEAT_WINDOWS */
3451
3452/*
3453 * Set current window height and take care of repositioning other windows to
3454 * fit around it.
3455 */
3456 void
3457win_setheight(height)
3458 int height;
3459{
3460 win_setheight_win(height, curwin);
3461}
3462
3463/*
3464 * Set the window height of window "win" and take care of repositioning other
3465 * windows to fit around it.
3466 */
3467 void
3468win_setheight_win(height, win)
3469 int height;
3470 win_T *win;
3471{
3472 int row;
3473
3474 if (win == curwin)
3475 {
3476 /* Always keep current window at least one line high, even when
3477 * 'winminheight' is zero. */
3478#ifdef FEAT_WINDOWS
3479 if (height < p_wmh)
3480 height = p_wmh;
3481#endif
3482 if (height == 0)
3483 height = 1;
3484 }
3485
3486#ifdef FEAT_WINDOWS
3487 frame_setheight(win->w_frame, height + win->w_status_height);
3488
3489 /* recompute the window positions */
3490 row = win_comp_pos();
3491#else
3492 if (height > topframe->fr_height)
3493 height = topframe->fr_height;
3494 win->w_height = height;
3495 row = height;
3496#endif
3497
3498 /*
3499 * If there is extra space created between the last window and the command
3500 * line, clear it.
3501 */
3502 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
3503 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
3504 cmdline_row = row;
3505 msg_row = row;
3506 msg_col = 0;
3507
3508 redraw_all_later(NOT_VALID);
3509}
3510
3511#if defined(FEAT_WINDOWS) || defined(PROTO)
3512
3513/*
3514 * Set the height of a frame to "height" and take care that all frames and
3515 * windows inside it are resized. Also resize frames on the left and right if
3516 * the are in the same FR_ROW frame.
3517 *
3518 * Strategy:
3519 * If the frame is part of a FR_COL frame, try fitting the frame in that
3520 * frame. If that doesn't work (the FR_COL frame is too small), recursively
3521 * go to containing frames to resize them and make room.
3522 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
3523 * Check for the minimal height of the FR_ROW frame.
3524 * At the top level we can also use change the command line height.
3525 */
3526 static void
3527frame_setheight(curfrp, height)
3528 frame_T *curfrp;
3529 int height;
3530{
3531 int room; /* total number of lines available */
3532 int take; /* number of lines taken from other windows */
3533 int room_cmdline; /* lines available from cmdline */
3534 int run;
3535 frame_T *frp;
3536 int h;
3537 int room_reserved;
3538
3539 /* If the height already is the desired value, nothing to do. */
3540 if (curfrp->fr_height == height)
3541 return;
3542
3543 if (curfrp->fr_parent == NULL)
3544 {
3545 /* topframe: can only change the command line */
3546 if (height > Rows - p_ch)
3547 height = Rows - p_ch;
3548 if (height > 0)
3549 frame_new_height(curfrp, height, FALSE, FALSE);
3550 }
3551 else if (curfrp->fr_parent->fr_layout == FR_ROW)
3552 {
3553 /* Row of frames: Also need to resize frames left and right of this
3554 * one. First check for the minimal height of these. */
3555 h = frame_minheight(curfrp->fr_parent, NULL);
3556 if (height < h)
3557 height = h;
3558 frame_setheight(curfrp->fr_parent, height);
3559 }
3560 else
3561 {
3562 /*
3563 * Column of frames: try to change only frames in this column.
3564 */
3565#ifdef FEAT_VERTSPLIT
3566 /*
3567 * Do this twice:
3568 * 1: compute room available, if it's not enough try resizing the
3569 * containing frame.
3570 * 2: compute the room available and adjust the height to it.
3571 * Try not to reduce the height of a window with 'winfixheight' set.
3572 */
3573 for (run = 1; run <= 2; ++run)
3574#else
3575 for (;;)
3576#endif
3577 {
3578 room = 0;
3579 room_reserved = 0;
3580 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3581 frp = frp->fr_next)
3582 {
3583 if (frp != curfrp
3584 && frp->fr_win != NULL
3585 && frp->fr_win->w_p_wfh)
3586 room_reserved += frp->fr_height;
3587 room += frp->fr_height;
3588 if (frp != curfrp)
3589 room -= frame_minheight(frp, NULL);
3590 }
3591#ifdef FEAT_VERTSPLIT
3592 if (curfrp->fr_width != Columns)
3593 room_cmdline = 0;
3594 else
3595#endif
3596 {
3597 room_cmdline = Rows - p_ch - (lastwin->w_winrow
3598 + lastwin->w_height + lastwin->w_status_height);
3599 if (room_cmdline < 0)
3600 room_cmdline = 0;
3601 }
3602
3603 if (height <= room + room_cmdline)
3604 break;
3605#ifdef FEAT_VERTSPLIT
3606 if (run == 2 || curfrp->fr_width == Columns)
3607#endif
3608 {
3609 if (height > room + room_cmdline)
3610 height = room + room_cmdline;
3611 break;
3612 }
3613#ifdef FEAT_VERTSPLIT
3614 frame_setheight(curfrp->fr_parent, height
3615 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
3616#endif
3617 /*NOTREACHED*/
3618 }
3619
3620 /*
3621 * Compute the number of lines we will take from others frames (can be
3622 * negative!).
3623 */
3624 take = height - curfrp->fr_height;
3625
3626 /* If there is not enough room, also reduce the height of a window
3627 * with 'winfixheight' set. */
3628 if (height > room + room_cmdline - room_reserved)
3629 room_reserved = room + room_cmdline - height;
3630 /* If there is only a 'winfixheight' window and making the
3631 * window smaller, need to make the other window taller. */
3632 if (take < 0 && room - curfrp->fr_height < room_reserved)
3633 room_reserved = 0;
3634
3635 if (take > 0 && room_cmdline > 0)
3636 {
3637 /* use lines from cmdline first */
3638 if (take < room_cmdline)
3639 room_cmdline = take;
3640 take -= room_cmdline;
3641 topframe->fr_height += room_cmdline;
3642 }
3643
3644 /*
3645 * set the current frame to the new height
3646 */
3647 frame_new_height(curfrp, height, FALSE, FALSE);
3648
3649 /*
3650 * First take lines from the frames after the current frame. If
3651 * that is not enough, takes lines from frames above the current
3652 * frame.
3653 */
3654 for (run = 0; run < 2; ++run)
3655 {
3656 if (run == 0)
3657 frp = curfrp->fr_next; /* 1st run: start with next window */
3658 else
3659 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3660 while (frp != NULL && take != 0)
3661 {
3662 h = frame_minheight(frp, NULL);
3663 if (room_reserved > 0
3664 && frp->fr_win != NULL
3665 && frp->fr_win->w_p_wfh)
3666 {
3667 if (room_reserved >= frp->fr_height)
3668 room_reserved -= frp->fr_height;
3669 else
3670 {
3671 if (frp->fr_height - room_reserved > take)
3672 room_reserved = frp->fr_height - take;
3673 take -= frp->fr_height - room_reserved;
3674 frame_new_height(frp, room_reserved, FALSE, FALSE);
3675 room_reserved = 0;
3676 }
3677 }
3678 else
3679 {
3680 if (frp->fr_height - take < h)
3681 {
3682 take -= frp->fr_height - h;
3683 frame_new_height(frp, h, FALSE, FALSE);
3684 }
3685 else
3686 {
3687 frame_new_height(frp, frp->fr_height - take,
3688 FALSE, FALSE);
3689 take = 0;
3690 }
3691 }
3692 if (run == 0)
3693 frp = frp->fr_next;
3694 else
3695 frp = frp->fr_prev;
3696 }
3697 }
3698 }
3699}
3700
3701#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3702/*
3703 * Set current window width and take care of repositioning other windows to
3704 * fit around it.
3705 */
3706 void
3707win_setwidth(width)
3708 int width;
3709{
3710 win_setwidth_win(width, curwin);
3711}
3712
3713 void
3714win_setwidth_win(width, wp)
3715 int width;
3716 win_T *wp;
3717{
3718 /* Always keep current window at least one column wide, even when
3719 * 'winminwidth' is zero. */
3720 if (wp == curwin)
3721 {
3722 if (width < p_wmw)
3723 width = p_wmw;
3724 if (width == 0)
3725 width = 1;
3726 }
3727
3728 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
3729
3730 /* recompute the window positions */
3731 (void)win_comp_pos();
3732
3733 redraw_all_later(NOT_VALID);
3734}
3735
3736/*
3737 * Set the width of a frame to "width" and take care that all frames and
3738 * windows inside it are resized. Also resize frames above and below if the
3739 * are in the same FR_ROW frame.
3740 *
3741 * Strategy is similar to frame_setheight().
3742 */
3743 static void
3744frame_setwidth(curfrp, width)
3745 frame_T *curfrp;
3746 int width;
3747{
3748 int room; /* total number of lines available */
3749 int take; /* number of lines taken from other windows */
3750 int run;
3751 frame_T *frp;
3752 int w;
3753
3754 /* If the width already is the desired value, nothing to do. */
3755 if (curfrp->fr_width == width)
3756 return;
3757
3758 if (curfrp->fr_parent == NULL)
3759 /* topframe: can't change width */
3760 return;
3761
3762 if (curfrp->fr_parent->fr_layout == FR_COL)
3763 {
3764 /* Column of frames: Also need to resize frames above and below of
3765 * this one. First check for the minimal width of these. */
3766 w = frame_minwidth(curfrp->fr_parent, NULL);
3767 if (width < w)
3768 width = w;
3769 frame_setwidth(curfrp->fr_parent, width);
3770 }
3771 else
3772 {
3773 /*
3774 * Row of frames: try to change only frames in this row.
3775 *
3776 * Do this twice:
3777 * 1: compute room available, if it's not enough try resizing the
3778 * containing frame.
3779 * 2: compute the room available and adjust the width to it.
3780 */
3781 for (run = 1; run <= 2; ++run)
3782 {
3783 room = 0;
3784 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3785 frp = frp->fr_next)
3786 {
3787 room += frp->fr_width;
3788 if (frp != curfrp)
3789 room -= frame_minwidth(frp, NULL);
3790 }
3791
3792 if (width <= room)
3793 break;
3794 if (run == 2 || curfrp->fr_height >= Rows - p_ch)
3795 {
3796 if (width > room)
3797 width = room;
3798 break;
3799 }
3800 frame_setwidth(curfrp->fr_parent, width
3801 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
3802 }
3803
3804
3805 /*
3806 * Compute the number of lines we will take from others frames (can be
3807 * negative!).
3808 */
3809 take = width - curfrp->fr_width;
3810
3811 /*
3812 * set the current frame to the new width
3813 */
3814 frame_new_width(curfrp, width, FALSE);
3815
3816 /*
3817 * First take lines from the frames right of the current frame. If
3818 * that is not enough, takes lines from frames left of the current
3819 * frame.
3820 */
3821 for (run = 0; run < 2; ++run)
3822 {
3823 if (run == 0)
3824 frp = curfrp->fr_next; /* 1st run: start with next window */
3825 else
3826 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3827 while (frp != NULL && take != 0)
3828 {
3829 w = frame_minwidth(frp, NULL);
3830 if (frp->fr_width - take < w)
3831 {
3832 take -= frp->fr_width - w;
3833 frame_new_width(frp, w, FALSE);
3834 }
3835 else
3836 {
3837 frame_new_width(frp, frp->fr_width - take, FALSE);
3838 take = 0;
3839 }
3840 if (run == 0)
3841 frp = frp->fr_next;
3842 else
3843 frp = frp->fr_prev;
3844 }
3845 }
3846 }
3847}
3848#endif /* FEAT_VERTSPLIT */
3849
3850/*
3851 * Check 'winminheight' for a valid value.
3852 */
3853 void
3854win_setminheight()
3855{
3856 int room;
3857 int first = TRUE;
3858 win_T *wp;
3859
3860 /* loop until there is a 'winminheight' that is possible */
3861 while (p_wmh > 0)
3862 {
3863 /* TODO: handle vertical splits */
3864 room = -p_wh;
3865 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3866 room += wp->w_height - p_wmh;
3867 if (room >= 0)
3868 break;
3869 --p_wmh;
3870 if (first)
3871 {
3872 EMSG(_(e_noroom));
3873 first = FALSE;
3874 }
3875 }
3876}
3877
3878#ifdef FEAT_MOUSE
3879
3880/*
3881 * Status line of dragwin is dragged "offset" lines down (negative is up).
3882 */
3883 void
3884win_drag_status_line(dragwin, offset)
3885 win_T *dragwin;
3886 int offset;
3887{
3888 frame_T *curfr;
3889 frame_T *fr;
3890 int room;
3891 int row;
3892 int up; /* if TRUE, drag status line up, otherwise down */
3893 int n;
3894
3895 fr = dragwin->w_frame;
3896 curfr = fr;
3897 if (fr != topframe) /* more than one window */
3898 {
3899 fr = fr->fr_parent;
3900 /* When the parent frame is not a column of frames, its parent should
3901 * be. */
3902 if (fr->fr_layout != FR_COL)
3903 {
3904 curfr = fr;
3905 if (fr != topframe) /* only a row of windows, may drag statusline */
3906 fr = fr->fr_parent;
3907 }
3908 }
3909
3910 /* If this is the last frame in a column, may want to resize the parent
3911 * frame instead (go two up to skip a row of frames). */
3912 while (curfr != topframe && curfr->fr_next == NULL)
3913 {
3914 if (fr != topframe)
3915 fr = fr->fr_parent;
3916 curfr = fr;
3917 if (fr != topframe)
3918 fr = fr->fr_parent;
3919 }
3920
3921 if (offset < 0) /* drag up */
3922 {
3923 up = TRUE;
3924 offset = -offset;
3925 /* sum up the room of the current frame and above it */
3926 if (fr == curfr)
3927 {
3928 /* only one window */
3929 room = fr->fr_height - frame_minheight(fr, NULL);
3930 }
3931 else
3932 {
3933 room = 0;
3934 for (fr = fr->fr_child; ; fr = fr->fr_next)
3935 {
3936 room += fr->fr_height - frame_minheight(fr, NULL);
3937 if (fr == curfr)
3938 break;
3939 }
3940 }
3941 fr = curfr->fr_next; /* put fr at frame that grows */
3942 }
3943 else /* drag down */
3944 {
3945 up = FALSE;
3946 /*
3947 * Only dragging the last status line can reduce p_ch.
3948 */
3949 room = Rows - cmdline_row;
3950 if (curfr->fr_next == NULL)
3951 room -= 1;
3952 else
3953 room -= p_ch;
3954 if (room < 0)
3955 room = 0;
3956 /* sum up the room of frames below of the current one */
3957 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
3958 room += fr->fr_height - frame_minheight(fr, NULL);
3959 fr = curfr; /* put fr at window that grows */
3960 }
3961
3962 if (room < offset) /* Not enough room */
3963 offset = room; /* Move as far as we can */
3964 if (offset <= 0)
3965 return;
3966
3967 /*
3968 * Grow frame fr by "offset" lines.
3969 * Doesn't happen when dragging the last status line up.
3970 */
3971 if (fr != NULL)
3972 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
3973
3974 if (up)
3975 fr = curfr; /* current frame gets smaller */
3976 else
3977 fr = curfr->fr_next; /* next frame gets smaller */
3978
3979 /*
3980 * Now make the other frames smaller.
3981 */
3982 while (fr != NULL && offset > 0)
3983 {
3984 n = frame_minheight(fr, NULL);
3985 if (fr->fr_height - offset <= n)
3986 {
3987 offset -= fr->fr_height - n;
3988 frame_new_height(fr, n, !up, FALSE);
3989 }
3990 else
3991 {
3992 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
3993 break;
3994 }
3995 if (up)
3996 fr = fr->fr_prev;
3997 else
3998 fr = fr->fr_next;
3999 }
4000 row = win_comp_pos();
4001 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4002 cmdline_row = row;
4003 p_ch = Rows - cmdline_row;
4004 if (p_ch < 1)
4005 p_ch = 1;
4006 redraw_all_later(NOT_VALID);
4007 showmode();
4008}
4009
4010#ifdef FEAT_VERTSPLIT
4011/*
4012 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4013 */
4014 void
4015win_drag_vsep_line(dragwin, offset)
4016 win_T *dragwin;
4017 int offset;
4018{
4019 frame_T *curfr;
4020 frame_T *fr;
4021 int room;
4022 int left; /* if TRUE, drag separator line left, otherwise right */
4023 int n;
4024
4025 fr = dragwin->w_frame;
4026 if (fr == topframe) /* only one window (cannot happe?) */
4027 return;
4028 curfr = fr;
4029 fr = fr->fr_parent;
4030 /* When the parent frame is not a row of frames, its parent should be. */
4031 if (fr->fr_layout != FR_ROW)
4032 {
4033 if (fr == topframe) /* only a column of windows (cannot happen?) */
4034 return;
4035 curfr = fr;
4036 fr = fr->fr_parent;
4037 }
4038
4039 /* If this is the last frame in a row, may want to resize a parent
4040 * frame instead. */
4041 while (curfr->fr_next == NULL)
4042 {
4043 if (fr == topframe)
4044 break;
4045 curfr = fr;
4046 fr = fr->fr_parent;
4047 if (fr != topframe)
4048 {
4049 curfr = fr;
4050 fr = fr->fr_parent;
4051 }
4052 }
4053
4054 if (offset < 0) /* drag left */
4055 {
4056 left = TRUE;
4057 offset = -offset;
4058 /* sum up the room of the current frame and left of it */
4059 room = 0;
4060 for (fr = fr->fr_child; ; fr = fr->fr_next)
4061 {
4062 room += fr->fr_width - frame_minwidth(fr, NULL);
4063 if (fr == curfr)
4064 break;
4065 }
4066 fr = curfr->fr_next; /* put fr at frame that grows */
4067 }
4068 else /* drag right */
4069 {
4070 left = FALSE;
4071 /* sum up the room of frames right of the current one */
4072 room = 0;
4073 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4074 room += fr->fr_width - frame_minwidth(fr, NULL);
4075 fr = curfr; /* put fr at window that grows */
4076 }
4077
4078 if (room < offset) /* Not enough room */
4079 offset = room; /* Move as far as we can */
4080 if (offset <= 0) /* No room at all, quit. */
4081 return;
4082
4083 /* grow frame fr by offset lines */
4084 frame_new_width(fr, fr->fr_width + offset, left);
4085
4086 /* shrink other frames: current and at the left or at the right */
4087 if (left)
4088 fr = curfr; /* current frame gets smaller */
4089 else
4090 fr = curfr->fr_next; /* next frame gets smaller */
4091
4092 while (fr != NULL && offset > 0)
4093 {
4094 n = frame_minwidth(fr, NULL);
4095 if (fr->fr_width - offset <= n)
4096 {
4097 offset -= fr->fr_width - n;
4098 frame_new_width(fr, n, !left);
4099 }
4100 else
4101 {
4102 frame_new_width(fr, fr->fr_width - offset, !left);
4103 break;
4104 }
4105 if (left)
4106 fr = fr->fr_prev;
4107 else
4108 fr = fr->fr_next;
4109 }
4110 (void)win_comp_pos();
4111 redraw_all_later(NOT_VALID);
4112}
4113#endif /* FEAT_VERTSPLIT */
4114#endif /* FEAT_MOUSE */
4115
4116#endif /* FEAT_WINDOWS */
4117
4118/*
4119 * Set the height of a window.
4120 * This takes care of the things inside the window, not what happens to the
4121 * window position, the frame or to other windows.
4122 */
4123 static void
4124win_new_height(wp, height)
4125 win_T *wp;
4126 int height;
4127{
4128 linenr_T lnum;
4129 int sline, line_size;
4130#define FRACTION_MULT 16384L
4131
4132 /* Don't want a negative height. Happens when splitting a tiny window.
4133 * Will equalize heights soon to fix it. */
4134 if (height < 0)
4135 height = 0;
4136
4137 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4138 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4139 + FRACTION_MULT / 2) / (long)wp->w_height;
4140
4141 wp->w_height = height;
4142 wp->w_skipcol = 0;
4143
4144 /* Don't change w_topline when height is zero. Don't set w_topline when
4145 * 'scrollbind' is set and this isn't the current window. */
4146 if (height > 0
4147#ifdef FEAT_SCROLLBIND
4148 && (!wp->w_p_scb || wp == curwin)
4149#endif
4150 )
4151 {
4152 lnum = wp->w_cursor.lnum;
4153 if (lnum < 1) /* can happen when starting up */
4154 lnum = 1;
4155 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4156 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4157 sline = wp->w_wrow - line_size;
4158 if (sline < 0)
4159 {
4160 /*
4161 * Cursor line would go off top of screen if w_wrow was this high.
4162 */
4163 wp->w_wrow = line_size;
4164 }
4165 else
4166 {
4167 while (sline > 0 && lnum > 1)
4168 {
4169#ifdef FEAT_FOLDING
4170 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4171 if (lnum == 1)
4172 {
4173 /* first line in buffer is folded */
4174 line_size = 1;
4175 --sline;
4176 break;
4177 }
4178#endif
4179 --lnum;
4180#ifdef FEAT_DIFF
4181 if (lnum == wp->w_topline)
4182 line_size = plines_win_nofill(wp, lnum, TRUE)
4183 + wp->w_topfill;
4184 else
4185#endif
4186 line_size = plines_win(wp, lnum, TRUE);
4187 sline -= line_size;
4188 }
4189 if (sline < 0)
4190 {
4191 /*
4192 * Line we want at top would go off top of screen. Use next
4193 * line instead.
4194 */
4195#ifdef FEAT_FOLDING
4196 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4197#endif
4198 lnum++;
4199 wp->w_wrow -= line_size + sline;
4200 }
4201 else if (sline > 0)
4202 {
4203 /* First line of file reached, use that as topline. */
4204 lnum = 1;
4205 wp->w_wrow -= sline;
4206 }
4207 }
4208 set_topline(wp, lnum);
4209 }
4210
4211 if (wp == curwin)
4212 {
4213 if (p_so)
4214 update_topline();
4215 curs_columns(FALSE); /* validate w_wrow */
4216 }
4217 wp->w_prev_fraction_row = wp->w_wrow;
4218
4219 win_comp_scroll(wp);
4220 redraw_win_later(wp, NOT_VALID);
4221#ifdef FEAT_WINDOWS
4222 wp->w_redr_status = TRUE;
4223#endif
4224 invalidate_botline_win(wp);
4225}
4226
4227#ifdef FEAT_VERTSPLIT
4228/*
4229 * Set the width of a window.
4230 */
4231 static void
4232win_new_width(wp, width)
4233 win_T *wp;
4234 int width;
4235{
4236 wp->w_width = width;
4237 wp->w_lines_valid = 0;
4238 changed_line_abv_curs_win(wp);
4239 invalidate_botline_win(wp);
4240 if (wp == curwin)
4241 {
4242 update_topline();
4243 curs_columns(TRUE); /* validate w_wrow */
4244 }
4245 redraw_win_later(wp, NOT_VALID);
4246 wp->w_redr_status = TRUE;
4247}
4248#endif
4249
4250 void
4251win_comp_scroll(wp)
4252 win_T *wp;
4253{
4254 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4255 if (wp->w_p_scr == 0)
4256 wp->w_p_scr = 1;
4257}
4258
4259/*
4260 * command_height: called whenever p_ch has been changed
4261 */
4262 void
4263command_height(old_p_ch)
4264 long old_p_ch;
4265{
4266#ifdef FEAT_WINDOWS
4267 int h;
4268 frame_T *frp;
4269
4270 /* Find bottom frame with width of screen. */
4271 frp = lastwin->w_frame;
4272# ifdef FEAT_VERTSPLIT
4273 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4274 frp = frp->fr_parent;
4275# endif
4276
4277 /* Avoid changing the height of a window with 'winfixheight' set. */
4278 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4279 && frp->fr_win->w_p_wfh)
4280 frp = frp->fr_prev;
4281
4282 if (starting != NO_SCREEN)
4283 {
4284 cmdline_row = Rows - p_ch;
4285
4286 if (p_ch > old_p_ch) /* p_ch got bigger */
4287 {
4288 while (p_ch > old_p_ch)
4289 {
4290 if (frp == NULL)
4291 {
4292 EMSG(_(e_noroom));
4293 p_ch = old_p_ch;
4294 cmdline_row = Rows - p_ch;
4295 break;
4296 }
4297 h = frp->fr_height - frame_minheight(frp, NULL);
4298 if (h > p_ch - old_p_ch)
4299 h = p_ch - old_p_ch;
4300 old_p_ch += h;
4301 frame_add_height(frp, -h);
4302 frp = frp->fr_prev;
4303 }
4304
4305 /* Recompute window positions. */
4306 (void)win_comp_pos();
4307
4308 /* clear the lines added to cmdline */
4309 if (full_screen)
4310 screen_fill((int)(cmdline_row), (int)Rows, 0,
4311 (int)Columns, ' ', ' ', 0);
4312 msg_row = cmdline_row;
4313 redraw_cmdline = TRUE;
4314 return;
4315 }
4316
4317 if (msg_row < cmdline_row)
4318 msg_row = cmdline_row;
4319 redraw_cmdline = TRUE;
4320 }
4321 frame_add_height(frp, (int)(old_p_ch - p_ch));
4322
4323 /* Recompute window positions. */
4324 if (frp != lastwin->w_frame)
4325 (void)win_comp_pos();
4326#else
4327 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4328 cmdline_row = Rows - p_ch;
4329#endif
4330}
4331
4332#if defined(FEAT_WINDOWS) || defined(PROTO)
4333/*
4334 * Resize frame "frp" to be "n" lines higher (negative for less high).
4335 * Also resize the frames it is contained in.
4336 */
4337 static void
4338frame_add_height(frp, n)
4339 frame_T *frp;
4340 int n;
4341{
4342 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4343 for (;;)
4344 {
4345 frp = frp->fr_parent;
4346 if (frp == NULL)
4347 break;
4348 frp->fr_height += n;
4349 }
4350}
4351
4352/*
4353 * Add or remove a status line for the bottom window(s), according to the
4354 * value of 'laststatus'.
4355 */
4356 void
4357last_status(morewin)
4358 int morewin; /* pretend there are two or more windows */
4359{
4360 /* Don't make a difference between horizontal or vertical split. */
4361 last_status_rec(topframe, (p_ls == 2
4362 || (p_ls == 1 && (morewin || lastwin != firstwin))));
4363}
4364
4365 static void
4366last_status_rec(fr, statusline)
4367 frame_T *fr;
4368 int statusline;
4369{
4370 frame_T *fp;
4371 win_T *wp;
4372
4373 if (fr->fr_layout == FR_LEAF)
4374 {
4375 wp = fr->fr_win;
4376 if (wp->w_status_height != 0 && !statusline)
4377 {
4378 /* remove status line */
4379 win_new_height(wp, wp->w_height + 1);
4380 wp->w_status_height = 0;
4381 comp_col();
4382 }
4383 else if (wp->w_status_height == 0 && statusline)
4384 {
4385 /* Find a frame to take a line from. */
4386 fp = fr;
4387 while (fp->fr_height <= frame_minheight(fp, NULL))
4388 {
4389 if (fp == topframe)
4390 {
4391 EMSG(_(e_noroom));
4392 return;
4393 }
4394 /* In a column of frames: go to frame above. If already at
4395 * the top or in a row of frames: go to parent. */
4396 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
4397 fp = fp->fr_prev;
4398 else
4399 fp = fp->fr_parent;
4400 }
4401 wp->w_status_height = 1;
4402 if (fp != fr)
4403 {
4404 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
4405 frame_fix_height(wp);
4406 (void)win_comp_pos();
4407 }
4408 else
4409 win_new_height(wp, wp->w_height - 1);
4410 comp_col();
4411 redraw_all_later(NOT_VALID);
4412 }
4413 }
4414#ifdef FEAT_VERTSPLIT
4415 else if (fr->fr_layout == FR_ROW)
4416 {
4417 /* vertically split windows, set status line for each one */
4418 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
4419 last_status_rec(fp, statusline);
4420 }
4421#endif
4422 else
4423 {
4424 /* horizontally split window, set status line for last one */
4425 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
4426 ;
4427 last_status_rec(fp, statusline);
4428 }
4429}
4430
4431#endif /* FEAT_WINDOWS */
4432
4433#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4434/*
4435 * Return the file name under or after the cursor.
4436 *
4437 * The 'path' option is searched if the file name is not absolute.
4438 * The string returned has been alloc'ed and should be freed by the caller.
4439 * NULL is returned if the file name or file is not found.
4440 *
4441 * options:
4442 * FNAME_MESS give error messages
4443 * FNAME_EXP expand to path
4444 * FNAME_HYP check for hypertext link
4445 * FNAME_INCL apply "includeexpr"
4446 */
4447 char_u *
4448file_name_at_cursor(options, count)
4449 int options;
4450 long count;
4451{
4452 return file_name_in_line(ml_get_curline(),
4453 curwin->w_cursor.col, options, count, curbuf->b_ffname);
4454}
4455
4456/*
4457 * Return the name of the file under or after ptr[col].
4458 * Otherwise like file_name_at_cursor().
4459 */
4460 char_u *
4461file_name_in_line(line, col, options, count, rel_fname)
4462 char_u *line;
4463 int col;
4464 int options;
4465 long count;
4466 char_u *rel_fname; /* file we are searching relative to */
4467{
4468 char_u *ptr;
4469 int len;
4470
4471 /*
4472 * search forward for what could be the start of a file name
4473 */
4474 ptr = line + col;
4475 while (*ptr != NUL && !vim_isfilec(*ptr))
4476 ++ptr;
4477 if (*ptr == NUL) /* nothing found */
4478 {
4479 if (options & FNAME_MESS)
4480 EMSG(_("E446: No file name under cursor"));
4481 return NULL;
4482 }
4483
4484 /*
4485 * Search backward for first char of the file name.
4486 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
4487 */
4488 while (ptr > line)
4489 {
4490#ifdef FEAT_MBYTE
4491 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
4492 ptr -= len + 1;
4493 else
4494#endif
4495 if (vim_isfilec(ptr[-1])
4496 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
4497 --ptr;
4498 else
4499 break;
4500 }
4501
4502 /*
4503 * Search forward for the last char of the file name.
4504 * Also allow "://" when ':' is not in 'isfname'.
4505 */
4506 len = 0;
4507 while (vim_isfilec(ptr[len])
4508 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
4509#ifdef FEAT_MBYTE
4510 if (has_mbyte)
4511 len += (*mb_ptr2len_check)(ptr + len);
4512 else
4513#endif
4514 ++len;
4515
4516 /*
4517 * If there is trailing punctuation, remove it.
4518 * But don't remove "..", could be a directory name.
4519 */
4520 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
4521 && ptr[len - 2] != '.')
4522 --len;
4523
4524 return find_file_name_in_path(ptr, len, options, count, rel_fname);
4525}
4526
4527# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4528static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
4529
4530 static char_u *
4531eval_includeexpr(ptr, len)
4532 char_u *ptr;
4533 int len;
4534{
4535 char_u *res;
4536
4537 set_vim_var_string(VV_FNAME, ptr, len);
4538 res = eval_to_string_safe(curbuf->b_p_inex, NULL);
4539 set_vim_var_string(VV_FNAME, NULL, 0);
4540 return res;
4541}
4542#endif
4543
4544/*
4545 * Return the name of the file ptr[len] in 'path'.
4546 * Otherwise like file_name_at_cursor().
4547 */
4548 char_u *
4549find_file_name_in_path(ptr, len, options, count, rel_fname)
4550 char_u *ptr;
4551 int len;
4552 int options;
4553 long count;
4554 char_u *rel_fname; /* file we are searching relative to */
4555{
4556 char_u *file_name;
4557 int c;
4558# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4559 char_u *tofree = NULL;
4560
4561 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4562 {
4563 tofree = eval_includeexpr(ptr, len);
4564 if (tofree != NULL)
4565 {
4566 ptr = tofree;
4567 len = (int)STRLEN(ptr);
4568 }
4569 }
4570# endif
4571
4572 if (options & FNAME_EXP)
4573 {
4574 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4575 TRUE, rel_fname);
4576
4577# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4578 /*
4579 * If the file could not be found in a normal way, try applying
4580 * 'includeexpr' (unless done already).
4581 */
4582 if (file_name == NULL
4583 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4584 {
4585 tofree = eval_includeexpr(ptr, len);
4586 if (tofree != NULL)
4587 {
4588 ptr = tofree;
4589 len = (int)STRLEN(ptr);
4590 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4591 TRUE, rel_fname);
4592 }
4593 }
4594# endif
4595 if (file_name == NULL && (options & FNAME_MESS))
4596 {
4597 c = ptr[len];
4598 ptr[len] = NUL;
4599 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
4600 ptr[len] = c;
4601 }
4602
4603 /* Repeat finding the file "count" times. This matters when it
4604 * appears several times in the path. */
4605 while (file_name != NULL && --count > 0)
4606 {
4607 vim_free(file_name);
4608 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
4609 }
4610 }
4611 else
4612 file_name = vim_strnsave(ptr, len);
4613
4614# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4615 vim_free(tofree);
4616# endif
4617
4618 return file_name;
4619}
4620#endif /* FEAT_SEARCHPATH */
4621
4622/*
4623 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4624 * Also check for ":\\", which MS Internet Explorer accepts, return
4625 * URL_BACKSLASH.
4626 */
4627 static int
4628path_is_url(p)
4629 char_u *p;
4630{
4631 if (STRNCMP(p, "://", (size_t)3) == 0)
4632 return URL_SLASH;
4633 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4634 return URL_BACKSLASH;
4635 return 0;
4636}
4637
4638/*
4639 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4640 * Return URL_BACKSLASH for "name:\\".
4641 * Return zero otherwise.
4642 */
4643 int
4644path_with_url(fname)
4645 char_u *fname;
4646{
4647 char_u *p;
4648
4649 for (p = fname; isalpha(*p); ++p)
4650 ;
4651 return path_is_url(p);
4652}
4653
4654/*
4655 * Return TRUE if "name" is a full (absolute) path name or URL.
4656 */
4657 int
4658vim_isAbsName(name)
4659 char_u *name;
4660{
4661 return (path_with_url(name) != 0 || mch_isFullName(name));
4662}
4663
4664/*
4665 * Get absolute file name into buffer 'buf' of length 'len' bytes.
4666 *
4667 * return FAIL for failure, OK otherwise
4668 */
4669 int
4670vim_FullName(fname, buf, len, force)
4671 char_u *fname, *buf;
4672 int len;
4673 int force;
4674{
4675 int retval = OK;
4676 int url;
4677
4678 *buf = NUL;
4679 if (fname == NULL)
4680 return FAIL;
4681
4682 url = path_with_url(fname);
4683 if (!url)
4684 retval = mch_FullName(fname, buf, len, force);
4685 if (url || retval == FAIL)
4686 {
4687 /* something failed; use the file name (truncate when too long) */
4688 STRNCPY(buf, fname, len);
4689 buf[len - 1] = NUL;
4690 }
4691#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
4692 slash_adjust(buf);
4693#endif
4694 return retval;
4695}
4696
4697/*
4698 * Return the minimal number of rows that is needed on the screen to display
4699 * the current number of windows.
4700 */
4701 int
4702min_rows()
4703{
4704 int total;
4705
4706 if (firstwin == NULL) /* not initialized yet */
4707 return MIN_LINES;
4708
4709 total = 1; /* count the room for the command line */
4710#ifdef FEAT_WINDOWS
4711 total += frame_minheight(topframe, NULL);
4712#else
4713 total += 1; /* at least one window should have a line! */
4714#endif
4715 return total;
4716}
4717
4718/*
4719 * Return TRUE if there is only one window, not counting a help or preview
4720 * window, unless it is the current window.
4721 */
4722 int
4723only_one_window()
4724{
4725#ifdef FEAT_WINDOWS
4726 int count = 0;
4727 win_T *wp;
4728
4729 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4730 if (!(wp->w_buffer->b_help
4731# ifdef FEAT_QUICKFIX
4732 || wp->w_p_pvw
4733# endif
4734 ) || wp == curwin)
4735 ++count;
4736 return (count <= 1);
4737#else
4738 return TRUE;
4739#endif
4740}
4741
4742#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
4743/*
4744 * Correct the cursor line number in other windows. Used after changing the
4745 * current buffer, and before applying autocommands.
4746 * When "do_curwin" is TRUE, also check current window.
4747 */
4748 void
4749check_lnums(do_curwin)
4750 int do_curwin;
4751{
4752 win_T *wp;
4753
4754#ifdef FEAT_WINDOWS
4755 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4756 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
4757#else
4758 wp = curwin;
4759 if (do_curwin)
4760#endif
4761 {
4762 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4763 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4764 if (wp->w_topline > curbuf->b_ml.ml_line_count)
4765 wp->w_topline = curbuf->b_ml.ml_line_count;
4766 }
4767}
4768#endif
4769
4770#if defined(FEAT_WINDOWS) || defined(PROTO)
4771
4772/*
4773 * A snapshot of the window sizes, to restore them after closing the help
4774 * window.
4775 * Only these fields are used:
4776 * fr_layout
4777 * fr_width
4778 * fr_height
4779 * fr_next
4780 * fr_child
4781 * fr_win (only valid for the old curwin, NULL otherwise)
4782 */
4783static frame_T *snapshot = NULL;
4784
4785/*
4786 * Create a snapshot of the current frame sizes.
4787 */
4788 static void
4789make_snapshot()
4790{
4791 clear_snapshot();
4792 make_snapshot_rec(topframe, &snapshot);
4793}
4794
4795 static void
4796make_snapshot_rec(fr, frp)
4797 frame_T *fr;
4798 frame_T **frp;
4799{
4800 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
4801 if (*frp == NULL)
4802 return;
4803 (*frp)->fr_layout = fr->fr_layout;
4804# ifdef FEAT_VERTSPLIT
4805 (*frp)->fr_width = fr->fr_width;
4806# endif
4807 (*frp)->fr_height = fr->fr_height;
4808 if (fr->fr_next != NULL)
4809 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
4810 if (fr->fr_child != NULL)
4811 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
4812 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
4813 (*frp)->fr_win = curwin;
4814}
4815
4816/*
4817 * Remove any existing snapshot.
4818 */
4819 static void
4820clear_snapshot()
4821{
4822 clear_snapshot_rec(snapshot);
4823 snapshot = NULL;
4824}
4825
4826 static void
4827clear_snapshot_rec(fr)
4828 frame_T *fr;
4829{
4830 if (fr != NULL)
4831 {
4832 clear_snapshot_rec(fr->fr_next);
4833 clear_snapshot_rec(fr->fr_child);
4834 vim_free(fr);
4835 }
4836}
4837
4838/*
4839 * Restore a previously created snapshot, if there is any.
4840 * This is only done if the screen size didn't change and the window layout is
4841 * still the same.
4842 */
4843 static void
4844restore_snapshot(close_curwin)
4845 int close_curwin; /* closing current window */
4846{
4847 win_T *wp;
4848
4849 if (snapshot != NULL
4850# ifdef FEAT_VERTSPLIT
4851 && snapshot->fr_width == topframe->fr_width
4852# endif
4853 && snapshot->fr_height == topframe->fr_height
4854 && check_snapshot_rec(snapshot, topframe) == OK)
4855 {
4856 wp = restore_snapshot_rec(snapshot, topframe);
4857 win_comp_pos();
4858 if (wp != NULL && close_curwin)
4859 win_goto(wp);
4860 redraw_all_later(CLEAR);
4861 }
4862 clear_snapshot();
4863}
4864
4865/*
4866 * Check if frames "sn" and "fr" have the same layout, same following frames
4867 * and same children.
4868 */
4869 static int
4870check_snapshot_rec(sn, fr)
4871 frame_T *sn;
4872 frame_T *fr;
4873{
4874 if (sn->fr_layout != fr->fr_layout
4875 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
4876 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
4877 || (sn->fr_next != NULL
4878 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
4879 || (sn->fr_child != NULL
4880 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
4881 return FAIL;
4882 return OK;
4883}
4884
4885/*
4886 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
4887 * following frames and children.
4888 * Returns a pointer to the old current window, or NULL.
4889 */
4890 static win_T *
4891restore_snapshot_rec(sn, fr)
4892 frame_T *sn;
4893 frame_T *fr;
4894{
4895 win_T *wp = NULL;
4896 win_T *wp2;
4897
4898 fr->fr_height = sn->fr_height;
4899# ifdef FEAT_VERTSPLIT
4900 fr->fr_width = sn->fr_width;
4901# endif
4902 if (fr->fr_layout == FR_LEAF)
4903 {
4904 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
4905# ifdef FEAT_VERTSPLIT
4906 frame_new_width(fr, fr->fr_width, FALSE);
4907# endif
4908 wp = sn->fr_win;
4909 }
4910 if (sn->fr_next != NULL)
4911 {
4912 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
4913 if (wp2 != NULL)
4914 wp = wp2;
4915 }
4916 if (sn->fr_child != NULL)
4917 {
4918 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
4919 if (wp2 != NULL)
4920 wp = wp2;
4921 }
4922 return wp;
4923}
4924
4925#endif
4926
4927#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
4928/*
4929 * Return TRUE if there is any vertically split window.
4930 */
4931 int
4932win_hasvertsplit()
4933{
4934 frame_T *fr;
4935
4936 if (topframe->fr_layout == FR_ROW)
4937 return TRUE;
4938
4939 if (topframe->fr_layout == FR_COL)
4940 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
4941 if (fr->fr_layout == FR_ROW)
4942 return TRUE;
4943
4944 return FALSE;
4945}
4946#endif