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