blob: 26003f37ae918be1de6d5e977ea8ed69fecb248c [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:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000485wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000488 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (ptr != NULL)
490 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000491# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 setpcmark();
495 if (win_split(0, 0) == OK)
496 {
497# ifdef FEAT_SCROLLBIND
498 curwin->w_p_scb = FALSE;
499# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000500 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
501 if (nchar == 'F' && lnum >= 0)
502 {
503 curwin->w_cursor.lnum = lnum;
504 check_cursor_lnum();
505 beginline(BL_SOL | BL_FIX);
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 }
508 vim_free(ptr);
509 }
510 break;
511#endif
512
513#ifdef FEAT_FIND_ID
514/* Go to the first occurence of the identifier under cursor along path in a
515 * new window -- webb
516 */
517 case 'i': /* Go to any match */
518 case Ctrl_I:
519 type = FIND_ANY;
520 /* FALLTHROUGH */
521 case 'd': /* Go to definition, using 'define' */
522 case Ctrl_D:
523 CHECK_CMDWIN
524 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
525 break;
526 find_pattern_in_path(ptr, 0, len, TRUE,
527 Prenum == 0 ? TRUE : FALSE, type,
528 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
529 curwin->w_set_curswant = TRUE;
530 break;
531#endif
532
Bram Moolenaar05159a02005-02-26 23:04:13 +0000533 case K_KENTER:
534 case CAR:
535#if defined(FEAT_QUICKFIX)
536 /*
537 * In a quickfix window a <CR> jumps to the error under the
538 * cursor in a new window.
539 */
540 if (bt_quickfix(curbuf))
541 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000542 sprintf((char *)cbuf, "split +%ld%s",
543 (long)curwin->w_cursor.lnum,
544 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000545 do_cmdline_cmd(cbuf);
546 }
547#endif
548 break;
549
550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551/* CTRL-W g extended commands */
552 case 'g':
553 case Ctrl_G:
554 CHECK_CMDWIN
555#ifdef USE_ON_FLY_SCROLL
556 dont_scroll = TRUE; /* disallow scrolling here */
557#endif
558 ++no_mapping;
559 ++allow_keys; /* no mapping for xchar, but allow key codes */
560 if (xchar == NUL)
561 xchar = safe_vgetc();
562#ifdef FEAT_LANGMAP
563 LANGMAP_ADJUST(xchar, TRUE);
564#endif
565 --no_mapping;
566 --allow_keys;
567#ifdef FEAT_CMDL_INFO
568 (void)add_to_showcmd(xchar);
569#endif
570 switch (xchar)
571 {
572#if defined(FEAT_QUICKFIX)
573 case '}':
574 xchar = Ctrl_RSB;
575 if (Prenum)
576 g_do_tagpreview = Prenum;
577 else
578 g_do_tagpreview = p_pvh;
579 /*FALLTHROUGH*/
580#endif
581 case ']':
582 case Ctrl_RSB:
583#ifdef FEAT_VISUAL
584 reset_VIsual_and_resel(); /* stop Visual mode */
585#endif
586 if (Prenum)
587 postponed_split = Prenum;
588 else
589 postponed_split = -1;
590
591 /* Execute the command right here, required when
592 * "wincmd g}" was used in a function. */
593 do_nv_ident('g', xchar);
594 break;
595
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000596#ifdef FEAT_SEARCHPATH
597 case 'f': /* CTRL-W gf: "gf" in a new tab page */
598 cmdmod.tab = TRUE;
599 goto wingotofile;
600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 default:
602 beep_flush();
603 break;
604 }
605 break;
606
607 default: beep_flush();
608 break;
609 }
610}
611
612/*
613 * split the current window, implements CTRL-W s and :split
614 *
615 * "size" is the height or width for the new window, 0 to use half of current
616 * height or width.
617 *
618 * "flags":
619 * WSP_ROOM: require enough room for new window
620 * WSP_VERT: vertical split.
621 * WSP_TOP: open window at the top-left of the shell (help window).
622 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
623 * WSP_HELP: creating the help window, keep layout snapshot
624 *
625 * return FAIL for failure, OK otherwise
626 */
627 int
628win_split(size, flags)
629 int size;
630 int flags;
631{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000632 /* When the ":tab" modifier was used open a new tab page instead. */
633 if (may_open_tabpage() == OK)
634 return OK;
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /* Add flags from ":vertical", ":topleft" and ":botright". */
637 flags |= cmdmod.split;
638 if ((flags & WSP_TOP) && (flags & WSP_BOT))
639 {
640 EMSG(_("E442: Can't split topleft and botright at the same time"));
641 return FAIL;
642 }
643
644 /* When creating the help window make a snapshot of the window layout.
645 * Otherwise clear the snapshot, it's now invalid. */
646 if (flags & WSP_HELP)
647 make_snapshot();
648 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000649 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
651 return win_split_ins(size, flags, NULL, 0);
652}
653
654/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000655 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 * When "newwin" is not NULL: insert this window at the far
657 * top/left/right/bottom.
658 * return FAIL for failure, OK otherwise
659 */
660 static int
661win_split_ins(size, flags, newwin, dir)
662 int size;
663 int flags;
664 win_T *newwin;
665 int dir;
666{
667 win_T *wp = newwin;
668 win_T *oldwin;
669 int new_size = size;
670 int i;
671 int need_status = 0;
672 int do_equal = FALSE;
673 int needed;
674 int available;
675 int oldwin_height = 0;
676 int layout;
677 frame_T *frp, *curfrp;
678 int before;
679
680 if (flags & WSP_TOP)
681 oldwin = firstwin;
682 else if (flags & WSP_BOT)
683 oldwin = lastwin;
684 else
685 oldwin = curwin;
686
687 /* add a status line when p_ls == 1 and splitting the first window */
688 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
689 {
690 if (oldwin->w_height <= p_wmh && newwin == NULL)
691 {
692 EMSG(_(e_noroom));
693 return FAIL;
694 }
695 need_status = STATUS_HEIGHT;
696 }
697
698#ifdef FEAT_VERTSPLIT
699 if (flags & WSP_VERT)
700 {
701 layout = FR_ROW;
702 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
703
704 /*
705 * Check if we are able to split the current window and compute its
706 * width.
707 */
708 needed = p_wmw + 1;
709 if (flags & WSP_ROOM)
710 needed += p_wiw - p_wmw;
711 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
712 {
713 available = topframe->fr_width;
714 needed += frame_minwidth(topframe, NULL);
715 }
716 else
717 available = oldwin->w_width;
718 if (available < needed && newwin == NULL)
719 {
720 EMSG(_(e_noroom));
721 return FAIL;
722 }
723 if (new_size == 0)
724 new_size = oldwin->w_width / 2;
725 if (new_size > oldwin->w_width - p_wmw - 1)
726 new_size = oldwin->w_width - p_wmw - 1;
727 if (new_size < p_wmw)
728 new_size = p_wmw;
729
730 /* if it doesn't fit in the current window, need win_equal() */
731 if (oldwin->w_width - new_size - 1 < p_wmw)
732 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000733
734 /* We don't like to take lines for the new window from a
735 * 'winfixwidth' window. Take them from a window to the left or right
736 * instead, if possible. */
737 if (oldwin->w_p_wfw)
738 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740 else
741#endif
742 {
743 layout = FR_COL;
744 do_equal = (p_ea && new_size == 0
745#ifdef FEAT_VERTSPLIT
746 && *p_ead != 'h'
747#endif
748 );
749
750 /*
751 * Check if we are able to split the current window and compute its
752 * height.
753 */
754 needed = p_wmh + STATUS_HEIGHT + need_status;
755 if (flags & WSP_ROOM)
756 needed += p_wh - p_wmh;
757 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
758 {
759 available = topframe->fr_height;
760 needed += frame_minheight(topframe, NULL);
761 }
762 else
763 {
764 available = oldwin->w_height;
765 needed += p_wmh;
766 }
767 if (available < needed && newwin == NULL)
768 {
769 EMSG(_(e_noroom));
770 return FAIL;
771 }
772 oldwin_height = oldwin->w_height;
773 if (need_status)
774 {
775 oldwin->w_status_height = STATUS_HEIGHT;
776 oldwin_height -= STATUS_HEIGHT;
777 }
778 if (new_size == 0)
779 new_size = oldwin_height / 2;
780
781 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
782 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
783 if (new_size < p_wmh)
784 new_size = p_wmh;
785
786 /* if it doesn't fit in the current window, need win_equal() */
787 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
788 do_equal = TRUE;
789
790 /* We don't like to take lines for the new window from a
791 * 'winfixheight' window. Take them from a window above or below
792 * instead, if possible. */
793 if (oldwin->w_p_wfh)
794 {
795 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
796 oldwin);
797 oldwin_height = oldwin->w_height;
798 if (need_status)
799 oldwin_height -= STATUS_HEIGHT;
800 }
801 }
802
803 /*
804 * allocate new window structure and link it in the window list
805 */
806 if ((flags & WSP_TOP) == 0
807 && ((flags & WSP_BOT)
808 || (flags & WSP_BELOW)
809 || (!(flags & WSP_ABOVE)
810 && (
811#ifdef FEAT_VERTSPLIT
812 (flags & WSP_VERT) ? p_spr :
813#endif
814 p_sb))))
815 {
816 /* new window below/right of current one */
817 if (newwin == NULL)
818 wp = win_alloc(oldwin);
819 else
820 win_append(oldwin, wp);
821 }
822 else
823 {
824 if (newwin == NULL)
825 wp = win_alloc(oldwin->w_prev);
826 else
827 win_append(oldwin->w_prev, wp);
828 }
829
830 if (newwin == NULL)
831 {
832 if (wp == NULL)
833 return FAIL;
834
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000835 /* make the contents of the new window the same as the current one */
836 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838
839 /*
840 * Reorganise the tree of frames to insert the new window.
841 */
842 if (flags & (WSP_TOP | WSP_BOT))
843 {
844#ifdef FEAT_VERTSPLIT
845 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
846 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
847#else
848 if (topframe->fr_layout == FR_COL)
849#endif
850 {
851 curfrp = topframe->fr_child;
852 if (flags & WSP_BOT)
853 while (curfrp->fr_next != NULL)
854 curfrp = curfrp->fr_next;
855 }
856 else
857 curfrp = topframe;
858 before = (flags & WSP_TOP);
859 }
860 else
861 {
862 curfrp = oldwin->w_frame;
863 if (flags & WSP_BELOW)
864 before = FALSE;
865 else if (flags & WSP_ABOVE)
866 before = TRUE;
867 else
868#ifdef FEAT_VERTSPLIT
869 if (flags & WSP_VERT)
870 before = !p_spr;
871 else
872#endif
873 before = !p_sb;
874 }
875 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
876 {
877 /* Need to create a new frame in the tree to make a branch. */
878 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
879 *frp = *curfrp;
880 curfrp->fr_layout = layout;
881 frp->fr_parent = curfrp;
882 frp->fr_next = NULL;
883 frp->fr_prev = NULL;
884 curfrp->fr_child = frp;
885 curfrp->fr_win = NULL;
886 curfrp = frp;
887 if (frp->fr_win != NULL)
888 oldwin->w_frame = frp;
889 else
890 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
891 frp->fr_parent = curfrp;
892 }
893
894 if (newwin == NULL)
895 {
896 /* Create a frame for the new window. */
897 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
898 frp->fr_layout = FR_LEAF;
899 frp->fr_win = wp;
900 wp->w_frame = frp;
901 }
902 else
903 frp = newwin->w_frame;
904 frp->fr_parent = curfrp->fr_parent;
905
906 /* Insert the new frame at the right place in the frame list. */
907 if (before)
908 frame_insert(curfrp, frp);
909 else
910 frame_append(curfrp, frp);
911
912#ifdef FEAT_VERTSPLIT
913 if (flags & WSP_VERT)
914 {
915 wp->w_p_scr = curwin->w_p_scr;
916 if (need_status)
917 {
918 --oldwin->w_height;
919 oldwin->w_status_height = need_status;
920 }
921 if (flags & (WSP_TOP | WSP_BOT))
922 {
923 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000924 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 wp->w_height = curfrp->fr_height - (p_ls > 0);
926 wp->w_status_height = (p_ls > 0);
927 }
928 else
929 {
930 /* height and row of new window is same as current window */
931 wp->w_winrow = oldwin->w_winrow;
932 wp->w_height = oldwin->w_height;
933 wp->w_status_height = oldwin->w_status_height;
934 }
935 frp->fr_height = curfrp->fr_height;
936
937 /* "new_size" of the current window goes to the new window, use
938 * one column for the vertical separator */
939 wp->w_width = new_size;
940 if (before)
941 wp->w_vsep_width = 1;
942 else
943 {
944 wp->w_vsep_width = oldwin->w_vsep_width;
945 oldwin->w_vsep_width = 1;
946 }
947 if (flags & (WSP_TOP | WSP_BOT))
948 {
949 if (flags & WSP_BOT)
950 frame_add_vsep(curfrp);
951 /* Set width of neighbor frame */
952 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000953 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
954 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 }
956 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000957 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (before) /* new window left of current one */
959 {
960 wp->w_wincol = oldwin->w_wincol;
961 oldwin->w_wincol += new_size + 1;
962 }
963 else /* new window right of current one */
964 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
965 frame_fix_width(oldwin);
966 frame_fix_width(wp);
967 }
968 else
969#endif
970 {
971 /* width and column of new window is same as current window */
972#ifdef FEAT_VERTSPLIT
973 if (flags & (WSP_TOP | WSP_BOT))
974 {
975 wp->w_wincol = 0;
976 wp->w_width = Columns;
977 wp->w_vsep_width = 0;
978 }
979 else
980 {
981 wp->w_wincol = oldwin->w_wincol;
982 wp->w_width = oldwin->w_width;
983 wp->w_vsep_width = oldwin->w_vsep_width;
984 }
985 frp->fr_width = curfrp->fr_width;
986#endif
987
988 /* "new_size" of the current window goes to the new window, use
989 * one row for the status line */
990 win_new_height(wp, new_size);
991 if (flags & (WSP_TOP | WSP_BOT))
992 frame_new_height(curfrp, curfrp->fr_height
993 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
994 else
995 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
996 if (before) /* new window above current one */
997 {
998 wp->w_winrow = oldwin->w_winrow;
999 wp->w_status_height = STATUS_HEIGHT;
1000 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1001 }
1002 else /* new window below current one */
1003 {
1004 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1005 wp->w_status_height = oldwin->w_status_height;
1006 oldwin->w_status_height = STATUS_HEIGHT;
1007 }
1008#ifdef FEAT_VERTSPLIT
1009 if (flags & WSP_BOT)
1010 frame_add_statusline(curfrp);
1011#endif
1012 frame_fix_height(wp);
1013 frame_fix_height(oldwin);
1014 }
1015
1016 if (flags & (WSP_TOP | WSP_BOT))
1017 (void)win_comp_pos();
1018
1019 /*
1020 * Both windows need redrawing
1021 */
1022 redraw_win_later(wp, NOT_VALID);
1023 wp->w_redr_status = TRUE;
1024 redraw_win_later(oldwin, NOT_VALID);
1025 oldwin->w_redr_status = TRUE;
1026
1027 if (need_status)
1028 {
1029 msg_row = Rows - 1;
1030 msg_col = sc_col;
1031 msg_clr_eos_force(); /* Old command/ruler may still be there */
1032 comp_col();
1033 msg_row = Rows - 1;
1034 msg_col = 0; /* put position back at start of line */
1035 }
1036
1037 /*
1038 * make the new window the current window and redraw
1039 */
1040 if (do_equal || dir != 0)
1041 win_equal(wp, TRUE,
1042#ifdef FEAT_VERTSPLIT
1043 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1044 : dir == 'h' ? 'b' :
1045#endif
1046 'v');
1047
1048 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1049 * size was given. */
1050#ifdef FEAT_VERTSPLIT
1051 if (flags & WSP_VERT)
1052 {
1053 i = p_wiw;
1054 if (size != 0)
1055 p_wiw = size;
1056
1057# ifdef FEAT_GUI
1058 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1059 if (gui.in_use)
1060 gui_init_which_components(NULL);
1061# endif
1062 }
1063 else
1064#endif
1065 {
1066 i = p_wh;
1067 if (size != 0)
1068 p_wh = size;
1069 }
1070 win_enter(wp, FALSE);
1071#ifdef FEAT_VERTSPLIT
1072 if (flags & WSP_VERT)
1073 p_wiw = i;
1074 else
1075#endif
1076 p_wh = i;
1077
1078 return OK;
1079}
1080
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001081/*
1082 * Initialize window "newp" from window "oldp".
1083 * Used when splitting a window and when creating a new tab page.
1084 * The windows will both edit the same buffer.
1085 */
1086 static void
1087win_init(newp, oldp)
1088 win_T *newp;
1089 win_T *oldp;
1090{
1091 int i;
1092
1093 newp->w_buffer = oldp->w_buffer;
1094 oldp->w_buffer->b_nwindows++;
1095 newp->w_cursor = oldp->w_cursor;
1096 newp->w_valid = 0;
1097 newp->w_curswant = oldp->w_curswant;
1098 newp->w_set_curswant = oldp->w_set_curswant;
1099 newp->w_topline = oldp->w_topline;
1100#ifdef FEAT_DIFF
1101 newp->w_topfill = oldp->w_topfill;
1102#endif
1103 newp->w_leftcol = oldp->w_leftcol;
1104 newp->w_pcmark = oldp->w_pcmark;
1105 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1106 newp->w_alt_fnum = oldp->w_alt_fnum;
1107 newp->w_fraction = oldp->w_fraction;
1108 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1109#ifdef FEAT_JUMPLIST
1110 copy_jumplist(oldp, newp);
1111#endif
1112#ifdef FEAT_QUICKFIX
1113 copy_loclist(oldp, newp);
1114#endif
1115 if (oldp->w_localdir != NULL)
1116 newp->w_localdir = vim_strsave(oldp->w_localdir);
1117
1118 /* Use the same argument list. */
1119 newp->w_alist = oldp->w_alist;
1120 ++newp->w_alist->al_refcount;
1121 newp->w_arg_idx = oldp->w_arg_idx;
1122
1123 /*
1124 * copy tagstack and options from existing window
1125 */
1126 for (i = 0; i < oldp->w_tagstacklen; i++)
1127 {
1128 newp->w_tagstack[i] = oldp->w_tagstack[i];
1129 if (newp->w_tagstack[i].tagname != NULL)
1130 newp->w_tagstack[i].tagname =
1131 vim_strsave(newp->w_tagstack[i].tagname);
1132 }
1133 newp->w_tagstackidx = oldp->w_tagstackidx;
1134 newp->w_tagstacklen = oldp->w_tagstacklen;
1135 win_copy_options(oldp, newp);
1136# ifdef FEAT_FOLDING
1137 copyFoldingState(oldp, newp);
1138# endif
1139}
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif /* FEAT_WINDOWS */
1142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#if defined(FEAT_WINDOWS) || defined(PROTO)
1144/*
1145 * Check if "win" is a pointer to an existing window.
1146 */
1147 int
1148win_valid(win)
1149 win_T *win;
1150{
1151 win_T *wp;
1152
1153 if (win == NULL)
1154 return FALSE;
1155 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1156 if (wp == win)
1157 return TRUE;
1158 return FALSE;
1159}
1160
1161/*
1162 * Return the number of windows.
1163 */
1164 int
1165win_count()
1166{
1167 win_T *wp;
1168 int count = 0;
1169
1170 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1171 ++count;
1172 return count;
1173}
1174
1175/*
1176 * Make "count" windows on the screen.
1177 * Return actual number of windows on the screen.
1178 * Must be called when there is just one window, filling the whole screen
1179 * (excluding the command line).
1180 */
1181/*ARGSUSED*/
1182 int
1183make_windows(count, vertical)
1184 int count;
1185 int vertical; /* split windows vertically if TRUE */
1186{
1187 int maxcount;
1188 int todo;
1189
1190#ifdef FEAT_VERTSPLIT
1191 if (vertical)
1192 {
1193 /* Each windows needs at least 'winminwidth' lines and a separator
1194 * column. */
1195 maxcount = (curwin->w_width + curwin->w_vsep_width
1196 - (p_wiw - p_wmw)) / (p_wmw + 1);
1197 }
1198 else
1199#endif
1200 {
1201 /* Each window needs at least 'winminheight' lines and a status line. */
1202 maxcount = (curwin->w_height + curwin->w_status_height
1203 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1204 }
1205
1206 if (maxcount < 2)
1207 maxcount = 2;
1208 if (count > maxcount)
1209 count = maxcount;
1210
1211 /*
1212 * add status line now, otherwise first window will be too big
1213 */
1214 if (count > 1)
1215 last_status(TRUE);
1216
1217#ifdef FEAT_AUTOCMD
1218 /*
1219 * Don't execute autocommands while creating the windows. Must do that
1220 * when putting the buffers in the windows.
1221 */
1222 ++autocmd_block;
1223#endif
1224
1225 /* todo is number of windows left to create */
1226 for (todo = count - 1; todo > 0; --todo)
1227#ifdef FEAT_VERTSPLIT
1228 if (vertical)
1229 {
1230 if (win_split(curwin->w_width - (curwin->w_width - todo)
1231 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1232 break;
1233 }
1234 else
1235#endif
1236 {
1237 if (win_split(curwin->w_height - (curwin->w_height - todo
1238 * STATUS_HEIGHT) / (todo + 1)
1239 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1240 break;
1241 }
1242
1243#ifdef FEAT_AUTOCMD
1244 --autocmd_block;
1245#endif
1246
1247 /* return actual number of windows */
1248 return (count - todo);
1249}
1250
1251/*
1252 * Exchange current and next window
1253 */
1254 static void
1255win_exchange(Prenum)
1256 long Prenum;
1257{
1258 frame_T *frp;
1259 frame_T *frp2;
1260 win_T *wp;
1261 win_T *wp2;
1262 int temp;
1263
1264 if (lastwin == firstwin) /* just one window */
1265 {
1266 beep_flush();
1267 return;
1268 }
1269
1270#ifdef FEAT_GUI
1271 need_mouse_correct = TRUE;
1272#endif
1273
1274 /*
1275 * find window to exchange with
1276 */
1277 if (Prenum)
1278 {
1279 frp = curwin->w_frame->fr_parent->fr_child;
1280 while (frp != NULL && --Prenum > 0)
1281 frp = frp->fr_next;
1282 }
1283 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1284 frp = curwin->w_frame->fr_next;
1285 else /* Swap last window in row/col with previous */
1286 frp = curwin->w_frame->fr_prev;
1287
1288 /* We can only exchange a window with another window, not with a frame
1289 * containing windows. */
1290 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1291 return;
1292 wp = frp->fr_win;
1293
1294/*
1295 * 1. remove curwin from the list. Remember after which window it was in wp2
1296 * 2. insert curwin before wp in the list
1297 * if wp != wp2
1298 * 3. remove wp from the list
1299 * 4. insert wp after wp2
1300 * 5. exchange the status line height and vsep width.
1301 */
1302 wp2 = curwin->w_prev;
1303 frp2 = curwin->w_frame->fr_prev;
1304 if (wp->w_prev != curwin)
1305 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001306 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 frame_remove(curwin->w_frame);
1308 win_append(wp->w_prev, curwin);
1309 frame_insert(frp, curwin->w_frame);
1310 }
1311 if (wp != wp2)
1312 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001313 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 frame_remove(wp->w_frame);
1315 win_append(wp2, wp);
1316 if (frp2 == NULL)
1317 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1318 else
1319 frame_append(frp2, wp->w_frame);
1320 }
1321 temp = curwin->w_status_height;
1322 curwin->w_status_height = wp->w_status_height;
1323 wp->w_status_height = temp;
1324#ifdef FEAT_VERTSPLIT
1325 temp = curwin->w_vsep_width;
1326 curwin->w_vsep_width = wp->w_vsep_width;
1327 wp->w_vsep_width = temp;
1328
1329 /* If the windows are not in the same frame, exchange the sizes to avoid
1330 * messing up the window layout. Otherwise fix the frame sizes. */
1331 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1332 {
1333 temp = curwin->w_height;
1334 curwin->w_height = wp->w_height;
1335 wp->w_height = temp;
1336 temp = curwin->w_width;
1337 curwin->w_width = wp->w_width;
1338 wp->w_width = temp;
1339 }
1340 else
1341 {
1342 frame_fix_height(curwin);
1343 frame_fix_height(wp);
1344 frame_fix_width(curwin);
1345 frame_fix_width(wp);
1346 }
1347#endif
1348
1349 (void)win_comp_pos(); /* recompute window positions */
1350
1351 win_enter(wp, TRUE);
1352 redraw_later(CLEAR);
1353}
1354
1355/*
1356 * rotate windows: if upwards TRUE the second window becomes the first one
1357 * if upwards FALSE the first window becomes the second one
1358 */
1359 static void
1360win_rotate(upwards, count)
1361 int upwards;
1362 int count;
1363{
1364 win_T *wp1;
1365 win_T *wp2;
1366 frame_T *frp;
1367 int n;
1368
1369 if (firstwin == lastwin) /* nothing to do */
1370 {
1371 beep_flush();
1372 return;
1373 }
1374
1375#ifdef FEAT_GUI
1376 need_mouse_correct = TRUE;
1377#endif
1378
1379#ifdef FEAT_VERTSPLIT
1380 /* Check if all frames in this row/col have one window. */
1381 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1382 frp = frp->fr_next)
1383 if (frp->fr_win == NULL)
1384 {
1385 EMSG(_("E443: Cannot rotate when another window is split"));
1386 return;
1387 }
1388#endif
1389
1390 while (count--)
1391 {
1392 if (upwards) /* first window becomes last window */
1393 {
1394 /* remove first window/frame from the list */
1395 frp = curwin->w_frame->fr_parent->fr_child;
1396 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 frame_remove(frp);
1399
1400 /* find last frame and append removed window/frame after it */
1401 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1402 ;
1403 win_append(frp->fr_win, wp1);
1404 frame_append(frp, wp1->w_frame);
1405
1406 wp2 = frp->fr_win; /* previously last window */
1407 }
1408 else /* last window becomes first window */
1409 {
1410 /* find last window/frame in the list and remove it */
1411 for (frp = curwin->w_frame; frp->fr_next != NULL;
1412 frp = frp->fr_next)
1413 ;
1414 wp1 = frp->fr_win;
1415 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 frame_remove(frp);
1418
1419 /* append the removed window/frame before the first in the list */
1420 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1421 frame_insert(frp->fr_parent->fr_child, frp);
1422 }
1423
1424 /* exchange status height and vsep width of old and new last window */
1425 n = wp2->w_status_height;
1426 wp2->w_status_height = wp1->w_status_height;
1427 wp1->w_status_height = n;
1428 frame_fix_height(wp1);
1429 frame_fix_height(wp2);
1430#ifdef FEAT_VERTSPLIT
1431 n = wp2->w_vsep_width;
1432 wp2->w_vsep_width = wp1->w_vsep_width;
1433 wp1->w_vsep_width = n;
1434 frame_fix_width(wp1);
1435 frame_fix_width(wp2);
1436#endif
1437
1438 /* recompute w_winrow and w_wincol for all windows */
1439 (void)win_comp_pos();
1440 }
1441
1442 redraw_later(CLEAR);
1443}
1444
1445/*
1446 * Move the current window to the very top/bottom/left/right of the screen.
1447 */
1448 static void
1449win_totop(size, flags)
1450 int size;
1451 int flags;
1452{
1453 int dir;
1454 int height = curwin->w_height;
1455
1456 if (lastwin == firstwin)
1457 {
1458 beep_flush();
1459 return;
1460 }
1461
1462 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001463 (void)winframe_remove(curwin, &dir, NULL);
1464 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 last_status(FALSE); /* may need to remove last status line */
1466 (void)win_comp_pos(); /* recompute window positions */
1467
1468 /* Split a window on the desired side and put the window there. */
1469 (void)win_split_ins(size, flags, curwin, dir);
1470 if (!(flags & WSP_VERT))
1471 {
1472 win_setheight(height);
1473 if (p_ea)
1474 win_equal(curwin, TRUE, 'v');
1475 }
1476
1477#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1478 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1479 * scrollbars. Have to update them anyway. */
1480 if (gui.in_use)
1481 {
1482 out_flush();
1483 gui_init_which_components(NULL);
1484 gui_update_scrollbars(TRUE);
1485 }
1486 need_mouse_correct = TRUE;
1487#endif
1488
1489}
1490
1491/*
1492 * Move window "win1" to below/right of "win2" and make "win1" the current
1493 * window. Only works within the same frame!
1494 */
1495 void
1496win_move_after(win1, win2)
1497 win_T *win1, *win2;
1498{
1499 int height;
1500
1501 /* check if the arguments are reasonable */
1502 if (win1 == win2)
1503 return;
1504
1505 /* check if there is something to do */
1506 if (win2->w_next != win1)
1507 {
1508 /* may need move the status line/vertical separator of the last window
1509 * */
1510 if (win1 == lastwin)
1511 {
1512 height = win1->w_prev->w_status_height;
1513 win1->w_prev->w_status_height = win1->w_status_height;
1514 win1->w_status_height = height;
1515#ifdef FEAT_VERTSPLIT
1516 win1->w_prev->w_vsep_width = 0;
1517 win1->w_vsep_width = 1;
1518#endif
1519 }
1520 else if (win2 == lastwin)
1521 {
1522 height = win1->w_status_height;
1523 win1->w_status_height = win2->w_status_height;
1524 win2->w_status_height = height;
1525#ifdef FEAT_VERTSPLIT
1526 win2->w_vsep_width = 1;
1527 win1->w_vsep_width = 0;
1528#endif
1529 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001530 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 frame_remove(win1->w_frame);
1532 win_append(win2, win1);
1533 frame_append(win2->w_frame, win1->w_frame);
1534
1535 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1536 redraw_later(NOT_VALID);
1537 }
1538 win_enter(win1, FALSE);
1539}
1540
1541/*
1542 * Make all windows the same height.
1543 * 'next_curwin' will soon be the current window, make sure it has enough
1544 * rows.
1545 */
1546 void
1547win_equal(next_curwin, current, dir)
1548 win_T *next_curwin; /* pointer to current window to be or NULL */
1549 int current; /* do only frame with current window */
1550 int dir; /* 'v' for vertically, 'h' for horizontally,
1551 'b' for both, 0 for using p_ead */
1552{
1553 if (dir == 0)
1554#ifdef FEAT_VERTSPLIT
1555 dir = *p_ead;
1556#else
1557 dir = 'b';
1558#endif
1559 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001560 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001561 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562}
1563
1564/*
1565 * Set a frame to a new position and height, spreading the available room
1566 * equally over contained frames.
1567 * The window "next_curwin" (if not NULL) should at least get the size from
1568 * 'winheight' and 'winwidth' if possible.
1569 */
1570 static void
1571win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1572 win_T *next_curwin; /* pointer to current window to be or NULL */
1573 int current; /* do only frame with current window */
1574 frame_T *topfr; /* frame to set size off */
1575 int dir; /* 'v', 'h' or 'b', see win_equal() */
1576 int col; /* horizontal position for frame */
1577 int row; /* vertical position for frame */
1578 int width; /* new width of frame */
1579 int height; /* new height of frame */
1580{
1581 int n, m;
1582 int extra_sep = 0;
1583 int wincount, totwincount = 0;
1584 frame_T *fr;
1585 int next_curwin_size = 0;
1586 int room = 0;
1587 int new_size;
1588 int has_next_curwin = 0;
1589 int hnc;
1590
1591 if (topfr->fr_layout == FR_LEAF)
1592 {
1593 /* Set the width/height of this frame.
1594 * Redraw when size or position changes */
1595 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1596#ifdef FEAT_VERTSPLIT
1597 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1598#endif
1599 )
1600 {
1601 topfr->fr_win->w_winrow = row;
1602 frame_new_height(topfr, height, FALSE, FALSE);
1603#ifdef FEAT_VERTSPLIT
1604 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001605 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606#endif
1607 redraw_all_later(CLEAR);
1608 }
1609 }
1610#ifdef FEAT_VERTSPLIT
1611 else if (topfr->fr_layout == FR_ROW)
1612 {
1613 topfr->fr_width = width;
1614 topfr->fr_height = height;
1615
1616 if (dir != 'v') /* equalize frame widths */
1617 {
1618 /* Compute the maximum number of windows horizontally in this
1619 * frame. */
1620 n = frame_minwidth(topfr, NOWIN);
1621 /* add one for the rightmost window, it doesn't have a separator */
1622 if (col + width == Columns)
1623 extra_sep = 1;
1624 else
1625 extra_sep = 0;
1626 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001627 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001629 /*
1630 * Compute width for "next_curwin" window and room available for
1631 * other windows.
1632 * "m" is the minimal width when counting p_wiw for "next_curwin".
1633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 m = frame_minwidth(topfr, next_curwin);
1635 room = width - m;
1636 if (room < 0)
1637 {
1638 next_curwin_size = p_wiw + room;
1639 room = 0;
1640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 else
1642 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001643 next_curwin_size = -1;
1644 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1645 {
1646 /* If 'winfixwidth' set keep the window width if
1647 * possible.
1648 * Watch out for this window being the next_curwin. */
1649 if (frame_fixed_width(fr))
1650 {
1651 n = frame_minwidth(fr, NOWIN);
1652 new_size = fr->fr_width;
1653 if (frame_has_win(fr, next_curwin))
1654 {
1655 room += p_wiw - p_wmw;
1656 next_curwin_size = 0;
1657 if (new_size < p_wiw)
1658 new_size = p_wiw;
1659 }
1660 else
1661 /* These windows don't use up room. */
1662 totwincount -= (n + (fr->fr_next == NULL
1663 ? extra_sep : 0)) / (p_wmw + 1);
1664 room -= new_size - n;
1665 if (room < 0)
1666 {
1667 new_size += room;
1668 room = 0;
1669 }
1670 fr->fr_newwidth = new_size;
1671 }
1672 }
1673 if (next_curwin_size == -1)
1674 {
1675 if (!has_next_curwin)
1676 next_curwin_size = 0;
1677 else if (totwincount > 1
1678 && (room + (totwincount - 2))
1679 / (totwincount - 1) > p_wiw)
1680 {
1681 next_curwin_size = (room + p_wiw + totwincount * p_wmw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001683 room -= next_curwin_size - p_wiw;
1684 }
1685 else
1686 next_curwin_size = p_wiw;
1687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001689
1690 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 --totwincount; /* don't count curwin */
1692 }
1693
1694 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1695 {
1696 n = m = 0;
1697 wincount = 1;
1698 if (fr->fr_next == NULL)
1699 /* last frame gets all that remains (avoid roundoff error) */
1700 new_size = width;
1701 else if (dir == 'v')
1702 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001703 else if (frame_fixed_width(fr))
1704 {
1705 new_size = fr->fr_newwidth;
1706 wincount = 0; /* doesn't count as a sizeable window */
1707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 else
1709 {
1710 /* Compute the maximum number of windows horiz. in "fr". */
1711 n = frame_minwidth(fr, NOWIN);
1712 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1713 / (p_wmw + 1);
1714 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001715 if (has_next_curwin)
1716 hnc = frame_has_win(fr, next_curwin);
1717 else
1718 hnc = FALSE;
1719 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001721 if (totwincount == 0)
1722 new_size = room;
1723 else
1724 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001726 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
1728 next_curwin_size -= p_wiw - (m - n);
1729 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001730 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001732 else
1733 room -= new_size;
1734 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 }
1736
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001737 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 * window, unless equalizing all frames. */
1739 if (!current || dir != 'v' || topfr->fr_parent != NULL
1740 || (new_size != fr->fr_width)
1741 || frame_has_win(fr, next_curwin))
1742 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001743 new_size, height);
1744 col += new_size;
1745 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 totwincount -= wincount;
1747 }
1748 }
1749#endif
1750 else /* topfr->fr_layout == FR_COL */
1751 {
1752#ifdef FEAT_VERTSPLIT
1753 topfr->fr_width = width;
1754#endif
1755 topfr->fr_height = height;
1756
1757 if (dir != 'h') /* equalize frame heights */
1758 {
1759 /* Compute maximum number of windows vertically in this frame. */
1760 n = frame_minheight(topfr, NOWIN);
1761 /* add one for the bottom window if it doesn't have a statusline */
1762 if (row + height == cmdline_row && p_ls == 0)
1763 extra_sep = 1;
1764 else
1765 extra_sep = 0;
1766 totwincount = (n + extra_sep) / (p_wmh + 1);
1767 has_next_curwin = frame_has_win(topfr, next_curwin);
1768
1769 /*
1770 * Compute height for "next_curwin" window and room available for
1771 * other windows.
1772 * "m" is the minimal height when counting p_wh for "next_curwin".
1773 */
1774 m = frame_minheight(topfr, next_curwin);
1775 room = height - m;
1776 if (room < 0)
1777 {
1778 /* The room is less then 'winheight', use all space for the
1779 * current window. */
1780 next_curwin_size = p_wh + room;
1781 room = 0;
1782 }
1783 else
1784 {
1785 next_curwin_size = -1;
1786 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1787 {
1788 /* If 'winfixheight' set keep the window height if
1789 * possible.
1790 * Watch out for this window being the next_curwin. */
1791 if (frame_fixed_height(fr))
1792 {
1793 n = frame_minheight(fr, NOWIN);
1794 new_size = fr->fr_height;
1795 if (frame_has_win(fr, next_curwin))
1796 {
1797 room += p_wh - p_wmh;
1798 next_curwin_size = 0;
1799 if (new_size < p_wh)
1800 new_size = p_wh;
1801 }
1802 else
1803 /* These windows don't use up room. */
1804 totwincount -= (n + (fr->fr_next == NULL
1805 ? extra_sep : 0)) / (p_wmh + 1);
1806 room -= new_size - n;
1807 if (room < 0)
1808 {
1809 new_size += room;
1810 room = 0;
1811 }
1812 fr->fr_newheight = new_size;
1813 }
1814 }
1815 if (next_curwin_size == -1)
1816 {
1817 if (!has_next_curwin)
1818 next_curwin_size = 0;
1819 else if (totwincount > 1
1820 && (room + (totwincount - 2))
1821 / (totwincount - 1) > p_wh)
1822 {
1823 next_curwin_size = (room + p_wh + totwincount * p_wmh
1824 + (totwincount - 1)) / totwincount;
1825 room -= next_curwin_size - p_wh;
1826 }
1827 else
1828 next_curwin_size = p_wh;
1829 }
1830 }
1831
1832 if (has_next_curwin)
1833 --totwincount; /* don't count curwin */
1834 }
1835
1836 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1837 {
1838 n = m = 0;
1839 wincount = 1;
1840 if (fr->fr_next == NULL)
1841 /* last frame gets all that remains (avoid roundoff error) */
1842 new_size = height;
1843 else if (dir == 'h')
1844 new_size = fr->fr_height;
1845 else if (frame_fixed_height(fr))
1846 {
1847 new_size = fr->fr_newheight;
1848 wincount = 0; /* doesn't count as a sizeable window */
1849 }
1850 else
1851 {
1852 /* Compute the maximum number of windows vert. in "fr". */
1853 n = frame_minheight(fr, NOWIN);
1854 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1855 / (p_wmh + 1);
1856 m = frame_minheight(fr, next_curwin);
1857 if (has_next_curwin)
1858 hnc = frame_has_win(fr, next_curwin);
1859 else
1860 hnc = FALSE;
1861 if (hnc) /* don't count next_curwin */
1862 --wincount;
1863 if (totwincount == 0)
1864 new_size = room;
1865 else
1866 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1867 / totwincount;
1868 if (hnc) /* add next_curwin size */
1869 {
1870 next_curwin_size -= p_wh - (m - n);
1871 new_size += next_curwin_size;
1872 room -= new_size - next_curwin_size;
1873 }
1874 else
1875 room -= new_size;
1876 new_size += n;
1877 }
1878 /* Skip frame that is full width when splitting or closing a
1879 * window, unless equalizing all frames. */
1880 if (!current || dir != 'h' || topfr->fr_parent != NULL
1881 || (new_size != fr->fr_height)
1882 || frame_has_win(fr, next_curwin))
1883 win_equal_rec(next_curwin, current, fr, dir, col, row,
1884 width, new_size);
1885 row += new_size;
1886 height -= new_size;
1887 totwincount -= wincount;
1888 }
1889 }
1890}
1891
1892/*
1893 * close all windows for buffer 'buf'
1894 */
1895 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001896close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001898 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001900 win_T *wp;
1901 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001902 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001905
1906 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001908 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001910 win_close(wp, FALSE);
1911
1912 /* Start all over, autocommands may change the window layout. */
1913 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 }
1915 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001916 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001918
1919 /* Also check windows in other tab pages. */
1920 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1921 {
1922 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001923 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001924 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1925 if (wp->w_buffer == buf)
1926 {
1927 win_close_othertab(wp, FALSE, tp);
1928
1929 /* Start all over, the tab page may be closed and
1930 * autocommands may change the window layout. */
1931 nexttp = first_tabpage;
1932 break;
1933 }
1934 }
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001937
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001938 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001939 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941
1942/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001943 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001944 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001945 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001946 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001947last_window()
1948{
1949 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1950}
1951
1952/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 * If "free_buf" is TRUE related buffer may be unloaded.
1955 *
1956 * called by :quit, :close, :xit, :wq and findtag()
1957 */
1958 void
1959win_close(win, free_buf)
1960 win_T *win;
1961 int free_buf;
1962{
1963 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001964 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#ifdef FEAT_AUTOCMD
1966 int other_buffer = FALSE;
1967#endif
1968 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 int dir;
1970 int help_window = FALSE;
1971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001972 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
1974 EMSG(_("E444: Cannot close last window"));
1975 return;
1976 }
1977
1978 /* When closing the help window, try restoring a snapshot after closing
1979 * the window. Otherwise clear the snapshot, it's now invalid. */
1980 if (win->w_buffer->b_help)
1981 help_window = TRUE;
1982 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001983 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985#ifdef FEAT_AUTOCMD
1986 if (win == curwin)
1987 {
1988 /*
1989 * Guess which window is going to be the new current window.
1990 * This may change because of the autocommands (sigh).
1991 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001992 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 /*
1995 * Be careful: If autocommands delete the window, return now.
1996 */
1997 if (wp->w_buffer != curbuf)
1998 {
1999 other_buffer = TRUE;
2000 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002001 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 return;
2003 }
2004 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002005 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return;
2007# ifdef FEAT_EVAL
2008 /* autocmds may abort script processing */
2009 if (aborting())
2010 return;
2011# endif
2012 }
2013#endif
2014
2015 /*
2016 * Close the link to the buffer.
2017 */
2018 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2019 /* Autocommands may have closed the window already, or closed the only
2020 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002021 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 return;
2023
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002024 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002027 /* When closing the last window in a tab page go to another tab page. */
2028 if (wp == NULL)
2029 {
2030 tabpage_T *ptp = NULL;
2031 tabpage_T *tp;
2032 tabpage_T *atp = alt_tabpage();
2033
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002034 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002035 ptp = tp;
2036 if (tp == NULL)
2037 {
2038 EMSG2(_(e_intern2), "win_close()");
2039 return;
2040 }
2041 if (ptp == NULL)
2042 first_tabpage = tp->tp_next;
2043 else
2044 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002045 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002046
2047 /* We don't do the window resizing stuff, let enter_tabpage() take
2048 * care of entering a window in another tab page. */
2049 enter_tabpage(atp, old_curbuf);
2050 return;
2051 }
2052
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 /* Make sure curwin isn't invalid. It can cause severe trouble when
2054 * printing an error message. For win_equal() curbuf needs to be valid
2055 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002056 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 curwin = wp;
2059#ifdef FEAT_QUICKFIX
2060 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2061 {
2062 /*
2063 * The cursor goes to the preview or the quickfix window, try
2064 * finding another window to go to.
2065 */
2066 for (;;)
2067 {
2068 if (wp->w_next == NULL)
2069 wp = firstwin;
2070 else
2071 wp = wp->w_next;
2072 if (wp == curwin)
2073 break;
2074 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2075 {
2076 curwin = wp;
2077 break;
2078 }
2079 }
2080 }
2081#endif
2082 curbuf = curwin->w_buffer;
2083 close_curwin = TRUE;
2084 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002085 if (p_ea
2086#ifdef FEAT_VERTSPLIT
2087 && (*p_ead == 'b' || *p_ead == dir)
2088#endif
2089 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 win_equal(curwin, TRUE,
2091#ifdef FEAT_VERTSPLIT
2092 dir
2093#else
2094 0
2095#endif
2096 );
2097 else
2098 win_comp_pos();
2099 if (close_curwin)
2100 {
2101 win_enter_ext(wp, FALSE, TRUE);
2102#ifdef FEAT_AUTOCMD
2103 if (other_buffer)
2104 /* careful: after this wp and win may be invalid! */
2105 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2106#endif
2107 }
2108
2109 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002110 * If last window has a status line now and we don't want one,
2111 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 */
2113 last_status(FALSE);
2114
2115 /* After closing the help window, try restoring the window layout from
2116 * before it was opened. */
2117 if (help_window)
2118 restore_snapshot(close_curwin);
2119
2120#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2121 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2122 if (gui.in_use && !win_hasvertsplit())
2123 gui_init_which_components(NULL);
2124#endif
2125
2126 redraw_all_later(NOT_VALID);
2127}
2128
2129/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002130 * Close window "win" in tab page "tp", which is not the current tab page.
2131 * This may be the last window ih that tab page and result in closing the tab,
2132 * thus "tp" may become invalid!
2133 * Called must check if buffer is hidden.
2134 */
2135 void
2136win_close_othertab(win, free_buf, tp)
2137 win_T *win;
2138 int free_buf;
2139 tabpage_T *tp;
2140{
2141 win_T *wp;
2142 int dir;
2143 tabpage_T *ptp = NULL;
2144
2145 /* Close the link to the buffer. */
2146 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2147
2148 /* Careful: Autocommands may have closed the tab page or made it the
2149 * current tab page. */
2150 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2151 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002152 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002153 return;
2154
2155 /* Autocommands may have closed the window already. */
2156 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2157 ;
2158 if (wp == NULL)
2159 return;
2160
2161 /* Free the memory used for the window. */
2162 wp = win_free_mem(win, &dir, tp);
2163
2164 /* When closing the last window in a tab page remove the tab page. */
2165 if (wp == NULL)
2166 {
2167 if (tp == first_tabpage)
2168 first_tabpage = tp->tp_next;
2169 else
2170 {
2171 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2172 ptp = ptp->tp_next)
2173 ;
2174 if (ptp == NULL)
2175 {
2176 EMSG2(_(e_intern2), "win_close_othertab()");
2177 return;
2178 }
2179 ptp->tp_next = tp->tp_next;
2180 }
2181 vim_free(tp);
2182 }
2183}
2184
2185/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002186 * Free the memory used for a window.
2187 * Returns a pointer to the window that got the freed up space.
2188 */
2189 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002190win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002191 win_T *win;
2192 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002193 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002194{
2195 frame_T *frp;
2196 win_T *wp;
2197
Bram Moolenaarea408852005-06-25 22:49:46 +00002198#ifdef FEAT_FOLDING
2199 clearFolding(win);
2200#endif
2201
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002202 /* reduce the reference count to the argument list. */
2203 alist_unlink(win->w_alist);
2204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002205 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002206 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002207 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002208 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002209 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002210
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002211 /* When deleting the current window of another tab page select a new
2212 * current window. */
2213 if (tp != NULL && win == tp->tp_curwin)
2214 tp->tp_curwin = wp;
2215
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002216 return wp;
2217}
2218
2219#if defined(EXITFREE) || defined(PROTO)
2220 void
2221win_free_all()
2222{
2223 int dummy;
2224
Bram Moolenaarf740b292006-02-16 22:11:02 +00002225# ifdef FEAT_WINDOWS
2226 while (first_tabpage->tp_next != NULL)
2227 tabpage_close(TRUE);
2228# endif
2229
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002230 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002231 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002232}
2233#endif
2234
2235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 * Remove a window and its frame from the tree of frames.
2237 * Returns a pointer to the window that got the freed up space.
2238 */
2239/*ARGSUSED*/
2240 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002241winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 win_T *win;
2243 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002244 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
2246 frame_T *frp, *frp2, *frp3;
2247 frame_T *frp_close = win->w_frame;
2248 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002249 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002252 * If there is only one window there is nothing to remove.
2253 */
2254 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2255 return NULL;
2256
2257 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Remove the window from its frame.
2259 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002260 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 wp = frame2win(frp2);
2262
2263 /* Remove this frame from the list of frames. */
2264 frame_remove(frp_close);
2265
2266#ifdef FEAT_VERTSPLIT
2267 if (frp_close->fr_parent->fr_layout == FR_COL)
2268 {
2269#endif
2270 /* When 'winfixheight' is set, remember its old size and restore
2271 * it later (it's a simplistic solution...). Don't do this if the
2272 * window will occupy the full height of the screen. */
2273 if (frp2->fr_win != NULL
2274 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2275 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002276 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2278 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002279 if (old_size != 0)
2280 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#ifdef FEAT_VERTSPLIT
2282 *dirp = 'v';
2283 }
2284 else
2285 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002286 /* When 'winfixwidth' is set, remember its old size and restore
2287 * it later (it's a simplistic solution...). Don't do this if the
2288 * window will occupy the full width of the screen. */
2289 if (frp2->fr_win != NULL
2290 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2291 && frp2->fr_win->w_p_wfw)
2292 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002294 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2295 if (old_size != 0)
2296 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 *dirp = 'h';
2298 }
2299#endif
2300
2301 /* If rows/columns go to a window below/right its positions need to be
2302 * updated. Can only be done after the sizes have been updated. */
2303 if (frp2 == frp_close->fr_next)
2304 {
2305 int row = win->w_winrow;
2306 int col = W_WINCOL(win);
2307
2308 frame_comp_pos(frp2, &row, &col);
2309 }
2310
2311 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2312 {
2313 /* There is no other frame in this list, move its info to the parent
2314 * and remove it. */
2315 frp2->fr_parent->fr_layout = frp2->fr_layout;
2316 frp2->fr_parent->fr_child = frp2->fr_child;
2317 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2318 frp->fr_parent = frp2->fr_parent;
2319 frp2->fr_parent->fr_win = frp2->fr_win;
2320 if (frp2->fr_win != NULL)
2321 frp2->fr_win->w_frame = frp2->fr_parent;
2322 frp = frp2->fr_parent;
2323 vim_free(frp2);
2324
2325 frp2 = frp->fr_parent;
2326 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2327 {
2328 /* The frame above the parent has the same layout, have to merge
2329 * the frames into this list. */
2330 if (frp2->fr_child == frp)
2331 frp2->fr_child = frp->fr_child;
2332 frp->fr_child->fr_prev = frp->fr_prev;
2333 if (frp->fr_prev != NULL)
2334 frp->fr_prev->fr_next = frp->fr_child;
2335 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2336 {
2337 frp3->fr_parent = frp2;
2338 if (frp3->fr_next == NULL)
2339 {
2340 frp3->fr_next = frp->fr_next;
2341 if (frp->fr_next != NULL)
2342 frp->fr_next->fr_prev = frp3;
2343 break;
2344 }
2345 }
2346 vim_free(frp);
2347 }
2348 }
2349
2350 return wp;
2351}
2352
2353/*
2354 * Find out which frame is going to get the freed up space when "win" is
2355 * closed.
2356 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2357 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2358 * This makes opening a window and closing it immediately keep the same window
2359 * layout.
2360 */
2361 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002362win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002364 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366 frame_T *frp;
2367 int b;
2368
Bram Moolenaarf740b292006-02-16 22:11:02 +00002369 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002370 /* Last window in this tab page, will go to next tab page. */
2371 return alt_tabpage()->tp_curwin->w_frame;
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 frp = win->w_frame;
2374#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002375 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 b = p_spr;
2377 else
2378#endif
2379 b = p_sb;
2380 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2381 return frp->fr_next;
2382 return frp->fr_prev;
2383}
2384
2385/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002386 * Return the tabpage that will be used if the current one is closed.
2387 */
2388 static tabpage_T *
2389alt_tabpage()
2390{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002391 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002392
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002393 /* Use the next tab page if possible. */
2394 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002395 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002396
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002397 /* Find the last but one tab page. */
2398 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2399 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002400 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002401}
2402
2403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 * Find the left-upper window in frame "frp".
2405 */
2406 static win_T *
2407frame2win(frp)
2408 frame_T *frp;
2409{
2410 while (frp->fr_win == NULL)
2411 frp = frp->fr_child;
2412 return frp->fr_win;
2413}
2414
2415/*
2416 * Return TRUE if frame "frp" contains window "wp".
2417 */
2418 static int
2419frame_has_win(frp, wp)
2420 frame_T *frp;
2421 win_T *wp;
2422{
2423 frame_T *p;
2424
2425 if (frp->fr_layout == FR_LEAF)
2426 return frp->fr_win == wp;
2427
2428 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2429 if (frame_has_win(p, wp))
2430 return TRUE;
2431 return FALSE;
2432}
2433
2434/*
2435 * Set a new height for a frame. Recursively sets the height for contained
2436 * frames and windows. Caller must take care of positions.
2437 */
2438 static void
2439frame_new_height(topfrp, height, topfirst, wfh)
2440 frame_T *topfrp;
2441 int height;
2442 int topfirst; /* resize topmost contained frame first */
2443 int wfh; /* obey 'winfixheight' when there is a choice;
2444 may cause the height not to be set */
2445{
2446 frame_T *frp;
2447 int extra_lines;
2448 int h;
2449
2450 if (topfrp->fr_win != NULL)
2451 {
2452 /* Simple case: just one window. */
2453 win_new_height(topfrp->fr_win,
2454 height - topfrp->fr_win->w_status_height);
2455 }
2456#ifdef FEAT_VERTSPLIT
2457 else if (topfrp->fr_layout == FR_ROW)
2458 {
2459 do
2460 {
2461 /* All frames in this row get the same new height. */
2462 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2463 {
2464 frame_new_height(frp, height, topfirst, wfh);
2465 if (frp->fr_height > height)
2466 {
2467 /* Could not fit the windows, make the whole row higher. */
2468 height = frp->fr_height;
2469 break;
2470 }
2471 }
2472 }
2473 while (frp != NULL);
2474 }
2475#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002476 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 /* Complicated case: Resize a column of frames. Resize the bottom
2479 * frame first, frames above that when needed. */
2480
2481 frp = topfrp->fr_child;
2482 if (wfh)
2483 /* Advance past frames with one window with 'wfh' set. */
2484 while (frame_fixed_height(frp))
2485 {
2486 frp = frp->fr_next;
2487 if (frp == NULL)
2488 return; /* no frame without 'wfh', give up */
2489 }
2490 if (!topfirst)
2491 {
2492 /* Find the bottom frame of this column */
2493 while (frp->fr_next != NULL)
2494 frp = frp->fr_next;
2495 if (wfh)
2496 /* Advance back for frames with one window with 'wfh' set. */
2497 while (frame_fixed_height(frp))
2498 frp = frp->fr_prev;
2499 }
2500
2501 extra_lines = height - topfrp->fr_height;
2502 if (extra_lines < 0)
2503 {
2504 /* reduce height of contained frames, bottom or top frame first */
2505 while (frp != NULL)
2506 {
2507 h = frame_minheight(frp, NULL);
2508 if (frp->fr_height + extra_lines < h)
2509 {
2510 extra_lines += frp->fr_height - h;
2511 frame_new_height(frp, h, topfirst, wfh);
2512 }
2513 else
2514 {
2515 frame_new_height(frp, frp->fr_height + extra_lines,
2516 topfirst, wfh);
2517 break;
2518 }
2519 if (topfirst)
2520 {
2521 do
2522 frp = frp->fr_next;
2523 while (wfh && frp != NULL && frame_fixed_height(frp));
2524 }
2525 else
2526 {
2527 do
2528 frp = frp->fr_prev;
2529 while (wfh && frp != NULL && frame_fixed_height(frp));
2530 }
2531 /* Increase "height" if we could not reduce enough frames. */
2532 if (frp == NULL)
2533 height -= extra_lines;
2534 }
2535 }
2536 else if (extra_lines > 0)
2537 {
2538 /* increase height of bottom or top frame */
2539 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2540 }
2541 }
2542 topfrp->fr_height = height;
2543}
2544
2545/*
2546 * Return TRUE if height of frame "frp" should not be changed because of
2547 * the 'winfixheight' option.
2548 */
2549 static int
2550frame_fixed_height(frp)
2551 frame_T *frp;
2552{
2553 /* frame with one window: fixed height if 'winfixheight' set. */
2554 if (frp->fr_win != NULL)
2555 return frp->fr_win->w_p_wfh;
2556
2557 if (frp->fr_layout == FR_ROW)
2558 {
2559 /* The frame is fixed height if one of the frames in the row is fixed
2560 * height. */
2561 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2562 if (frame_fixed_height(frp))
2563 return TRUE;
2564 return FALSE;
2565 }
2566
2567 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2568 * frames in the row are fixed height. */
2569 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2570 if (!frame_fixed_height(frp))
2571 return FALSE;
2572 return TRUE;
2573}
2574
2575#ifdef FEAT_VERTSPLIT
2576/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002577 * Return TRUE if width of frame "frp" should not be changed because of
2578 * the 'winfixwidth' option.
2579 */
2580 static int
2581frame_fixed_width(frp)
2582 frame_T *frp;
2583{
2584 /* frame with one window: fixed width if 'winfixwidth' set. */
2585 if (frp->fr_win != NULL)
2586 return frp->fr_win->w_p_wfw;
2587
2588 if (frp->fr_layout == FR_COL)
2589 {
2590 /* The frame is fixed width if one of the frames in the row is fixed
2591 * width. */
2592 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2593 if (frame_fixed_width(frp))
2594 return TRUE;
2595 return FALSE;
2596 }
2597
2598 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2599 * frames in the row are fixed width. */
2600 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2601 if (!frame_fixed_width(frp))
2602 return FALSE;
2603 return TRUE;
2604}
2605
2606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * Add a status line to windows at the bottom of "frp".
2608 * Note: Does not check if there is room!
2609 */
2610 static void
2611frame_add_statusline(frp)
2612 frame_T *frp;
2613{
2614 win_T *wp;
2615
2616 if (frp->fr_layout == FR_LEAF)
2617 {
2618 wp = frp->fr_win;
2619 if (wp->w_status_height == 0)
2620 {
2621 if (wp->w_height > 0) /* don't make it negative */
2622 --wp->w_height;
2623 wp->w_status_height = STATUS_HEIGHT;
2624 }
2625 }
2626 else if (frp->fr_layout == FR_ROW)
2627 {
2628 /* Handle all the frames in the row. */
2629 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2630 frame_add_statusline(frp);
2631 }
2632 else /* frp->fr_layout == FR_COL */
2633 {
2634 /* Only need to handle the last frame in the column. */
2635 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2636 ;
2637 frame_add_statusline(frp);
2638 }
2639}
2640
2641/*
2642 * Set width of a frame. Handles recursively going through contained frames.
2643 * May remove separator line for windows at the right side (for win_close()).
2644 */
2645 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002646frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 frame_T *topfrp;
2648 int width;
2649 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002650 int wfw; /* obey 'winfixwidth' when there is a choice;
2651 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 frame_T *frp;
2654 int extra_cols;
2655 int w;
2656 win_T *wp;
2657
2658 if (topfrp->fr_layout == FR_LEAF)
2659 {
2660 /* Simple case: just one window. */
2661 wp = topfrp->fr_win;
2662 /* Find out if there are any windows right of this one. */
2663 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2664 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2665 break;
2666 if (frp->fr_parent == NULL)
2667 wp->w_vsep_width = 0;
2668 win_new_width(wp, width - wp->w_vsep_width);
2669 }
2670 else if (topfrp->fr_layout == FR_COL)
2671 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002672 do
2673 {
2674 /* All frames in this column get the same new width. */
2675 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2676 {
2677 frame_new_width(frp, width, leftfirst, wfw);
2678 if (frp->fr_width > width)
2679 {
2680 /* Could not fit the windows, make whole column wider. */
2681 width = frp->fr_width;
2682 break;
2683 }
2684 }
2685 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687 else /* fr_layout == FR_ROW */
2688 {
2689 /* Complicated case: Resize a row of frames. Resize the rightmost
2690 * frame first, frames left of it when needed. */
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002693 if (wfw)
2694 /* Advance past frames with one window with 'wfw' set. */
2695 while (frame_fixed_width(frp))
2696 {
2697 frp = frp->fr_next;
2698 if (frp == NULL)
2699 return; /* no frame without 'wfw', give up */
2700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002702 {
2703 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 while (frp->fr_next != NULL)
2705 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 if (wfw)
2707 /* Advance back for frames with one window with 'wfw' set. */
2708 while (frame_fixed_width(frp))
2709 frp = frp->fr_prev;
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
2712 extra_cols = width - topfrp->fr_width;
2713 if (extra_cols < 0)
2714 {
2715 /* reduce frame width, rightmost frame first */
2716 while (frp != NULL)
2717 {
2718 w = frame_minwidth(frp, NULL);
2719 if (frp->fr_width + extra_cols < w)
2720 {
2721 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002722 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724 else
2725 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002726 frame_new_width(frp, frp->fr_width + extra_cols,
2727 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 break;
2729 }
2730 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002731 {
2732 do
2733 frp = frp->fr_next;
2734 while (wfw && frp != NULL && frame_fixed_width(frp));
2735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002737 {
2738 do
2739 frp = frp->fr_prev;
2740 while (wfw && frp != NULL && frame_fixed_width(frp));
2741 }
2742 /* Increase "width" if we could not reduce enough frames. */
2743 if (frp == NULL)
2744 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 }
2747 else if (extra_cols > 0)
2748 {
2749 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002750 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 }
2753 topfrp->fr_width = width;
2754}
2755
2756/*
2757 * Add the vertical separator to windows at the right side of "frp".
2758 * Note: Does not check if there is room!
2759 */
2760 static void
2761frame_add_vsep(frp)
2762 frame_T *frp;
2763{
2764 win_T *wp;
2765
2766 if (frp->fr_layout == FR_LEAF)
2767 {
2768 wp = frp->fr_win;
2769 if (wp->w_vsep_width == 0)
2770 {
2771 if (wp->w_width > 0) /* don't make it negative */
2772 --wp->w_width;
2773 wp->w_vsep_width = 1;
2774 }
2775 }
2776 else if (frp->fr_layout == FR_COL)
2777 {
2778 /* Handle all the frames in the column. */
2779 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2780 frame_add_vsep(frp);
2781 }
2782 else /* frp->fr_layout == FR_ROW */
2783 {
2784 /* Only need to handle the last frame in the row. */
2785 frp = frp->fr_child;
2786 while (frp->fr_next != NULL)
2787 frp = frp->fr_next;
2788 frame_add_vsep(frp);
2789 }
2790}
2791
2792/*
2793 * Set frame width from the window it contains.
2794 */
2795 static void
2796frame_fix_width(wp)
2797 win_T *wp;
2798{
2799 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2800}
2801#endif
2802
2803/*
2804 * Set frame height from the window it contains.
2805 */
2806 static void
2807frame_fix_height(wp)
2808 win_T *wp;
2809{
2810 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2811}
2812
2813/*
2814 * Compute the minimal height for frame "topfrp".
2815 * Uses the 'winminheight' option.
2816 * When "next_curwin" isn't NULL, use p_wh for this window.
2817 * When "next_curwin" is NOWIN, don't use at least one line for the current
2818 * window.
2819 */
2820 static int
2821frame_minheight(topfrp, next_curwin)
2822 frame_T *topfrp;
2823 win_T *next_curwin;
2824{
2825 frame_T *frp;
2826 int m;
2827#ifdef FEAT_VERTSPLIT
2828 int n;
2829#endif
2830
2831 if (topfrp->fr_win != NULL)
2832 {
2833 if (topfrp->fr_win == next_curwin)
2834 m = p_wh + topfrp->fr_win->w_status_height;
2835 else
2836 {
2837 /* window: minimal height of the window plus status line */
2838 m = p_wmh + topfrp->fr_win->w_status_height;
2839 /* Current window is minimal one line high */
2840 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2841 ++m;
2842 }
2843 }
2844#ifdef FEAT_VERTSPLIT
2845 else if (topfrp->fr_layout == FR_ROW)
2846 {
2847 /* get the minimal height from each frame in this row */
2848 m = 0;
2849 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2850 {
2851 n = frame_minheight(frp, next_curwin);
2852 if (n > m)
2853 m = n;
2854 }
2855 }
2856#endif
2857 else
2858 {
2859 /* Add up the minimal heights for all frames in this column. */
2860 m = 0;
2861 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2862 m += frame_minheight(frp, next_curwin);
2863 }
2864
2865 return m;
2866}
2867
2868#ifdef FEAT_VERTSPLIT
2869/*
2870 * Compute the minimal width for frame "topfrp".
2871 * When "next_curwin" isn't NULL, use p_wiw for this window.
2872 * When "next_curwin" is NOWIN, don't use at least one column for the current
2873 * window.
2874 */
2875 static int
2876frame_minwidth(topfrp, next_curwin)
2877 frame_T *topfrp;
2878 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2879{
2880 frame_T *frp;
2881 int m, n;
2882
2883 if (topfrp->fr_win != NULL)
2884 {
2885 if (topfrp->fr_win == next_curwin)
2886 m = p_wiw + topfrp->fr_win->w_vsep_width;
2887 else
2888 {
2889 /* window: minimal width of the window plus separator column */
2890 m = p_wmw + topfrp->fr_win->w_vsep_width;
2891 /* Current window is minimal one column wide */
2892 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2893 ++m;
2894 }
2895 }
2896 else if (topfrp->fr_layout == FR_COL)
2897 {
2898 /* get the minimal width from each frame in this column */
2899 m = 0;
2900 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2901 {
2902 n = frame_minwidth(frp, next_curwin);
2903 if (n > m)
2904 m = n;
2905 }
2906 }
2907 else
2908 {
2909 /* Add up the minimal widths for all frames in this row. */
2910 m = 0;
2911 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2912 m += frame_minwidth(frp, next_curwin);
2913 }
2914
2915 return m;
2916}
2917#endif
2918
2919
2920/*
2921 * Try to close all windows except current one.
2922 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2923 * used and the buffer was modified.
2924 *
2925 * Used by ":bdel" and ":only".
2926 */
2927 void
2928close_others(message, forceit)
2929 int message;
2930 int forceit; /* always hide all other windows */
2931{
2932 win_T *wp;
2933 win_T *nextwp;
2934 int r;
2935
2936 if (lastwin == firstwin)
2937 {
2938 if (message
2939#ifdef FEAT_AUTOCMD
2940 && !autocmd_busy
2941#endif
2942 )
2943 MSG(_("Already only one window"));
2944 return;
2945 }
2946
2947 /* Be very careful here: autocommands may change the window layout. */
2948 for (wp = firstwin; win_valid(wp); wp = nextwp)
2949 {
2950 nextwp = wp->w_next;
2951 if (wp != curwin) /* don't close current window */
2952 {
2953
2954 /* Check if it's allowed to abandon this window */
2955 r = can_abandon(wp->w_buffer, forceit);
2956#ifdef FEAT_AUTOCMD
2957 if (!win_valid(wp)) /* autocommands messed wp up */
2958 {
2959 nextwp = firstwin;
2960 continue;
2961 }
2962#endif
2963 if (!r)
2964 {
2965#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2966 if (message && (p_confirm || cmdmod.confirm) && p_write)
2967 {
2968 dialog_changed(wp->w_buffer, FALSE);
2969# ifdef FEAT_AUTOCMD
2970 if (!win_valid(wp)) /* autocommands messed wp up */
2971 {
2972 nextwp = firstwin;
2973 continue;
2974 }
2975# endif
2976 }
2977 if (bufIsChanged(wp->w_buffer))
2978#endif
2979 continue;
2980 }
2981 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2982 }
2983 }
2984
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002985 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 EMSG(_("E445: Other window contains changes"));
2987}
2988
2989#endif /* FEAT_WINDOWS */
2990
2991/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002992 * Init the current window "curwin".
2993 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
2995 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002996curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002998 redraw_win_later(curwin, NOT_VALID);
2999 curwin->w_lines_valid = 0;
3000 curwin->w_cursor.lnum = 1;
3001 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003003 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003005 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3006 curwin->w_pcmark.col = 0;
3007 curwin->w_prev_pcmark.lnum = 0;
3008 curwin->w_prev_pcmark.col = 0;
3009 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003011 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003013 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#ifdef FEAT_FKMAP
3015 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003016 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003018 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#endif
3020}
3021
3022/*
3023 * Allocate the first window and put an empty buffer in it.
3024 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003027 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028win_alloc_first()
3029{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003030 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003031 return FAIL;
3032
3033#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003034 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003035 if (first_tabpage == NULL)
3036 return FAIL;
3037 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003038 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003039#endif
3040 return OK;
3041}
3042
3043/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003044 * Allocate the first window or the first window in a new tab page.
3045 * When "oldwin" is NULL create an empty buffer for it.
3046 * When "oldwin" is not NULL copy info from it to the new window (only with
3047 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003048 * Return FAIL when something goes wrong (out of memory).
3049 */
3050 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051win_alloc_firstwin(oldwin)
3052 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003053{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003055 if (oldwin == NULL)
3056 {
3057 /* Very first window, need to create an empty buffer for it and
3058 * initialize from scratch. */
3059 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3060 if (curwin == NULL || curbuf == NULL)
3061 return FAIL;
3062 curwin->w_buffer = curbuf;
3063 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003065 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003067 curwin_init(); /* init current window */
3068 }
3069#ifdef FEAT_WINDOWS
3070 else
3071 {
3072 /* First window in new tab page, initialize it from "oldwin". */
3073 win_init(curwin, oldwin);
3074
3075# ifdef FEAT_SCROLLBIND
3076 /* We don't want scroll-binding in the first window. */
3077 curwin->w_p_scb = FALSE;
3078# endif
3079 }
3080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3083 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 topframe->fr_layout = FR_LEAF;
3086#ifdef FEAT_VERTSPLIT
3087 topframe->fr_width = Columns;
3088#endif
3089 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003090#ifdef FEAT_WINDOWS
3091 p_ch_used = p_ch;
3092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 topframe->fr_win = curwin;
3094 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003095
3096 return OK;
3097}
3098
3099/*
3100 * Initialize the window and frame size to the maximum.
3101 */
3102 void
3103win_init_size()
3104{
3105 firstwin->w_height = ROWS_AVAIL;
3106 topframe->fr_height = ROWS_AVAIL;
3107#ifdef FEAT_VERTSPLIT
3108 firstwin->w_width = Columns;
3109 topframe->fr_width = Columns;
3110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111}
3112
3113#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003114
3115/*
3116 * Allocate a new tabpage_T and init the values.
3117 * Returns NULL when out of memory.
3118 */
3119 static tabpage_T *
3120alloc_tabpage()
3121{
3122 tabpage_T *tp;
3123
3124 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3125 if (tp != NULL)
3126 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003127# ifdef FEAT_GUI
3128 int i;
3129
3130 for (i = 0; i < 3; i++)
3131 tp->tp_prev_which_scrollbars[i] = -1;
3132# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003133# ifdef FEAT_DIFF
3134 tp->tp_diff_invalid = TRUE;
3135# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003136#ifdef FEAT_EVAL
3137 /* init t: variables */
3138 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3139#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003140 }
3141 return tp;
3142}
3143
3144 static void
3145free_tabpage(tp)
3146 tabpage_T *tp;
3147{
3148# ifdef FEAT_DIFF
3149 diff_clear(tp);
3150# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003151 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003152#ifdef FEAT_EVAL
3153 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3154#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003155 vim_free(tp);
3156}
3157
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003158/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003159 * Create a new Tab page with one window.
3160 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003161 * When "after" is 0 put it just after the current Tab page.
3162 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003163 * Return FAIL or OK.
3164 */
3165 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003166win_new_tabpage(after)
3167 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003168{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003169 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003170 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003171 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003172
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003173 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003174 if (newtp == NULL)
3175 return FAIL;
3176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003177 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003178 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003179 {
3180 vim_free(newtp);
3181 return FAIL;
3182 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003183 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184
3185 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003186 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003187 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003188 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003189 if (after == 1)
3190 {
3191 /* New tab page becomes the first one. */
3192 newtp->tp_next = first_tabpage;
3193 first_tabpage = newtp;
3194 }
3195 else
3196 {
3197 if (after > 0)
3198 {
3199 /* Put new tab page before tab page "after". */
3200 n = 2;
3201 for (tp = first_tabpage; tp->tp_next != NULL
3202 && n < after; tp = tp->tp_next)
3203 ++n;
3204 }
3205 newtp->tp_next = tp->tp_next;
3206 tp->tp_next = newtp;
3207 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003208 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003209 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003210 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003211
3212 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003213 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003214
3215#if defined(FEAT_GUI)
3216 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3217 * scrollbars. Have to update them anyway. */
3218 if (gui.in_use && starting == 0)
3219 {
3220 gui_init_which_components(NULL);
3221 gui_update_scrollbars(TRUE);
3222 }
3223 need_mouse_correct = TRUE;
3224#endif
3225
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003226 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003227#ifdef FEAT_AUTOCMD
3228 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3229 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3230#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003231 return OK;
3232 }
3233
3234 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003235 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003236 return FAIL;
3237}
3238
3239/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003240 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3241 * like with ":split".
3242 * Returns OK if a new tab page was created, FAIL otherwise.
3243 */
3244 int
3245may_open_tabpage()
3246{
3247 int n = cmdmod.tab;
3248
3249 if (cmdmod.tab != 0)
3250 {
3251 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3252 return win_new_tabpage(n);
3253 }
3254 return FAIL;
3255}
3256
3257/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003258 * Create up to "maxcount" tabpages with empty windows.
3259 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003260 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003261 int
3262make_tabpages(maxcount)
3263 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003265 int count = maxcount;
3266 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003267
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003268 /* Limit to 'tabpagemax' tabs. */
3269 if (count > p_tpm)
3270 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003271
3272#ifdef FEAT_AUTOCMD
3273 /*
3274 * Don't execute autocommands while creating the tab pages. Must do that
3275 * when putting the buffers in the windows.
3276 */
3277 ++autocmd_block;
3278#endif
3279
3280 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003281 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003282 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003283
3284#ifdef FEAT_AUTOCMD
3285 --autocmd_block;
3286#endif
3287
3288 /* return actual number of tab pages */
3289 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003290}
3291
3292/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003293 * Return TRUE when "tpc" points to a valid tab page.
3294 */
3295 int
3296valid_tabpage(tpc)
3297 tabpage_T *tpc;
3298{
3299 tabpage_T *tp;
3300
3301 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3302 if (tp == tpc)
3303 return TRUE;
3304 return FALSE;
3305}
3306
3307/*
3308 * Find tab page "n" (first one is 1). Returns NULL when not found.
3309 */
3310 tabpage_T *
3311find_tabpage(n)
3312 int n;
3313{
3314 tabpage_T *tp;
3315 int i = 1;
3316
3317 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3318 ++i;
3319 return tp;
3320}
3321
3322/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003323 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003324 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003325 */
3326 int
3327tabpage_index(ftp)
3328 tabpage_T *ftp;
3329{
3330 int i = 1;
3331 tabpage_T *tp;
3332
3333 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3334 ++i;
3335 return i;
3336}
3337
3338/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003339 * Prepare for leaving the current tab page.
3340 * When autocomands change "curtab" we don't leave the tab page and return
3341 * FAIL.
3342 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003344/*ARGSUSED*/
3345 static int
3346leave_tabpage(new_curbuf)
3347 buf_T *new_curbuf; /* what is going to be the new curbuf,
3348 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003350 tabpage_T *tp = curtab;
3351
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003352#ifdef FEAT_VISUAL
3353 reset_VIsual_and_resel(); /* stop Visual mode */
3354#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003355#ifdef FEAT_AUTOCMD
3356 if (new_curbuf != curbuf)
3357 {
3358 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3359 if (curtab != tp)
3360 return FAIL;
3361 }
3362 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3363 if (curtab != tp)
3364 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003365 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003366 if (curtab != tp)
3367 return FAIL;
3368#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003369#if defined(FEAT_GUI)
3370 /* Remove the scrollbars. They may be added back later. */
3371 if (gui.in_use)
3372 gui_remove_scrollbars();
3373#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003374 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003375 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003376 tp->tp_firstwin = firstwin;
3377 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003378 tp->tp_old_Rows = Rows;
3379 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380 firstwin = NULL;
3381 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383}
3384
3385/*
3386 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003387 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003388 */
3389/*ARGSUSED*/
3390 static void
3391enter_tabpage(tp, old_curbuf)
3392 tabpage_T *tp;
3393 buf_T *old_curbuf;
3394{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003395 int old_off = tp->tp_firstwin->w_winrow;
3396
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003397 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003398 firstwin = tp->tp_firstwin;
3399 lastwin = tp->tp_lastwin;
3400 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401#ifdef FEAT_AUTOCMD
3402 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3403#endif
3404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003405 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003406 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003407
3408#ifdef FEAT_AUTOCMD
3409 if (old_curbuf != curbuf)
3410 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3411#endif
3412
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003413 last_status(FALSE); /* status line may appear or disappear */
3414 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415 must_redraw = CLEAR; /* need to redraw everything */
3416#ifdef FEAT_DIFF
3417 diff_need_scrollbind = TRUE;
3418#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003420 /* The tabpage line may have appeared or disappeared, may need to resize
3421 * the frames for that. When the Vim window was resized need to update
3422 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003423 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3424#ifdef FEAT_GUI_TABLINE
3425 && !gui_use_tabline()
3426#endif
3427 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003428 shell_new_rows();
3429#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003430 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003431 shell_new_columns(); /* update window widths */
3432#endif
3433
3434#if defined(FEAT_GUI)
3435 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3436 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003437 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003438 {
3439 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003440 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003441 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003442 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003443#endif
3444
3445 redraw_all_later(CLEAR);
3446}
3447
3448/*
3449 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003450 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451 */
3452 void
3453goto_tabpage(n)
3454 int n;
3455{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003456 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003457 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003458 int i;
3459
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003460 /* If there is only one it can't work. */
3461 if (first_tabpage->tp_next == NULL)
3462 {
3463 if (n > 1)
3464 beep_flush();
3465 return;
3466 }
3467
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003468 if (n == 0)
3469 {
3470 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003471 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472 tp = first_tabpage;
3473 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003474 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003476 else if (n < 0)
3477 {
3478 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3479 * this N times. */
3480 ttp = curtab;
3481 for (i = n; i < 0; ++i)
3482 {
3483 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3484 tp = tp->tp_next)
3485 ;
3486 ttp = tp;
3487 }
3488 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003489 else if (n == 9999)
3490 {
3491 /* Go to last tab page. */
3492 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3493 ;
3494 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 else
3496 {
3497 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003498 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003499 if (tp == NULL)
3500 {
3501 beep_flush();
3502 return;
3503 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003504 }
3505
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003506 goto_tabpage_tp(tp);
3507
3508#ifdef FEAT_GUI_TABLINE
3509 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003510 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003511#endif
3512}
3513
3514/*
3515 * Go to tabpage "tp".
3516 * Note: doesn't update the GUI tab.
3517 */
3518 void
3519goto_tabpage_tp(tp)
3520 tabpage_T *tp;
3521{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003522 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003523 {
3524 if (valid_tabpage(tp))
3525 enter_tabpage(tp, curbuf);
3526 else
3527 enter_tabpage(curtab, curbuf);
3528 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003532 * Move the current tab page to before tab page "nr".
3533 */
3534 void
3535tabpage_move(nr)
3536 int nr;
3537{
3538 int n = nr;
3539 tabpage_T *tp;
3540
3541 if (first_tabpage->tp_next == NULL)
3542 return;
3543
3544 /* Remove the current tab page from the list of tab pages. */
3545 if (curtab == first_tabpage)
3546 first_tabpage = curtab->tp_next;
3547 else
3548 {
3549 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3550 if (tp->tp_next == curtab)
3551 break;
3552 if (tp == NULL) /* "cannot happen" */
3553 return;
3554 tp->tp_next = curtab->tp_next;
3555 }
3556
3557 /* Re-insert it at the specified position. */
3558 if (n == 0)
3559 {
3560 curtab->tp_next = first_tabpage;
3561 first_tabpage = curtab;
3562 }
3563 else
3564 {
3565 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3566 --n;
3567 curtab->tp_next = tp->tp_next;
3568 tp->tp_next = curtab;
3569 }
3570
3571 /* Need to redraw the tabline. Tab page contents doesn't change. */
3572 redraw_tabline = TRUE;
3573}
3574
3575
3576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 * Go to another window.
3578 * When jumping to another buffer, stop Visual mode. Do this before
3579 * changing windows so we can yank the selection into the '*' register.
3580 * When jumping to another window on the same buffer, adjust its cursor
3581 * position to keep the same Visual area.
3582 */
3583 void
3584win_goto(wp)
3585 win_T *wp;
3586{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003587 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003590 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 return;
3592 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003593#ifdef FEAT_AUTOCMD
3594 if (curbuf_locked())
3595 return;
3596#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598#ifdef FEAT_VISUAL
3599 if (wp->w_buffer != curbuf)
3600 reset_VIsual_and_resel();
3601 else if (VIsual_active)
3602 wp->w_cursor = curwin->w_cursor;
3603#endif
3604
3605#ifdef FEAT_GUI
3606 need_mouse_correct = TRUE;
3607#endif
3608 win_enter(wp, TRUE);
3609}
3610
3611#if defined(FEAT_PERL) || defined(PROTO)
3612/*
3613 * Find window number "winnr" (counting top to bottom).
3614 */
3615 win_T *
3616win_find_nr(winnr)
3617 int winnr;
3618{
3619 win_T *wp;
3620
3621# ifdef FEAT_WINDOWS
3622 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3623 if (--winnr == 0)
3624 break;
3625 return wp;
3626# else
3627 return curwin;
3628# endif
3629}
3630#endif
3631
3632#ifdef FEAT_VERTSPLIT
3633/*
3634 * Move to window above or below "count" times.
3635 */
3636 static void
3637win_goto_ver(up, count)
3638 int up; /* TRUE to go to win above */
3639 long count;
3640{
3641 frame_T *fr;
3642 frame_T *nfr;
3643 frame_T *foundfr;
3644
3645 foundfr = curwin->w_frame;
3646 while (count--)
3647 {
3648 /*
3649 * First go upwards in the tree of frames until we find a upwards or
3650 * downwards neighbor.
3651 */
3652 fr = foundfr;
3653 for (;;)
3654 {
3655 if (fr == topframe)
3656 goto end;
3657 if (up)
3658 nfr = fr->fr_prev;
3659 else
3660 nfr = fr->fr_next;
3661 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3662 break;
3663 fr = fr->fr_parent;
3664 }
3665
3666 /*
3667 * Now go downwards to find the bottom or top frame in it.
3668 */
3669 for (;;)
3670 {
3671 if (nfr->fr_layout == FR_LEAF)
3672 {
3673 foundfr = nfr;
3674 break;
3675 }
3676 fr = nfr->fr_child;
3677 if (nfr->fr_layout == FR_ROW)
3678 {
3679 /* Find the frame at the cursor row. */
3680 while (fr->fr_next != NULL
3681 && frame2win(fr)->w_wincol + fr->fr_width
3682 <= curwin->w_wincol + curwin->w_wcol)
3683 fr = fr->fr_next;
3684 }
3685 if (nfr->fr_layout == FR_COL && up)
3686 while (fr->fr_next != NULL)
3687 fr = fr->fr_next;
3688 nfr = fr;
3689 }
3690 }
3691end:
3692 if (foundfr != NULL)
3693 win_goto(foundfr->fr_win);
3694}
3695
3696/*
3697 * Move to left or right window.
3698 */
3699 static void
3700win_goto_hor(left, count)
3701 int left; /* TRUE to go to left win */
3702 long count;
3703{
3704 frame_T *fr;
3705 frame_T *nfr;
3706 frame_T *foundfr;
3707
3708 foundfr = curwin->w_frame;
3709 while (count--)
3710 {
3711 /*
3712 * First go upwards in the tree of frames until we find a left or
3713 * right neighbor.
3714 */
3715 fr = foundfr;
3716 for (;;)
3717 {
3718 if (fr == topframe)
3719 goto end;
3720 if (left)
3721 nfr = fr->fr_prev;
3722 else
3723 nfr = fr->fr_next;
3724 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3725 break;
3726 fr = fr->fr_parent;
3727 }
3728
3729 /*
3730 * Now go downwards to find the leftmost or rightmost frame in it.
3731 */
3732 for (;;)
3733 {
3734 if (nfr->fr_layout == FR_LEAF)
3735 {
3736 foundfr = nfr;
3737 break;
3738 }
3739 fr = nfr->fr_child;
3740 if (nfr->fr_layout == FR_COL)
3741 {
3742 /* Find the frame at the cursor row. */
3743 while (fr->fr_next != NULL
3744 && frame2win(fr)->w_winrow + fr->fr_height
3745 <= curwin->w_winrow + curwin->w_wrow)
3746 fr = fr->fr_next;
3747 }
3748 if (nfr->fr_layout == FR_ROW && left)
3749 while (fr->fr_next != NULL)
3750 fr = fr->fr_next;
3751 nfr = fr;
3752 }
3753 }
3754end:
3755 if (foundfr != NULL)
3756 win_goto(foundfr->fr_win);
3757}
3758#endif
3759
3760/*
3761 * Make window "wp" the current window.
3762 */
3763 void
3764win_enter(wp, undo_sync)
3765 win_T *wp;
3766 int undo_sync;
3767{
3768 win_enter_ext(wp, undo_sync, FALSE);
3769}
3770
3771/*
3772 * Make window wp the current window.
3773 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3774 * been closed and isn't valid.
3775 */
3776 static void
3777win_enter_ext(wp, undo_sync, curwin_invalid)
3778 win_T *wp;
3779 int undo_sync;
3780 int curwin_invalid;
3781{
3782#ifdef FEAT_AUTOCMD
3783 int other_buffer = FALSE;
3784#endif
3785
3786 if (wp == curwin && !curwin_invalid) /* nothing to do */
3787 return;
3788
3789#ifdef FEAT_AUTOCMD
3790 if (!curwin_invalid)
3791 {
3792 /*
3793 * Be careful: If autocommands delete the window, return now.
3794 */
3795 if (wp->w_buffer != curbuf)
3796 {
3797 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3798 other_buffer = TRUE;
3799 if (!win_valid(wp))
3800 return;
3801 }
3802 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3803 if (!win_valid(wp))
3804 return;
3805# ifdef FEAT_EVAL
3806 /* autocmds may abort script processing */
3807 if (aborting())
3808 return;
3809# endif
3810 }
3811#endif
3812
3813 /* sync undo before leaving the current buffer */
3814 if (undo_sync && curbuf != wp->w_buffer)
3815 u_sync();
3816 /* may have to copy the buffer options when 'cpo' contains 'S' */
3817 if (wp->w_buffer != curbuf)
3818 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3819 if (!curwin_invalid)
3820 {
3821 prevwin = curwin; /* remember for CTRL-W p */
3822 curwin->w_redr_status = TRUE;
3823 }
3824 curwin = wp;
3825 curbuf = wp->w_buffer;
3826 check_cursor();
3827#ifdef FEAT_VIRTUALEDIT
3828 if (!virtual_active())
3829 curwin->w_cursor.coladd = 0;
3830#endif
3831 changed_line_abv_curs(); /* assume cursor position needs updating */
3832
3833 if (curwin->w_localdir != NULL)
3834 {
3835 /* Window has a local directory: Save current directory as global
3836 * directory (unless that was done already) and change to the local
3837 * directory. */
3838 if (globaldir == NULL)
3839 {
3840 char_u cwd[MAXPATHL];
3841
3842 if (mch_dirname(cwd, MAXPATHL) == OK)
3843 globaldir = vim_strsave(cwd);
3844 }
3845 mch_chdir((char *)curwin->w_localdir);
3846 shorten_fnames(TRUE);
3847 }
3848 else if (globaldir != NULL)
3849 {
3850 /* Window doesn't have a local directory and we are not in the global
3851 * directory: Change to the global directory. */
3852 mch_chdir((char *)globaldir);
3853 vim_free(globaldir);
3854 globaldir = NULL;
3855 shorten_fnames(TRUE);
3856 }
3857
3858#ifdef FEAT_AUTOCMD
3859 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3860 if (other_buffer)
3861 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3862#endif
3863
3864#ifdef FEAT_TITLE
3865 maketitle();
3866#endif
3867 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003868 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (restart_edit)
3870 redraw_later(VALID); /* causes status line redraw */
3871
3872 /* set window height to desired minimal value */
3873 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3874 win_setheight((int)p_wh);
3875 else if (curwin->w_height == 0)
3876 win_setheight(1);
3877
3878#ifdef FEAT_VERTSPLIT
3879 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003880 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 win_setwidth((int)p_wiw);
3882#endif
3883
3884#ifdef FEAT_MOUSE
3885 setmouse(); /* in case jumped to/from help buffer */
3886#endif
3887
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003888#ifdef FEAT_AUTOCHDIR
3889 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * switching windows. */
3891 if (p_acd && curbuf->b_ffname != NULL
3892 && vim_chdirfile(curbuf->b_ffname) == OK)
3893 shorten_fnames(TRUE);
3894#endif
3895}
3896
3897#endif /* FEAT_WINDOWS */
3898
3899#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3900/*
3901 * Jump to the first open window that contains buffer buf if one exists
3902 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3903 * Returns pointer to window if it exists, otherwise NULL.
3904 */
3905 win_T *
3906buf_jump_open_win(buf)
3907 buf_T *buf;
3908{
3909# ifdef FEAT_WINDOWS
3910 win_T *wp;
3911
3912 for (wp = firstwin; wp; wp = wp->w_next)
3913 if (wp->w_buffer == buf)
3914 break;
3915 if (wp != NULL)
3916 win_enter(wp, FALSE);
3917 return wp;
3918# else
3919 if (curwin->w_buffer == buf)
3920 return curwin;
3921 return NULL;
3922# endif
3923}
3924#endif
3925
3926/*
3927 * allocate a window structure and link it in the window list
3928 */
3929/*ARGSUSED*/
3930 static win_T *
3931win_alloc(after)
3932 win_T *after;
3933{
3934 win_T *newwin;
3935
3936 /*
3937 * allocate window structure and linesizes arrays
3938 */
3939 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3940 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3941 {
3942 vim_free(newwin);
3943 newwin = NULL;
3944 }
3945
3946 if (newwin != NULL)
3947 {
3948 /*
3949 * link the window in the window list
3950 */
3951#ifdef FEAT_WINDOWS
3952 win_append(after, newwin);
3953#endif
3954#ifdef FEAT_VERTSPLIT
3955 newwin->w_wincol = 0;
3956 newwin->w_width = Columns;
3957#endif
3958
3959 /* position the display and the cursor at the top of the file. */
3960 newwin->w_topline = 1;
3961#ifdef FEAT_DIFF
3962 newwin->w_topfill = 0;
3963#endif
3964 newwin->w_botline = 2;
3965 newwin->w_cursor.lnum = 1;
3966#ifdef FEAT_SCROLLBIND
3967 newwin->w_scbind_pos = 1;
3968#endif
3969
3970 /* We won't calculate w_fraction until resizing the window */
3971 newwin->w_fraction = 0;
3972 newwin->w_prev_fraction_row = -1;
3973
3974#ifdef FEAT_GUI
3975 if (gui.in_use)
3976 {
3977 out_flush();
3978 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3979 SBAR_LEFT, newwin);
3980 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3981 SBAR_RIGHT, newwin);
3982 }
3983#endif
3984#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003985 /* init w: variables */
3986 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987#endif
3988#ifdef FEAT_FOLDING
3989 foldInitWin(newwin);
3990#endif
3991 }
3992 return newwin;
3993}
3994
3995#if defined(FEAT_WINDOWS) || defined(PROTO)
3996
3997/*
3998 * remove window 'wp' from the window list and free the structure
3999 */
4000 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004001win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004003 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 int i;
4006
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004007#ifdef FEAT_MZSCHEME
4008 mzscheme_window_free(wp);
4009#endif
4010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#ifdef FEAT_PERL
4012 perl_win_free(wp);
4013#endif
4014
4015#ifdef FEAT_PYTHON
4016 python_window_free(wp);
4017#endif
4018
4019#ifdef FEAT_TCL
4020 tcl_window_free(wp);
4021#endif
4022
4023#ifdef FEAT_RUBY
4024 ruby_window_free(wp);
4025#endif
4026
4027 clear_winopt(&wp->w_onebuf_opt);
4028 clear_winopt(&wp->w_allbuf_opt);
4029
4030#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004031 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032#endif
4033
4034 if (prevwin == wp)
4035 prevwin = NULL;
4036 win_free_lsize(wp);
4037
4038 for (i = 0; i < wp->w_tagstacklen; ++i)
4039 vim_free(wp->w_tagstack[i].tagname);
4040
4041 vim_free(wp->w_localdir);
4042#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004043 vim_free(wp->w_match[0].regprog);
4044 vim_free(wp->w_match[1].regprog);
4045 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046#endif
4047#ifdef FEAT_JUMPLIST
4048 free_jumplist(wp);
4049#endif
4050
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004051#ifdef FEAT_QUICKFIX
4052 qf_free_all(wp);
4053#endif
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055#ifdef FEAT_GUI
4056 if (gui.in_use)
4057 {
4058 out_flush();
4059 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4060 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4061 }
4062#endif /* FEAT_GUI */
4063
Bram Moolenaarf740b292006-02-16 22:11:02 +00004064 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 vim_free(wp);
4066}
4067
4068/*
4069 * Append window "wp" in the window list after window "after".
4070 */
4071 static void
4072win_append(after, wp)
4073 win_T *after, *wp;
4074{
4075 win_T *before;
4076
4077 if (after == NULL) /* after NULL is in front of the first */
4078 before = firstwin;
4079 else
4080 before = after->w_next;
4081
4082 wp->w_next = before;
4083 wp->w_prev = after;
4084 if (after == NULL)
4085 firstwin = wp;
4086 else
4087 after->w_next = wp;
4088 if (before == NULL)
4089 lastwin = wp;
4090 else
4091 before->w_prev = wp;
4092}
4093
4094/*
4095 * Remove a window from the window list.
4096 */
4097 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004098win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004100 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 if (wp->w_prev != NULL)
4103 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004104 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004106 else
4107 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (wp->w_next != NULL)
4109 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004110 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004112 else
4113 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116/*
4117 * Append frame "frp" in a frame list after frame "after".
4118 */
4119 static void
4120frame_append(after, frp)
4121 frame_T *after, *frp;
4122{
4123 frp->fr_next = after->fr_next;
4124 after->fr_next = frp;
4125 if (frp->fr_next != NULL)
4126 frp->fr_next->fr_prev = frp;
4127 frp->fr_prev = after;
4128}
4129
4130/*
4131 * Insert frame "frp" in a frame list before frame "before".
4132 */
4133 static void
4134frame_insert(before, frp)
4135 frame_T *before, *frp;
4136{
4137 frp->fr_next = before;
4138 frp->fr_prev = before->fr_prev;
4139 before->fr_prev = frp;
4140 if (frp->fr_prev != NULL)
4141 frp->fr_prev->fr_next = frp;
4142 else
4143 frp->fr_parent->fr_child = frp;
4144}
4145
4146/*
4147 * Remove a frame from a frame list.
4148 */
4149 static void
4150frame_remove(frp)
4151 frame_T *frp;
4152{
4153 if (frp->fr_prev != NULL)
4154 frp->fr_prev->fr_next = frp->fr_next;
4155 else
4156 frp->fr_parent->fr_child = frp->fr_next;
4157 if (frp->fr_next != NULL)
4158 frp->fr_next->fr_prev = frp->fr_prev;
4159}
4160
4161#endif /* FEAT_WINDOWS */
4162
4163/*
4164 * Allocate w_lines[] for window "wp".
4165 * Return FAIL for failure, OK for success.
4166 */
4167 int
4168win_alloc_lines(wp)
4169 win_T *wp;
4170{
4171 wp->w_lines_valid = 0;
4172 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4173 if (wp->w_lines == NULL)
4174 return FAIL;
4175 return OK;
4176}
4177
4178/*
4179 * free lsize arrays for a window
4180 */
4181 void
4182win_free_lsize(wp)
4183 win_T *wp;
4184{
4185 vim_free(wp->w_lines);
4186 wp->w_lines = NULL;
4187}
4188
4189/*
4190 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004191 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
4193 void
4194shell_new_rows()
4195{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004196 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 if (firstwin == NULL) /* not initialized yet */
4199 return;
4200#ifdef FEAT_WINDOWS
4201 if (h < frame_minheight(topframe, NULL))
4202 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004203
4204 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * that doesn't result in the right height, forget about that option. */
4206 frame_new_height(topframe, h, FALSE, TRUE);
4207 if (topframe->fr_height != h)
4208 frame_new_height(topframe, h, FALSE, FALSE);
4209
4210 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4211#else
4212 if (h < 1)
4213 h = 1;
4214 win_new_height(firstwin, h);
4215#endif
4216 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004217#ifdef FEAT_WINDOWS
4218 p_ch_used = p_ch;
4219#endif
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#if 0
4222 /* Disabled: don't want making the screen smaller make a window larger. */
4223 if (p_ea)
4224 win_equal(curwin, FALSE, 'v');
4225#endif
4226}
4227
4228#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4229/*
4230 * Called from win_new_shellsize() after Columns changed.
4231 */
4232 void
4233shell_new_columns()
4234{
4235 if (firstwin == NULL) /* not initialized yet */
4236 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004237
4238 /* First try setting the widths of windows with 'winfixwidth'. If that
4239 * doesn't result in the right width, forget about that option. */
4240 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4241 if (topframe->fr_width != Columns)
4242 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4245#if 0
4246 /* Disabled: don't want making the screen smaller make a window larger. */
4247 if (p_ea)
4248 win_equal(curwin, FALSE, 'h');
4249#endif
4250}
4251#endif
4252
4253#if defined(FEAT_CMDWIN) || defined(PROTO)
4254/*
4255 * Save the size of all windows in "gap".
4256 */
4257 void
4258win_size_save(gap)
4259 garray_T *gap;
4260
4261{
4262 win_T *wp;
4263
4264 ga_init2(gap, (int)sizeof(int), 1);
4265 if (ga_grow(gap, win_count() * 2) == OK)
4266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4267 {
4268 ((int *)gap->ga_data)[gap->ga_len++] =
4269 wp->w_width + wp->w_vsep_width;
4270 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4271 }
4272}
4273
4274/*
4275 * Restore window sizes, but only if the number of windows is still the same.
4276 * Does not free the growarray.
4277 */
4278 void
4279win_size_restore(gap)
4280 garray_T *gap;
4281{
4282 win_T *wp;
4283 int i;
4284
4285 if (win_count() * 2 == gap->ga_len)
4286 {
4287 i = 0;
4288 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4289 {
4290 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4291 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4292 }
4293 /* recompute the window positions */
4294 (void)win_comp_pos();
4295 }
4296}
4297#endif /* FEAT_CMDWIN */
4298
4299#if defined(FEAT_WINDOWS) || defined(PROTO)
4300/*
4301 * Update the position for all windows, using the width and height of the
4302 * frames.
4303 * Returns the row just after the last window.
4304 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004305 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306win_comp_pos()
4307{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004308 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 int col = 0;
4310
4311 frame_comp_pos(topframe, &row, &col);
4312 return row;
4313}
4314
4315/*
4316 * Update the position of the windows in frame "topfrp", using the width and
4317 * height of the frames.
4318 * "*row" and "*col" are the top-left position of the frame. They are updated
4319 * to the bottom-right position plus one.
4320 */
4321 static void
4322frame_comp_pos(topfrp, row, col)
4323 frame_T *topfrp;
4324 int *row;
4325 int *col;
4326{
4327 win_T *wp;
4328 frame_T *frp;
4329#ifdef FEAT_VERTSPLIT
4330 int startcol;
4331 int startrow;
4332#endif
4333
4334 wp = topfrp->fr_win;
4335 if (wp != NULL)
4336 {
4337 if (wp->w_winrow != *row
4338#ifdef FEAT_VERTSPLIT
4339 || wp->w_wincol != *col
4340#endif
4341 )
4342 {
4343 /* position changed, redraw */
4344 wp->w_winrow = *row;
4345#ifdef FEAT_VERTSPLIT
4346 wp->w_wincol = *col;
4347#endif
4348 redraw_win_later(wp, NOT_VALID);
4349 wp->w_redr_status = TRUE;
4350 }
4351 *row += wp->w_height + wp->w_status_height;
4352#ifdef FEAT_VERTSPLIT
4353 *col += wp->w_width + wp->w_vsep_width;
4354#endif
4355 }
4356 else
4357 {
4358#ifdef FEAT_VERTSPLIT
4359 startrow = *row;
4360 startcol = *col;
4361#endif
4362 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4363 {
4364#ifdef FEAT_VERTSPLIT
4365 if (topfrp->fr_layout == FR_ROW)
4366 *row = startrow; /* all frames are at the same row */
4367 else
4368 *col = startcol; /* all frames are at the same col */
4369#endif
4370 frame_comp_pos(frp, row, col);
4371 }
4372 }
4373}
4374
4375#endif /* FEAT_WINDOWS */
4376
4377/*
4378 * Set current window height and take care of repositioning other windows to
4379 * fit around it.
4380 */
4381 void
4382win_setheight(height)
4383 int height;
4384{
4385 win_setheight_win(height, curwin);
4386}
4387
4388/*
4389 * Set the window height of window "win" and take care of repositioning other
4390 * windows to fit around it.
4391 */
4392 void
4393win_setheight_win(height, win)
4394 int height;
4395 win_T *win;
4396{
4397 int row;
4398
4399 if (win == curwin)
4400 {
4401 /* Always keep current window at least one line high, even when
4402 * 'winminheight' is zero. */
4403#ifdef FEAT_WINDOWS
4404 if (height < p_wmh)
4405 height = p_wmh;
4406#endif
4407 if (height == 0)
4408 height = 1;
4409 }
4410
4411#ifdef FEAT_WINDOWS
4412 frame_setheight(win->w_frame, height + win->w_status_height);
4413
4414 /* recompute the window positions */
4415 row = win_comp_pos();
4416#else
4417 if (height > topframe->fr_height)
4418 height = topframe->fr_height;
4419 win->w_height = height;
4420 row = height;
4421#endif
4422
4423 /*
4424 * If there is extra space created between the last window and the command
4425 * line, clear it.
4426 */
4427 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4428 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4429 cmdline_row = row;
4430 msg_row = row;
4431 msg_col = 0;
4432
4433 redraw_all_later(NOT_VALID);
4434}
4435
4436#if defined(FEAT_WINDOWS) || defined(PROTO)
4437
4438/*
4439 * Set the height of a frame to "height" and take care that all frames and
4440 * windows inside it are resized. Also resize frames on the left and right if
4441 * the are in the same FR_ROW frame.
4442 *
4443 * Strategy:
4444 * If the frame is part of a FR_COL frame, try fitting the frame in that
4445 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4446 * go to containing frames to resize them and make room.
4447 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4448 * Check for the minimal height of the FR_ROW frame.
4449 * At the top level we can also use change the command line height.
4450 */
4451 static void
4452frame_setheight(curfrp, height)
4453 frame_T *curfrp;
4454 int height;
4455{
4456 int room; /* total number of lines available */
4457 int take; /* number of lines taken from other windows */
4458 int room_cmdline; /* lines available from cmdline */
4459 int run;
4460 frame_T *frp;
4461 int h;
4462 int room_reserved;
4463
4464 /* If the height already is the desired value, nothing to do. */
4465 if (curfrp->fr_height == height)
4466 return;
4467
4468 if (curfrp->fr_parent == NULL)
4469 {
4470 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004471 if (height > ROWS_AVAIL)
4472 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (height > 0)
4474 frame_new_height(curfrp, height, FALSE, FALSE);
4475 }
4476 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4477 {
4478 /* Row of frames: Also need to resize frames left and right of this
4479 * one. First check for the minimal height of these. */
4480 h = frame_minheight(curfrp->fr_parent, NULL);
4481 if (height < h)
4482 height = h;
4483 frame_setheight(curfrp->fr_parent, height);
4484 }
4485 else
4486 {
4487 /*
4488 * Column of frames: try to change only frames in this column.
4489 */
4490#ifdef FEAT_VERTSPLIT
4491 /*
4492 * Do this twice:
4493 * 1: compute room available, if it's not enough try resizing the
4494 * containing frame.
4495 * 2: compute the room available and adjust the height to it.
4496 * Try not to reduce the height of a window with 'winfixheight' set.
4497 */
4498 for (run = 1; run <= 2; ++run)
4499#else
4500 for (;;)
4501#endif
4502 {
4503 room = 0;
4504 room_reserved = 0;
4505 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4506 frp = frp->fr_next)
4507 {
4508 if (frp != curfrp
4509 && frp->fr_win != NULL
4510 && frp->fr_win->w_p_wfh)
4511 room_reserved += frp->fr_height;
4512 room += frp->fr_height;
4513 if (frp != curfrp)
4514 room -= frame_minheight(frp, NULL);
4515 }
4516#ifdef FEAT_VERTSPLIT
4517 if (curfrp->fr_width != Columns)
4518 room_cmdline = 0;
4519 else
4520#endif
4521 {
4522 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4523 + lastwin->w_height + lastwin->w_status_height);
4524 if (room_cmdline < 0)
4525 room_cmdline = 0;
4526 }
4527
4528 if (height <= room + room_cmdline)
4529 break;
4530#ifdef FEAT_VERTSPLIT
4531 if (run == 2 || curfrp->fr_width == Columns)
4532#endif
4533 {
4534 if (height > room + room_cmdline)
4535 height = room + room_cmdline;
4536 break;
4537 }
4538#ifdef FEAT_VERTSPLIT
4539 frame_setheight(curfrp->fr_parent, height
4540 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4541#endif
4542 /*NOTREACHED*/
4543 }
4544
4545 /*
4546 * Compute the number of lines we will take from others frames (can be
4547 * negative!).
4548 */
4549 take = height - curfrp->fr_height;
4550
4551 /* If there is not enough room, also reduce the height of a window
4552 * with 'winfixheight' set. */
4553 if (height > room + room_cmdline - room_reserved)
4554 room_reserved = room + room_cmdline - height;
4555 /* If there is only a 'winfixheight' window and making the
4556 * window smaller, need to make the other window taller. */
4557 if (take < 0 && room - curfrp->fr_height < room_reserved)
4558 room_reserved = 0;
4559
4560 if (take > 0 && room_cmdline > 0)
4561 {
4562 /* use lines from cmdline first */
4563 if (take < room_cmdline)
4564 room_cmdline = take;
4565 take -= room_cmdline;
4566 topframe->fr_height += room_cmdline;
4567 }
4568
4569 /*
4570 * set the current frame to the new height
4571 */
4572 frame_new_height(curfrp, height, FALSE, FALSE);
4573
4574 /*
4575 * First take lines from the frames after the current frame. If
4576 * that is not enough, takes lines from frames above the current
4577 * frame.
4578 */
4579 for (run = 0; run < 2; ++run)
4580 {
4581 if (run == 0)
4582 frp = curfrp->fr_next; /* 1st run: start with next window */
4583 else
4584 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4585 while (frp != NULL && take != 0)
4586 {
4587 h = frame_minheight(frp, NULL);
4588 if (room_reserved > 0
4589 && frp->fr_win != NULL
4590 && frp->fr_win->w_p_wfh)
4591 {
4592 if (room_reserved >= frp->fr_height)
4593 room_reserved -= frp->fr_height;
4594 else
4595 {
4596 if (frp->fr_height - room_reserved > take)
4597 room_reserved = frp->fr_height - take;
4598 take -= frp->fr_height - room_reserved;
4599 frame_new_height(frp, room_reserved, FALSE, FALSE);
4600 room_reserved = 0;
4601 }
4602 }
4603 else
4604 {
4605 if (frp->fr_height - take < h)
4606 {
4607 take -= frp->fr_height - h;
4608 frame_new_height(frp, h, FALSE, FALSE);
4609 }
4610 else
4611 {
4612 frame_new_height(frp, frp->fr_height - take,
4613 FALSE, FALSE);
4614 take = 0;
4615 }
4616 }
4617 if (run == 0)
4618 frp = frp->fr_next;
4619 else
4620 frp = frp->fr_prev;
4621 }
4622 }
4623 }
4624}
4625
4626#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4627/*
4628 * Set current window width and take care of repositioning other windows to
4629 * fit around it.
4630 */
4631 void
4632win_setwidth(width)
4633 int width;
4634{
4635 win_setwidth_win(width, curwin);
4636}
4637
4638 void
4639win_setwidth_win(width, wp)
4640 int width;
4641 win_T *wp;
4642{
4643 /* Always keep current window at least one column wide, even when
4644 * 'winminwidth' is zero. */
4645 if (wp == curwin)
4646 {
4647 if (width < p_wmw)
4648 width = p_wmw;
4649 if (width == 0)
4650 width = 1;
4651 }
4652
4653 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4654
4655 /* recompute the window positions */
4656 (void)win_comp_pos();
4657
4658 redraw_all_later(NOT_VALID);
4659}
4660
4661/*
4662 * Set the width of a frame to "width" and take care that all frames and
4663 * windows inside it are resized. Also resize frames above and below if the
4664 * are in the same FR_ROW frame.
4665 *
4666 * Strategy is similar to frame_setheight().
4667 */
4668 static void
4669frame_setwidth(curfrp, width)
4670 frame_T *curfrp;
4671 int width;
4672{
4673 int room; /* total number of lines available */
4674 int take; /* number of lines taken from other windows */
4675 int run;
4676 frame_T *frp;
4677 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004678 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 /* If the width already is the desired value, nothing to do. */
4681 if (curfrp->fr_width == width)
4682 return;
4683
4684 if (curfrp->fr_parent == NULL)
4685 /* topframe: can't change width */
4686 return;
4687
4688 if (curfrp->fr_parent->fr_layout == FR_COL)
4689 {
4690 /* Column of frames: Also need to resize frames above and below of
4691 * this one. First check for the minimal width of these. */
4692 w = frame_minwidth(curfrp->fr_parent, NULL);
4693 if (width < w)
4694 width = w;
4695 frame_setwidth(curfrp->fr_parent, width);
4696 }
4697 else
4698 {
4699 /*
4700 * Row of frames: try to change only frames in this row.
4701 *
4702 * Do this twice:
4703 * 1: compute room available, if it's not enough try resizing the
4704 * containing frame.
4705 * 2: compute the room available and adjust the width to it.
4706 */
4707 for (run = 1; run <= 2; ++run)
4708 {
4709 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004710 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4712 frp = frp->fr_next)
4713 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004714 if (frp != curfrp
4715 && frp->fr_win != NULL
4716 && frp->fr_win->w_p_wfw)
4717 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 room += frp->fr_width;
4719 if (frp != curfrp)
4720 room -= frame_minwidth(frp, NULL);
4721 }
4722
4723 if (width <= room)
4724 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004725 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 if (width > room)
4728 width = room;
4729 break;
4730 }
4731 frame_setwidth(curfrp->fr_parent, width
4732 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Compute the number of lines we will take from others frames (can be
4737 * negative!).
4738 */
4739 take = width - curfrp->fr_width;
4740
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004741 /* If there is not enough room, also reduce the width of a window
4742 * with 'winfixwidth' set. */
4743 if (width > room - room_reserved)
4744 room_reserved = room - width;
4745 /* If there is only a 'winfixwidth' window and making the
4746 * window smaller, need to make the other window narrower. */
4747 if (take < 0 && room - curfrp->fr_width < room_reserved)
4748 room_reserved = 0;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 /*
4751 * set the current frame to the new width
4752 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004753 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 /*
4756 * First take lines from the frames right of the current frame. If
4757 * that is not enough, takes lines from frames left of the current
4758 * frame.
4759 */
4760 for (run = 0; run < 2; ++run)
4761 {
4762 if (run == 0)
4763 frp = curfrp->fr_next; /* 1st run: start with next window */
4764 else
4765 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4766 while (frp != NULL && take != 0)
4767 {
4768 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004769 if (room_reserved > 0
4770 && frp->fr_win != NULL
4771 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004773 if (room_reserved >= frp->fr_width)
4774 room_reserved -= frp->fr_width;
4775 else
4776 {
4777 if (frp->fr_width - room_reserved > take)
4778 room_reserved = frp->fr_width - take;
4779 take -= frp->fr_width - room_reserved;
4780 frame_new_width(frp, room_reserved, FALSE, FALSE);
4781 room_reserved = 0;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 else
4785 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004786 if (frp->fr_width - take < w)
4787 {
4788 take -= frp->fr_width - w;
4789 frame_new_width(frp, w, FALSE, FALSE);
4790 }
4791 else
4792 {
4793 frame_new_width(frp, frp->fr_width - take,
4794 FALSE, FALSE);
4795 take = 0;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 if (run == 0)
4799 frp = frp->fr_next;
4800 else
4801 frp = frp->fr_prev;
4802 }
4803 }
4804 }
4805}
4806#endif /* FEAT_VERTSPLIT */
4807
4808/*
4809 * Check 'winminheight' for a valid value.
4810 */
4811 void
4812win_setminheight()
4813{
4814 int room;
4815 int first = TRUE;
4816 win_T *wp;
4817
4818 /* loop until there is a 'winminheight' that is possible */
4819 while (p_wmh > 0)
4820 {
4821 /* TODO: handle vertical splits */
4822 room = -p_wh;
4823 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4824 room += wp->w_height - p_wmh;
4825 if (room >= 0)
4826 break;
4827 --p_wmh;
4828 if (first)
4829 {
4830 EMSG(_(e_noroom));
4831 first = FALSE;
4832 }
4833 }
4834}
4835
4836#ifdef FEAT_MOUSE
4837
4838/*
4839 * Status line of dragwin is dragged "offset" lines down (negative is up).
4840 */
4841 void
4842win_drag_status_line(dragwin, offset)
4843 win_T *dragwin;
4844 int offset;
4845{
4846 frame_T *curfr;
4847 frame_T *fr;
4848 int room;
4849 int row;
4850 int up; /* if TRUE, drag status line up, otherwise down */
4851 int n;
4852
4853 fr = dragwin->w_frame;
4854 curfr = fr;
4855 if (fr != topframe) /* more than one window */
4856 {
4857 fr = fr->fr_parent;
4858 /* When the parent frame is not a column of frames, its parent should
4859 * be. */
4860 if (fr->fr_layout != FR_COL)
4861 {
4862 curfr = fr;
4863 if (fr != topframe) /* only a row of windows, may drag statusline */
4864 fr = fr->fr_parent;
4865 }
4866 }
4867
4868 /* If this is the last frame in a column, may want to resize the parent
4869 * frame instead (go two up to skip a row of frames). */
4870 while (curfr != topframe && curfr->fr_next == NULL)
4871 {
4872 if (fr != topframe)
4873 fr = fr->fr_parent;
4874 curfr = fr;
4875 if (fr != topframe)
4876 fr = fr->fr_parent;
4877 }
4878
4879 if (offset < 0) /* drag up */
4880 {
4881 up = TRUE;
4882 offset = -offset;
4883 /* sum up the room of the current frame and above it */
4884 if (fr == curfr)
4885 {
4886 /* only one window */
4887 room = fr->fr_height - frame_minheight(fr, NULL);
4888 }
4889 else
4890 {
4891 room = 0;
4892 for (fr = fr->fr_child; ; fr = fr->fr_next)
4893 {
4894 room += fr->fr_height - frame_minheight(fr, NULL);
4895 if (fr == curfr)
4896 break;
4897 }
4898 }
4899 fr = curfr->fr_next; /* put fr at frame that grows */
4900 }
4901 else /* drag down */
4902 {
4903 up = FALSE;
4904 /*
4905 * Only dragging the last status line can reduce p_ch.
4906 */
4907 room = Rows - cmdline_row;
4908 if (curfr->fr_next == NULL)
4909 room -= 1;
4910 else
4911 room -= p_ch;
4912 if (room < 0)
4913 room = 0;
4914 /* sum up the room of frames below of the current one */
4915 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4916 room += fr->fr_height - frame_minheight(fr, NULL);
4917 fr = curfr; /* put fr at window that grows */
4918 }
4919
4920 if (room < offset) /* Not enough room */
4921 offset = room; /* Move as far as we can */
4922 if (offset <= 0)
4923 return;
4924
4925 /*
4926 * Grow frame fr by "offset" lines.
4927 * Doesn't happen when dragging the last status line up.
4928 */
4929 if (fr != NULL)
4930 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4931
4932 if (up)
4933 fr = curfr; /* current frame gets smaller */
4934 else
4935 fr = curfr->fr_next; /* next frame gets smaller */
4936
4937 /*
4938 * Now make the other frames smaller.
4939 */
4940 while (fr != NULL && offset > 0)
4941 {
4942 n = frame_minheight(fr, NULL);
4943 if (fr->fr_height - offset <= n)
4944 {
4945 offset -= fr->fr_height - n;
4946 frame_new_height(fr, n, !up, FALSE);
4947 }
4948 else
4949 {
4950 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4951 break;
4952 }
4953 if (up)
4954 fr = fr->fr_prev;
4955 else
4956 fr = fr->fr_next;
4957 }
4958 row = win_comp_pos();
4959 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4960 cmdline_row = row;
4961 p_ch = Rows - cmdline_row;
4962 if (p_ch < 1)
4963 p_ch = 1;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004964 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 showmode();
4966}
4967
4968#ifdef FEAT_VERTSPLIT
4969/*
4970 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4971 */
4972 void
4973win_drag_vsep_line(dragwin, offset)
4974 win_T *dragwin;
4975 int offset;
4976{
4977 frame_T *curfr;
4978 frame_T *fr;
4979 int room;
4980 int left; /* if TRUE, drag separator line left, otherwise right */
4981 int n;
4982
4983 fr = dragwin->w_frame;
4984 if (fr == topframe) /* only one window (cannot happe?) */
4985 return;
4986 curfr = fr;
4987 fr = fr->fr_parent;
4988 /* When the parent frame is not a row of frames, its parent should be. */
4989 if (fr->fr_layout != FR_ROW)
4990 {
4991 if (fr == topframe) /* only a column of windows (cannot happen?) */
4992 return;
4993 curfr = fr;
4994 fr = fr->fr_parent;
4995 }
4996
4997 /* If this is the last frame in a row, may want to resize a parent
4998 * frame instead. */
4999 while (curfr->fr_next == NULL)
5000 {
5001 if (fr == topframe)
5002 break;
5003 curfr = fr;
5004 fr = fr->fr_parent;
5005 if (fr != topframe)
5006 {
5007 curfr = fr;
5008 fr = fr->fr_parent;
5009 }
5010 }
5011
5012 if (offset < 0) /* drag left */
5013 {
5014 left = TRUE;
5015 offset = -offset;
5016 /* sum up the room of the current frame and left of it */
5017 room = 0;
5018 for (fr = fr->fr_child; ; fr = fr->fr_next)
5019 {
5020 room += fr->fr_width - frame_minwidth(fr, NULL);
5021 if (fr == curfr)
5022 break;
5023 }
5024 fr = curfr->fr_next; /* put fr at frame that grows */
5025 }
5026 else /* drag right */
5027 {
5028 left = FALSE;
5029 /* sum up the room of frames right of the current one */
5030 room = 0;
5031 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5032 room += fr->fr_width - frame_minwidth(fr, NULL);
5033 fr = curfr; /* put fr at window that grows */
5034 }
5035
5036 if (room < offset) /* Not enough room */
5037 offset = room; /* Move as far as we can */
5038 if (offset <= 0) /* No room at all, quit. */
5039 return;
5040
5041 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005042 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /* shrink other frames: current and at the left or at the right */
5045 if (left)
5046 fr = curfr; /* current frame gets smaller */
5047 else
5048 fr = curfr->fr_next; /* next frame gets smaller */
5049
5050 while (fr != NULL && offset > 0)
5051 {
5052 n = frame_minwidth(fr, NULL);
5053 if (fr->fr_width - offset <= n)
5054 {
5055 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005056 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 else
5059 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005060 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
5062 }
5063 if (left)
5064 fr = fr->fr_prev;
5065 else
5066 fr = fr->fr_next;
5067 }
5068 (void)win_comp_pos();
5069 redraw_all_later(NOT_VALID);
5070}
5071#endif /* FEAT_VERTSPLIT */
5072#endif /* FEAT_MOUSE */
5073
5074#endif /* FEAT_WINDOWS */
5075
5076/*
5077 * Set the height of a window.
5078 * This takes care of the things inside the window, not what happens to the
5079 * window position, the frame or to other windows.
5080 */
5081 static void
5082win_new_height(wp, height)
5083 win_T *wp;
5084 int height;
5085{
5086 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005087 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005089 int space;
5090 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#define FRACTION_MULT 16384L
5092
5093 /* Don't want a negative height. Happens when splitting a tiny window.
5094 * Will equalize heights soon to fix it. */
5095 if (height < 0)
5096 height = 0;
5097
5098 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5099 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5100 + FRACTION_MULT / 2) / (long)wp->w_height;
5101
5102 wp->w_height = height;
5103 wp->w_skipcol = 0;
5104
5105 /* Don't change w_topline when height is zero. Don't set w_topline when
5106 * 'scrollbind' is set and this isn't the current window. */
5107 if (height > 0
5108#ifdef FEAT_SCROLLBIND
5109 && (!wp->w_p_scb || wp == curwin)
5110#endif
5111 )
5112 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005113 /*
5114 * Find a value for w_topline that shows the cursor at the same
5115 * relative position in the window as before (more or less).
5116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 lnum = wp->w_cursor.lnum;
5118 if (lnum < 1) /* can happen when starting up */
5119 lnum = 1;
5120 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5121 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5122 sline = wp->w_wrow - line_size;
5123 if (sline < 0)
5124 {
5125 /*
5126 * Cursor line would go off top of screen if w_wrow was this high.
5127 */
5128 wp->w_wrow = line_size;
5129 }
5130 else
5131 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005132 space = height;
5133 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005135 space -= line_size;
5136 if (space > 0 && sline <= 0 && !did_below)
5137 {
5138 /* Try to use "~" lines below the text to avoid that text
5139 * is above the window while there are empty lines.
5140 * Subtract the rows below the cursor from "space" and
5141 * give the rest to "sline". */
5142 did_below = TRUE;
5143 bot = wp->w_cursor.lnum;
5144 while (space > 0)
5145 {
5146 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5147 space = 0;
5148 else
5149 {
5150#ifdef FEAT_FOLDING
5151 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5152#endif
5153 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5154 break;
5155 ++bot;
5156 space -= plines_win(wp, bot, TRUE);
5157 }
5158 }
5159 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5160 sline += space;
5161 }
5162 if (sline <= 0)
5163 break;
5164
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165#ifdef FEAT_FOLDING
5166 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5167 if (lnum == 1)
5168 {
5169 /* first line in buffer is folded */
5170 line_size = 1;
5171 --sline;
5172 break;
5173 }
5174#endif
5175 --lnum;
5176#ifdef FEAT_DIFF
5177 if (lnum == wp->w_topline)
5178 line_size = plines_win_nofill(wp, lnum, TRUE)
5179 + wp->w_topfill;
5180 else
5181#endif
5182 line_size = plines_win(wp, lnum, TRUE);
5183 sline -= line_size;
5184 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (sline < 0)
5187 {
5188 /*
5189 * Line we want at top would go off top of screen. Use next
5190 * line instead.
5191 */
5192#ifdef FEAT_FOLDING
5193 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5194#endif
5195 lnum++;
5196 wp->w_wrow -= line_size + sline;
5197 }
5198 else if (sline > 0)
5199 {
5200 /* First line of file reached, use that as topline. */
5201 lnum = 1;
5202 wp->w_wrow -= sline;
5203 }
5204 }
5205 set_topline(wp, lnum);
5206 }
5207
5208 if (wp == curwin)
5209 {
5210 if (p_so)
5211 update_topline();
5212 curs_columns(FALSE); /* validate w_wrow */
5213 }
5214 wp->w_prev_fraction_row = wp->w_wrow;
5215
5216 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005217 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#ifdef FEAT_WINDOWS
5219 wp->w_redr_status = TRUE;
5220#endif
5221 invalidate_botline_win(wp);
5222}
5223
5224#ifdef FEAT_VERTSPLIT
5225/*
5226 * Set the width of a window.
5227 */
5228 static void
5229win_new_width(wp, width)
5230 win_T *wp;
5231 int width;
5232{
5233 wp->w_width = width;
5234 wp->w_lines_valid = 0;
5235 changed_line_abv_curs_win(wp);
5236 invalidate_botline_win(wp);
5237 if (wp == curwin)
5238 {
5239 update_topline();
5240 curs_columns(TRUE); /* validate w_wrow */
5241 }
5242 redraw_win_later(wp, NOT_VALID);
5243 wp->w_redr_status = TRUE;
5244}
5245#endif
5246
5247 void
5248win_comp_scroll(wp)
5249 win_T *wp;
5250{
5251 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5252 if (wp->w_p_scr == 0)
5253 wp->w_p_scr = 1;
5254}
5255
5256/*
5257 * command_height: called whenever p_ch has been changed
5258 */
5259 void
5260command_height(old_p_ch)
5261 long old_p_ch;
5262{
5263#ifdef FEAT_WINDOWS
5264 int h;
5265 frame_T *frp;
5266
Bram Moolenaar05159a02005-02-26 23:04:13 +00005267 /* When passed a negative value use the value of p_ch that we remembered.
5268 * This is needed for when the GUI starts up, we can't be sure in what
5269 * order things happen. */
5270 if (old_p_ch < 0)
5271 old_p_ch = p_ch_used;
5272 p_ch_used = p_ch;
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 /* Find bottom frame with width of screen. */
5275 frp = lastwin->w_frame;
5276# ifdef FEAT_VERTSPLIT
5277 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5278 frp = frp->fr_parent;
5279# endif
5280
5281 /* Avoid changing the height of a window with 'winfixheight' set. */
5282 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5283 && frp->fr_win->w_p_wfh)
5284 frp = frp->fr_prev;
5285
5286 if (starting != NO_SCREEN)
5287 {
5288 cmdline_row = Rows - p_ch;
5289
5290 if (p_ch > old_p_ch) /* p_ch got bigger */
5291 {
5292 while (p_ch > old_p_ch)
5293 {
5294 if (frp == NULL)
5295 {
5296 EMSG(_(e_noroom));
5297 p_ch = old_p_ch;
5298 cmdline_row = Rows - p_ch;
5299 break;
5300 }
5301 h = frp->fr_height - frame_minheight(frp, NULL);
5302 if (h > p_ch - old_p_ch)
5303 h = p_ch - old_p_ch;
5304 old_p_ch += h;
5305 frame_add_height(frp, -h);
5306 frp = frp->fr_prev;
5307 }
5308
5309 /* Recompute window positions. */
5310 (void)win_comp_pos();
5311
5312 /* clear the lines added to cmdline */
5313 if (full_screen)
5314 screen_fill((int)(cmdline_row), (int)Rows, 0,
5315 (int)Columns, ' ', ' ', 0);
5316 msg_row = cmdline_row;
5317 redraw_cmdline = TRUE;
5318 return;
5319 }
5320
5321 if (msg_row < cmdline_row)
5322 msg_row = cmdline_row;
5323 redraw_cmdline = TRUE;
5324 }
5325 frame_add_height(frp, (int)(old_p_ch - p_ch));
5326
5327 /* Recompute window positions. */
5328 if (frp != lastwin->w_frame)
5329 (void)win_comp_pos();
5330#else
5331 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5332 cmdline_row = Rows - p_ch;
5333#endif
5334}
5335
5336#if defined(FEAT_WINDOWS) || defined(PROTO)
5337/*
5338 * Resize frame "frp" to be "n" lines higher (negative for less high).
5339 * Also resize the frames it is contained in.
5340 */
5341 static void
5342frame_add_height(frp, n)
5343 frame_T *frp;
5344 int n;
5345{
5346 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5347 for (;;)
5348 {
5349 frp = frp->fr_parent;
5350 if (frp == NULL)
5351 break;
5352 frp->fr_height += n;
5353 }
5354}
5355
5356/*
5357 * Add or remove a status line for the bottom window(s), according to the
5358 * value of 'laststatus'.
5359 */
5360 void
5361last_status(morewin)
5362 int morewin; /* pretend there are two or more windows */
5363{
5364 /* Don't make a difference between horizontal or vertical split. */
5365 last_status_rec(topframe, (p_ls == 2
5366 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5367}
5368
5369 static void
5370last_status_rec(fr, statusline)
5371 frame_T *fr;
5372 int statusline;
5373{
5374 frame_T *fp;
5375 win_T *wp;
5376
5377 if (fr->fr_layout == FR_LEAF)
5378 {
5379 wp = fr->fr_win;
5380 if (wp->w_status_height != 0 && !statusline)
5381 {
5382 /* remove status line */
5383 win_new_height(wp, wp->w_height + 1);
5384 wp->w_status_height = 0;
5385 comp_col();
5386 }
5387 else if (wp->w_status_height == 0 && statusline)
5388 {
5389 /* Find a frame to take a line from. */
5390 fp = fr;
5391 while (fp->fr_height <= frame_minheight(fp, NULL))
5392 {
5393 if (fp == topframe)
5394 {
5395 EMSG(_(e_noroom));
5396 return;
5397 }
5398 /* In a column of frames: go to frame above. If already at
5399 * the top or in a row of frames: go to parent. */
5400 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5401 fp = fp->fr_prev;
5402 else
5403 fp = fp->fr_parent;
5404 }
5405 wp->w_status_height = 1;
5406 if (fp != fr)
5407 {
5408 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5409 frame_fix_height(wp);
5410 (void)win_comp_pos();
5411 }
5412 else
5413 win_new_height(wp, wp->w_height - 1);
5414 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005415 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
5417 }
5418#ifdef FEAT_VERTSPLIT
5419 else if (fr->fr_layout == FR_ROW)
5420 {
5421 /* vertically split windows, set status line for each one */
5422 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5423 last_status_rec(fp, statusline);
5424 }
5425#endif
5426 else
5427 {
5428 /* horizontally split window, set status line for last one */
5429 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5430 ;
5431 last_status_rec(fp, statusline);
5432 }
5433}
5434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005435/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005436 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005437 */
5438 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005439tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005440{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005441#ifdef FEAT_GUI_TABLINE
5442 /* When the GUI has the tabline then this always returns zero. */
5443 if (gui_use_tabline())
5444 return 0;
5445#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005446 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005447 {
5448 case 0: return 0;
5449 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5450 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005451 return 1;
5452}
5453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454#endif /* FEAT_WINDOWS */
5455
5456#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5457/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005458 * Get the file name at the cursor.
5459 * If Visual mode is active, use the selected text if it's in one line.
5460 * Returns the name in allocated memory, NULL for failure.
5461 */
5462 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005463grab_file_name(count, file_lnum)
5464 long count;
5465 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005466{
5467# ifdef FEAT_VISUAL
5468 if (VIsual_active)
5469 {
5470 int len;
5471 char_u *ptr;
5472
5473 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5474 return NULL;
5475 return find_file_name_in_path(ptr, len,
5476 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5477 }
5478# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005479 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5480 file_lnum);
5481
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005482}
5483
5484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 * Return the file name under or after the cursor.
5486 *
5487 * The 'path' option is searched if the file name is not absolute.
5488 * The string returned has been alloc'ed and should be freed by the caller.
5489 * NULL is returned if the file name or file is not found.
5490 *
5491 * options:
5492 * FNAME_MESS give error messages
5493 * FNAME_EXP expand to path
5494 * FNAME_HYP check for hypertext link
5495 * FNAME_INCL apply "includeexpr"
5496 */
5497 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005498file_name_at_cursor(options, count, file_lnum)
5499 int options;
5500 long count;
5501 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005504 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5505 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506}
5507
5508/*
5509 * Return the name of the file under or after ptr[col].
5510 * Otherwise like file_name_at_cursor().
5511 */
5512 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005513file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u *line;
5515 int col;
5516 int options;
5517 long count;
5518 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005519 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 char_u *ptr;
5522 int len;
5523
5524 /*
5525 * search forward for what could be the start of a file name
5526 */
5527 ptr = line + col;
5528 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005529 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (*ptr == NUL) /* nothing found */
5531 {
5532 if (options & FNAME_MESS)
5533 EMSG(_("E446: No file name under cursor"));
5534 return NULL;
5535 }
5536
5537 /*
5538 * Search backward for first char of the file name.
5539 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5540 */
5541 while (ptr > line)
5542 {
5543#ifdef FEAT_MBYTE
5544 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5545 ptr -= len + 1;
5546 else
5547#endif
5548 if (vim_isfilec(ptr[-1])
5549 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5550 --ptr;
5551 else
5552 break;
5553 }
5554
5555 /*
5556 * Search forward for the last char of the file name.
5557 * Also allow "://" when ':' is not in 'isfname'.
5558 */
5559 len = 0;
5560 while (vim_isfilec(ptr[len])
5561 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5562#ifdef FEAT_MBYTE
5563 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005564 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 else
5566#endif
5567 ++len;
5568
5569 /*
5570 * If there is trailing punctuation, remove it.
5571 * But don't remove "..", could be a directory name.
5572 */
5573 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5574 && ptr[len - 2] != '.')
5575 --len;
5576
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005577 if (file_lnum != NULL)
5578 {
5579 char_u *p;
5580
5581 /* Get the number after the file name and a separator character */
5582 p = ptr + len;
5583 p = skipwhite(p);
5584 if (*p != NUL)
5585 {
5586 if (!isdigit(*p))
5587 ++p; /* skip the separator */
5588 p = skipwhite(p);
5589 if (isdigit(*p))
5590 *file_lnum = (int)getdigits(&p);
5591 }
5592 }
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5595}
5596
5597# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5598static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5599
5600 static char_u *
5601eval_includeexpr(ptr, len)
5602 char_u *ptr;
5603 int len;
5604{
5605 char_u *res;
5606
5607 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005608 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005609 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 set_vim_var_string(VV_FNAME, NULL, 0);
5611 return res;
5612}
5613#endif
5614
5615/*
5616 * Return the name of the file ptr[len] in 'path'.
5617 * Otherwise like file_name_at_cursor().
5618 */
5619 char_u *
5620find_file_name_in_path(ptr, len, options, count, rel_fname)
5621 char_u *ptr;
5622 int len;
5623 int options;
5624 long count;
5625 char_u *rel_fname; /* file we are searching relative to */
5626{
5627 char_u *file_name;
5628 int c;
5629# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5630 char_u *tofree = NULL;
5631
5632 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5633 {
5634 tofree = eval_includeexpr(ptr, len);
5635 if (tofree != NULL)
5636 {
5637 ptr = tofree;
5638 len = (int)STRLEN(ptr);
5639 }
5640 }
5641# endif
5642
5643 if (options & FNAME_EXP)
5644 {
5645 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5646 TRUE, rel_fname);
5647
5648# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5649 /*
5650 * If the file could not be found in a normal way, try applying
5651 * 'includeexpr' (unless done already).
5652 */
5653 if (file_name == NULL
5654 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5655 {
5656 tofree = eval_includeexpr(ptr, len);
5657 if (tofree != NULL)
5658 {
5659 ptr = tofree;
5660 len = (int)STRLEN(ptr);
5661 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5662 TRUE, rel_fname);
5663 }
5664 }
5665# endif
5666 if (file_name == NULL && (options & FNAME_MESS))
5667 {
5668 c = ptr[len];
5669 ptr[len] = NUL;
5670 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5671 ptr[len] = c;
5672 }
5673
5674 /* Repeat finding the file "count" times. This matters when it
5675 * appears several times in the path. */
5676 while (file_name != NULL && --count > 0)
5677 {
5678 vim_free(file_name);
5679 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5680 }
5681 }
5682 else
5683 file_name = vim_strnsave(ptr, len);
5684
5685# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5686 vim_free(tofree);
5687# endif
5688
5689 return file_name;
5690}
5691#endif /* FEAT_SEARCHPATH */
5692
5693/*
5694 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5695 * Also check for ":\\", which MS Internet Explorer accepts, return
5696 * URL_BACKSLASH.
5697 */
5698 static int
5699path_is_url(p)
5700 char_u *p;
5701{
5702 if (STRNCMP(p, "://", (size_t)3) == 0)
5703 return URL_SLASH;
5704 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5705 return URL_BACKSLASH;
5706 return 0;
5707}
5708
5709/*
5710 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5711 * Return URL_BACKSLASH for "name:\\".
5712 * Return zero otherwise.
5713 */
5714 int
5715path_with_url(fname)
5716 char_u *fname;
5717{
5718 char_u *p;
5719
5720 for (p = fname; isalpha(*p); ++p)
5721 ;
5722 return path_is_url(p);
5723}
5724
5725/*
5726 * Return TRUE if "name" is a full (absolute) path name or URL.
5727 */
5728 int
5729vim_isAbsName(name)
5730 char_u *name;
5731{
5732 return (path_with_url(name) != 0 || mch_isFullName(name));
5733}
5734
5735/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005736 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 *
5738 * return FAIL for failure, OK otherwise
5739 */
5740 int
5741vim_FullName(fname, buf, len, force)
5742 char_u *fname, *buf;
5743 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005744 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 int retval = OK;
5747 int url;
5748
5749 *buf = NUL;
5750 if (fname == NULL)
5751 return FAIL;
5752
5753 url = path_with_url(fname);
5754 if (!url)
5755 retval = mch_FullName(fname, buf, len, force);
5756 if (url || retval == FAIL)
5757 {
5758 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005759 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5762 slash_adjust(buf);
5763#endif
5764 return retval;
5765}
5766
5767/*
5768 * Return the minimal number of rows that is needed on the screen to display
5769 * the current number of windows.
5770 */
5771 int
5772min_rows()
5773{
5774 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005775#ifdef FEAT_WINDOWS
5776 tabpage_T *tp;
5777 int n;
5778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 if (firstwin == NULL) /* not initialized yet */
5781 return MIN_LINES;
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005784 total = 0;
5785 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5786 {
5787 n = frame_minheight(tp->tp_topframe, NULL);
5788 if (total < n)
5789 total = n;
5790 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005791 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005793 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005795 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 return total;
5797}
5798
5799/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005800 * Return TRUE if there is only one window (in the current tab page), not
5801 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 */
5803 int
5804only_one_window()
5805{
5806#ifdef FEAT_WINDOWS
5807 int count = 0;
5808 win_T *wp;
5809
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005810 /* If there is another tab page there always is another window. */
5811 if (first_tabpage->tp_next != NULL)
5812 return FALSE;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005815 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816# ifdef FEAT_QUICKFIX
5817 || wp->w_p_pvw
5818# endif
5819 ) || wp == curwin)
5820 ++count;
5821 return (count <= 1);
5822#else
5823 return TRUE;
5824#endif
5825}
5826
5827#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5828/*
5829 * Correct the cursor line number in other windows. Used after changing the
5830 * current buffer, and before applying autocommands.
5831 * When "do_curwin" is TRUE, also check current window.
5832 */
5833 void
5834check_lnums(do_curwin)
5835 int do_curwin;
5836{
5837 win_T *wp;
5838
5839#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005840 tabpage_T *tp;
5841
5842 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5844#else
5845 wp = curwin;
5846 if (do_curwin)
5847#endif
5848 {
5849 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5850 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5851 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5852 wp->w_topline = curbuf->b_ml.ml_line_count;
5853 }
5854}
5855#endif
5856
5857#if defined(FEAT_WINDOWS) || defined(PROTO)
5858
5859/*
5860 * A snapshot of the window sizes, to restore them after closing the help
5861 * window.
5862 * Only these fields are used:
5863 * fr_layout
5864 * fr_width
5865 * fr_height
5866 * fr_next
5867 * fr_child
5868 * fr_win (only valid for the old curwin, NULL otherwise)
5869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870
5871/*
5872 * Create a snapshot of the current frame sizes.
5873 */
5874 static void
5875make_snapshot()
5876{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005877 clear_snapshot(curtab);
5878 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879}
5880
5881 static void
5882make_snapshot_rec(fr, frp)
5883 frame_T *fr;
5884 frame_T **frp;
5885{
5886 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5887 if (*frp == NULL)
5888 return;
5889 (*frp)->fr_layout = fr->fr_layout;
5890# ifdef FEAT_VERTSPLIT
5891 (*frp)->fr_width = fr->fr_width;
5892# endif
5893 (*frp)->fr_height = fr->fr_height;
5894 if (fr->fr_next != NULL)
5895 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5896 if (fr->fr_child != NULL)
5897 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5898 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5899 (*frp)->fr_win = curwin;
5900}
5901
5902/*
5903 * Remove any existing snapshot.
5904 */
5905 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005906clear_snapshot(tp)
5907 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005909 clear_snapshot_rec(tp->tp_snapshot);
5910 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911}
5912
5913 static void
5914clear_snapshot_rec(fr)
5915 frame_T *fr;
5916{
5917 if (fr != NULL)
5918 {
5919 clear_snapshot_rec(fr->fr_next);
5920 clear_snapshot_rec(fr->fr_child);
5921 vim_free(fr);
5922 }
5923}
5924
5925/*
5926 * Restore a previously created snapshot, if there is any.
5927 * This is only done if the screen size didn't change and the window layout is
5928 * still the same.
5929 */
5930 static void
5931restore_snapshot(close_curwin)
5932 int close_curwin; /* closing current window */
5933{
5934 win_T *wp;
5935
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005936 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005938 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005940 && curtab->tp_snapshot->fr_height == topframe->fr_height
5941 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005943 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 win_comp_pos();
5945 if (wp != NULL && close_curwin)
5946 win_goto(wp);
5947 redraw_all_later(CLEAR);
5948 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005949 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950}
5951
5952/*
5953 * Check if frames "sn" and "fr" have the same layout, same following frames
5954 * and same children.
5955 */
5956 static int
5957check_snapshot_rec(sn, fr)
5958 frame_T *sn;
5959 frame_T *fr;
5960{
5961 if (sn->fr_layout != fr->fr_layout
5962 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5963 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5964 || (sn->fr_next != NULL
5965 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5966 || (sn->fr_child != NULL
5967 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5968 return FAIL;
5969 return OK;
5970}
5971
5972/*
5973 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5974 * following frames and children.
5975 * Returns a pointer to the old current window, or NULL.
5976 */
5977 static win_T *
5978restore_snapshot_rec(sn, fr)
5979 frame_T *sn;
5980 frame_T *fr;
5981{
5982 win_T *wp = NULL;
5983 win_T *wp2;
5984
5985 fr->fr_height = sn->fr_height;
5986# ifdef FEAT_VERTSPLIT
5987 fr->fr_width = sn->fr_width;
5988# endif
5989 if (fr->fr_layout == FR_LEAF)
5990 {
5991 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5992# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005993 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994# endif
5995 wp = sn->fr_win;
5996 }
5997 if (sn->fr_next != NULL)
5998 {
5999 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6000 if (wp2 != NULL)
6001 wp = wp2;
6002 }
6003 if (sn->fr_child != NULL)
6004 {
6005 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6006 if (wp2 != NULL)
6007 wp = wp2;
6008 }
6009 return wp;
6010}
6011
6012#endif
6013
6014#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6015/*
6016 * Return TRUE if there is any vertically split window.
6017 */
6018 int
6019win_hasvertsplit()
6020{
6021 frame_T *fr;
6022
6023 if (topframe->fr_layout == FR_ROW)
6024 return TRUE;
6025
6026 if (topframe->fr_layout == FR_COL)
6027 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6028 if (fr->fr_layout == FR_ROW)
6029 return TRUE;
6030
6031 return FALSE;
6032}
6033#endif