blob: c37ec6769797d9bbebfbc02be8fc65301e8697bb [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 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003116# ifdef FEAT_GUI
3117 int i;
3118
3119 for (i = 0; i < 3; i++)
3120 tp->tp_prev_which_scrollbars[i] = -1;
3121# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003122# ifdef FEAT_DIFF
3123 tp->tp_diff_invalid = TRUE;
3124# endif
3125 }
3126 return tp;
3127}
3128
3129 static void
3130free_tabpage(tp)
3131 tabpage_T *tp;
3132{
3133# ifdef FEAT_DIFF
3134 diff_clear(tp);
3135# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003136 clear_snapshot(tp);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003137 vim_free(tp);
3138}
3139
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003140/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003141 * Create a new Tab page with one window.
3142 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003143 * When "after" is 0 put it just after the current Tab page.
3144 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003145 * Return FAIL or OK.
3146 */
3147 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003148win_new_tabpage(after)
3149 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003150{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003151 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003152 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003153 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003154
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003155 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003156 if (newtp == NULL)
3157 return FAIL;
3158
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003159 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003160 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003161 {
3162 vim_free(newtp);
3163 return FAIL;
3164 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003165 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003166
3167 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003168 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003169 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003170 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003171 if (after == 1)
3172 {
3173 /* New tab page becomes the first one. */
3174 newtp->tp_next = first_tabpage;
3175 first_tabpage = newtp;
3176 }
3177 else
3178 {
3179 if (after > 0)
3180 {
3181 /* Put new tab page before tab page "after". */
3182 n = 2;
3183 for (tp = first_tabpage; tp->tp_next != NULL
3184 && n < after; tp = tp->tp_next)
3185 ++n;
3186 }
3187 newtp->tp_next = tp->tp_next;
3188 tp->tp_next = newtp;
3189 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003190 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003191 firstwin->w_winrow = tabline_height();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003192
3193 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003194 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003196#ifdef FEAT_AUTOCMD
3197 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3198 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3199#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200 return OK;
3201 }
3202
3203 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003204 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003205 return FAIL;
3206}
3207
3208/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3210 * like with ":split".
3211 * Returns OK if a new tab page was created, FAIL otherwise.
3212 */
3213 int
3214may_open_tabpage()
3215{
3216 int n = cmdmod.tab;
3217
3218 if (cmdmod.tab != 0)
3219 {
3220 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3221 return win_new_tabpage(n);
3222 }
3223 return FAIL;
3224}
3225
3226/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003227 * Create up to "maxcount" tabpages with empty windows.
3228 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003229 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003230 int
3231make_tabpages(maxcount)
3232 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003233{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003234 int count = maxcount;
3235 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003236
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003237 /* Limit to 'tabpagemax' tabs. */
3238 if (count > p_tpm)
3239 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003240
3241#ifdef FEAT_AUTOCMD
3242 /*
3243 * Don't execute autocommands while creating the tab pages. Must do that
3244 * when putting the buffers in the windows.
3245 */
3246 ++autocmd_block;
3247#endif
3248
3249 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003250 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003252
3253#ifdef FEAT_AUTOCMD
3254 --autocmd_block;
3255#endif
3256
3257 /* return actual number of tab pages */
3258 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259}
3260
3261/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003262 * Return TRUE when "tpc" points to a valid tab page.
3263 */
3264 int
3265valid_tabpage(tpc)
3266 tabpage_T *tpc;
3267{
3268 tabpage_T *tp;
3269
3270 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3271 if (tp == tpc)
3272 return TRUE;
3273 return FALSE;
3274}
3275
3276/*
3277 * Find tab page "n" (first one is 1). Returns NULL when not found.
3278 */
3279 tabpage_T *
3280find_tabpage(n)
3281 int n;
3282{
3283 tabpage_T *tp;
3284 int i = 1;
3285
3286 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3287 ++i;
3288 return tp;
3289}
3290
3291/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003292 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003293 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003294 */
3295 int
3296tabpage_index(ftp)
3297 tabpage_T *ftp;
3298{
3299 int i = 1;
3300 tabpage_T *tp;
3301
3302 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3303 ++i;
3304 return i;
3305}
3306
3307/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003308 * Prepare for leaving the current tab page.
3309 * When autocomands change "curtab" we don't leave the tab page and return
3310 * FAIL.
3311 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003312 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003313/*ARGSUSED*/
3314 static int
3315leave_tabpage(new_curbuf)
3316 buf_T *new_curbuf; /* what is going to be the new curbuf,
3317 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003318{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003319 tabpage_T *tp = curtab;
3320
3321#ifdef FEAT_AUTOCMD
3322 if (new_curbuf != curbuf)
3323 {
3324 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3325 if (curtab != tp)
3326 return FAIL;
3327 }
3328 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3329 if (curtab != tp)
3330 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003331 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003332 if (curtab != tp)
3333 return FAIL;
3334#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003335#if defined(FEAT_GUI)
3336 /* Remove the scrollbars. They may be added back later. */
3337 if (gui.in_use)
3338 gui_remove_scrollbars();
3339#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003340 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003341 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342 tp->tp_firstwin = firstwin;
3343 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003344 tp->tp_old_Rows = Rows;
3345 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 firstwin = NULL;
3347 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003348 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349}
3350
3351/*
3352 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003353 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003354 */
3355/*ARGSUSED*/
3356 static void
3357enter_tabpage(tp, old_curbuf)
3358 tabpage_T *tp;
3359 buf_T *old_curbuf;
3360{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003361 int old_off = tp->tp_firstwin->w_winrow;
3362
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003363 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003364 firstwin = tp->tp_firstwin;
3365 lastwin = tp->tp_lastwin;
3366 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003367#ifdef FEAT_AUTOCMD
3368 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3369#endif
3370
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003371 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003372 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003373
3374#ifdef FEAT_AUTOCMD
3375 if (old_curbuf != curbuf)
3376 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3377#endif
3378
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003379 last_status(FALSE); /* status line may appear or disappear */
3380 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003381 must_redraw = CLEAR; /* need to redraw everything */
3382#ifdef FEAT_DIFF
3383 diff_need_scrollbind = TRUE;
3384#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003386 /* The tabpage line may have appeared or disappeared, may need to resize
3387 * the frames for that. When the Vim window was resized need to update
3388 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3390#ifdef FEAT_GUI_TABLINE
3391 && !gui_use_tabline()
3392#endif
3393 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003394 shell_new_rows();
3395#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003396 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003397 shell_new_columns(); /* update window widths */
3398#endif
3399
3400#if defined(FEAT_GUI)
3401 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3402 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003403 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003404 {
3405 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003406 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003407 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003408 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003409#endif
3410
3411 redraw_all_later(CLEAR);
3412}
3413
3414/*
3415 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003416 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003417 */
3418 void
3419goto_tabpage(n)
3420 int n;
3421{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003423 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424 int i;
3425
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003426 /* If there is only one it can't work. */
3427 if (first_tabpage->tp_next == NULL)
3428 {
3429 if (n > 1)
3430 beep_flush();
3431 return;
3432 }
3433
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 if (n == 0)
3435 {
3436 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003437 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 tp = first_tabpage;
3439 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003440 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003442 else if (n < 0)
3443 {
3444 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3445 * this N times. */
3446 ttp = curtab;
3447 for (i = n; i < 0; ++i)
3448 {
3449 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3450 tp = tp->tp_next)
3451 ;
3452 ttp = tp;
3453 }
3454 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003455 else if (n == 9999)
3456 {
3457 /* Go to last tab page. */
3458 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3459 ;
3460 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003461 else
3462 {
3463 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003464 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003465 if (tp == NULL)
3466 {
3467 beep_flush();
3468 return;
3469 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470 }
3471
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003472 goto_tabpage_tp(tp);
3473
3474#ifdef FEAT_GUI_TABLINE
3475 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003476 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003477#endif
3478}
3479
3480/*
3481 * Go to tabpage "tp".
3482 * Note: doesn't update the GUI tab.
3483 */
3484 void
3485goto_tabpage_tp(tp)
3486 tabpage_T *tp;
3487{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003488 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003489 {
3490 if (valid_tabpage(tp))
3491 enter_tabpage(tp, curbuf);
3492 else
3493 enter_tabpage(curtab, curbuf);
3494 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003498 * Move the current tab page to before tab page "nr".
3499 */
3500 void
3501tabpage_move(nr)
3502 int nr;
3503{
3504 int n = nr;
3505 tabpage_T *tp;
3506
3507 if (first_tabpage->tp_next == NULL)
3508 return;
3509
3510 /* Remove the current tab page from the list of tab pages. */
3511 if (curtab == first_tabpage)
3512 first_tabpage = curtab->tp_next;
3513 else
3514 {
3515 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3516 if (tp->tp_next == curtab)
3517 break;
3518 if (tp == NULL) /* "cannot happen" */
3519 return;
3520 tp->tp_next = curtab->tp_next;
3521 }
3522
3523 /* Re-insert it at the specified position. */
3524 if (n == 0)
3525 {
3526 curtab->tp_next = first_tabpage;
3527 first_tabpage = curtab;
3528 }
3529 else
3530 {
3531 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3532 --n;
3533 curtab->tp_next = tp->tp_next;
3534 tp->tp_next = curtab;
3535 }
3536
3537 /* Need to redraw the tabline. Tab page contents doesn't change. */
3538 redraw_tabline = TRUE;
3539}
3540
3541
3542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 * Go to another window.
3544 * When jumping to another buffer, stop Visual mode. Do this before
3545 * changing windows so we can yank the selection into the '*' register.
3546 * When jumping to another window on the same buffer, adjust its cursor
3547 * position to keep the same Visual area.
3548 */
3549 void
3550win_goto(wp)
3551 win_T *wp;
3552{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003553 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003556 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 return;
3558 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#ifdef FEAT_VISUAL
3561 if (wp->w_buffer != curbuf)
3562 reset_VIsual_and_resel();
3563 else if (VIsual_active)
3564 wp->w_cursor = curwin->w_cursor;
3565#endif
3566
3567#ifdef FEAT_GUI
3568 need_mouse_correct = TRUE;
3569#endif
3570 win_enter(wp, TRUE);
3571}
3572
3573#if defined(FEAT_PERL) || defined(PROTO)
3574/*
3575 * Find window number "winnr" (counting top to bottom).
3576 */
3577 win_T *
3578win_find_nr(winnr)
3579 int winnr;
3580{
3581 win_T *wp;
3582
3583# ifdef FEAT_WINDOWS
3584 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3585 if (--winnr == 0)
3586 break;
3587 return wp;
3588# else
3589 return curwin;
3590# endif
3591}
3592#endif
3593
3594#ifdef FEAT_VERTSPLIT
3595/*
3596 * Move to window above or below "count" times.
3597 */
3598 static void
3599win_goto_ver(up, count)
3600 int up; /* TRUE to go to win above */
3601 long count;
3602{
3603 frame_T *fr;
3604 frame_T *nfr;
3605 frame_T *foundfr;
3606
3607 foundfr = curwin->w_frame;
3608 while (count--)
3609 {
3610 /*
3611 * First go upwards in the tree of frames until we find a upwards or
3612 * downwards neighbor.
3613 */
3614 fr = foundfr;
3615 for (;;)
3616 {
3617 if (fr == topframe)
3618 goto end;
3619 if (up)
3620 nfr = fr->fr_prev;
3621 else
3622 nfr = fr->fr_next;
3623 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3624 break;
3625 fr = fr->fr_parent;
3626 }
3627
3628 /*
3629 * Now go downwards to find the bottom or top frame in it.
3630 */
3631 for (;;)
3632 {
3633 if (nfr->fr_layout == FR_LEAF)
3634 {
3635 foundfr = nfr;
3636 break;
3637 }
3638 fr = nfr->fr_child;
3639 if (nfr->fr_layout == FR_ROW)
3640 {
3641 /* Find the frame at the cursor row. */
3642 while (fr->fr_next != NULL
3643 && frame2win(fr)->w_wincol + fr->fr_width
3644 <= curwin->w_wincol + curwin->w_wcol)
3645 fr = fr->fr_next;
3646 }
3647 if (nfr->fr_layout == FR_COL && up)
3648 while (fr->fr_next != NULL)
3649 fr = fr->fr_next;
3650 nfr = fr;
3651 }
3652 }
3653end:
3654 if (foundfr != NULL)
3655 win_goto(foundfr->fr_win);
3656}
3657
3658/*
3659 * Move to left or right window.
3660 */
3661 static void
3662win_goto_hor(left, count)
3663 int left; /* TRUE to go to left win */
3664 long count;
3665{
3666 frame_T *fr;
3667 frame_T *nfr;
3668 frame_T *foundfr;
3669
3670 foundfr = curwin->w_frame;
3671 while (count--)
3672 {
3673 /*
3674 * First go upwards in the tree of frames until we find a left or
3675 * right neighbor.
3676 */
3677 fr = foundfr;
3678 for (;;)
3679 {
3680 if (fr == topframe)
3681 goto end;
3682 if (left)
3683 nfr = fr->fr_prev;
3684 else
3685 nfr = fr->fr_next;
3686 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3687 break;
3688 fr = fr->fr_parent;
3689 }
3690
3691 /*
3692 * Now go downwards to find the leftmost or rightmost frame in it.
3693 */
3694 for (;;)
3695 {
3696 if (nfr->fr_layout == FR_LEAF)
3697 {
3698 foundfr = nfr;
3699 break;
3700 }
3701 fr = nfr->fr_child;
3702 if (nfr->fr_layout == FR_COL)
3703 {
3704 /* Find the frame at the cursor row. */
3705 while (fr->fr_next != NULL
3706 && frame2win(fr)->w_winrow + fr->fr_height
3707 <= curwin->w_winrow + curwin->w_wrow)
3708 fr = fr->fr_next;
3709 }
3710 if (nfr->fr_layout == FR_ROW && left)
3711 while (fr->fr_next != NULL)
3712 fr = fr->fr_next;
3713 nfr = fr;
3714 }
3715 }
3716end:
3717 if (foundfr != NULL)
3718 win_goto(foundfr->fr_win);
3719}
3720#endif
3721
3722/*
3723 * Make window "wp" the current window.
3724 */
3725 void
3726win_enter(wp, undo_sync)
3727 win_T *wp;
3728 int undo_sync;
3729{
3730 win_enter_ext(wp, undo_sync, FALSE);
3731}
3732
3733/*
3734 * Make window wp the current window.
3735 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3736 * been closed and isn't valid.
3737 */
3738 static void
3739win_enter_ext(wp, undo_sync, curwin_invalid)
3740 win_T *wp;
3741 int undo_sync;
3742 int curwin_invalid;
3743{
3744#ifdef FEAT_AUTOCMD
3745 int other_buffer = FALSE;
3746#endif
3747
3748 if (wp == curwin && !curwin_invalid) /* nothing to do */
3749 return;
3750
3751#ifdef FEAT_AUTOCMD
3752 if (!curwin_invalid)
3753 {
3754 /*
3755 * Be careful: If autocommands delete the window, return now.
3756 */
3757 if (wp->w_buffer != curbuf)
3758 {
3759 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3760 other_buffer = TRUE;
3761 if (!win_valid(wp))
3762 return;
3763 }
3764 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3765 if (!win_valid(wp))
3766 return;
3767# ifdef FEAT_EVAL
3768 /* autocmds may abort script processing */
3769 if (aborting())
3770 return;
3771# endif
3772 }
3773#endif
3774
3775 /* sync undo before leaving the current buffer */
3776 if (undo_sync && curbuf != wp->w_buffer)
3777 u_sync();
3778 /* may have to copy the buffer options when 'cpo' contains 'S' */
3779 if (wp->w_buffer != curbuf)
3780 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3781 if (!curwin_invalid)
3782 {
3783 prevwin = curwin; /* remember for CTRL-W p */
3784 curwin->w_redr_status = TRUE;
3785 }
3786 curwin = wp;
3787 curbuf = wp->w_buffer;
3788 check_cursor();
3789#ifdef FEAT_VIRTUALEDIT
3790 if (!virtual_active())
3791 curwin->w_cursor.coladd = 0;
3792#endif
3793 changed_line_abv_curs(); /* assume cursor position needs updating */
3794
3795 if (curwin->w_localdir != NULL)
3796 {
3797 /* Window has a local directory: Save current directory as global
3798 * directory (unless that was done already) and change to the local
3799 * directory. */
3800 if (globaldir == NULL)
3801 {
3802 char_u cwd[MAXPATHL];
3803
3804 if (mch_dirname(cwd, MAXPATHL) == OK)
3805 globaldir = vim_strsave(cwd);
3806 }
3807 mch_chdir((char *)curwin->w_localdir);
3808 shorten_fnames(TRUE);
3809 }
3810 else if (globaldir != NULL)
3811 {
3812 /* Window doesn't have a local directory and we are not in the global
3813 * directory: Change to the global directory. */
3814 mch_chdir((char *)globaldir);
3815 vim_free(globaldir);
3816 globaldir = NULL;
3817 shorten_fnames(TRUE);
3818 }
3819
3820#ifdef FEAT_AUTOCMD
3821 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3822 if (other_buffer)
3823 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3824#endif
3825
3826#ifdef FEAT_TITLE
3827 maketitle();
3828#endif
3829 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003830 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (restart_edit)
3832 redraw_later(VALID); /* causes status line redraw */
3833
3834 /* set window height to desired minimal value */
3835 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3836 win_setheight((int)p_wh);
3837 else if (curwin->w_height == 0)
3838 win_setheight(1);
3839
3840#ifdef FEAT_VERTSPLIT
3841 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003842 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 win_setwidth((int)p_wiw);
3844#endif
3845
3846#ifdef FEAT_MOUSE
3847 setmouse(); /* in case jumped to/from help buffer */
3848#endif
3849
3850#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3851 /* Change directories when the acd option is set on and after
3852 * switching windows. */
3853 if (p_acd && curbuf->b_ffname != NULL
3854 && vim_chdirfile(curbuf->b_ffname) == OK)
3855 shorten_fnames(TRUE);
3856#endif
3857}
3858
3859#endif /* FEAT_WINDOWS */
3860
3861#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3862/*
3863 * Jump to the first open window that contains buffer buf if one exists
3864 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3865 * Returns pointer to window if it exists, otherwise NULL.
3866 */
3867 win_T *
3868buf_jump_open_win(buf)
3869 buf_T *buf;
3870{
3871# ifdef FEAT_WINDOWS
3872 win_T *wp;
3873
3874 for (wp = firstwin; wp; wp = wp->w_next)
3875 if (wp->w_buffer == buf)
3876 break;
3877 if (wp != NULL)
3878 win_enter(wp, FALSE);
3879 return wp;
3880# else
3881 if (curwin->w_buffer == buf)
3882 return curwin;
3883 return NULL;
3884# endif
3885}
3886#endif
3887
3888/*
3889 * allocate a window structure and link it in the window list
3890 */
3891/*ARGSUSED*/
3892 static win_T *
3893win_alloc(after)
3894 win_T *after;
3895{
3896 win_T *newwin;
3897
3898 /*
3899 * allocate window structure and linesizes arrays
3900 */
3901 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3902 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3903 {
3904 vim_free(newwin);
3905 newwin = NULL;
3906 }
3907
3908 if (newwin != NULL)
3909 {
3910 /*
3911 * link the window in the window list
3912 */
3913#ifdef FEAT_WINDOWS
3914 win_append(after, newwin);
3915#endif
3916#ifdef FEAT_VERTSPLIT
3917 newwin->w_wincol = 0;
3918 newwin->w_width = Columns;
3919#endif
3920
3921 /* position the display and the cursor at the top of the file. */
3922 newwin->w_topline = 1;
3923#ifdef FEAT_DIFF
3924 newwin->w_topfill = 0;
3925#endif
3926 newwin->w_botline = 2;
3927 newwin->w_cursor.lnum = 1;
3928#ifdef FEAT_SCROLLBIND
3929 newwin->w_scbind_pos = 1;
3930#endif
3931
3932 /* We won't calculate w_fraction until resizing the window */
3933 newwin->w_fraction = 0;
3934 newwin->w_prev_fraction_row = -1;
3935
3936#ifdef FEAT_GUI
3937 if (gui.in_use)
3938 {
3939 out_flush();
3940 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3941 SBAR_LEFT, newwin);
3942 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3943 SBAR_RIGHT, newwin);
3944 }
3945#endif
3946#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003947 /* init w: variables */
3948 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#endif
3950#ifdef FEAT_FOLDING
3951 foldInitWin(newwin);
3952#endif
3953 }
3954 return newwin;
3955}
3956
3957#if defined(FEAT_WINDOWS) || defined(PROTO)
3958
3959/*
3960 * remove window 'wp' from the window list and free the structure
3961 */
3962 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003963win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003965 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 int i;
3968
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003969#ifdef FEAT_MZSCHEME
3970 mzscheme_window_free(wp);
3971#endif
3972
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973#ifdef FEAT_PERL
3974 perl_win_free(wp);
3975#endif
3976
3977#ifdef FEAT_PYTHON
3978 python_window_free(wp);
3979#endif
3980
3981#ifdef FEAT_TCL
3982 tcl_window_free(wp);
3983#endif
3984
3985#ifdef FEAT_RUBY
3986 ruby_window_free(wp);
3987#endif
3988
3989 clear_winopt(&wp->w_onebuf_opt);
3990 clear_winopt(&wp->w_allbuf_opt);
3991
3992#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003993 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994#endif
3995
3996 if (prevwin == wp)
3997 prevwin = NULL;
3998 win_free_lsize(wp);
3999
4000 for (i = 0; i < wp->w_tagstacklen; ++i)
4001 vim_free(wp->w_tagstack[i].tagname);
4002
4003 vim_free(wp->w_localdir);
4004#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004005 vim_free(wp->w_match[0].regprog);
4006 vim_free(wp->w_match[1].regprog);
4007 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008#endif
4009#ifdef FEAT_JUMPLIST
4010 free_jumplist(wp);
4011#endif
4012
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004013#ifdef FEAT_QUICKFIX
4014 qf_free_all(wp);
4015#endif
4016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef FEAT_GUI
4018 if (gui.in_use)
4019 {
4020 out_flush();
4021 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4022 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4023 }
4024#endif /* FEAT_GUI */
4025
Bram Moolenaarf740b292006-02-16 22:11:02 +00004026 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 vim_free(wp);
4028}
4029
4030/*
4031 * Append window "wp" in the window list after window "after".
4032 */
4033 static void
4034win_append(after, wp)
4035 win_T *after, *wp;
4036{
4037 win_T *before;
4038
4039 if (after == NULL) /* after NULL is in front of the first */
4040 before = firstwin;
4041 else
4042 before = after->w_next;
4043
4044 wp->w_next = before;
4045 wp->w_prev = after;
4046 if (after == NULL)
4047 firstwin = wp;
4048 else
4049 after->w_next = wp;
4050 if (before == NULL)
4051 lastwin = wp;
4052 else
4053 before->w_prev = wp;
4054}
4055
4056/*
4057 * Remove a window from the window list.
4058 */
4059 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004060win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004062 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063{
4064 if (wp->w_prev != NULL)
4065 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004066 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004068 else
4069 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (wp->w_next != NULL)
4071 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004072 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004074 else
4075 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076}
4077
4078/*
4079 * Append frame "frp" in a frame list after frame "after".
4080 */
4081 static void
4082frame_append(after, frp)
4083 frame_T *after, *frp;
4084{
4085 frp->fr_next = after->fr_next;
4086 after->fr_next = frp;
4087 if (frp->fr_next != NULL)
4088 frp->fr_next->fr_prev = frp;
4089 frp->fr_prev = after;
4090}
4091
4092/*
4093 * Insert frame "frp" in a frame list before frame "before".
4094 */
4095 static void
4096frame_insert(before, frp)
4097 frame_T *before, *frp;
4098{
4099 frp->fr_next = before;
4100 frp->fr_prev = before->fr_prev;
4101 before->fr_prev = frp;
4102 if (frp->fr_prev != NULL)
4103 frp->fr_prev->fr_next = frp;
4104 else
4105 frp->fr_parent->fr_child = frp;
4106}
4107
4108/*
4109 * Remove a frame from a frame list.
4110 */
4111 static void
4112frame_remove(frp)
4113 frame_T *frp;
4114{
4115 if (frp->fr_prev != NULL)
4116 frp->fr_prev->fr_next = frp->fr_next;
4117 else
4118 frp->fr_parent->fr_child = frp->fr_next;
4119 if (frp->fr_next != NULL)
4120 frp->fr_next->fr_prev = frp->fr_prev;
4121}
4122
4123#endif /* FEAT_WINDOWS */
4124
4125/*
4126 * Allocate w_lines[] for window "wp".
4127 * Return FAIL for failure, OK for success.
4128 */
4129 int
4130win_alloc_lines(wp)
4131 win_T *wp;
4132{
4133 wp->w_lines_valid = 0;
4134 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4135 if (wp->w_lines == NULL)
4136 return FAIL;
4137 return OK;
4138}
4139
4140/*
4141 * free lsize arrays for a window
4142 */
4143 void
4144win_free_lsize(wp)
4145 win_T *wp;
4146{
4147 vim_free(wp->w_lines);
4148 wp->w_lines = NULL;
4149}
4150
4151/*
4152 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004153 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 */
4155 void
4156shell_new_rows()
4157{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004158 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 if (firstwin == NULL) /* not initialized yet */
4161 return;
4162#ifdef FEAT_WINDOWS
4163 if (h < frame_minheight(topframe, NULL))
4164 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004165
4166 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * that doesn't result in the right height, forget about that option. */
4168 frame_new_height(topframe, h, FALSE, TRUE);
4169 if (topframe->fr_height != h)
4170 frame_new_height(topframe, h, FALSE, FALSE);
4171
4172 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4173#else
4174 if (h < 1)
4175 h = 1;
4176 win_new_height(firstwin, h);
4177#endif
4178 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004179#ifdef FEAT_WINDOWS
4180 p_ch_used = p_ch;
4181#endif
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183#if 0
4184 /* Disabled: don't want making the screen smaller make a window larger. */
4185 if (p_ea)
4186 win_equal(curwin, FALSE, 'v');
4187#endif
4188}
4189
4190#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4191/*
4192 * Called from win_new_shellsize() after Columns changed.
4193 */
4194 void
4195shell_new_columns()
4196{
4197 if (firstwin == NULL) /* not initialized yet */
4198 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004199
4200 /* First try setting the widths of windows with 'winfixwidth'. If that
4201 * doesn't result in the right width, forget about that option. */
4202 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4203 if (topframe->fr_width != Columns)
4204 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4207#if 0
4208 /* Disabled: don't want making the screen smaller make a window larger. */
4209 if (p_ea)
4210 win_equal(curwin, FALSE, 'h');
4211#endif
4212}
4213#endif
4214
4215#if defined(FEAT_CMDWIN) || defined(PROTO)
4216/*
4217 * Save the size of all windows in "gap".
4218 */
4219 void
4220win_size_save(gap)
4221 garray_T *gap;
4222
4223{
4224 win_T *wp;
4225
4226 ga_init2(gap, (int)sizeof(int), 1);
4227 if (ga_grow(gap, win_count() * 2) == OK)
4228 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4229 {
4230 ((int *)gap->ga_data)[gap->ga_len++] =
4231 wp->w_width + wp->w_vsep_width;
4232 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4233 }
4234}
4235
4236/*
4237 * Restore window sizes, but only if the number of windows is still the same.
4238 * Does not free the growarray.
4239 */
4240 void
4241win_size_restore(gap)
4242 garray_T *gap;
4243{
4244 win_T *wp;
4245 int i;
4246
4247 if (win_count() * 2 == gap->ga_len)
4248 {
4249 i = 0;
4250 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4251 {
4252 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4253 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4254 }
4255 /* recompute the window positions */
4256 (void)win_comp_pos();
4257 }
4258}
4259#endif /* FEAT_CMDWIN */
4260
4261#if defined(FEAT_WINDOWS) || defined(PROTO)
4262/*
4263 * Update the position for all windows, using the width and height of the
4264 * frames.
4265 * Returns the row just after the last window.
4266 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004267 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268win_comp_pos()
4269{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004270 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 int col = 0;
4272
4273 frame_comp_pos(topframe, &row, &col);
4274 return row;
4275}
4276
4277/*
4278 * Update the position of the windows in frame "topfrp", using the width and
4279 * height of the frames.
4280 * "*row" and "*col" are the top-left position of the frame. They are updated
4281 * to the bottom-right position plus one.
4282 */
4283 static void
4284frame_comp_pos(topfrp, row, col)
4285 frame_T *topfrp;
4286 int *row;
4287 int *col;
4288{
4289 win_T *wp;
4290 frame_T *frp;
4291#ifdef FEAT_VERTSPLIT
4292 int startcol;
4293 int startrow;
4294#endif
4295
4296 wp = topfrp->fr_win;
4297 if (wp != NULL)
4298 {
4299 if (wp->w_winrow != *row
4300#ifdef FEAT_VERTSPLIT
4301 || wp->w_wincol != *col
4302#endif
4303 )
4304 {
4305 /* position changed, redraw */
4306 wp->w_winrow = *row;
4307#ifdef FEAT_VERTSPLIT
4308 wp->w_wincol = *col;
4309#endif
4310 redraw_win_later(wp, NOT_VALID);
4311 wp->w_redr_status = TRUE;
4312 }
4313 *row += wp->w_height + wp->w_status_height;
4314#ifdef FEAT_VERTSPLIT
4315 *col += wp->w_width + wp->w_vsep_width;
4316#endif
4317 }
4318 else
4319 {
4320#ifdef FEAT_VERTSPLIT
4321 startrow = *row;
4322 startcol = *col;
4323#endif
4324 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4325 {
4326#ifdef FEAT_VERTSPLIT
4327 if (topfrp->fr_layout == FR_ROW)
4328 *row = startrow; /* all frames are at the same row */
4329 else
4330 *col = startcol; /* all frames are at the same col */
4331#endif
4332 frame_comp_pos(frp, row, col);
4333 }
4334 }
4335}
4336
4337#endif /* FEAT_WINDOWS */
4338
4339/*
4340 * Set current window height and take care of repositioning other windows to
4341 * fit around it.
4342 */
4343 void
4344win_setheight(height)
4345 int height;
4346{
4347 win_setheight_win(height, curwin);
4348}
4349
4350/*
4351 * Set the window height of window "win" and take care of repositioning other
4352 * windows to fit around it.
4353 */
4354 void
4355win_setheight_win(height, win)
4356 int height;
4357 win_T *win;
4358{
4359 int row;
4360
4361 if (win == curwin)
4362 {
4363 /* Always keep current window at least one line high, even when
4364 * 'winminheight' is zero. */
4365#ifdef FEAT_WINDOWS
4366 if (height < p_wmh)
4367 height = p_wmh;
4368#endif
4369 if (height == 0)
4370 height = 1;
4371 }
4372
4373#ifdef FEAT_WINDOWS
4374 frame_setheight(win->w_frame, height + win->w_status_height);
4375
4376 /* recompute the window positions */
4377 row = win_comp_pos();
4378#else
4379 if (height > topframe->fr_height)
4380 height = topframe->fr_height;
4381 win->w_height = height;
4382 row = height;
4383#endif
4384
4385 /*
4386 * If there is extra space created between the last window and the command
4387 * line, clear it.
4388 */
4389 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4390 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4391 cmdline_row = row;
4392 msg_row = row;
4393 msg_col = 0;
4394
4395 redraw_all_later(NOT_VALID);
4396}
4397
4398#if defined(FEAT_WINDOWS) || defined(PROTO)
4399
4400/*
4401 * Set the height of a frame to "height" and take care that all frames and
4402 * windows inside it are resized. Also resize frames on the left and right if
4403 * the are in the same FR_ROW frame.
4404 *
4405 * Strategy:
4406 * If the frame is part of a FR_COL frame, try fitting the frame in that
4407 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4408 * go to containing frames to resize them and make room.
4409 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4410 * Check for the minimal height of the FR_ROW frame.
4411 * At the top level we can also use change the command line height.
4412 */
4413 static void
4414frame_setheight(curfrp, height)
4415 frame_T *curfrp;
4416 int height;
4417{
4418 int room; /* total number of lines available */
4419 int take; /* number of lines taken from other windows */
4420 int room_cmdline; /* lines available from cmdline */
4421 int run;
4422 frame_T *frp;
4423 int h;
4424 int room_reserved;
4425
4426 /* If the height already is the desired value, nothing to do. */
4427 if (curfrp->fr_height == height)
4428 return;
4429
4430 if (curfrp->fr_parent == NULL)
4431 {
4432 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004433 if (height > ROWS_AVAIL)
4434 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (height > 0)
4436 frame_new_height(curfrp, height, FALSE, FALSE);
4437 }
4438 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4439 {
4440 /* Row of frames: Also need to resize frames left and right of this
4441 * one. First check for the minimal height of these. */
4442 h = frame_minheight(curfrp->fr_parent, NULL);
4443 if (height < h)
4444 height = h;
4445 frame_setheight(curfrp->fr_parent, height);
4446 }
4447 else
4448 {
4449 /*
4450 * Column of frames: try to change only frames in this column.
4451 */
4452#ifdef FEAT_VERTSPLIT
4453 /*
4454 * Do this twice:
4455 * 1: compute room available, if it's not enough try resizing the
4456 * containing frame.
4457 * 2: compute the room available and adjust the height to it.
4458 * Try not to reduce the height of a window with 'winfixheight' set.
4459 */
4460 for (run = 1; run <= 2; ++run)
4461#else
4462 for (;;)
4463#endif
4464 {
4465 room = 0;
4466 room_reserved = 0;
4467 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4468 frp = frp->fr_next)
4469 {
4470 if (frp != curfrp
4471 && frp->fr_win != NULL
4472 && frp->fr_win->w_p_wfh)
4473 room_reserved += frp->fr_height;
4474 room += frp->fr_height;
4475 if (frp != curfrp)
4476 room -= frame_minheight(frp, NULL);
4477 }
4478#ifdef FEAT_VERTSPLIT
4479 if (curfrp->fr_width != Columns)
4480 room_cmdline = 0;
4481 else
4482#endif
4483 {
4484 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4485 + lastwin->w_height + lastwin->w_status_height);
4486 if (room_cmdline < 0)
4487 room_cmdline = 0;
4488 }
4489
4490 if (height <= room + room_cmdline)
4491 break;
4492#ifdef FEAT_VERTSPLIT
4493 if (run == 2 || curfrp->fr_width == Columns)
4494#endif
4495 {
4496 if (height > room + room_cmdline)
4497 height = room + room_cmdline;
4498 break;
4499 }
4500#ifdef FEAT_VERTSPLIT
4501 frame_setheight(curfrp->fr_parent, height
4502 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4503#endif
4504 /*NOTREACHED*/
4505 }
4506
4507 /*
4508 * Compute the number of lines we will take from others frames (can be
4509 * negative!).
4510 */
4511 take = height - curfrp->fr_height;
4512
4513 /* If there is not enough room, also reduce the height of a window
4514 * with 'winfixheight' set. */
4515 if (height > room + room_cmdline - room_reserved)
4516 room_reserved = room + room_cmdline - height;
4517 /* If there is only a 'winfixheight' window and making the
4518 * window smaller, need to make the other window taller. */
4519 if (take < 0 && room - curfrp->fr_height < room_reserved)
4520 room_reserved = 0;
4521
4522 if (take > 0 && room_cmdline > 0)
4523 {
4524 /* use lines from cmdline first */
4525 if (take < room_cmdline)
4526 room_cmdline = take;
4527 take -= room_cmdline;
4528 topframe->fr_height += room_cmdline;
4529 }
4530
4531 /*
4532 * set the current frame to the new height
4533 */
4534 frame_new_height(curfrp, height, FALSE, FALSE);
4535
4536 /*
4537 * First take lines from the frames after the current frame. If
4538 * that is not enough, takes lines from frames above the current
4539 * frame.
4540 */
4541 for (run = 0; run < 2; ++run)
4542 {
4543 if (run == 0)
4544 frp = curfrp->fr_next; /* 1st run: start with next window */
4545 else
4546 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4547 while (frp != NULL && take != 0)
4548 {
4549 h = frame_minheight(frp, NULL);
4550 if (room_reserved > 0
4551 && frp->fr_win != NULL
4552 && frp->fr_win->w_p_wfh)
4553 {
4554 if (room_reserved >= frp->fr_height)
4555 room_reserved -= frp->fr_height;
4556 else
4557 {
4558 if (frp->fr_height - room_reserved > take)
4559 room_reserved = frp->fr_height - take;
4560 take -= frp->fr_height - room_reserved;
4561 frame_new_height(frp, room_reserved, FALSE, FALSE);
4562 room_reserved = 0;
4563 }
4564 }
4565 else
4566 {
4567 if (frp->fr_height - take < h)
4568 {
4569 take -= frp->fr_height - h;
4570 frame_new_height(frp, h, FALSE, FALSE);
4571 }
4572 else
4573 {
4574 frame_new_height(frp, frp->fr_height - take,
4575 FALSE, FALSE);
4576 take = 0;
4577 }
4578 }
4579 if (run == 0)
4580 frp = frp->fr_next;
4581 else
4582 frp = frp->fr_prev;
4583 }
4584 }
4585 }
4586}
4587
4588#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4589/*
4590 * Set current window width and take care of repositioning other windows to
4591 * fit around it.
4592 */
4593 void
4594win_setwidth(width)
4595 int width;
4596{
4597 win_setwidth_win(width, curwin);
4598}
4599
4600 void
4601win_setwidth_win(width, wp)
4602 int width;
4603 win_T *wp;
4604{
4605 /* Always keep current window at least one column wide, even when
4606 * 'winminwidth' is zero. */
4607 if (wp == curwin)
4608 {
4609 if (width < p_wmw)
4610 width = p_wmw;
4611 if (width == 0)
4612 width = 1;
4613 }
4614
4615 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4616
4617 /* recompute the window positions */
4618 (void)win_comp_pos();
4619
4620 redraw_all_later(NOT_VALID);
4621}
4622
4623/*
4624 * Set the width of a frame to "width" and take care that all frames and
4625 * windows inside it are resized. Also resize frames above and below if the
4626 * are in the same FR_ROW frame.
4627 *
4628 * Strategy is similar to frame_setheight().
4629 */
4630 static void
4631frame_setwidth(curfrp, width)
4632 frame_T *curfrp;
4633 int width;
4634{
4635 int room; /* total number of lines available */
4636 int take; /* number of lines taken from other windows */
4637 int run;
4638 frame_T *frp;
4639 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004640 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
4642 /* If the width already is the desired value, nothing to do. */
4643 if (curfrp->fr_width == width)
4644 return;
4645
4646 if (curfrp->fr_parent == NULL)
4647 /* topframe: can't change width */
4648 return;
4649
4650 if (curfrp->fr_parent->fr_layout == FR_COL)
4651 {
4652 /* Column of frames: Also need to resize frames above and below of
4653 * this one. First check for the minimal width of these. */
4654 w = frame_minwidth(curfrp->fr_parent, NULL);
4655 if (width < w)
4656 width = w;
4657 frame_setwidth(curfrp->fr_parent, width);
4658 }
4659 else
4660 {
4661 /*
4662 * Row of frames: try to change only frames in this row.
4663 *
4664 * Do this twice:
4665 * 1: compute room available, if it's not enough try resizing the
4666 * containing frame.
4667 * 2: compute the room available and adjust the width to it.
4668 */
4669 for (run = 1; run <= 2; ++run)
4670 {
4671 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004672 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4674 frp = frp->fr_next)
4675 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004676 if (frp != curfrp
4677 && frp->fr_win != NULL
4678 && frp->fr_win->w_p_wfw)
4679 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 room += frp->fr_width;
4681 if (frp != curfrp)
4682 room -= frame_minwidth(frp, NULL);
4683 }
4684
4685 if (width <= room)
4686 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004687 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 if (width > room)
4690 width = room;
4691 break;
4692 }
4693 frame_setwidth(curfrp->fr_parent, width
4694 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4695 }
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 /*
4698 * Compute the number of lines we will take from others frames (can be
4699 * negative!).
4700 */
4701 take = width - curfrp->fr_width;
4702
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004703 /* If there is not enough room, also reduce the width of a window
4704 * with 'winfixwidth' set. */
4705 if (width > room - room_reserved)
4706 room_reserved = room - width;
4707 /* If there is only a 'winfixwidth' window and making the
4708 * window smaller, need to make the other window narrower. */
4709 if (take < 0 && room - curfrp->fr_width < room_reserved)
4710 room_reserved = 0;
4711
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 /*
4713 * set the current frame to the new width
4714 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004715 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 /*
4718 * First take lines from the frames right of the current frame. If
4719 * that is not enough, takes lines from frames left of the current
4720 * frame.
4721 */
4722 for (run = 0; run < 2; ++run)
4723 {
4724 if (run == 0)
4725 frp = curfrp->fr_next; /* 1st run: start with next window */
4726 else
4727 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4728 while (frp != NULL && take != 0)
4729 {
4730 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004731 if (room_reserved > 0
4732 && frp->fr_win != NULL
4733 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004735 if (room_reserved >= frp->fr_width)
4736 room_reserved -= frp->fr_width;
4737 else
4738 {
4739 if (frp->fr_width - room_reserved > take)
4740 room_reserved = frp->fr_width - take;
4741 take -= frp->fr_width - room_reserved;
4742 frame_new_width(frp, room_reserved, FALSE, FALSE);
4743 room_reserved = 0;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 else
4747 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004748 if (frp->fr_width - take < w)
4749 {
4750 take -= frp->fr_width - w;
4751 frame_new_width(frp, w, FALSE, FALSE);
4752 }
4753 else
4754 {
4755 frame_new_width(frp, frp->fr_width - take,
4756 FALSE, FALSE);
4757 take = 0;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 if (run == 0)
4761 frp = frp->fr_next;
4762 else
4763 frp = frp->fr_prev;
4764 }
4765 }
4766 }
4767}
4768#endif /* FEAT_VERTSPLIT */
4769
4770/*
4771 * Check 'winminheight' for a valid value.
4772 */
4773 void
4774win_setminheight()
4775{
4776 int room;
4777 int first = TRUE;
4778 win_T *wp;
4779
4780 /* loop until there is a 'winminheight' that is possible */
4781 while (p_wmh > 0)
4782 {
4783 /* TODO: handle vertical splits */
4784 room = -p_wh;
4785 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4786 room += wp->w_height - p_wmh;
4787 if (room >= 0)
4788 break;
4789 --p_wmh;
4790 if (first)
4791 {
4792 EMSG(_(e_noroom));
4793 first = FALSE;
4794 }
4795 }
4796}
4797
4798#ifdef FEAT_MOUSE
4799
4800/*
4801 * Status line of dragwin is dragged "offset" lines down (negative is up).
4802 */
4803 void
4804win_drag_status_line(dragwin, offset)
4805 win_T *dragwin;
4806 int offset;
4807{
4808 frame_T *curfr;
4809 frame_T *fr;
4810 int room;
4811 int row;
4812 int up; /* if TRUE, drag status line up, otherwise down */
4813 int n;
4814
4815 fr = dragwin->w_frame;
4816 curfr = fr;
4817 if (fr != topframe) /* more than one window */
4818 {
4819 fr = fr->fr_parent;
4820 /* When the parent frame is not a column of frames, its parent should
4821 * be. */
4822 if (fr->fr_layout != FR_COL)
4823 {
4824 curfr = fr;
4825 if (fr != topframe) /* only a row of windows, may drag statusline */
4826 fr = fr->fr_parent;
4827 }
4828 }
4829
4830 /* If this is the last frame in a column, may want to resize the parent
4831 * frame instead (go two up to skip a row of frames). */
4832 while (curfr != topframe && curfr->fr_next == NULL)
4833 {
4834 if (fr != topframe)
4835 fr = fr->fr_parent;
4836 curfr = fr;
4837 if (fr != topframe)
4838 fr = fr->fr_parent;
4839 }
4840
4841 if (offset < 0) /* drag up */
4842 {
4843 up = TRUE;
4844 offset = -offset;
4845 /* sum up the room of the current frame and above it */
4846 if (fr == curfr)
4847 {
4848 /* only one window */
4849 room = fr->fr_height - frame_minheight(fr, NULL);
4850 }
4851 else
4852 {
4853 room = 0;
4854 for (fr = fr->fr_child; ; fr = fr->fr_next)
4855 {
4856 room += fr->fr_height - frame_minheight(fr, NULL);
4857 if (fr == curfr)
4858 break;
4859 }
4860 }
4861 fr = curfr->fr_next; /* put fr at frame that grows */
4862 }
4863 else /* drag down */
4864 {
4865 up = FALSE;
4866 /*
4867 * Only dragging the last status line can reduce p_ch.
4868 */
4869 room = Rows - cmdline_row;
4870 if (curfr->fr_next == NULL)
4871 room -= 1;
4872 else
4873 room -= p_ch;
4874 if (room < 0)
4875 room = 0;
4876 /* sum up the room of frames below of the current one */
4877 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4878 room += fr->fr_height - frame_minheight(fr, NULL);
4879 fr = curfr; /* put fr at window that grows */
4880 }
4881
4882 if (room < offset) /* Not enough room */
4883 offset = room; /* Move as far as we can */
4884 if (offset <= 0)
4885 return;
4886
4887 /*
4888 * Grow frame fr by "offset" lines.
4889 * Doesn't happen when dragging the last status line up.
4890 */
4891 if (fr != NULL)
4892 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4893
4894 if (up)
4895 fr = curfr; /* current frame gets smaller */
4896 else
4897 fr = curfr->fr_next; /* next frame gets smaller */
4898
4899 /*
4900 * Now make the other frames smaller.
4901 */
4902 while (fr != NULL && offset > 0)
4903 {
4904 n = frame_minheight(fr, NULL);
4905 if (fr->fr_height - offset <= n)
4906 {
4907 offset -= fr->fr_height - n;
4908 frame_new_height(fr, n, !up, FALSE);
4909 }
4910 else
4911 {
4912 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4913 break;
4914 }
4915 if (up)
4916 fr = fr->fr_prev;
4917 else
4918 fr = fr->fr_next;
4919 }
4920 row = win_comp_pos();
4921 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4922 cmdline_row = row;
4923 p_ch = Rows - cmdline_row;
4924 if (p_ch < 1)
4925 p_ch = 1;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004926 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 showmode();
4928}
4929
4930#ifdef FEAT_VERTSPLIT
4931/*
4932 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4933 */
4934 void
4935win_drag_vsep_line(dragwin, offset)
4936 win_T *dragwin;
4937 int offset;
4938{
4939 frame_T *curfr;
4940 frame_T *fr;
4941 int room;
4942 int left; /* if TRUE, drag separator line left, otherwise right */
4943 int n;
4944
4945 fr = dragwin->w_frame;
4946 if (fr == topframe) /* only one window (cannot happe?) */
4947 return;
4948 curfr = fr;
4949 fr = fr->fr_parent;
4950 /* When the parent frame is not a row of frames, its parent should be. */
4951 if (fr->fr_layout != FR_ROW)
4952 {
4953 if (fr == topframe) /* only a column of windows (cannot happen?) */
4954 return;
4955 curfr = fr;
4956 fr = fr->fr_parent;
4957 }
4958
4959 /* If this is the last frame in a row, may want to resize a parent
4960 * frame instead. */
4961 while (curfr->fr_next == NULL)
4962 {
4963 if (fr == topframe)
4964 break;
4965 curfr = fr;
4966 fr = fr->fr_parent;
4967 if (fr != topframe)
4968 {
4969 curfr = fr;
4970 fr = fr->fr_parent;
4971 }
4972 }
4973
4974 if (offset < 0) /* drag left */
4975 {
4976 left = TRUE;
4977 offset = -offset;
4978 /* sum up the room of the current frame and left of it */
4979 room = 0;
4980 for (fr = fr->fr_child; ; fr = fr->fr_next)
4981 {
4982 room += fr->fr_width - frame_minwidth(fr, NULL);
4983 if (fr == curfr)
4984 break;
4985 }
4986 fr = curfr->fr_next; /* put fr at frame that grows */
4987 }
4988 else /* drag right */
4989 {
4990 left = FALSE;
4991 /* sum up the room of frames right of the current one */
4992 room = 0;
4993 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4994 room += fr->fr_width - frame_minwidth(fr, NULL);
4995 fr = curfr; /* put fr at window that grows */
4996 }
4997
4998 if (room < offset) /* Not enough room */
4999 offset = room; /* Move as far as we can */
5000 if (offset <= 0) /* No room at all, quit. */
5001 return;
5002
5003 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005004 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /* shrink other frames: current and at the left or at the right */
5007 if (left)
5008 fr = curfr; /* current frame gets smaller */
5009 else
5010 fr = curfr->fr_next; /* next frame gets smaller */
5011
5012 while (fr != NULL && offset > 0)
5013 {
5014 n = frame_minwidth(fr, NULL);
5015 if (fr->fr_width - offset <= n)
5016 {
5017 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005018 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else
5021 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005022 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024 }
5025 if (left)
5026 fr = fr->fr_prev;
5027 else
5028 fr = fr->fr_next;
5029 }
5030 (void)win_comp_pos();
5031 redraw_all_later(NOT_VALID);
5032}
5033#endif /* FEAT_VERTSPLIT */
5034#endif /* FEAT_MOUSE */
5035
5036#endif /* FEAT_WINDOWS */
5037
5038/*
5039 * Set the height of a window.
5040 * This takes care of the things inside the window, not what happens to the
5041 * window position, the frame or to other windows.
5042 */
5043 static void
5044win_new_height(wp, height)
5045 win_T *wp;
5046 int height;
5047{
5048 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005049 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005051 int space;
5052 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053#define FRACTION_MULT 16384L
5054
5055 /* Don't want a negative height. Happens when splitting a tiny window.
5056 * Will equalize heights soon to fix it. */
5057 if (height < 0)
5058 height = 0;
5059
5060 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5061 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5062 + FRACTION_MULT / 2) / (long)wp->w_height;
5063
5064 wp->w_height = height;
5065 wp->w_skipcol = 0;
5066
5067 /* Don't change w_topline when height is zero. Don't set w_topline when
5068 * 'scrollbind' is set and this isn't the current window. */
5069 if (height > 0
5070#ifdef FEAT_SCROLLBIND
5071 && (!wp->w_p_scb || wp == curwin)
5072#endif
5073 )
5074 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005075 /*
5076 * Find a value for w_topline that shows the cursor at the same
5077 * relative position in the window as before (more or less).
5078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 lnum = wp->w_cursor.lnum;
5080 if (lnum < 1) /* can happen when starting up */
5081 lnum = 1;
5082 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5083 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5084 sline = wp->w_wrow - line_size;
5085 if (sline < 0)
5086 {
5087 /*
5088 * Cursor line would go off top of screen if w_wrow was this high.
5089 */
5090 wp->w_wrow = line_size;
5091 }
5092 else
5093 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005094 space = height;
5095 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005097 space -= line_size;
5098 if (space > 0 && sline <= 0 && !did_below)
5099 {
5100 /* Try to use "~" lines below the text to avoid that text
5101 * is above the window while there are empty lines.
5102 * Subtract the rows below the cursor from "space" and
5103 * give the rest to "sline". */
5104 did_below = TRUE;
5105 bot = wp->w_cursor.lnum;
5106 while (space > 0)
5107 {
5108 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5109 space = 0;
5110 else
5111 {
5112#ifdef FEAT_FOLDING
5113 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5114#endif
5115 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5116 break;
5117 ++bot;
5118 space -= plines_win(wp, bot, TRUE);
5119 }
5120 }
5121 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5122 sline += space;
5123 }
5124 if (sline <= 0)
5125 break;
5126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127#ifdef FEAT_FOLDING
5128 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5129 if (lnum == 1)
5130 {
5131 /* first line in buffer is folded */
5132 line_size = 1;
5133 --sline;
5134 break;
5135 }
5136#endif
5137 --lnum;
5138#ifdef FEAT_DIFF
5139 if (lnum == wp->w_topline)
5140 line_size = plines_win_nofill(wp, lnum, TRUE)
5141 + wp->w_topfill;
5142 else
5143#endif
5144 line_size = plines_win(wp, lnum, TRUE);
5145 sline -= line_size;
5146 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (sline < 0)
5149 {
5150 /*
5151 * Line we want at top would go off top of screen. Use next
5152 * line instead.
5153 */
5154#ifdef FEAT_FOLDING
5155 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5156#endif
5157 lnum++;
5158 wp->w_wrow -= line_size + sline;
5159 }
5160 else if (sline > 0)
5161 {
5162 /* First line of file reached, use that as topline. */
5163 lnum = 1;
5164 wp->w_wrow -= sline;
5165 }
5166 }
5167 set_topline(wp, lnum);
5168 }
5169
5170 if (wp == curwin)
5171 {
5172 if (p_so)
5173 update_topline();
5174 curs_columns(FALSE); /* validate w_wrow */
5175 }
5176 wp->w_prev_fraction_row = wp->w_wrow;
5177
5178 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005179 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_WINDOWS
5181 wp->w_redr_status = TRUE;
5182#endif
5183 invalidate_botline_win(wp);
5184}
5185
5186#ifdef FEAT_VERTSPLIT
5187/*
5188 * Set the width of a window.
5189 */
5190 static void
5191win_new_width(wp, width)
5192 win_T *wp;
5193 int width;
5194{
5195 wp->w_width = width;
5196 wp->w_lines_valid = 0;
5197 changed_line_abv_curs_win(wp);
5198 invalidate_botline_win(wp);
5199 if (wp == curwin)
5200 {
5201 update_topline();
5202 curs_columns(TRUE); /* validate w_wrow */
5203 }
5204 redraw_win_later(wp, NOT_VALID);
5205 wp->w_redr_status = TRUE;
5206}
5207#endif
5208
5209 void
5210win_comp_scroll(wp)
5211 win_T *wp;
5212{
5213 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5214 if (wp->w_p_scr == 0)
5215 wp->w_p_scr = 1;
5216}
5217
5218/*
5219 * command_height: called whenever p_ch has been changed
5220 */
5221 void
5222command_height(old_p_ch)
5223 long old_p_ch;
5224{
5225#ifdef FEAT_WINDOWS
5226 int h;
5227 frame_T *frp;
5228
Bram Moolenaar05159a02005-02-26 23:04:13 +00005229 /* When passed a negative value use the value of p_ch that we remembered.
5230 * This is needed for when the GUI starts up, we can't be sure in what
5231 * order things happen. */
5232 if (old_p_ch < 0)
5233 old_p_ch = p_ch_used;
5234 p_ch_used = p_ch;
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 /* Find bottom frame with width of screen. */
5237 frp = lastwin->w_frame;
5238# ifdef FEAT_VERTSPLIT
5239 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5240 frp = frp->fr_parent;
5241# endif
5242
5243 /* Avoid changing the height of a window with 'winfixheight' set. */
5244 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5245 && frp->fr_win->w_p_wfh)
5246 frp = frp->fr_prev;
5247
5248 if (starting != NO_SCREEN)
5249 {
5250 cmdline_row = Rows - p_ch;
5251
5252 if (p_ch > old_p_ch) /* p_ch got bigger */
5253 {
5254 while (p_ch > old_p_ch)
5255 {
5256 if (frp == NULL)
5257 {
5258 EMSG(_(e_noroom));
5259 p_ch = old_p_ch;
5260 cmdline_row = Rows - p_ch;
5261 break;
5262 }
5263 h = frp->fr_height - frame_minheight(frp, NULL);
5264 if (h > p_ch - old_p_ch)
5265 h = p_ch - old_p_ch;
5266 old_p_ch += h;
5267 frame_add_height(frp, -h);
5268 frp = frp->fr_prev;
5269 }
5270
5271 /* Recompute window positions. */
5272 (void)win_comp_pos();
5273
5274 /* clear the lines added to cmdline */
5275 if (full_screen)
5276 screen_fill((int)(cmdline_row), (int)Rows, 0,
5277 (int)Columns, ' ', ' ', 0);
5278 msg_row = cmdline_row;
5279 redraw_cmdline = TRUE;
5280 return;
5281 }
5282
5283 if (msg_row < cmdline_row)
5284 msg_row = cmdline_row;
5285 redraw_cmdline = TRUE;
5286 }
5287 frame_add_height(frp, (int)(old_p_ch - p_ch));
5288
5289 /* Recompute window positions. */
5290 if (frp != lastwin->w_frame)
5291 (void)win_comp_pos();
5292#else
5293 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5294 cmdline_row = Rows - p_ch;
5295#endif
5296}
5297
5298#if defined(FEAT_WINDOWS) || defined(PROTO)
5299/*
5300 * Resize frame "frp" to be "n" lines higher (negative for less high).
5301 * Also resize the frames it is contained in.
5302 */
5303 static void
5304frame_add_height(frp, n)
5305 frame_T *frp;
5306 int n;
5307{
5308 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5309 for (;;)
5310 {
5311 frp = frp->fr_parent;
5312 if (frp == NULL)
5313 break;
5314 frp->fr_height += n;
5315 }
5316}
5317
5318/*
5319 * Add or remove a status line for the bottom window(s), according to the
5320 * value of 'laststatus'.
5321 */
5322 void
5323last_status(morewin)
5324 int morewin; /* pretend there are two or more windows */
5325{
5326 /* Don't make a difference between horizontal or vertical split. */
5327 last_status_rec(topframe, (p_ls == 2
5328 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5329}
5330
5331 static void
5332last_status_rec(fr, statusline)
5333 frame_T *fr;
5334 int statusline;
5335{
5336 frame_T *fp;
5337 win_T *wp;
5338
5339 if (fr->fr_layout == FR_LEAF)
5340 {
5341 wp = fr->fr_win;
5342 if (wp->w_status_height != 0 && !statusline)
5343 {
5344 /* remove status line */
5345 win_new_height(wp, wp->w_height + 1);
5346 wp->w_status_height = 0;
5347 comp_col();
5348 }
5349 else if (wp->w_status_height == 0 && statusline)
5350 {
5351 /* Find a frame to take a line from. */
5352 fp = fr;
5353 while (fp->fr_height <= frame_minheight(fp, NULL))
5354 {
5355 if (fp == topframe)
5356 {
5357 EMSG(_(e_noroom));
5358 return;
5359 }
5360 /* In a column of frames: go to frame above. If already at
5361 * the top or in a row of frames: go to parent. */
5362 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5363 fp = fp->fr_prev;
5364 else
5365 fp = fp->fr_parent;
5366 }
5367 wp->w_status_height = 1;
5368 if (fp != fr)
5369 {
5370 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5371 frame_fix_height(wp);
5372 (void)win_comp_pos();
5373 }
5374 else
5375 win_new_height(wp, wp->w_height - 1);
5376 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005377 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379 }
5380#ifdef FEAT_VERTSPLIT
5381 else if (fr->fr_layout == FR_ROW)
5382 {
5383 /* vertically split windows, set status line for each one */
5384 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5385 last_status_rec(fp, statusline);
5386 }
5387#endif
5388 else
5389 {
5390 /* horizontally split window, set status line for last one */
5391 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5392 ;
5393 last_status_rec(fp, statusline);
5394 }
5395}
5396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005397/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005398 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005399 */
5400 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005401tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005402{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005403#ifdef FEAT_GUI_TABLINE
5404 /* When the GUI has the tabline then this always returns zero. */
5405 if (gui_use_tabline())
5406 return 0;
5407#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005408 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005409 {
5410 case 0: return 0;
5411 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5412 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005413 return 1;
5414}
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#endif /* FEAT_WINDOWS */
5417
5418#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5419/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005420 * Get the file name at the cursor.
5421 * If Visual mode is active, use the selected text if it's in one line.
5422 * Returns the name in allocated memory, NULL for failure.
5423 */
5424 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005425grab_file_name(count, file_lnum)
5426 long count;
5427 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005428{
5429# ifdef FEAT_VISUAL
5430 if (VIsual_active)
5431 {
5432 int len;
5433 char_u *ptr;
5434
5435 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5436 return NULL;
5437 return find_file_name_in_path(ptr, len,
5438 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5439 }
5440# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005441 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5442 file_lnum);
5443
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005444}
5445
5446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 * Return the file name under or after the cursor.
5448 *
5449 * The 'path' option is searched if the file name is not absolute.
5450 * The string returned has been alloc'ed and should be freed by the caller.
5451 * NULL is returned if the file name or file is not found.
5452 *
5453 * options:
5454 * FNAME_MESS give error messages
5455 * FNAME_EXP expand to path
5456 * FNAME_HYP check for hypertext link
5457 * FNAME_INCL apply "includeexpr"
5458 */
5459 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005460file_name_at_cursor(options, count, file_lnum)
5461 int options;
5462 long count;
5463 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005466 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5467 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468}
5469
5470/*
5471 * Return the name of the file under or after ptr[col].
5472 * Otherwise like file_name_at_cursor().
5473 */
5474 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005475file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 char_u *line;
5477 int col;
5478 int options;
5479 long count;
5480 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005481 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 char_u *ptr;
5484 int len;
5485
5486 /*
5487 * search forward for what could be the start of a file name
5488 */
5489 ptr = line + col;
5490 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005491 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (*ptr == NUL) /* nothing found */
5493 {
5494 if (options & FNAME_MESS)
5495 EMSG(_("E446: No file name under cursor"));
5496 return NULL;
5497 }
5498
5499 /*
5500 * Search backward for first char of the file name.
5501 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5502 */
5503 while (ptr > line)
5504 {
5505#ifdef FEAT_MBYTE
5506 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5507 ptr -= len + 1;
5508 else
5509#endif
5510 if (vim_isfilec(ptr[-1])
5511 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5512 --ptr;
5513 else
5514 break;
5515 }
5516
5517 /*
5518 * Search forward for the last char of the file name.
5519 * Also allow "://" when ':' is not in 'isfname'.
5520 */
5521 len = 0;
5522 while (vim_isfilec(ptr[len])
5523 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5524#ifdef FEAT_MBYTE
5525 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005526 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 else
5528#endif
5529 ++len;
5530
5531 /*
5532 * If there is trailing punctuation, remove it.
5533 * But don't remove "..", could be a directory name.
5534 */
5535 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5536 && ptr[len - 2] != '.')
5537 --len;
5538
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005539 if (file_lnum != NULL)
5540 {
5541 char_u *p;
5542
5543 /* Get the number after the file name and a separator character */
5544 p = ptr + len;
5545 p = skipwhite(p);
5546 if (*p != NUL)
5547 {
5548 if (!isdigit(*p))
5549 ++p; /* skip the separator */
5550 p = skipwhite(p);
5551 if (isdigit(*p))
5552 *file_lnum = (int)getdigits(&p);
5553 }
5554 }
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5557}
5558
5559# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5560static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5561
5562 static char_u *
5563eval_includeexpr(ptr, len)
5564 char_u *ptr;
5565 int len;
5566{
5567 char_u *res;
5568
5569 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005570 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005571 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 set_vim_var_string(VV_FNAME, NULL, 0);
5573 return res;
5574}
5575#endif
5576
5577/*
5578 * Return the name of the file ptr[len] in 'path'.
5579 * Otherwise like file_name_at_cursor().
5580 */
5581 char_u *
5582find_file_name_in_path(ptr, len, options, count, rel_fname)
5583 char_u *ptr;
5584 int len;
5585 int options;
5586 long count;
5587 char_u *rel_fname; /* file we are searching relative to */
5588{
5589 char_u *file_name;
5590 int c;
5591# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5592 char_u *tofree = NULL;
5593
5594 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5595 {
5596 tofree = eval_includeexpr(ptr, len);
5597 if (tofree != NULL)
5598 {
5599 ptr = tofree;
5600 len = (int)STRLEN(ptr);
5601 }
5602 }
5603# endif
5604
5605 if (options & FNAME_EXP)
5606 {
5607 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5608 TRUE, rel_fname);
5609
5610# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5611 /*
5612 * If the file could not be found in a normal way, try applying
5613 * 'includeexpr' (unless done already).
5614 */
5615 if (file_name == NULL
5616 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5617 {
5618 tofree = eval_includeexpr(ptr, len);
5619 if (tofree != NULL)
5620 {
5621 ptr = tofree;
5622 len = (int)STRLEN(ptr);
5623 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5624 TRUE, rel_fname);
5625 }
5626 }
5627# endif
5628 if (file_name == NULL && (options & FNAME_MESS))
5629 {
5630 c = ptr[len];
5631 ptr[len] = NUL;
5632 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5633 ptr[len] = c;
5634 }
5635
5636 /* Repeat finding the file "count" times. This matters when it
5637 * appears several times in the path. */
5638 while (file_name != NULL && --count > 0)
5639 {
5640 vim_free(file_name);
5641 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5642 }
5643 }
5644 else
5645 file_name = vim_strnsave(ptr, len);
5646
5647# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5648 vim_free(tofree);
5649# endif
5650
5651 return file_name;
5652}
5653#endif /* FEAT_SEARCHPATH */
5654
5655/*
5656 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5657 * Also check for ":\\", which MS Internet Explorer accepts, return
5658 * URL_BACKSLASH.
5659 */
5660 static int
5661path_is_url(p)
5662 char_u *p;
5663{
5664 if (STRNCMP(p, "://", (size_t)3) == 0)
5665 return URL_SLASH;
5666 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5667 return URL_BACKSLASH;
5668 return 0;
5669}
5670
5671/*
5672 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5673 * Return URL_BACKSLASH for "name:\\".
5674 * Return zero otherwise.
5675 */
5676 int
5677path_with_url(fname)
5678 char_u *fname;
5679{
5680 char_u *p;
5681
5682 for (p = fname; isalpha(*p); ++p)
5683 ;
5684 return path_is_url(p);
5685}
5686
5687/*
5688 * Return TRUE if "name" is a full (absolute) path name or URL.
5689 */
5690 int
5691vim_isAbsName(name)
5692 char_u *name;
5693{
5694 return (path_with_url(name) != 0 || mch_isFullName(name));
5695}
5696
5697/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005698 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 *
5700 * return FAIL for failure, OK otherwise
5701 */
5702 int
5703vim_FullName(fname, buf, len, force)
5704 char_u *fname, *buf;
5705 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005706 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 int retval = OK;
5709 int url;
5710
5711 *buf = NUL;
5712 if (fname == NULL)
5713 return FAIL;
5714
5715 url = path_with_url(fname);
5716 if (!url)
5717 retval = mch_FullName(fname, buf, len, force);
5718 if (url || retval == FAIL)
5719 {
5720 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005721 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5724 slash_adjust(buf);
5725#endif
5726 return retval;
5727}
5728
5729/*
5730 * Return the minimal number of rows that is needed on the screen to display
5731 * the current number of windows.
5732 */
5733 int
5734min_rows()
5735{
5736 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005737#ifdef FEAT_WINDOWS
5738 tabpage_T *tp;
5739 int n;
5740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 if (firstwin == NULL) /* not initialized yet */
5743 return MIN_LINES;
5744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005746 total = 0;
5747 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5748 {
5749 n = frame_minheight(tp->tp_topframe, NULL);
5750 if (total < n)
5751 total = n;
5752 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005753 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005755 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005757 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 return total;
5759}
5760
5761/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005762 * Return TRUE if there is only one window (in the current tab page), not
5763 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 */
5765 int
5766only_one_window()
5767{
5768#ifdef FEAT_WINDOWS
5769 int count = 0;
5770 win_T *wp;
5771
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005772 /* If there is another tab page there always is another window. */
5773 if (first_tabpage->tp_next != NULL)
5774 return FALSE;
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005777 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778# ifdef FEAT_QUICKFIX
5779 || wp->w_p_pvw
5780# endif
5781 ) || wp == curwin)
5782 ++count;
5783 return (count <= 1);
5784#else
5785 return TRUE;
5786#endif
5787}
5788
5789#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5790/*
5791 * Correct the cursor line number in other windows. Used after changing the
5792 * current buffer, and before applying autocommands.
5793 * When "do_curwin" is TRUE, also check current window.
5794 */
5795 void
5796check_lnums(do_curwin)
5797 int do_curwin;
5798{
5799 win_T *wp;
5800
5801#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005802 tabpage_T *tp;
5803
5804 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5806#else
5807 wp = curwin;
5808 if (do_curwin)
5809#endif
5810 {
5811 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5812 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5813 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5814 wp->w_topline = curbuf->b_ml.ml_line_count;
5815 }
5816}
5817#endif
5818
5819#if defined(FEAT_WINDOWS) || defined(PROTO)
5820
5821/*
5822 * A snapshot of the window sizes, to restore them after closing the help
5823 * window.
5824 * Only these fields are used:
5825 * fr_layout
5826 * fr_width
5827 * fr_height
5828 * fr_next
5829 * fr_child
5830 * fr_win (only valid for the old curwin, NULL otherwise)
5831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
5833/*
5834 * Create a snapshot of the current frame sizes.
5835 */
5836 static void
5837make_snapshot()
5838{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005839 clear_snapshot(curtab);
5840 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842
5843 static void
5844make_snapshot_rec(fr, frp)
5845 frame_T *fr;
5846 frame_T **frp;
5847{
5848 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5849 if (*frp == NULL)
5850 return;
5851 (*frp)->fr_layout = fr->fr_layout;
5852# ifdef FEAT_VERTSPLIT
5853 (*frp)->fr_width = fr->fr_width;
5854# endif
5855 (*frp)->fr_height = fr->fr_height;
5856 if (fr->fr_next != NULL)
5857 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5858 if (fr->fr_child != NULL)
5859 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5860 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5861 (*frp)->fr_win = curwin;
5862}
5863
5864/*
5865 * Remove any existing snapshot.
5866 */
5867 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005868clear_snapshot(tp)
5869 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005871 clear_snapshot_rec(tp->tp_snapshot);
5872 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873}
5874
5875 static void
5876clear_snapshot_rec(fr)
5877 frame_T *fr;
5878{
5879 if (fr != NULL)
5880 {
5881 clear_snapshot_rec(fr->fr_next);
5882 clear_snapshot_rec(fr->fr_child);
5883 vim_free(fr);
5884 }
5885}
5886
5887/*
5888 * Restore a previously created snapshot, if there is any.
5889 * This is only done if the screen size didn't change and the window layout is
5890 * still the same.
5891 */
5892 static void
5893restore_snapshot(close_curwin)
5894 int close_curwin; /* closing current window */
5895{
5896 win_T *wp;
5897
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005898 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005900 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 && curtab->tp_snapshot->fr_height == topframe->fr_height
5903 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005905 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 win_comp_pos();
5907 if (wp != NULL && close_curwin)
5908 win_goto(wp);
5909 redraw_all_later(CLEAR);
5910 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005911 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912}
5913
5914/*
5915 * Check if frames "sn" and "fr" have the same layout, same following frames
5916 * and same children.
5917 */
5918 static int
5919check_snapshot_rec(sn, fr)
5920 frame_T *sn;
5921 frame_T *fr;
5922{
5923 if (sn->fr_layout != fr->fr_layout
5924 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5925 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5926 || (sn->fr_next != NULL
5927 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5928 || (sn->fr_child != NULL
5929 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5930 return FAIL;
5931 return OK;
5932}
5933
5934/*
5935 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5936 * following frames and children.
5937 * Returns a pointer to the old current window, or NULL.
5938 */
5939 static win_T *
5940restore_snapshot_rec(sn, fr)
5941 frame_T *sn;
5942 frame_T *fr;
5943{
5944 win_T *wp = NULL;
5945 win_T *wp2;
5946
5947 fr->fr_height = sn->fr_height;
5948# ifdef FEAT_VERTSPLIT
5949 fr->fr_width = sn->fr_width;
5950# endif
5951 if (fr->fr_layout == FR_LEAF)
5952 {
5953 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5954# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005955 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956# endif
5957 wp = sn->fr_win;
5958 }
5959 if (sn->fr_next != NULL)
5960 {
5961 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5962 if (wp2 != NULL)
5963 wp = wp2;
5964 }
5965 if (sn->fr_child != NULL)
5966 {
5967 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5968 if (wp2 != NULL)
5969 wp = wp2;
5970 }
5971 return wp;
5972}
5973
5974#endif
5975
5976#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5977/*
5978 * Return TRUE if there is any vertically split window.
5979 */
5980 int
5981win_hasvertsplit()
5982{
5983 frame_T *fr;
5984
5985 if (topframe->fr_layout == FR_ROW)
5986 return TRUE;
5987
5988 if (topframe->fr_layout == FR_COL)
5989 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5990 if (fr->fr_layout == FR_ROW)
5991 return TRUE;
5992
5993 return FALSE;
5994}
5995#endif