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