blob: 6a12de19f531220f9ab9014fa735398436e0a8f8 [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 Moolenaar607a95ed2006-03-28 20:57:42 +00003195
3196#if defined(FEAT_GUI)
3197 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3198 * scrollbars. Have to update them anyway. */
3199 if (gui.in_use && starting == 0)
3200 {
3201 gui_init_which_components(NULL);
3202 gui_update_scrollbars(TRUE);
3203 }
3204 need_mouse_correct = TRUE;
3205#endif
3206
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003208#ifdef FEAT_AUTOCMD
3209 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3210 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3211#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003212 return OK;
3213 }
3214
3215 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003216 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003217 return FAIL;
3218}
3219
3220/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003221 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3222 * like with ":split".
3223 * Returns OK if a new tab page was created, FAIL otherwise.
3224 */
3225 int
3226may_open_tabpage()
3227{
3228 int n = cmdmod.tab;
3229
3230 if (cmdmod.tab != 0)
3231 {
3232 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3233 return win_new_tabpage(n);
3234 }
3235 return FAIL;
3236}
3237
3238/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003239 * Create up to "maxcount" tabpages with empty windows.
3240 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003242 int
3243make_tabpages(maxcount)
3244 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003245{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003246 int count = maxcount;
3247 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003248
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003249 /* Limit to 'tabpagemax' tabs. */
3250 if (count > p_tpm)
3251 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003252
3253#ifdef FEAT_AUTOCMD
3254 /*
3255 * Don't execute autocommands while creating the tab pages. Must do that
3256 * when putting the buffers in the windows.
3257 */
3258 ++autocmd_block;
3259#endif
3260
3261 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003262 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003264
3265#ifdef FEAT_AUTOCMD
3266 --autocmd_block;
3267#endif
3268
3269 /* return actual number of tab pages */
3270 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003271}
3272
3273/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003274 * Return TRUE when "tpc" points to a valid tab page.
3275 */
3276 int
3277valid_tabpage(tpc)
3278 tabpage_T *tpc;
3279{
3280 tabpage_T *tp;
3281
3282 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3283 if (tp == tpc)
3284 return TRUE;
3285 return FALSE;
3286}
3287
3288/*
3289 * Find tab page "n" (first one is 1). Returns NULL when not found.
3290 */
3291 tabpage_T *
3292find_tabpage(n)
3293 int n;
3294{
3295 tabpage_T *tp;
3296 int i = 1;
3297
3298 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3299 ++i;
3300 return tp;
3301}
3302
3303/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003304 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003305 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003306 */
3307 int
3308tabpage_index(ftp)
3309 tabpage_T *ftp;
3310{
3311 int i = 1;
3312 tabpage_T *tp;
3313
3314 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3315 ++i;
3316 return i;
3317}
3318
3319/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003320 * Prepare for leaving the current tab page.
3321 * When autocomands change "curtab" we don't leave the tab page and return
3322 * FAIL.
3323 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003325/*ARGSUSED*/
3326 static int
3327leave_tabpage(new_curbuf)
3328 buf_T *new_curbuf; /* what is going to be the new curbuf,
3329 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003330{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003331 tabpage_T *tp = curtab;
3332
3333#ifdef FEAT_AUTOCMD
3334 if (new_curbuf != curbuf)
3335 {
3336 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3337 if (curtab != tp)
3338 return FAIL;
3339 }
3340 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3341 if (curtab != tp)
3342 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003343 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003344 if (curtab != tp)
3345 return FAIL;
3346#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003347#if defined(FEAT_GUI)
3348 /* Remove the scrollbars. They may be added back later. */
3349 if (gui.in_use)
3350 gui_remove_scrollbars();
3351#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003353 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003354 tp->tp_firstwin = firstwin;
3355 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003356 tp->tp_old_Rows = Rows;
3357 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358 firstwin = NULL;
3359 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003360 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003361}
3362
3363/*
3364 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003365 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003366 */
3367/*ARGSUSED*/
3368 static void
3369enter_tabpage(tp, old_curbuf)
3370 tabpage_T *tp;
3371 buf_T *old_curbuf;
3372{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003373 int old_off = tp->tp_firstwin->w_winrow;
3374
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003375 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003376 firstwin = tp->tp_firstwin;
3377 lastwin = tp->tp_lastwin;
3378 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003379#ifdef FEAT_AUTOCMD
3380 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3381#endif
3382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003384 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385
3386#ifdef FEAT_AUTOCMD
3387 if (old_curbuf != curbuf)
3388 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3389#endif
3390
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003391 last_status(FALSE); /* status line may appear or disappear */
3392 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393 must_redraw = CLEAR; /* need to redraw everything */
3394#ifdef FEAT_DIFF
3395 diff_need_scrollbind = TRUE;
3396#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003398 /* The tabpage line may have appeared or disappeared, may need to resize
3399 * the frames for that. When the Vim window was resized need to update
3400 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003401 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3402#ifdef FEAT_GUI_TABLINE
3403 && !gui_use_tabline()
3404#endif
3405 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003406 shell_new_rows();
3407#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003408 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003409 shell_new_columns(); /* update window widths */
3410#endif
3411
3412#if defined(FEAT_GUI)
3413 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3414 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003415 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003416 {
3417 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003418 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003419 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003420 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421#endif
3422
3423 redraw_all_later(CLEAR);
3424}
3425
3426/*
3427 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003428 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429 */
3430 void
3431goto_tabpage(n)
3432 int n;
3433{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003435 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003436 int i;
3437
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003438 /* If there is only one it can't work. */
3439 if (first_tabpage->tp_next == NULL)
3440 {
3441 if (n > 1)
3442 beep_flush();
3443 return;
3444 }
3445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003446 if (n == 0)
3447 {
3448 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003449 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003450 tp = first_tabpage;
3451 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003452 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003453 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003454 else if (n < 0)
3455 {
3456 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3457 * this N times. */
3458 ttp = curtab;
3459 for (i = n; i < 0; ++i)
3460 {
3461 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3462 tp = tp->tp_next)
3463 ;
3464 ttp = tp;
3465 }
3466 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003467 else if (n == 9999)
3468 {
3469 /* Go to last tab page. */
3470 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3471 ;
3472 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473 else
3474 {
3475 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003476 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003477 if (tp == NULL)
3478 {
3479 beep_flush();
3480 return;
3481 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003482 }
3483
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003484 goto_tabpage_tp(tp);
3485
3486#ifdef FEAT_GUI_TABLINE
3487 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003488 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489#endif
3490}
3491
3492/*
3493 * Go to tabpage "tp".
3494 * Note: doesn't update the GUI tab.
3495 */
3496 void
3497goto_tabpage_tp(tp)
3498 tabpage_T *tp;
3499{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003500 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003501 {
3502 if (valid_tabpage(tp))
3503 enter_tabpage(tp, curbuf);
3504 else
3505 enter_tabpage(curtab, curbuf);
3506 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003510 * Move the current tab page to before tab page "nr".
3511 */
3512 void
3513tabpage_move(nr)
3514 int nr;
3515{
3516 int n = nr;
3517 tabpage_T *tp;
3518
3519 if (first_tabpage->tp_next == NULL)
3520 return;
3521
3522 /* Remove the current tab page from the list of tab pages. */
3523 if (curtab == first_tabpage)
3524 first_tabpage = curtab->tp_next;
3525 else
3526 {
3527 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3528 if (tp->tp_next == curtab)
3529 break;
3530 if (tp == NULL) /* "cannot happen" */
3531 return;
3532 tp->tp_next = curtab->tp_next;
3533 }
3534
3535 /* Re-insert it at the specified position. */
3536 if (n == 0)
3537 {
3538 curtab->tp_next = first_tabpage;
3539 first_tabpage = curtab;
3540 }
3541 else
3542 {
3543 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3544 --n;
3545 curtab->tp_next = tp->tp_next;
3546 tp->tp_next = curtab;
3547 }
3548
3549 /* Need to redraw the tabline. Tab page contents doesn't change. */
3550 redraw_tabline = TRUE;
3551}
3552
3553
3554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Go to another window.
3556 * When jumping to another buffer, stop Visual mode. Do this before
3557 * changing windows so we can yank the selection into the '*' register.
3558 * When jumping to another window on the same buffer, adjust its cursor
3559 * position to keep the same Visual area.
3560 */
3561 void
3562win_goto(wp)
3563 win_T *wp;
3564{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003565 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
3567 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003568 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return;
3570 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#ifdef FEAT_VISUAL
3573 if (wp->w_buffer != curbuf)
3574 reset_VIsual_and_resel();
3575 else if (VIsual_active)
3576 wp->w_cursor = curwin->w_cursor;
3577#endif
3578
3579#ifdef FEAT_GUI
3580 need_mouse_correct = TRUE;
3581#endif
3582 win_enter(wp, TRUE);
3583}
3584
3585#if defined(FEAT_PERL) || defined(PROTO)
3586/*
3587 * Find window number "winnr" (counting top to bottom).
3588 */
3589 win_T *
3590win_find_nr(winnr)
3591 int winnr;
3592{
3593 win_T *wp;
3594
3595# ifdef FEAT_WINDOWS
3596 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3597 if (--winnr == 0)
3598 break;
3599 return wp;
3600# else
3601 return curwin;
3602# endif
3603}
3604#endif
3605
3606#ifdef FEAT_VERTSPLIT
3607/*
3608 * Move to window above or below "count" times.
3609 */
3610 static void
3611win_goto_ver(up, count)
3612 int up; /* TRUE to go to win above */
3613 long count;
3614{
3615 frame_T *fr;
3616 frame_T *nfr;
3617 frame_T *foundfr;
3618
3619 foundfr = curwin->w_frame;
3620 while (count--)
3621 {
3622 /*
3623 * First go upwards in the tree of frames until we find a upwards or
3624 * downwards neighbor.
3625 */
3626 fr = foundfr;
3627 for (;;)
3628 {
3629 if (fr == topframe)
3630 goto end;
3631 if (up)
3632 nfr = fr->fr_prev;
3633 else
3634 nfr = fr->fr_next;
3635 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3636 break;
3637 fr = fr->fr_parent;
3638 }
3639
3640 /*
3641 * Now go downwards to find the bottom or top frame in it.
3642 */
3643 for (;;)
3644 {
3645 if (nfr->fr_layout == FR_LEAF)
3646 {
3647 foundfr = nfr;
3648 break;
3649 }
3650 fr = nfr->fr_child;
3651 if (nfr->fr_layout == FR_ROW)
3652 {
3653 /* Find the frame at the cursor row. */
3654 while (fr->fr_next != NULL
3655 && frame2win(fr)->w_wincol + fr->fr_width
3656 <= curwin->w_wincol + curwin->w_wcol)
3657 fr = fr->fr_next;
3658 }
3659 if (nfr->fr_layout == FR_COL && up)
3660 while (fr->fr_next != NULL)
3661 fr = fr->fr_next;
3662 nfr = fr;
3663 }
3664 }
3665end:
3666 if (foundfr != NULL)
3667 win_goto(foundfr->fr_win);
3668}
3669
3670/*
3671 * Move to left or right window.
3672 */
3673 static void
3674win_goto_hor(left, count)
3675 int left; /* TRUE to go to left win */
3676 long count;
3677{
3678 frame_T *fr;
3679 frame_T *nfr;
3680 frame_T *foundfr;
3681
3682 foundfr = curwin->w_frame;
3683 while (count--)
3684 {
3685 /*
3686 * First go upwards in the tree of frames until we find a left or
3687 * right neighbor.
3688 */
3689 fr = foundfr;
3690 for (;;)
3691 {
3692 if (fr == topframe)
3693 goto end;
3694 if (left)
3695 nfr = fr->fr_prev;
3696 else
3697 nfr = fr->fr_next;
3698 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3699 break;
3700 fr = fr->fr_parent;
3701 }
3702
3703 /*
3704 * Now go downwards to find the leftmost or rightmost frame in it.
3705 */
3706 for (;;)
3707 {
3708 if (nfr->fr_layout == FR_LEAF)
3709 {
3710 foundfr = nfr;
3711 break;
3712 }
3713 fr = nfr->fr_child;
3714 if (nfr->fr_layout == FR_COL)
3715 {
3716 /* Find the frame at the cursor row. */
3717 while (fr->fr_next != NULL
3718 && frame2win(fr)->w_winrow + fr->fr_height
3719 <= curwin->w_winrow + curwin->w_wrow)
3720 fr = fr->fr_next;
3721 }
3722 if (nfr->fr_layout == FR_ROW && left)
3723 while (fr->fr_next != NULL)
3724 fr = fr->fr_next;
3725 nfr = fr;
3726 }
3727 }
3728end:
3729 if (foundfr != NULL)
3730 win_goto(foundfr->fr_win);
3731}
3732#endif
3733
3734/*
3735 * Make window "wp" the current window.
3736 */
3737 void
3738win_enter(wp, undo_sync)
3739 win_T *wp;
3740 int undo_sync;
3741{
3742 win_enter_ext(wp, undo_sync, FALSE);
3743}
3744
3745/*
3746 * Make window wp the current window.
3747 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3748 * been closed and isn't valid.
3749 */
3750 static void
3751win_enter_ext(wp, undo_sync, curwin_invalid)
3752 win_T *wp;
3753 int undo_sync;
3754 int curwin_invalid;
3755{
3756#ifdef FEAT_AUTOCMD
3757 int other_buffer = FALSE;
3758#endif
3759
3760 if (wp == curwin && !curwin_invalid) /* nothing to do */
3761 return;
3762
3763#ifdef FEAT_AUTOCMD
3764 if (!curwin_invalid)
3765 {
3766 /*
3767 * Be careful: If autocommands delete the window, return now.
3768 */
3769 if (wp->w_buffer != curbuf)
3770 {
3771 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3772 other_buffer = TRUE;
3773 if (!win_valid(wp))
3774 return;
3775 }
3776 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3777 if (!win_valid(wp))
3778 return;
3779# ifdef FEAT_EVAL
3780 /* autocmds may abort script processing */
3781 if (aborting())
3782 return;
3783# endif
3784 }
3785#endif
3786
3787 /* sync undo before leaving the current buffer */
3788 if (undo_sync && curbuf != wp->w_buffer)
3789 u_sync();
3790 /* may have to copy the buffer options when 'cpo' contains 'S' */
3791 if (wp->w_buffer != curbuf)
3792 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3793 if (!curwin_invalid)
3794 {
3795 prevwin = curwin; /* remember for CTRL-W p */
3796 curwin->w_redr_status = TRUE;
3797 }
3798 curwin = wp;
3799 curbuf = wp->w_buffer;
3800 check_cursor();
3801#ifdef FEAT_VIRTUALEDIT
3802 if (!virtual_active())
3803 curwin->w_cursor.coladd = 0;
3804#endif
3805 changed_line_abv_curs(); /* assume cursor position needs updating */
3806
3807 if (curwin->w_localdir != NULL)
3808 {
3809 /* Window has a local directory: Save current directory as global
3810 * directory (unless that was done already) and change to the local
3811 * directory. */
3812 if (globaldir == NULL)
3813 {
3814 char_u cwd[MAXPATHL];
3815
3816 if (mch_dirname(cwd, MAXPATHL) == OK)
3817 globaldir = vim_strsave(cwd);
3818 }
3819 mch_chdir((char *)curwin->w_localdir);
3820 shorten_fnames(TRUE);
3821 }
3822 else if (globaldir != NULL)
3823 {
3824 /* Window doesn't have a local directory and we are not in the global
3825 * directory: Change to the global directory. */
3826 mch_chdir((char *)globaldir);
3827 vim_free(globaldir);
3828 globaldir = NULL;
3829 shorten_fnames(TRUE);
3830 }
3831
3832#ifdef FEAT_AUTOCMD
3833 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3834 if (other_buffer)
3835 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3836#endif
3837
3838#ifdef FEAT_TITLE
3839 maketitle();
3840#endif
3841 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003842 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (restart_edit)
3844 redraw_later(VALID); /* causes status line redraw */
3845
3846 /* set window height to desired minimal value */
3847 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3848 win_setheight((int)p_wh);
3849 else if (curwin->w_height == 0)
3850 win_setheight(1);
3851
3852#ifdef FEAT_VERTSPLIT
3853 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003854 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 win_setwidth((int)p_wiw);
3856#endif
3857
3858#ifdef FEAT_MOUSE
3859 setmouse(); /* in case jumped to/from help buffer */
3860#endif
3861
3862#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3863 /* Change directories when the acd option is set on and after
3864 * switching windows. */
3865 if (p_acd && curbuf->b_ffname != NULL
3866 && vim_chdirfile(curbuf->b_ffname) == OK)
3867 shorten_fnames(TRUE);
3868#endif
3869}
3870
3871#endif /* FEAT_WINDOWS */
3872
3873#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3874/*
3875 * Jump to the first open window that contains buffer buf if one exists
3876 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3877 * Returns pointer to window if it exists, otherwise NULL.
3878 */
3879 win_T *
3880buf_jump_open_win(buf)
3881 buf_T *buf;
3882{
3883# ifdef FEAT_WINDOWS
3884 win_T *wp;
3885
3886 for (wp = firstwin; wp; wp = wp->w_next)
3887 if (wp->w_buffer == buf)
3888 break;
3889 if (wp != NULL)
3890 win_enter(wp, FALSE);
3891 return wp;
3892# else
3893 if (curwin->w_buffer == buf)
3894 return curwin;
3895 return NULL;
3896# endif
3897}
3898#endif
3899
3900/*
3901 * allocate a window structure and link it in the window list
3902 */
3903/*ARGSUSED*/
3904 static win_T *
3905win_alloc(after)
3906 win_T *after;
3907{
3908 win_T *newwin;
3909
3910 /*
3911 * allocate window structure and linesizes arrays
3912 */
3913 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3914 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3915 {
3916 vim_free(newwin);
3917 newwin = NULL;
3918 }
3919
3920 if (newwin != NULL)
3921 {
3922 /*
3923 * link the window in the window list
3924 */
3925#ifdef FEAT_WINDOWS
3926 win_append(after, newwin);
3927#endif
3928#ifdef FEAT_VERTSPLIT
3929 newwin->w_wincol = 0;
3930 newwin->w_width = Columns;
3931#endif
3932
3933 /* position the display and the cursor at the top of the file. */
3934 newwin->w_topline = 1;
3935#ifdef FEAT_DIFF
3936 newwin->w_topfill = 0;
3937#endif
3938 newwin->w_botline = 2;
3939 newwin->w_cursor.lnum = 1;
3940#ifdef FEAT_SCROLLBIND
3941 newwin->w_scbind_pos = 1;
3942#endif
3943
3944 /* We won't calculate w_fraction until resizing the window */
3945 newwin->w_fraction = 0;
3946 newwin->w_prev_fraction_row = -1;
3947
3948#ifdef FEAT_GUI
3949 if (gui.in_use)
3950 {
3951 out_flush();
3952 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3953 SBAR_LEFT, newwin);
3954 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3955 SBAR_RIGHT, newwin);
3956 }
3957#endif
3958#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003959 /* init w: variables */
3960 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961#endif
3962#ifdef FEAT_FOLDING
3963 foldInitWin(newwin);
3964#endif
3965 }
3966 return newwin;
3967}
3968
3969#if defined(FEAT_WINDOWS) || defined(PROTO)
3970
3971/*
3972 * remove window 'wp' from the window list and free the structure
3973 */
3974 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003975win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003977 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int i;
3980
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003981#ifdef FEAT_MZSCHEME
3982 mzscheme_window_free(wp);
3983#endif
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#ifdef FEAT_PERL
3986 perl_win_free(wp);
3987#endif
3988
3989#ifdef FEAT_PYTHON
3990 python_window_free(wp);
3991#endif
3992
3993#ifdef FEAT_TCL
3994 tcl_window_free(wp);
3995#endif
3996
3997#ifdef FEAT_RUBY
3998 ruby_window_free(wp);
3999#endif
4000
4001 clear_winopt(&wp->w_onebuf_opt);
4002 clear_winopt(&wp->w_allbuf_opt);
4003
4004#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004005 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006#endif
4007
4008 if (prevwin == wp)
4009 prevwin = NULL;
4010 win_free_lsize(wp);
4011
4012 for (i = 0; i < wp->w_tagstacklen; ++i)
4013 vim_free(wp->w_tagstack[i].tagname);
4014
4015 vim_free(wp->w_localdir);
4016#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004017 vim_free(wp->w_match[0].regprog);
4018 vim_free(wp->w_match[1].regprog);
4019 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#endif
4021#ifdef FEAT_JUMPLIST
4022 free_jumplist(wp);
4023#endif
4024
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004025#ifdef FEAT_QUICKFIX
4026 qf_free_all(wp);
4027#endif
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#ifdef FEAT_GUI
4030 if (gui.in_use)
4031 {
4032 out_flush();
4033 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4034 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4035 }
4036#endif /* FEAT_GUI */
4037
Bram Moolenaarf740b292006-02-16 22:11:02 +00004038 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 vim_free(wp);
4040}
4041
4042/*
4043 * Append window "wp" in the window list after window "after".
4044 */
4045 static void
4046win_append(after, wp)
4047 win_T *after, *wp;
4048{
4049 win_T *before;
4050
4051 if (after == NULL) /* after NULL is in front of the first */
4052 before = firstwin;
4053 else
4054 before = after->w_next;
4055
4056 wp->w_next = before;
4057 wp->w_prev = after;
4058 if (after == NULL)
4059 firstwin = wp;
4060 else
4061 after->w_next = wp;
4062 if (before == NULL)
4063 lastwin = wp;
4064 else
4065 before->w_prev = wp;
4066}
4067
4068/*
4069 * Remove a window from the window list.
4070 */
4071 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004072win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004074 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075{
4076 if (wp->w_prev != NULL)
4077 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004078 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004080 else
4081 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (wp->w_next != NULL)
4083 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004084 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004086 else
4087 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088}
4089
4090/*
4091 * Append frame "frp" in a frame list after frame "after".
4092 */
4093 static void
4094frame_append(after, frp)
4095 frame_T *after, *frp;
4096{
4097 frp->fr_next = after->fr_next;
4098 after->fr_next = frp;
4099 if (frp->fr_next != NULL)
4100 frp->fr_next->fr_prev = frp;
4101 frp->fr_prev = after;
4102}
4103
4104/*
4105 * Insert frame "frp" in a frame list before frame "before".
4106 */
4107 static void
4108frame_insert(before, frp)
4109 frame_T *before, *frp;
4110{
4111 frp->fr_next = before;
4112 frp->fr_prev = before->fr_prev;
4113 before->fr_prev = frp;
4114 if (frp->fr_prev != NULL)
4115 frp->fr_prev->fr_next = frp;
4116 else
4117 frp->fr_parent->fr_child = frp;
4118}
4119
4120/*
4121 * Remove a frame from a frame list.
4122 */
4123 static void
4124frame_remove(frp)
4125 frame_T *frp;
4126{
4127 if (frp->fr_prev != NULL)
4128 frp->fr_prev->fr_next = frp->fr_next;
4129 else
4130 frp->fr_parent->fr_child = frp->fr_next;
4131 if (frp->fr_next != NULL)
4132 frp->fr_next->fr_prev = frp->fr_prev;
4133}
4134
4135#endif /* FEAT_WINDOWS */
4136
4137/*
4138 * Allocate w_lines[] for window "wp".
4139 * Return FAIL for failure, OK for success.
4140 */
4141 int
4142win_alloc_lines(wp)
4143 win_T *wp;
4144{
4145 wp->w_lines_valid = 0;
4146 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4147 if (wp->w_lines == NULL)
4148 return FAIL;
4149 return OK;
4150}
4151
4152/*
4153 * free lsize arrays for a window
4154 */
4155 void
4156win_free_lsize(wp)
4157 win_T *wp;
4158{
4159 vim_free(wp->w_lines);
4160 wp->w_lines = NULL;
4161}
4162
4163/*
4164 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004165 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
4167 void
4168shell_new_rows()
4169{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004170 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 if (firstwin == NULL) /* not initialized yet */
4173 return;
4174#ifdef FEAT_WINDOWS
4175 if (h < frame_minheight(topframe, NULL))
4176 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004177
4178 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * that doesn't result in the right height, forget about that option. */
4180 frame_new_height(topframe, h, FALSE, TRUE);
4181 if (topframe->fr_height != h)
4182 frame_new_height(topframe, h, FALSE, FALSE);
4183
4184 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4185#else
4186 if (h < 1)
4187 h = 1;
4188 win_new_height(firstwin, h);
4189#endif
4190 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004191#ifdef FEAT_WINDOWS
4192 p_ch_used = p_ch;
4193#endif
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195#if 0
4196 /* Disabled: don't want making the screen smaller make a window larger. */
4197 if (p_ea)
4198 win_equal(curwin, FALSE, 'v');
4199#endif
4200}
4201
4202#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4203/*
4204 * Called from win_new_shellsize() after Columns changed.
4205 */
4206 void
4207shell_new_columns()
4208{
4209 if (firstwin == NULL) /* not initialized yet */
4210 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004211
4212 /* First try setting the widths of windows with 'winfixwidth'. If that
4213 * doesn't result in the right width, forget about that option. */
4214 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4215 if (topframe->fr_width != Columns)
4216 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4219#if 0
4220 /* Disabled: don't want making the screen smaller make a window larger. */
4221 if (p_ea)
4222 win_equal(curwin, FALSE, 'h');
4223#endif
4224}
4225#endif
4226
4227#if defined(FEAT_CMDWIN) || defined(PROTO)
4228/*
4229 * Save the size of all windows in "gap".
4230 */
4231 void
4232win_size_save(gap)
4233 garray_T *gap;
4234
4235{
4236 win_T *wp;
4237
4238 ga_init2(gap, (int)sizeof(int), 1);
4239 if (ga_grow(gap, win_count() * 2) == OK)
4240 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4241 {
4242 ((int *)gap->ga_data)[gap->ga_len++] =
4243 wp->w_width + wp->w_vsep_width;
4244 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4245 }
4246}
4247
4248/*
4249 * Restore window sizes, but only if the number of windows is still the same.
4250 * Does not free the growarray.
4251 */
4252 void
4253win_size_restore(gap)
4254 garray_T *gap;
4255{
4256 win_T *wp;
4257 int i;
4258
4259 if (win_count() * 2 == gap->ga_len)
4260 {
4261 i = 0;
4262 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4263 {
4264 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4265 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4266 }
4267 /* recompute the window positions */
4268 (void)win_comp_pos();
4269 }
4270}
4271#endif /* FEAT_CMDWIN */
4272
4273#if defined(FEAT_WINDOWS) || defined(PROTO)
4274/*
4275 * Update the position for all windows, using the width and height of the
4276 * frames.
4277 * Returns the row just after the last window.
4278 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004279 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280win_comp_pos()
4281{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004282 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 int col = 0;
4284
4285 frame_comp_pos(topframe, &row, &col);
4286 return row;
4287}
4288
4289/*
4290 * Update the position of the windows in frame "topfrp", using the width and
4291 * height of the frames.
4292 * "*row" and "*col" are the top-left position of the frame. They are updated
4293 * to the bottom-right position plus one.
4294 */
4295 static void
4296frame_comp_pos(topfrp, row, col)
4297 frame_T *topfrp;
4298 int *row;
4299 int *col;
4300{
4301 win_T *wp;
4302 frame_T *frp;
4303#ifdef FEAT_VERTSPLIT
4304 int startcol;
4305 int startrow;
4306#endif
4307
4308 wp = topfrp->fr_win;
4309 if (wp != NULL)
4310 {
4311 if (wp->w_winrow != *row
4312#ifdef FEAT_VERTSPLIT
4313 || wp->w_wincol != *col
4314#endif
4315 )
4316 {
4317 /* position changed, redraw */
4318 wp->w_winrow = *row;
4319#ifdef FEAT_VERTSPLIT
4320 wp->w_wincol = *col;
4321#endif
4322 redraw_win_later(wp, NOT_VALID);
4323 wp->w_redr_status = TRUE;
4324 }
4325 *row += wp->w_height + wp->w_status_height;
4326#ifdef FEAT_VERTSPLIT
4327 *col += wp->w_width + wp->w_vsep_width;
4328#endif
4329 }
4330 else
4331 {
4332#ifdef FEAT_VERTSPLIT
4333 startrow = *row;
4334 startcol = *col;
4335#endif
4336 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4337 {
4338#ifdef FEAT_VERTSPLIT
4339 if (topfrp->fr_layout == FR_ROW)
4340 *row = startrow; /* all frames are at the same row */
4341 else
4342 *col = startcol; /* all frames are at the same col */
4343#endif
4344 frame_comp_pos(frp, row, col);
4345 }
4346 }
4347}
4348
4349#endif /* FEAT_WINDOWS */
4350
4351/*
4352 * Set current window height and take care of repositioning other windows to
4353 * fit around it.
4354 */
4355 void
4356win_setheight(height)
4357 int height;
4358{
4359 win_setheight_win(height, curwin);
4360}
4361
4362/*
4363 * Set the window height of window "win" and take care of repositioning other
4364 * windows to fit around it.
4365 */
4366 void
4367win_setheight_win(height, win)
4368 int height;
4369 win_T *win;
4370{
4371 int row;
4372
4373 if (win == curwin)
4374 {
4375 /* Always keep current window at least one line high, even when
4376 * 'winminheight' is zero. */
4377#ifdef FEAT_WINDOWS
4378 if (height < p_wmh)
4379 height = p_wmh;
4380#endif
4381 if (height == 0)
4382 height = 1;
4383 }
4384
4385#ifdef FEAT_WINDOWS
4386 frame_setheight(win->w_frame, height + win->w_status_height);
4387
4388 /* recompute the window positions */
4389 row = win_comp_pos();
4390#else
4391 if (height > topframe->fr_height)
4392 height = topframe->fr_height;
4393 win->w_height = height;
4394 row = height;
4395#endif
4396
4397 /*
4398 * If there is extra space created between the last window and the command
4399 * line, clear it.
4400 */
4401 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4402 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4403 cmdline_row = row;
4404 msg_row = row;
4405 msg_col = 0;
4406
4407 redraw_all_later(NOT_VALID);
4408}
4409
4410#if defined(FEAT_WINDOWS) || defined(PROTO)
4411
4412/*
4413 * Set the height of a frame to "height" and take care that all frames and
4414 * windows inside it are resized. Also resize frames on the left and right if
4415 * the are in the same FR_ROW frame.
4416 *
4417 * Strategy:
4418 * If the frame is part of a FR_COL frame, try fitting the frame in that
4419 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4420 * go to containing frames to resize them and make room.
4421 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4422 * Check for the minimal height of the FR_ROW frame.
4423 * At the top level we can also use change the command line height.
4424 */
4425 static void
4426frame_setheight(curfrp, height)
4427 frame_T *curfrp;
4428 int height;
4429{
4430 int room; /* total number of lines available */
4431 int take; /* number of lines taken from other windows */
4432 int room_cmdline; /* lines available from cmdline */
4433 int run;
4434 frame_T *frp;
4435 int h;
4436 int room_reserved;
4437
4438 /* If the height already is the desired value, nothing to do. */
4439 if (curfrp->fr_height == height)
4440 return;
4441
4442 if (curfrp->fr_parent == NULL)
4443 {
4444 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004445 if (height > ROWS_AVAIL)
4446 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (height > 0)
4448 frame_new_height(curfrp, height, FALSE, FALSE);
4449 }
4450 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4451 {
4452 /* Row of frames: Also need to resize frames left and right of this
4453 * one. First check for the minimal height of these. */
4454 h = frame_minheight(curfrp->fr_parent, NULL);
4455 if (height < h)
4456 height = h;
4457 frame_setheight(curfrp->fr_parent, height);
4458 }
4459 else
4460 {
4461 /*
4462 * Column of frames: try to change only frames in this column.
4463 */
4464#ifdef FEAT_VERTSPLIT
4465 /*
4466 * Do this twice:
4467 * 1: compute room available, if it's not enough try resizing the
4468 * containing frame.
4469 * 2: compute the room available and adjust the height to it.
4470 * Try not to reduce the height of a window with 'winfixheight' set.
4471 */
4472 for (run = 1; run <= 2; ++run)
4473#else
4474 for (;;)
4475#endif
4476 {
4477 room = 0;
4478 room_reserved = 0;
4479 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4480 frp = frp->fr_next)
4481 {
4482 if (frp != curfrp
4483 && frp->fr_win != NULL
4484 && frp->fr_win->w_p_wfh)
4485 room_reserved += frp->fr_height;
4486 room += frp->fr_height;
4487 if (frp != curfrp)
4488 room -= frame_minheight(frp, NULL);
4489 }
4490#ifdef FEAT_VERTSPLIT
4491 if (curfrp->fr_width != Columns)
4492 room_cmdline = 0;
4493 else
4494#endif
4495 {
4496 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4497 + lastwin->w_height + lastwin->w_status_height);
4498 if (room_cmdline < 0)
4499 room_cmdline = 0;
4500 }
4501
4502 if (height <= room + room_cmdline)
4503 break;
4504#ifdef FEAT_VERTSPLIT
4505 if (run == 2 || curfrp->fr_width == Columns)
4506#endif
4507 {
4508 if (height > room + room_cmdline)
4509 height = room + room_cmdline;
4510 break;
4511 }
4512#ifdef FEAT_VERTSPLIT
4513 frame_setheight(curfrp->fr_parent, height
4514 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4515#endif
4516 /*NOTREACHED*/
4517 }
4518
4519 /*
4520 * Compute the number of lines we will take from others frames (can be
4521 * negative!).
4522 */
4523 take = height - curfrp->fr_height;
4524
4525 /* If there is not enough room, also reduce the height of a window
4526 * with 'winfixheight' set. */
4527 if (height > room + room_cmdline - room_reserved)
4528 room_reserved = room + room_cmdline - height;
4529 /* If there is only a 'winfixheight' window and making the
4530 * window smaller, need to make the other window taller. */
4531 if (take < 0 && room - curfrp->fr_height < room_reserved)
4532 room_reserved = 0;
4533
4534 if (take > 0 && room_cmdline > 0)
4535 {
4536 /* use lines from cmdline first */
4537 if (take < room_cmdline)
4538 room_cmdline = take;
4539 take -= room_cmdline;
4540 topframe->fr_height += room_cmdline;
4541 }
4542
4543 /*
4544 * set the current frame to the new height
4545 */
4546 frame_new_height(curfrp, height, FALSE, FALSE);
4547
4548 /*
4549 * First take lines from the frames after the current frame. If
4550 * that is not enough, takes lines from frames above the current
4551 * frame.
4552 */
4553 for (run = 0; run < 2; ++run)
4554 {
4555 if (run == 0)
4556 frp = curfrp->fr_next; /* 1st run: start with next window */
4557 else
4558 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4559 while (frp != NULL && take != 0)
4560 {
4561 h = frame_minheight(frp, NULL);
4562 if (room_reserved > 0
4563 && frp->fr_win != NULL
4564 && frp->fr_win->w_p_wfh)
4565 {
4566 if (room_reserved >= frp->fr_height)
4567 room_reserved -= frp->fr_height;
4568 else
4569 {
4570 if (frp->fr_height - room_reserved > take)
4571 room_reserved = frp->fr_height - take;
4572 take -= frp->fr_height - room_reserved;
4573 frame_new_height(frp, room_reserved, FALSE, FALSE);
4574 room_reserved = 0;
4575 }
4576 }
4577 else
4578 {
4579 if (frp->fr_height - take < h)
4580 {
4581 take -= frp->fr_height - h;
4582 frame_new_height(frp, h, FALSE, FALSE);
4583 }
4584 else
4585 {
4586 frame_new_height(frp, frp->fr_height - take,
4587 FALSE, FALSE);
4588 take = 0;
4589 }
4590 }
4591 if (run == 0)
4592 frp = frp->fr_next;
4593 else
4594 frp = frp->fr_prev;
4595 }
4596 }
4597 }
4598}
4599
4600#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4601/*
4602 * Set current window width and take care of repositioning other windows to
4603 * fit around it.
4604 */
4605 void
4606win_setwidth(width)
4607 int width;
4608{
4609 win_setwidth_win(width, curwin);
4610}
4611
4612 void
4613win_setwidth_win(width, wp)
4614 int width;
4615 win_T *wp;
4616{
4617 /* Always keep current window at least one column wide, even when
4618 * 'winminwidth' is zero. */
4619 if (wp == curwin)
4620 {
4621 if (width < p_wmw)
4622 width = p_wmw;
4623 if (width == 0)
4624 width = 1;
4625 }
4626
4627 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4628
4629 /* recompute the window positions */
4630 (void)win_comp_pos();
4631
4632 redraw_all_later(NOT_VALID);
4633}
4634
4635/*
4636 * Set the width of a frame to "width" and take care that all frames and
4637 * windows inside it are resized. Also resize frames above and below if the
4638 * are in the same FR_ROW frame.
4639 *
4640 * Strategy is similar to frame_setheight().
4641 */
4642 static void
4643frame_setwidth(curfrp, width)
4644 frame_T *curfrp;
4645 int width;
4646{
4647 int room; /* total number of lines available */
4648 int take; /* number of lines taken from other windows */
4649 int run;
4650 frame_T *frp;
4651 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004652 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 /* If the width already is the desired value, nothing to do. */
4655 if (curfrp->fr_width == width)
4656 return;
4657
4658 if (curfrp->fr_parent == NULL)
4659 /* topframe: can't change width */
4660 return;
4661
4662 if (curfrp->fr_parent->fr_layout == FR_COL)
4663 {
4664 /* Column of frames: Also need to resize frames above and below of
4665 * this one. First check for the minimal width of these. */
4666 w = frame_minwidth(curfrp->fr_parent, NULL);
4667 if (width < w)
4668 width = w;
4669 frame_setwidth(curfrp->fr_parent, width);
4670 }
4671 else
4672 {
4673 /*
4674 * Row of frames: try to change only frames in this row.
4675 *
4676 * Do this twice:
4677 * 1: compute room available, if it's not enough try resizing the
4678 * containing frame.
4679 * 2: compute the room available and adjust the width to it.
4680 */
4681 for (run = 1; run <= 2; ++run)
4682 {
4683 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004684 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4686 frp = frp->fr_next)
4687 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004688 if (frp != curfrp
4689 && frp->fr_win != NULL
4690 && frp->fr_win->w_p_wfw)
4691 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 room += frp->fr_width;
4693 if (frp != curfrp)
4694 room -= frame_minwidth(frp, NULL);
4695 }
4696
4697 if (width <= room)
4698 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004699 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
4701 if (width > room)
4702 width = room;
4703 break;
4704 }
4705 frame_setwidth(curfrp->fr_parent, width
4706 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /*
4710 * Compute the number of lines we will take from others frames (can be
4711 * negative!).
4712 */
4713 take = width - curfrp->fr_width;
4714
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004715 /* If there is not enough room, also reduce the width of a window
4716 * with 'winfixwidth' set. */
4717 if (width > room - room_reserved)
4718 room_reserved = room - width;
4719 /* If there is only a 'winfixwidth' window and making the
4720 * window smaller, need to make the other window narrower. */
4721 if (take < 0 && room - curfrp->fr_width < room_reserved)
4722 room_reserved = 0;
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 /*
4725 * set the current frame to the new width
4726 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004727 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
4730 * First take lines from the frames right of the current frame. If
4731 * that is not enough, takes lines from frames left of the current
4732 * frame.
4733 */
4734 for (run = 0; run < 2; ++run)
4735 {
4736 if (run == 0)
4737 frp = curfrp->fr_next; /* 1st run: start with next window */
4738 else
4739 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4740 while (frp != NULL && take != 0)
4741 {
4742 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004743 if (room_reserved > 0
4744 && frp->fr_win != NULL
4745 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004747 if (room_reserved >= frp->fr_width)
4748 room_reserved -= frp->fr_width;
4749 else
4750 {
4751 if (frp->fr_width - room_reserved > take)
4752 room_reserved = frp->fr_width - take;
4753 take -= frp->fr_width - room_reserved;
4754 frame_new_width(frp, room_reserved, FALSE, FALSE);
4755 room_reserved = 0;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 }
4758 else
4759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004760 if (frp->fr_width - take < w)
4761 {
4762 take -= frp->fr_width - w;
4763 frame_new_width(frp, w, FALSE, FALSE);
4764 }
4765 else
4766 {
4767 frame_new_width(frp, frp->fr_width - take,
4768 FALSE, FALSE);
4769 take = 0;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 if (run == 0)
4773 frp = frp->fr_next;
4774 else
4775 frp = frp->fr_prev;
4776 }
4777 }
4778 }
4779}
4780#endif /* FEAT_VERTSPLIT */
4781
4782/*
4783 * Check 'winminheight' for a valid value.
4784 */
4785 void
4786win_setminheight()
4787{
4788 int room;
4789 int first = TRUE;
4790 win_T *wp;
4791
4792 /* loop until there is a 'winminheight' that is possible */
4793 while (p_wmh > 0)
4794 {
4795 /* TODO: handle vertical splits */
4796 room = -p_wh;
4797 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4798 room += wp->w_height - p_wmh;
4799 if (room >= 0)
4800 break;
4801 --p_wmh;
4802 if (first)
4803 {
4804 EMSG(_(e_noroom));
4805 first = FALSE;
4806 }
4807 }
4808}
4809
4810#ifdef FEAT_MOUSE
4811
4812/*
4813 * Status line of dragwin is dragged "offset" lines down (negative is up).
4814 */
4815 void
4816win_drag_status_line(dragwin, offset)
4817 win_T *dragwin;
4818 int offset;
4819{
4820 frame_T *curfr;
4821 frame_T *fr;
4822 int room;
4823 int row;
4824 int up; /* if TRUE, drag status line up, otherwise down */
4825 int n;
4826
4827 fr = dragwin->w_frame;
4828 curfr = fr;
4829 if (fr != topframe) /* more than one window */
4830 {
4831 fr = fr->fr_parent;
4832 /* When the parent frame is not a column of frames, its parent should
4833 * be. */
4834 if (fr->fr_layout != FR_COL)
4835 {
4836 curfr = fr;
4837 if (fr != topframe) /* only a row of windows, may drag statusline */
4838 fr = fr->fr_parent;
4839 }
4840 }
4841
4842 /* If this is the last frame in a column, may want to resize the parent
4843 * frame instead (go two up to skip a row of frames). */
4844 while (curfr != topframe && curfr->fr_next == NULL)
4845 {
4846 if (fr != topframe)
4847 fr = fr->fr_parent;
4848 curfr = fr;
4849 if (fr != topframe)
4850 fr = fr->fr_parent;
4851 }
4852
4853 if (offset < 0) /* drag up */
4854 {
4855 up = TRUE;
4856 offset = -offset;
4857 /* sum up the room of the current frame and above it */
4858 if (fr == curfr)
4859 {
4860 /* only one window */
4861 room = fr->fr_height - frame_minheight(fr, NULL);
4862 }
4863 else
4864 {
4865 room = 0;
4866 for (fr = fr->fr_child; ; fr = fr->fr_next)
4867 {
4868 room += fr->fr_height - frame_minheight(fr, NULL);
4869 if (fr == curfr)
4870 break;
4871 }
4872 }
4873 fr = curfr->fr_next; /* put fr at frame that grows */
4874 }
4875 else /* drag down */
4876 {
4877 up = FALSE;
4878 /*
4879 * Only dragging the last status line can reduce p_ch.
4880 */
4881 room = Rows - cmdline_row;
4882 if (curfr->fr_next == NULL)
4883 room -= 1;
4884 else
4885 room -= p_ch;
4886 if (room < 0)
4887 room = 0;
4888 /* sum up the room of frames below of the current one */
4889 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4890 room += fr->fr_height - frame_minheight(fr, NULL);
4891 fr = curfr; /* put fr at window that grows */
4892 }
4893
4894 if (room < offset) /* Not enough room */
4895 offset = room; /* Move as far as we can */
4896 if (offset <= 0)
4897 return;
4898
4899 /*
4900 * Grow frame fr by "offset" lines.
4901 * Doesn't happen when dragging the last status line up.
4902 */
4903 if (fr != NULL)
4904 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4905
4906 if (up)
4907 fr = curfr; /* current frame gets smaller */
4908 else
4909 fr = curfr->fr_next; /* next frame gets smaller */
4910
4911 /*
4912 * Now make the other frames smaller.
4913 */
4914 while (fr != NULL && offset > 0)
4915 {
4916 n = frame_minheight(fr, NULL);
4917 if (fr->fr_height - offset <= n)
4918 {
4919 offset -= fr->fr_height - n;
4920 frame_new_height(fr, n, !up, FALSE);
4921 }
4922 else
4923 {
4924 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4925 break;
4926 }
4927 if (up)
4928 fr = fr->fr_prev;
4929 else
4930 fr = fr->fr_next;
4931 }
4932 row = win_comp_pos();
4933 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4934 cmdline_row = row;
4935 p_ch = Rows - cmdline_row;
4936 if (p_ch < 1)
4937 p_ch = 1;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004938 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 showmode();
4940}
4941
4942#ifdef FEAT_VERTSPLIT
4943/*
4944 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4945 */
4946 void
4947win_drag_vsep_line(dragwin, offset)
4948 win_T *dragwin;
4949 int offset;
4950{
4951 frame_T *curfr;
4952 frame_T *fr;
4953 int room;
4954 int left; /* if TRUE, drag separator line left, otherwise right */
4955 int n;
4956
4957 fr = dragwin->w_frame;
4958 if (fr == topframe) /* only one window (cannot happe?) */
4959 return;
4960 curfr = fr;
4961 fr = fr->fr_parent;
4962 /* When the parent frame is not a row of frames, its parent should be. */
4963 if (fr->fr_layout != FR_ROW)
4964 {
4965 if (fr == topframe) /* only a column of windows (cannot happen?) */
4966 return;
4967 curfr = fr;
4968 fr = fr->fr_parent;
4969 }
4970
4971 /* If this is the last frame in a row, may want to resize a parent
4972 * frame instead. */
4973 while (curfr->fr_next == NULL)
4974 {
4975 if (fr == topframe)
4976 break;
4977 curfr = fr;
4978 fr = fr->fr_parent;
4979 if (fr != topframe)
4980 {
4981 curfr = fr;
4982 fr = fr->fr_parent;
4983 }
4984 }
4985
4986 if (offset < 0) /* drag left */
4987 {
4988 left = TRUE;
4989 offset = -offset;
4990 /* sum up the room of the current frame and left of it */
4991 room = 0;
4992 for (fr = fr->fr_child; ; fr = fr->fr_next)
4993 {
4994 room += fr->fr_width - frame_minwidth(fr, NULL);
4995 if (fr == curfr)
4996 break;
4997 }
4998 fr = curfr->fr_next; /* put fr at frame that grows */
4999 }
5000 else /* drag right */
5001 {
5002 left = FALSE;
5003 /* sum up the room of frames right of the current one */
5004 room = 0;
5005 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5006 room += fr->fr_width - frame_minwidth(fr, NULL);
5007 fr = curfr; /* put fr at window that grows */
5008 }
5009
5010 if (room < offset) /* Not enough room */
5011 offset = room; /* Move as far as we can */
5012 if (offset <= 0) /* No room at all, quit. */
5013 return;
5014
5015 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005016 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 /* shrink other frames: current and at the left or at the right */
5019 if (left)
5020 fr = curfr; /* current frame gets smaller */
5021 else
5022 fr = curfr->fr_next; /* next frame gets smaller */
5023
5024 while (fr != NULL && offset > 0)
5025 {
5026 n = frame_minwidth(fr, NULL);
5027 if (fr->fr_width - offset <= n)
5028 {
5029 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005030 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 else
5033 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005034 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
5037 if (left)
5038 fr = fr->fr_prev;
5039 else
5040 fr = fr->fr_next;
5041 }
5042 (void)win_comp_pos();
5043 redraw_all_later(NOT_VALID);
5044}
5045#endif /* FEAT_VERTSPLIT */
5046#endif /* FEAT_MOUSE */
5047
5048#endif /* FEAT_WINDOWS */
5049
5050/*
5051 * Set the height of a window.
5052 * This takes care of the things inside the window, not what happens to the
5053 * window position, the frame or to other windows.
5054 */
5055 static void
5056win_new_height(wp, height)
5057 win_T *wp;
5058 int height;
5059{
5060 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005061 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005063 int space;
5064 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065#define FRACTION_MULT 16384L
5066
5067 /* Don't want a negative height. Happens when splitting a tiny window.
5068 * Will equalize heights soon to fix it. */
5069 if (height < 0)
5070 height = 0;
5071
5072 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5073 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5074 + FRACTION_MULT / 2) / (long)wp->w_height;
5075
5076 wp->w_height = height;
5077 wp->w_skipcol = 0;
5078
5079 /* Don't change w_topline when height is zero. Don't set w_topline when
5080 * 'scrollbind' is set and this isn't the current window. */
5081 if (height > 0
5082#ifdef FEAT_SCROLLBIND
5083 && (!wp->w_p_scb || wp == curwin)
5084#endif
5085 )
5086 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005087 /*
5088 * Find a value for w_topline that shows the cursor at the same
5089 * relative position in the window as before (more or less).
5090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 lnum = wp->w_cursor.lnum;
5092 if (lnum < 1) /* can happen when starting up */
5093 lnum = 1;
5094 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5095 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5096 sline = wp->w_wrow - line_size;
5097 if (sline < 0)
5098 {
5099 /*
5100 * Cursor line would go off top of screen if w_wrow was this high.
5101 */
5102 wp->w_wrow = line_size;
5103 }
5104 else
5105 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005106 space = height;
5107 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005109 space -= line_size;
5110 if (space > 0 && sline <= 0 && !did_below)
5111 {
5112 /* Try to use "~" lines below the text to avoid that text
5113 * is above the window while there are empty lines.
5114 * Subtract the rows below the cursor from "space" and
5115 * give the rest to "sline". */
5116 did_below = TRUE;
5117 bot = wp->w_cursor.lnum;
5118 while (space > 0)
5119 {
5120 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5121 space = 0;
5122 else
5123 {
5124#ifdef FEAT_FOLDING
5125 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5126#endif
5127 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5128 break;
5129 ++bot;
5130 space -= plines_win(wp, bot, TRUE);
5131 }
5132 }
5133 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5134 sline += space;
5135 }
5136 if (sline <= 0)
5137 break;
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#ifdef FEAT_FOLDING
5140 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5141 if (lnum == 1)
5142 {
5143 /* first line in buffer is folded */
5144 line_size = 1;
5145 --sline;
5146 break;
5147 }
5148#endif
5149 --lnum;
5150#ifdef FEAT_DIFF
5151 if (lnum == wp->w_topline)
5152 line_size = plines_win_nofill(wp, lnum, TRUE)
5153 + wp->w_topfill;
5154 else
5155#endif
5156 line_size = plines_win(wp, lnum, TRUE);
5157 sline -= line_size;
5158 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (sline < 0)
5161 {
5162 /*
5163 * Line we want at top would go off top of screen. Use next
5164 * line instead.
5165 */
5166#ifdef FEAT_FOLDING
5167 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5168#endif
5169 lnum++;
5170 wp->w_wrow -= line_size + sline;
5171 }
5172 else if (sline > 0)
5173 {
5174 /* First line of file reached, use that as topline. */
5175 lnum = 1;
5176 wp->w_wrow -= sline;
5177 }
5178 }
5179 set_topline(wp, lnum);
5180 }
5181
5182 if (wp == curwin)
5183 {
5184 if (p_so)
5185 update_topline();
5186 curs_columns(FALSE); /* validate w_wrow */
5187 }
5188 wp->w_prev_fraction_row = wp->w_wrow;
5189
5190 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005191 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#ifdef FEAT_WINDOWS
5193 wp->w_redr_status = TRUE;
5194#endif
5195 invalidate_botline_win(wp);
5196}
5197
5198#ifdef FEAT_VERTSPLIT
5199/*
5200 * Set the width of a window.
5201 */
5202 static void
5203win_new_width(wp, width)
5204 win_T *wp;
5205 int width;
5206{
5207 wp->w_width = width;
5208 wp->w_lines_valid = 0;
5209 changed_line_abv_curs_win(wp);
5210 invalidate_botline_win(wp);
5211 if (wp == curwin)
5212 {
5213 update_topline();
5214 curs_columns(TRUE); /* validate w_wrow */
5215 }
5216 redraw_win_later(wp, NOT_VALID);
5217 wp->w_redr_status = TRUE;
5218}
5219#endif
5220
5221 void
5222win_comp_scroll(wp)
5223 win_T *wp;
5224{
5225 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5226 if (wp->w_p_scr == 0)
5227 wp->w_p_scr = 1;
5228}
5229
5230/*
5231 * command_height: called whenever p_ch has been changed
5232 */
5233 void
5234command_height(old_p_ch)
5235 long old_p_ch;
5236{
5237#ifdef FEAT_WINDOWS
5238 int h;
5239 frame_T *frp;
5240
Bram Moolenaar05159a02005-02-26 23:04:13 +00005241 /* When passed a negative value use the value of p_ch that we remembered.
5242 * This is needed for when the GUI starts up, we can't be sure in what
5243 * order things happen. */
5244 if (old_p_ch < 0)
5245 old_p_ch = p_ch_used;
5246 p_ch_used = p_ch;
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 /* Find bottom frame with width of screen. */
5249 frp = lastwin->w_frame;
5250# ifdef FEAT_VERTSPLIT
5251 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5252 frp = frp->fr_parent;
5253# endif
5254
5255 /* Avoid changing the height of a window with 'winfixheight' set. */
5256 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5257 && frp->fr_win->w_p_wfh)
5258 frp = frp->fr_prev;
5259
5260 if (starting != NO_SCREEN)
5261 {
5262 cmdline_row = Rows - p_ch;
5263
5264 if (p_ch > old_p_ch) /* p_ch got bigger */
5265 {
5266 while (p_ch > old_p_ch)
5267 {
5268 if (frp == NULL)
5269 {
5270 EMSG(_(e_noroom));
5271 p_ch = old_p_ch;
5272 cmdline_row = Rows - p_ch;
5273 break;
5274 }
5275 h = frp->fr_height - frame_minheight(frp, NULL);
5276 if (h > p_ch - old_p_ch)
5277 h = p_ch - old_p_ch;
5278 old_p_ch += h;
5279 frame_add_height(frp, -h);
5280 frp = frp->fr_prev;
5281 }
5282
5283 /* Recompute window positions. */
5284 (void)win_comp_pos();
5285
5286 /* clear the lines added to cmdline */
5287 if (full_screen)
5288 screen_fill((int)(cmdline_row), (int)Rows, 0,
5289 (int)Columns, ' ', ' ', 0);
5290 msg_row = cmdline_row;
5291 redraw_cmdline = TRUE;
5292 return;
5293 }
5294
5295 if (msg_row < cmdline_row)
5296 msg_row = cmdline_row;
5297 redraw_cmdline = TRUE;
5298 }
5299 frame_add_height(frp, (int)(old_p_ch - p_ch));
5300
5301 /* Recompute window positions. */
5302 if (frp != lastwin->w_frame)
5303 (void)win_comp_pos();
5304#else
5305 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5306 cmdline_row = Rows - p_ch;
5307#endif
5308}
5309
5310#if defined(FEAT_WINDOWS) || defined(PROTO)
5311/*
5312 * Resize frame "frp" to be "n" lines higher (negative for less high).
5313 * Also resize the frames it is contained in.
5314 */
5315 static void
5316frame_add_height(frp, n)
5317 frame_T *frp;
5318 int n;
5319{
5320 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5321 for (;;)
5322 {
5323 frp = frp->fr_parent;
5324 if (frp == NULL)
5325 break;
5326 frp->fr_height += n;
5327 }
5328}
5329
5330/*
5331 * Add or remove a status line for the bottom window(s), according to the
5332 * value of 'laststatus'.
5333 */
5334 void
5335last_status(morewin)
5336 int morewin; /* pretend there are two or more windows */
5337{
5338 /* Don't make a difference between horizontal or vertical split. */
5339 last_status_rec(topframe, (p_ls == 2
5340 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5341}
5342
5343 static void
5344last_status_rec(fr, statusline)
5345 frame_T *fr;
5346 int statusline;
5347{
5348 frame_T *fp;
5349 win_T *wp;
5350
5351 if (fr->fr_layout == FR_LEAF)
5352 {
5353 wp = fr->fr_win;
5354 if (wp->w_status_height != 0 && !statusline)
5355 {
5356 /* remove status line */
5357 win_new_height(wp, wp->w_height + 1);
5358 wp->w_status_height = 0;
5359 comp_col();
5360 }
5361 else if (wp->w_status_height == 0 && statusline)
5362 {
5363 /* Find a frame to take a line from. */
5364 fp = fr;
5365 while (fp->fr_height <= frame_minheight(fp, NULL))
5366 {
5367 if (fp == topframe)
5368 {
5369 EMSG(_(e_noroom));
5370 return;
5371 }
5372 /* In a column of frames: go to frame above. If already at
5373 * the top or in a row of frames: go to parent. */
5374 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5375 fp = fp->fr_prev;
5376 else
5377 fp = fp->fr_parent;
5378 }
5379 wp->w_status_height = 1;
5380 if (fp != fr)
5381 {
5382 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5383 frame_fix_height(wp);
5384 (void)win_comp_pos();
5385 }
5386 else
5387 win_new_height(wp, wp->w_height - 1);
5388 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005389 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 }
5392#ifdef FEAT_VERTSPLIT
5393 else if (fr->fr_layout == FR_ROW)
5394 {
5395 /* vertically split windows, set status line for each one */
5396 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5397 last_status_rec(fp, statusline);
5398 }
5399#endif
5400 else
5401 {
5402 /* horizontally split window, set status line for last one */
5403 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5404 ;
5405 last_status_rec(fp, statusline);
5406 }
5407}
5408
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005409/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005410 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005411 */
5412 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005413tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005414{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005415#ifdef FEAT_GUI_TABLINE
5416 /* When the GUI has the tabline then this always returns zero. */
5417 if (gui_use_tabline())
5418 return 0;
5419#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005420 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005421 {
5422 case 0: return 0;
5423 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5424 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005425 return 1;
5426}
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#endif /* FEAT_WINDOWS */
5429
5430#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5431/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005432 * Get the file name at the cursor.
5433 * If Visual mode is active, use the selected text if it's in one line.
5434 * Returns the name in allocated memory, NULL for failure.
5435 */
5436 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005437grab_file_name(count, file_lnum)
5438 long count;
5439 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005440{
5441# ifdef FEAT_VISUAL
5442 if (VIsual_active)
5443 {
5444 int len;
5445 char_u *ptr;
5446
5447 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5448 return NULL;
5449 return find_file_name_in_path(ptr, len,
5450 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5451 }
5452# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005453 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5454 file_lnum);
5455
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005456}
5457
5458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 * Return the file name under or after the cursor.
5460 *
5461 * The 'path' option is searched if the file name is not absolute.
5462 * The string returned has been alloc'ed and should be freed by the caller.
5463 * NULL is returned if the file name or file is not found.
5464 *
5465 * options:
5466 * FNAME_MESS give error messages
5467 * FNAME_EXP expand to path
5468 * FNAME_HYP check for hypertext link
5469 * FNAME_INCL apply "includeexpr"
5470 */
5471 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005472file_name_at_cursor(options, count, file_lnum)
5473 int options;
5474 long count;
5475 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005478 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5479 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
5481
5482/*
5483 * Return the name of the file under or after ptr[col].
5484 * Otherwise like file_name_at_cursor().
5485 */
5486 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005487file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 char_u *line;
5489 int col;
5490 int options;
5491 long count;
5492 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005493 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 char_u *ptr;
5496 int len;
5497
5498 /*
5499 * search forward for what could be the start of a file name
5500 */
5501 ptr = line + col;
5502 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005503 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (*ptr == NUL) /* nothing found */
5505 {
5506 if (options & FNAME_MESS)
5507 EMSG(_("E446: No file name under cursor"));
5508 return NULL;
5509 }
5510
5511 /*
5512 * Search backward for first char of the file name.
5513 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5514 */
5515 while (ptr > line)
5516 {
5517#ifdef FEAT_MBYTE
5518 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5519 ptr -= len + 1;
5520 else
5521#endif
5522 if (vim_isfilec(ptr[-1])
5523 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5524 --ptr;
5525 else
5526 break;
5527 }
5528
5529 /*
5530 * Search forward for the last char of the file name.
5531 * Also allow "://" when ':' is not in 'isfname'.
5532 */
5533 len = 0;
5534 while (vim_isfilec(ptr[len])
5535 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5536#ifdef FEAT_MBYTE
5537 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005538 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else
5540#endif
5541 ++len;
5542
5543 /*
5544 * If there is trailing punctuation, remove it.
5545 * But don't remove "..", could be a directory name.
5546 */
5547 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5548 && ptr[len - 2] != '.')
5549 --len;
5550
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005551 if (file_lnum != NULL)
5552 {
5553 char_u *p;
5554
5555 /* Get the number after the file name and a separator character */
5556 p = ptr + len;
5557 p = skipwhite(p);
5558 if (*p != NUL)
5559 {
5560 if (!isdigit(*p))
5561 ++p; /* skip the separator */
5562 p = skipwhite(p);
5563 if (isdigit(*p))
5564 *file_lnum = (int)getdigits(&p);
5565 }
5566 }
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5569}
5570
5571# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5572static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5573
5574 static char_u *
5575eval_includeexpr(ptr, len)
5576 char_u *ptr;
5577 int len;
5578{
5579 char_u *res;
5580
5581 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005582 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005583 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 set_vim_var_string(VV_FNAME, NULL, 0);
5585 return res;
5586}
5587#endif
5588
5589/*
5590 * Return the name of the file ptr[len] in 'path'.
5591 * Otherwise like file_name_at_cursor().
5592 */
5593 char_u *
5594find_file_name_in_path(ptr, len, options, count, rel_fname)
5595 char_u *ptr;
5596 int len;
5597 int options;
5598 long count;
5599 char_u *rel_fname; /* file we are searching relative to */
5600{
5601 char_u *file_name;
5602 int c;
5603# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5604 char_u *tofree = NULL;
5605
5606 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5607 {
5608 tofree = eval_includeexpr(ptr, len);
5609 if (tofree != NULL)
5610 {
5611 ptr = tofree;
5612 len = (int)STRLEN(ptr);
5613 }
5614 }
5615# endif
5616
5617 if (options & FNAME_EXP)
5618 {
5619 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5620 TRUE, rel_fname);
5621
5622# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5623 /*
5624 * If the file could not be found in a normal way, try applying
5625 * 'includeexpr' (unless done already).
5626 */
5627 if (file_name == NULL
5628 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5629 {
5630 tofree = eval_includeexpr(ptr, len);
5631 if (tofree != NULL)
5632 {
5633 ptr = tofree;
5634 len = (int)STRLEN(ptr);
5635 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5636 TRUE, rel_fname);
5637 }
5638 }
5639# endif
5640 if (file_name == NULL && (options & FNAME_MESS))
5641 {
5642 c = ptr[len];
5643 ptr[len] = NUL;
5644 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5645 ptr[len] = c;
5646 }
5647
5648 /* Repeat finding the file "count" times. This matters when it
5649 * appears several times in the path. */
5650 while (file_name != NULL && --count > 0)
5651 {
5652 vim_free(file_name);
5653 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5654 }
5655 }
5656 else
5657 file_name = vim_strnsave(ptr, len);
5658
5659# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5660 vim_free(tofree);
5661# endif
5662
5663 return file_name;
5664}
5665#endif /* FEAT_SEARCHPATH */
5666
5667/*
5668 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5669 * Also check for ":\\", which MS Internet Explorer accepts, return
5670 * URL_BACKSLASH.
5671 */
5672 static int
5673path_is_url(p)
5674 char_u *p;
5675{
5676 if (STRNCMP(p, "://", (size_t)3) == 0)
5677 return URL_SLASH;
5678 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5679 return URL_BACKSLASH;
5680 return 0;
5681}
5682
5683/*
5684 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5685 * Return URL_BACKSLASH for "name:\\".
5686 * Return zero otherwise.
5687 */
5688 int
5689path_with_url(fname)
5690 char_u *fname;
5691{
5692 char_u *p;
5693
5694 for (p = fname; isalpha(*p); ++p)
5695 ;
5696 return path_is_url(p);
5697}
5698
5699/*
5700 * Return TRUE if "name" is a full (absolute) path name or URL.
5701 */
5702 int
5703vim_isAbsName(name)
5704 char_u *name;
5705{
5706 return (path_with_url(name) != 0 || mch_isFullName(name));
5707}
5708
5709/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005710 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 *
5712 * return FAIL for failure, OK otherwise
5713 */
5714 int
5715vim_FullName(fname, buf, len, force)
5716 char_u *fname, *buf;
5717 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005718 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719{
5720 int retval = OK;
5721 int url;
5722
5723 *buf = NUL;
5724 if (fname == NULL)
5725 return FAIL;
5726
5727 url = path_with_url(fname);
5728 if (!url)
5729 retval = mch_FullName(fname, buf, len, force);
5730 if (url || retval == FAIL)
5731 {
5732 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005733 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5736 slash_adjust(buf);
5737#endif
5738 return retval;
5739}
5740
5741/*
5742 * Return the minimal number of rows that is needed on the screen to display
5743 * the current number of windows.
5744 */
5745 int
5746min_rows()
5747{
5748 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005749#ifdef FEAT_WINDOWS
5750 tabpage_T *tp;
5751 int n;
5752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 if (firstwin == NULL) /* not initialized yet */
5755 return MIN_LINES;
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005758 total = 0;
5759 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5760 {
5761 n = frame_minheight(tp->tp_topframe, NULL);
5762 if (total < n)
5763 total = n;
5764 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005765 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005767 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005769 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 return total;
5771}
5772
5773/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005774 * Return TRUE if there is only one window (in the current tab page), not
5775 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 */
5777 int
5778only_one_window()
5779{
5780#ifdef FEAT_WINDOWS
5781 int count = 0;
5782 win_T *wp;
5783
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005784 /* If there is another tab page there always is another window. */
5785 if (first_tabpage->tp_next != NULL)
5786 return FALSE;
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005789 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790# ifdef FEAT_QUICKFIX
5791 || wp->w_p_pvw
5792# endif
5793 ) || wp == curwin)
5794 ++count;
5795 return (count <= 1);
5796#else
5797 return TRUE;
5798#endif
5799}
5800
5801#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5802/*
5803 * Correct the cursor line number in other windows. Used after changing the
5804 * current buffer, and before applying autocommands.
5805 * When "do_curwin" is TRUE, also check current window.
5806 */
5807 void
5808check_lnums(do_curwin)
5809 int do_curwin;
5810{
5811 win_T *wp;
5812
5813#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005814 tabpage_T *tp;
5815
5816 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5818#else
5819 wp = curwin;
5820 if (do_curwin)
5821#endif
5822 {
5823 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5824 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5825 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5826 wp->w_topline = curbuf->b_ml.ml_line_count;
5827 }
5828}
5829#endif
5830
5831#if defined(FEAT_WINDOWS) || defined(PROTO)
5832
5833/*
5834 * A snapshot of the window sizes, to restore them after closing the help
5835 * window.
5836 * Only these fields are used:
5837 * fr_layout
5838 * fr_width
5839 * fr_height
5840 * fr_next
5841 * fr_child
5842 * fr_win (only valid for the old curwin, NULL otherwise)
5843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
5845/*
5846 * Create a snapshot of the current frame sizes.
5847 */
5848 static void
5849make_snapshot()
5850{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005851 clear_snapshot(curtab);
5852 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853}
5854
5855 static void
5856make_snapshot_rec(fr, frp)
5857 frame_T *fr;
5858 frame_T **frp;
5859{
5860 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5861 if (*frp == NULL)
5862 return;
5863 (*frp)->fr_layout = fr->fr_layout;
5864# ifdef FEAT_VERTSPLIT
5865 (*frp)->fr_width = fr->fr_width;
5866# endif
5867 (*frp)->fr_height = fr->fr_height;
5868 if (fr->fr_next != NULL)
5869 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5870 if (fr->fr_child != NULL)
5871 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5872 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5873 (*frp)->fr_win = curwin;
5874}
5875
5876/*
5877 * Remove any existing snapshot.
5878 */
5879 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005880clear_snapshot(tp)
5881 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005883 clear_snapshot_rec(tp->tp_snapshot);
5884 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886
5887 static void
5888clear_snapshot_rec(fr)
5889 frame_T *fr;
5890{
5891 if (fr != NULL)
5892 {
5893 clear_snapshot_rec(fr->fr_next);
5894 clear_snapshot_rec(fr->fr_child);
5895 vim_free(fr);
5896 }
5897}
5898
5899/*
5900 * Restore a previously created snapshot, if there is any.
5901 * This is only done if the screen size didn't change and the window layout is
5902 * still the same.
5903 */
5904 static void
5905restore_snapshot(close_curwin)
5906 int close_curwin; /* closing current window */
5907{
5908 win_T *wp;
5909
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005910 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005912 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005914 && curtab->tp_snapshot->fr_height == topframe->fr_height
5915 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005917 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 win_comp_pos();
5919 if (wp != NULL && close_curwin)
5920 win_goto(wp);
5921 redraw_all_later(CLEAR);
5922 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005923 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924}
5925
5926/*
5927 * Check if frames "sn" and "fr" have the same layout, same following frames
5928 * and same children.
5929 */
5930 static int
5931check_snapshot_rec(sn, fr)
5932 frame_T *sn;
5933 frame_T *fr;
5934{
5935 if (sn->fr_layout != fr->fr_layout
5936 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5937 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5938 || (sn->fr_next != NULL
5939 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5940 || (sn->fr_child != NULL
5941 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5942 return FAIL;
5943 return OK;
5944}
5945
5946/*
5947 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5948 * following frames and children.
5949 * Returns a pointer to the old current window, or NULL.
5950 */
5951 static win_T *
5952restore_snapshot_rec(sn, fr)
5953 frame_T *sn;
5954 frame_T *fr;
5955{
5956 win_T *wp = NULL;
5957 win_T *wp2;
5958
5959 fr->fr_height = sn->fr_height;
5960# ifdef FEAT_VERTSPLIT
5961 fr->fr_width = sn->fr_width;
5962# endif
5963 if (fr->fr_layout == FR_LEAF)
5964 {
5965 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5966# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005967 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968# endif
5969 wp = sn->fr_win;
5970 }
5971 if (sn->fr_next != NULL)
5972 {
5973 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5974 if (wp2 != NULL)
5975 wp = wp2;
5976 }
5977 if (sn->fr_child != NULL)
5978 {
5979 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5980 if (wp2 != NULL)
5981 wp = wp2;
5982 }
5983 return wp;
5984}
5985
5986#endif
5987
5988#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5989/*
5990 * Return TRUE if there is any vertically split window.
5991 */
5992 int
5993win_hasvertsplit()
5994{
5995 frame_T *fr;
5996
5997 if (topframe->fr_layout == FR_ROW)
5998 return TRUE;
5999
6000 if (topframe->fr_layout == FR_COL)
6001 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6002 if (fr->fr_layout == FR_ROW)
6003 return TRUE;
6004
6005 return FALSE;
6006}
6007#endif