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