blob: c450221917792c36db8813fb0d3eca0182e90577 [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
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
50static void free_tabpage __ARGS((tabpage_T *tp));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000051static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000052static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_fix_height __ARGS((win_T *wp));
54static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
55static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000056static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000058static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static void frame_append __ARGS((frame_T *after, frame_T *frp));
60static void frame_insert __ARGS((frame_T *before, frame_T *frp));
61static void frame_remove __ARGS((frame_T *frp));
62#ifdef FEAT_VERTSPLIT
63static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void win_goto_ver __ARGS((int up, long count));
65static void win_goto_hor __ARGS((int left, long count));
66#endif
67static void frame_add_height __ARGS((frame_T *frp, int n));
68static void last_status_rec __ARGS((frame_T *fr, int statusline));
69
70static void make_snapshot __ARGS((void));
71static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000072static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static void clear_snapshot_rec __ARGS((frame_T *fr));
74static void restore_snapshot __ARGS((int close_curwin));
75static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
77
78#endif /* FEAT_WINDOWS */
79static win_T *win_alloc __ARGS((win_T *after));
80static void win_new_height __ARGS((win_T *, int));
81
82#define URL_SLASH 1 /* path_is_url() has found "://" */
83#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
84
85#define NOWIN (win_T *)-1 /* non-exisiting window */
86
Bram Moolenaar05159a02005-02-26 23:04:13 +000087#ifdef FEAT_WINDOWS
88static long p_ch_used = 1L; /* value of 'cmdheight' when frame
89 size was set */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000090# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000091#else
92# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000093#endif
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#if defined(FEAT_WINDOWS) || defined(PROTO)
96/*
97 * all CTRL-W window commands are handled here, called from normal_cmd().
98 */
99 void
100do_window(nchar, Prenum, xchar)
101 int nchar;
102 long Prenum;
103 int xchar; /* extra char from ":wincmd gx" or NUL */
104{
105 long Prenum1;
106 win_T *wp;
107#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000109 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110#endif
111#ifdef FEAT_FIND_ID
112 int type = FIND_DEFINE;
113 int len;
114#endif
115 char_u cbuf[40];
116
117 if (Prenum == 0)
118 Prenum1 = 1;
119 else
120 Prenum1 = Prenum;
121
122#ifdef FEAT_CMDWIN
123# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124#else
125# define CHECK_CMDWIN
126#endif
127
128 switch (nchar)
129 {
130/* split current window in two parts, horizontally */
131 case 'S':
132 case Ctrl_S:
133 case 's':
134 CHECK_CMDWIN
135#ifdef FEAT_VISUAL
136 reset_VIsual_and_resel(); /* stop Visual mode */
137#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000138#ifdef FEAT_QUICKFIX
139 /* When splitting the quickfix window open a new buffer in it,
140 * don't replicate the quickfix buffer. */
141 if (bt_quickfix(curbuf))
142 goto newwindow;
143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_GUI
145 need_mouse_correct = TRUE;
146#endif
147 win_split((int)Prenum, 0);
148 break;
149
150#ifdef FEAT_VERTSPLIT
151/* split current window in two parts, vertically */
152 case Ctrl_V:
153 case 'v':
154 CHECK_CMDWIN
155#ifdef FEAT_VISUAL
156 reset_VIsual_and_resel(); /* stop Visual mode */
157#endif
158#ifdef FEAT_GUI
159 need_mouse_correct = TRUE;
160#endif
161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
169#ifdef FEAT_VISUAL
170 reset_VIsual_and_resel(); /* stop Visual mode */
171#endif
172 STRCPY(cbuf, "split #");
173 if (Prenum)
174 sprintf((char *)cbuf + 7, "%ld", Prenum);
175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
189 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 else
191 cbuf[0] = NUL;
192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
199#ifdef FEAT_VISUAL
200 reset_VIsual_and_resel(); /* stop Visual mode */
201#endif
202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
208#ifdef FEAT_VISUAL
209 reset_VIsual_and_resel(); /* stop Visual mode */
210#endif
211 do_cmdline_cmd((char_u *)"close");
212 break;
213
214#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215/* close preview window */
216 case Ctrl_Z:
217 case 'z':
218 CHECK_CMDWIN
219#ifdef FEAT_VISUAL
220 reset_VIsual_and_resel(); /* stop Visual mode */
221#endif
222 do_cmdline_cmd((char_u *)"pclose");
223 break;
224
225/* cursor to preview window */
226 case 'P':
227 for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 if (wp->w_p_pvw)
229 break;
230 if (wp == NULL)
231 EMSG(_("E441: There is no preview window"));
232 else
233 win_goto(wp);
234 break;
235#endif
236
237/* close all but current window */
238 case Ctrl_O:
239 case 'o':
240 CHECK_CMDWIN
241#ifdef FEAT_VISUAL
242 reset_VIsual_and_resel(); /* stop Visual mode */
243#endif
244 do_cmdline_cmd((char_u *)"only");
245 break;
246
247/* cursor to next window with wrap around */
248 case Ctrl_W:
249 case 'w':
250/* cursor to previous window with wrap around */
251 case 'W':
252 CHECK_CMDWIN
253 if (lastwin == firstwin && Prenum != 1) /* just one window */
254 beep_flush();
255 else
256 {
257 if (Prenum) /* go to specified window */
258 {
259 for (wp = firstwin; --Prenum > 0; )
260 {
261 if (wp->w_next == NULL)
262 break;
263 else
264 wp = wp->w_next;
265 }
266 }
267 else
268 {
269 if (nchar == 'W') /* go to previous window */
270 {
271 wp = curwin->w_prev;
272 if (wp == NULL)
273 wp = lastwin; /* wrap around */
274 }
275 else /* go to next window */
276 {
277 wp = curwin->w_next;
278 if (wp == NULL)
279 wp = firstwin; /* wrap around */
280 }
281 }
282 win_goto(wp);
283 }
284 break;
285
286/* cursor to window below */
287 case 'j':
288 case K_DOWN:
289 case Ctrl_J:
290 CHECK_CMDWIN
291#ifdef FEAT_VERTSPLIT
292 win_goto_ver(FALSE, Prenum1);
293#else
294 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 wp = wp->w_next)
296 ;
297 win_goto(wp);
298#endif
299 break;
300
301/* cursor to window above */
302 case 'k':
303 case K_UP:
304 case Ctrl_K:
305 CHECK_CMDWIN
306#ifdef FEAT_VERTSPLIT
307 win_goto_ver(TRUE, Prenum1);
308#else
309 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 wp = wp->w_prev)
311 ;
312 win_goto(wp);
313#endif
314 break;
315
316#ifdef FEAT_VERTSPLIT
317/* cursor to left window */
318 case 'h':
319 case K_LEFT:
320 case Ctrl_H:
321 case K_BS:
322 CHECK_CMDWIN
323 win_goto_hor(TRUE, Prenum1);
324 break;
325
326/* cursor to right window */
327 case 'l':
328 case K_RIGHT:
329 case Ctrl_L:
330 CHECK_CMDWIN
331 win_goto_hor(FALSE, Prenum1);
332 break;
333#endif
334
335/* cursor to top-left window */
336 case 't':
337 case Ctrl_T:
338 win_goto(firstwin);
339 break;
340
341/* cursor to bottom-right window */
342 case 'b':
343 case Ctrl_B:
344 win_goto(lastwin);
345 break;
346
347/* cursor to last accessed (previous) window */
348 case 'p':
349 case Ctrl_P:
350 if (prevwin == NULL)
351 beep_flush();
352 else
353 win_goto(prevwin);
354 break;
355
356/* exchange current and next window */
357 case 'x':
358 case Ctrl_X:
359 CHECK_CMDWIN
360 win_exchange(Prenum);
361 break;
362
363/* rotate windows downwards */
364 case Ctrl_R:
365 case 'r':
366 CHECK_CMDWIN
367#ifdef FEAT_VISUAL
368 reset_VIsual_and_resel(); /* stop Visual mode */
369#endif
370 win_rotate(FALSE, (int)Prenum1); /* downwards */
371 break;
372
373/* rotate windows upwards */
374 case 'R':
375 CHECK_CMDWIN
376#ifdef FEAT_VISUAL
377 reset_VIsual_and_resel(); /* stop Visual mode */
378#endif
379 win_rotate(TRUE, (int)Prenum1); /* upwards */
380 break;
381
382/* move window to the very top/bottom/left/right */
383 case 'K':
384 case 'J':
385#ifdef FEAT_VERTSPLIT
386 case 'H':
387 case 'L':
388#endif
389 CHECK_CMDWIN
390 win_totop((int)Prenum,
391 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
392 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
393 break;
394
395/* make all windows the same height */
396 case '=':
397#ifdef FEAT_GUI
398 need_mouse_correct = TRUE;
399#endif
400 win_equal(NULL, FALSE, 'b');
401 break;
402
403/* increase current window height */
404 case '+':
405#ifdef FEAT_GUI
406 need_mouse_correct = TRUE;
407#endif
408 win_setheight(curwin->w_height + (int)Prenum1);
409 break;
410
411/* decrease current window height */
412 case '-':
413#ifdef FEAT_GUI
414 need_mouse_correct = TRUE;
415#endif
416 win_setheight(curwin->w_height - (int)Prenum1);
417 break;
418
419/* set current window height */
420 case Ctrl__:
421 case '_':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_setheight(Prenum ? (int)Prenum : 9999);
426 break;
427
428#ifdef FEAT_VERTSPLIT
429/* increase current window width */
430 case '>':
431#ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433#endif
434 win_setwidth(curwin->w_width + (int)Prenum1);
435 break;
436
437/* decrease current window width */
438 case '<':
439#ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441#endif
442 win_setwidth(curwin->w_width - (int)Prenum1);
443 break;
444
445/* set current window width */
446 case '|':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
451 break;
452#endif
453
454/* jump to tag and split window if tag exists (in preview window) */
455#if defined(FEAT_QUICKFIX)
456 case '}':
457 CHECK_CMDWIN
458 if (Prenum)
459 g_do_tagpreview = Prenum;
460 else
461 g_do_tagpreview = p_pvh;
462 /*FALLTHROUGH*/
463#endif
464 case ']':
465 case Ctrl_RSB:
466 CHECK_CMDWIN
467#ifdef FEAT_VISUAL
468 reset_VIsual_and_resel(); /* stop Visual mode */
469#endif
470 if (Prenum)
471 postponed_split = Prenum;
472 else
473 postponed_split = -1;
474
475 /* Execute the command right here, required when
476 * "wincmd ]" was used in a function. */
477 do_nv_ident(Ctrl_RSB, NUL);
478 break;
479
480#ifdef FEAT_SEARCHPATH
481/* edit file name under cursor in a new window */
482 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000483 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 case Ctrl_F:
485 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000486
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000487 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 if (ptr != NULL)
489 {
490#ifdef FEAT_GUI
491 need_mouse_correct = TRUE;
492#endif
493 setpcmark();
494 if (win_split(0, 0) == OK)
495 {
496# ifdef FEAT_SCROLLBIND
497 curwin->w_p_scb = FALSE;
498# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000499 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
500 if (nchar == 'F' && lnum >= 0)
501 {
502 curwin->w_cursor.lnum = lnum;
503 check_cursor_lnum();
504 beginline(BL_SOL | BL_FIX);
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 }
507 vim_free(ptr);
508 }
509 break;
510#endif
511
512#ifdef FEAT_FIND_ID
513/* Go to the first occurence of the identifier under cursor along path in a
514 * new window -- webb
515 */
516 case 'i': /* Go to any match */
517 case Ctrl_I:
518 type = FIND_ANY;
519 /* FALLTHROUGH */
520 case 'd': /* Go to definition, using 'define' */
521 case Ctrl_D:
522 CHECK_CMDWIN
523 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
524 break;
525 find_pattern_in_path(ptr, 0, len, TRUE,
526 Prenum == 0 ? TRUE : FALSE, type,
527 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
528 curwin->w_set_curswant = TRUE;
529 break;
530#endif
531
Bram Moolenaar05159a02005-02-26 23:04:13 +0000532 case K_KENTER:
533 case CAR:
534#if defined(FEAT_QUICKFIX)
535 /*
536 * In a quickfix window a <CR> jumps to the error under the
537 * cursor in a new window.
538 */
539 if (bt_quickfix(curbuf))
540 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000541 sprintf((char *)cbuf, "split +%ld%s",
542 (long)curwin->w_cursor.lnum,
543 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000544 do_cmdline_cmd(cbuf);
545 }
546#endif
547 break;
548
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/* CTRL-W g extended commands */
551 case 'g':
552 case Ctrl_G:
553 CHECK_CMDWIN
554#ifdef USE_ON_FLY_SCROLL
555 dont_scroll = TRUE; /* disallow scrolling here */
556#endif
557 ++no_mapping;
558 ++allow_keys; /* no mapping for xchar, but allow key codes */
559 if (xchar == NUL)
560 xchar = safe_vgetc();
561#ifdef FEAT_LANGMAP
562 LANGMAP_ADJUST(xchar, TRUE);
563#endif
564 --no_mapping;
565 --allow_keys;
566#ifdef FEAT_CMDL_INFO
567 (void)add_to_showcmd(xchar);
568#endif
569 switch (xchar)
570 {
571#if defined(FEAT_QUICKFIX)
572 case '}':
573 xchar = Ctrl_RSB;
574 if (Prenum)
575 g_do_tagpreview = Prenum;
576 else
577 g_do_tagpreview = p_pvh;
578 /*FALLTHROUGH*/
579#endif
580 case ']':
581 case Ctrl_RSB:
582#ifdef FEAT_VISUAL
583 reset_VIsual_and_resel(); /* stop Visual mode */
584#endif
585 if (Prenum)
586 postponed_split = Prenum;
587 else
588 postponed_split = -1;
589
590 /* Execute the command right here, required when
591 * "wincmd g}" was used in a function. */
592 do_nv_ident('g', xchar);
593 break;
594
595 default:
596 beep_flush();
597 break;
598 }
599 break;
600
601 default: beep_flush();
602 break;
603 }
604}
605
606/*
607 * split the current window, implements CTRL-W s and :split
608 *
609 * "size" is the height or width for the new window, 0 to use half of current
610 * height or width.
611 *
612 * "flags":
613 * WSP_ROOM: require enough room for new window
614 * WSP_VERT: vertical split.
615 * WSP_TOP: open window at the top-left of the shell (help window).
616 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
617 * WSP_HELP: creating the help window, keep layout snapshot
618 *
619 * return FAIL for failure, OK otherwise
620 */
621 int
622win_split(size, flags)
623 int size;
624 int flags;
625{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000626 /* When the ":tab" modifier was used open a new tab page instead. */
627 if (may_open_tabpage() == OK)
628 return OK;
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 /* Add flags from ":vertical", ":topleft" and ":botright". */
631 flags |= cmdmod.split;
632 if ((flags & WSP_TOP) && (flags & WSP_BOT))
633 {
634 EMSG(_("E442: Can't split topleft and botright at the same time"));
635 return FAIL;
636 }
637
638 /* When creating the help window make a snapshot of the window layout.
639 * Otherwise clear the snapshot, it's now invalid. */
640 if (flags & WSP_HELP)
641 make_snapshot();
642 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000643 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
645 return win_split_ins(size, flags, NULL, 0);
646}
647
648/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000649 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 * When "newwin" is not NULL: insert this window at the far
651 * top/left/right/bottom.
652 * return FAIL for failure, OK otherwise
653 */
654 static int
655win_split_ins(size, flags, newwin, dir)
656 int size;
657 int flags;
658 win_T *newwin;
659 int dir;
660{
661 win_T *wp = newwin;
662 win_T *oldwin;
663 int new_size = size;
664 int i;
665 int need_status = 0;
666 int do_equal = FALSE;
667 int needed;
668 int available;
669 int oldwin_height = 0;
670 int layout;
671 frame_T *frp, *curfrp;
672 int before;
673
674 if (flags & WSP_TOP)
675 oldwin = firstwin;
676 else if (flags & WSP_BOT)
677 oldwin = lastwin;
678 else
679 oldwin = curwin;
680
681 /* add a status line when p_ls == 1 and splitting the first window */
682 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
683 {
684 if (oldwin->w_height <= p_wmh && newwin == NULL)
685 {
686 EMSG(_(e_noroom));
687 return FAIL;
688 }
689 need_status = STATUS_HEIGHT;
690 }
691
692#ifdef FEAT_VERTSPLIT
693 if (flags & WSP_VERT)
694 {
695 layout = FR_ROW;
696 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
697
698 /*
699 * Check if we are able to split the current window and compute its
700 * width.
701 */
702 needed = p_wmw + 1;
703 if (flags & WSP_ROOM)
704 needed += p_wiw - p_wmw;
705 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
706 {
707 available = topframe->fr_width;
708 needed += frame_minwidth(topframe, NULL);
709 }
710 else
711 available = oldwin->w_width;
712 if (available < needed && newwin == NULL)
713 {
714 EMSG(_(e_noroom));
715 return FAIL;
716 }
717 if (new_size == 0)
718 new_size = oldwin->w_width / 2;
719 if (new_size > oldwin->w_width - p_wmw - 1)
720 new_size = oldwin->w_width - p_wmw - 1;
721 if (new_size < p_wmw)
722 new_size = p_wmw;
723
724 /* if it doesn't fit in the current window, need win_equal() */
725 if (oldwin->w_width - new_size - 1 < p_wmw)
726 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000727
728 /* We don't like to take lines for the new window from a
729 * 'winfixwidth' window. Take them from a window to the left or right
730 * instead, if possible. */
731 if (oldwin->w_p_wfw)
732 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 else
735#endif
736 {
737 layout = FR_COL;
738 do_equal = (p_ea && new_size == 0
739#ifdef FEAT_VERTSPLIT
740 && *p_ead != 'h'
741#endif
742 );
743
744 /*
745 * Check if we are able to split the current window and compute its
746 * height.
747 */
748 needed = p_wmh + STATUS_HEIGHT + need_status;
749 if (flags & WSP_ROOM)
750 needed += p_wh - p_wmh;
751 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
752 {
753 available = topframe->fr_height;
754 needed += frame_minheight(topframe, NULL);
755 }
756 else
757 {
758 available = oldwin->w_height;
759 needed += p_wmh;
760 }
761 if (available < needed && newwin == NULL)
762 {
763 EMSG(_(e_noroom));
764 return FAIL;
765 }
766 oldwin_height = oldwin->w_height;
767 if (need_status)
768 {
769 oldwin->w_status_height = STATUS_HEIGHT;
770 oldwin_height -= STATUS_HEIGHT;
771 }
772 if (new_size == 0)
773 new_size = oldwin_height / 2;
774
775 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
776 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
777 if (new_size < p_wmh)
778 new_size = p_wmh;
779
780 /* if it doesn't fit in the current window, need win_equal() */
781 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
782 do_equal = TRUE;
783
784 /* We don't like to take lines for the new window from a
785 * 'winfixheight' window. Take them from a window above or below
786 * instead, if possible. */
787 if (oldwin->w_p_wfh)
788 {
789 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
790 oldwin);
791 oldwin_height = oldwin->w_height;
792 if (need_status)
793 oldwin_height -= STATUS_HEIGHT;
794 }
795 }
796
797 /*
798 * allocate new window structure and link it in the window list
799 */
800 if ((flags & WSP_TOP) == 0
801 && ((flags & WSP_BOT)
802 || (flags & WSP_BELOW)
803 || (!(flags & WSP_ABOVE)
804 && (
805#ifdef FEAT_VERTSPLIT
806 (flags & WSP_VERT) ? p_spr :
807#endif
808 p_sb))))
809 {
810 /* new window below/right of current one */
811 if (newwin == NULL)
812 wp = win_alloc(oldwin);
813 else
814 win_append(oldwin, wp);
815 }
816 else
817 {
818 if (newwin == NULL)
819 wp = win_alloc(oldwin->w_prev);
820 else
821 win_append(oldwin->w_prev, wp);
822 }
823
824 if (newwin == NULL)
825 {
826 if (wp == NULL)
827 return FAIL;
828
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000829 /* make the contents of the new window the same as the current one */
830 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832
833 /*
834 * Reorganise the tree of frames to insert the new window.
835 */
836 if (flags & (WSP_TOP | WSP_BOT))
837 {
838#ifdef FEAT_VERTSPLIT
839 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
840 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
841#else
842 if (topframe->fr_layout == FR_COL)
843#endif
844 {
845 curfrp = topframe->fr_child;
846 if (flags & WSP_BOT)
847 while (curfrp->fr_next != NULL)
848 curfrp = curfrp->fr_next;
849 }
850 else
851 curfrp = topframe;
852 before = (flags & WSP_TOP);
853 }
854 else
855 {
856 curfrp = oldwin->w_frame;
857 if (flags & WSP_BELOW)
858 before = FALSE;
859 else if (flags & WSP_ABOVE)
860 before = TRUE;
861 else
862#ifdef FEAT_VERTSPLIT
863 if (flags & WSP_VERT)
864 before = !p_spr;
865 else
866#endif
867 before = !p_sb;
868 }
869 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
870 {
871 /* Need to create a new frame in the tree to make a branch. */
872 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
873 *frp = *curfrp;
874 curfrp->fr_layout = layout;
875 frp->fr_parent = curfrp;
876 frp->fr_next = NULL;
877 frp->fr_prev = NULL;
878 curfrp->fr_child = frp;
879 curfrp->fr_win = NULL;
880 curfrp = frp;
881 if (frp->fr_win != NULL)
882 oldwin->w_frame = frp;
883 else
884 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
885 frp->fr_parent = curfrp;
886 }
887
888 if (newwin == NULL)
889 {
890 /* Create a frame for the new window. */
891 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
892 frp->fr_layout = FR_LEAF;
893 frp->fr_win = wp;
894 wp->w_frame = frp;
895 }
896 else
897 frp = newwin->w_frame;
898 frp->fr_parent = curfrp->fr_parent;
899
900 /* Insert the new frame at the right place in the frame list. */
901 if (before)
902 frame_insert(curfrp, frp);
903 else
904 frame_append(curfrp, frp);
905
906#ifdef FEAT_VERTSPLIT
907 if (flags & WSP_VERT)
908 {
909 wp->w_p_scr = curwin->w_p_scr;
910 if (need_status)
911 {
912 --oldwin->w_height;
913 oldwin->w_status_height = need_status;
914 }
915 if (flags & (WSP_TOP | WSP_BOT))
916 {
917 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000918 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 wp->w_height = curfrp->fr_height - (p_ls > 0);
920 wp->w_status_height = (p_ls > 0);
921 }
922 else
923 {
924 /* height and row of new window is same as current window */
925 wp->w_winrow = oldwin->w_winrow;
926 wp->w_height = oldwin->w_height;
927 wp->w_status_height = oldwin->w_status_height;
928 }
929 frp->fr_height = curfrp->fr_height;
930
931 /* "new_size" of the current window goes to the new window, use
932 * one column for the vertical separator */
933 wp->w_width = new_size;
934 if (before)
935 wp->w_vsep_width = 1;
936 else
937 {
938 wp->w_vsep_width = oldwin->w_vsep_width;
939 oldwin->w_vsep_width = 1;
940 }
941 if (flags & (WSP_TOP | WSP_BOT))
942 {
943 if (flags & WSP_BOT)
944 frame_add_vsep(curfrp);
945 /* Set width of neighbor frame */
946 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000947 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
948 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 }
950 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000951 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (before) /* new window left of current one */
953 {
954 wp->w_wincol = oldwin->w_wincol;
955 oldwin->w_wincol += new_size + 1;
956 }
957 else /* new window right of current one */
958 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
959 frame_fix_width(oldwin);
960 frame_fix_width(wp);
961 }
962 else
963#endif
964 {
965 /* width and column of new window is same as current window */
966#ifdef FEAT_VERTSPLIT
967 if (flags & (WSP_TOP | WSP_BOT))
968 {
969 wp->w_wincol = 0;
970 wp->w_width = Columns;
971 wp->w_vsep_width = 0;
972 }
973 else
974 {
975 wp->w_wincol = oldwin->w_wincol;
976 wp->w_width = oldwin->w_width;
977 wp->w_vsep_width = oldwin->w_vsep_width;
978 }
979 frp->fr_width = curfrp->fr_width;
980#endif
981
982 /* "new_size" of the current window goes to the new window, use
983 * one row for the status line */
984 win_new_height(wp, new_size);
985 if (flags & (WSP_TOP | WSP_BOT))
986 frame_new_height(curfrp, curfrp->fr_height
987 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
988 else
989 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
990 if (before) /* new window above current one */
991 {
992 wp->w_winrow = oldwin->w_winrow;
993 wp->w_status_height = STATUS_HEIGHT;
994 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
995 }
996 else /* new window below current one */
997 {
998 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
999 wp->w_status_height = oldwin->w_status_height;
1000 oldwin->w_status_height = STATUS_HEIGHT;
1001 }
1002#ifdef FEAT_VERTSPLIT
1003 if (flags & WSP_BOT)
1004 frame_add_statusline(curfrp);
1005#endif
1006 frame_fix_height(wp);
1007 frame_fix_height(oldwin);
1008 }
1009
1010 if (flags & (WSP_TOP | WSP_BOT))
1011 (void)win_comp_pos();
1012
1013 /*
1014 * Both windows need redrawing
1015 */
1016 redraw_win_later(wp, NOT_VALID);
1017 wp->w_redr_status = TRUE;
1018 redraw_win_later(oldwin, NOT_VALID);
1019 oldwin->w_redr_status = TRUE;
1020
1021 if (need_status)
1022 {
1023 msg_row = Rows - 1;
1024 msg_col = sc_col;
1025 msg_clr_eos_force(); /* Old command/ruler may still be there */
1026 comp_col();
1027 msg_row = Rows - 1;
1028 msg_col = 0; /* put position back at start of line */
1029 }
1030
1031 /*
1032 * make the new window the current window and redraw
1033 */
1034 if (do_equal || dir != 0)
1035 win_equal(wp, TRUE,
1036#ifdef FEAT_VERTSPLIT
1037 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1038 : dir == 'h' ? 'b' :
1039#endif
1040 'v');
1041
1042 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1043 * size was given. */
1044#ifdef FEAT_VERTSPLIT
1045 if (flags & WSP_VERT)
1046 {
1047 i = p_wiw;
1048 if (size != 0)
1049 p_wiw = size;
1050
1051# ifdef FEAT_GUI
1052 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1053 if (gui.in_use)
1054 gui_init_which_components(NULL);
1055# endif
1056 }
1057 else
1058#endif
1059 {
1060 i = p_wh;
1061 if (size != 0)
1062 p_wh = size;
1063 }
1064 win_enter(wp, FALSE);
1065#ifdef FEAT_VERTSPLIT
1066 if (flags & WSP_VERT)
1067 p_wiw = i;
1068 else
1069#endif
1070 p_wh = i;
1071
1072 return OK;
1073}
1074
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001075/*
1076 * Initialize window "newp" from window "oldp".
1077 * Used when splitting a window and when creating a new tab page.
1078 * The windows will both edit the same buffer.
1079 */
1080 static void
1081win_init(newp, oldp)
1082 win_T *newp;
1083 win_T *oldp;
1084{
1085 int i;
1086
1087 newp->w_buffer = oldp->w_buffer;
1088 oldp->w_buffer->b_nwindows++;
1089 newp->w_cursor = oldp->w_cursor;
1090 newp->w_valid = 0;
1091 newp->w_curswant = oldp->w_curswant;
1092 newp->w_set_curswant = oldp->w_set_curswant;
1093 newp->w_topline = oldp->w_topline;
1094#ifdef FEAT_DIFF
1095 newp->w_topfill = oldp->w_topfill;
1096#endif
1097 newp->w_leftcol = oldp->w_leftcol;
1098 newp->w_pcmark = oldp->w_pcmark;
1099 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1100 newp->w_alt_fnum = oldp->w_alt_fnum;
1101 newp->w_fraction = oldp->w_fraction;
1102 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1103#ifdef FEAT_JUMPLIST
1104 copy_jumplist(oldp, newp);
1105#endif
1106#ifdef FEAT_QUICKFIX
1107 copy_loclist(oldp, newp);
1108#endif
1109 if (oldp->w_localdir != NULL)
1110 newp->w_localdir = vim_strsave(oldp->w_localdir);
1111
1112 /* Use the same argument list. */
1113 newp->w_alist = oldp->w_alist;
1114 ++newp->w_alist->al_refcount;
1115 newp->w_arg_idx = oldp->w_arg_idx;
1116
1117 /*
1118 * copy tagstack and options from existing window
1119 */
1120 for (i = 0; i < oldp->w_tagstacklen; i++)
1121 {
1122 newp->w_tagstack[i] = oldp->w_tagstack[i];
1123 if (newp->w_tagstack[i].tagname != NULL)
1124 newp->w_tagstack[i].tagname =
1125 vim_strsave(newp->w_tagstack[i].tagname);
1126 }
1127 newp->w_tagstackidx = oldp->w_tagstackidx;
1128 newp->w_tagstacklen = oldp->w_tagstacklen;
1129 win_copy_options(oldp, newp);
1130# ifdef FEAT_FOLDING
1131 copyFoldingState(oldp, newp);
1132# endif
1133}
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#endif /* FEAT_WINDOWS */
1136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#if defined(FEAT_WINDOWS) || defined(PROTO)
1138/*
1139 * Check if "win" is a pointer to an existing window.
1140 */
1141 int
1142win_valid(win)
1143 win_T *win;
1144{
1145 win_T *wp;
1146
1147 if (win == NULL)
1148 return FALSE;
1149 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1150 if (wp == win)
1151 return TRUE;
1152 return FALSE;
1153}
1154
1155/*
1156 * Return the number of windows.
1157 */
1158 int
1159win_count()
1160{
1161 win_T *wp;
1162 int count = 0;
1163
1164 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1165 ++count;
1166 return count;
1167}
1168
1169/*
1170 * Make "count" windows on the screen.
1171 * Return actual number of windows on the screen.
1172 * Must be called when there is just one window, filling the whole screen
1173 * (excluding the command line).
1174 */
1175/*ARGSUSED*/
1176 int
1177make_windows(count, vertical)
1178 int count;
1179 int vertical; /* split windows vertically if TRUE */
1180{
1181 int maxcount;
1182 int todo;
1183
1184#ifdef FEAT_VERTSPLIT
1185 if (vertical)
1186 {
1187 /* Each windows needs at least 'winminwidth' lines and a separator
1188 * column. */
1189 maxcount = (curwin->w_width + curwin->w_vsep_width
1190 - (p_wiw - p_wmw)) / (p_wmw + 1);
1191 }
1192 else
1193#endif
1194 {
1195 /* Each window needs at least 'winminheight' lines and a status line. */
1196 maxcount = (curwin->w_height + curwin->w_status_height
1197 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1198 }
1199
1200 if (maxcount < 2)
1201 maxcount = 2;
1202 if (count > maxcount)
1203 count = maxcount;
1204
1205 /*
1206 * add status line now, otherwise first window will be too big
1207 */
1208 if (count > 1)
1209 last_status(TRUE);
1210
1211#ifdef FEAT_AUTOCMD
1212 /*
1213 * Don't execute autocommands while creating the windows. Must do that
1214 * when putting the buffers in the windows.
1215 */
1216 ++autocmd_block;
1217#endif
1218
1219 /* todo is number of windows left to create */
1220 for (todo = count - 1; todo > 0; --todo)
1221#ifdef FEAT_VERTSPLIT
1222 if (vertical)
1223 {
1224 if (win_split(curwin->w_width - (curwin->w_width - todo)
1225 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1226 break;
1227 }
1228 else
1229#endif
1230 {
1231 if (win_split(curwin->w_height - (curwin->w_height - todo
1232 * STATUS_HEIGHT) / (todo + 1)
1233 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1234 break;
1235 }
1236
1237#ifdef FEAT_AUTOCMD
1238 --autocmd_block;
1239#endif
1240
1241 /* return actual number of windows */
1242 return (count - todo);
1243}
1244
1245/*
1246 * Exchange current and next window
1247 */
1248 static void
1249win_exchange(Prenum)
1250 long Prenum;
1251{
1252 frame_T *frp;
1253 frame_T *frp2;
1254 win_T *wp;
1255 win_T *wp2;
1256 int temp;
1257
1258 if (lastwin == firstwin) /* just one window */
1259 {
1260 beep_flush();
1261 return;
1262 }
1263
1264#ifdef FEAT_GUI
1265 need_mouse_correct = TRUE;
1266#endif
1267
1268 /*
1269 * find window to exchange with
1270 */
1271 if (Prenum)
1272 {
1273 frp = curwin->w_frame->fr_parent->fr_child;
1274 while (frp != NULL && --Prenum > 0)
1275 frp = frp->fr_next;
1276 }
1277 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1278 frp = curwin->w_frame->fr_next;
1279 else /* Swap last window in row/col with previous */
1280 frp = curwin->w_frame->fr_prev;
1281
1282 /* We can only exchange a window with another window, not with a frame
1283 * containing windows. */
1284 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1285 return;
1286 wp = frp->fr_win;
1287
1288/*
1289 * 1. remove curwin from the list. Remember after which window it was in wp2
1290 * 2. insert curwin before wp in the list
1291 * if wp != wp2
1292 * 3. remove wp from the list
1293 * 4. insert wp after wp2
1294 * 5. exchange the status line height and vsep width.
1295 */
1296 wp2 = curwin->w_prev;
1297 frp2 = curwin->w_frame->fr_prev;
1298 if (wp->w_prev != curwin)
1299 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001300 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 frame_remove(curwin->w_frame);
1302 win_append(wp->w_prev, curwin);
1303 frame_insert(frp, curwin->w_frame);
1304 }
1305 if (wp != wp2)
1306 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001307 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 frame_remove(wp->w_frame);
1309 win_append(wp2, wp);
1310 if (frp2 == NULL)
1311 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1312 else
1313 frame_append(frp2, wp->w_frame);
1314 }
1315 temp = curwin->w_status_height;
1316 curwin->w_status_height = wp->w_status_height;
1317 wp->w_status_height = temp;
1318#ifdef FEAT_VERTSPLIT
1319 temp = curwin->w_vsep_width;
1320 curwin->w_vsep_width = wp->w_vsep_width;
1321 wp->w_vsep_width = temp;
1322
1323 /* If the windows are not in the same frame, exchange the sizes to avoid
1324 * messing up the window layout. Otherwise fix the frame sizes. */
1325 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1326 {
1327 temp = curwin->w_height;
1328 curwin->w_height = wp->w_height;
1329 wp->w_height = temp;
1330 temp = curwin->w_width;
1331 curwin->w_width = wp->w_width;
1332 wp->w_width = temp;
1333 }
1334 else
1335 {
1336 frame_fix_height(curwin);
1337 frame_fix_height(wp);
1338 frame_fix_width(curwin);
1339 frame_fix_width(wp);
1340 }
1341#endif
1342
1343 (void)win_comp_pos(); /* recompute window positions */
1344
1345 win_enter(wp, TRUE);
1346 redraw_later(CLEAR);
1347}
1348
1349/*
1350 * rotate windows: if upwards TRUE the second window becomes the first one
1351 * if upwards FALSE the first window becomes the second one
1352 */
1353 static void
1354win_rotate(upwards, count)
1355 int upwards;
1356 int count;
1357{
1358 win_T *wp1;
1359 win_T *wp2;
1360 frame_T *frp;
1361 int n;
1362
1363 if (firstwin == lastwin) /* nothing to do */
1364 {
1365 beep_flush();
1366 return;
1367 }
1368
1369#ifdef FEAT_GUI
1370 need_mouse_correct = TRUE;
1371#endif
1372
1373#ifdef FEAT_VERTSPLIT
1374 /* Check if all frames in this row/col have one window. */
1375 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1376 frp = frp->fr_next)
1377 if (frp->fr_win == NULL)
1378 {
1379 EMSG(_("E443: Cannot rotate when another window is split"));
1380 return;
1381 }
1382#endif
1383
1384 while (count--)
1385 {
1386 if (upwards) /* first window becomes last window */
1387 {
1388 /* remove first window/frame from the list */
1389 frp = curwin->w_frame->fr_parent->fr_child;
1390 wp1 = frp->fr_win;
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 /* find last frame and append removed window/frame after it */
1395 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1396 ;
1397 win_append(frp->fr_win, wp1);
1398 frame_append(frp, wp1->w_frame);
1399
1400 wp2 = frp->fr_win; /* previously last window */
1401 }
1402 else /* last window becomes first window */
1403 {
1404 /* find last window/frame in the list and remove it */
1405 for (frp = curwin->w_frame; frp->fr_next != NULL;
1406 frp = frp->fr_next)
1407 ;
1408 wp1 = frp->fr_win;
1409 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001410 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 frame_remove(frp);
1412
1413 /* append the removed window/frame before the first in the list */
1414 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1415 frame_insert(frp->fr_parent->fr_child, frp);
1416 }
1417
1418 /* exchange status height and vsep width of old and new last window */
1419 n = wp2->w_status_height;
1420 wp2->w_status_height = wp1->w_status_height;
1421 wp1->w_status_height = n;
1422 frame_fix_height(wp1);
1423 frame_fix_height(wp2);
1424#ifdef FEAT_VERTSPLIT
1425 n = wp2->w_vsep_width;
1426 wp2->w_vsep_width = wp1->w_vsep_width;
1427 wp1->w_vsep_width = n;
1428 frame_fix_width(wp1);
1429 frame_fix_width(wp2);
1430#endif
1431
1432 /* recompute w_winrow and w_wincol for all windows */
1433 (void)win_comp_pos();
1434 }
1435
1436 redraw_later(CLEAR);
1437}
1438
1439/*
1440 * Move the current window to the very top/bottom/left/right of the screen.
1441 */
1442 static void
1443win_totop(size, flags)
1444 int size;
1445 int flags;
1446{
1447 int dir;
1448 int height = curwin->w_height;
1449
1450 if (lastwin == firstwin)
1451 {
1452 beep_flush();
1453 return;
1454 }
1455
1456 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457 (void)winframe_remove(curwin, &dir, NULL);
1458 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 last_status(FALSE); /* may need to remove last status line */
1460 (void)win_comp_pos(); /* recompute window positions */
1461
1462 /* Split a window on the desired side and put the window there. */
1463 (void)win_split_ins(size, flags, curwin, dir);
1464 if (!(flags & WSP_VERT))
1465 {
1466 win_setheight(height);
1467 if (p_ea)
1468 win_equal(curwin, TRUE, 'v');
1469 }
1470
1471#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1472 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1473 * scrollbars. Have to update them anyway. */
1474 if (gui.in_use)
1475 {
1476 out_flush();
1477 gui_init_which_components(NULL);
1478 gui_update_scrollbars(TRUE);
1479 }
1480 need_mouse_correct = TRUE;
1481#endif
1482
1483}
1484
1485/*
1486 * Move window "win1" to below/right of "win2" and make "win1" the current
1487 * window. Only works within the same frame!
1488 */
1489 void
1490win_move_after(win1, win2)
1491 win_T *win1, *win2;
1492{
1493 int height;
1494
1495 /* check if the arguments are reasonable */
1496 if (win1 == win2)
1497 return;
1498
1499 /* check if there is something to do */
1500 if (win2->w_next != win1)
1501 {
1502 /* may need move the status line/vertical separator of the last window
1503 * */
1504 if (win1 == lastwin)
1505 {
1506 height = win1->w_prev->w_status_height;
1507 win1->w_prev->w_status_height = win1->w_status_height;
1508 win1->w_status_height = height;
1509#ifdef FEAT_VERTSPLIT
1510 win1->w_prev->w_vsep_width = 0;
1511 win1->w_vsep_width = 1;
1512#endif
1513 }
1514 else if (win2 == lastwin)
1515 {
1516 height = win1->w_status_height;
1517 win1->w_status_height = win2->w_status_height;
1518 win2->w_status_height = height;
1519#ifdef FEAT_VERTSPLIT
1520 win2->w_vsep_width = 1;
1521 win1->w_vsep_width = 0;
1522#endif
1523 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001524 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 frame_remove(win1->w_frame);
1526 win_append(win2, win1);
1527 frame_append(win2->w_frame, win1->w_frame);
1528
1529 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1530 redraw_later(NOT_VALID);
1531 }
1532 win_enter(win1, FALSE);
1533}
1534
1535/*
1536 * Make all windows the same height.
1537 * 'next_curwin' will soon be the current window, make sure it has enough
1538 * rows.
1539 */
1540 void
1541win_equal(next_curwin, current, dir)
1542 win_T *next_curwin; /* pointer to current window to be or NULL */
1543 int current; /* do only frame with current window */
1544 int dir; /* 'v' for vertically, 'h' for horizontally,
1545 'b' for both, 0 for using p_ead */
1546{
1547 if (dir == 0)
1548#ifdef FEAT_VERTSPLIT
1549 dir = *p_ead;
1550#else
1551 dir = 'b';
1552#endif
1553 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001554 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001555 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556}
1557
1558/*
1559 * Set a frame to a new position and height, spreading the available room
1560 * equally over contained frames.
1561 * The window "next_curwin" (if not NULL) should at least get the size from
1562 * 'winheight' and 'winwidth' if possible.
1563 */
1564 static void
1565win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1566 win_T *next_curwin; /* pointer to current window to be or NULL */
1567 int current; /* do only frame with current window */
1568 frame_T *topfr; /* frame to set size off */
1569 int dir; /* 'v', 'h' or 'b', see win_equal() */
1570 int col; /* horizontal position for frame */
1571 int row; /* vertical position for frame */
1572 int width; /* new width of frame */
1573 int height; /* new height of frame */
1574{
1575 int n, m;
1576 int extra_sep = 0;
1577 int wincount, totwincount = 0;
1578 frame_T *fr;
1579 int next_curwin_size = 0;
1580 int room = 0;
1581 int new_size;
1582 int has_next_curwin = 0;
1583 int hnc;
1584
1585 if (topfr->fr_layout == FR_LEAF)
1586 {
1587 /* Set the width/height of this frame.
1588 * Redraw when size or position changes */
1589 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1590#ifdef FEAT_VERTSPLIT
1591 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1592#endif
1593 )
1594 {
1595 topfr->fr_win->w_winrow = row;
1596 frame_new_height(topfr, height, FALSE, FALSE);
1597#ifdef FEAT_VERTSPLIT
1598 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001599 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600#endif
1601 redraw_all_later(CLEAR);
1602 }
1603 }
1604#ifdef FEAT_VERTSPLIT
1605 else if (topfr->fr_layout == FR_ROW)
1606 {
1607 topfr->fr_width = width;
1608 topfr->fr_height = height;
1609
1610 if (dir != 'v') /* equalize frame widths */
1611 {
1612 /* Compute the maximum number of windows horizontally in this
1613 * frame. */
1614 n = frame_minwidth(topfr, NOWIN);
1615 /* add one for the rightmost window, it doesn't have a separator */
1616 if (col + width == Columns)
1617 extra_sep = 1;
1618 else
1619 extra_sep = 0;
1620 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001621 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001623 /*
1624 * Compute width for "next_curwin" window and room available for
1625 * other windows.
1626 * "m" is the minimal width when counting p_wiw for "next_curwin".
1627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 m = frame_minwidth(topfr, next_curwin);
1629 room = width - m;
1630 if (room < 0)
1631 {
1632 next_curwin_size = p_wiw + room;
1633 room = 0;
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 else
1636 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001637 next_curwin_size = -1;
1638 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1639 {
1640 /* If 'winfixwidth' set keep the window width if
1641 * possible.
1642 * Watch out for this window being the next_curwin. */
1643 if (frame_fixed_width(fr))
1644 {
1645 n = frame_minwidth(fr, NOWIN);
1646 new_size = fr->fr_width;
1647 if (frame_has_win(fr, next_curwin))
1648 {
1649 room += p_wiw - p_wmw;
1650 next_curwin_size = 0;
1651 if (new_size < p_wiw)
1652 new_size = p_wiw;
1653 }
1654 else
1655 /* These windows don't use up room. */
1656 totwincount -= (n + (fr->fr_next == NULL
1657 ? extra_sep : 0)) / (p_wmw + 1);
1658 room -= new_size - n;
1659 if (room < 0)
1660 {
1661 new_size += room;
1662 room = 0;
1663 }
1664 fr->fr_newwidth = new_size;
1665 }
1666 }
1667 if (next_curwin_size == -1)
1668 {
1669 if (!has_next_curwin)
1670 next_curwin_size = 0;
1671 else if (totwincount > 1
1672 && (room + (totwincount - 2))
1673 / (totwincount - 1) > p_wiw)
1674 {
1675 next_curwin_size = (room + p_wiw + totwincount * p_wmw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001677 room -= next_curwin_size - p_wiw;
1678 }
1679 else
1680 next_curwin_size = p_wiw;
1681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001683
1684 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 --totwincount; /* don't count curwin */
1686 }
1687
1688 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1689 {
1690 n = m = 0;
1691 wincount = 1;
1692 if (fr->fr_next == NULL)
1693 /* last frame gets all that remains (avoid roundoff error) */
1694 new_size = width;
1695 else if (dir == 'v')
1696 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001697 else if (frame_fixed_width(fr))
1698 {
1699 new_size = fr->fr_newwidth;
1700 wincount = 0; /* doesn't count as a sizeable window */
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 else
1703 {
1704 /* Compute the maximum number of windows horiz. in "fr". */
1705 n = frame_minwidth(fr, NOWIN);
1706 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1707 / (p_wmw + 1);
1708 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001709 if (has_next_curwin)
1710 hnc = frame_has_win(fr, next_curwin);
1711 else
1712 hnc = FALSE;
1713 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001715 if (totwincount == 0)
1716 new_size = room;
1717 else
1718 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001720 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
1722 next_curwin_size -= p_wiw - (m - n);
1723 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001724 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001726 else
1727 room -= new_size;
1728 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001731 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 * window, unless equalizing all frames. */
1733 if (!current || dir != 'v' || topfr->fr_parent != NULL
1734 || (new_size != fr->fr_width)
1735 || frame_has_win(fr, next_curwin))
1736 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001737 new_size, height);
1738 col += new_size;
1739 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 totwincount -= wincount;
1741 }
1742 }
1743#endif
1744 else /* topfr->fr_layout == FR_COL */
1745 {
1746#ifdef FEAT_VERTSPLIT
1747 topfr->fr_width = width;
1748#endif
1749 topfr->fr_height = height;
1750
1751 if (dir != 'h') /* equalize frame heights */
1752 {
1753 /* Compute maximum number of windows vertically in this frame. */
1754 n = frame_minheight(topfr, NOWIN);
1755 /* add one for the bottom window if it doesn't have a statusline */
1756 if (row + height == cmdline_row && p_ls == 0)
1757 extra_sep = 1;
1758 else
1759 extra_sep = 0;
1760 totwincount = (n + extra_sep) / (p_wmh + 1);
1761 has_next_curwin = frame_has_win(topfr, next_curwin);
1762
1763 /*
1764 * Compute height for "next_curwin" window and room available for
1765 * other windows.
1766 * "m" is the minimal height when counting p_wh for "next_curwin".
1767 */
1768 m = frame_minheight(topfr, next_curwin);
1769 room = height - m;
1770 if (room < 0)
1771 {
1772 /* The room is less then 'winheight', use all space for the
1773 * current window. */
1774 next_curwin_size = p_wh + room;
1775 room = 0;
1776 }
1777 else
1778 {
1779 next_curwin_size = -1;
1780 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1781 {
1782 /* If 'winfixheight' set keep the window height if
1783 * possible.
1784 * Watch out for this window being the next_curwin. */
1785 if (frame_fixed_height(fr))
1786 {
1787 n = frame_minheight(fr, NOWIN);
1788 new_size = fr->fr_height;
1789 if (frame_has_win(fr, next_curwin))
1790 {
1791 room += p_wh - p_wmh;
1792 next_curwin_size = 0;
1793 if (new_size < p_wh)
1794 new_size = p_wh;
1795 }
1796 else
1797 /* These windows don't use up room. */
1798 totwincount -= (n + (fr->fr_next == NULL
1799 ? extra_sep : 0)) / (p_wmh + 1);
1800 room -= new_size - n;
1801 if (room < 0)
1802 {
1803 new_size += room;
1804 room = 0;
1805 }
1806 fr->fr_newheight = new_size;
1807 }
1808 }
1809 if (next_curwin_size == -1)
1810 {
1811 if (!has_next_curwin)
1812 next_curwin_size = 0;
1813 else if (totwincount > 1
1814 && (room + (totwincount - 2))
1815 / (totwincount - 1) > p_wh)
1816 {
1817 next_curwin_size = (room + p_wh + totwincount * p_wmh
1818 + (totwincount - 1)) / totwincount;
1819 room -= next_curwin_size - p_wh;
1820 }
1821 else
1822 next_curwin_size = p_wh;
1823 }
1824 }
1825
1826 if (has_next_curwin)
1827 --totwincount; /* don't count curwin */
1828 }
1829
1830 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1831 {
1832 n = m = 0;
1833 wincount = 1;
1834 if (fr->fr_next == NULL)
1835 /* last frame gets all that remains (avoid roundoff error) */
1836 new_size = height;
1837 else if (dir == 'h')
1838 new_size = fr->fr_height;
1839 else if (frame_fixed_height(fr))
1840 {
1841 new_size = fr->fr_newheight;
1842 wincount = 0; /* doesn't count as a sizeable window */
1843 }
1844 else
1845 {
1846 /* Compute the maximum number of windows vert. in "fr". */
1847 n = frame_minheight(fr, NOWIN);
1848 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1849 / (p_wmh + 1);
1850 m = frame_minheight(fr, next_curwin);
1851 if (has_next_curwin)
1852 hnc = frame_has_win(fr, next_curwin);
1853 else
1854 hnc = FALSE;
1855 if (hnc) /* don't count next_curwin */
1856 --wincount;
1857 if (totwincount == 0)
1858 new_size = room;
1859 else
1860 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1861 / totwincount;
1862 if (hnc) /* add next_curwin size */
1863 {
1864 next_curwin_size -= p_wh - (m - n);
1865 new_size += next_curwin_size;
1866 room -= new_size - next_curwin_size;
1867 }
1868 else
1869 room -= new_size;
1870 new_size += n;
1871 }
1872 /* Skip frame that is full width when splitting or closing a
1873 * window, unless equalizing all frames. */
1874 if (!current || dir != 'h' || topfr->fr_parent != NULL
1875 || (new_size != fr->fr_height)
1876 || frame_has_win(fr, next_curwin))
1877 win_equal_rec(next_curwin, current, fr, dir, col, row,
1878 width, new_size);
1879 row += new_size;
1880 height -= new_size;
1881 totwincount -= wincount;
1882 }
1883 }
1884}
1885
1886/*
1887 * close all windows for buffer 'buf'
1888 */
1889 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001890close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001892 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001894 win_T *wp;
1895 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001896 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001899
1900 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001902 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001904 win_close(wp, FALSE);
1905
1906 /* Start all over, autocommands may change the window layout. */
1907 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001910 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001912
1913 /* Also check windows in other tab pages. */
1914 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1915 {
1916 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001917 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001918 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1919 if (wp->w_buffer == buf)
1920 {
1921 win_close_othertab(wp, FALSE, tp);
1922
1923 /* Start all over, the tab page may be closed and
1924 * autocommands may change the window layout. */
1925 nexttp = first_tabpage;
1926 break;
1927 }
1928 }
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001931
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001932 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001933 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001937 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001938 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001939 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001940 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001941last_window()
1942{
1943 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1944}
1945
1946/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00001947 * Close window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 * If "free_buf" is TRUE related buffer may be unloaded.
1949 *
1950 * called by :quit, :close, :xit, :wq and findtag()
1951 */
1952 void
1953win_close(win, free_buf)
1954 win_T *win;
1955 int free_buf;
1956{
1957 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001958 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#ifdef FEAT_AUTOCMD
1960 int other_buffer = FALSE;
1961#endif
1962 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 int dir;
1964 int help_window = FALSE;
1965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001966 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 EMSG(_("E444: Cannot close last window"));
1969 return;
1970 }
1971
1972 /* When closing the help window, try restoring a snapshot after closing
1973 * the window. Otherwise clear the snapshot, it's now invalid. */
1974 if (win->w_buffer->b_help)
1975 help_window = TRUE;
1976 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001977 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979#ifdef FEAT_AUTOCMD
1980 if (win == curwin)
1981 {
1982 /*
1983 * Guess which window is going to be the new current window.
1984 * This may change because of the autocommands (sigh).
1985 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001986 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 /*
1989 * Be careful: If autocommands delete the window, return now.
1990 */
1991 if (wp->w_buffer != curbuf)
1992 {
1993 other_buffer = TRUE;
1994 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001995 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return;
1997 }
1998 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001999 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return;
2001# ifdef FEAT_EVAL
2002 /* autocmds may abort script processing */
2003 if (aborting())
2004 return;
2005# endif
2006 }
2007#endif
2008
2009 /*
2010 * Close the link to the buffer.
2011 */
2012 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2013 /* Autocommands may have closed the window already, or closed the only
2014 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002015 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 return;
2017
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002018 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002021 /* When closing the last window in a tab page go to another tab page. */
2022 if (wp == NULL)
2023 {
2024 tabpage_T *ptp = NULL;
2025 tabpage_T *tp;
2026 tabpage_T *atp = alt_tabpage();
2027
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002028 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002029 ptp = tp;
2030 if (tp == NULL)
2031 {
2032 EMSG2(_(e_intern2), "win_close()");
2033 return;
2034 }
2035 if (ptp == NULL)
2036 first_tabpage = tp->tp_next;
2037 else
2038 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002039 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002040
2041 /* We don't do the window resizing stuff, let enter_tabpage() take
2042 * care of entering a window in another tab page. */
2043 enter_tabpage(atp, old_curbuf);
2044 return;
2045 }
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 /* Make sure curwin isn't invalid. It can cause severe trouble when
2048 * printing an error message. For win_equal() curbuf needs to be valid
2049 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002050 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 curwin = wp;
2053#ifdef FEAT_QUICKFIX
2054 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2055 {
2056 /*
2057 * The cursor goes to the preview or the quickfix window, try
2058 * finding another window to go to.
2059 */
2060 for (;;)
2061 {
2062 if (wp->w_next == NULL)
2063 wp = firstwin;
2064 else
2065 wp = wp->w_next;
2066 if (wp == curwin)
2067 break;
2068 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2069 {
2070 curwin = wp;
2071 break;
2072 }
2073 }
2074 }
2075#endif
2076 curbuf = curwin->w_buffer;
2077 close_curwin = TRUE;
2078 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002079 if (p_ea
2080#ifdef FEAT_VERTSPLIT
2081 && (*p_ead == 'b' || *p_ead == dir)
2082#endif
2083 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 win_equal(curwin, TRUE,
2085#ifdef FEAT_VERTSPLIT
2086 dir
2087#else
2088 0
2089#endif
2090 );
2091 else
2092 win_comp_pos();
2093 if (close_curwin)
2094 {
2095 win_enter_ext(wp, FALSE, TRUE);
2096#ifdef FEAT_AUTOCMD
2097 if (other_buffer)
2098 /* careful: after this wp and win may be invalid! */
2099 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2100#endif
2101 }
2102
2103 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002104 * If last window has a status line now and we don't want one,
2105 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 */
2107 last_status(FALSE);
2108
2109 /* After closing the help window, try restoring the window layout from
2110 * before it was opened. */
2111 if (help_window)
2112 restore_snapshot(close_curwin);
2113
2114#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2115 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2116 if (gui.in_use && !win_hasvertsplit())
2117 gui_init_which_components(NULL);
2118#endif
2119
2120 redraw_all_later(NOT_VALID);
2121}
2122
2123/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124 * Close window "win" in tab page "tp", which is not the current tab page.
2125 * This may be the last window ih that tab page and result in closing the tab,
2126 * thus "tp" may become invalid!
2127 * Called must check if buffer is hidden.
2128 */
2129 void
2130win_close_othertab(win, free_buf, tp)
2131 win_T *win;
2132 int free_buf;
2133 tabpage_T *tp;
2134{
2135 win_T *wp;
2136 int dir;
2137 tabpage_T *ptp = NULL;
2138
2139 /* Close the link to the buffer. */
2140 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2141
2142 /* Careful: Autocommands may have closed the tab page or made it the
2143 * current tab page. */
2144 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2145 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002146 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002147 return;
2148
2149 /* Autocommands may have closed the window already. */
2150 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2151 ;
2152 if (wp == NULL)
2153 return;
2154
2155 /* Free the memory used for the window. */
2156 wp = win_free_mem(win, &dir, tp);
2157
2158 /* When closing the last window in a tab page remove the tab page. */
2159 if (wp == NULL)
2160 {
2161 if (tp == first_tabpage)
2162 first_tabpage = tp->tp_next;
2163 else
2164 {
2165 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2166 ptp = ptp->tp_next)
2167 ;
2168 if (ptp == NULL)
2169 {
2170 EMSG2(_(e_intern2), "win_close_othertab()");
2171 return;
2172 }
2173 ptp->tp_next = tp->tp_next;
2174 }
2175 vim_free(tp);
2176 }
2177}
2178
2179/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002180 * Free the memory used for a window.
2181 * Returns a pointer to the window that got the freed up space.
2182 */
2183 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002184win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002185 win_T *win;
2186 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002187 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002188{
2189 frame_T *frp;
2190 win_T *wp;
2191
Bram Moolenaarea408852005-06-25 22:49:46 +00002192#ifdef FEAT_FOLDING
2193 clearFolding(win);
2194#endif
2195
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002196 /* reduce the reference count to the argument list. */
2197 alist_unlink(win->w_alist);
2198
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002199 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002200 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002201 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002202 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002203 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002204
2205 return wp;
2206}
2207
2208#if defined(EXITFREE) || defined(PROTO)
2209 void
2210win_free_all()
2211{
2212 int dummy;
2213
Bram Moolenaarf740b292006-02-16 22:11:02 +00002214# ifdef FEAT_WINDOWS
2215 while (first_tabpage->tp_next != NULL)
2216 tabpage_close(TRUE);
2217# endif
2218
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002219 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002220 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002221}
2222#endif
2223
2224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * Remove a window and its frame from the tree of frames.
2226 * Returns a pointer to the window that got the freed up space.
2227 */
2228/*ARGSUSED*/
2229 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002230winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 win_T *win;
2232 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002233 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 frame_T *frp, *frp2, *frp3;
2236 frame_T *frp_close = win->w_frame;
2237 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002238 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241 * If there is only one window there is nothing to remove.
2242 */
2243 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2244 return NULL;
2245
2246 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * Remove the window from its frame.
2248 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002249 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 wp = frame2win(frp2);
2251
2252 /* Remove this frame from the list of frames. */
2253 frame_remove(frp_close);
2254
2255#ifdef FEAT_VERTSPLIT
2256 if (frp_close->fr_parent->fr_layout == FR_COL)
2257 {
2258#endif
2259 /* When 'winfixheight' is set, remember its old size and restore
2260 * it later (it's a simplistic solution...). Don't do this if the
2261 * window will occupy the full height of the screen. */
2262 if (frp2->fr_win != NULL
2263 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2264 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002265 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2267 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002268 if (old_size != 0)
2269 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#ifdef FEAT_VERTSPLIT
2271 *dirp = 'v';
2272 }
2273 else
2274 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002275 /* When 'winfixwidth' is set, remember its old size and restore
2276 * it later (it's a simplistic solution...). Don't do this if the
2277 * window will occupy the full width of the screen. */
2278 if (frp2->fr_win != NULL
2279 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2280 && frp2->fr_win->w_p_wfw)
2281 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002283 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2284 if (old_size != 0)
2285 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 *dirp = 'h';
2287 }
2288#endif
2289
2290 /* If rows/columns go to a window below/right its positions need to be
2291 * updated. Can only be done after the sizes have been updated. */
2292 if (frp2 == frp_close->fr_next)
2293 {
2294 int row = win->w_winrow;
2295 int col = W_WINCOL(win);
2296
2297 frame_comp_pos(frp2, &row, &col);
2298 }
2299
2300 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2301 {
2302 /* There is no other frame in this list, move its info to the parent
2303 * and remove it. */
2304 frp2->fr_parent->fr_layout = frp2->fr_layout;
2305 frp2->fr_parent->fr_child = frp2->fr_child;
2306 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2307 frp->fr_parent = frp2->fr_parent;
2308 frp2->fr_parent->fr_win = frp2->fr_win;
2309 if (frp2->fr_win != NULL)
2310 frp2->fr_win->w_frame = frp2->fr_parent;
2311 frp = frp2->fr_parent;
2312 vim_free(frp2);
2313
2314 frp2 = frp->fr_parent;
2315 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2316 {
2317 /* The frame above the parent has the same layout, have to merge
2318 * the frames into this list. */
2319 if (frp2->fr_child == frp)
2320 frp2->fr_child = frp->fr_child;
2321 frp->fr_child->fr_prev = frp->fr_prev;
2322 if (frp->fr_prev != NULL)
2323 frp->fr_prev->fr_next = frp->fr_child;
2324 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2325 {
2326 frp3->fr_parent = frp2;
2327 if (frp3->fr_next == NULL)
2328 {
2329 frp3->fr_next = frp->fr_next;
2330 if (frp->fr_next != NULL)
2331 frp->fr_next->fr_prev = frp3;
2332 break;
2333 }
2334 }
2335 vim_free(frp);
2336 }
2337 }
2338
2339 return wp;
2340}
2341
2342/*
2343 * Find out which frame is going to get the freed up space when "win" is
2344 * closed.
2345 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2346 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2347 * This makes opening a window and closing it immediately keep the same window
2348 * layout.
2349 */
2350 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002351win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002353 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 frame_T *frp;
2356 int b;
2357
Bram Moolenaarf740b292006-02-16 22:11:02 +00002358 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002359 /* Last window in this tab page, will go to next tab page. */
2360 return alt_tabpage()->tp_curwin->w_frame;
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 frp = win->w_frame;
2363#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002364 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 b = p_spr;
2366 else
2367#endif
2368 b = p_sb;
2369 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2370 return frp->fr_next;
2371 return frp->fr_prev;
2372}
2373
2374/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002375 * Return the tabpage that will be used if the current one is closed.
2376 */
2377 static tabpage_T *
2378alt_tabpage()
2379{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002380 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002381
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002382 /* Use the next tab page if possible. */
2383 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002384 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002385
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002386 /* Find the last but one tab page. */
2387 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2388 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002389 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390}
2391
2392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 * Find the left-upper window in frame "frp".
2394 */
2395 static win_T *
2396frame2win(frp)
2397 frame_T *frp;
2398{
2399 while (frp->fr_win == NULL)
2400 frp = frp->fr_child;
2401 return frp->fr_win;
2402}
2403
2404/*
2405 * Return TRUE if frame "frp" contains window "wp".
2406 */
2407 static int
2408frame_has_win(frp, wp)
2409 frame_T *frp;
2410 win_T *wp;
2411{
2412 frame_T *p;
2413
2414 if (frp->fr_layout == FR_LEAF)
2415 return frp->fr_win == wp;
2416
2417 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2418 if (frame_has_win(p, wp))
2419 return TRUE;
2420 return FALSE;
2421}
2422
2423/*
2424 * Set a new height for a frame. Recursively sets the height for contained
2425 * frames and windows. Caller must take care of positions.
2426 */
2427 static void
2428frame_new_height(topfrp, height, topfirst, wfh)
2429 frame_T *topfrp;
2430 int height;
2431 int topfirst; /* resize topmost contained frame first */
2432 int wfh; /* obey 'winfixheight' when there is a choice;
2433 may cause the height not to be set */
2434{
2435 frame_T *frp;
2436 int extra_lines;
2437 int h;
2438
2439 if (topfrp->fr_win != NULL)
2440 {
2441 /* Simple case: just one window. */
2442 win_new_height(topfrp->fr_win,
2443 height - topfrp->fr_win->w_status_height);
2444 }
2445#ifdef FEAT_VERTSPLIT
2446 else if (topfrp->fr_layout == FR_ROW)
2447 {
2448 do
2449 {
2450 /* All frames in this row get the same new height. */
2451 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2452 {
2453 frame_new_height(frp, height, topfirst, wfh);
2454 if (frp->fr_height > height)
2455 {
2456 /* Could not fit the windows, make the whole row higher. */
2457 height = frp->fr_height;
2458 break;
2459 }
2460 }
2461 }
2462 while (frp != NULL);
2463 }
2464#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002465 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467 /* Complicated case: Resize a column of frames. Resize the bottom
2468 * frame first, frames above that when needed. */
2469
2470 frp = topfrp->fr_child;
2471 if (wfh)
2472 /* Advance past frames with one window with 'wfh' set. */
2473 while (frame_fixed_height(frp))
2474 {
2475 frp = frp->fr_next;
2476 if (frp == NULL)
2477 return; /* no frame without 'wfh', give up */
2478 }
2479 if (!topfirst)
2480 {
2481 /* Find the bottom frame of this column */
2482 while (frp->fr_next != NULL)
2483 frp = frp->fr_next;
2484 if (wfh)
2485 /* Advance back for frames with one window with 'wfh' set. */
2486 while (frame_fixed_height(frp))
2487 frp = frp->fr_prev;
2488 }
2489
2490 extra_lines = height - topfrp->fr_height;
2491 if (extra_lines < 0)
2492 {
2493 /* reduce height of contained frames, bottom or top frame first */
2494 while (frp != NULL)
2495 {
2496 h = frame_minheight(frp, NULL);
2497 if (frp->fr_height + extra_lines < h)
2498 {
2499 extra_lines += frp->fr_height - h;
2500 frame_new_height(frp, h, topfirst, wfh);
2501 }
2502 else
2503 {
2504 frame_new_height(frp, frp->fr_height + extra_lines,
2505 topfirst, wfh);
2506 break;
2507 }
2508 if (topfirst)
2509 {
2510 do
2511 frp = frp->fr_next;
2512 while (wfh && frp != NULL && frame_fixed_height(frp));
2513 }
2514 else
2515 {
2516 do
2517 frp = frp->fr_prev;
2518 while (wfh && frp != NULL && frame_fixed_height(frp));
2519 }
2520 /* Increase "height" if we could not reduce enough frames. */
2521 if (frp == NULL)
2522 height -= extra_lines;
2523 }
2524 }
2525 else if (extra_lines > 0)
2526 {
2527 /* increase height of bottom or top frame */
2528 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2529 }
2530 }
2531 topfrp->fr_height = height;
2532}
2533
2534/*
2535 * Return TRUE if height of frame "frp" should not be changed because of
2536 * the 'winfixheight' option.
2537 */
2538 static int
2539frame_fixed_height(frp)
2540 frame_T *frp;
2541{
2542 /* frame with one window: fixed height if 'winfixheight' set. */
2543 if (frp->fr_win != NULL)
2544 return frp->fr_win->w_p_wfh;
2545
2546 if (frp->fr_layout == FR_ROW)
2547 {
2548 /* The frame is fixed height if one of the frames in the row is fixed
2549 * height. */
2550 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2551 if (frame_fixed_height(frp))
2552 return TRUE;
2553 return FALSE;
2554 }
2555
2556 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2557 * frames in the row are fixed height. */
2558 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2559 if (!frame_fixed_height(frp))
2560 return FALSE;
2561 return TRUE;
2562}
2563
2564#ifdef FEAT_VERTSPLIT
2565/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002566 * Return TRUE if width of frame "frp" should not be changed because of
2567 * the 'winfixwidth' option.
2568 */
2569 static int
2570frame_fixed_width(frp)
2571 frame_T *frp;
2572{
2573 /* frame with one window: fixed width if 'winfixwidth' set. */
2574 if (frp->fr_win != NULL)
2575 return frp->fr_win->w_p_wfw;
2576
2577 if (frp->fr_layout == FR_COL)
2578 {
2579 /* The frame is fixed width if one of the frames in the row is fixed
2580 * width. */
2581 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2582 if (frame_fixed_width(frp))
2583 return TRUE;
2584 return FALSE;
2585 }
2586
2587 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2588 * frames in the row are fixed width. */
2589 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2590 if (!frame_fixed_width(frp))
2591 return FALSE;
2592 return TRUE;
2593}
2594
2595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * Add a status line to windows at the bottom of "frp".
2597 * Note: Does not check if there is room!
2598 */
2599 static void
2600frame_add_statusline(frp)
2601 frame_T *frp;
2602{
2603 win_T *wp;
2604
2605 if (frp->fr_layout == FR_LEAF)
2606 {
2607 wp = frp->fr_win;
2608 if (wp->w_status_height == 0)
2609 {
2610 if (wp->w_height > 0) /* don't make it negative */
2611 --wp->w_height;
2612 wp->w_status_height = STATUS_HEIGHT;
2613 }
2614 }
2615 else if (frp->fr_layout == FR_ROW)
2616 {
2617 /* Handle all the frames in the row. */
2618 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2619 frame_add_statusline(frp);
2620 }
2621 else /* frp->fr_layout == FR_COL */
2622 {
2623 /* Only need to handle the last frame in the column. */
2624 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2625 ;
2626 frame_add_statusline(frp);
2627 }
2628}
2629
2630/*
2631 * Set width of a frame. Handles recursively going through contained frames.
2632 * May remove separator line for windows at the right side (for win_close()).
2633 */
2634 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002635frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 frame_T *topfrp;
2637 int width;
2638 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002639 int wfw; /* obey 'winfixwidth' when there is a choice;
2640 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 frame_T *frp;
2643 int extra_cols;
2644 int w;
2645 win_T *wp;
2646
2647 if (topfrp->fr_layout == FR_LEAF)
2648 {
2649 /* Simple case: just one window. */
2650 wp = topfrp->fr_win;
2651 /* Find out if there are any windows right of this one. */
2652 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2653 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2654 break;
2655 if (frp->fr_parent == NULL)
2656 wp->w_vsep_width = 0;
2657 win_new_width(wp, width - wp->w_vsep_width);
2658 }
2659 else if (topfrp->fr_layout == FR_COL)
2660 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002661 do
2662 {
2663 /* All frames in this column get the same new width. */
2664 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2665 {
2666 frame_new_width(frp, width, leftfirst, wfw);
2667 if (frp->fr_width > width)
2668 {
2669 /* Could not fit the windows, make whole column wider. */
2670 width = frp->fr_width;
2671 break;
2672 }
2673 }
2674 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
2676 else /* fr_layout == FR_ROW */
2677 {
2678 /* Complicated case: Resize a row of frames. Resize the rightmost
2679 * frame first, frames left of it when needed. */
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002682 if (wfw)
2683 /* Advance past frames with one window with 'wfw' set. */
2684 while (frame_fixed_width(frp))
2685 {
2686 frp = frp->fr_next;
2687 if (frp == NULL)
2688 return; /* no frame without 'wfw', give up */
2689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002691 {
2692 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 while (frp->fr_next != NULL)
2694 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002695 if (wfw)
2696 /* Advance back for frames with one window with 'wfw' set. */
2697 while (frame_fixed_width(frp))
2698 frp = frp->fr_prev;
2699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 extra_cols = width - topfrp->fr_width;
2702 if (extra_cols < 0)
2703 {
2704 /* reduce frame width, rightmost frame first */
2705 while (frp != NULL)
2706 {
2707 w = frame_minwidth(frp, NULL);
2708 if (frp->fr_width + extra_cols < w)
2709 {
2710 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002711 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002715 frame_new_width(frp, frp->fr_width + extra_cols,
2716 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 break;
2718 }
2719 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002720 {
2721 do
2722 frp = frp->fr_next;
2723 while (wfw && frp != NULL && frame_fixed_width(frp));
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002726 {
2727 do
2728 frp = frp->fr_prev;
2729 while (wfw && frp != NULL && frame_fixed_width(frp));
2730 }
2731 /* Increase "width" if we could not reduce enough frames. */
2732 if (frp == NULL)
2733 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 }
2735 }
2736 else if (extra_cols > 0)
2737 {
2738 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002739 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 }
2742 topfrp->fr_width = width;
2743}
2744
2745/*
2746 * Add the vertical separator to windows at the right side of "frp".
2747 * Note: Does not check if there is room!
2748 */
2749 static void
2750frame_add_vsep(frp)
2751 frame_T *frp;
2752{
2753 win_T *wp;
2754
2755 if (frp->fr_layout == FR_LEAF)
2756 {
2757 wp = frp->fr_win;
2758 if (wp->w_vsep_width == 0)
2759 {
2760 if (wp->w_width > 0) /* don't make it negative */
2761 --wp->w_width;
2762 wp->w_vsep_width = 1;
2763 }
2764 }
2765 else if (frp->fr_layout == FR_COL)
2766 {
2767 /* Handle all the frames in the column. */
2768 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2769 frame_add_vsep(frp);
2770 }
2771 else /* frp->fr_layout == FR_ROW */
2772 {
2773 /* Only need to handle the last frame in the row. */
2774 frp = frp->fr_child;
2775 while (frp->fr_next != NULL)
2776 frp = frp->fr_next;
2777 frame_add_vsep(frp);
2778 }
2779}
2780
2781/*
2782 * Set frame width from the window it contains.
2783 */
2784 static void
2785frame_fix_width(wp)
2786 win_T *wp;
2787{
2788 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2789}
2790#endif
2791
2792/*
2793 * Set frame height from the window it contains.
2794 */
2795 static void
2796frame_fix_height(wp)
2797 win_T *wp;
2798{
2799 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2800}
2801
2802/*
2803 * Compute the minimal height for frame "topfrp".
2804 * Uses the 'winminheight' option.
2805 * When "next_curwin" isn't NULL, use p_wh for this window.
2806 * When "next_curwin" is NOWIN, don't use at least one line for the current
2807 * window.
2808 */
2809 static int
2810frame_minheight(topfrp, next_curwin)
2811 frame_T *topfrp;
2812 win_T *next_curwin;
2813{
2814 frame_T *frp;
2815 int m;
2816#ifdef FEAT_VERTSPLIT
2817 int n;
2818#endif
2819
2820 if (topfrp->fr_win != NULL)
2821 {
2822 if (topfrp->fr_win == next_curwin)
2823 m = p_wh + topfrp->fr_win->w_status_height;
2824 else
2825 {
2826 /* window: minimal height of the window plus status line */
2827 m = p_wmh + topfrp->fr_win->w_status_height;
2828 /* Current window is minimal one line high */
2829 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2830 ++m;
2831 }
2832 }
2833#ifdef FEAT_VERTSPLIT
2834 else if (topfrp->fr_layout == FR_ROW)
2835 {
2836 /* get the minimal height from each frame in this row */
2837 m = 0;
2838 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2839 {
2840 n = frame_minheight(frp, next_curwin);
2841 if (n > m)
2842 m = n;
2843 }
2844 }
2845#endif
2846 else
2847 {
2848 /* Add up the minimal heights for all frames in this column. */
2849 m = 0;
2850 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2851 m += frame_minheight(frp, next_curwin);
2852 }
2853
2854 return m;
2855}
2856
2857#ifdef FEAT_VERTSPLIT
2858/*
2859 * Compute the minimal width for frame "topfrp".
2860 * When "next_curwin" isn't NULL, use p_wiw for this window.
2861 * When "next_curwin" is NOWIN, don't use at least one column for the current
2862 * window.
2863 */
2864 static int
2865frame_minwidth(topfrp, next_curwin)
2866 frame_T *topfrp;
2867 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2868{
2869 frame_T *frp;
2870 int m, n;
2871
2872 if (topfrp->fr_win != NULL)
2873 {
2874 if (topfrp->fr_win == next_curwin)
2875 m = p_wiw + topfrp->fr_win->w_vsep_width;
2876 else
2877 {
2878 /* window: minimal width of the window plus separator column */
2879 m = p_wmw + topfrp->fr_win->w_vsep_width;
2880 /* Current window is minimal one column wide */
2881 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2882 ++m;
2883 }
2884 }
2885 else if (topfrp->fr_layout == FR_COL)
2886 {
2887 /* get the minimal width from each frame in this column */
2888 m = 0;
2889 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2890 {
2891 n = frame_minwidth(frp, next_curwin);
2892 if (n > m)
2893 m = n;
2894 }
2895 }
2896 else
2897 {
2898 /* Add up the minimal widths for all frames in this row. */
2899 m = 0;
2900 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2901 m += frame_minwidth(frp, next_curwin);
2902 }
2903
2904 return m;
2905}
2906#endif
2907
2908
2909/*
2910 * Try to close all windows except current one.
2911 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2912 * used and the buffer was modified.
2913 *
2914 * Used by ":bdel" and ":only".
2915 */
2916 void
2917close_others(message, forceit)
2918 int message;
2919 int forceit; /* always hide all other windows */
2920{
2921 win_T *wp;
2922 win_T *nextwp;
2923 int r;
2924
2925 if (lastwin == firstwin)
2926 {
2927 if (message
2928#ifdef FEAT_AUTOCMD
2929 && !autocmd_busy
2930#endif
2931 )
2932 MSG(_("Already only one window"));
2933 return;
2934 }
2935
2936 /* Be very careful here: autocommands may change the window layout. */
2937 for (wp = firstwin; win_valid(wp); wp = nextwp)
2938 {
2939 nextwp = wp->w_next;
2940 if (wp != curwin) /* don't close current window */
2941 {
2942
2943 /* Check if it's allowed to abandon this window */
2944 r = can_abandon(wp->w_buffer, forceit);
2945#ifdef FEAT_AUTOCMD
2946 if (!win_valid(wp)) /* autocommands messed wp up */
2947 {
2948 nextwp = firstwin;
2949 continue;
2950 }
2951#endif
2952 if (!r)
2953 {
2954#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2955 if (message && (p_confirm || cmdmod.confirm) && p_write)
2956 {
2957 dialog_changed(wp->w_buffer, FALSE);
2958# ifdef FEAT_AUTOCMD
2959 if (!win_valid(wp)) /* autocommands messed wp up */
2960 {
2961 nextwp = firstwin;
2962 continue;
2963 }
2964# endif
2965 }
2966 if (bufIsChanged(wp->w_buffer))
2967#endif
2968 continue;
2969 }
2970 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2971 }
2972 }
2973
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002974 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 EMSG(_("E445: Other window contains changes"));
2976}
2977
2978#endif /* FEAT_WINDOWS */
2979
2980/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002981 * Init the current window "curwin".
2982 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
2984 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002985curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002987 redraw_win_later(curwin, NOT_VALID);
2988 curwin->w_lines_valid = 0;
2989 curwin->w_cursor.lnum = 1;
2990 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002992 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002994 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2995 curwin->w_pcmark.col = 0;
2996 curwin->w_prev_pcmark.lnum = 0;
2997 curwin->w_prev_pcmark.col = 0;
2998 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003000 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003002 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003#ifdef FEAT_FKMAP
3004 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003005 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003007 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#endif
3009}
3010
3011/*
3012 * Allocate the first window and put an empty buffer in it.
3013 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003014 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003016 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017win_alloc_first()
3018{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003019 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003020 return FAIL;
3021
3022#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003023 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003024 if (first_tabpage == NULL)
3025 return FAIL;
3026 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003027 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003028#endif
3029 return OK;
3030}
3031
3032/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003033 * Allocate the first window or the first window in a new tab page.
3034 * When "oldwin" is NULL create an empty buffer for it.
3035 * When "oldwin" is not NULL copy info from it to the new window (only with
3036 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003037 * Return FAIL when something goes wrong (out of memory).
3038 */
3039 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003040win_alloc_firstwin(oldwin)
3041 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003042{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003044 if (oldwin == NULL)
3045 {
3046 /* Very first window, need to create an empty buffer for it and
3047 * initialize from scratch. */
3048 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3049 if (curwin == NULL || curbuf == NULL)
3050 return FAIL;
3051 curwin->w_buffer = curbuf;
3052 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003054 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003056 curwin_init(); /* init current window */
3057 }
3058#ifdef FEAT_WINDOWS
3059 else
3060 {
3061 /* First window in new tab page, initialize it from "oldwin". */
3062 win_init(curwin, oldwin);
3063
3064# ifdef FEAT_SCROLLBIND
3065 /* We don't want scroll-binding in the first window. */
3066 curwin->w_p_scb = FALSE;
3067# endif
3068 }
3069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3072 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003073 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 topframe->fr_layout = FR_LEAF;
3075#ifdef FEAT_VERTSPLIT
3076 topframe->fr_width = Columns;
3077#endif
3078 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003079#ifdef FEAT_WINDOWS
3080 p_ch_used = p_ch;
3081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 topframe->fr_win = curwin;
3083 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084
3085 return OK;
3086}
3087
3088/*
3089 * Initialize the window and frame size to the maximum.
3090 */
3091 void
3092win_init_size()
3093{
3094 firstwin->w_height = ROWS_AVAIL;
3095 topframe->fr_height = ROWS_AVAIL;
3096#ifdef FEAT_VERTSPLIT
3097 firstwin->w_width = Columns;
3098 topframe->fr_width = Columns;
3099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100}
3101
3102#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003103
3104/*
3105 * Allocate a new tabpage_T and init the values.
3106 * Returns NULL when out of memory.
3107 */
3108 static tabpage_T *
3109alloc_tabpage()
3110{
3111 tabpage_T *tp;
3112
3113 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3114 if (tp != NULL)
3115 {
3116# ifdef FEAT_DIFF
3117 tp->tp_diff_invalid = TRUE;
3118# endif
3119 }
3120 return tp;
3121}
3122
3123 static void
3124free_tabpage(tp)
3125 tabpage_T *tp;
3126{
3127# ifdef FEAT_DIFF
3128 diff_clear(tp);
3129# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003130 clear_snapshot(tp);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003131 vim_free(tp);
3132}
3133
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003135 * Create a new Tab page with one window.
3136 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003137 * When "after" is 0 put it just after the current Tab page.
3138 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003139 * Return FAIL or OK.
3140 */
3141 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003142win_new_tabpage(after)
3143 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003144{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003145 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003146 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003147 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003148
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003149 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003150 if (newtp == NULL)
3151 return FAIL;
3152
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003153 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003154 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003155 {
3156 vim_free(newtp);
3157 return FAIL;
3158 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003159 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160
3161 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003162 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003163 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003165 if (after == 1)
3166 {
3167 /* New tab page becomes the first one. */
3168 newtp->tp_next = first_tabpage;
3169 first_tabpage = newtp;
3170 }
3171 else
3172 {
3173 if (after > 0)
3174 {
3175 /* Put new tab page before tab page "after". */
3176 n = 2;
3177 for (tp = first_tabpage; tp->tp_next != NULL
3178 && n < after; tp = tp->tp_next)
3179 ++n;
3180 }
3181 newtp->tp_next = tp->tp_next;
3182 tp->tp_next = newtp;
3183 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003185 firstwin->w_winrow = tabline_height();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003186
3187 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003188 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003189 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003190#ifdef FEAT_AUTOCMD
3191 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3192 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3193#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003194 return OK;
3195 }
3196
3197 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003198 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199 return FAIL;
3200}
3201
3202/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003203 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3204 * like with ":split".
3205 * Returns OK if a new tab page was created, FAIL otherwise.
3206 */
3207 int
3208may_open_tabpage()
3209{
3210 int n = cmdmod.tab;
3211
3212 if (cmdmod.tab != 0)
3213 {
3214 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3215 return win_new_tabpage(n);
3216 }
3217 return FAIL;
3218}
3219
3220/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003221 * Create up to "maxcount" tabpages with empty windows.
3222 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003223 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003224 int
3225make_tabpages(maxcount)
3226 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003227{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003228 int count = maxcount;
3229 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003231 /* Limit to 'tabpagemax' tabs. */
3232 if (count > p_tpm)
3233 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003234
3235#ifdef FEAT_AUTOCMD
3236 /*
3237 * Don't execute autocommands while creating the tab pages. Must do that
3238 * when putting the buffers in the windows.
3239 */
3240 ++autocmd_block;
3241#endif
3242
3243 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003244 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003245 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003246
3247#ifdef FEAT_AUTOCMD
3248 --autocmd_block;
3249#endif
3250
3251 /* return actual number of tab pages */
3252 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003253}
3254
3255/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003256 * Return TRUE when "tpc" points to a valid tab page.
3257 */
3258 int
3259valid_tabpage(tpc)
3260 tabpage_T *tpc;
3261{
3262 tabpage_T *tp;
3263
3264 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3265 if (tp == tpc)
3266 return TRUE;
3267 return FALSE;
3268}
3269
3270/*
3271 * Find tab page "n" (first one is 1). Returns NULL when not found.
3272 */
3273 tabpage_T *
3274find_tabpage(n)
3275 int n;
3276{
3277 tabpage_T *tp;
3278 int i = 1;
3279
3280 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3281 ++i;
3282 return tp;
3283}
3284
3285/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003286 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003287 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003288 */
3289 int
3290tabpage_index(ftp)
3291 tabpage_T *ftp;
3292{
3293 int i = 1;
3294 tabpage_T *tp;
3295
3296 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3297 ++i;
3298 return i;
3299}
3300
3301/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003302 * Prepare for leaving the current tab page.
3303 * When autocomands change "curtab" we don't leave the tab page and return
3304 * FAIL.
3305 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003306 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003307/*ARGSUSED*/
3308 static int
3309leave_tabpage(new_curbuf)
3310 buf_T *new_curbuf; /* what is going to be the new curbuf,
3311 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003312{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003313 tabpage_T *tp = curtab;
3314
3315#ifdef FEAT_AUTOCMD
3316 if (new_curbuf != curbuf)
3317 {
3318 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3319 if (curtab != tp)
3320 return FAIL;
3321 }
3322 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3323 if (curtab != tp)
3324 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003325 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003326 if (curtab != tp)
3327 return FAIL;
3328#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003329#if defined(FEAT_GUI)
3330 /* Remove the scrollbars. They may be added back later. */
3331 if (gui.in_use)
3332 gui_remove_scrollbars();
3333#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003335 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003336 tp->tp_firstwin = firstwin;
3337 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003338 tp->tp_old_Rows = Rows;
3339 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003340 firstwin = NULL;
3341 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003342 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343}
3344
3345/*
3346 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003348 */
3349/*ARGSUSED*/
3350 static void
3351enter_tabpage(tp, old_curbuf)
3352 tabpage_T *tp;
3353 buf_T *old_curbuf;
3354{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003355 int old_off = tp->tp_firstwin->w_winrow;
3356
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003357 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358 firstwin = tp->tp_firstwin;
3359 lastwin = tp->tp_lastwin;
3360 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003361#ifdef FEAT_AUTOCMD
3362 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3363#endif
3364
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003365 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003366 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003367
3368#ifdef FEAT_AUTOCMD
3369 if (old_curbuf != curbuf)
3370 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3371#endif
3372
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003373 last_status(FALSE); /* status line may appear or disappear */
3374 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003375 must_redraw = CLEAR; /* need to redraw everything */
3376#ifdef FEAT_DIFF
3377 diff_need_scrollbind = TRUE;
3378#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003380 /* The tabpage line may have appeared or disappeared, may need to resize
3381 * the frames for that. When the Vim window was resized need to update
3382 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003383 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3384#ifdef FEAT_GUI_TABLINE
3385 && !gui_use_tabline()
3386#endif
3387 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003388 shell_new_rows();
3389#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003390 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003391 shell_new_columns(); /* update window widths */
3392#endif
3393
3394#if defined(FEAT_GUI)
3395 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3396 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003397 if (gui.in_use && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003398 gui_update_scrollbars(TRUE);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003399 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003400#endif
3401
3402 redraw_all_later(CLEAR);
3403}
3404
3405/*
3406 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003407 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 */
3409 void
3410goto_tabpage(n)
3411 int n;
3412{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003413 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003414 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415 int i;
3416
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 /* If there is only one it can't work. */
3418 if (first_tabpage->tp_next == NULL)
3419 {
3420 if (n > 1)
3421 beep_flush();
3422 return;
3423 }
3424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425 if (n == 0)
3426 {
3427 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003428 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429 tp = first_tabpage;
3430 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003431 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003433 else if (n < 0)
3434 {
3435 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3436 * this N times. */
3437 ttp = curtab;
3438 for (i = n; i < 0; ++i)
3439 {
3440 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3441 tp = tp->tp_next)
3442 ;
3443 ttp = tp;
3444 }
3445 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003446 else if (n == 9999)
3447 {
3448 /* Go to last tab page. */
3449 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3450 ;
3451 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452 else
3453 {
3454 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003455 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003456 if (tp == NULL)
3457 {
3458 beep_flush();
3459 return;
3460 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003461 }
3462
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003463 goto_tabpage_tp(tp);
3464
3465#ifdef FEAT_GUI_TABLINE
3466 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003467 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468#endif
3469}
3470
3471/*
3472 * Go to tabpage "tp".
3473 * Note: doesn't update the GUI tab.
3474 */
3475 void
3476goto_tabpage_tp(tp)
3477 tabpage_T *tp;
3478{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003479 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003480 {
3481 if (valid_tabpage(tp))
3482 enter_tabpage(tp, curbuf);
3483 else
3484 enter_tabpage(curtab, curbuf);
3485 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003489 * Move the current tab page to before tab page "nr".
3490 */
3491 void
3492tabpage_move(nr)
3493 int nr;
3494{
3495 int n = nr;
3496 tabpage_T *tp;
3497
3498 if (first_tabpage->tp_next == NULL)
3499 return;
3500
3501 /* Remove the current tab page from the list of tab pages. */
3502 if (curtab == first_tabpage)
3503 first_tabpage = curtab->tp_next;
3504 else
3505 {
3506 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3507 if (tp->tp_next == curtab)
3508 break;
3509 if (tp == NULL) /* "cannot happen" */
3510 return;
3511 tp->tp_next = curtab->tp_next;
3512 }
3513
3514 /* Re-insert it at the specified position. */
3515 if (n == 0)
3516 {
3517 curtab->tp_next = first_tabpage;
3518 first_tabpage = curtab;
3519 }
3520 else
3521 {
3522 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3523 --n;
3524 curtab->tp_next = tp->tp_next;
3525 tp->tp_next = curtab;
3526 }
3527
3528 /* Need to redraw the tabline. Tab page contents doesn't change. */
3529 redraw_tabline = TRUE;
3530}
3531
3532
3533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * Go to another window.
3535 * When jumping to another buffer, stop Visual mode. Do this before
3536 * changing windows so we can yank the selection into the '*' register.
3537 * When jumping to another window on the same buffer, adjust its cursor
3538 * position to keep the same Visual area.
3539 */
3540 void
3541win_goto(wp)
3542 win_T *wp;
3543{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003544 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003547 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 return;
3549 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef FEAT_VISUAL
3552 if (wp->w_buffer != curbuf)
3553 reset_VIsual_and_resel();
3554 else if (VIsual_active)
3555 wp->w_cursor = curwin->w_cursor;
3556#endif
3557
3558#ifdef FEAT_GUI
3559 need_mouse_correct = TRUE;
3560#endif
3561 win_enter(wp, TRUE);
3562}
3563
3564#if defined(FEAT_PERL) || defined(PROTO)
3565/*
3566 * Find window number "winnr" (counting top to bottom).
3567 */
3568 win_T *
3569win_find_nr(winnr)
3570 int winnr;
3571{
3572 win_T *wp;
3573
3574# ifdef FEAT_WINDOWS
3575 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3576 if (--winnr == 0)
3577 break;
3578 return wp;
3579# else
3580 return curwin;
3581# endif
3582}
3583#endif
3584
3585#ifdef FEAT_VERTSPLIT
3586/*
3587 * Move to window above or below "count" times.
3588 */
3589 static void
3590win_goto_ver(up, count)
3591 int up; /* TRUE to go to win above */
3592 long count;
3593{
3594 frame_T *fr;
3595 frame_T *nfr;
3596 frame_T *foundfr;
3597
3598 foundfr = curwin->w_frame;
3599 while (count--)
3600 {
3601 /*
3602 * First go upwards in the tree of frames until we find a upwards or
3603 * downwards neighbor.
3604 */
3605 fr = foundfr;
3606 for (;;)
3607 {
3608 if (fr == topframe)
3609 goto end;
3610 if (up)
3611 nfr = fr->fr_prev;
3612 else
3613 nfr = fr->fr_next;
3614 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3615 break;
3616 fr = fr->fr_parent;
3617 }
3618
3619 /*
3620 * Now go downwards to find the bottom or top frame in it.
3621 */
3622 for (;;)
3623 {
3624 if (nfr->fr_layout == FR_LEAF)
3625 {
3626 foundfr = nfr;
3627 break;
3628 }
3629 fr = nfr->fr_child;
3630 if (nfr->fr_layout == FR_ROW)
3631 {
3632 /* Find the frame at the cursor row. */
3633 while (fr->fr_next != NULL
3634 && frame2win(fr)->w_wincol + fr->fr_width
3635 <= curwin->w_wincol + curwin->w_wcol)
3636 fr = fr->fr_next;
3637 }
3638 if (nfr->fr_layout == FR_COL && up)
3639 while (fr->fr_next != NULL)
3640 fr = fr->fr_next;
3641 nfr = fr;
3642 }
3643 }
3644end:
3645 if (foundfr != NULL)
3646 win_goto(foundfr->fr_win);
3647}
3648
3649/*
3650 * Move to left or right window.
3651 */
3652 static void
3653win_goto_hor(left, count)
3654 int left; /* TRUE to go to left win */
3655 long count;
3656{
3657 frame_T *fr;
3658 frame_T *nfr;
3659 frame_T *foundfr;
3660
3661 foundfr = curwin->w_frame;
3662 while (count--)
3663 {
3664 /*
3665 * First go upwards in the tree of frames until we find a left or
3666 * right neighbor.
3667 */
3668 fr = foundfr;
3669 for (;;)
3670 {
3671 if (fr == topframe)
3672 goto end;
3673 if (left)
3674 nfr = fr->fr_prev;
3675 else
3676 nfr = fr->fr_next;
3677 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3678 break;
3679 fr = fr->fr_parent;
3680 }
3681
3682 /*
3683 * Now go downwards to find the leftmost or rightmost frame in it.
3684 */
3685 for (;;)
3686 {
3687 if (nfr->fr_layout == FR_LEAF)
3688 {
3689 foundfr = nfr;
3690 break;
3691 }
3692 fr = nfr->fr_child;
3693 if (nfr->fr_layout == FR_COL)
3694 {
3695 /* Find the frame at the cursor row. */
3696 while (fr->fr_next != NULL
3697 && frame2win(fr)->w_winrow + fr->fr_height
3698 <= curwin->w_winrow + curwin->w_wrow)
3699 fr = fr->fr_next;
3700 }
3701 if (nfr->fr_layout == FR_ROW && left)
3702 while (fr->fr_next != NULL)
3703 fr = fr->fr_next;
3704 nfr = fr;
3705 }
3706 }
3707end:
3708 if (foundfr != NULL)
3709 win_goto(foundfr->fr_win);
3710}
3711#endif
3712
3713/*
3714 * Make window "wp" the current window.
3715 */
3716 void
3717win_enter(wp, undo_sync)
3718 win_T *wp;
3719 int undo_sync;
3720{
3721 win_enter_ext(wp, undo_sync, FALSE);
3722}
3723
3724/*
3725 * Make window wp the current window.
3726 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3727 * been closed and isn't valid.
3728 */
3729 static void
3730win_enter_ext(wp, undo_sync, curwin_invalid)
3731 win_T *wp;
3732 int undo_sync;
3733 int curwin_invalid;
3734{
3735#ifdef FEAT_AUTOCMD
3736 int other_buffer = FALSE;
3737#endif
3738
3739 if (wp == curwin && !curwin_invalid) /* nothing to do */
3740 return;
3741
3742#ifdef FEAT_AUTOCMD
3743 if (!curwin_invalid)
3744 {
3745 /*
3746 * Be careful: If autocommands delete the window, return now.
3747 */
3748 if (wp->w_buffer != curbuf)
3749 {
3750 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3751 other_buffer = TRUE;
3752 if (!win_valid(wp))
3753 return;
3754 }
3755 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3756 if (!win_valid(wp))
3757 return;
3758# ifdef FEAT_EVAL
3759 /* autocmds may abort script processing */
3760 if (aborting())
3761 return;
3762# endif
3763 }
3764#endif
3765
3766 /* sync undo before leaving the current buffer */
3767 if (undo_sync && curbuf != wp->w_buffer)
3768 u_sync();
3769 /* may have to copy the buffer options when 'cpo' contains 'S' */
3770 if (wp->w_buffer != curbuf)
3771 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3772 if (!curwin_invalid)
3773 {
3774 prevwin = curwin; /* remember for CTRL-W p */
3775 curwin->w_redr_status = TRUE;
3776 }
3777 curwin = wp;
3778 curbuf = wp->w_buffer;
3779 check_cursor();
3780#ifdef FEAT_VIRTUALEDIT
3781 if (!virtual_active())
3782 curwin->w_cursor.coladd = 0;
3783#endif
3784 changed_line_abv_curs(); /* assume cursor position needs updating */
3785
3786 if (curwin->w_localdir != NULL)
3787 {
3788 /* Window has a local directory: Save current directory as global
3789 * directory (unless that was done already) and change to the local
3790 * directory. */
3791 if (globaldir == NULL)
3792 {
3793 char_u cwd[MAXPATHL];
3794
3795 if (mch_dirname(cwd, MAXPATHL) == OK)
3796 globaldir = vim_strsave(cwd);
3797 }
3798 mch_chdir((char *)curwin->w_localdir);
3799 shorten_fnames(TRUE);
3800 }
3801 else if (globaldir != NULL)
3802 {
3803 /* Window doesn't have a local directory and we are not in the global
3804 * directory: Change to the global directory. */
3805 mch_chdir((char *)globaldir);
3806 vim_free(globaldir);
3807 globaldir = NULL;
3808 shorten_fnames(TRUE);
3809 }
3810
3811#ifdef FEAT_AUTOCMD
3812 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3813 if (other_buffer)
3814 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3815#endif
3816
3817#ifdef FEAT_TITLE
3818 maketitle();
3819#endif
3820 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003821 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (restart_edit)
3823 redraw_later(VALID); /* causes status line redraw */
3824
3825 /* set window height to desired minimal value */
3826 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3827 win_setheight((int)p_wh);
3828 else if (curwin->w_height == 0)
3829 win_setheight(1);
3830
3831#ifdef FEAT_VERTSPLIT
3832 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003833 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 win_setwidth((int)p_wiw);
3835#endif
3836
3837#ifdef FEAT_MOUSE
3838 setmouse(); /* in case jumped to/from help buffer */
3839#endif
3840
3841#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3842 /* Change directories when the acd option is set on and after
3843 * switching windows. */
3844 if (p_acd && curbuf->b_ffname != NULL
3845 && vim_chdirfile(curbuf->b_ffname) == OK)
3846 shorten_fnames(TRUE);
3847#endif
3848}
3849
3850#endif /* FEAT_WINDOWS */
3851
3852#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3853/*
3854 * Jump to the first open window that contains buffer buf if one exists
3855 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3856 * Returns pointer to window if it exists, otherwise NULL.
3857 */
3858 win_T *
3859buf_jump_open_win(buf)
3860 buf_T *buf;
3861{
3862# ifdef FEAT_WINDOWS
3863 win_T *wp;
3864
3865 for (wp = firstwin; wp; wp = wp->w_next)
3866 if (wp->w_buffer == buf)
3867 break;
3868 if (wp != NULL)
3869 win_enter(wp, FALSE);
3870 return wp;
3871# else
3872 if (curwin->w_buffer == buf)
3873 return curwin;
3874 return NULL;
3875# endif
3876}
3877#endif
3878
3879/*
3880 * allocate a window structure and link it in the window list
3881 */
3882/*ARGSUSED*/
3883 static win_T *
3884win_alloc(after)
3885 win_T *after;
3886{
3887 win_T *newwin;
3888
3889 /*
3890 * allocate window structure and linesizes arrays
3891 */
3892 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3893 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3894 {
3895 vim_free(newwin);
3896 newwin = NULL;
3897 }
3898
3899 if (newwin != NULL)
3900 {
3901 /*
3902 * link the window in the window list
3903 */
3904#ifdef FEAT_WINDOWS
3905 win_append(after, newwin);
3906#endif
3907#ifdef FEAT_VERTSPLIT
3908 newwin->w_wincol = 0;
3909 newwin->w_width = Columns;
3910#endif
3911
3912 /* position the display and the cursor at the top of the file. */
3913 newwin->w_topline = 1;
3914#ifdef FEAT_DIFF
3915 newwin->w_topfill = 0;
3916#endif
3917 newwin->w_botline = 2;
3918 newwin->w_cursor.lnum = 1;
3919#ifdef FEAT_SCROLLBIND
3920 newwin->w_scbind_pos = 1;
3921#endif
3922
3923 /* We won't calculate w_fraction until resizing the window */
3924 newwin->w_fraction = 0;
3925 newwin->w_prev_fraction_row = -1;
3926
3927#ifdef FEAT_GUI
3928 if (gui.in_use)
3929 {
3930 out_flush();
3931 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3932 SBAR_LEFT, newwin);
3933 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3934 SBAR_RIGHT, newwin);
3935 }
3936#endif
3937#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003938 /* init w: variables */
3939 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#endif
3941#ifdef FEAT_FOLDING
3942 foldInitWin(newwin);
3943#endif
3944 }
3945 return newwin;
3946}
3947
3948#if defined(FEAT_WINDOWS) || defined(PROTO)
3949
3950/*
3951 * remove window 'wp' from the window list and free the structure
3952 */
3953 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003954win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003956 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 int i;
3959
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003960#ifdef FEAT_MZSCHEME
3961 mzscheme_window_free(wp);
3962#endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#ifdef FEAT_PERL
3965 perl_win_free(wp);
3966#endif
3967
3968#ifdef FEAT_PYTHON
3969 python_window_free(wp);
3970#endif
3971
3972#ifdef FEAT_TCL
3973 tcl_window_free(wp);
3974#endif
3975
3976#ifdef FEAT_RUBY
3977 ruby_window_free(wp);
3978#endif
3979
3980 clear_winopt(&wp->w_onebuf_opt);
3981 clear_winopt(&wp->w_allbuf_opt);
3982
3983#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003984 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#endif
3986
3987 if (prevwin == wp)
3988 prevwin = NULL;
3989 win_free_lsize(wp);
3990
3991 for (i = 0; i < wp->w_tagstacklen; ++i)
3992 vim_free(wp->w_tagstack[i].tagname);
3993
3994 vim_free(wp->w_localdir);
3995#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003996 vim_free(wp->w_match[0].regprog);
3997 vim_free(wp->w_match[1].regprog);
3998 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#endif
4000#ifdef FEAT_JUMPLIST
4001 free_jumplist(wp);
4002#endif
4003
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004004#ifdef FEAT_QUICKFIX
4005 qf_free_all(wp);
4006#endif
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#ifdef FEAT_GUI
4009 if (gui.in_use)
4010 {
4011 out_flush();
4012 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4013 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4014 }
4015#endif /* FEAT_GUI */
4016
Bram Moolenaarf740b292006-02-16 22:11:02 +00004017 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 vim_free(wp);
4019}
4020
4021/*
4022 * Append window "wp" in the window list after window "after".
4023 */
4024 static void
4025win_append(after, wp)
4026 win_T *after, *wp;
4027{
4028 win_T *before;
4029
4030 if (after == NULL) /* after NULL is in front of the first */
4031 before = firstwin;
4032 else
4033 before = after->w_next;
4034
4035 wp->w_next = before;
4036 wp->w_prev = after;
4037 if (after == NULL)
4038 firstwin = wp;
4039 else
4040 after->w_next = wp;
4041 if (before == NULL)
4042 lastwin = wp;
4043 else
4044 before->w_prev = wp;
4045}
4046
4047/*
4048 * Remove a window from the window list.
4049 */
4050 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004051win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004053 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
4055 if (wp->w_prev != NULL)
4056 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004057 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004059 else
4060 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if (wp->w_next != NULL)
4062 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004063 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004065 else
4066 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067}
4068
4069/*
4070 * Append frame "frp" in a frame list after frame "after".
4071 */
4072 static void
4073frame_append(after, frp)
4074 frame_T *after, *frp;
4075{
4076 frp->fr_next = after->fr_next;
4077 after->fr_next = frp;
4078 if (frp->fr_next != NULL)
4079 frp->fr_next->fr_prev = frp;
4080 frp->fr_prev = after;
4081}
4082
4083/*
4084 * Insert frame "frp" in a frame list before frame "before".
4085 */
4086 static void
4087frame_insert(before, frp)
4088 frame_T *before, *frp;
4089{
4090 frp->fr_next = before;
4091 frp->fr_prev = before->fr_prev;
4092 before->fr_prev = frp;
4093 if (frp->fr_prev != NULL)
4094 frp->fr_prev->fr_next = frp;
4095 else
4096 frp->fr_parent->fr_child = frp;
4097}
4098
4099/*
4100 * Remove a frame from a frame list.
4101 */
4102 static void
4103frame_remove(frp)
4104 frame_T *frp;
4105{
4106 if (frp->fr_prev != NULL)
4107 frp->fr_prev->fr_next = frp->fr_next;
4108 else
4109 frp->fr_parent->fr_child = frp->fr_next;
4110 if (frp->fr_next != NULL)
4111 frp->fr_next->fr_prev = frp->fr_prev;
4112}
4113
4114#endif /* FEAT_WINDOWS */
4115
4116/*
4117 * Allocate w_lines[] for window "wp".
4118 * Return FAIL for failure, OK for success.
4119 */
4120 int
4121win_alloc_lines(wp)
4122 win_T *wp;
4123{
4124 wp->w_lines_valid = 0;
4125 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4126 if (wp->w_lines == NULL)
4127 return FAIL;
4128 return OK;
4129}
4130
4131/*
4132 * free lsize arrays for a window
4133 */
4134 void
4135win_free_lsize(wp)
4136 win_T *wp;
4137{
4138 vim_free(wp->w_lines);
4139 wp->w_lines = NULL;
4140}
4141
4142/*
4143 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004144 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 */
4146 void
4147shell_new_rows()
4148{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004149 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 if (firstwin == NULL) /* not initialized yet */
4152 return;
4153#ifdef FEAT_WINDOWS
4154 if (h < frame_minheight(topframe, NULL))
4155 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004156
4157 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * that doesn't result in the right height, forget about that option. */
4159 frame_new_height(topframe, h, FALSE, TRUE);
4160 if (topframe->fr_height != h)
4161 frame_new_height(topframe, h, FALSE, FALSE);
4162
4163 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4164#else
4165 if (h < 1)
4166 h = 1;
4167 win_new_height(firstwin, h);
4168#endif
4169 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004170#ifdef FEAT_WINDOWS
4171 p_ch_used = p_ch;
4172#endif
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#if 0
4175 /* Disabled: don't want making the screen smaller make a window larger. */
4176 if (p_ea)
4177 win_equal(curwin, FALSE, 'v');
4178#endif
4179}
4180
4181#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4182/*
4183 * Called from win_new_shellsize() after Columns changed.
4184 */
4185 void
4186shell_new_columns()
4187{
4188 if (firstwin == NULL) /* not initialized yet */
4189 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004190
4191 /* First try setting the widths of windows with 'winfixwidth'. If that
4192 * doesn't result in the right width, forget about that option. */
4193 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4194 if (topframe->fr_width != Columns)
4195 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4198#if 0
4199 /* Disabled: don't want making the screen smaller make a window larger. */
4200 if (p_ea)
4201 win_equal(curwin, FALSE, 'h');
4202#endif
4203}
4204#endif
4205
4206#if defined(FEAT_CMDWIN) || defined(PROTO)
4207/*
4208 * Save the size of all windows in "gap".
4209 */
4210 void
4211win_size_save(gap)
4212 garray_T *gap;
4213
4214{
4215 win_T *wp;
4216
4217 ga_init2(gap, (int)sizeof(int), 1);
4218 if (ga_grow(gap, win_count() * 2) == OK)
4219 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4220 {
4221 ((int *)gap->ga_data)[gap->ga_len++] =
4222 wp->w_width + wp->w_vsep_width;
4223 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4224 }
4225}
4226
4227/*
4228 * Restore window sizes, but only if the number of windows is still the same.
4229 * Does not free the growarray.
4230 */
4231 void
4232win_size_restore(gap)
4233 garray_T *gap;
4234{
4235 win_T *wp;
4236 int i;
4237
4238 if (win_count() * 2 == gap->ga_len)
4239 {
4240 i = 0;
4241 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4242 {
4243 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4244 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4245 }
4246 /* recompute the window positions */
4247 (void)win_comp_pos();
4248 }
4249}
4250#endif /* FEAT_CMDWIN */
4251
4252#if defined(FEAT_WINDOWS) || defined(PROTO)
4253/*
4254 * Update the position for all windows, using the width and height of the
4255 * frames.
4256 * Returns the row just after the last window.
4257 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004258 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259win_comp_pos()
4260{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004261 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 int col = 0;
4263
4264 frame_comp_pos(topframe, &row, &col);
4265 return row;
4266}
4267
4268/*
4269 * Update the position of the windows in frame "topfrp", using the width and
4270 * height of the frames.
4271 * "*row" and "*col" are the top-left position of the frame. They are updated
4272 * to the bottom-right position plus one.
4273 */
4274 static void
4275frame_comp_pos(topfrp, row, col)
4276 frame_T *topfrp;
4277 int *row;
4278 int *col;
4279{
4280 win_T *wp;
4281 frame_T *frp;
4282#ifdef FEAT_VERTSPLIT
4283 int startcol;
4284 int startrow;
4285#endif
4286
4287 wp = topfrp->fr_win;
4288 if (wp != NULL)
4289 {
4290 if (wp->w_winrow != *row
4291#ifdef FEAT_VERTSPLIT
4292 || wp->w_wincol != *col
4293#endif
4294 )
4295 {
4296 /* position changed, redraw */
4297 wp->w_winrow = *row;
4298#ifdef FEAT_VERTSPLIT
4299 wp->w_wincol = *col;
4300#endif
4301 redraw_win_later(wp, NOT_VALID);
4302 wp->w_redr_status = TRUE;
4303 }
4304 *row += wp->w_height + wp->w_status_height;
4305#ifdef FEAT_VERTSPLIT
4306 *col += wp->w_width + wp->w_vsep_width;
4307#endif
4308 }
4309 else
4310 {
4311#ifdef FEAT_VERTSPLIT
4312 startrow = *row;
4313 startcol = *col;
4314#endif
4315 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4316 {
4317#ifdef FEAT_VERTSPLIT
4318 if (topfrp->fr_layout == FR_ROW)
4319 *row = startrow; /* all frames are at the same row */
4320 else
4321 *col = startcol; /* all frames are at the same col */
4322#endif
4323 frame_comp_pos(frp, row, col);
4324 }
4325 }
4326}
4327
4328#endif /* FEAT_WINDOWS */
4329
4330/*
4331 * Set current window height and take care of repositioning other windows to
4332 * fit around it.
4333 */
4334 void
4335win_setheight(height)
4336 int height;
4337{
4338 win_setheight_win(height, curwin);
4339}
4340
4341/*
4342 * Set the window height of window "win" and take care of repositioning other
4343 * windows to fit around it.
4344 */
4345 void
4346win_setheight_win(height, win)
4347 int height;
4348 win_T *win;
4349{
4350 int row;
4351
4352 if (win == curwin)
4353 {
4354 /* Always keep current window at least one line high, even when
4355 * 'winminheight' is zero. */
4356#ifdef FEAT_WINDOWS
4357 if (height < p_wmh)
4358 height = p_wmh;
4359#endif
4360 if (height == 0)
4361 height = 1;
4362 }
4363
4364#ifdef FEAT_WINDOWS
4365 frame_setheight(win->w_frame, height + win->w_status_height);
4366
4367 /* recompute the window positions */
4368 row = win_comp_pos();
4369#else
4370 if (height > topframe->fr_height)
4371 height = topframe->fr_height;
4372 win->w_height = height;
4373 row = height;
4374#endif
4375
4376 /*
4377 * If there is extra space created between the last window and the command
4378 * line, clear it.
4379 */
4380 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4381 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4382 cmdline_row = row;
4383 msg_row = row;
4384 msg_col = 0;
4385
4386 redraw_all_later(NOT_VALID);
4387}
4388
4389#if defined(FEAT_WINDOWS) || defined(PROTO)
4390
4391/*
4392 * Set the height of a frame to "height" and take care that all frames and
4393 * windows inside it are resized. Also resize frames on the left and right if
4394 * the are in the same FR_ROW frame.
4395 *
4396 * Strategy:
4397 * If the frame is part of a FR_COL frame, try fitting the frame in that
4398 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4399 * go to containing frames to resize them and make room.
4400 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4401 * Check for the minimal height of the FR_ROW frame.
4402 * At the top level we can also use change the command line height.
4403 */
4404 static void
4405frame_setheight(curfrp, height)
4406 frame_T *curfrp;
4407 int height;
4408{
4409 int room; /* total number of lines available */
4410 int take; /* number of lines taken from other windows */
4411 int room_cmdline; /* lines available from cmdline */
4412 int run;
4413 frame_T *frp;
4414 int h;
4415 int room_reserved;
4416
4417 /* If the height already is the desired value, nothing to do. */
4418 if (curfrp->fr_height == height)
4419 return;
4420
4421 if (curfrp->fr_parent == NULL)
4422 {
4423 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004424 if (height > ROWS_AVAIL)
4425 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 if (height > 0)
4427 frame_new_height(curfrp, height, FALSE, FALSE);
4428 }
4429 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4430 {
4431 /* Row of frames: Also need to resize frames left and right of this
4432 * one. First check for the minimal height of these. */
4433 h = frame_minheight(curfrp->fr_parent, NULL);
4434 if (height < h)
4435 height = h;
4436 frame_setheight(curfrp->fr_parent, height);
4437 }
4438 else
4439 {
4440 /*
4441 * Column of frames: try to change only frames in this column.
4442 */
4443#ifdef FEAT_VERTSPLIT
4444 /*
4445 * Do this twice:
4446 * 1: compute room available, if it's not enough try resizing the
4447 * containing frame.
4448 * 2: compute the room available and adjust the height to it.
4449 * Try not to reduce the height of a window with 'winfixheight' set.
4450 */
4451 for (run = 1; run <= 2; ++run)
4452#else
4453 for (;;)
4454#endif
4455 {
4456 room = 0;
4457 room_reserved = 0;
4458 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4459 frp = frp->fr_next)
4460 {
4461 if (frp != curfrp
4462 && frp->fr_win != NULL
4463 && frp->fr_win->w_p_wfh)
4464 room_reserved += frp->fr_height;
4465 room += frp->fr_height;
4466 if (frp != curfrp)
4467 room -= frame_minheight(frp, NULL);
4468 }
4469#ifdef FEAT_VERTSPLIT
4470 if (curfrp->fr_width != Columns)
4471 room_cmdline = 0;
4472 else
4473#endif
4474 {
4475 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4476 + lastwin->w_height + lastwin->w_status_height);
4477 if (room_cmdline < 0)
4478 room_cmdline = 0;
4479 }
4480
4481 if (height <= room + room_cmdline)
4482 break;
4483#ifdef FEAT_VERTSPLIT
4484 if (run == 2 || curfrp->fr_width == Columns)
4485#endif
4486 {
4487 if (height > room + room_cmdline)
4488 height = room + room_cmdline;
4489 break;
4490 }
4491#ifdef FEAT_VERTSPLIT
4492 frame_setheight(curfrp->fr_parent, height
4493 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4494#endif
4495 /*NOTREACHED*/
4496 }
4497
4498 /*
4499 * Compute the number of lines we will take from others frames (can be
4500 * negative!).
4501 */
4502 take = height - curfrp->fr_height;
4503
4504 /* If there is not enough room, also reduce the height of a window
4505 * with 'winfixheight' set. */
4506 if (height > room + room_cmdline - room_reserved)
4507 room_reserved = room + room_cmdline - height;
4508 /* If there is only a 'winfixheight' window and making the
4509 * window smaller, need to make the other window taller. */
4510 if (take < 0 && room - curfrp->fr_height < room_reserved)
4511 room_reserved = 0;
4512
4513 if (take > 0 && room_cmdline > 0)
4514 {
4515 /* use lines from cmdline first */
4516 if (take < room_cmdline)
4517 room_cmdline = take;
4518 take -= room_cmdline;
4519 topframe->fr_height += room_cmdline;
4520 }
4521
4522 /*
4523 * set the current frame to the new height
4524 */
4525 frame_new_height(curfrp, height, FALSE, FALSE);
4526
4527 /*
4528 * First take lines from the frames after the current frame. If
4529 * that is not enough, takes lines from frames above the current
4530 * frame.
4531 */
4532 for (run = 0; run < 2; ++run)
4533 {
4534 if (run == 0)
4535 frp = curfrp->fr_next; /* 1st run: start with next window */
4536 else
4537 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4538 while (frp != NULL && take != 0)
4539 {
4540 h = frame_minheight(frp, NULL);
4541 if (room_reserved > 0
4542 && frp->fr_win != NULL
4543 && frp->fr_win->w_p_wfh)
4544 {
4545 if (room_reserved >= frp->fr_height)
4546 room_reserved -= frp->fr_height;
4547 else
4548 {
4549 if (frp->fr_height - room_reserved > take)
4550 room_reserved = frp->fr_height - take;
4551 take -= frp->fr_height - room_reserved;
4552 frame_new_height(frp, room_reserved, FALSE, FALSE);
4553 room_reserved = 0;
4554 }
4555 }
4556 else
4557 {
4558 if (frp->fr_height - take < h)
4559 {
4560 take -= frp->fr_height - h;
4561 frame_new_height(frp, h, FALSE, FALSE);
4562 }
4563 else
4564 {
4565 frame_new_height(frp, frp->fr_height - take,
4566 FALSE, FALSE);
4567 take = 0;
4568 }
4569 }
4570 if (run == 0)
4571 frp = frp->fr_next;
4572 else
4573 frp = frp->fr_prev;
4574 }
4575 }
4576 }
4577}
4578
4579#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4580/*
4581 * Set current window width and take care of repositioning other windows to
4582 * fit around it.
4583 */
4584 void
4585win_setwidth(width)
4586 int width;
4587{
4588 win_setwidth_win(width, curwin);
4589}
4590
4591 void
4592win_setwidth_win(width, wp)
4593 int width;
4594 win_T *wp;
4595{
4596 /* Always keep current window at least one column wide, even when
4597 * 'winminwidth' is zero. */
4598 if (wp == curwin)
4599 {
4600 if (width < p_wmw)
4601 width = p_wmw;
4602 if (width == 0)
4603 width = 1;
4604 }
4605
4606 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4607
4608 /* recompute the window positions */
4609 (void)win_comp_pos();
4610
4611 redraw_all_later(NOT_VALID);
4612}
4613
4614/*
4615 * Set the width of a frame to "width" and take care that all frames and
4616 * windows inside it are resized. Also resize frames above and below if the
4617 * are in the same FR_ROW frame.
4618 *
4619 * Strategy is similar to frame_setheight().
4620 */
4621 static void
4622frame_setwidth(curfrp, width)
4623 frame_T *curfrp;
4624 int width;
4625{
4626 int room; /* total number of lines available */
4627 int take; /* number of lines taken from other windows */
4628 int run;
4629 frame_T *frp;
4630 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004631 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 /* If the width already is the desired value, nothing to do. */
4634 if (curfrp->fr_width == width)
4635 return;
4636
4637 if (curfrp->fr_parent == NULL)
4638 /* topframe: can't change width */
4639 return;
4640
4641 if (curfrp->fr_parent->fr_layout == FR_COL)
4642 {
4643 /* Column of frames: Also need to resize frames above and below of
4644 * this one. First check for the minimal width of these. */
4645 w = frame_minwidth(curfrp->fr_parent, NULL);
4646 if (width < w)
4647 width = w;
4648 frame_setwidth(curfrp->fr_parent, width);
4649 }
4650 else
4651 {
4652 /*
4653 * Row of frames: try to change only frames in this row.
4654 *
4655 * Do this twice:
4656 * 1: compute room available, if it's not enough try resizing the
4657 * containing frame.
4658 * 2: compute the room available and adjust the width to it.
4659 */
4660 for (run = 1; run <= 2; ++run)
4661 {
4662 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004663 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4665 frp = frp->fr_next)
4666 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004667 if (frp != curfrp
4668 && frp->fr_win != NULL
4669 && frp->fr_win->w_p_wfw)
4670 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 room += frp->fr_width;
4672 if (frp != curfrp)
4673 room -= frame_minwidth(frp, NULL);
4674 }
4675
4676 if (width <= room)
4677 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004678 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
4680 if (width > room)
4681 width = room;
4682 break;
4683 }
4684 frame_setwidth(curfrp->fr_parent, width
4685 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4686 }
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 /*
4689 * Compute the number of lines we will take from others frames (can be
4690 * negative!).
4691 */
4692 take = width - curfrp->fr_width;
4693
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004694 /* If there is not enough room, also reduce the width of a window
4695 * with 'winfixwidth' set. */
4696 if (width > room - room_reserved)
4697 room_reserved = room - width;
4698 /* If there is only a 'winfixwidth' window and making the
4699 * window smaller, need to make the other window narrower. */
4700 if (take < 0 && room - curfrp->fr_width < room_reserved)
4701 room_reserved = 0;
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * set the current frame to the new width
4705 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004706 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 /*
4709 * First take lines from the frames right of the current frame. If
4710 * that is not enough, takes lines from frames left of the current
4711 * frame.
4712 */
4713 for (run = 0; run < 2; ++run)
4714 {
4715 if (run == 0)
4716 frp = curfrp->fr_next; /* 1st run: start with next window */
4717 else
4718 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4719 while (frp != NULL && take != 0)
4720 {
4721 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004722 if (room_reserved > 0
4723 && frp->fr_win != NULL
4724 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004726 if (room_reserved >= frp->fr_width)
4727 room_reserved -= frp->fr_width;
4728 else
4729 {
4730 if (frp->fr_width - room_reserved > take)
4731 room_reserved = frp->fr_width - take;
4732 take -= frp->fr_width - room_reserved;
4733 frame_new_width(frp, room_reserved, FALSE, FALSE);
4734 room_reserved = 0;
4735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 else
4738 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004739 if (frp->fr_width - take < w)
4740 {
4741 take -= frp->fr_width - w;
4742 frame_new_width(frp, w, FALSE, FALSE);
4743 }
4744 else
4745 {
4746 frame_new_width(frp, frp->fr_width - take,
4747 FALSE, FALSE);
4748 take = 0;
4749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 }
4751 if (run == 0)
4752 frp = frp->fr_next;
4753 else
4754 frp = frp->fr_prev;
4755 }
4756 }
4757 }
4758}
4759#endif /* FEAT_VERTSPLIT */
4760
4761/*
4762 * Check 'winminheight' for a valid value.
4763 */
4764 void
4765win_setminheight()
4766{
4767 int room;
4768 int first = TRUE;
4769 win_T *wp;
4770
4771 /* loop until there is a 'winminheight' that is possible */
4772 while (p_wmh > 0)
4773 {
4774 /* TODO: handle vertical splits */
4775 room = -p_wh;
4776 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4777 room += wp->w_height - p_wmh;
4778 if (room >= 0)
4779 break;
4780 --p_wmh;
4781 if (first)
4782 {
4783 EMSG(_(e_noroom));
4784 first = FALSE;
4785 }
4786 }
4787}
4788
4789#ifdef FEAT_MOUSE
4790
4791/*
4792 * Status line of dragwin is dragged "offset" lines down (negative is up).
4793 */
4794 void
4795win_drag_status_line(dragwin, offset)
4796 win_T *dragwin;
4797 int offset;
4798{
4799 frame_T *curfr;
4800 frame_T *fr;
4801 int room;
4802 int row;
4803 int up; /* if TRUE, drag status line up, otherwise down */
4804 int n;
4805
4806 fr = dragwin->w_frame;
4807 curfr = fr;
4808 if (fr != topframe) /* more than one window */
4809 {
4810 fr = fr->fr_parent;
4811 /* When the parent frame is not a column of frames, its parent should
4812 * be. */
4813 if (fr->fr_layout != FR_COL)
4814 {
4815 curfr = fr;
4816 if (fr != topframe) /* only a row of windows, may drag statusline */
4817 fr = fr->fr_parent;
4818 }
4819 }
4820
4821 /* If this is the last frame in a column, may want to resize the parent
4822 * frame instead (go two up to skip a row of frames). */
4823 while (curfr != topframe && curfr->fr_next == NULL)
4824 {
4825 if (fr != topframe)
4826 fr = fr->fr_parent;
4827 curfr = fr;
4828 if (fr != topframe)
4829 fr = fr->fr_parent;
4830 }
4831
4832 if (offset < 0) /* drag up */
4833 {
4834 up = TRUE;
4835 offset = -offset;
4836 /* sum up the room of the current frame and above it */
4837 if (fr == curfr)
4838 {
4839 /* only one window */
4840 room = fr->fr_height - frame_minheight(fr, NULL);
4841 }
4842 else
4843 {
4844 room = 0;
4845 for (fr = fr->fr_child; ; fr = fr->fr_next)
4846 {
4847 room += fr->fr_height - frame_minheight(fr, NULL);
4848 if (fr == curfr)
4849 break;
4850 }
4851 }
4852 fr = curfr->fr_next; /* put fr at frame that grows */
4853 }
4854 else /* drag down */
4855 {
4856 up = FALSE;
4857 /*
4858 * Only dragging the last status line can reduce p_ch.
4859 */
4860 room = Rows - cmdline_row;
4861 if (curfr->fr_next == NULL)
4862 room -= 1;
4863 else
4864 room -= p_ch;
4865 if (room < 0)
4866 room = 0;
4867 /* sum up the room of frames below of the current one */
4868 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4869 room += fr->fr_height - frame_minheight(fr, NULL);
4870 fr = curfr; /* put fr at window that grows */
4871 }
4872
4873 if (room < offset) /* Not enough room */
4874 offset = room; /* Move as far as we can */
4875 if (offset <= 0)
4876 return;
4877
4878 /*
4879 * Grow frame fr by "offset" lines.
4880 * Doesn't happen when dragging the last status line up.
4881 */
4882 if (fr != NULL)
4883 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4884
4885 if (up)
4886 fr = curfr; /* current frame gets smaller */
4887 else
4888 fr = curfr->fr_next; /* next frame gets smaller */
4889
4890 /*
4891 * Now make the other frames smaller.
4892 */
4893 while (fr != NULL && offset > 0)
4894 {
4895 n = frame_minheight(fr, NULL);
4896 if (fr->fr_height - offset <= n)
4897 {
4898 offset -= fr->fr_height - n;
4899 frame_new_height(fr, n, !up, FALSE);
4900 }
4901 else
4902 {
4903 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4904 break;
4905 }
4906 if (up)
4907 fr = fr->fr_prev;
4908 else
4909 fr = fr->fr_next;
4910 }
4911 row = win_comp_pos();
4912 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4913 cmdline_row = row;
4914 p_ch = Rows - cmdline_row;
4915 if (p_ch < 1)
4916 p_ch = 1;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004917 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 showmode();
4919}
4920
4921#ifdef FEAT_VERTSPLIT
4922/*
4923 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4924 */
4925 void
4926win_drag_vsep_line(dragwin, offset)
4927 win_T *dragwin;
4928 int offset;
4929{
4930 frame_T *curfr;
4931 frame_T *fr;
4932 int room;
4933 int left; /* if TRUE, drag separator line left, otherwise right */
4934 int n;
4935
4936 fr = dragwin->w_frame;
4937 if (fr == topframe) /* only one window (cannot happe?) */
4938 return;
4939 curfr = fr;
4940 fr = fr->fr_parent;
4941 /* When the parent frame is not a row of frames, its parent should be. */
4942 if (fr->fr_layout != FR_ROW)
4943 {
4944 if (fr == topframe) /* only a column of windows (cannot happen?) */
4945 return;
4946 curfr = fr;
4947 fr = fr->fr_parent;
4948 }
4949
4950 /* If this is the last frame in a row, may want to resize a parent
4951 * frame instead. */
4952 while (curfr->fr_next == NULL)
4953 {
4954 if (fr == topframe)
4955 break;
4956 curfr = fr;
4957 fr = fr->fr_parent;
4958 if (fr != topframe)
4959 {
4960 curfr = fr;
4961 fr = fr->fr_parent;
4962 }
4963 }
4964
4965 if (offset < 0) /* drag left */
4966 {
4967 left = TRUE;
4968 offset = -offset;
4969 /* sum up the room of the current frame and left of it */
4970 room = 0;
4971 for (fr = fr->fr_child; ; fr = fr->fr_next)
4972 {
4973 room += fr->fr_width - frame_minwidth(fr, NULL);
4974 if (fr == curfr)
4975 break;
4976 }
4977 fr = curfr->fr_next; /* put fr at frame that grows */
4978 }
4979 else /* drag right */
4980 {
4981 left = FALSE;
4982 /* sum up the room of frames right of the current one */
4983 room = 0;
4984 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4985 room += fr->fr_width - frame_minwidth(fr, NULL);
4986 fr = curfr; /* put fr at window that grows */
4987 }
4988
4989 if (room < offset) /* Not enough room */
4990 offset = room; /* Move as far as we can */
4991 if (offset <= 0) /* No room at all, quit. */
4992 return;
4993
4994 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004995 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996
4997 /* shrink other frames: current and at the left or at the right */
4998 if (left)
4999 fr = curfr; /* current frame gets smaller */
5000 else
5001 fr = curfr->fr_next; /* next frame gets smaller */
5002
5003 while (fr != NULL && offset > 0)
5004 {
5005 n = frame_minwidth(fr, NULL);
5006 if (fr->fr_width - offset <= n)
5007 {
5008 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005009 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 else
5012 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005013 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 break;
5015 }
5016 if (left)
5017 fr = fr->fr_prev;
5018 else
5019 fr = fr->fr_next;
5020 }
5021 (void)win_comp_pos();
5022 redraw_all_later(NOT_VALID);
5023}
5024#endif /* FEAT_VERTSPLIT */
5025#endif /* FEAT_MOUSE */
5026
5027#endif /* FEAT_WINDOWS */
5028
5029/*
5030 * Set the height of a window.
5031 * This takes care of the things inside the window, not what happens to the
5032 * window position, the frame or to other windows.
5033 */
5034 static void
5035win_new_height(wp, height)
5036 win_T *wp;
5037 int height;
5038{
5039 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005040 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005042 int space;
5043 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#define FRACTION_MULT 16384L
5045
5046 /* Don't want a negative height. Happens when splitting a tiny window.
5047 * Will equalize heights soon to fix it. */
5048 if (height < 0)
5049 height = 0;
5050
5051 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5052 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5053 + FRACTION_MULT / 2) / (long)wp->w_height;
5054
5055 wp->w_height = height;
5056 wp->w_skipcol = 0;
5057
5058 /* Don't change w_topline when height is zero. Don't set w_topline when
5059 * 'scrollbind' is set and this isn't the current window. */
5060 if (height > 0
5061#ifdef FEAT_SCROLLBIND
5062 && (!wp->w_p_scb || wp == curwin)
5063#endif
5064 )
5065 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005066 /*
5067 * Find a value for w_topline that shows the cursor at the same
5068 * relative position in the window as before (more or less).
5069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 lnum = wp->w_cursor.lnum;
5071 if (lnum < 1) /* can happen when starting up */
5072 lnum = 1;
5073 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5074 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5075 sline = wp->w_wrow - line_size;
5076 if (sline < 0)
5077 {
5078 /*
5079 * Cursor line would go off top of screen if w_wrow was this high.
5080 */
5081 wp->w_wrow = line_size;
5082 }
5083 else
5084 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005085 space = height;
5086 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005088 space -= line_size;
5089 if (space > 0 && sline <= 0 && !did_below)
5090 {
5091 /* Try to use "~" lines below the text to avoid that text
5092 * is above the window while there are empty lines.
5093 * Subtract the rows below the cursor from "space" and
5094 * give the rest to "sline". */
5095 did_below = TRUE;
5096 bot = wp->w_cursor.lnum;
5097 while (space > 0)
5098 {
5099 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5100 space = 0;
5101 else
5102 {
5103#ifdef FEAT_FOLDING
5104 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5105#endif
5106 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5107 break;
5108 ++bot;
5109 space -= plines_win(wp, bot, TRUE);
5110 }
5111 }
5112 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5113 sline += space;
5114 }
5115 if (sline <= 0)
5116 break;
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#ifdef FEAT_FOLDING
5119 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5120 if (lnum == 1)
5121 {
5122 /* first line in buffer is folded */
5123 line_size = 1;
5124 --sline;
5125 break;
5126 }
5127#endif
5128 --lnum;
5129#ifdef FEAT_DIFF
5130 if (lnum == wp->w_topline)
5131 line_size = plines_win_nofill(wp, lnum, TRUE)
5132 + wp->w_topfill;
5133 else
5134#endif
5135 line_size = plines_win(wp, lnum, TRUE);
5136 sline -= line_size;
5137 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (sline < 0)
5140 {
5141 /*
5142 * Line we want at top would go off top of screen. Use next
5143 * line instead.
5144 */
5145#ifdef FEAT_FOLDING
5146 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5147#endif
5148 lnum++;
5149 wp->w_wrow -= line_size + sline;
5150 }
5151 else if (sline > 0)
5152 {
5153 /* First line of file reached, use that as topline. */
5154 lnum = 1;
5155 wp->w_wrow -= sline;
5156 }
5157 }
5158 set_topline(wp, lnum);
5159 }
5160
5161 if (wp == curwin)
5162 {
5163 if (p_so)
5164 update_topline();
5165 curs_columns(FALSE); /* validate w_wrow */
5166 }
5167 wp->w_prev_fraction_row = wp->w_wrow;
5168
5169 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005170 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#ifdef FEAT_WINDOWS
5172 wp->w_redr_status = TRUE;
5173#endif
5174 invalidate_botline_win(wp);
5175}
5176
5177#ifdef FEAT_VERTSPLIT
5178/*
5179 * Set the width of a window.
5180 */
5181 static void
5182win_new_width(wp, width)
5183 win_T *wp;
5184 int width;
5185{
5186 wp->w_width = width;
5187 wp->w_lines_valid = 0;
5188 changed_line_abv_curs_win(wp);
5189 invalidate_botline_win(wp);
5190 if (wp == curwin)
5191 {
5192 update_topline();
5193 curs_columns(TRUE); /* validate w_wrow */
5194 }
5195 redraw_win_later(wp, NOT_VALID);
5196 wp->w_redr_status = TRUE;
5197}
5198#endif
5199
5200 void
5201win_comp_scroll(wp)
5202 win_T *wp;
5203{
5204 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5205 if (wp->w_p_scr == 0)
5206 wp->w_p_scr = 1;
5207}
5208
5209/*
5210 * command_height: called whenever p_ch has been changed
5211 */
5212 void
5213command_height(old_p_ch)
5214 long old_p_ch;
5215{
5216#ifdef FEAT_WINDOWS
5217 int h;
5218 frame_T *frp;
5219
Bram Moolenaar05159a02005-02-26 23:04:13 +00005220 /* When passed a negative value use the value of p_ch that we remembered.
5221 * This is needed for when the GUI starts up, we can't be sure in what
5222 * order things happen. */
5223 if (old_p_ch < 0)
5224 old_p_ch = p_ch_used;
5225 p_ch_used = p_ch;
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 /* Find bottom frame with width of screen. */
5228 frp = lastwin->w_frame;
5229# ifdef FEAT_VERTSPLIT
5230 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5231 frp = frp->fr_parent;
5232# endif
5233
5234 /* Avoid changing the height of a window with 'winfixheight' set. */
5235 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5236 && frp->fr_win->w_p_wfh)
5237 frp = frp->fr_prev;
5238
5239 if (starting != NO_SCREEN)
5240 {
5241 cmdline_row = Rows - p_ch;
5242
5243 if (p_ch > old_p_ch) /* p_ch got bigger */
5244 {
5245 while (p_ch > old_p_ch)
5246 {
5247 if (frp == NULL)
5248 {
5249 EMSG(_(e_noroom));
5250 p_ch = old_p_ch;
5251 cmdline_row = Rows - p_ch;
5252 break;
5253 }
5254 h = frp->fr_height - frame_minheight(frp, NULL);
5255 if (h > p_ch - old_p_ch)
5256 h = p_ch - old_p_ch;
5257 old_p_ch += h;
5258 frame_add_height(frp, -h);
5259 frp = frp->fr_prev;
5260 }
5261
5262 /* Recompute window positions. */
5263 (void)win_comp_pos();
5264
5265 /* clear the lines added to cmdline */
5266 if (full_screen)
5267 screen_fill((int)(cmdline_row), (int)Rows, 0,
5268 (int)Columns, ' ', ' ', 0);
5269 msg_row = cmdline_row;
5270 redraw_cmdline = TRUE;
5271 return;
5272 }
5273
5274 if (msg_row < cmdline_row)
5275 msg_row = cmdline_row;
5276 redraw_cmdline = TRUE;
5277 }
5278 frame_add_height(frp, (int)(old_p_ch - p_ch));
5279
5280 /* Recompute window positions. */
5281 if (frp != lastwin->w_frame)
5282 (void)win_comp_pos();
5283#else
5284 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5285 cmdline_row = Rows - p_ch;
5286#endif
5287}
5288
5289#if defined(FEAT_WINDOWS) || defined(PROTO)
5290/*
5291 * Resize frame "frp" to be "n" lines higher (negative for less high).
5292 * Also resize the frames it is contained in.
5293 */
5294 static void
5295frame_add_height(frp, n)
5296 frame_T *frp;
5297 int n;
5298{
5299 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5300 for (;;)
5301 {
5302 frp = frp->fr_parent;
5303 if (frp == NULL)
5304 break;
5305 frp->fr_height += n;
5306 }
5307}
5308
5309/*
5310 * Add or remove a status line for the bottom window(s), according to the
5311 * value of 'laststatus'.
5312 */
5313 void
5314last_status(morewin)
5315 int morewin; /* pretend there are two or more windows */
5316{
5317 /* Don't make a difference between horizontal or vertical split. */
5318 last_status_rec(topframe, (p_ls == 2
5319 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5320}
5321
5322 static void
5323last_status_rec(fr, statusline)
5324 frame_T *fr;
5325 int statusline;
5326{
5327 frame_T *fp;
5328 win_T *wp;
5329
5330 if (fr->fr_layout == FR_LEAF)
5331 {
5332 wp = fr->fr_win;
5333 if (wp->w_status_height != 0 && !statusline)
5334 {
5335 /* remove status line */
5336 win_new_height(wp, wp->w_height + 1);
5337 wp->w_status_height = 0;
5338 comp_col();
5339 }
5340 else if (wp->w_status_height == 0 && statusline)
5341 {
5342 /* Find a frame to take a line from. */
5343 fp = fr;
5344 while (fp->fr_height <= frame_minheight(fp, NULL))
5345 {
5346 if (fp == topframe)
5347 {
5348 EMSG(_(e_noroom));
5349 return;
5350 }
5351 /* In a column of frames: go to frame above. If already at
5352 * the top or in a row of frames: go to parent. */
5353 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5354 fp = fp->fr_prev;
5355 else
5356 fp = fp->fr_parent;
5357 }
5358 wp->w_status_height = 1;
5359 if (fp != fr)
5360 {
5361 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5362 frame_fix_height(wp);
5363 (void)win_comp_pos();
5364 }
5365 else
5366 win_new_height(wp, wp->w_height - 1);
5367 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005368 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 }
5371#ifdef FEAT_VERTSPLIT
5372 else if (fr->fr_layout == FR_ROW)
5373 {
5374 /* vertically split windows, set status line for each one */
5375 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5376 last_status_rec(fp, statusline);
5377 }
5378#endif
5379 else
5380 {
5381 /* horizontally split window, set status line for last one */
5382 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5383 ;
5384 last_status_rec(fp, statusline);
5385 }
5386}
5387
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005388/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005389 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005390 */
5391 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005392tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005393{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005394#ifdef FEAT_GUI_TABLINE
5395 /* When the GUI has the tabline then this always returns zero. */
5396 if (gui_use_tabline())
5397 return 0;
5398#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005399 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005400 {
5401 case 0: return 0;
5402 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5403 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005404 return 1;
5405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif /* FEAT_WINDOWS */
5408
5409#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5410/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005411 * Get the file name at the cursor.
5412 * If Visual mode is active, use the selected text if it's in one line.
5413 * Returns the name in allocated memory, NULL for failure.
5414 */
5415 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005416grab_file_name(count, file_lnum)
5417 long count;
5418 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005419{
5420# ifdef FEAT_VISUAL
5421 if (VIsual_active)
5422 {
5423 int len;
5424 char_u *ptr;
5425
5426 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5427 return NULL;
5428 return find_file_name_in_path(ptr, len,
5429 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5430 }
5431# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005432 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5433 file_lnum);
5434
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005435}
5436
5437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 * Return the file name under or after the cursor.
5439 *
5440 * The 'path' option is searched if the file name is not absolute.
5441 * The string returned has been alloc'ed and should be freed by the caller.
5442 * NULL is returned if the file name or file is not found.
5443 *
5444 * options:
5445 * FNAME_MESS give error messages
5446 * FNAME_EXP expand to path
5447 * FNAME_HYP check for hypertext link
5448 * FNAME_INCL apply "includeexpr"
5449 */
5450 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005451file_name_at_cursor(options, count, file_lnum)
5452 int options;
5453 long count;
5454 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005457 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5458 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460
5461/*
5462 * Return the name of the file under or after ptr[col].
5463 * Otherwise like file_name_at_cursor().
5464 */
5465 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005466file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 char_u *line;
5468 int col;
5469 int options;
5470 long count;
5471 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005472 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
5474 char_u *ptr;
5475 int len;
5476
5477 /*
5478 * search forward for what could be the start of a file name
5479 */
5480 ptr = line + col;
5481 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005482 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (*ptr == NUL) /* nothing found */
5484 {
5485 if (options & FNAME_MESS)
5486 EMSG(_("E446: No file name under cursor"));
5487 return NULL;
5488 }
5489
5490 /*
5491 * Search backward for first char of the file name.
5492 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5493 */
5494 while (ptr > line)
5495 {
5496#ifdef FEAT_MBYTE
5497 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5498 ptr -= len + 1;
5499 else
5500#endif
5501 if (vim_isfilec(ptr[-1])
5502 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5503 --ptr;
5504 else
5505 break;
5506 }
5507
5508 /*
5509 * Search forward for the last char of the file name.
5510 * Also allow "://" when ':' is not in 'isfname'.
5511 */
5512 len = 0;
5513 while (vim_isfilec(ptr[len])
5514 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5515#ifdef FEAT_MBYTE
5516 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005517 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 else
5519#endif
5520 ++len;
5521
5522 /*
5523 * If there is trailing punctuation, remove it.
5524 * But don't remove "..", could be a directory name.
5525 */
5526 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5527 && ptr[len - 2] != '.')
5528 --len;
5529
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005530 if (file_lnum != NULL)
5531 {
5532 char_u *p;
5533
5534 /* Get the number after the file name and a separator character */
5535 p = ptr + len;
5536 p = skipwhite(p);
5537 if (*p != NUL)
5538 {
5539 if (!isdigit(*p))
5540 ++p; /* skip the separator */
5541 p = skipwhite(p);
5542 if (isdigit(*p))
5543 *file_lnum = (int)getdigits(&p);
5544 }
5545 }
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5548}
5549
5550# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5551static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5552
5553 static char_u *
5554eval_includeexpr(ptr, len)
5555 char_u *ptr;
5556 int len;
5557{
5558 char_u *res;
5559
5560 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005561 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005562 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 set_vim_var_string(VV_FNAME, NULL, 0);
5564 return res;
5565}
5566#endif
5567
5568/*
5569 * Return the name of the file ptr[len] in 'path'.
5570 * Otherwise like file_name_at_cursor().
5571 */
5572 char_u *
5573find_file_name_in_path(ptr, len, options, count, rel_fname)
5574 char_u *ptr;
5575 int len;
5576 int options;
5577 long count;
5578 char_u *rel_fname; /* file we are searching relative to */
5579{
5580 char_u *file_name;
5581 int c;
5582# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5583 char_u *tofree = NULL;
5584
5585 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5586 {
5587 tofree = eval_includeexpr(ptr, len);
5588 if (tofree != NULL)
5589 {
5590 ptr = tofree;
5591 len = (int)STRLEN(ptr);
5592 }
5593 }
5594# endif
5595
5596 if (options & FNAME_EXP)
5597 {
5598 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5599 TRUE, rel_fname);
5600
5601# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5602 /*
5603 * If the file could not be found in a normal way, try applying
5604 * 'includeexpr' (unless done already).
5605 */
5606 if (file_name == NULL
5607 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5608 {
5609 tofree = eval_includeexpr(ptr, len);
5610 if (tofree != NULL)
5611 {
5612 ptr = tofree;
5613 len = (int)STRLEN(ptr);
5614 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5615 TRUE, rel_fname);
5616 }
5617 }
5618# endif
5619 if (file_name == NULL && (options & FNAME_MESS))
5620 {
5621 c = ptr[len];
5622 ptr[len] = NUL;
5623 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5624 ptr[len] = c;
5625 }
5626
5627 /* Repeat finding the file "count" times. This matters when it
5628 * appears several times in the path. */
5629 while (file_name != NULL && --count > 0)
5630 {
5631 vim_free(file_name);
5632 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5633 }
5634 }
5635 else
5636 file_name = vim_strnsave(ptr, len);
5637
5638# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5639 vim_free(tofree);
5640# endif
5641
5642 return file_name;
5643}
5644#endif /* FEAT_SEARCHPATH */
5645
5646/*
5647 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5648 * Also check for ":\\", which MS Internet Explorer accepts, return
5649 * URL_BACKSLASH.
5650 */
5651 static int
5652path_is_url(p)
5653 char_u *p;
5654{
5655 if (STRNCMP(p, "://", (size_t)3) == 0)
5656 return URL_SLASH;
5657 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5658 return URL_BACKSLASH;
5659 return 0;
5660}
5661
5662/*
5663 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5664 * Return URL_BACKSLASH for "name:\\".
5665 * Return zero otherwise.
5666 */
5667 int
5668path_with_url(fname)
5669 char_u *fname;
5670{
5671 char_u *p;
5672
5673 for (p = fname; isalpha(*p); ++p)
5674 ;
5675 return path_is_url(p);
5676}
5677
5678/*
5679 * Return TRUE if "name" is a full (absolute) path name or URL.
5680 */
5681 int
5682vim_isAbsName(name)
5683 char_u *name;
5684{
5685 return (path_with_url(name) != 0 || mch_isFullName(name));
5686}
5687
5688/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005689 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 *
5691 * return FAIL for failure, OK otherwise
5692 */
5693 int
5694vim_FullName(fname, buf, len, force)
5695 char_u *fname, *buf;
5696 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005697 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
5699 int retval = OK;
5700 int url;
5701
5702 *buf = NUL;
5703 if (fname == NULL)
5704 return FAIL;
5705
5706 url = path_with_url(fname);
5707 if (!url)
5708 retval = mch_FullName(fname, buf, len, force);
5709 if (url || retval == FAIL)
5710 {
5711 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005712 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5715 slash_adjust(buf);
5716#endif
5717 return retval;
5718}
5719
5720/*
5721 * Return the minimal number of rows that is needed on the screen to display
5722 * the current number of windows.
5723 */
5724 int
5725min_rows()
5726{
5727 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005728#ifdef FEAT_WINDOWS
5729 tabpage_T *tp;
5730 int n;
5731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733 if (firstwin == NULL) /* not initialized yet */
5734 return MIN_LINES;
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005737 total = 0;
5738 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5739 {
5740 n = frame_minheight(tp->tp_topframe, NULL);
5741 if (total < n)
5742 total = n;
5743 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005744 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005746 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005748 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 return total;
5750}
5751
5752/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005753 * Return TRUE if there is only one window (in the current tab page), not
5754 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 */
5756 int
5757only_one_window()
5758{
5759#ifdef FEAT_WINDOWS
5760 int count = 0;
5761 win_T *wp;
5762
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005763 /* If there is another tab page there always is another window. */
5764 if (first_tabpage->tp_next != NULL)
5765 return FALSE;
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005768 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769# ifdef FEAT_QUICKFIX
5770 || wp->w_p_pvw
5771# endif
5772 ) || wp == curwin)
5773 ++count;
5774 return (count <= 1);
5775#else
5776 return TRUE;
5777#endif
5778}
5779
5780#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5781/*
5782 * Correct the cursor line number in other windows. Used after changing the
5783 * current buffer, and before applying autocommands.
5784 * When "do_curwin" is TRUE, also check current window.
5785 */
5786 void
5787check_lnums(do_curwin)
5788 int do_curwin;
5789{
5790 win_T *wp;
5791
5792#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005793 tabpage_T *tp;
5794
5795 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5797#else
5798 wp = curwin;
5799 if (do_curwin)
5800#endif
5801 {
5802 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5803 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5804 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5805 wp->w_topline = curbuf->b_ml.ml_line_count;
5806 }
5807}
5808#endif
5809
5810#if defined(FEAT_WINDOWS) || defined(PROTO)
5811
5812/*
5813 * A snapshot of the window sizes, to restore them after closing the help
5814 * window.
5815 * Only these fields are used:
5816 * fr_layout
5817 * fr_width
5818 * fr_height
5819 * fr_next
5820 * fr_child
5821 * fr_win (only valid for the old curwin, NULL otherwise)
5822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824/*
5825 * Create a snapshot of the current frame sizes.
5826 */
5827 static void
5828make_snapshot()
5829{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005830 clear_snapshot(curtab);
5831 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
5834 static void
5835make_snapshot_rec(fr, frp)
5836 frame_T *fr;
5837 frame_T **frp;
5838{
5839 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5840 if (*frp == NULL)
5841 return;
5842 (*frp)->fr_layout = fr->fr_layout;
5843# ifdef FEAT_VERTSPLIT
5844 (*frp)->fr_width = fr->fr_width;
5845# endif
5846 (*frp)->fr_height = fr->fr_height;
5847 if (fr->fr_next != NULL)
5848 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5849 if (fr->fr_child != NULL)
5850 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5851 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5852 (*frp)->fr_win = curwin;
5853}
5854
5855/*
5856 * Remove any existing snapshot.
5857 */
5858 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005859clear_snapshot(tp)
5860 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005862 clear_snapshot_rec(tp->tp_snapshot);
5863 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864}
5865
5866 static void
5867clear_snapshot_rec(fr)
5868 frame_T *fr;
5869{
5870 if (fr != NULL)
5871 {
5872 clear_snapshot_rec(fr->fr_next);
5873 clear_snapshot_rec(fr->fr_child);
5874 vim_free(fr);
5875 }
5876}
5877
5878/*
5879 * Restore a previously created snapshot, if there is any.
5880 * This is only done if the screen size didn't change and the window layout is
5881 * still the same.
5882 */
5883 static void
5884restore_snapshot(close_curwin)
5885 int close_curwin; /* closing current window */
5886{
5887 win_T *wp;
5888
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005889 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005891 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005893 && curtab->tp_snapshot->fr_height == topframe->fr_height
5894 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005896 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 win_comp_pos();
5898 if (wp != NULL && close_curwin)
5899 win_goto(wp);
5900 redraw_all_later(CLEAR);
5901 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903}
5904
5905/*
5906 * Check if frames "sn" and "fr" have the same layout, same following frames
5907 * and same children.
5908 */
5909 static int
5910check_snapshot_rec(sn, fr)
5911 frame_T *sn;
5912 frame_T *fr;
5913{
5914 if (sn->fr_layout != fr->fr_layout
5915 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5916 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5917 || (sn->fr_next != NULL
5918 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5919 || (sn->fr_child != NULL
5920 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5921 return FAIL;
5922 return OK;
5923}
5924
5925/*
5926 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5927 * following frames and children.
5928 * Returns a pointer to the old current window, or NULL.
5929 */
5930 static win_T *
5931restore_snapshot_rec(sn, fr)
5932 frame_T *sn;
5933 frame_T *fr;
5934{
5935 win_T *wp = NULL;
5936 win_T *wp2;
5937
5938 fr->fr_height = sn->fr_height;
5939# ifdef FEAT_VERTSPLIT
5940 fr->fr_width = sn->fr_width;
5941# endif
5942 if (fr->fr_layout == FR_LEAF)
5943 {
5944 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5945# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005946 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947# endif
5948 wp = sn->fr_win;
5949 }
5950 if (sn->fr_next != NULL)
5951 {
5952 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5953 if (wp2 != NULL)
5954 wp = wp2;
5955 }
5956 if (sn->fr_child != NULL)
5957 {
5958 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5959 if (wp2 != NULL)
5960 wp = wp2;
5961 }
5962 return wp;
5963}
5964
5965#endif
5966
5967#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5968/*
5969 * Return TRUE if there is any vertically split window.
5970 */
5971 int
5972win_hasvertsplit()
5973{
5974 frame_T *fr;
5975
5976 if (topframe->fr_layout == FR_ROW)
5977 return TRUE;
5978
5979 if (topframe->fr_layout == FR_COL)
5980 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5981 if (fr->fr_layout == FR_ROW)
5982 return TRUE;
5983
5984 return FALSE;
5985}
5986#endif