blob: ec9c39e44229ab2e05d079e422fa5b403a9ba366 [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 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001927 if (p_ea
1928#ifdef FEAT_VERTSPLIT
1929 && (*p_ead == 'b' || *p_ead == dir)
1930#endif
1931 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 win_equal(curwin, TRUE,
1933#ifdef FEAT_VERTSPLIT
1934 dir
1935#else
1936 0
1937#endif
1938 );
1939 else
1940 win_comp_pos();
1941 if (close_curwin)
1942 {
1943 win_enter_ext(wp, FALSE, TRUE);
1944#ifdef FEAT_AUTOCMD
1945 if (other_buffer)
1946 /* careful: after this wp and win may be invalid! */
1947 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1948#endif
1949 }
1950
1951 /*
1952 * if last window has a status line now and we don't want one,
1953 * remove the status line
1954 */
1955 last_status(FALSE);
1956
1957 /* After closing the help window, try restoring the window layout from
1958 * before it was opened. */
1959 if (help_window)
1960 restore_snapshot(close_curwin);
1961
1962#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1963 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
1964 if (gui.in_use && !win_hasvertsplit())
1965 gui_init_which_components(NULL);
1966#endif
1967
1968 redraw_all_later(NOT_VALID);
1969}
1970
1971/*
1972 * Remove a window and its frame from the tree of frames.
1973 * Returns a pointer to the window that got the freed up space.
1974 */
1975/*ARGSUSED*/
1976 static win_T *
1977winframe_remove(win, dirp)
1978 win_T *win;
1979 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
1980{
1981 frame_T *frp, *frp2, *frp3;
1982 frame_T *frp_close = win->w_frame;
1983 win_T *wp;
1984 int old_height = 0;
1985
1986 /*
1987 * Remove the window from its frame.
1988 */
1989 frp2 = win_altframe(win);
1990 wp = frame2win(frp2);
1991
1992 /* Remove this frame from the list of frames. */
1993 frame_remove(frp_close);
1994
1995#ifdef FEAT_VERTSPLIT
1996 if (frp_close->fr_parent->fr_layout == FR_COL)
1997 {
1998#endif
1999 /* When 'winfixheight' is set, remember its old size and restore
2000 * it later (it's a simplistic solution...). Don't do this if the
2001 * window will occupy the full height of the screen. */
2002 if (frp2->fr_win != NULL
2003 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2004 && frp2->fr_win->w_p_wfh)
2005 old_height = frp2->fr_win->w_height;
2006 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2007 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2008 if (old_height != 0)
2009 win_setheight_win(old_height, frp2->fr_win);
2010#ifdef FEAT_VERTSPLIT
2011 *dirp = 'v';
2012 }
2013 else
2014 {
2015 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2016 frp2 == frp_close->fr_next ? TRUE : FALSE);
2017 *dirp = 'h';
2018 }
2019#endif
2020
2021 /* If rows/columns go to a window below/right its positions need to be
2022 * updated. Can only be done after the sizes have been updated. */
2023 if (frp2 == frp_close->fr_next)
2024 {
2025 int row = win->w_winrow;
2026 int col = W_WINCOL(win);
2027
2028 frame_comp_pos(frp2, &row, &col);
2029 }
2030
2031 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2032 {
2033 /* There is no other frame in this list, move its info to the parent
2034 * and remove it. */
2035 frp2->fr_parent->fr_layout = frp2->fr_layout;
2036 frp2->fr_parent->fr_child = frp2->fr_child;
2037 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2038 frp->fr_parent = frp2->fr_parent;
2039 frp2->fr_parent->fr_win = frp2->fr_win;
2040 if (frp2->fr_win != NULL)
2041 frp2->fr_win->w_frame = frp2->fr_parent;
2042 frp = frp2->fr_parent;
2043 vim_free(frp2);
2044
2045 frp2 = frp->fr_parent;
2046 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2047 {
2048 /* The frame above the parent has the same layout, have to merge
2049 * the frames into this list. */
2050 if (frp2->fr_child == frp)
2051 frp2->fr_child = frp->fr_child;
2052 frp->fr_child->fr_prev = frp->fr_prev;
2053 if (frp->fr_prev != NULL)
2054 frp->fr_prev->fr_next = frp->fr_child;
2055 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2056 {
2057 frp3->fr_parent = frp2;
2058 if (frp3->fr_next == NULL)
2059 {
2060 frp3->fr_next = frp->fr_next;
2061 if (frp->fr_next != NULL)
2062 frp->fr_next->fr_prev = frp3;
2063 break;
2064 }
2065 }
2066 vim_free(frp);
2067 }
2068 }
2069
2070 return wp;
2071}
2072
2073/*
2074 * Find out which frame is going to get the freed up space when "win" is
2075 * closed.
2076 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2077 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2078 * This makes opening a window and closing it immediately keep the same window
2079 * layout.
2080 */
2081 static frame_T *
2082win_altframe(win)
2083 win_T *win;
2084{
2085 frame_T *frp;
2086 int b;
2087
2088 frp = win->w_frame;
2089#ifdef FEAT_VERTSPLIT
2090 if (frp->fr_parent->fr_layout == FR_ROW)
2091 b = p_spr;
2092 else
2093#endif
2094 b = p_sb;
2095 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2096 return frp->fr_next;
2097 return frp->fr_prev;
2098}
2099
2100/*
2101 * Find the left-upper window in frame "frp".
2102 */
2103 static win_T *
2104frame2win(frp)
2105 frame_T *frp;
2106{
2107 while (frp->fr_win == NULL)
2108 frp = frp->fr_child;
2109 return frp->fr_win;
2110}
2111
2112/*
2113 * Return TRUE if frame "frp" contains window "wp".
2114 */
2115 static int
2116frame_has_win(frp, wp)
2117 frame_T *frp;
2118 win_T *wp;
2119{
2120 frame_T *p;
2121
2122 if (frp->fr_layout == FR_LEAF)
2123 return frp->fr_win == wp;
2124
2125 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2126 if (frame_has_win(p, wp))
2127 return TRUE;
2128 return FALSE;
2129}
2130
2131/*
2132 * Set a new height for a frame. Recursively sets the height for contained
2133 * frames and windows. Caller must take care of positions.
2134 */
2135 static void
2136frame_new_height(topfrp, height, topfirst, wfh)
2137 frame_T *topfrp;
2138 int height;
2139 int topfirst; /* resize topmost contained frame first */
2140 int wfh; /* obey 'winfixheight' when there is a choice;
2141 may cause the height not to be set */
2142{
2143 frame_T *frp;
2144 int extra_lines;
2145 int h;
2146
2147 if (topfrp->fr_win != NULL)
2148 {
2149 /* Simple case: just one window. */
2150 win_new_height(topfrp->fr_win,
2151 height - topfrp->fr_win->w_status_height);
2152 }
2153#ifdef FEAT_VERTSPLIT
2154 else if (topfrp->fr_layout == FR_ROW)
2155 {
2156 do
2157 {
2158 /* All frames in this row get the same new height. */
2159 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2160 {
2161 frame_new_height(frp, height, topfirst, wfh);
2162 if (frp->fr_height > height)
2163 {
2164 /* Could not fit the windows, make the whole row higher. */
2165 height = frp->fr_height;
2166 break;
2167 }
2168 }
2169 }
2170 while (frp != NULL);
2171 }
2172#endif
2173 else
2174 {
2175 /* Complicated case: Resize a column of frames. Resize the bottom
2176 * frame first, frames above that when needed. */
2177
2178 frp = topfrp->fr_child;
2179 if (wfh)
2180 /* Advance past frames with one window with 'wfh' set. */
2181 while (frame_fixed_height(frp))
2182 {
2183 frp = frp->fr_next;
2184 if (frp == NULL)
2185 return; /* no frame without 'wfh', give up */
2186 }
2187 if (!topfirst)
2188 {
2189 /* Find the bottom frame of this column */
2190 while (frp->fr_next != NULL)
2191 frp = frp->fr_next;
2192 if (wfh)
2193 /* Advance back for frames with one window with 'wfh' set. */
2194 while (frame_fixed_height(frp))
2195 frp = frp->fr_prev;
2196 }
2197
2198 extra_lines = height - topfrp->fr_height;
2199 if (extra_lines < 0)
2200 {
2201 /* reduce height of contained frames, bottom or top frame first */
2202 while (frp != NULL)
2203 {
2204 h = frame_minheight(frp, NULL);
2205 if (frp->fr_height + extra_lines < h)
2206 {
2207 extra_lines += frp->fr_height - h;
2208 frame_new_height(frp, h, topfirst, wfh);
2209 }
2210 else
2211 {
2212 frame_new_height(frp, frp->fr_height + extra_lines,
2213 topfirst, wfh);
2214 break;
2215 }
2216 if (topfirst)
2217 {
2218 do
2219 frp = frp->fr_next;
2220 while (wfh && frp != NULL && frame_fixed_height(frp));
2221 }
2222 else
2223 {
2224 do
2225 frp = frp->fr_prev;
2226 while (wfh && frp != NULL && frame_fixed_height(frp));
2227 }
2228 /* Increase "height" if we could not reduce enough frames. */
2229 if (frp == NULL)
2230 height -= extra_lines;
2231 }
2232 }
2233 else if (extra_lines > 0)
2234 {
2235 /* increase height of bottom or top frame */
2236 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2237 }
2238 }
2239 topfrp->fr_height = height;
2240}
2241
2242/*
2243 * Return TRUE if height of frame "frp" should not be changed because of
2244 * the 'winfixheight' option.
2245 */
2246 static int
2247frame_fixed_height(frp)
2248 frame_T *frp;
2249{
2250 /* frame with one window: fixed height if 'winfixheight' set. */
2251 if (frp->fr_win != NULL)
2252 return frp->fr_win->w_p_wfh;
2253
2254 if (frp->fr_layout == FR_ROW)
2255 {
2256 /* The frame is fixed height if one of the frames in the row is fixed
2257 * height. */
2258 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2259 if (frame_fixed_height(frp))
2260 return TRUE;
2261 return FALSE;
2262 }
2263
2264 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2265 * frames in the row are fixed height. */
2266 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2267 if (!frame_fixed_height(frp))
2268 return FALSE;
2269 return TRUE;
2270}
2271
2272#ifdef FEAT_VERTSPLIT
2273/*
2274 * Add a status line to windows at the bottom of "frp".
2275 * Note: Does not check if there is room!
2276 */
2277 static void
2278frame_add_statusline(frp)
2279 frame_T *frp;
2280{
2281 win_T *wp;
2282
2283 if (frp->fr_layout == FR_LEAF)
2284 {
2285 wp = frp->fr_win;
2286 if (wp->w_status_height == 0)
2287 {
2288 if (wp->w_height > 0) /* don't make it negative */
2289 --wp->w_height;
2290 wp->w_status_height = STATUS_HEIGHT;
2291 }
2292 }
2293 else if (frp->fr_layout == FR_ROW)
2294 {
2295 /* Handle all the frames in the row. */
2296 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2297 frame_add_statusline(frp);
2298 }
2299 else /* frp->fr_layout == FR_COL */
2300 {
2301 /* Only need to handle the last frame in the column. */
2302 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2303 ;
2304 frame_add_statusline(frp);
2305 }
2306}
2307
2308/*
2309 * Set width of a frame. Handles recursively going through contained frames.
2310 * May remove separator line for windows at the right side (for win_close()).
2311 */
2312 static void
2313frame_new_width(topfrp, width, leftfirst)
2314 frame_T *topfrp;
2315 int width;
2316 int leftfirst; /* resize leftmost contained frame first */
2317{
2318 frame_T *frp;
2319 int extra_cols;
2320 int w;
2321 win_T *wp;
2322
2323 if (topfrp->fr_layout == FR_LEAF)
2324 {
2325 /* Simple case: just one window. */
2326 wp = topfrp->fr_win;
2327 /* Find out if there are any windows right of this one. */
2328 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2329 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2330 break;
2331 if (frp->fr_parent == NULL)
2332 wp->w_vsep_width = 0;
2333 win_new_width(wp, width - wp->w_vsep_width);
2334 }
2335 else if (topfrp->fr_layout == FR_COL)
2336 {
2337 /* All frames in this column get the same new width. */
2338 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2339 frame_new_width(frp, width, leftfirst);
2340 }
2341 else /* fr_layout == FR_ROW */
2342 {
2343 /* Complicated case: Resize a row of frames. Resize the rightmost
2344 * frame first, frames left of it when needed. */
2345
2346 /* Find the rightmost frame of this row */
2347 frp = topfrp->fr_child;
2348 if (!leftfirst)
2349 while (frp->fr_next != NULL)
2350 frp = frp->fr_next;
2351
2352 extra_cols = width - topfrp->fr_width;
2353 if (extra_cols < 0)
2354 {
2355 /* reduce frame width, rightmost frame first */
2356 while (frp != NULL)
2357 {
2358 w = frame_minwidth(frp, NULL);
2359 if (frp->fr_width + extra_cols < w)
2360 {
2361 extra_cols += frp->fr_width - w;
2362 frame_new_width(frp, w, leftfirst);
2363 }
2364 else
2365 {
2366 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2367 break;
2368 }
2369 if (leftfirst)
2370 frp = frp->fr_next;
2371 else
2372 frp = frp->fr_prev;
2373 }
2374 }
2375 else if (extra_cols > 0)
2376 {
2377 /* increase width of rightmost frame */
2378 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2379 }
2380 }
2381 topfrp->fr_width = width;
2382}
2383
2384/*
2385 * Add the vertical separator to windows at the right side of "frp".
2386 * Note: Does not check if there is room!
2387 */
2388 static void
2389frame_add_vsep(frp)
2390 frame_T *frp;
2391{
2392 win_T *wp;
2393
2394 if (frp->fr_layout == FR_LEAF)
2395 {
2396 wp = frp->fr_win;
2397 if (wp->w_vsep_width == 0)
2398 {
2399 if (wp->w_width > 0) /* don't make it negative */
2400 --wp->w_width;
2401 wp->w_vsep_width = 1;
2402 }
2403 }
2404 else if (frp->fr_layout == FR_COL)
2405 {
2406 /* Handle all the frames in the column. */
2407 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2408 frame_add_vsep(frp);
2409 }
2410 else /* frp->fr_layout == FR_ROW */
2411 {
2412 /* Only need to handle the last frame in the row. */
2413 frp = frp->fr_child;
2414 while (frp->fr_next != NULL)
2415 frp = frp->fr_next;
2416 frame_add_vsep(frp);
2417 }
2418}
2419
2420/*
2421 * Set frame width from the window it contains.
2422 */
2423 static void
2424frame_fix_width(wp)
2425 win_T *wp;
2426{
2427 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2428}
2429#endif
2430
2431/*
2432 * Set frame height from the window it contains.
2433 */
2434 static void
2435frame_fix_height(wp)
2436 win_T *wp;
2437{
2438 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2439}
2440
2441/*
2442 * Compute the minimal height for frame "topfrp".
2443 * Uses the 'winminheight' option.
2444 * When "next_curwin" isn't NULL, use p_wh for this window.
2445 * When "next_curwin" is NOWIN, don't use at least one line for the current
2446 * window.
2447 */
2448 static int
2449frame_minheight(topfrp, next_curwin)
2450 frame_T *topfrp;
2451 win_T *next_curwin;
2452{
2453 frame_T *frp;
2454 int m;
2455#ifdef FEAT_VERTSPLIT
2456 int n;
2457#endif
2458
2459 if (topfrp->fr_win != NULL)
2460 {
2461 if (topfrp->fr_win == next_curwin)
2462 m = p_wh + topfrp->fr_win->w_status_height;
2463 else
2464 {
2465 /* window: minimal height of the window plus status line */
2466 m = p_wmh + topfrp->fr_win->w_status_height;
2467 /* Current window is minimal one line high */
2468 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2469 ++m;
2470 }
2471 }
2472#ifdef FEAT_VERTSPLIT
2473 else if (topfrp->fr_layout == FR_ROW)
2474 {
2475 /* get the minimal height from each frame in this row */
2476 m = 0;
2477 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2478 {
2479 n = frame_minheight(frp, next_curwin);
2480 if (n > m)
2481 m = n;
2482 }
2483 }
2484#endif
2485 else
2486 {
2487 /* Add up the minimal heights for all frames in this column. */
2488 m = 0;
2489 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2490 m += frame_minheight(frp, next_curwin);
2491 }
2492
2493 return m;
2494}
2495
2496#ifdef FEAT_VERTSPLIT
2497/*
2498 * Compute the minimal width for frame "topfrp".
2499 * When "next_curwin" isn't NULL, use p_wiw for this window.
2500 * When "next_curwin" is NOWIN, don't use at least one column for the current
2501 * window.
2502 */
2503 static int
2504frame_minwidth(topfrp, next_curwin)
2505 frame_T *topfrp;
2506 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2507{
2508 frame_T *frp;
2509 int m, n;
2510
2511 if (topfrp->fr_win != NULL)
2512 {
2513 if (topfrp->fr_win == next_curwin)
2514 m = p_wiw + topfrp->fr_win->w_vsep_width;
2515 else
2516 {
2517 /* window: minimal width of the window plus separator column */
2518 m = p_wmw + topfrp->fr_win->w_vsep_width;
2519 /* Current window is minimal one column wide */
2520 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2521 ++m;
2522 }
2523 }
2524 else if (topfrp->fr_layout == FR_COL)
2525 {
2526 /* get the minimal width from each frame in this column */
2527 m = 0;
2528 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2529 {
2530 n = frame_minwidth(frp, next_curwin);
2531 if (n > m)
2532 m = n;
2533 }
2534 }
2535 else
2536 {
2537 /* Add up the minimal widths for all frames in this row. */
2538 m = 0;
2539 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2540 m += frame_minwidth(frp, next_curwin);
2541 }
2542
2543 return m;
2544}
2545#endif
2546
2547
2548/*
2549 * Try to close all windows except current one.
2550 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2551 * used and the buffer was modified.
2552 *
2553 * Used by ":bdel" and ":only".
2554 */
2555 void
2556close_others(message, forceit)
2557 int message;
2558 int forceit; /* always hide all other windows */
2559{
2560 win_T *wp;
2561 win_T *nextwp;
2562 int r;
2563
2564 if (lastwin == firstwin)
2565 {
2566 if (message
2567#ifdef FEAT_AUTOCMD
2568 && !autocmd_busy
2569#endif
2570 )
2571 MSG(_("Already only one window"));
2572 return;
2573 }
2574
2575 /* Be very careful here: autocommands may change the window layout. */
2576 for (wp = firstwin; win_valid(wp); wp = nextwp)
2577 {
2578 nextwp = wp->w_next;
2579 if (wp != curwin) /* don't close current window */
2580 {
2581
2582 /* Check if it's allowed to abandon this window */
2583 r = can_abandon(wp->w_buffer, forceit);
2584#ifdef FEAT_AUTOCMD
2585 if (!win_valid(wp)) /* autocommands messed wp up */
2586 {
2587 nextwp = firstwin;
2588 continue;
2589 }
2590#endif
2591 if (!r)
2592 {
2593#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2594 if (message && (p_confirm || cmdmod.confirm) && p_write)
2595 {
2596 dialog_changed(wp->w_buffer, FALSE);
2597# ifdef FEAT_AUTOCMD
2598 if (!win_valid(wp)) /* autocommands messed wp up */
2599 {
2600 nextwp = firstwin;
2601 continue;
2602 }
2603# endif
2604 }
2605 if (bufIsChanged(wp->w_buffer))
2606#endif
2607 continue;
2608 }
2609 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2610 }
2611 }
2612
2613 /*
2614 * If current window has a status line and we don't want one,
2615 * remove the status line.
2616 */
2617 if (lastwin != firstwin)
2618 EMSG(_("E445: Other window contains changes"));
2619}
2620
2621#endif /* FEAT_WINDOWS */
2622
2623/*
2624 * init the cursor in the window
2625 *
2626 * called when a new file is being edited
2627 */
2628 void
2629win_init(wp)
2630 win_T *wp;
2631{
2632 redraw_win_later(wp, NOT_VALID);
2633 wp->w_lines_valid = 0;
2634 wp->w_cursor.lnum = 1;
2635 wp->w_curswant = wp->w_cursor.col = 0;
2636#ifdef FEAT_VIRTUALEDIT
2637 wp->w_cursor.coladd = 0;
2638#endif
2639 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2640 wp->w_pcmark.col = 0;
2641 wp->w_prev_pcmark.lnum = 0;
2642 wp->w_prev_pcmark.col = 0;
2643 wp->w_topline = 1;
2644#ifdef FEAT_DIFF
2645 wp->w_topfill = 0;
2646#endif
2647 wp->w_botline = 2;
2648#ifdef FEAT_FKMAP
2649 if (curwin->w_p_rl)
2650 wp->w_farsi = W_CONV + W_R_L;
2651 else
2652 wp->w_farsi = W_CONV;
2653#endif
2654}
2655
2656/*
2657 * Allocate the first window and put an empty buffer in it.
2658 * Called from main().
2659 * When this fails we can't do anything: exit.
2660 */
2661 void
2662win_alloc_first()
2663{
2664 curwin = win_alloc(NULL);
2665 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2666 if (curwin == NULL || curbuf == NULL)
2667 mch_exit(0);
2668 curwin->w_buffer = curbuf;
2669 curbuf->b_nwindows = 1; /* there is one window */
2670#ifdef FEAT_WINDOWS
2671 curwin->w_alist = &global_alist;
2672#endif
2673 win_init(curwin); /* init current window */
2674
2675 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2676 if (topframe == NULL)
2677 mch_exit(0);
2678 topframe->fr_layout = FR_LEAF;
2679#ifdef FEAT_VERTSPLIT
2680 topframe->fr_width = Columns;
2681#endif
2682 topframe->fr_height = Rows - p_ch;
2683 topframe->fr_win = curwin;
2684 curwin->w_frame = topframe;
2685}
2686
2687#if defined(FEAT_WINDOWS) || defined(PROTO)
2688
2689/*
2690 * Go to another window.
2691 * When jumping to another buffer, stop Visual mode. Do this before
2692 * changing windows so we can yank the selection into the '*' register.
2693 * When jumping to another window on the same buffer, adjust its cursor
2694 * position to keep the same Visual area.
2695 */
2696 void
2697win_goto(wp)
2698 win_T *wp;
2699{
2700#ifdef FEAT_CMDWIN
2701 if (cmdwin_type != 0)
2702 {
2703 beep_flush();
2704 return;
2705 }
2706#endif
2707#ifdef FEAT_VISUAL
2708 if (wp->w_buffer != curbuf)
2709 reset_VIsual_and_resel();
2710 else if (VIsual_active)
2711 wp->w_cursor = curwin->w_cursor;
2712#endif
2713
2714#ifdef FEAT_GUI
2715 need_mouse_correct = TRUE;
2716#endif
2717 win_enter(wp, TRUE);
2718}
2719
2720#if defined(FEAT_PERL) || defined(PROTO)
2721/*
2722 * Find window number "winnr" (counting top to bottom).
2723 */
2724 win_T *
2725win_find_nr(winnr)
2726 int winnr;
2727{
2728 win_T *wp;
2729
2730# ifdef FEAT_WINDOWS
2731 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2732 if (--winnr == 0)
2733 break;
2734 return wp;
2735# else
2736 return curwin;
2737# endif
2738}
2739#endif
2740
2741#ifdef FEAT_VERTSPLIT
2742/*
2743 * Move to window above or below "count" times.
2744 */
2745 static void
2746win_goto_ver(up, count)
2747 int up; /* TRUE to go to win above */
2748 long count;
2749{
2750 frame_T *fr;
2751 frame_T *nfr;
2752 frame_T *foundfr;
2753
2754 foundfr = curwin->w_frame;
2755 while (count--)
2756 {
2757 /*
2758 * First go upwards in the tree of frames until we find a upwards or
2759 * downwards neighbor.
2760 */
2761 fr = foundfr;
2762 for (;;)
2763 {
2764 if (fr == topframe)
2765 goto end;
2766 if (up)
2767 nfr = fr->fr_prev;
2768 else
2769 nfr = fr->fr_next;
2770 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
2771 break;
2772 fr = fr->fr_parent;
2773 }
2774
2775 /*
2776 * Now go downwards to find the bottom or top frame in it.
2777 */
2778 for (;;)
2779 {
2780 if (nfr->fr_layout == FR_LEAF)
2781 {
2782 foundfr = nfr;
2783 break;
2784 }
2785 fr = nfr->fr_child;
2786 if (nfr->fr_layout == FR_ROW)
2787 {
2788 /* Find the frame at the cursor row. */
2789 while (fr->fr_next != NULL
2790 && frame2win(fr)->w_wincol + fr->fr_width
2791 <= curwin->w_wincol + curwin->w_wcol)
2792 fr = fr->fr_next;
2793 }
2794 if (nfr->fr_layout == FR_COL && up)
2795 while (fr->fr_next != NULL)
2796 fr = fr->fr_next;
2797 nfr = fr;
2798 }
2799 }
2800end:
2801 if (foundfr != NULL)
2802 win_goto(foundfr->fr_win);
2803}
2804
2805/*
2806 * Move to left or right window.
2807 */
2808 static void
2809win_goto_hor(left, count)
2810 int left; /* TRUE to go to left win */
2811 long count;
2812{
2813 frame_T *fr;
2814 frame_T *nfr;
2815 frame_T *foundfr;
2816
2817 foundfr = curwin->w_frame;
2818 while (count--)
2819 {
2820 /*
2821 * First go upwards in the tree of frames until we find a left or
2822 * right neighbor.
2823 */
2824 fr = foundfr;
2825 for (;;)
2826 {
2827 if (fr == topframe)
2828 goto end;
2829 if (left)
2830 nfr = fr->fr_prev;
2831 else
2832 nfr = fr->fr_next;
2833 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
2834 break;
2835 fr = fr->fr_parent;
2836 }
2837
2838 /*
2839 * Now go downwards to find the leftmost or rightmost frame in it.
2840 */
2841 for (;;)
2842 {
2843 if (nfr->fr_layout == FR_LEAF)
2844 {
2845 foundfr = nfr;
2846 break;
2847 }
2848 fr = nfr->fr_child;
2849 if (nfr->fr_layout == FR_COL)
2850 {
2851 /* Find the frame at the cursor row. */
2852 while (fr->fr_next != NULL
2853 && frame2win(fr)->w_winrow + fr->fr_height
2854 <= curwin->w_winrow + curwin->w_wrow)
2855 fr = fr->fr_next;
2856 }
2857 if (nfr->fr_layout == FR_ROW && left)
2858 while (fr->fr_next != NULL)
2859 fr = fr->fr_next;
2860 nfr = fr;
2861 }
2862 }
2863end:
2864 if (foundfr != NULL)
2865 win_goto(foundfr->fr_win);
2866}
2867#endif
2868
2869/*
2870 * Make window "wp" the current window.
2871 */
2872 void
2873win_enter(wp, undo_sync)
2874 win_T *wp;
2875 int undo_sync;
2876{
2877 win_enter_ext(wp, undo_sync, FALSE);
2878}
2879
2880/*
2881 * Make window wp the current window.
2882 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
2883 * been closed and isn't valid.
2884 */
2885 static void
2886win_enter_ext(wp, undo_sync, curwin_invalid)
2887 win_T *wp;
2888 int undo_sync;
2889 int curwin_invalid;
2890{
2891#ifdef FEAT_AUTOCMD
2892 int other_buffer = FALSE;
2893#endif
2894
2895 if (wp == curwin && !curwin_invalid) /* nothing to do */
2896 return;
2897
2898#ifdef FEAT_AUTOCMD
2899 if (!curwin_invalid)
2900 {
2901 /*
2902 * Be careful: If autocommands delete the window, return now.
2903 */
2904 if (wp->w_buffer != curbuf)
2905 {
2906 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2907 other_buffer = TRUE;
2908 if (!win_valid(wp))
2909 return;
2910 }
2911 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2912 if (!win_valid(wp))
2913 return;
2914# ifdef FEAT_EVAL
2915 /* autocmds may abort script processing */
2916 if (aborting())
2917 return;
2918# endif
2919 }
2920#endif
2921
2922 /* sync undo before leaving the current buffer */
2923 if (undo_sync && curbuf != wp->w_buffer)
2924 u_sync();
2925 /* may have to copy the buffer options when 'cpo' contains 'S' */
2926 if (wp->w_buffer != curbuf)
2927 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
2928 if (!curwin_invalid)
2929 {
2930 prevwin = curwin; /* remember for CTRL-W p */
2931 curwin->w_redr_status = TRUE;
2932 }
2933 curwin = wp;
2934 curbuf = wp->w_buffer;
2935 check_cursor();
2936#ifdef FEAT_VIRTUALEDIT
2937 if (!virtual_active())
2938 curwin->w_cursor.coladd = 0;
2939#endif
2940 changed_line_abv_curs(); /* assume cursor position needs updating */
2941
2942 if (curwin->w_localdir != NULL)
2943 {
2944 /* Window has a local directory: Save current directory as global
2945 * directory (unless that was done already) and change to the local
2946 * directory. */
2947 if (globaldir == NULL)
2948 {
2949 char_u cwd[MAXPATHL];
2950
2951 if (mch_dirname(cwd, MAXPATHL) == OK)
2952 globaldir = vim_strsave(cwd);
2953 }
2954 mch_chdir((char *)curwin->w_localdir);
2955 shorten_fnames(TRUE);
2956 }
2957 else if (globaldir != NULL)
2958 {
2959 /* Window doesn't have a local directory and we are not in the global
2960 * directory: Change to the global directory. */
2961 mch_chdir((char *)globaldir);
2962 vim_free(globaldir);
2963 globaldir = NULL;
2964 shorten_fnames(TRUE);
2965 }
2966
2967#ifdef FEAT_AUTOCMD
2968 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2969 if (other_buffer)
2970 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2971#endif
2972
2973#ifdef FEAT_TITLE
2974 maketitle();
2975#endif
2976 curwin->w_redr_status = TRUE;
2977 if (restart_edit)
2978 redraw_later(VALID); /* causes status line redraw */
2979
2980 /* set window height to desired minimal value */
2981 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
2982 win_setheight((int)p_wh);
2983 else if (curwin->w_height == 0)
2984 win_setheight(1);
2985
2986#ifdef FEAT_VERTSPLIT
2987 /* set window width to desired minimal value */
2988 if (curwin->w_width < p_wiw)
2989 win_setwidth((int)p_wiw);
2990#endif
2991
2992#ifdef FEAT_MOUSE
2993 setmouse(); /* in case jumped to/from help buffer */
2994#endif
2995
2996#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
2997 /* Change directories when the acd option is set on and after
2998 * switching windows. */
2999 if (p_acd && curbuf->b_ffname != NULL
3000 && vim_chdirfile(curbuf->b_ffname) == OK)
3001 shorten_fnames(TRUE);
3002#endif
3003}
3004
3005#endif /* FEAT_WINDOWS */
3006
3007#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3008/*
3009 * Jump to the first open window that contains buffer buf if one exists
3010 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3011 * Returns pointer to window if it exists, otherwise NULL.
3012 */
3013 win_T *
3014buf_jump_open_win(buf)
3015 buf_T *buf;
3016{
3017# ifdef FEAT_WINDOWS
3018 win_T *wp;
3019
3020 for (wp = firstwin; wp; wp = wp->w_next)
3021 if (wp->w_buffer == buf)
3022 break;
3023 if (wp != NULL)
3024 win_enter(wp, FALSE);
3025 return wp;
3026# else
3027 if (curwin->w_buffer == buf)
3028 return curwin;
3029 return NULL;
3030# endif
3031}
3032#endif
3033
3034/*
3035 * allocate a window structure and link it in the window list
3036 */
3037/*ARGSUSED*/
3038 static win_T *
3039win_alloc(after)
3040 win_T *after;
3041{
3042 win_T *newwin;
3043
3044 /*
3045 * allocate window structure and linesizes arrays
3046 */
3047 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3048 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3049 {
3050 vim_free(newwin);
3051 newwin = NULL;
3052 }
3053
3054 if (newwin != NULL)
3055 {
3056 /*
3057 * link the window in the window list
3058 */
3059#ifdef FEAT_WINDOWS
3060 win_append(after, newwin);
3061#endif
3062#ifdef FEAT_VERTSPLIT
3063 newwin->w_wincol = 0;
3064 newwin->w_width = Columns;
3065#endif
3066
3067 /* position the display and the cursor at the top of the file. */
3068 newwin->w_topline = 1;
3069#ifdef FEAT_DIFF
3070 newwin->w_topfill = 0;
3071#endif
3072 newwin->w_botline = 2;
3073 newwin->w_cursor.lnum = 1;
3074#ifdef FEAT_SCROLLBIND
3075 newwin->w_scbind_pos = 1;
3076#endif
3077
3078 /* We won't calculate w_fraction until resizing the window */
3079 newwin->w_fraction = 0;
3080 newwin->w_prev_fraction_row = -1;
3081
3082#ifdef FEAT_GUI
3083 if (gui.in_use)
3084 {
3085 out_flush();
3086 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3087 SBAR_LEFT, newwin);
3088 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3089 SBAR_RIGHT, newwin);
3090 }
3091#endif
3092#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003093 /* init w: variables */
3094 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#endif
3096#ifdef FEAT_FOLDING
3097 foldInitWin(newwin);
3098#endif
3099 }
3100 return newwin;
3101}
3102
3103#if defined(FEAT_WINDOWS) || defined(PROTO)
3104
3105/*
3106 * remove window 'wp' from the window list and free the structure
3107 */
3108 static void
3109win_free(wp)
3110 win_T *wp;
3111{
3112 int i;
3113
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003114#ifdef FEAT_MZSCHEME
3115 mzscheme_window_free(wp);
3116#endif
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#ifdef FEAT_PERL
3119 perl_win_free(wp);
3120#endif
3121
3122#ifdef FEAT_PYTHON
3123 python_window_free(wp);
3124#endif
3125
3126#ifdef FEAT_TCL
3127 tcl_window_free(wp);
3128#endif
3129
3130#ifdef FEAT_RUBY
3131 ruby_window_free(wp);
3132#endif
3133
3134 clear_winopt(&wp->w_onebuf_opt);
3135 clear_winopt(&wp->w_allbuf_opt);
3136
3137#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003138 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
3140
3141 if (prevwin == wp)
3142 prevwin = NULL;
3143 win_free_lsize(wp);
3144
3145 for (i = 0; i < wp->w_tagstacklen; ++i)
3146 vim_free(wp->w_tagstack[i].tagname);
3147
3148 vim_free(wp->w_localdir);
3149#ifdef FEAT_SEARCH_EXTRA
3150 vim_free(wp->w_match.regprog);
3151#endif
3152#ifdef FEAT_JUMPLIST
3153 free_jumplist(wp);
3154#endif
3155
3156#ifdef FEAT_GUI
3157 if (gui.in_use)
3158 {
3159 out_flush();
3160 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3161 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3162 }
3163#endif /* FEAT_GUI */
3164
3165 win_remove(wp);
3166 vim_free(wp);
3167}
3168
3169/*
3170 * Append window "wp" in the window list after window "after".
3171 */
3172 static void
3173win_append(after, wp)
3174 win_T *after, *wp;
3175{
3176 win_T *before;
3177
3178 if (after == NULL) /* after NULL is in front of the first */
3179 before = firstwin;
3180 else
3181 before = after->w_next;
3182
3183 wp->w_next = before;
3184 wp->w_prev = after;
3185 if (after == NULL)
3186 firstwin = wp;
3187 else
3188 after->w_next = wp;
3189 if (before == NULL)
3190 lastwin = wp;
3191 else
3192 before->w_prev = wp;
3193}
3194
3195/*
3196 * Remove a window from the window list.
3197 */
3198 static void
3199win_remove(wp)
3200 win_T *wp;
3201{
3202 if (wp->w_prev != NULL)
3203 wp->w_prev->w_next = wp->w_next;
3204 else
3205 firstwin = wp->w_next;
3206 if (wp->w_next != NULL)
3207 wp->w_next->w_prev = wp->w_prev;
3208 else
3209 lastwin = wp->w_prev;
3210}
3211
3212/*
3213 * Append frame "frp" in a frame list after frame "after".
3214 */
3215 static void
3216frame_append(after, frp)
3217 frame_T *after, *frp;
3218{
3219 frp->fr_next = after->fr_next;
3220 after->fr_next = frp;
3221 if (frp->fr_next != NULL)
3222 frp->fr_next->fr_prev = frp;
3223 frp->fr_prev = after;
3224}
3225
3226/*
3227 * Insert frame "frp" in a frame list before frame "before".
3228 */
3229 static void
3230frame_insert(before, frp)
3231 frame_T *before, *frp;
3232{
3233 frp->fr_next = before;
3234 frp->fr_prev = before->fr_prev;
3235 before->fr_prev = frp;
3236 if (frp->fr_prev != NULL)
3237 frp->fr_prev->fr_next = frp;
3238 else
3239 frp->fr_parent->fr_child = frp;
3240}
3241
3242/*
3243 * Remove a frame from a frame list.
3244 */
3245 static void
3246frame_remove(frp)
3247 frame_T *frp;
3248{
3249 if (frp->fr_prev != NULL)
3250 frp->fr_prev->fr_next = frp->fr_next;
3251 else
3252 frp->fr_parent->fr_child = frp->fr_next;
3253 if (frp->fr_next != NULL)
3254 frp->fr_next->fr_prev = frp->fr_prev;
3255}
3256
3257#endif /* FEAT_WINDOWS */
3258
3259/*
3260 * Allocate w_lines[] for window "wp".
3261 * Return FAIL for failure, OK for success.
3262 */
3263 int
3264win_alloc_lines(wp)
3265 win_T *wp;
3266{
3267 wp->w_lines_valid = 0;
3268 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3269 if (wp->w_lines == NULL)
3270 return FAIL;
3271 return OK;
3272}
3273
3274/*
3275 * free lsize arrays for a window
3276 */
3277 void
3278win_free_lsize(wp)
3279 win_T *wp;
3280{
3281 vim_free(wp->w_lines);
3282 wp->w_lines = NULL;
3283}
3284
3285/*
3286 * Called from win_new_shellsize() after Rows changed.
3287 */
3288 void
3289shell_new_rows()
3290{
3291 int h = (int)(Rows - p_ch);
3292
3293 if (firstwin == NULL) /* not initialized yet */
3294 return;
3295#ifdef FEAT_WINDOWS
3296 if (h < frame_minheight(topframe, NULL))
3297 h = frame_minheight(topframe, NULL);
3298 /* First try setting the heights of windows without 'winfixheight'. If
3299 * that doesn't result in the right height, forget about that option. */
3300 frame_new_height(topframe, h, FALSE, TRUE);
3301 if (topframe->fr_height != h)
3302 frame_new_height(topframe, h, FALSE, FALSE);
3303
3304 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3305#else
3306 if (h < 1)
3307 h = 1;
3308 win_new_height(firstwin, h);
3309#endif
3310 compute_cmdrow();
3311#if 0
3312 /* Disabled: don't want making the screen smaller make a window larger. */
3313 if (p_ea)
3314 win_equal(curwin, FALSE, 'v');
3315#endif
3316}
3317
3318#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3319/*
3320 * Called from win_new_shellsize() after Columns changed.
3321 */
3322 void
3323shell_new_columns()
3324{
3325 if (firstwin == NULL) /* not initialized yet */
3326 return;
3327 frame_new_width(topframe, (int)Columns, FALSE);
3328 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
3329#if 0
3330 /* Disabled: don't want making the screen smaller make a window larger. */
3331 if (p_ea)
3332 win_equal(curwin, FALSE, 'h');
3333#endif
3334}
3335#endif
3336
3337#if defined(FEAT_CMDWIN) || defined(PROTO)
3338/*
3339 * Save the size of all windows in "gap".
3340 */
3341 void
3342win_size_save(gap)
3343 garray_T *gap;
3344
3345{
3346 win_T *wp;
3347
3348 ga_init2(gap, (int)sizeof(int), 1);
3349 if (ga_grow(gap, win_count() * 2) == OK)
3350 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3351 {
3352 ((int *)gap->ga_data)[gap->ga_len++] =
3353 wp->w_width + wp->w_vsep_width;
3354 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
3355 }
3356}
3357
3358/*
3359 * Restore window sizes, but only if the number of windows is still the same.
3360 * Does not free the growarray.
3361 */
3362 void
3363win_size_restore(gap)
3364 garray_T *gap;
3365{
3366 win_T *wp;
3367 int i;
3368
3369 if (win_count() * 2 == gap->ga_len)
3370 {
3371 i = 0;
3372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3373 {
3374 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
3375 win_setheight_win(((int *)gap->ga_data)[i++], wp);
3376 }
3377 /* recompute the window positions */
3378 (void)win_comp_pos();
3379 }
3380}
3381#endif /* FEAT_CMDWIN */
3382
3383#if defined(FEAT_WINDOWS) || defined(PROTO)
3384/*
3385 * Update the position for all windows, using the width and height of the
3386 * frames.
3387 * Returns the row just after the last window.
3388 */
3389 static int
3390win_comp_pos()
3391{
3392 int row = 0;
3393 int col = 0;
3394
3395 frame_comp_pos(topframe, &row, &col);
3396 return row;
3397}
3398
3399/*
3400 * Update the position of the windows in frame "topfrp", using the width and
3401 * height of the frames.
3402 * "*row" and "*col" are the top-left position of the frame. They are updated
3403 * to the bottom-right position plus one.
3404 */
3405 static void
3406frame_comp_pos(topfrp, row, col)
3407 frame_T *topfrp;
3408 int *row;
3409 int *col;
3410{
3411 win_T *wp;
3412 frame_T *frp;
3413#ifdef FEAT_VERTSPLIT
3414 int startcol;
3415 int startrow;
3416#endif
3417
3418 wp = topfrp->fr_win;
3419 if (wp != NULL)
3420 {
3421 if (wp->w_winrow != *row
3422#ifdef FEAT_VERTSPLIT
3423 || wp->w_wincol != *col
3424#endif
3425 )
3426 {
3427 /* position changed, redraw */
3428 wp->w_winrow = *row;
3429#ifdef FEAT_VERTSPLIT
3430 wp->w_wincol = *col;
3431#endif
3432 redraw_win_later(wp, NOT_VALID);
3433 wp->w_redr_status = TRUE;
3434 }
3435 *row += wp->w_height + wp->w_status_height;
3436#ifdef FEAT_VERTSPLIT
3437 *col += wp->w_width + wp->w_vsep_width;
3438#endif
3439 }
3440 else
3441 {
3442#ifdef FEAT_VERTSPLIT
3443 startrow = *row;
3444 startcol = *col;
3445#endif
3446 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3447 {
3448#ifdef FEAT_VERTSPLIT
3449 if (topfrp->fr_layout == FR_ROW)
3450 *row = startrow; /* all frames are at the same row */
3451 else
3452 *col = startcol; /* all frames are at the same col */
3453#endif
3454 frame_comp_pos(frp, row, col);
3455 }
3456 }
3457}
3458
3459#endif /* FEAT_WINDOWS */
3460
3461/*
3462 * Set current window height and take care of repositioning other windows to
3463 * fit around it.
3464 */
3465 void
3466win_setheight(height)
3467 int height;
3468{
3469 win_setheight_win(height, curwin);
3470}
3471
3472/*
3473 * Set the window height of window "win" and take care of repositioning other
3474 * windows to fit around it.
3475 */
3476 void
3477win_setheight_win(height, win)
3478 int height;
3479 win_T *win;
3480{
3481 int row;
3482
3483 if (win == curwin)
3484 {
3485 /* Always keep current window at least one line high, even when
3486 * 'winminheight' is zero. */
3487#ifdef FEAT_WINDOWS
3488 if (height < p_wmh)
3489 height = p_wmh;
3490#endif
3491 if (height == 0)
3492 height = 1;
3493 }
3494
3495#ifdef FEAT_WINDOWS
3496 frame_setheight(win->w_frame, height + win->w_status_height);
3497
3498 /* recompute the window positions */
3499 row = win_comp_pos();
3500#else
3501 if (height > topframe->fr_height)
3502 height = topframe->fr_height;
3503 win->w_height = height;
3504 row = height;
3505#endif
3506
3507 /*
3508 * If there is extra space created between the last window and the command
3509 * line, clear it.
3510 */
3511 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
3512 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
3513 cmdline_row = row;
3514 msg_row = row;
3515 msg_col = 0;
3516
3517 redraw_all_later(NOT_VALID);
3518}
3519
3520#if defined(FEAT_WINDOWS) || defined(PROTO)
3521
3522/*
3523 * Set the height of a frame to "height" and take care that all frames and
3524 * windows inside it are resized. Also resize frames on the left and right if
3525 * the are in the same FR_ROW frame.
3526 *
3527 * Strategy:
3528 * If the frame is part of a FR_COL frame, try fitting the frame in that
3529 * frame. If that doesn't work (the FR_COL frame is too small), recursively
3530 * go to containing frames to resize them and make room.
3531 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
3532 * Check for the minimal height of the FR_ROW frame.
3533 * At the top level we can also use change the command line height.
3534 */
3535 static void
3536frame_setheight(curfrp, height)
3537 frame_T *curfrp;
3538 int height;
3539{
3540 int room; /* total number of lines available */
3541 int take; /* number of lines taken from other windows */
3542 int room_cmdline; /* lines available from cmdline */
3543 int run;
3544 frame_T *frp;
3545 int h;
3546 int room_reserved;
3547
3548 /* If the height already is the desired value, nothing to do. */
3549 if (curfrp->fr_height == height)
3550 return;
3551
3552 if (curfrp->fr_parent == NULL)
3553 {
3554 /* topframe: can only change the command line */
3555 if (height > Rows - p_ch)
3556 height = Rows - p_ch;
3557 if (height > 0)
3558 frame_new_height(curfrp, height, FALSE, FALSE);
3559 }
3560 else if (curfrp->fr_parent->fr_layout == FR_ROW)
3561 {
3562 /* Row of frames: Also need to resize frames left and right of this
3563 * one. First check for the minimal height of these. */
3564 h = frame_minheight(curfrp->fr_parent, NULL);
3565 if (height < h)
3566 height = h;
3567 frame_setheight(curfrp->fr_parent, height);
3568 }
3569 else
3570 {
3571 /*
3572 * Column of frames: try to change only frames in this column.
3573 */
3574#ifdef FEAT_VERTSPLIT
3575 /*
3576 * Do this twice:
3577 * 1: compute room available, if it's not enough try resizing the
3578 * containing frame.
3579 * 2: compute the room available and adjust the height to it.
3580 * Try not to reduce the height of a window with 'winfixheight' set.
3581 */
3582 for (run = 1; run <= 2; ++run)
3583#else
3584 for (;;)
3585#endif
3586 {
3587 room = 0;
3588 room_reserved = 0;
3589 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3590 frp = frp->fr_next)
3591 {
3592 if (frp != curfrp
3593 && frp->fr_win != NULL
3594 && frp->fr_win->w_p_wfh)
3595 room_reserved += frp->fr_height;
3596 room += frp->fr_height;
3597 if (frp != curfrp)
3598 room -= frame_minheight(frp, NULL);
3599 }
3600#ifdef FEAT_VERTSPLIT
3601 if (curfrp->fr_width != Columns)
3602 room_cmdline = 0;
3603 else
3604#endif
3605 {
3606 room_cmdline = Rows - p_ch - (lastwin->w_winrow
3607 + lastwin->w_height + lastwin->w_status_height);
3608 if (room_cmdline < 0)
3609 room_cmdline = 0;
3610 }
3611
3612 if (height <= room + room_cmdline)
3613 break;
3614#ifdef FEAT_VERTSPLIT
3615 if (run == 2 || curfrp->fr_width == Columns)
3616#endif
3617 {
3618 if (height > room + room_cmdline)
3619 height = room + room_cmdline;
3620 break;
3621 }
3622#ifdef FEAT_VERTSPLIT
3623 frame_setheight(curfrp->fr_parent, height
3624 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
3625#endif
3626 /*NOTREACHED*/
3627 }
3628
3629 /*
3630 * Compute the number of lines we will take from others frames (can be
3631 * negative!).
3632 */
3633 take = height - curfrp->fr_height;
3634
3635 /* If there is not enough room, also reduce the height of a window
3636 * with 'winfixheight' set. */
3637 if (height > room + room_cmdline - room_reserved)
3638 room_reserved = room + room_cmdline - height;
3639 /* If there is only a 'winfixheight' window and making the
3640 * window smaller, need to make the other window taller. */
3641 if (take < 0 && room - curfrp->fr_height < room_reserved)
3642 room_reserved = 0;
3643
3644 if (take > 0 && room_cmdline > 0)
3645 {
3646 /* use lines from cmdline first */
3647 if (take < room_cmdline)
3648 room_cmdline = take;
3649 take -= room_cmdline;
3650 topframe->fr_height += room_cmdline;
3651 }
3652
3653 /*
3654 * set the current frame to the new height
3655 */
3656 frame_new_height(curfrp, height, FALSE, FALSE);
3657
3658 /*
3659 * First take lines from the frames after the current frame. If
3660 * that is not enough, takes lines from frames above the current
3661 * frame.
3662 */
3663 for (run = 0; run < 2; ++run)
3664 {
3665 if (run == 0)
3666 frp = curfrp->fr_next; /* 1st run: start with next window */
3667 else
3668 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3669 while (frp != NULL && take != 0)
3670 {
3671 h = frame_minheight(frp, NULL);
3672 if (room_reserved > 0
3673 && frp->fr_win != NULL
3674 && frp->fr_win->w_p_wfh)
3675 {
3676 if (room_reserved >= frp->fr_height)
3677 room_reserved -= frp->fr_height;
3678 else
3679 {
3680 if (frp->fr_height - room_reserved > take)
3681 room_reserved = frp->fr_height - take;
3682 take -= frp->fr_height - room_reserved;
3683 frame_new_height(frp, room_reserved, FALSE, FALSE);
3684 room_reserved = 0;
3685 }
3686 }
3687 else
3688 {
3689 if (frp->fr_height - take < h)
3690 {
3691 take -= frp->fr_height - h;
3692 frame_new_height(frp, h, FALSE, FALSE);
3693 }
3694 else
3695 {
3696 frame_new_height(frp, frp->fr_height - take,
3697 FALSE, FALSE);
3698 take = 0;
3699 }
3700 }
3701 if (run == 0)
3702 frp = frp->fr_next;
3703 else
3704 frp = frp->fr_prev;
3705 }
3706 }
3707 }
3708}
3709
3710#if defined(FEAT_VERTSPLIT) || defined(PROTO)
3711/*
3712 * Set current window width and take care of repositioning other windows to
3713 * fit around it.
3714 */
3715 void
3716win_setwidth(width)
3717 int width;
3718{
3719 win_setwidth_win(width, curwin);
3720}
3721
3722 void
3723win_setwidth_win(width, wp)
3724 int width;
3725 win_T *wp;
3726{
3727 /* Always keep current window at least one column wide, even when
3728 * 'winminwidth' is zero. */
3729 if (wp == curwin)
3730 {
3731 if (width < p_wmw)
3732 width = p_wmw;
3733 if (width == 0)
3734 width = 1;
3735 }
3736
3737 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
3738
3739 /* recompute the window positions */
3740 (void)win_comp_pos();
3741
3742 redraw_all_later(NOT_VALID);
3743}
3744
3745/*
3746 * Set the width of a frame to "width" and take care that all frames and
3747 * windows inside it are resized. Also resize frames above and below if the
3748 * are in the same FR_ROW frame.
3749 *
3750 * Strategy is similar to frame_setheight().
3751 */
3752 static void
3753frame_setwidth(curfrp, width)
3754 frame_T *curfrp;
3755 int width;
3756{
3757 int room; /* total number of lines available */
3758 int take; /* number of lines taken from other windows */
3759 int run;
3760 frame_T *frp;
3761 int w;
3762
3763 /* If the width already is the desired value, nothing to do. */
3764 if (curfrp->fr_width == width)
3765 return;
3766
3767 if (curfrp->fr_parent == NULL)
3768 /* topframe: can't change width */
3769 return;
3770
3771 if (curfrp->fr_parent->fr_layout == FR_COL)
3772 {
3773 /* Column of frames: Also need to resize frames above and below of
3774 * this one. First check for the minimal width of these. */
3775 w = frame_minwidth(curfrp->fr_parent, NULL);
3776 if (width < w)
3777 width = w;
3778 frame_setwidth(curfrp->fr_parent, width);
3779 }
3780 else
3781 {
3782 /*
3783 * Row of frames: try to change only frames in this row.
3784 *
3785 * Do this twice:
3786 * 1: compute room available, if it's not enough try resizing the
3787 * containing frame.
3788 * 2: compute the room available and adjust the width to it.
3789 */
3790 for (run = 1; run <= 2; ++run)
3791 {
3792 room = 0;
3793 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
3794 frp = frp->fr_next)
3795 {
3796 room += frp->fr_width;
3797 if (frp != curfrp)
3798 room -= frame_minwidth(frp, NULL);
3799 }
3800
3801 if (width <= room)
3802 break;
3803 if (run == 2 || curfrp->fr_height >= Rows - p_ch)
3804 {
3805 if (width > room)
3806 width = room;
3807 break;
3808 }
3809 frame_setwidth(curfrp->fr_parent, width
3810 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
3811 }
3812
3813
3814 /*
3815 * Compute the number of lines we will take from others frames (can be
3816 * negative!).
3817 */
3818 take = width - curfrp->fr_width;
3819
3820 /*
3821 * set the current frame to the new width
3822 */
3823 frame_new_width(curfrp, width, FALSE);
3824
3825 /*
3826 * First take lines from the frames right of the current frame. If
3827 * that is not enough, takes lines from frames left of the current
3828 * frame.
3829 */
3830 for (run = 0; run < 2; ++run)
3831 {
3832 if (run == 0)
3833 frp = curfrp->fr_next; /* 1st run: start with next window */
3834 else
3835 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
3836 while (frp != NULL && take != 0)
3837 {
3838 w = frame_minwidth(frp, NULL);
3839 if (frp->fr_width - take < w)
3840 {
3841 take -= frp->fr_width - w;
3842 frame_new_width(frp, w, FALSE);
3843 }
3844 else
3845 {
3846 frame_new_width(frp, frp->fr_width - take, FALSE);
3847 take = 0;
3848 }
3849 if (run == 0)
3850 frp = frp->fr_next;
3851 else
3852 frp = frp->fr_prev;
3853 }
3854 }
3855 }
3856}
3857#endif /* FEAT_VERTSPLIT */
3858
3859/*
3860 * Check 'winminheight' for a valid value.
3861 */
3862 void
3863win_setminheight()
3864{
3865 int room;
3866 int first = TRUE;
3867 win_T *wp;
3868
3869 /* loop until there is a 'winminheight' that is possible */
3870 while (p_wmh > 0)
3871 {
3872 /* TODO: handle vertical splits */
3873 room = -p_wh;
3874 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3875 room += wp->w_height - p_wmh;
3876 if (room >= 0)
3877 break;
3878 --p_wmh;
3879 if (first)
3880 {
3881 EMSG(_(e_noroom));
3882 first = FALSE;
3883 }
3884 }
3885}
3886
3887#ifdef FEAT_MOUSE
3888
3889/*
3890 * Status line of dragwin is dragged "offset" lines down (negative is up).
3891 */
3892 void
3893win_drag_status_line(dragwin, offset)
3894 win_T *dragwin;
3895 int offset;
3896{
3897 frame_T *curfr;
3898 frame_T *fr;
3899 int room;
3900 int row;
3901 int up; /* if TRUE, drag status line up, otherwise down */
3902 int n;
3903
3904 fr = dragwin->w_frame;
3905 curfr = fr;
3906 if (fr != topframe) /* more than one window */
3907 {
3908 fr = fr->fr_parent;
3909 /* When the parent frame is not a column of frames, its parent should
3910 * be. */
3911 if (fr->fr_layout != FR_COL)
3912 {
3913 curfr = fr;
3914 if (fr != topframe) /* only a row of windows, may drag statusline */
3915 fr = fr->fr_parent;
3916 }
3917 }
3918
3919 /* If this is the last frame in a column, may want to resize the parent
3920 * frame instead (go two up to skip a row of frames). */
3921 while (curfr != topframe && curfr->fr_next == NULL)
3922 {
3923 if (fr != topframe)
3924 fr = fr->fr_parent;
3925 curfr = fr;
3926 if (fr != topframe)
3927 fr = fr->fr_parent;
3928 }
3929
3930 if (offset < 0) /* drag up */
3931 {
3932 up = TRUE;
3933 offset = -offset;
3934 /* sum up the room of the current frame and above it */
3935 if (fr == curfr)
3936 {
3937 /* only one window */
3938 room = fr->fr_height - frame_minheight(fr, NULL);
3939 }
3940 else
3941 {
3942 room = 0;
3943 for (fr = fr->fr_child; ; fr = fr->fr_next)
3944 {
3945 room += fr->fr_height - frame_minheight(fr, NULL);
3946 if (fr == curfr)
3947 break;
3948 }
3949 }
3950 fr = curfr->fr_next; /* put fr at frame that grows */
3951 }
3952 else /* drag down */
3953 {
3954 up = FALSE;
3955 /*
3956 * Only dragging the last status line can reduce p_ch.
3957 */
3958 room = Rows - cmdline_row;
3959 if (curfr->fr_next == NULL)
3960 room -= 1;
3961 else
3962 room -= p_ch;
3963 if (room < 0)
3964 room = 0;
3965 /* sum up the room of frames below of the current one */
3966 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
3967 room += fr->fr_height - frame_minheight(fr, NULL);
3968 fr = curfr; /* put fr at window that grows */
3969 }
3970
3971 if (room < offset) /* Not enough room */
3972 offset = room; /* Move as far as we can */
3973 if (offset <= 0)
3974 return;
3975
3976 /*
3977 * Grow frame fr by "offset" lines.
3978 * Doesn't happen when dragging the last status line up.
3979 */
3980 if (fr != NULL)
3981 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
3982
3983 if (up)
3984 fr = curfr; /* current frame gets smaller */
3985 else
3986 fr = curfr->fr_next; /* next frame gets smaller */
3987
3988 /*
3989 * Now make the other frames smaller.
3990 */
3991 while (fr != NULL && offset > 0)
3992 {
3993 n = frame_minheight(fr, NULL);
3994 if (fr->fr_height - offset <= n)
3995 {
3996 offset -= fr->fr_height - n;
3997 frame_new_height(fr, n, !up, FALSE);
3998 }
3999 else
4000 {
4001 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4002 break;
4003 }
4004 if (up)
4005 fr = fr->fr_prev;
4006 else
4007 fr = fr->fr_next;
4008 }
4009 row = win_comp_pos();
4010 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4011 cmdline_row = row;
4012 p_ch = Rows - cmdline_row;
4013 if (p_ch < 1)
4014 p_ch = 1;
4015 redraw_all_later(NOT_VALID);
4016 showmode();
4017}
4018
4019#ifdef FEAT_VERTSPLIT
4020/*
4021 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4022 */
4023 void
4024win_drag_vsep_line(dragwin, offset)
4025 win_T *dragwin;
4026 int offset;
4027{
4028 frame_T *curfr;
4029 frame_T *fr;
4030 int room;
4031 int left; /* if TRUE, drag separator line left, otherwise right */
4032 int n;
4033
4034 fr = dragwin->w_frame;
4035 if (fr == topframe) /* only one window (cannot happe?) */
4036 return;
4037 curfr = fr;
4038 fr = fr->fr_parent;
4039 /* When the parent frame is not a row of frames, its parent should be. */
4040 if (fr->fr_layout != FR_ROW)
4041 {
4042 if (fr == topframe) /* only a column of windows (cannot happen?) */
4043 return;
4044 curfr = fr;
4045 fr = fr->fr_parent;
4046 }
4047
4048 /* If this is the last frame in a row, may want to resize a parent
4049 * frame instead. */
4050 while (curfr->fr_next == NULL)
4051 {
4052 if (fr == topframe)
4053 break;
4054 curfr = fr;
4055 fr = fr->fr_parent;
4056 if (fr != topframe)
4057 {
4058 curfr = fr;
4059 fr = fr->fr_parent;
4060 }
4061 }
4062
4063 if (offset < 0) /* drag left */
4064 {
4065 left = TRUE;
4066 offset = -offset;
4067 /* sum up the room of the current frame and left of it */
4068 room = 0;
4069 for (fr = fr->fr_child; ; fr = fr->fr_next)
4070 {
4071 room += fr->fr_width - frame_minwidth(fr, NULL);
4072 if (fr == curfr)
4073 break;
4074 }
4075 fr = curfr->fr_next; /* put fr at frame that grows */
4076 }
4077 else /* drag right */
4078 {
4079 left = FALSE;
4080 /* sum up the room of frames right of the current one */
4081 room = 0;
4082 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4083 room += fr->fr_width - frame_minwidth(fr, NULL);
4084 fr = curfr; /* put fr at window that grows */
4085 }
4086
4087 if (room < offset) /* Not enough room */
4088 offset = room; /* Move as far as we can */
4089 if (offset <= 0) /* No room at all, quit. */
4090 return;
4091
4092 /* grow frame fr by offset lines */
4093 frame_new_width(fr, fr->fr_width + offset, left);
4094
4095 /* shrink other frames: current and at the left or at the right */
4096 if (left)
4097 fr = curfr; /* current frame gets smaller */
4098 else
4099 fr = curfr->fr_next; /* next frame gets smaller */
4100
4101 while (fr != NULL && offset > 0)
4102 {
4103 n = frame_minwidth(fr, NULL);
4104 if (fr->fr_width - offset <= n)
4105 {
4106 offset -= fr->fr_width - n;
4107 frame_new_width(fr, n, !left);
4108 }
4109 else
4110 {
4111 frame_new_width(fr, fr->fr_width - offset, !left);
4112 break;
4113 }
4114 if (left)
4115 fr = fr->fr_prev;
4116 else
4117 fr = fr->fr_next;
4118 }
4119 (void)win_comp_pos();
4120 redraw_all_later(NOT_VALID);
4121}
4122#endif /* FEAT_VERTSPLIT */
4123#endif /* FEAT_MOUSE */
4124
4125#endif /* FEAT_WINDOWS */
4126
4127/*
4128 * Set the height of a window.
4129 * This takes care of the things inside the window, not what happens to the
4130 * window position, the frame or to other windows.
4131 */
4132 static void
4133win_new_height(wp, height)
4134 win_T *wp;
4135 int height;
4136{
4137 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004138 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004140 int space;
4141 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#define FRACTION_MULT 16384L
4143
4144 /* Don't want a negative height. Happens when splitting a tiny window.
4145 * Will equalize heights soon to fix it. */
4146 if (height < 0)
4147 height = 0;
4148
4149 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4150 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4151 + FRACTION_MULT / 2) / (long)wp->w_height;
4152
4153 wp->w_height = height;
4154 wp->w_skipcol = 0;
4155
4156 /* Don't change w_topline when height is zero. Don't set w_topline when
4157 * 'scrollbind' is set and this isn't the current window. */
4158 if (height > 0
4159#ifdef FEAT_SCROLLBIND
4160 && (!wp->w_p_scb || wp == curwin)
4161#endif
4162 )
4163 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004164 /*
4165 * Find a value for w_topline that shows the cursor at the same
4166 * relative position in the window as before (more or less).
4167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 lnum = wp->w_cursor.lnum;
4169 if (lnum < 1) /* can happen when starting up */
4170 lnum = 1;
4171 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4172 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4173 sline = wp->w_wrow - line_size;
4174 if (sline < 0)
4175 {
4176 /*
4177 * Cursor line would go off top of screen if w_wrow was this high.
4178 */
4179 wp->w_wrow = line_size;
4180 }
4181 else
4182 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004183 space = height;
4184 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004186 space -= line_size;
4187 if (space > 0 && sline <= 0 && !did_below)
4188 {
4189 /* Try to use "~" lines below the text to avoid that text
4190 * is above the window while there are empty lines.
4191 * Subtract the rows below the cursor from "space" and
4192 * give the rest to "sline". */
4193 did_below = TRUE;
4194 bot = wp->w_cursor.lnum;
4195 while (space > 0)
4196 {
4197 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4198 space = 0;
4199 else
4200 {
4201#ifdef FEAT_FOLDING
4202 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4203#endif
4204 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4205 break;
4206 ++bot;
4207 space -= plines_win(wp, bot, TRUE);
4208 }
4209 }
4210 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4211 sline += space;
4212 }
4213 if (sline <= 0)
4214 break;
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#ifdef FEAT_FOLDING
4217 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4218 if (lnum == 1)
4219 {
4220 /* first line in buffer is folded */
4221 line_size = 1;
4222 --sline;
4223 break;
4224 }
4225#endif
4226 --lnum;
4227#ifdef FEAT_DIFF
4228 if (lnum == wp->w_topline)
4229 line_size = plines_win_nofill(wp, lnum, TRUE)
4230 + wp->w_topfill;
4231 else
4232#endif
4233 line_size = plines_win(wp, lnum, TRUE);
4234 sline -= line_size;
4235 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 if (sline < 0)
4238 {
4239 /*
4240 * Line we want at top would go off top of screen. Use next
4241 * line instead.
4242 */
4243#ifdef FEAT_FOLDING
4244 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4245#endif
4246 lnum++;
4247 wp->w_wrow -= line_size + sline;
4248 }
4249 else if (sline > 0)
4250 {
4251 /* First line of file reached, use that as topline. */
4252 lnum = 1;
4253 wp->w_wrow -= sline;
4254 }
4255 }
4256 set_topline(wp, lnum);
4257 }
4258
4259 if (wp == curwin)
4260 {
4261 if (p_so)
4262 update_topline();
4263 curs_columns(FALSE); /* validate w_wrow */
4264 }
4265 wp->w_prev_fraction_row = wp->w_wrow;
4266
4267 win_comp_scroll(wp);
4268 redraw_win_later(wp, NOT_VALID);
4269#ifdef FEAT_WINDOWS
4270 wp->w_redr_status = TRUE;
4271#endif
4272 invalidate_botline_win(wp);
4273}
4274
4275#ifdef FEAT_VERTSPLIT
4276/*
4277 * Set the width of a window.
4278 */
4279 static void
4280win_new_width(wp, width)
4281 win_T *wp;
4282 int width;
4283{
4284 wp->w_width = width;
4285 wp->w_lines_valid = 0;
4286 changed_line_abv_curs_win(wp);
4287 invalidate_botline_win(wp);
4288 if (wp == curwin)
4289 {
4290 update_topline();
4291 curs_columns(TRUE); /* validate w_wrow */
4292 }
4293 redraw_win_later(wp, NOT_VALID);
4294 wp->w_redr_status = TRUE;
4295}
4296#endif
4297
4298 void
4299win_comp_scroll(wp)
4300 win_T *wp;
4301{
4302 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
4303 if (wp->w_p_scr == 0)
4304 wp->w_p_scr = 1;
4305}
4306
4307/*
4308 * command_height: called whenever p_ch has been changed
4309 */
4310 void
4311command_height(old_p_ch)
4312 long old_p_ch;
4313{
4314#ifdef FEAT_WINDOWS
4315 int h;
4316 frame_T *frp;
4317
4318 /* Find bottom frame with width of screen. */
4319 frp = lastwin->w_frame;
4320# ifdef FEAT_VERTSPLIT
4321 while (frp->fr_width != Columns && frp->fr_parent != NULL)
4322 frp = frp->fr_parent;
4323# endif
4324
4325 /* Avoid changing the height of a window with 'winfixheight' set. */
4326 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
4327 && frp->fr_win->w_p_wfh)
4328 frp = frp->fr_prev;
4329
4330 if (starting != NO_SCREEN)
4331 {
4332 cmdline_row = Rows - p_ch;
4333
4334 if (p_ch > old_p_ch) /* p_ch got bigger */
4335 {
4336 while (p_ch > old_p_ch)
4337 {
4338 if (frp == NULL)
4339 {
4340 EMSG(_(e_noroom));
4341 p_ch = old_p_ch;
4342 cmdline_row = Rows - p_ch;
4343 break;
4344 }
4345 h = frp->fr_height - frame_minheight(frp, NULL);
4346 if (h > p_ch - old_p_ch)
4347 h = p_ch - old_p_ch;
4348 old_p_ch += h;
4349 frame_add_height(frp, -h);
4350 frp = frp->fr_prev;
4351 }
4352
4353 /* Recompute window positions. */
4354 (void)win_comp_pos();
4355
4356 /* clear the lines added to cmdline */
4357 if (full_screen)
4358 screen_fill((int)(cmdline_row), (int)Rows, 0,
4359 (int)Columns, ' ', ' ', 0);
4360 msg_row = cmdline_row;
4361 redraw_cmdline = TRUE;
4362 return;
4363 }
4364
4365 if (msg_row < cmdline_row)
4366 msg_row = cmdline_row;
4367 redraw_cmdline = TRUE;
4368 }
4369 frame_add_height(frp, (int)(old_p_ch - p_ch));
4370
4371 /* Recompute window positions. */
4372 if (frp != lastwin->w_frame)
4373 (void)win_comp_pos();
4374#else
4375 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
4376 cmdline_row = Rows - p_ch;
4377#endif
4378}
4379
4380#if defined(FEAT_WINDOWS) || defined(PROTO)
4381/*
4382 * Resize frame "frp" to be "n" lines higher (negative for less high).
4383 * Also resize the frames it is contained in.
4384 */
4385 static void
4386frame_add_height(frp, n)
4387 frame_T *frp;
4388 int n;
4389{
4390 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
4391 for (;;)
4392 {
4393 frp = frp->fr_parent;
4394 if (frp == NULL)
4395 break;
4396 frp->fr_height += n;
4397 }
4398}
4399
4400/*
4401 * Add or remove a status line for the bottom window(s), according to the
4402 * value of 'laststatus'.
4403 */
4404 void
4405last_status(morewin)
4406 int morewin; /* pretend there are two or more windows */
4407{
4408 /* Don't make a difference between horizontal or vertical split. */
4409 last_status_rec(topframe, (p_ls == 2
4410 || (p_ls == 1 && (morewin || lastwin != firstwin))));
4411}
4412
4413 static void
4414last_status_rec(fr, statusline)
4415 frame_T *fr;
4416 int statusline;
4417{
4418 frame_T *fp;
4419 win_T *wp;
4420
4421 if (fr->fr_layout == FR_LEAF)
4422 {
4423 wp = fr->fr_win;
4424 if (wp->w_status_height != 0 && !statusline)
4425 {
4426 /* remove status line */
4427 win_new_height(wp, wp->w_height + 1);
4428 wp->w_status_height = 0;
4429 comp_col();
4430 }
4431 else if (wp->w_status_height == 0 && statusline)
4432 {
4433 /* Find a frame to take a line from. */
4434 fp = fr;
4435 while (fp->fr_height <= frame_minheight(fp, NULL))
4436 {
4437 if (fp == topframe)
4438 {
4439 EMSG(_(e_noroom));
4440 return;
4441 }
4442 /* In a column of frames: go to frame above. If already at
4443 * the top or in a row of frames: go to parent. */
4444 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
4445 fp = fp->fr_prev;
4446 else
4447 fp = fp->fr_parent;
4448 }
4449 wp->w_status_height = 1;
4450 if (fp != fr)
4451 {
4452 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
4453 frame_fix_height(wp);
4454 (void)win_comp_pos();
4455 }
4456 else
4457 win_new_height(wp, wp->w_height - 1);
4458 comp_col();
4459 redraw_all_later(NOT_VALID);
4460 }
4461 }
4462#ifdef FEAT_VERTSPLIT
4463 else if (fr->fr_layout == FR_ROW)
4464 {
4465 /* vertically split windows, set status line for each one */
4466 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
4467 last_status_rec(fp, statusline);
4468 }
4469#endif
4470 else
4471 {
4472 /* horizontally split window, set status line for last one */
4473 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
4474 ;
4475 last_status_rec(fp, statusline);
4476 }
4477}
4478
4479#endif /* FEAT_WINDOWS */
4480
4481#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4482/*
4483 * Return the file name under or after the cursor.
4484 *
4485 * The 'path' option is searched if the file name is not absolute.
4486 * The string returned has been alloc'ed and should be freed by the caller.
4487 * NULL is returned if the file name or file is not found.
4488 *
4489 * options:
4490 * FNAME_MESS give error messages
4491 * FNAME_EXP expand to path
4492 * FNAME_HYP check for hypertext link
4493 * FNAME_INCL apply "includeexpr"
4494 */
4495 char_u *
4496file_name_at_cursor(options, count)
4497 int options;
4498 long count;
4499{
4500 return file_name_in_line(ml_get_curline(),
4501 curwin->w_cursor.col, options, count, curbuf->b_ffname);
4502}
4503
4504/*
4505 * Return the name of the file under or after ptr[col].
4506 * Otherwise like file_name_at_cursor().
4507 */
4508 char_u *
4509file_name_in_line(line, col, options, count, rel_fname)
4510 char_u *line;
4511 int col;
4512 int options;
4513 long count;
4514 char_u *rel_fname; /* file we are searching relative to */
4515{
4516 char_u *ptr;
4517 int len;
4518
4519 /*
4520 * search forward for what could be the start of a file name
4521 */
4522 ptr = line + col;
4523 while (*ptr != NUL && !vim_isfilec(*ptr))
4524 ++ptr;
4525 if (*ptr == NUL) /* nothing found */
4526 {
4527 if (options & FNAME_MESS)
4528 EMSG(_("E446: No file name under cursor"));
4529 return NULL;
4530 }
4531
4532 /*
4533 * Search backward for first char of the file name.
4534 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
4535 */
4536 while (ptr > line)
4537 {
4538#ifdef FEAT_MBYTE
4539 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
4540 ptr -= len + 1;
4541 else
4542#endif
4543 if (vim_isfilec(ptr[-1])
4544 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
4545 --ptr;
4546 else
4547 break;
4548 }
4549
4550 /*
4551 * Search forward for the last char of the file name.
4552 * Also allow "://" when ':' is not in 'isfname'.
4553 */
4554 len = 0;
4555 while (vim_isfilec(ptr[len])
4556 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
4557#ifdef FEAT_MBYTE
4558 if (has_mbyte)
4559 len += (*mb_ptr2len_check)(ptr + len);
4560 else
4561#endif
4562 ++len;
4563
4564 /*
4565 * If there is trailing punctuation, remove it.
4566 * But don't remove "..", could be a directory name.
4567 */
4568 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
4569 && ptr[len - 2] != '.')
4570 --len;
4571
4572 return find_file_name_in_path(ptr, len, options, count, rel_fname);
4573}
4574
4575# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4576static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
4577
4578 static char_u *
4579eval_includeexpr(ptr, len)
4580 char_u *ptr;
4581 int len;
4582{
4583 char_u *res;
4584
4585 set_vim_var_string(VV_FNAME, ptr, len);
4586 res = eval_to_string_safe(curbuf->b_p_inex, NULL);
4587 set_vim_var_string(VV_FNAME, NULL, 0);
4588 return res;
4589}
4590#endif
4591
4592/*
4593 * Return the name of the file ptr[len] in 'path'.
4594 * Otherwise like file_name_at_cursor().
4595 */
4596 char_u *
4597find_file_name_in_path(ptr, len, options, count, rel_fname)
4598 char_u *ptr;
4599 int len;
4600 int options;
4601 long count;
4602 char_u *rel_fname; /* file we are searching relative to */
4603{
4604 char_u *file_name;
4605 int c;
4606# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4607 char_u *tofree = NULL;
4608
4609 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4610 {
4611 tofree = eval_includeexpr(ptr, len);
4612 if (tofree != NULL)
4613 {
4614 ptr = tofree;
4615 len = (int)STRLEN(ptr);
4616 }
4617 }
4618# endif
4619
4620 if (options & FNAME_EXP)
4621 {
4622 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4623 TRUE, rel_fname);
4624
4625# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4626 /*
4627 * If the file could not be found in a normal way, try applying
4628 * 'includeexpr' (unless done already).
4629 */
4630 if (file_name == NULL
4631 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
4632 {
4633 tofree = eval_includeexpr(ptr, len);
4634 if (tofree != NULL)
4635 {
4636 ptr = tofree;
4637 len = (int)STRLEN(ptr);
4638 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
4639 TRUE, rel_fname);
4640 }
4641 }
4642# endif
4643 if (file_name == NULL && (options & FNAME_MESS))
4644 {
4645 c = ptr[len];
4646 ptr[len] = NUL;
4647 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
4648 ptr[len] = c;
4649 }
4650
4651 /* Repeat finding the file "count" times. This matters when it
4652 * appears several times in the path. */
4653 while (file_name != NULL && --count > 0)
4654 {
4655 vim_free(file_name);
4656 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
4657 }
4658 }
4659 else
4660 file_name = vim_strnsave(ptr, len);
4661
4662# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
4663 vim_free(tofree);
4664# endif
4665
4666 return file_name;
4667}
4668#endif /* FEAT_SEARCHPATH */
4669
4670/*
4671 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
4672 * Also check for ":\\", which MS Internet Explorer accepts, return
4673 * URL_BACKSLASH.
4674 */
4675 static int
4676path_is_url(p)
4677 char_u *p;
4678{
4679 if (STRNCMP(p, "://", (size_t)3) == 0)
4680 return URL_SLASH;
4681 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
4682 return URL_BACKSLASH;
4683 return 0;
4684}
4685
4686/*
4687 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
4688 * Return URL_BACKSLASH for "name:\\".
4689 * Return zero otherwise.
4690 */
4691 int
4692path_with_url(fname)
4693 char_u *fname;
4694{
4695 char_u *p;
4696
4697 for (p = fname; isalpha(*p); ++p)
4698 ;
4699 return path_is_url(p);
4700}
4701
4702/*
4703 * Return TRUE if "name" is a full (absolute) path name or URL.
4704 */
4705 int
4706vim_isAbsName(name)
4707 char_u *name;
4708{
4709 return (path_with_url(name) != 0 || mch_isFullName(name));
4710}
4711
4712/*
4713 * Get absolute file name into buffer 'buf' of length 'len' bytes.
4714 *
4715 * return FAIL for failure, OK otherwise
4716 */
4717 int
4718vim_FullName(fname, buf, len, force)
4719 char_u *fname, *buf;
4720 int len;
4721 int force;
4722{
4723 int retval = OK;
4724 int url;
4725
4726 *buf = NUL;
4727 if (fname == NULL)
4728 return FAIL;
4729
4730 url = path_with_url(fname);
4731 if (!url)
4732 retval = mch_FullName(fname, buf, len, force);
4733 if (url || retval == FAIL)
4734 {
4735 /* something failed; use the file name (truncate when too long) */
4736 STRNCPY(buf, fname, len);
4737 buf[len - 1] = NUL;
4738 }
4739#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
4740 slash_adjust(buf);
4741#endif
4742 return retval;
4743}
4744
4745/*
4746 * Return the minimal number of rows that is needed on the screen to display
4747 * the current number of windows.
4748 */
4749 int
4750min_rows()
4751{
4752 int total;
4753
4754 if (firstwin == NULL) /* not initialized yet */
4755 return MIN_LINES;
4756
4757 total = 1; /* count the room for the command line */
4758#ifdef FEAT_WINDOWS
4759 total += frame_minheight(topframe, NULL);
4760#else
4761 total += 1; /* at least one window should have a line! */
4762#endif
4763 return total;
4764}
4765
4766/*
4767 * Return TRUE if there is only one window, not counting a help or preview
4768 * window, unless it is the current window.
4769 */
4770 int
4771only_one_window()
4772{
4773#ifdef FEAT_WINDOWS
4774 int count = 0;
4775 win_T *wp;
4776
4777 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4778 if (!(wp->w_buffer->b_help
4779# ifdef FEAT_QUICKFIX
4780 || wp->w_p_pvw
4781# endif
4782 ) || wp == curwin)
4783 ++count;
4784 return (count <= 1);
4785#else
4786 return TRUE;
4787#endif
4788}
4789
4790#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
4791/*
4792 * Correct the cursor line number in other windows. Used after changing the
4793 * current buffer, and before applying autocommands.
4794 * When "do_curwin" is TRUE, also check current window.
4795 */
4796 void
4797check_lnums(do_curwin)
4798 int do_curwin;
4799{
4800 win_T *wp;
4801
4802#ifdef FEAT_WINDOWS
4803 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4804 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
4805#else
4806 wp = curwin;
4807 if (do_curwin)
4808#endif
4809 {
4810 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4811 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4812 if (wp->w_topline > curbuf->b_ml.ml_line_count)
4813 wp->w_topline = curbuf->b_ml.ml_line_count;
4814 }
4815}
4816#endif
4817
4818#if defined(FEAT_WINDOWS) || defined(PROTO)
4819
4820/*
4821 * A snapshot of the window sizes, to restore them after closing the help
4822 * window.
4823 * Only these fields are used:
4824 * fr_layout
4825 * fr_width
4826 * fr_height
4827 * fr_next
4828 * fr_child
4829 * fr_win (only valid for the old curwin, NULL otherwise)
4830 */
4831static frame_T *snapshot = NULL;
4832
4833/*
4834 * Create a snapshot of the current frame sizes.
4835 */
4836 static void
4837make_snapshot()
4838{
4839 clear_snapshot();
4840 make_snapshot_rec(topframe, &snapshot);
4841}
4842
4843 static void
4844make_snapshot_rec(fr, frp)
4845 frame_T *fr;
4846 frame_T **frp;
4847{
4848 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
4849 if (*frp == NULL)
4850 return;
4851 (*frp)->fr_layout = fr->fr_layout;
4852# ifdef FEAT_VERTSPLIT
4853 (*frp)->fr_width = fr->fr_width;
4854# endif
4855 (*frp)->fr_height = fr->fr_height;
4856 if (fr->fr_next != NULL)
4857 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
4858 if (fr->fr_child != NULL)
4859 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
4860 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
4861 (*frp)->fr_win = curwin;
4862}
4863
4864/*
4865 * Remove any existing snapshot.
4866 */
4867 static void
4868clear_snapshot()
4869{
4870 clear_snapshot_rec(snapshot);
4871 snapshot = NULL;
4872}
4873
4874 static void
4875clear_snapshot_rec(fr)
4876 frame_T *fr;
4877{
4878 if (fr != NULL)
4879 {
4880 clear_snapshot_rec(fr->fr_next);
4881 clear_snapshot_rec(fr->fr_child);
4882 vim_free(fr);
4883 }
4884}
4885
4886/*
4887 * Restore a previously created snapshot, if there is any.
4888 * This is only done if the screen size didn't change and the window layout is
4889 * still the same.
4890 */
4891 static void
4892restore_snapshot(close_curwin)
4893 int close_curwin; /* closing current window */
4894{
4895 win_T *wp;
4896
4897 if (snapshot != NULL
4898# ifdef FEAT_VERTSPLIT
4899 && snapshot->fr_width == topframe->fr_width
4900# endif
4901 && snapshot->fr_height == topframe->fr_height
4902 && check_snapshot_rec(snapshot, topframe) == OK)
4903 {
4904 wp = restore_snapshot_rec(snapshot, topframe);
4905 win_comp_pos();
4906 if (wp != NULL && close_curwin)
4907 win_goto(wp);
4908 redraw_all_later(CLEAR);
4909 }
4910 clear_snapshot();
4911}
4912
4913/*
4914 * Check if frames "sn" and "fr" have the same layout, same following frames
4915 * and same children.
4916 */
4917 static int
4918check_snapshot_rec(sn, fr)
4919 frame_T *sn;
4920 frame_T *fr;
4921{
4922 if (sn->fr_layout != fr->fr_layout
4923 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
4924 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
4925 || (sn->fr_next != NULL
4926 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
4927 || (sn->fr_child != NULL
4928 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
4929 return FAIL;
4930 return OK;
4931}
4932
4933/*
4934 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
4935 * following frames and children.
4936 * Returns a pointer to the old current window, or NULL.
4937 */
4938 static win_T *
4939restore_snapshot_rec(sn, fr)
4940 frame_T *sn;
4941 frame_T *fr;
4942{
4943 win_T *wp = NULL;
4944 win_T *wp2;
4945
4946 fr->fr_height = sn->fr_height;
4947# ifdef FEAT_VERTSPLIT
4948 fr->fr_width = sn->fr_width;
4949# endif
4950 if (fr->fr_layout == FR_LEAF)
4951 {
4952 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
4953# ifdef FEAT_VERTSPLIT
4954 frame_new_width(fr, fr->fr_width, FALSE);
4955# endif
4956 wp = sn->fr_win;
4957 }
4958 if (sn->fr_next != NULL)
4959 {
4960 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
4961 if (wp2 != NULL)
4962 wp = wp2;
4963 }
4964 if (sn->fr_child != NULL)
4965 {
4966 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
4967 if (wp2 != NULL)
4968 wp = wp2;
4969 }
4970 return wp;
4971}
4972
4973#endif
4974
4975#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
4976/*
4977 * Return TRUE if there is any vertically split window.
4978 */
4979 int
4980win_hasvertsplit()
4981{
4982 frame_T *fr;
4983
4984 if (topframe->fr_layout == FR_ROW)
4985 return TRUE;
4986
4987 if (topframe->fr_layout == FR_COL)
4988 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
4989 if (fr->fr_layout == FR_ROW)
4990 return TRUE;
4991
4992 return FALSE;
4993}
4994#endif