blob: 628f9e0f97de173b7f885eeb8f1d52b40bdd4f76 [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
Bram Moolenaar32466aa2006-02-24 23:53:04 +000088# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000089#else
90# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000091#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000094
95static char *m_onlyone = N_("Already only one window");
96
Bram Moolenaar071d4272004-06-13 20:20:40 +000097/*
98 * all CTRL-W window commands are handled here, called from normal_cmd().
99 */
100 void
101do_window(nchar, Prenum, xchar)
102 int nchar;
103 long Prenum;
104 int xchar; /* extra char from ":wincmd gx" or NUL */
105{
106 long Prenum1;
107 win_T *wp;
108#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
109 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000110 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
112#ifdef FEAT_FIND_ID
113 int type = FIND_DEFINE;
114 int len;
115#endif
116 char_u cbuf[40];
117
118 if (Prenum == 0)
119 Prenum1 = 1;
120 else
121 Prenum1 = Prenum;
122
123#ifdef FEAT_CMDWIN
124# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
125#else
126# define CHECK_CMDWIN
127#endif
128
129 switch (nchar)
130 {
131/* split current window in two parts, horizontally */
132 case 'S':
133 case Ctrl_S:
134 case 's':
135 CHECK_CMDWIN
136#ifdef FEAT_VISUAL
137 reset_VIsual_and_resel(); /* stop Visual mode */
138#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000139#ifdef FEAT_QUICKFIX
140 /* When splitting the quickfix window open a new buffer in it,
141 * don't replicate the quickfix buffer. */
142 if (bt_quickfix(curbuf))
143 goto newwindow;
144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#ifdef FEAT_GUI
146 need_mouse_correct = TRUE;
147#endif
148 win_split((int)Prenum, 0);
149 break;
150
151#ifdef FEAT_VERTSPLIT
152/* split current window in two parts, vertically */
153 case Ctrl_V:
154 case 'v':
155 CHECK_CMDWIN
156#ifdef FEAT_VISUAL
157 reset_VIsual_and_resel(); /* stop Visual mode */
158#endif
159#ifdef FEAT_GUI
160 need_mouse_correct = TRUE;
161#endif
162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
170#ifdef FEAT_VISUAL
171 reset_VIsual_and_resel(); /* stop Visual mode */
172#endif
173 STRCPY(cbuf, "split #");
174 if (Prenum)
175 sprintf((char *)cbuf + 7, "%ld", Prenum);
176 do_cmdline_cmd(cbuf);
177 break;
178
179/* open new window */
180 case Ctrl_N:
181 case 'n':
182 CHECK_CMDWIN
183#ifdef FEAT_VISUAL
184 reset_VIsual_and_resel(); /* stop Visual mode */
185#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000186#ifdef FEAT_QUICKFIX
187newwindow:
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (Prenum)
190 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
191 else
192 cbuf[0] = NUL;
193 STRCAT(cbuf, "new");
194 do_cmdline_cmd(cbuf);
195 break;
196
197/* quit current window */
198 case Ctrl_Q:
199 case 'q':
200#ifdef FEAT_VISUAL
201 reset_VIsual_and_resel(); /* stop Visual mode */
202#endif
203 do_cmdline_cmd((char_u *)"quit");
204 break;
205
206/* close current window */
207 case Ctrl_C:
208 case 'c':
209#ifdef FEAT_VISUAL
210 reset_VIsual_and_resel(); /* stop Visual mode */
211#endif
212 do_cmdline_cmd((char_u *)"close");
213 break;
214
215#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
216/* close preview window */
217 case Ctrl_Z:
218 case 'z':
219 CHECK_CMDWIN
220#ifdef FEAT_VISUAL
221 reset_VIsual_and_resel(); /* stop Visual mode */
222#endif
223 do_cmdline_cmd((char_u *)"pclose");
224 break;
225
226/* cursor to preview window */
227 case 'P':
228 for (wp = firstwin; wp != NULL; wp = wp->w_next)
229 if (wp->w_p_pvw)
230 break;
231 if (wp == NULL)
232 EMSG(_("E441: There is no preview window"));
233 else
234 win_goto(wp);
235 break;
236#endif
237
238/* close all but current window */
239 case Ctrl_O:
240 case 'o':
241 CHECK_CMDWIN
242#ifdef FEAT_VISUAL
243 reset_VIsual_and_resel(); /* stop Visual mode */
244#endif
245 do_cmdline_cmd((char_u *)"only");
246 break;
247
248/* cursor to next window with wrap around */
249 case Ctrl_W:
250 case 'w':
251/* cursor to previous window with wrap around */
252 case 'W':
253 CHECK_CMDWIN
254 if (lastwin == firstwin && Prenum != 1) /* just one window */
255 beep_flush();
256 else
257 {
258 if (Prenum) /* go to specified window */
259 {
260 for (wp = firstwin; --Prenum > 0; )
261 {
262 if (wp->w_next == NULL)
263 break;
264 else
265 wp = wp->w_next;
266 }
267 }
268 else
269 {
270 if (nchar == 'W') /* go to previous window */
271 {
272 wp = curwin->w_prev;
273 if (wp == NULL)
274 wp = lastwin; /* wrap around */
275 }
276 else /* go to next window */
277 {
278 wp = curwin->w_next;
279 if (wp == NULL)
280 wp = firstwin; /* wrap around */
281 }
282 }
283 win_goto(wp);
284 }
285 break;
286
287/* cursor to window below */
288 case 'j':
289 case K_DOWN:
290 case Ctrl_J:
291 CHECK_CMDWIN
292#ifdef FEAT_VERTSPLIT
293 win_goto_ver(FALSE, Prenum1);
294#else
295 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
296 wp = wp->w_next)
297 ;
298 win_goto(wp);
299#endif
300 break;
301
302/* cursor to window above */
303 case 'k':
304 case K_UP:
305 case Ctrl_K:
306 CHECK_CMDWIN
307#ifdef FEAT_VERTSPLIT
308 win_goto_ver(TRUE, Prenum1);
309#else
310 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
311 wp = wp->w_prev)
312 ;
313 win_goto(wp);
314#endif
315 break;
316
317#ifdef FEAT_VERTSPLIT
318/* cursor to left window */
319 case 'h':
320 case K_LEFT:
321 case Ctrl_H:
322 case K_BS:
323 CHECK_CMDWIN
324 win_goto_hor(TRUE, Prenum1);
325 break;
326
327/* cursor to right window */
328 case 'l':
329 case K_RIGHT:
330 case Ctrl_L:
331 CHECK_CMDWIN
332 win_goto_hor(FALSE, Prenum1);
333 break;
334#endif
335
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000336/* move window to new tab page */
337 case 'T':
338 if (firstwin == lastwin)
339 MSG(_(m_onlyone));
340 else
341 {
342 tabpage_T *oldtab = curtab;
343 tabpage_T *newtab;
344 win_T *wp = curwin;
345
346 /* First create a new tab with the window, then go back to
347 * the old tab and close the window there. */
348 if (win_new_tabpage((int)Prenum) == OK
349 && valid_tabpage(oldtab))
350 {
351 newtab = curtab;
352 goto_tabpage_tp(oldtab);
353 if (curwin == wp)
354 win_close(curwin, FALSE);
355 if (valid_tabpage(newtab))
356 goto_tabpage_tp(newtab);
357 }
358 }
359 break;
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361/* cursor to top-left window */
362 case 't':
363 case Ctrl_T:
364 win_goto(firstwin);
365 break;
366
367/* cursor to bottom-right window */
368 case 'b':
369 case Ctrl_B:
370 win_goto(lastwin);
371 break;
372
373/* cursor to last accessed (previous) window */
374 case 'p':
375 case Ctrl_P:
376 if (prevwin == NULL)
377 beep_flush();
378 else
379 win_goto(prevwin);
380 break;
381
382/* exchange current and next window */
383 case 'x':
384 case Ctrl_X:
385 CHECK_CMDWIN
386 win_exchange(Prenum);
387 break;
388
389/* rotate windows downwards */
390 case Ctrl_R:
391 case 'r':
392 CHECK_CMDWIN
393#ifdef FEAT_VISUAL
394 reset_VIsual_and_resel(); /* stop Visual mode */
395#endif
396 win_rotate(FALSE, (int)Prenum1); /* downwards */
397 break;
398
399/* rotate windows upwards */
400 case 'R':
401 CHECK_CMDWIN
402#ifdef FEAT_VISUAL
403 reset_VIsual_and_resel(); /* stop Visual mode */
404#endif
405 win_rotate(TRUE, (int)Prenum1); /* upwards */
406 break;
407
408/* move window to the very top/bottom/left/right */
409 case 'K':
410 case 'J':
411#ifdef FEAT_VERTSPLIT
412 case 'H':
413 case 'L':
414#endif
415 CHECK_CMDWIN
416 win_totop((int)Prenum,
417 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
418 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
419 break;
420
421/* make all windows the same height */
422 case '=':
423#ifdef FEAT_GUI
424 need_mouse_correct = TRUE;
425#endif
426 win_equal(NULL, FALSE, 'b');
427 break;
428
429/* increase current window height */
430 case '+':
431#ifdef FEAT_GUI
432 need_mouse_correct = TRUE;
433#endif
434 win_setheight(curwin->w_height + (int)Prenum1);
435 break;
436
437/* decrease current window height */
438 case '-':
439#ifdef FEAT_GUI
440 need_mouse_correct = TRUE;
441#endif
442 win_setheight(curwin->w_height - (int)Prenum1);
443 break;
444
445/* set current window height */
446 case Ctrl__:
447 case '_':
448#ifdef FEAT_GUI
449 need_mouse_correct = TRUE;
450#endif
451 win_setheight(Prenum ? (int)Prenum : 9999);
452 break;
453
454#ifdef FEAT_VERTSPLIT
455/* increase current window width */
456 case '>':
457#ifdef FEAT_GUI
458 need_mouse_correct = TRUE;
459#endif
460 win_setwidth(curwin->w_width + (int)Prenum1);
461 break;
462
463/* decrease current window width */
464 case '<':
465#ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467#endif
468 win_setwidth(curwin->w_width - (int)Prenum1);
469 break;
470
471/* set current window width */
472 case '|':
473#ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475#endif
476 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
477 break;
478#endif
479
480/* jump to tag and split window if tag exists (in preview window) */
481#if defined(FEAT_QUICKFIX)
482 case '}':
483 CHECK_CMDWIN
484 if (Prenum)
485 g_do_tagpreview = Prenum;
486 else
487 g_do_tagpreview = p_pvh;
488 /*FALLTHROUGH*/
489#endif
490 case ']':
491 case Ctrl_RSB:
492 CHECK_CMDWIN
493#ifdef FEAT_VISUAL
494 reset_VIsual_and_resel(); /* stop Visual mode */
495#endif
496 if (Prenum)
497 postponed_split = Prenum;
498 else
499 postponed_split = -1;
500
501 /* Execute the command right here, required when
502 * "wincmd ]" was used in a function. */
503 do_nv_ident(Ctrl_RSB, NUL);
504 break;
505
506#ifdef FEAT_SEARCHPATH
507/* edit file name under cursor in a new window */
508 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000509 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000511wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000513
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (ptr != NULL)
516 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000517# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000519# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 setpcmark();
521 if (win_split(0, 0) == OK)
522 {
523# ifdef FEAT_SCROLLBIND
524 curwin->w_p_scb = FALSE;
525# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000526 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
527 if (nchar == 'F' && lnum >= 0)
528 {
529 curwin->w_cursor.lnum = lnum;
530 check_cursor_lnum();
531 beginline(BL_SOL | BL_FIX);
532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 vim_free(ptr);
535 }
536 break;
537#endif
538
539#ifdef FEAT_FIND_ID
540/* Go to the first occurence of the identifier under cursor along path in a
541 * new window -- webb
542 */
543 case 'i': /* Go to any match */
544 case Ctrl_I:
545 type = FIND_ANY;
546 /* FALLTHROUGH */
547 case 'd': /* Go to definition, using 'define' */
548 case Ctrl_D:
549 CHECK_CMDWIN
550 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
551 break;
552 find_pattern_in_path(ptr, 0, len, TRUE,
553 Prenum == 0 ? TRUE : FALSE, type,
554 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
555 curwin->w_set_curswant = TRUE;
556 break;
557#endif
558
Bram Moolenaar05159a02005-02-26 23:04:13 +0000559 case K_KENTER:
560 case CAR:
561#if defined(FEAT_QUICKFIX)
562 /*
563 * In a quickfix window a <CR> jumps to the error under the
564 * cursor in a new window.
565 */
566 if (bt_quickfix(curbuf))
567 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000568 sprintf((char *)cbuf, "split +%ld%s",
569 (long)curwin->w_cursor.lnum,
570 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000571 do_cmdline_cmd(cbuf);
572 }
573#endif
574 break;
575
576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577/* CTRL-W g extended commands */
578 case 'g':
579 case Ctrl_G:
580 CHECK_CMDWIN
581#ifdef USE_ON_FLY_SCROLL
582 dont_scroll = TRUE; /* disallow scrolling here */
583#endif
584 ++no_mapping;
585 ++allow_keys; /* no mapping for xchar, but allow key codes */
586 if (xchar == NUL)
587 xchar = safe_vgetc();
588#ifdef FEAT_LANGMAP
589 LANGMAP_ADJUST(xchar, TRUE);
590#endif
591 --no_mapping;
592 --allow_keys;
593#ifdef FEAT_CMDL_INFO
594 (void)add_to_showcmd(xchar);
595#endif
596 switch (xchar)
597 {
598#if defined(FEAT_QUICKFIX)
599 case '}':
600 xchar = Ctrl_RSB;
601 if (Prenum)
602 g_do_tagpreview = Prenum;
603 else
604 g_do_tagpreview = p_pvh;
605 /*FALLTHROUGH*/
606#endif
607 case ']':
608 case Ctrl_RSB:
609#ifdef FEAT_VISUAL
610 reset_VIsual_and_resel(); /* stop Visual mode */
611#endif
612 if (Prenum)
613 postponed_split = Prenum;
614 else
615 postponed_split = -1;
616
617 /* Execute the command right here, required when
618 * "wincmd g}" was used in a function. */
619 do_nv_ident('g', xchar);
620 break;
621
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000622#ifdef FEAT_SEARCHPATH
623 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000624 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000625 cmdmod.tab = TRUE;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000626 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000627 goto wingotofile;
628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 default:
630 beep_flush();
631 break;
632 }
633 break;
634
635 default: beep_flush();
636 break;
637 }
638}
639
640/*
641 * split the current window, implements CTRL-W s and :split
642 *
643 * "size" is the height or width for the new window, 0 to use half of current
644 * height or width.
645 *
646 * "flags":
647 * WSP_ROOM: require enough room for new window
648 * WSP_VERT: vertical split.
649 * WSP_TOP: open window at the top-left of the shell (help window).
650 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
651 * WSP_HELP: creating the help window, keep layout snapshot
652 *
653 * return FAIL for failure, OK otherwise
654 */
655 int
656win_split(size, flags)
657 int size;
658 int flags;
659{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000660 /* When the ":tab" modifier was used open a new tab page instead. */
661 if (may_open_tabpage() == OK)
662 return OK;
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 /* Add flags from ":vertical", ":topleft" and ":botright". */
665 flags |= cmdmod.split;
666 if ((flags & WSP_TOP) && (flags & WSP_BOT))
667 {
668 EMSG(_("E442: Can't split topleft and botright at the same time"));
669 return FAIL;
670 }
671
672 /* When creating the help window make a snapshot of the window layout.
673 * Otherwise clear the snapshot, it's now invalid. */
674 if (flags & WSP_HELP)
675 make_snapshot();
676 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000677 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 return win_split_ins(size, flags, NULL, 0);
680}
681
682/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000683 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 * When "newwin" is not NULL: insert this window at the far
685 * top/left/right/bottom.
686 * return FAIL for failure, OK otherwise
687 */
688 static int
689win_split_ins(size, flags, newwin, dir)
690 int size;
691 int flags;
692 win_T *newwin;
693 int dir;
694{
695 win_T *wp = newwin;
696 win_T *oldwin;
697 int new_size = size;
698 int i;
699 int need_status = 0;
700 int do_equal = FALSE;
701 int needed;
702 int available;
703 int oldwin_height = 0;
704 int layout;
705 frame_T *frp, *curfrp;
706 int before;
707
708 if (flags & WSP_TOP)
709 oldwin = firstwin;
710 else if (flags & WSP_BOT)
711 oldwin = lastwin;
712 else
713 oldwin = curwin;
714
715 /* add a status line when p_ls == 1 and splitting the first window */
716 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
717 {
718 if (oldwin->w_height <= p_wmh && newwin == NULL)
719 {
720 EMSG(_(e_noroom));
721 return FAIL;
722 }
723 need_status = STATUS_HEIGHT;
724 }
725
726#ifdef FEAT_VERTSPLIT
727 if (flags & WSP_VERT)
728 {
729 layout = FR_ROW;
730 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
731
732 /*
733 * Check if we are able to split the current window and compute its
734 * width.
735 */
736 needed = p_wmw + 1;
737 if (flags & WSP_ROOM)
738 needed += p_wiw - p_wmw;
739 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
740 {
741 available = topframe->fr_width;
742 needed += frame_minwidth(topframe, NULL);
743 }
744 else
745 available = oldwin->w_width;
746 if (available < needed && newwin == NULL)
747 {
748 EMSG(_(e_noroom));
749 return FAIL;
750 }
751 if (new_size == 0)
752 new_size = oldwin->w_width / 2;
753 if (new_size > oldwin->w_width - p_wmw - 1)
754 new_size = oldwin->w_width - p_wmw - 1;
755 if (new_size < p_wmw)
756 new_size = p_wmw;
757
758 /* if it doesn't fit in the current window, need win_equal() */
759 if (oldwin->w_width - new_size - 1 < p_wmw)
760 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000761
762 /* We don't like to take lines for the new window from a
763 * 'winfixwidth' window. Take them from a window to the left or right
764 * instead, if possible. */
765 if (oldwin->w_p_wfw)
766 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 else
769#endif
770 {
771 layout = FR_COL;
772 do_equal = (p_ea && new_size == 0
773#ifdef FEAT_VERTSPLIT
774 && *p_ead != 'h'
775#endif
776 );
777
778 /*
779 * Check if we are able to split the current window and compute its
780 * height.
781 */
782 needed = p_wmh + STATUS_HEIGHT + need_status;
783 if (flags & WSP_ROOM)
784 needed += p_wh - p_wmh;
785 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
786 {
787 available = topframe->fr_height;
788 needed += frame_minheight(topframe, NULL);
789 }
790 else
791 {
792 available = oldwin->w_height;
793 needed += p_wmh;
794 }
795 if (available < needed && newwin == NULL)
796 {
797 EMSG(_(e_noroom));
798 return FAIL;
799 }
800 oldwin_height = oldwin->w_height;
801 if (need_status)
802 {
803 oldwin->w_status_height = STATUS_HEIGHT;
804 oldwin_height -= STATUS_HEIGHT;
805 }
806 if (new_size == 0)
807 new_size = oldwin_height / 2;
808
809 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
810 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
811 if (new_size < p_wmh)
812 new_size = p_wmh;
813
814 /* if it doesn't fit in the current window, need win_equal() */
815 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
816 do_equal = TRUE;
817
818 /* We don't like to take lines for the new window from a
819 * 'winfixheight' window. Take them from a window above or below
820 * instead, if possible. */
821 if (oldwin->w_p_wfh)
822 {
823 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
824 oldwin);
825 oldwin_height = oldwin->w_height;
826 if (need_status)
827 oldwin_height -= STATUS_HEIGHT;
828 }
829 }
830
831 /*
832 * allocate new window structure and link it in the window list
833 */
834 if ((flags & WSP_TOP) == 0
835 && ((flags & WSP_BOT)
836 || (flags & WSP_BELOW)
837 || (!(flags & WSP_ABOVE)
838 && (
839#ifdef FEAT_VERTSPLIT
840 (flags & WSP_VERT) ? p_spr :
841#endif
842 p_sb))))
843 {
844 /* new window below/right of current one */
845 if (newwin == NULL)
846 wp = win_alloc(oldwin);
847 else
848 win_append(oldwin, wp);
849 }
850 else
851 {
852 if (newwin == NULL)
853 wp = win_alloc(oldwin->w_prev);
854 else
855 win_append(oldwin->w_prev, wp);
856 }
857
858 if (newwin == NULL)
859 {
860 if (wp == NULL)
861 return FAIL;
862
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000863 /* make the contents of the new window the same as the current one */
864 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866
867 /*
868 * Reorganise the tree of frames to insert the new window.
869 */
870 if (flags & (WSP_TOP | WSP_BOT))
871 {
872#ifdef FEAT_VERTSPLIT
873 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
874 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
875#else
876 if (topframe->fr_layout == FR_COL)
877#endif
878 {
879 curfrp = topframe->fr_child;
880 if (flags & WSP_BOT)
881 while (curfrp->fr_next != NULL)
882 curfrp = curfrp->fr_next;
883 }
884 else
885 curfrp = topframe;
886 before = (flags & WSP_TOP);
887 }
888 else
889 {
890 curfrp = oldwin->w_frame;
891 if (flags & WSP_BELOW)
892 before = FALSE;
893 else if (flags & WSP_ABOVE)
894 before = TRUE;
895 else
896#ifdef FEAT_VERTSPLIT
897 if (flags & WSP_VERT)
898 before = !p_spr;
899 else
900#endif
901 before = !p_sb;
902 }
903 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
904 {
905 /* Need to create a new frame in the tree to make a branch. */
906 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
907 *frp = *curfrp;
908 curfrp->fr_layout = layout;
909 frp->fr_parent = curfrp;
910 frp->fr_next = NULL;
911 frp->fr_prev = NULL;
912 curfrp->fr_child = frp;
913 curfrp->fr_win = NULL;
914 curfrp = frp;
915 if (frp->fr_win != NULL)
916 oldwin->w_frame = frp;
917 else
918 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
919 frp->fr_parent = curfrp;
920 }
921
922 if (newwin == NULL)
923 {
924 /* Create a frame for the new window. */
925 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
926 frp->fr_layout = FR_LEAF;
927 frp->fr_win = wp;
928 wp->w_frame = frp;
929 }
930 else
931 frp = newwin->w_frame;
932 frp->fr_parent = curfrp->fr_parent;
933
934 /* Insert the new frame at the right place in the frame list. */
935 if (before)
936 frame_insert(curfrp, frp);
937 else
938 frame_append(curfrp, frp);
939
940#ifdef FEAT_VERTSPLIT
941 if (flags & WSP_VERT)
942 {
943 wp->w_p_scr = curwin->w_p_scr;
944 if (need_status)
945 {
946 --oldwin->w_height;
947 oldwin->w_status_height = need_status;
948 }
949 if (flags & (WSP_TOP | WSP_BOT))
950 {
951 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000952 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 wp->w_height = curfrp->fr_height - (p_ls > 0);
954 wp->w_status_height = (p_ls > 0);
955 }
956 else
957 {
958 /* height and row of new window is same as current window */
959 wp->w_winrow = oldwin->w_winrow;
960 wp->w_height = oldwin->w_height;
961 wp->w_status_height = oldwin->w_status_height;
962 }
963 frp->fr_height = curfrp->fr_height;
964
965 /* "new_size" of the current window goes to the new window, use
966 * one column for the vertical separator */
967 wp->w_width = new_size;
968 if (before)
969 wp->w_vsep_width = 1;
970 else
971 {
972 wp->w_vsep_width = oldwin->w_vsep_width;
973 oldwin->w_vsep_width = 1;
974 }
975 if (flags & (WSP_TOP | WSP_BOT))
976 {
977 if (flags & WSP_BOT)
978 frame_add_vsep(curfrp);
979 /* Set width of neighbor frame */
980 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000981 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
982 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000985 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 if (before) /* new window left of current one */
987 {
988 wp->w_wincol = oldwin->w_wincol;
989 oldwin->w_wincol += new_size + 1;
990 }
991 else /* new window right of current one */
992 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
993 frame_fix_width(oldwin);
994 frame_fix_width(wp);
995 }
996 else
997#endif
998 {
999 /* width and column of new window is same as current window */
1000#ifdef FEAT_VERTSPLIT
1001 if (flags & (WSP_TOP | WSP_BOT))
1002 {
1003 wp->w_wincol = 0;
1004 wp->w_width = Columns;
1005 wp->w_vsep_width = 0;
1006 }
1007 else
1008 {
1009 wp->w_wincol = oldwin->w_wincol;
1010 wp->w_width = oldwin->w_width;
1011 wp->w_vsep_width = oldwin->w_vsep_width;
1012 }
1013 frp->fr_width = curfrp->fr_width;
1014#endif
1015
1016 /* "new_size" of the current window goes to the new window, use
1017 * one row for the status line */
1018 win_new_height(wp, new_size);
1019 if (flags & (WSP_TOP | WSP_BOT))
1020 frame_new_height(curfrp, curfrp->fr_height
1021 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1022 else
1023 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1024 if (before) /* new window above current one */
1025 {
1026 wp->w_winrow = oldwin->w_winrow;
1027 wp->w_status_height = STATUS_HEIGHT;
1028 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1029 }
1030 else /* new window below current one */
1031 {
1032 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1033 wp->w_status_height = oldwin->w_status_height;
1034 oldwin->w_status_height = STATUS_HEIGHT;
1035 }
1036#ifdef FEAT_VERTSPLIT
1037 if (flags & WSP_BOT)
1038 frame_add_statusline(curfrp);
1039#endif
1040 frame_fix_height(wp);
1041 frame_fix_height(oldwin);
1042 }
1043
1044 if (flags & (WSP_TOP | WSP_BOT))
1045 (void)win_comp_pos();
1046
1047 /*
1048 * Both windows need redrawing
1049 */
1050 redraw_win_later(wp, NOT_VALID);
1051 wp->w_redr_status = TRUE;
1052 redraw_win_later(oldwin, NOT_VALID);
1053 oldwin->w_redr_status = TRUE;
1054
1055 if (need_status)
1056 {
1057 msg_row = Rows - 1;
1058 msg_col = sc_col;
1059 msg_clr_eos_force(); /* Old command/ruler may still be there */
1060 comp_col();
1061 msg_row = Rows - 1;
1062 msg_col = 0; /* put position back at start of line */
1063 }
1064
1065 /*
1066 * make the new window the current window and redraw
1067 */
1068 if (do_equal || dir != 0)
1069 win_equal(wp, TRUE,
1070#ifdef FEAT_VERTSPLIT
1071 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1072 : dir == 'h' ? 'b' :
1073#endif
1074 'v');
1075
1076 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1077 * size was given. */
1078#ifdef FEAT_VERTSPLIT
1079 if (flags & WSP_VERT)
1080 {
1081 i = p_wiw;
1082 if (size != 0)
1083 p_wiw = size;
1084
1085# ifdef FEAT_GUI
1086 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1087 if (gui.in_use)
1088 gui_init_which_components(NULL);
1089# endif
1090 }
1091 else
1092#endif
1093 {
1094 i = p_wh;
1095 if (size != 0)
1096 p_wh = size;
1097 }
1098 win_enter(wp, FALSE);
1099#ifdef FEAT_VERTSPLIT
1100 if (flags & WSP_VERT)
1101 p_wiw = i;
1102 else
1103#endif
1104 p_wh = i;
1105
1106 return OK;
1107}
1108
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001109/*
1110 * Initialize window "newp" from window "oldp".
1111 * Used when splitting a window and when creating a new tab page.
1112 * The windows will both edit the same buffer.
1113 */
1114 static void
1115win_init(newp, oldp)
1116 win_T *newp;
1117 win_T *oldp;
1118{
1119 int i;
1120
1121 newp->w_buffer = oldp->w_buffer;
1122 oldp->w_buffer->b_nwindows++;
1123 newp->w_cursor = oldp->w_cursor;
1124 newp->w_valid = 0;
1125 newp->w_curswant = oldp->w_curswant;
1126 newp->w_set_curswant = oldp->w_set_curswant;
1127 newp->w_topline = oldp->w_topline;
1128#ifdef FEAT_DIFF
1129 newp->w_topfill = oldp->w_topfill;
1130#endif
1131 newp->w_leftcol = oldp->w_leftcol;
1132 newp->w_pcmark = oldp->w_pcmark;
1133 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1134 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001135 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001136 newp->w_fraction = oldp->w_fraction;
1137 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1138#ifdef FEAT_JUMPLIST
1139 copy_jumplist(oldp, newp);
1140#endif
1141#ifdef FEAT_QUICKFIX
1142 copy_loclist(oldp, newp);
1143#endif
1144 if (oldp->w_localdir != NULL)
1145 newp->w_localdir = vim_strsave(oldp->w_localdir);
1146
1147 /* Use the same argument list. */
1148 newp->w_alist = oldp->w_alist;
1149 ++newp->w_alist->al_refcount;
1150 newp->w_arg_idx = oldp->w_arg_idx;
1151
1152 /*
1153 * copy tagstack and options from existing window
1154 */
1155 for (i = 0; i < oldp->w_tagstacklen; i++)
1156 {
1157 newp->w_tagstack[i] = oldp->w_tagstack[i];
1158 if (newp->w_tagstack[i].tagname != NULL)
1159 newp->w_tagstack[i].tagname =
1160 vim_strsave(newp->w_tagstack[i].tagname);
1161 }
1162 newp->w_tagstackidx = oldp->w_tagstackidx;
1163 newp->w_tagstacklen = oldp->w_tagstacklen;
1164 win_copy_options(oldp, newp);
1165# ifdef FEAT_FOLDING
1166 copyFoldingState(oldp, newp);
1167# endif
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170#endif /* FEAT_WINDOWS */
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#if defined(FEAT_WINDOWS) || defined(PROTO)
1173/*
1174 * Check if "win" is a pointer to an existing window.
1175 */
1176 int
1177win_valid(win)
1178 win_T *win;
1179{
1180 win_T *wp;
1181
1182 if (win == NULL)
1183 return FALSE;
1184 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1185 if (wp == win)
1186 return TRUE;
1187 return FALSE;
1188}
1189
1190/*
1191 * Return the number of windows.
1192 */
1193 int
1194win_count()
1195{
1196 win_T *wp;
1197 int count = 0;
1198
1199 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1200 ++count;
1201 return count;
1202}
1203
1204/*
1205 * Make "count" windows on the screen.
1206 * Return actual number of windows on the screen.
1207 * Must be called when there is just one window, filling the whole screen
1208 * (excluding the command line).
1209 */
1210/*ARGSUSED*/
1211 int
1212make_windows(count, vertical)
1213 int count;
1214 int vertical; /* split windows vertically if TRUE */
1215{
1216 int maxcount;
1217 int todo;
1218
1219#ifdef FEAT_VERTSPLIT
1220 if (vertical)
1221 {
1222 /* Each windows needs at least 'winminwidth' lines and a separator
1223 * column. */
1224 maxcount = (curwin->w_width + curwin->w_vsep_width
1225 - (p_wiw - p_wmw)) / (p_wmw + 1);
1226 }
1227 else
1228#endif
1229 {
1230 /* Each window needs at least 'winminheight' lines and a status line. */
1231 maxcount = (curwin->w_height + curwin->w_status_height
1232 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1233 }
1234
1235 if (maxcount < 2)
1236 maxcount = 2;
1237 if (count > maxcount)
1238 count = maxcount;
1239
1240 /*
1241 * add status line now, otherwise first window will be too big
1242 */
1243 if (count > 1)
1244 last_status(TRUE);
1245
1246#ifdef FEAT_AUTOCMD
1247 /*
1248 * Don't execute autocommands while creating the windows. Must do that
1249 * when putting the buffers in the windows.
1250 */
1251 ++autocmd_block;
1252#endif
1253
1254 /* todo is number of windows left to create */
1255 for (todo = count - 1; todo > 0; --todo)
1256#ifdef FEAT_VERTSPLIT
1257 if (vertical)
1258 {
1259 if (win_split(curwin->w_width - (curwin->w_width - todo)
1260 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1261 break;
1262 }
1263 else
1264#endif
1265 {
1266 if (win_split(curwin->w_height - (curwin->w_height - todo
1267 * STATUS_HEIGHT) / (todo + 1)
1268 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1269 break;
1270 }
1271
1272#ifdef FEAT_AUTOCMD
1273 --autocmd_block;
1274#endif
1275
1276 /* return actual number of windows */
1277 return (count - todo);
1278}
1279
1280/*
1281 * Exchange current and next window
1282 */
1283 static void
1284win_exchange(Prenum)
1285 long Prenum;
1286{
1287 frame_T *frp;
1288 frame_T *frp2;
1289 win_T *wp;
1290 win_T *wp2;
1291 int temp;
1292
1293 if (lastwin == firstwin) /* just one window */
1294 {
1295 beep_flush();
1296 return;
1297 }
1298
1299#ifdef FEAT_GUI
1300 need_mouse_correct = TRUE;
1301#endif
1302
1303 /*
1304 * find window to exchange with
1305 */
1306 if (Prenum)
1307 {
1308 frp = curwin->w_frame->fr_parent->fr_child;
1309 while (frp != NULL && --Prenum > 0)
1310 frp = frp->fr_next;
1311 }
1312 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1313 frp = curwin->w_frame->fr_next;
1314 else /* Swap last window in row/col with previous */
1315 frp = curwin->w_frame->fr_prev;
1316
1317 /* We can only exchange a window with another window, not with a frame
1318 * containing windows. */
1319 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1320 return;
1321 wp = frp->fr_win;
1322
1323/*
1324 * 1. remove curwin from the list. Remember after which window it was in wp2
1325 * 2. insert curwin before wp in the list
1326 * if wp != wp2
1327 * 3. remove wp from the list
1328 * 4. insert wp after wp2
1329 * 5. exchange the status line height and vsep width.
1330 */
1331 wp2 = curwin->w_prev;
1332 frp2 = curwin->w_frame->fr_prev;
1333 if (wp->w_prev != curwin)
1334 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001335 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 frame_remove(curwin->w_frame);
1337 win_append(wp->w_prev, curwin);
1338 frame_insert(frp, curwin->w_frame);
1339 }
1340 if (wp != wp2)
1341 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001342 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 frame_remove(wp->w_frame);
1344 win_append(wp2, wp);
1345 if (frp2 == NULL)
1346 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1347 else
1348 frame_append(frp2, wp->w_frame);
1349 }
1350 temp = curwin->w_status_height;
1351 curwin->w_status_height = wp->w_status_height;
1352 wp->w_status_height = temp;
1353#ifdef FEAT_VERTSPLIT
1354 temp = curwin->w_vsep_width;
1355 curwin->w_vsep_width = wp->w_vsep_width;
1356 wp->w_vsep_width = temp;
1357
1358 /* If the windows are not in the same frame, exchange the sizes to avoid
1359 * messing up the window layout. Otherwise fix the frame sizes. */
1360 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1361 {
1362 temp = curwin->w_height;
1363 curwin->w_height = wp->w_height;
1364 wp->w_height = temp;
1365 temp = curwin->w_width;
1366 curwin->w_width = wp->w_width;
1367 wp->w_width = temp;
1368 }
1369 else
1370 {
1371 frame_fix_height(curwin);
1372 frame_fix_height(wp);
1373 frame_fix_width(curwin);
1374 frame_fix_width(wp);
1375 }
1376#endif
1377
1378 (void)win_comp_pos(); /* recompute window positions */
1379
1380 win_enter(wp, TRUE);
1381 redraw_later(CLEAR);
1382}
1383
1384/*
1385 * rotate windows: if upwards TRUE the second window becomes the first one
1386 * if upwards FALSE the first window becomes the second one
1387 */
1388 static void
1389win_rotate(upwards, count)
1390 int upwards;
1391 int count;
1392{
1393 win_T *wp1;
1394 win_T *wp2;
1395 frame_T *frp;
1396 int n;
1397
1398 if (firstwin == lastwin) /* nothing to do */
1399 {
1400 beep_flush();
1401 return;
1402 }
1403
1404#ifdef FEAT_GUI
1405 need_mouse_correct = TRUE;
1406#endif
1407
1408#ifdef FEAT_VERTSPLIT
1409 /* Check if all frames in this row/col have one window. */
1410 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1411 frp = frp->fr_next)
1412 if (frp->fr_win == NULL)
1413 {
1414 EMSG(_("E443: Cannot rotate when another window is split"));
1415 return;
1416 }
1417#endif
1418
1419 while (count--)
1420 {
1421 if (upwards) /* first window becomes last window */
1422 {
1423 /* remove first window/frame from the list */
1424 frp = curwin->w_frame->fr_parent->fr_child;
1425 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001426 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 frame_remove(frp);
1428
1429 /* find last frame and append removed window/frame after it */
1430 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1431 ;
1432 win_append(frp->fr_win, wp1);
1433 frame_append(frp, wp1->w_frame);
1434
1435 wp2 = frp->fr_win; /* previously last window */
1436 }
1437 else /* last window becomes first window */
1438 {
1439 /* find last window/frame in the list and remove it */
1440 for (frp = curwin->w_frame; frp->fr_next != NULL;
1441 frp = frp->fr_next)
1442 ;
1443 wp1 = frp->fr_win;
1444 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 frame_remove(frp);
1447
1448 /* append the removed window/frame before the first in the list */
1449 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1450 frame_insert(frp->fr_parent->fr_child, frp);
1451 }
1452
1453 /* exchange status height and vsep width of old and new last window */
1454 n = wp2->w_status_height;
1455 wp2->w_status_height = wp1->w_status_height;
1456 wp1->w_status_height = n;
1457 frame_fix_height(wp1);
1458 frame_fix_height(wp2);
1459#ifdef FEAT_VERTSPLIT
1460 n = wp2->w_vsep_width;
1461 wp2->w_vsep_width = wp1->w_vsep_width;
1462 wp1->w_vsep_width = n;
1463 frame_fix_width(wp1);
1464 frame_fix_width(wp2);
1465#endif
1466
1467 /* recompute w_winrow and w_wincol for all windows */
1468 (void)win_comp_pos();
1469 }
1470
1471 redraw_later(CLEAR);
1472}
1473
1474/*
1475 * Move the current window to the very top/bottom/left/right of the screen.
1476 */
1477 static void
1478win_totop(size, flags)
1479 int size;
1480 int flags;
1481{
1482 int dir;
1483 int height = curwin->w_height;
1484
1485 if (lastwin == firstwin)
1486 {
1487 beep_flush();
1488 return;
1489 }
1490
1491 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001492 (void)winframe_remove(curwin, &dir, NULL);
1493 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 last_status(FALSE); /* may need to remove last status line */
1495 (void)win_comp_pos(); /* recompute window positions */
1496
1497 /* Split a window on the desired side and put the window there. */
1498 (void)win_split_ins(size, flags, curwin, dir);
1499 if (!(flags & WSP_VERT))
1500 {
1501 win_setheight(height);
1502 if (p_ea)
1503 win_equal(curwin, TRUE, 'v');
1504 }
1505
1506#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1507 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1508 * scrollbars. Have to update them anyway. */
1509 if (gui.in_use)
1510 {
1511 out_flush();
1512 gui_init_which_components(NULL);
1513 gui_update_scrollbars(TRUE);
1514 }
1515 need_mouse_correct = TRUE;
1516#endif
1517
1518}
1519
1520/*
1521 * Move window "win1" to below/right of "win2" and make "win1" the current
1522 * window. Only works within the same frame!
1523 */
1524 void
1525win_move_after(win1, win2)
1526 win_T *win1, *win2;
1527{
1528 int height;
1529
1530 /* check if the arguments are reasonable */
1531 if (win1 == win2)
1532 return;
1533
1534 /* check if there is something to do */
1535 if (win2->w_next != win1)
1536 {
1537 /* may need move the status line/vertical separator of the last window
1538 * */
1539 if (win1 == lastwin)
1540 {
1541 height = win1->w_prev->w_status_height;
1542 win1->w_prev->w_status_height = win1->w_status_height;
1543 win1->w_status_height = height;
1544#ifdef FEAT_VERTSPLIT
1545 win1->w_prev->w_vsep_width = 0;
1546 win1->w_vsep_width = 1;
1547#endif
1548 }
1549 else if (win2 == lastwin)
1550 {
1551 height = win1->w_status_height;
1552 win1->w_status_height = win2->w_status_height;
1553 win2->w_status_height = height;
1554#ifdef FEAT_VERTSPLIT
1555 win2->w_vsep_width = 1;
1556 win1->w_vsep_width = 0;
1557#endif
1558 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001559 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 frame_remove(win1->w_frame);
1561 win_append(win2, win1);
1562 frame_append(win2->w_frame, win1->w_frame);
1563
1564 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1565 redraw_later(NOT_VALID);
1566 }
1567 win_enter(win1, FALSE);
1568}
1569
1570/*
1571 * Make all windows the same height.
1572 * 'next_curwin' will soon be the current window, make sure it has enough
1573 * rows.
1574 */
1575 void
1576win_equal(next_curwin, current, dir)
1577 win_T *next_curwin; /* pointer to current window to be or NULL */
1578 int current; /* do only frame with current window */
1579 int dir; /* 'v' for vertically, 'h' for horizontally,
1580 'b' for both, 0 for using p_ead */
1581{
1582 if (dir == 0)
1583#ifdef FEAT_VERTSPLIT
1584 dir = *p_ead;
1585#else
1586 dir = 'b';
1587#endif
1588 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001589 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001590 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591}
1592
1593/*
1594 * Set a frame to a new position and height, spreading the available room
1595 * equally over contained frames.
1596 * The window "next_curwin" (if not NULL) should at least get the size from
1597 * 'winheight' and 'winwidth' if possible.
1598 */
1599 static void
1600win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1601 win_T *next_curwin; /* pointer to current window to be or NULL */
1602 int current; /* do only frame with current window */
1603 frame_T *topfr; /* frame to set size off */
1604 int dir; /* 'v', 'h' or 'b', see win_equal() */
1605 int col; /* horizontal position for frame */
1606 int row; /* vertical position for frame */
1607 int width; /* new width of frame */
1608 int height; /* new height of frame */
1609{
1610 int n, m;
1611 int extra_sep = 0;
1612 int wincount, totwincount = 0;
1613 frame_T *fr;
1614 int next_curwin_size = 0;
1615 int room = 0;
1616 int new_size;
1617 int has_next_curwin = 0;
1618 int hnc;
1619
1620 if (topfr->fr_layout == FR_LEAF)
1621 {
1622 /* Set the width/height of this frame.
1623 * Redraw when size or position changes */
1624 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1625#ifdef FEAT_VERTSPLIT
1626 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1627#endif
1628 )
1629 {
1630 topfr->fr_win->w_winrow = row;
1631 frame_new_height(topfr, height, FALSE, FALSE);
1632#ifdef FEAT_VERTSPLIT
1633 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001634 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
1636 redraw_all_later(CLEAR);
1637 }
1638 }
1639#ifdef FEAT_VERTSPLIT
1640 else if (topfr->fr_layout == FR_ROW)
1641 {
1642 topfr->fr_width = width;
1643 topfr->fr_height = height;
1644
1645 if (dir != 'v') /* equalize frame widths */
1646 {
1647 /* Compute the maximum number of windows horizontally in this
1648 * frame. */
1649 n = frame_minwidth(topfr, NOWIN);
1650 /* add one for the rightmost window, it doesn't have a separator */
1651 if (col + width == Columns)
1652 extra_sep = 1;
1653 else
1654 extra_sep = 0;
1655 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001656 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001658 /*
1659 * Compute width for "next_curwin" window and room available for
1660 * other windows.
1661 * "m" is the minimal width when counting p_wiw for "next_curwin".
1662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 m = frame_minwidth(topfr, next_curwin);
1664 room = width - m;
1665 if (room < 0)
1666 {
1667 next_curwin_size = p_wiw + room;
1668 room = 0;
1669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 else
1671 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001672 next_curwin_size = -1;
1673 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1674 {
1675 /* If 'winfixwidth' set keep the window width if
1676 * possible.
1677 * Watch out for this window being the next_curwin. */
1678 if (frame_fixed_width(fr))
1679 {
1680 n = frame_minwidth(fr, NOWIN);
1681 new_size = fr->fr_width;
1682 if (frame_has_win(fr, next_curwin))
1683 {
1684 room += p_wiw - p_wmw;
1685 next_curwin_size = 0;
1686 if (new_size < p_wiw)
1687 new_size = p_wiw;
1688 }
1689 else
1690 /* These windows don't use up room. */
1691 totwincount -= (n + (fr->fr_next == NULL
1692 ? extra_sep : 0)) / (p_wmw + 1);
1693 room -= new_size - n;
1694 if (room < 0)
1695 {
1696 new_size += room;
1697 room = 0;
1698 }
1699 fr->fr_newwidth = new_size;
1700 }
1701 }
1702 if (next_curwin_size == -1)
1703 {
1704 if (!has_next_curwin)
1705 next_curwin_size = 0;
1706 else if (totwincount > 1
1707 && (room + (totwincount - 2))
1708 / (totwincount - 1) > p_wiw)
1709 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001710 /* Can make all windows wider than 'winwidth', spread
1711 * the room equally. */
1712 next_curwin_size = (room + p_wiw
1713 + (totwincount - 1) * p_wmw
1714 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001715 room -= next_curwin_size - p_wiw;
1716 }
1717 else
1718 next_curwin_size = p_wiw;
1719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001721
1722 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 --totwincount; /* don't count curwin */
1724 }
1725
1726 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1727 {
1728 n = m = 0;
1729 wincount = 1;
1730 if (fr->fr_next == NULL)
1731 /* last frame gets all that remains (avoid roundoff error) */
1732 new_size = width;
1733 else if (dir == 'v')
1734 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001735 else if (frame_fixed_width(fr))
1736 {
1737 new_size = fr->fr_newwidth;
1738 wincount = 0; /* doesn't count as a sizeable window */
1739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 else
1741 {
1742 /* Compute the maximum number of windows horiz. in "fr". */
1743 n = frame_minwidth(fr, NOWIN);
1744 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1745 / (p_wmw + 1);
1746 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001747 if (has_next_curwin)
1748 hnc = frame_has_win(fr, next_curwin);
1749 else
1750 hnc = FALSE;
1751 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001753 if (totwincount == 0)
1754 new_size = room;
1755 else
1756 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001758 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 next_curwin_size -= p_wiw - (m - n);
1761 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001762 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001764 else
1765 room -= new_size;
1766 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001769 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 * window, unless equalizing all frames. */
1771 if (!current || dir != 'v' || topfr->fr_parent != NULL
1772 || (new_size != fr->fr_width)
1773 || frame_has_win(fr, next_curwin))
1774 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001775 new_size, height);
1776 col += new_size;
1777 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 totwincount -= wincount;
1779 }
1780 }
1781#endif
1782 else /* topfr->fr_layout == FR_COL */
1783 {
1784#ifdef FEAT_VERTSPLIT
1785 topfr->fr_width = width;
1786#endif
1787 topfr->fr_height = height;
1788
1789 if (dir != 'h') /* equalize frame heights */
1790 {
1791 /* Compute maximum number of windows vertically in this frame. */
1792 n = frame_minheight(topfr, NOWIN);
1793 /* add one for the bottom window if it doesn't have a statusline */
1794 if (row + height == cmdline_row && p_ls == 0)
1795 extra_sep = 1;
1796 else
1797 extra_sep = 0;
1798 totwincount = (n + extra_sep) / (p_wmh + 1);
1799 has_next_curwin = frame_has_win(topfr, next_curwin);
1800
1801 /*
1802 * Compute height for "next_curwin" window and room available for
1803 * other windows.
1804 * "m" is the minimal height when counting p_wh for "next_curwin".
1805 */
1806 m = frame_minheight(topfr, next_curwin);
1807 room = height - m;
1808 if (room < 0)
1809 {
1810 /* The room is less then 'winheight', use all space for the
1811 * current window. */
1812 next_curwin_size = p_wh + room;
1813 room = 0;
1814 }
1815 else
1816 {
1817 next_curwin_size = -1;
1818 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1819 {
1820 /* If 'winfixheight' set keep the window height if
1821 * possible.
1822 * Watch out for this window being the next_curwin. */
1823 if (frame_fixed_height(fr))
1824 {
1825 n = frame_minheight(fr, NOWIN);
1826 new_size = fr->fr_height;
1827 if (frame_has_win(fr, next_curwin))
1828 {
1829 room += p_wh - p_wmh;
1830 next_curwin_size = 0;
1831 if (new_size < p_wh)
1832 new_size = p_wh;
1833 }
1834 else
1835 /* These windows don't use up room. */
1836 totwincount -= (n + (fr->fr_next == NULL
1837 ? extra_sep : 0)) / (p_wmh + 1);
1838 room -= new_size - n;
1839 if (room < 0)
1840 {
1841 new_size += room;
1842 room = 0;
1843 }
1844 fr->fr_newheight = new_size;
1845 }
1846 }
1847 if (next_curwin_size == -1)
1848 {
1849 if (!has_next_curwin)
1850 next_curwin_size = 0;
1851 else if (totwincount > 1
1852 && (room + (totwincount - 2))
1853 / (totwincount - 1) > p_wh)
1854 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001855 /* can make all windows higher than 'winheight',
1856 * spread the room equally. */
1857 next_curwin_size = (room + p_wh
1858 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 + (totwincount - 1)) / totwincount;
1860 room -= next_curwin_size - p_wh;
1861 }
1862 else
1863 next_curwin_size = p_wh;
1864 }
1865 }
1866
1867 if (has_next_curwin)
1868 --totwincount; /* don't count curwin */
1869 }
1870
1871 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1872 {
1873 n = m = 0;
1874 wincount = 1;
1875 if (fr->fr_next == NULL)
1876 /* last frame gets all that remains (avoid roundoff error) */
1877 new_size = height;
1878 else if (dir == 'h')
1879 new_size = fr->fr_height;
1880 else if (frame_fixed_height(fr))
1881 {
1882 new_size = fr->fr_newheight;
1883 wincount = 0; /* doesn't count as a sizeable window */
1884 }
1885 else
1886 {
1887 /* Compute the maximum number of windows vert. in "fr". */
1888 n = frame_minheight(fr, NOWIN);
1889 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1890 / (p_wmh + 1);
1891 m = frame_minheight(fr, next_curwin);
1892 if (has_next_curwin)
1893 hnc = frame_has_win(fr, next_curwin);
1894 else
1895 hnc = FALSE;
1896 if (hnc) /* don't count next_curwin */
1897 --wincount;
1898 if (totwincount == 0)
1899 new_size = room;
1900 else
1901 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1902 / totwincount;
1903 if (hnc) /* add next_curwin size */
1904 {
1905 next_curwin_size -= p_wh - (m - n);
1906 new_size += next_curwin_size;
1907 room -= new_size - next_curwin_size;
1908 }
1909 else
1910 room -= new_size;
1911 new_size += n;
1912 }
1913 /* Skip frame that is full width when splitting or closing a
1914 * window, unless equalizing all frames. */
1915 if (!current || dir != 'h' || topfr->fr_parent != NULL
1916 || (new_size != fr->fr_height)
1917 || frame_has_win(fr, next_curwin))
1918 win_equal_rec(next_curwin, current, fr, dir, col, row,
1919 width, new_size);
1920 row += new_size;
1921 height -= new_size;
1922 totwincount -= wincount;
1923 }
1924 }
1925}
1926
1927/*
1928 * close all windows for buffer 'buf'
1929 */
1930 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001931close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001933 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001935 win_T *wp;
1936 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001937 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001940
1941 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001943 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001945 win_close(wp, FALSE);
1946
1947 /* Start all over, autocommands may change the window layout. */
1948 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001951 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001953
1954 /* Also check windows in other tab pages. */
1955 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1956 {
1957 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001958 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001959 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1960 if (wp->w_buffer == buf)
1961 {
1962 win_close_othertab(wp, FALSE, tp);
1963
1964 /* Start all over, the tab page may be closed and
1965 * autocommands may change the window layout. */
1966 nexttp = first_tabpage;
1967 break;
1968 }
1969 }
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001972
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001973 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001974 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975}
1976
1977/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001978 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001979 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001980 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001981 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001982last_window()
1983{
1984 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1985}
1986
1987/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001988 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 * If "free_buf" is TRUE related buffer may be unloaded.
1990 *
1991 * called by :quit, :close, :xit, :wq and findtag()
1992 */
1993 void
1994win_close(win, free_buf)
1995 win_T *win;
1996 int free_buf;
1997{
1998 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001999 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef FEAT_AUTOCMD
2001 int other_buffer = FALSE;
2002#endif
2003 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 int dir;
2005 int help_window = FALSE;
2006
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002007 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
2009 EMSG(_("E444: Cannot close last window"));
2010 return;
2011 }
2012
2013 /* When closing the help window, try restoring a snapshot after closing
2014 * the window. Otherwise clear the snapshot, it's now invalid. */
2015 if (win->w_buffer->b_help)
2016 help_window = TRUE;
2017 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002018 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020#ifdef FEAT_AUTOCMD
2021 if (win == curwin)
2022 {
2023 /*
2024 * Guess which window is going to be the new current window.
2025 * This may change because of the autocommands (sigh).
2026 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /*
2030 * Be careful: If autocommands delete the window, return now.
2031 */
2032 if (wp->w_buffer != curbuf)
2033 {
2034 other_buffer = TRUE;
2035 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002036 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return;
2038 }
2039 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002040 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return;
2042# ifdef FEAT_EVAL
2043 /* autocmds may abort script processing */
2044 if (aborting())
2045 return;
2046# endif
2047 }
2048#endif
2049
2050 /*
2051 * Close the link to the buffer.
2052 */
2053 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2054 /* Autocommands may have closed the window already, or closed the only
2055 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002056 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 return;
2058
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002059 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002062 /* When closing the last window in a tab page go to another tab page. */
2063 if (wp == NULL)
2064 {
2065 tabpage_T *ptp = NULL;
2066 tabpage_T *tp;
2067 tabpage_T *atp = alt_tabpage();
2068
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002069 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002070 ptp = tp;
2071 if (tp == NULL)
2072 {
2073 EMSG2(_(e_intern2), "win_close()");
2074 return;
2075 }
2076 if (ptp == NULL)
2077 first_tabpage = tp->tp_next;
2078 else
2079 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002080 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002081
2082 /* We don't do the window resizing stuff, let enter_tabpage() take
2083 * care of entering a window in another tab page. */
2084 enter_tabpage(atp, old_curbuf);
2085 return;
2086 }
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 /* Make sure curwin isn't invalid. It can cause severe trouble when
2089 * printing an error message. For win_equal() curbuf needs to be valid
2090 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002091 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 curwin = wp;
2094#ifdef FEAT_QUICKFIX
2095 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2096 {
2097 /*
2098 * The cursor goes to the preview or the quickfix window, try
2099 * finding another window to go to.
2100 */
2101 for (;;)
2102 {
2103 if (wp->w_next == NULL)
2104 wp = firstwin;
2105 else
2106 wp = wp->w_next;
2107 if (wp == curwin)
2108 break;
2109 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2110 {
2111 curwin = wp;
2112 break;
2113 }
2114 }
2115 }
2116#endif
2117 curbuf = curwin->w_buffer;
2118 close_curwin = TRUE;
2119 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002120 if (p_ea
2121#ifdef FEAT_VERTSPLIT
2122 && (*p_ead == 'b' || *p_ead == dir)
2123#endif
2124 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 win_equal(curwin, TRUE,
2126#ifdef FEAT_VERTSPLIT
2127 dir
2128#else
2129 0
2130#endif
2131 );
2132 else
2133 win_comp_pos();
2134 if (close_curwin)
2135 {
2136 win_enter_ext(wp, FALSE, TRUE);
2137#ifdef FEAT_AUTOCMD
2138 if (other_buffer)
2139 /* careful: after this wp and win may be invalid! */
2140 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2141#endif
2142 }
2143
2144 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002145 * If last window has a status line now and we don't want one,
2146 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 */
2148 last_status(FALSE);
2149
2150 /* After closing the help window, try restoring the window layout from
2151 * before it was opened. */
2152 if (help_window)
2153 restore_snapshot(close_curwin);
2154
2155#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2156 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2157 if (gui.in_use && !win_hasvertsplit())
2158 gui_init_which_components(NULL);
2159#endif
2160
2161 redraw_all_later(NOT_VALID);
2162}
2163
2164/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002165 * Close window "win" in tab page "tp", which is not the current tab page.
2166 * This may be the last window ih that tab page and result in closing the tab,
2167 * thus "tp" may become invalid!
2168 * Called must check if buffer is hidden.
2169 */
2170 void
2171win_close_othertab(win, free_buf, tp)
2172 win_T *win;
2173 int free_buf;
2174 tabpage_T *tp;
2175{
2176 win_T *wp;
2177 int dir;
2178 tabpage_T *ptp = NULL;
2179
2180 /* Close the link to the buffer. */
2181 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2182
2183 /* Careful: Autocommands may have closed the tab page or made it the
2184 * current tab page. */
2185 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2186 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002187 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002188 return;
2189
2190 /* Autocommands may have closed the window already. */
2191 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2192 ;
2193 if (wp == NULL)
2194 return;
2195
2196 /* Free the memory used for the window. */
2197 wp = win_free_mem(win, &dir, tp);
2198
2199 /* When closing the last window in a tab page remove the tab page. */
2200 if (wp == NULL)
2201 {
2202 if (tp == first_tabpage)
2203 first_tabpage = tp->tp_next;
2204 else
2205 {
2206 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2207 ptp = ptp->tp_next)
2208 ;
2209 if (ptp == NULL)
2210 {
2211 EMSG2(_(e_intern2), "win_close_othertab()");
2212 return;
2213 }
2214 ptp->tp_next = tp->tp_next;
2215 }
2216 vim_free(tp);
2217 }
2218}
2219
2220/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002221 * Free the memory used for a window.
2222 * Returns a pointer to the window that got the freed up space.
2223 */
2224 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002225win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002226 win_T *win;
2227 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002228 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002229{
2230 frame_T *frp;
2231 win_T *wp;
2232
Bram Moolenaarea408852005-06-25 22:49:46 +00002233#ifdef FEAT_FOLDING
2234 clearFolding(win);
2235#endif
2236
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002237 /* reduce the reference count to the argument list. */
2238 alist_unlink(win->w_alist);
2239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002240 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002241 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002242 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002243 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002244 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002245
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002246 /* When deleting the current window of another tab page select a new
2247 * current window. */
2248 if (tp != NULL && win == tp->tp_curwin)
2249 tp->tp_curwin = wp;
2250
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002251 return wp;
2252}
2253
2254#if defined(EXITFREE) || defined(PROTO)
2255 void
2256win_free_all()
2257{
2258 int dummy;
2259
Bram Moolenaarf740b292006-02-16 22:11:02 +00002260# ifdef FEAT_WINDOWS
2261 while (first_tabpage->tp_next != NULL)
2262 tabpage_close(TRUE);
2263# endif
2264
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002265 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002266 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002267}
2268#endif
2269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Remove a window and its frame from the tree of frames.
2272 * Returns a pointer to the window that got the freed up space.
2273 */
2274/*ARGSUSED*/
2275 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002276winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 win_T *win;
2278 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002279 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 frame_T *frp, *frp2, *frp3;
2282 frame_T *frp_close = win->w_frame;
2283 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002284 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
2286 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002287 * If there is only one window there is nothing to remove.
2288 */
2289 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2290 return NULL;
2291
2292 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 * Remove the window from its frame.
2294 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002295 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 wp = frame2win(frp2);
2297
2298 /* Remove this frame from the list of frames. */
2299 frame_remove(frp_close);
2300
2301#ifdef FEAT_VERTSPLIT
2302 if (frp_close->fr_parent->fr_layout == FR_COL)
2303 {
2304#endif
2305 /* When 'winfixheight' is set, remember its old size and restore
2306 * it later (it's a simplistic solution...). Don't do this if the
2307 * window will occupy the full height of the screen. */
2308 if (frp2->fr_win != NULL
2309 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2310 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002311 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2313 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002314 if (old_size != 0)
2315 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_VERTSPLIT
2317 *dirp = 'v';
2318 }
2319 else
2320 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002321 /* When 'winfixwidth' is set, remember its old size and restore
2322 * it later (it's a simplistic solution...). Don't do this if the
2323 * window will occupy the full width of the screen. */
2324 if (frp2->fr_win != NULL
2325 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2326 && frp2->fr_win->w_p_wfw)
2327 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002329 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2330 if (old_size != 0)
2331 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 *dirp = 'h';
2333 }
2334#endif
2335
2336 /* If rows/columns go to a window below/right its positions need to be
2337 * updated. Can only be done after the sizes have been updated. */
2338 if (frp2 == frp_close->fr_next)
2339 {
2340 int row = win->w_winrow;
2341 int col = W_WINCOL(win);
2342
2343 frame_comp_pos(frp2, &row, &col);
2344 }
2345
2346 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2347 {
2348 /* There is no other frame in this list, move its info to the parent
2349 * and remove it. */
2350 frp2->fr_parent->fr_layout = frp2->fr_layout;
2351 frp2->fr_parent->fr_child = frp2->fr_child;
2352 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2353 frp->fr_parent = frp2->fr_parent;
2354 frp2->fr_parent->fr_win = frp2->fr_win;
2355 if (frp2->fr_win != NULL)
2356 frp2->fr_win->w_frame = frp2->fr_parent;
2357 frp = frp2->fr_parent;
2358 vim_free(frp2);
2359
2360 frp2 = frp->fr_parent;
2361 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2362 {
2363 /* The frame above the parent has the same layout, have to merge
2364 * the frames into this list. */
2365 if (frp2->fr_child == frp)
2366 frp2->fr_child = frp->fr_child;
2367 frp->fr_child->fr_prev = frp->fr_prev;
2368 if (frp->fr_prev != NULL)
2369 frp->fr_prev->fr_next = frp->fr_child;
2370 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2371 {
2372 frp3->fr_parent = frp2;
2373 if (frp3->fr_next == NULL)
2374 {
2375 frp3->fr_next = frp->fr_next;
2376 if (frp->fr_next != NULL)
2377 frp->fr_next->fr_prev = frp3;
2378 break;
2379 }
2380 }
2381 vim_free(frp);
2382 }
2383 }
2384
2385 return wp;
2386}
2387
2388/*
2389 * Find out which frame is going to get the freed up space when "win" is
2390 * closed.
2391 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2392 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2393 * This makes opening a window and closing it immediately keep the same window
2394 * layout.
2395 */
2396 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002397win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002399 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401 frame_T *frp;
2402 int b;
2403
Bram Moolenaarf740b292006-02-16 22:11:02 +00002404 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002405 /* Last window in this tab page, will go to next tab page. */
2406 return alt_tabpage()->tp_curwin->w_frame;
2407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 frp = win->w_frame;
2409#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002410 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 b = p_spr;
2412 else
2413#endif
2414 b = p_sb;
2415 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2416 return frp->fr_next;
2417 return frp->fr_prev;
2418}
2419
2420/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002421 * Return the tabpage that will be used if the current one is closed.
2422 */
2423 static tabpage_T *
2424alt_tabpage()
2425{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002426 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002427
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002428 /* Use the next tab page if possible. */
2429 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002430 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002431
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002432 /* Find the last but one tab page. */
2433 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2434 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002435 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002436}
2437
2438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * Find the left-upper window in frame "frp".
2440 */
2441 static win_T *
2442frame2win(frp)
2443 frame_T *frp;
2444{
2445 while (frp->fr_win == NULL)
2446 frp = frp->fr_child;
2447 return frp->fr_win;
2448}
2449
2450/*
2451 * Return TRUE if frame "frp" contains window "wp".
2452 */
2453 static int
2454frame_has_win(frp, wp)
2455 frame_T *frp;
2456 win_T *wp;
2457{
2458 frame_T *p;
2459
2460 if (frp->fr_layout == FR_LEAF)
2461 return frp->fr_win == wp;
2462
2463 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2464 if (frame_has_win(p, wp))
2465 return TRUE;
2466 return FALSE;
2467}
2468
2469/*
2470 * Set a new height for a frame. Recursively sets the height for contained
2471 * frames and windows. Caller must take care of positions.
2472 */
2473 static void
2474frame_new_height(topfrp, height, topfirst, wfh)
2475 frame_T *topfrp;
2476 int height;
2477 int topfirst; /* resize topmost contained frame first */
2478 int wfh; /* obey 'winfixheight' when there is a choice;
2479 may cause the height not to be set */
2480{
2481 frame_T *frp;
2482 int extra_lines;
2483 int h;
2484
2485 if (topfrp->fr_win != NULL)
2486 {
2487 /* Simple case: just one window. */
2488 win_new_height(topfrp->fr_win,
2489 height - topfrp->fr_win->w_status_height);
2490 }
2491#ifdef FEAT_VERTSPLIT
2492 else if (topfrp->fr_layout == FR_ROW)
2493 {
2494 do
2495 {
2496 /* All frames in this row get the same new height. */
2497 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2498 {
2499 frame_new_height(frp, height, topfirst, wfh);
2500 if (frp->fr_height > height)
2501 {
2502 /* Could not fit the windows, make the whole row higher. */
2503 height = frp->fr_height;
2504 break;
2505 }
2506 }
2507 }
2508 while (frp != NULL);
2509 }
2510#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002511 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
2513 /* Complicated case: Resize a column of frames. Resize the bottom
2514 * frame first, frames above that when needed. */
2515
2516 frp = topfrp->fr_child;
2517 if (wfh)
2518 /* Advance past frames with one window with 'wfh' set. */
2519 while (frame_fixed_height(frp))
2520 {
2521 frp = frp->fr_next;
2522 if (frp == NULL)
2523 return; /* no frame without 'wfh', give up */
2524 }
2525 if (!topfirst)
2526 {
2527 /* Find the bottom frame of this column */
2528 while (frp->fr_next != NULL)
2529 frp = frp->fr_next;
2530 if (wfh)
2531 /* Advance back for frames with one window with 'wfh' set. */
2532 while (frame_fixed_height(frp))
2533 frp = frp->fr_prev;
2534 }
2535
2536 extra_lines = height - topfrp->fr_height;
2537 if (extra_lines < 0)
2538 {
2539 /* reduce height of contained frames, bottom or top frame first */
2540 while (frp != NULL)
2541 {
2542 h = frame_minheight(frp, NULL);
2543 if (frp->fr_height + extra_lines < h)
2544 {
2545 extra_lines += frp->fr_height - h;
2546 frame_new_height(frp, h, topfirst, wfh);
2547 }
2548 else
2549 {
2550 frame_new_height(frp, frp->fr_height + extra_lines,
2551 topfirst, wfh);
2552 break;
2553 }
2554 if (topfirst)
2555 {
2556 do
2557 frp = frp->fr_next;
2558 while (wfh && frp != NULL && frame_fixed_height(frp));
2559 }
2560 else
2561 {
2562 do
2563 frp = frp->fr_prev;
2564 while (wfh && frp != NULL && frame_fixed_height(frp));
2565 }
2566 /* Increase "height" if we could not reduce enough frames. */
2567 if (frp == NULL)
2568 height -= extra_lines;
2569 }
2570 }
2571 else if (extra_lines > 0)
2572 {
2573 /* increase height of bottom or top frame */
2574 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2575 }
2576 }
2577 topfrp->fr_height = height;
2578}
2579
2580/*
2581 * Return TRUE if height of frame "frp" should not be changed because of
2582 * the 'winfixheight' option.
2583 */
2584 static int
2585frame_fixed_height(frp)
2586 frame_T *frp;
2587{
2588 /* frame with one window: fixed height if 'winfixheight' set. */
2589 if (frp->fr_win != NULL)
2590 return frp->fr_win->w_p_wfh;
2591
2592 if (frp->fr_layout == FR_ROW)
2593 {
2594 /* The frame is fixed height if one of the frames in the row is fixed
2595 * height. */
2596 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2597 if (frame_fixed_height(frp))
2598 return TRUE;
2599 return FALSE;
2600 }
2601
2602 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2603 * frames in the row are fixed height. */
2604 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2605 if (!frame_fixed_height(frp))
2606 return FALSE;
2607 return TRUE;
2608}
2609
2610#ifdef FEAT_VERTSPLIT
2611/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002612 * Return TRUE if width of frame "frp" should not be changed because of
2613 * the 'winfixwidth' option.
2614 */
2615 static int
2616frame_fixed_width(frp)
2617 frame_T *frp;
2618{
2619 /* frame with one window: fixed width if 'winfixwidth' set. */
2620 if (frp->fr_win != NULL)
2621 return frp->fr_win->w_p_wfw;
2622
2623 if (frp->fr_layout == FR_COL)
2624 {
2625 /* The frame is fixed width if one of the frames in the row is fixed
2626 * width. */
2627 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2628 if (frame_fixed_width(frp))
2629 return TRUE;
2630 return FALSE;
2631 }
2632
2633 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2634 * frames in the row are fixed width. */
2635 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2636 if (!frame_fixed_width(frp))
2637 return FALSE;
2638 return TRUE;
2639}
2640
2641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 * Add a status line to windows at the bottom of "frp".
2643 * Note: Does not check if there is room!
2644 */
2645 static void
2646frame_add_statusline(frp)
2647 frame_T *frp;
2648{
2649 win_T *wp;
2650
2651 if (frp->fr_layout == FR_LEAF)
2652 {
2653 wp = frp->fr_win;
2654 if (wp->w_status_height == 0)
2655 {
2656 if (wp->w_height > 0) /* don't make it negative */
2657 --wp->w_height;
2658 wp->w_status_height = STATUS_HEIGHT;
2659 }
2660 }
2661 else if (frp->fr_layout == FR_ROW)
2662 {
2663 /* Handle all the frames in the row. */
2664 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2665 frame_add_statusline(frp);
2666 }
2667 else /* frp->fr_layout == FR_COL */
2668 {
2669 /* Only need to handle the last frame in the column. */
2670 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2671 ;
2672 frame_add_statusline(frp);
2673 }
2674}
2675
2676/*
2677 * Set width of a frame. Handles recursively going through contained frames.
2678 * May remove separator line for windows at the right side (for win_close()).
2679 */
2680 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002681frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 frame_T *topfrp;
2683 int width;
2684 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002685 int wfw; /* obey 'winfixwidth' when there is a choice;
2686 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 frame_T *frp;
2689 int extra_cols;
2690 int w;
2691 win_T *wp;
2692
2693 if (topfrp->fr_layout == FR_LEAF)
2694 {
2695 /* Simple case: just one window. */
2696 wp = topfrp->fr_win;
2697 /* Find out if there are any windows right of this one. */
2698 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2699 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2700 break;
2701 if (frp->fr_parent == NULL)
2702 wp->w_vsep_width = 0;
2703 win_new_width(wp, width - wp->w_vsep_width);
2704 }
2705 else if (topfrp->fr_layout == FR_COL)
2706 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002707 do
2708 {
2709 /* All frames in this column get the same new width. */
2710 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2711 {
2712 frame_new_width(frp, width, leftfirst, wfw);
2713 if (frp->fr_width > width)
2714 {
2715 /* Could not fit the windows, make whole column wider. */
2716 width = frp->fr_width;
2717 break;
2718 }
2719 }
2720 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 else /* fr_layout == FR_ROW */
2723 {
2724 /* Complicated case: Resize a row of frames. Resize the rightmost
2725 * frame first, frames left of it when needed. */
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002728 if (wfw)
2729 /* Advance past frames with one window with 'wfw' set. */
2730 while (frame_fixed_width(frp))
2731 {
2732 frp = frp->fr_next;
2733 if (frp == NULL)
2734 return; /* no frame without 'wfw', give up */
2735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002737 {
2738 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 while (frp->fr_next != NULL)
2740 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002741 if (wfw)
2742 /* Advance back for frames with one window with 'wfw' set. */
2743 while (frame_fixed_width(frp))
2744 frp = frp->fr_prev;
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 extra_cols = width - topfrp->fr_width;
2748 if (extra_cols < 0)
2749 {
2750 /* reduce frame width, rightmost frame first */
2751 while (frp != NULL)
2752 {
2753 w = frame_minwidth(frp, NULL);
2754 if (frp->fr_width + extra_cols < w)
2755 {
2756 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002757 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759 else
2760 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002761 frame_new_width(frp, frp->fr_width + extra_cols,
2762 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 break;
2764 }
2765 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002766 {
2767 do
2768 frp = frp->fr_next;
2769 while (wfw && frp != NULL && frame_fixed_width(frp));
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002772 {
2773 do
2774 frp = frp->fr_prev;
2775 while (wfw && frp != NULL && frame_fixed_width(frp));
2776 }
2777 /* Increase "width" if we could not reduce enough frames. */
2778 if (frp == NULL)
2779 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 }
2782 else if (extra_cols > 0)
2783 {
2784 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002785 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
2787 }
2788 topfrp->fr_width = width;
2789}
2790
2791/*
2792 * Add the vertical separator to windows at the right side of "frp".
2793 * Note: Does not check if there is room!
2794 */
2795 static void
2796frame_add_vsep(frp)
2797 frame_T *frp;
2798{
2799 win_T *wp;
2800
2801 if (frp->fr_layout == FR_LEAF)
2802 {
2803 wp = frp->fr_win;
2804 if (wp->w_vsep_width == 0)
2805 {
2806 if (wp->w_width > 0) /* don't make it negative */
2807 --wp->w_width;
2808 wp->w_vsep_width = 1;
2809 }
2810 }
2811 else if (frp->fr_layout == FR_COL)
2812 {
2813 /* Handle all the frames in the column. */
2814 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2815 frame_add_vsep(frp);
2816 }
2817 else /* frp->fr_layout == FR_ROW */
2818 {
2819 /* Only need to handle the last frame in the row. */
2820 frp = frp->fr_child;
2821 while (frp->fr_next != NULL)
2822 frp = frp->fr_next;
2823 frame_add_vsep(frp);
2824 }
2825}
2826
2827/*
2828 * Set frame width from the window it contains.
2829 */
2830 static void
2831frame_fix_width(wp)
2832 win_T *wp;
2833{
2834 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2835}
2836#endif
2837
2838/*
2839 * Set frame height from the window it contains.
2840 */
2841 static void
2842frame_fix_height(wp)
2843 win_T *wp;
2844{
2845 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2846}
2847
2848/*
2849 * Compute the minimal height for frame "topfrp".
2850 * Uses the 'winminheight' option.
2851 * When "next_curwin" isn't NULL, use p_wh for this window.
2852 * When "next_curwin" is NOWIN, don't use at least one line for the current
2853 * window.
2854 */
2855 static int
2856frame_minheight(topfrp, next_curwin)
2857 frame_T *topfrp;
2858 win_T *next_curwin;
2859{
2860 frame_T *frp;
2861 int m;
2862#ifdef FEAT_VERTSPLIT
2863 int n;
2864#endif
2865
2866 if (topfrp->fr_win != NULL)
2867 {
2868 if (topfrp->fr_win == next_curwin)
2869 m = p_wh + topfrp->fr_win->w_status_height;
2870 else
2871 {
2872 /* window: minimal height of the window plus status line */
2873 m = p_wmh + topfrp->fr_win->w_status_height;
2874 /* Current window is minimal one line high */
2875 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2876 ++m;
2877 }
2878 }
2879#ifdef FEAT_VERTSPLIT
2880 else if (topfrp->fr_layout == FR_ROW)
2881 {
2882 /* get the minimal height from each frame in this row */
2883 m = 0;
2884 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2885 {
2886 n = frame_minheight(frp, next_curwin);
2887 if (n > m)
2888 m = n;
2889 }
2890 }
2891#endif
2892 else
2893 {
2894 /* Add up the minimal heights for all frames in this column. */
2895 m = 0;
2896 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2897 m += frame_minheight(frp, next_curwin);
2898 }
2899
2900 return m;
2901}
2902
2903#ifdef FEAT_VERTSPLIT
2904/*
2905 * Compute the minimal width for frame "topfrp".
2906 * When "next_curwin" isn't NULL, use p_wiw for this window.
2907 * When "next_curwin" is NOWIN, don't use at least one column for the current
2908 * window.
2909 */
2910 static int
2911frame_minwidth(topfrp, next_curwin)
2912 frame_T *topfrp;
2913 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2914{
2915 frame_T *frp;
2916 int m, n;
2917
2918 if (topfrp->fr_win != NULL)
2919 {
2920 if (topfrp->fr_win == next_curwin)
2921 m = p_wiw + topfrp->fr_win->w_vsep_width;
2922 else
2923 {
2924 /* window: minimal width of the window plus separator column */
2925 m = p_wmw + topfrp->fr_win->w_vsep_width;
2926 /* Current window is minimal one column wide */
2927 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2928 ++m;
2929 }
2930 }
2931 else if (topfrp->fr_layout == FR_COL)
2932 {
2933 /* get the minimal width from each frame in this column */
2934 m = 0;
2935 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2936 {
2937 n = frame_minwidth(frp, next_curwin);
2938 if (n > m)
2939 m = n;
2940 }
2941 }
2942 else
2943 {
2944 /* Add up the minimal widths for all frames in this row. */
2945 m = 0;
2946 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2947 m += frame_minwidth(frp, next_curwin);
2948 }
2949
2950 return m;
2951}
2952#endif
2953
2954
2955/*
2956 * Try to close all windows except current one.
2957 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2958 * used and the buffer was modified.
2959 *
2960 * Used by ":bdel" and ":only".
2961 */
2962 void
2963close_others(message, forceit)
2964 int message;
2965 int forceit; /* always hide all other windows */
2966{
2967 win_T *wp;
2968 win_T *nextwp;
2969 int r;
2970
2971 if (lastwin == firstwin)
2972 {
2973 if (message
2974#ifdef FEAT_AUTOCMD
2975 && !autocmd_busy
2976#endif
2977 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002978 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 return;
2980 }
2981
2982 /* Be very careful here: autocommands may change the window layout. */
2983 for (wp = firstwin; win_valid(wp); wp = nextwp)
2984 {
2985 nextwp = wp->w_next;
2986 if (wp != curwin) /* don't close current window */
2987 {
2988
2989 /* Check if it's allowed to abandon this window */
2990 r = can_abandon(wp->w_buffer, forceit);
2991#ifdef FEAT_AUTOCMD
2992 if (!win_valid(wp)) /* autocommands messed wp up */
2993 {
2994 nextwp = firstwin;
2995 continue;
2996 }
2997#endif
2998 if (!r)
2999 {
3000#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3001 if (message && (p_confirm || cmdmod.confirm) && p_write)
3002 {
3003 dialog_changed(wp->w_buffer, FALSE);
3004# ifdef FEAT_AUTOCMD
3005 if (!win_valid(wp)) /* autocommands messed wp up */
3006 {
3007 nextwp = firstwin;
3008 continue;
3009 }
3010# endif
3011 }
3012 if (bufIsChanged(wp->w_buffer))
3013#endif
3014 continue;
3015 }
3016 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3017 }
3018 }
3019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003020 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 EMSG(_("E445: Other window contains changes"));
3022}
3023
3024#endif /* FEAT_WINDOWS */
3025
3026/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003027 * Init the current window "curwin".
3028 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 */
3030 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003031curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003033 redraw_win_later(curwin, NOT_VALID);
3034 curwin->w_lines_valid = 0;
3035 curwin->w_cursor.lnum = 1;
3036 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003038 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003040 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3041 curwin->w_pcmark.col = 0;
3042 curwin->w_prev_pcmark.lnum = 0;
3043 curwin->w_prev_pcmark.col = 0;
3044 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003046 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003048 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#ifdef FEAT_FKMAP
3050 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003051 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054#endif
3055}
3056
3057/*
3058 * Allocate the first window and put an empty buffer in it.
3059 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003060 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003062 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063win_alloc_first()
3064{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003065 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003066 return FAIL;
3067
3068#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003069 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003070 if (first_tabpage == NULL)
3071 return FAIL;
3072 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003073 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003074#endif
3075 return OK;
3076}
3077
3078/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003079 * Allocate the first window or the first window in a new tab page.
3080 * When "oldwin" is NULL create an empty buffer for it.
3081 * When "oldwin" is not NULL copy info from it to the new window (only with
3082 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083 * Return FAIL when something goes wrong (out of memory).
3084 */
3085 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003086win_alloc_firstwin(oldwin)
3087 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003090 if (oldwin == NULL)
3091 {
3092 /* Very first window, need to create an empty buffer for it and
3093 * initialize from scratch. */
3094 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3095 if (curwin == NULL || curbuf == NULL)
3096 return FAIL;
3097 curwin->w_buffer = curbuf;
3098 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003100 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003102 curwin_init(); /* init current window */
3103 }
3104#ifdef FEAT_WINDOWS
3105 else
3106 {
3107 /* First window in new tab page, initialize it from "oldwin". */
3108 win_init(curwin, oldwin);
3109
3110# ifdef FEAT_SCROLLBIND
3111 /* We don't want scroll-binding in the first window. */
3112 curwin->w_p_scb = FALSE;
3113# endif
3114 }
3115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3118 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 topframe->fr_layout = FR_LEAF;
3121#ifdef FEAT_VERTSPLIT
3122 topframe->fr_width = Columns;
3123#endif
3124 topframe->fr_height = Rows - p_ch;
3125 topframe->fr_win = curwin;
3126 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003127
3128 return OK;
3129}
3130
3131/*
3132 * Initialize the window and frame size to the maximum.
3133 */
3134 void
3135win_init_size()
3136{
3137 firstwin->w_height = ROWS_AVAIL;
3138 topframe->fr_height = ROWS_AVAIL;
3139#ifdef FEAT_VERTSPLIT
3140 firstwin->w_width = Columns;
3141 topframe->fr_width = Columns;
3142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143}
3144
3145#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003146
3147/*
3148 * Allocate a new tabpage_T and init the values.
3149 * Returns NULL when out of memory.
3150 */
3151 static tabpage_T *
3152alloc_tabpage()
3153{
3154 tabpage_T *tp;
3155
3156 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3157 if (tp != NULL)
3158 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003159# ifdef FEAT_GUI
3160 int i;
3161
3162 for (i = 0; i < 3; i++)
3163 tp->tp_prev_which_scrollbars[i] = -1;
3164# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003165# ifdef FEAT_DIFF
3166 tp->tp_diff_invalid = TRUE;
3167# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003168#ifdef FEAT_EVAL
3169 /* init t: variables */
3170 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3171#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003172 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003173 }
3174 return tp;
3175}
3176
3177 static void
3178free_tabpage(tp)
3179 tabpage_T *tp;
3180{
3181# ifdef FEAT_DIFF
3182 diff_clear(tp);
3183# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003184 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003185#ifdef FEAT_EVAL
3186 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3187#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003188 vim_free(tp);
3189}
3190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003191/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003192 * Create a new Tab page with one window.
3193 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003194 * When "after" is 0 put it just after the current Tab page.
3195 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003196 * Return FAIL or OK.
3197 */
3198 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003199win_new_tabpage(after)
3200 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003201{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003202 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003203 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003204 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003205
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003206 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207 if (newtp == NULL)
3208 return FAIL;
3209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003210 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003211 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003212 {
3213 vim_free(newtp);
3214 return FAIL;
3215 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003216 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003217
3218 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003219 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003221 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003222 if (after == 1)
3223 {
3224 /* New tab page becomes the first one. */
3225 newtp->tp_next = first_tabpage;
3226 first_tabpage = newtp;
3227 }
3228 else
3229 {
3230 if (after > 0)
3231 {
3232 /* Put new tab page before tab page "after". */
3233 n = 2;
3234 for (tp = first_tabpage; tp->tp_next != NULL
3235 && n < after; tp = tp->tp_next)
3236 ++n;
3237 }
3238 newtp->tp_next = tp->tp_next;
3239 tp->tp_next = newtp;
3240 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003241 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003243 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003244
3245 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003246 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003247
3248#if defined(FEAT_GUI)
3249 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3250 * scrollbars. Have to update them anyway. */
3251 if (gui.in_use && starting == 0)
3252 {
3253 gui_init_which_components(NULL);
3254 gui_update_scrollbars(TRUE);
3255 }
3256 need_mouse_correct = TRUE;
3257#endif
3258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003260#ifdef FEAT_AUTOCMD
3261 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3262 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3263#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003264 return OK;
3265 }
3266
3267 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003268 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003269 return FAIL;
3270}
3271
3272/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003273 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3274 * like with ":split".
3275 * Returns OK if a new tab page was created, FAIL otherwise.
3276 */
3277 int
3278may_open_tabpage()
3279{
3280 int n = cmdmod.tab;
3281
3282 if (cmdmod.tab != 0)
3283 {
3284 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3285 return win_new_tabpage(n);
3286 }
3287 return FAIL;
3288}
3289
3290/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003291 * Create up to "maxcount" tabpages with empty windows.
3292 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003294 int
3295make_tabpages(maxcount)
3296 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003297{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003298 int count = maxcount;
3299 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003301 /* Limit to 'tabpagemax' tabs. */
3302 if (count > p_tpm)
3303 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003304
3305#ifdef FEAT_AUTOCMD
3306 /*
3307 * Don't execute autocommands while creating the tab pages. Must do that
3308 * when putting the buffers in the windows.
3309 */
3310 ++autocmd_block;
3311#endif
3312
3313 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003314 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003315 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003316
3317#ifdef FEAT_AUTOCMD
3318 --autocmd_block;
3319#endif
3320
3321 /* return actual number of tab pages */
3322 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003323}
3324
3325/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003326 * Return TRUE when "tpc" points to a valid tab page.
3327 */
3328 int
3329valid_tabpage(tpc)
3330 tabpage_T *tpc;
3331{
3332 tabpage_T *tp;
3333
3334 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3335 if (tp == tpc)
3336 return TRUE;
3337 return FALSE;
3338}
3339
3340/*
3341 * Find tab page "n" (first one is 1). Returns NULL when not found.
3342 */
3343 tabpage_T *
3344find_tabpage(n)
3345 int n;
3346{
3347 tabpage_T *tp;
3348 int i = 1;
3349
3350 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3351 ++i;
3352 return tp;
3353}
3354
3355/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003356 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003357 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358 */
3359 int
3360tabpage_index(ftp)
3361 tabpage_T *ftp;
3362{
3363 int i = 1;
3364 tabpage_T *tp;
3365
3366 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3367 ++i;
3368 return i;
3369}
3370
3371/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003372 * Prepare for leaving the current tab page.
3373 * When autocomands change "curtab" we don't leave the tab page and return
3374 * FAIL.
3375 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003376 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003377/*ARGSUSED*/
3378 static int
3379leave_tabpage(new_curbuf)
3380 buf_T *new_curbuf; /* what is going to be the new curbuf,
3381 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003382{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003383 tabpage_T *tp = curtab;
3384
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003385#ifdef FEAT_VISUAL
3386 reset_VIsual_and_resel(); /* stop Visual mode */
3387#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003388#ifdef FEAT_AUTOCMD
3389 if (new_curbuf != curbuf)
3390 {
3391 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3392 if (curtab != tp)
3393 return FAIL;
3394 }
3395 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3396 if (curtab != tp)
3397 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003398 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003399 if (curtab != tp)
3400 return FAIL;
3401#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003402#if defined(FEAT_GUI)
3403 /* Remove the scrollbars. They may be added back later. */
3404 if (gui.in_use)
3405 gui_remove_scrollbars();
3406#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003407 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003408 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003409 tp->tp_firstwin = firstwin;
3410 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003411 tp->tp_old_Rows = Rows;
3412 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003413 firstwin = NULL;
3414 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003415 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416}
3417
3418/*
3419 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003420 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 */
3422/*ARGSUSED*/
3423 static void
3424enter_tabpage(tp, old_curbuf)
3425 tabpage_T *tp;
3426 buf_T *old_curbuf;
3427{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003428 int old_off = tp->tp_firstwin->w_winrow;
3429
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003430 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431 firstwin = tp->tp_firstwin;
3432 lastwin = tp->tp_lastwin;
3433 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003434#ifdef FEAT_AUTOCMD
3435 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3436#endif
3437
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003439 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003440
3441#ifdef FEAT_AUTOCMD
3442 if (old_curbuf != curbuf)
3443 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3444#endif
3445
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003446 last_status(FALSE); /* status line may appear or disappear */
3447 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003448 must_redraw = CLEAR; /* need to redraw everything */
3449#ifdef FEAT_DIFF
3450 diff_need_scrollbind = TRUE;
3451#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003453 /* The tabpage line may have appeared or disappeared, may need to resize
3454 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003455 * frame sizes too. Use the stored value of p_ch, so that it can be
3456 * different for each tab page. */
3457 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003458 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3459#ifdef FEAT_GUI_TABLINE
3460 && !gui_use_tabline()
3461#endif
3462 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003463 shell_new_rows();
3464#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003465 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003466 shell_new_columns(); /* update window widths */
3467#endif
3468
3469#if defined(FEAT_GUI)
3470 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3471 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003472 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003473 {
3474 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003475 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003476 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003477 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478#endif
3479
3480 redraw_all_later(CLEAR);
3481}
3482
3483/*
3484 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003485 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003486 */
3487 void
3488goto_tabpage(n)
3489 int n;
3490{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003492 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003493 int i;
3494
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003495 /* If there is only one it can't work. */
3496 if (first_tabpage->tp_next == NULL)
3497 {
3498 if (n > 1)
3499 beep_flush();
3500 return;
3501 }
3502
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 if (n == 0)
3504 {
3505 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003506 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003507 tp = first_tabpage;
3508 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003509 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003511 else if (n < 0)
3512 {
3513 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3514 * this N times. */
3515 ttp = curtab;
3516 for (i = n; i < 0; ++i)
3517 {
3518 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3519 tp = tp->tp_next)
3520 ;
3521 ttp = tp;
3522 }
3523 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003524 else if (n == 9999)
3525 {
3526 /* Go to last tab page. */
3527 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3528 ;
3529 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 else
3531 {
3532 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003533 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003534 if (tp == NULL)
3535 {
3536 beep_flush();
3537 return;
3538 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 }
3540
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003541 goto_tabpage_tp(tp);
3542
3543#ifdef FEAT_GUI_TABLINE
3544 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003545 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003546#endif
3547}
3548
3549/*
3550 * Go to tabpage "tp".
3551 * Note: doesn't update the GUI tab.
3552 */
3553 void
3554goto_tabpage_tp(tp)
3555 tabpage_T *tp;
3556{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003557 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003558 {
3559 if (valid_tabpage(tp))
3560 enter_tabpage(tp, curbuf);
3561 else
3562 enter_tabpage(curtab, curbuf);
3563 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003567 * Enter window "wp" in tab page "tp".
3568 * Also updates the GUI tab.
3569 */
3570 void
3571goto_tabpage_win(tp, wp)
3572 tabpage_T *tp;
3573 win_T *wp;
3574{
3575 goto_tabpage_tp(tp);
3576 if (curtab == tp && win_valid(wp))
3577 {
3578 win_enter(wp, TRUE);
3579# ifdef FEAT_GUI_TABLINE
3580 if (gui_use_tabline())
3581 gui_mch_set_curtab(tabpage_index(curtab));
3582# endif
3583 }
3584}
3585
3586/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003587 * Move the current tab page to before tab page "nr".
3588 */
3589 void
3590tabpage_move(nr)
3591 int nr;
3592{
3593 int n = nr;
3594 tabpage_T *tp;
3595
3596 if (first_tabpage->tp_next == NULL)
3597 return;
3598
3599 /* Remove the current tab page from the list of tab pages. */
3600 if (curtab == first_tabpage)
3601 first_tabpage = curtab->tp_next;
3602 else
3603 {
3604 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3605 if (tp->tp_next == curtab)
3606 break;
3607 if (tp == NULL) /* "cannot happen" */
3608 return;
3609 tp->tp_next = curtab->tp_next;
3610 }
3611
3612 /* Re-insert it at the specified position. */
3613 if (n == 0)
3614 {
3615 curtab->tp_next = first_tabpage;
3616 first_tabpage = curtab;
3617 }
3618 else
3619 {
3620 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3621 --n;
3622 curtab->tp_next = tp->tp_next;
3623 tp->tp_next = curtab;
3624 }
3625
3626 /* Need to redraw the tabline. Tab page contents doesn't change. */
3627 redraw_tabline = TRUE;
3628}
3629
3630
3631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Go to another window.
3633 * When jumping to another buffer, stop Visual mode. Do this before
3634 * changing windows so we can yank the selection into the '*' register.
3635 * When jumping to another window on the same buffer, adjust its cursor
3636 * position to keep the same Visual area.
3637 */
3638 void
3639win_goto(wp)
3640 win_T *wp;
3641{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003642 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003645 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 return;
3647 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648#ifdef FEAT_AUTOCMD
3649 if (curbuf_locked())
3650 return;
3651#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#ifdef FEAT_VISUAL
3654 if (wp->w_buffer != curbuf)
3655 reset_VIsual_and_resel();
3656 else if (VIsual_active)
3657 wp->w_cursor = curwin->w_cursor;
3658#endif
3659
3660#ifdef FEAT_GUI
3661 need_mouse_correct = TRUE;
3662#endif
3663 win_enter(wp, TRUE);
3664}
3665
3666#if defined(FEAT_PERL) || defined(PROTO)
3667/*
3668 * Find window number "winnr" (counting top to bottom).
3669 */
3670 win_T *
3671win_find_nr(winnr)
3672 int winnr;
3673{
3674 win_T *wp;
3675
3676# ifdef FEAT_WINDOWS
3677 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3678 if (--winnr == 0)
3679 break;
3680 return wp;
3681# else
3682 return curwin;
3683# endif
3684}
3685#endif
3686
3687#ifdef FEAT_VERTSPLIT
3688/*
3689 * Move to window above or below "count" times.
3690 */
3691 static void
3692win_goto_ver(up, count)
3693 int up; /* TRUE to go to win above */
3694 long count;
3695{
3696 frame_T *fr;
3697 frame_T *nfr;
3698 frame_T *foundfr;
3699
3700 foundfr = curwin->w_frame;
3701 while (count--)
3702 {
3703 /*
3704 * First go upwards in the tree of frames until we find a upwards or
3705 * downwards neighbor.
3706 */
3707 fr = foundfr;
3708 for (;;)
3709 {
3710 if (fr == topframe)
3711 goto end;
3712 if (up)
3713 nfr = fr->fr_prev;
3714 else
3715 nfr = fr->fr_next;
3716 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3717 break;
3718 fr = fr->fr_parent;
3719 }
3720
3721 /*
3722 * Now go downwards to find the bottom or top frame in it.
3723 */
3724 for (;;)
3725 {
3726 if (nfr->fr_layout == FR_LEAF)
3727 {
3728 foundfr = nfr;
3729 break;
3730 }
3731 fr = nfr->fr_child;
3732 if (nfr->fr_layout == FR_ROW)
3733 {
3734 /* Find the frame at the cursor row. */
3735 while (fr->fr_next != NULL
3736 && frame2win(fr)->w_wincol + fr->fr_width
3737 <= curwin->w_wincol + curwin->w_wcol)
3738 fr = fr->fr_next;
3739 }
3740 if (nfr->fr_layout == FR_COL && up)
3741 while (fr->fr_next != NULL)
3742 fr = fr->fr_next;
3743 nfr = fr;
3744 }
3745 }
3746end:
3747 if (foundfr != NULL)
3748 win_goto(foundfr->fr_win);
3749}
3750
3751/*
3752 * Move to left or right window.
3753 */
3754 static void
3755win_goto_hor(left, count)
3756 int left; /* TRUE to go to left win */
3757 long count;
3758{
3759 frame_T *fr;
3760 frame_T *nfr;
3761 frame_T *foundfr;
3762
3763 foundfr = curwin->w_frame;
3764 while (count--)
3765 {
3766 /*
3767 * First go upwards in the tree of frames until we find a left or
3768 * right neighbor.
3769 */
3770 fr = foundfr;
3771 for (;;)
3772 {
3773 if (fr == topframe)
3774 goto end;
3775 if (left)
3776 nfr = fr->fr_prev;
3777 else
3778 nfr = fr->fr_next;
3779 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3780 break;
3781 fr = fr->fr_parent;
3782 }
3783
3784 /*
3785 * Now go downwards to find the leftmost or rightmost frame in it.
3786 */
3787 for (;;)
3788 {
3789 if (nfr->fr_layout == FR_LEAF)
3790 {
3791 foundfr = nfr;
3792 break;
3793 }
3794 fr = nfr->fr_child;
3795 if (nfr->fr_layout == FR_COL)
3796 {
3797 /* Find the frame at the cursor row. */
3798 while (fr->fr_next != NULL
3799 && frame2win(fr)->w_winrow + fr->fr_height
3800 <= curwin->w_winrow + curwin->w_wrow)
3801 fr = fr->fr_next;
3802 }
3803 if (nfr->fr_layout == FR_ROW && left)
3804 while (fr->fr_next != NULL)
3805 fr = fr->fr_next;
3806 nfr = fr;
3807 }
3808 }
3809end:
3810 if (foundfr != NULL)
3811 win_goto(foundfr->fr_win);
3812}
3813#endif
3814
3815/*
3816 * Make window "wp" the current window.
3817 */
3818 void
3819win_enter(wp, undo_sync)
3820 win_T *wp;
3821 int undo_sync;
3822{
3823 win_enter_ext(wp, undo_sync, FALSE);
3824}
3825
3826/*
3827 * Make window wp the current window.
3828 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3829 * been closed and isn't valid.
3830 */
3831 static void
3832win_enter_ext(wp, undo_sync, curwin_invalid)
3833 win_T *wp;
3834 int undo_sync;
3835 int curwin_invalid;
3836{
3837#ifdef FEAT_AUTOCMD
3838 int other_buffer = FALSE;
3839#endif
3840
3841 if (wp == curwin && !curwin_invalid) /* nothing to do */
3842 return;
3843
3844#ifdef FEAT_AUTOCMD
3845 if (!curwin_invalid)
3846 {
3847 /*
3848 * Be careful: If autocommands delete the window, return now.
3849 */
3850 if (wp->w_buffer != curbuf)
3851 {
3852 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3853 other_buffer = TRUE;
3854 if (!win_valid(wp))
3855 return;
3856 }
3857 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3858 if (!win_valid(wp))
3859 return;
3860# ifdef FEAT_EVAL
3861 /* autocmds may abort script processing */
3862 if (aborting())
3863 return;
3864# endif
3865 }
3866#endif
3867
3868 /* sync undo before leaving the current buffer */
3869 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003870 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /* may have to copy the buffer options when 'cpo' contains 'S' */
3872 if (wp->w_buffer != curbuf)
3873 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3874 if (!curwin_invalid)
3875 {
3876 prevwin = curwin; /* remember for CTRL-W p */
3877 curwin->w_redr_status = TRUE;
3878 }
3879 curwin = wp;
3880 curbuf = wp->w_buffer;
3881 check_cursor();
3882#ifdef FEAT_VIRTUALEDIT
3883 if (!virtual_active())
3884 curwin->w_cursor.coladd = 0;
3885#endif
3886 changed_line_abv_curs(); /* assume cursor position needs updating */
3887
3888 if (curwin->w_localdir != NULL)
3889 {
3890 /* Window has a local directory: Save current directory as global
3891 * directory (unless that was done already) and change to the local
3892 * directory. */
3893 if (globaldir == NULL)
3894 {
3895 char_u cwd[MAXPATHL];
3896
3897 if (mch_dirname(cwd, MAXPATHL) == OK)
3898 globaldir = vim_strsave(cwd);
3899 }
3900 mch_chdir((char *)curwin->w_localdir);
3901 shorten_fnames(TRUE);
3902 }
3903 else if (globaldir != NULL)
3904 {
3905 /* Window doesn't have a local directory and we are not in the global
3906 * directory: Change to the global directory. */
3907 mch_chdir((char *)globaldir);
3908 vim_free(globaldir);
3909 globaldir = NULL;
3910 shorten_fnames(TRUE);
3911 }
3912
3913#ifdef FEAT_AUTOCMD
3914 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3915 if (other_buffer)
3916 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3917#endif
3918
3919#ifdef FEAT_TITLE
3920 maketitle();
3921#endif
3922 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003923 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 if (restart_edit)
3925 redraw_later(VALID); /* causes status line redraw */
3926
3927 /* set window height to desired minimal value */
3928 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3929 win_setheight((int)p_wh);
3930 else if (curwin->w_height == 0)
3931 win_setheight(1);
3932
3933#ifdef FEAT_VERTSPLIT
3934 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003935 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 win_setwidth((int)p_wiw);
3937#endif
3938
3939#ifdef FEAT_MOUSE
3940 setmouse(); /* in case jumped to/from help buffer */
3941#endif
3942
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003943#ifdef FEAT_AUTOCHDIR
3944 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 * switching windows. */
3946 if (p_acd && curbuf->b_ffname != NULL
3947 && vim_chdirfile(curbuf->b_ffname) == OK)
3948 shorten_fnames(TRUE);
3949#endif
3950}
3951
3952#endif /* FEAT_WINDOWS */
3953
3954#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3955/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003956 * Jump to the first open window that contains buffer "buf", if one exists.
3957 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 */
3959 win_T *
3960buf_jump_open_win(buf)
3961 buf_T *buf;
3962{
3963# ifdef FEAT_WINDOWS
3964 win_T *wp;
3965
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003966 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if (wp->w_buffer == buf)
3968 break;
3969 if (wp != NULL)
3970 win_enter(wp, FALSE);
3971 return wp;
3972# else
3973 if (curwin->w_buffer == buf)
3974 return curwin;
3975 return NULL;
3976# endif
3977}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003978
3979/*
3980 * Jump to the first open window in any tab page that contains buffer "buf",
3981 * if one exists.
3982 * Returns a pointer to the window found, otherwise NULL.
3983 */
3984 win_T *
3985buf_jump_open_tab(buf)
3986 buf_T *buf;
3987{
3988# ifdef FEAT_WINDOWS
3989 win_T *wp;
3990 tabpage_T *tp;
3991
3992 /* First try the current tab page. */
3993 wp = buf_jump_open_win(buf);
3994 if (wp != NULL)
3995 return wp;
3996
3997 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3998 if (tp != curtab)
3999 {
4000 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4001 if (wp->w_buffer == buf)
4002 break;
4003 if (wp != NULL)
4004 {
4005 goto_tabpage_win(tp, wp);
4006 if (curwin != wp)
4007 wp = NULL; /* something went wrong */
4008 break;
4009 }
4010 }
4011
4012 return wp;
4013# else
4014 if (curwin->w_buffer == buf)
4015 return curwin;
4016 return NULL;
4017# endif
4018}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#endif
4020
4021/*
4022 * allocate a window structure and link it in the window list
4023 */
4024/*ARGSUSED*/
4025 static win_T *
4026win_alloc(after)
4027 win_T *after;
4028{
4029 win_T *newwin;
4030
4031 /*
4032 * allocate window structure and linesizes arrays
4033 */
4034 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4035 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4036 {
4037 vim_free(newwin);
4038 newwin = NULL;
4039 }
4040
4041 if (newwin != NULL)
4042 {
4043 /*
4044 * link the window in the window list
4045 */
4046#ifdef FEAT_WINDOWS
4047 win_append(after, newwin);
4048#endif
4049#ifdef FEAT_VERTSPLIT
4050 newwin->w_wincol = 0;
4051 newwin->w_width = Columns;
4052#endif
4053
4054 /* position the display and the cursor at the top of the file. */
4055 newwin->w_topline = 1;
4056#ifdef FEAT_DIFF
4057 newwin->w_topfill = 0;
4058#endif
4059 newwin->w_botline = 2;
4060 newwin->w_cursor.lnum = 1;
4061#ifdef FEAT_SCROLLBIND
4062 newwin->w_scbind_pos = 1;
4063#endif
4064
4065 /* We won't calculate w_fraction until resizing the window */
4066 newwin->w_fraction = 0;
4067 newwin->w_prev_fraction_row = -1;
4068
4069#ifdef FEAT_GUI
4070 if (gui.in_use)
4071 {
4072 out_flush();
4073 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4074 SBAR_LEFT, newwin);
4075 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4076 SBAR_RIGHT, newwin);
4077 }
4078#endif
4079#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004080 /* init w: variables */
4081 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#endif
4083#ifdef FEAT_FOLDING
4084 foldInitWin(newwin);
4085#endif
4086 }
4087 return newwin;
4088}
4089
4090#if defined(FEAT_WINDOWS) || defined(PROTO)
4091
4092/*
4093 * remove window 'wp' from the window list and free the structure
4094 */
4095 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004096win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004098 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 int i;
4101
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004102#ifdef FEAT_MZSCHEME
4103 mzscheme_window_free(wp);
4104#endif
4105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_PERL
4107 perl_win_free(wp);
4108#endif
4109
4110#ifdef FEAT_PYTHON
4111 python_window_free(wp);
4112#endif
4113
4114#ifdef FEAT_TCL
4115 tcl_window_free(wp);
4116#endif
4117
4118#ifdef FEAT_RUBY
4119 ruby_window_free(wp);
4120#endif
4121
4122 clear_winopt(&wp->w_onebuf_opt);
4123 clear_winopt(&wp->w_allbuf_opt);
4124
4125#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004126 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#endif
4128
4129 if (prevwin == wp)
4130 prevwin = NULL;
4131 win_free_lsize(wp);
4132
4133 for (i = 0; i < wp->w_tagstacklen; ++i)
4134 vim_free(wp->w_tagstack[i].tagname);
4135
4136 vim_free(wp->w_localdir);
4137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004138 vim_free(wp->w_match[0].regprog);
4139 vim_free(wp->w_match[1].regprog);
4140 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141#endif
4142#ifdef FEAT_JUMPLIST
4143 free_jumplist(wp);
4144#endif
4145
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004146#ifdef FEAT_QUICKFIX
4147 qf_free_all(wp);
4148#endif
4149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#ifdef FEAT_GUI
4151 if (gui.in_use)
4152 {
4153 out_flush();
4154 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4155 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4156 }
4157#endif /* FEAT_GUI */
4158
Bram Moolenaarf740b292006-02-16 22:11:02 +00004159 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 vim_free(wp);
4161}
4162
4163/*
4164 * Append window "wp" in the window list after window "after".
4165 */
4166 static void
4167win_append(after, wp)
4168 win_T *after, *wp;
4169{
4170 win_T *before;
4171
4172 if (after == NULL) /* after NULL is in front of the first */
4173 before = firstwin;
4174 else
4175 before = after->w_next;
4176
4177 wp->w_next = before;
4178 wp->w_prev = after;
4179 if (after == NULL)
4180 firstwin = wp;
4181 else
4182 after->w_next = wp;
4183 if (before == NULL)
4184 lastwin = wp;
4185 else
4186 before->w_prev = wp;
4187}
4188
4189/*
4190 * Remove a window from the window list.
4191 */
4192 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004193win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004195 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 if (wp->w_prev != NULL)
4198 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004199 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004201 else
4202 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (wp->w_next != NULL)
4204 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004205 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004207 else
4208 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209}
4210
4211/*
4212 * Append frame "frp" in a frame list after frame "after".
4213 */
4214 static void
4215frame_append(after, frp)
4216 frame_T *after, *frp;
4217{
4218 frp->fr_next = after->fr_next;
4219 after->fr_next = frp;
4220 if (frp->fr_next != NULL)
4221 frp->fr_next->fr_prev = frp;
4222 frp->fr_prev = after;
4223}
4224
4225/*
4226 * Insert frame "frp" in a frame list before frame "before".
4227 */
4228 static void
4229frame_insert(before, frp)
4230 frame_T *before, *frp;
4231{
4232 frp->fr_next = before;
4233 frp->fr_prev = before->fr_prev;
4234 before->fr_prev = frp;
4235 if (frp->fr_prev != NULL)
4236 frp->fr_prev->fr_next = frp;
4237 else
4238 frp->fr_parent->fr_child = frp;
4239}
4240
4241/*
4242 * Remove a frame from a frame list.
4243 */
4244 static void
4245frame_remove(frp)
4246 frame_T *frp;
4247{
4248 if (frp->fr_prev != NULL)
4249 frp->fr_prev->fr_next = frp->fr_next;
4250 else
4251 frp->fr_parent->fr_child = frp->fr_next;
4252 if (frp->fr_next != NULL)
4253 frp->fr_next->fr_prev = frp->fr_prev;
4254}
4255
4256#endif /* FEAT_WINDOWS */
4257
4258/*
4259 * Allocate w_lines[] for window "wp".
4260 * Return FAIL for failure, OK for success.
4261 */
4262 int
4263win_alloc_lines(wp)
4264 win_T *wp;
4265{
4266 wp->w_lines_valid = 0;
4267 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4268 if (wp->w_lines == NULL)
4269 return FAIL;
4270 return OK;
4271}
4272
4273/*
4274 * free lsize arrays for a window
4275 */
4276 void
4277win_free_lsize(wp)
4278 win_T *wp;
4279{
4280 vim_free(wp->w_lines);
4281 wp->w_lines = NULL;
4282}
4283
4284/*
4285 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004286 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 void
4289shell_new_rows()
4290{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004291 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 if (firstwin == NULL) /* not initialized yet */
4294 return;
4295#ifdef FEAT_WINDOWS
4296 if (h < frame_minheight(topframe, NULL))
4297 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004298
4299 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 * that doesn't result in the right height, forget about that option. */
4301 frame_new_height(topframe, h, FALSE, TRUE);
4302 if (topframe->fr_height != h)
4303 frame_new_height(topframe, h, FALSE, FALSE);
4304
4305 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4306#else
4307 if (h < 1)
4308 h = 1;
4309 win_new_height(firstwin, h);
4310#endif
4311 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004312#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004313 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004314#endif
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#if 0
4317 /* Disabled: don't want making the screen smaller make a window larger. */
4318 if (p_ea)
4319 win_equal(curwin, FALSE, 'v');
4320#endif
4321}
4322
4323#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4324/*
4325 * Called from win_new_shellsize() after Columns changed.
4326 */
4327 void
4328shell_new_columns()
4329{
4330 if (firstwin == NULL) /* not initialized yet */
4331 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004332
4333 /* First try setting the widths of windows with 'winfixwidth'. If that
4334 * doesn't result in the right width, forget about that option. */
4335 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4336 if (topframe->fr_width != Columns)
4337 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4340#if 0
4341 /* Disabled: don't want making the screen smaller make a window larger. */
4342 if (p_ea)
4343 win_equal(curwin, FALSE, 'h');
4344#endif
4345}
4346#endif
4347
4348#if defined(FEAT_CMDWIN) || defined(PROTO)
4349/*
4350 * Save the size of all windows in "gap".
4351 */
4352 void
4353win_size_save(gap)
4354 garray_T *gap;
4355
4356{
4357 win_T *wp;
4358
4359 ga_init2(gap, (int)sizeof(int), 1);
4360 if (ga_grow(gap, win_count() * 2) == OK)
4361 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4362 {
4363 ((int *)gap->ga_data)[gap->ga_len++] =
4364 wp->w_width + wp->w_vsep_width;
4365 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4366 }
4367}
4368
4369/*
4370 * Restore window sizes, but only if the number of windows is still the same.
4371 * Does not free the growarray.
4372 */
4373 void
4374win_size_restore(gap)
4375 garray_T *gap;
4376{
4377 win_T *wp;
4378 int i;
4379
4380 if (win_count() * 2 == gap->ga_len)
4381 {
4382 i = 0;
4383 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4384 {
4385 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4386 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4387 }
4388 /* recompute the window positions */
4389 (void)win_comp_pos();
4390 }
4391}
4392#endif /* FEAT_CMDWIN */
4393
4394#if defined(FEAT_WINDOWS) || defined(PROTO)
4395/*
4396 * Update the position for all windows, using the width and height of the
4397 * frames.
4398 * Returns the row just after the last window.
4399 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004400 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401win_comp_pos()
4402{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004403 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 int col = 0;
4405
4406 frame_comp_pos(topframe, &row, &col);
4407 return row;
4408}
4409
4410/*
4411 * Update the position of the windows in frame "topfrp", using the width and
4412 * height of the frames.
4413 * "*row" and "*col" are the top-left position of the frame. They are updated
4414 * to the bottom-right position plus one.
4415 */
4416 static void
4417frame_comp_pos(topfrp, row, col)
4418 frame_T *topfrp;
4419 int *row;
4420 int *col;
4421{
4422 win_T *wp;
4423 frame_T *frp;
4424#ifdef FEAT_VERTSPLIT
4425 int startcol;
4426 int startrow;
4427#endif
4428
4429 wp = topfrp->fr_win;
4430 if (wp != NULL)
4431 {
4432 if (wp->w_winrow != *row
4433#ifdef FEAT_VERTSPLIT
4434 || wp->w_wincol != *col
4435#endif
4436 )
4437 {
4438 /* position changed, redraw */
4439 wp->w_winrow = *row;
4440#ifdef FEAT_VERTSPLIT
4441 wp->w_wincol = *col;
4442#endif
4443 redraw_win_later(wp, NOT_VALID);
4444 wp->w_redr_status = TRUE;
4445 }
4446 *row += wp->w_height + wp->w_status_height;
4447#ifdef FEAT_VERTSPLIT
4448 *col += wp->w_width + wp->w_vsep_width;
4449#endif
4450 }
4451 else
4452 {
4453#ifdef FEAT_VERTSPLIT
4454 startrow = *row;
4455 startcol = *col;
4456#endif
4457 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4458 {
4459#ifdef FEAT_VERTSPLIT
4460 if (topfrp->fr_layout == FR_ROW)
4461 *row = startrow; /* all frames are at the same row */
4462 else
4463 *col = startcol; /* all frames are at the same col */
4464#endif
4465 frame_comp_pos(frp, row, col);
4466 }
4467 }
4468}
4469
4470#endif /* FEAT_WINDOWS */
4471
4472/*
4473 * Set current window height and take care of repositioning other windows to
4474 * fit around it.
4475 */
4476 void
4477win_setheight(height)
4478 int height;
4479{
4480 win_setheight_win(height, curwin);
4481}
4482
4483/*
4484 * Set the window height of window "win" and take care of repositioning other
4485 * windows to fit around it.
4486 */
4487 void
4488win_setheight_win(height, win)
4489 int height;
4490 win_T *win;
4491{
4492 int row;
4493
4494 if (win == curwin)
4495 {
4496 /* Always keep current window at least one line high, even when
4497 * 'winminheight' is zero. */
4498#ifdef FEAT_WINDOWS
4499 if (height < p_wmh)
4500 height = p_wmh;
4501#endif
4502 if (height == 0)
4503 height = 1;
4504 }
4505
4506#ifdef FEAT_WINDOWS
4507 frame_setheight(win->w_frame, height + win->w_status_height);
4508
4509 /* recompute the window positions */
4510 row = win_comp_pos();
4511#else
4512 if (height > topframe->fr_height)
4513 height = topframe->fr_height;
4514 win->w_height = height;
4515 row = height;
4516#endif
4517
4518 /*
4519 * If there is extra space created between the last window and the command
4520 * line, clear it.
4521 */
4522 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4523 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4524 cmdline_row = row;
4525 msg_row = row;
4526 msg_col = 0;
4527
4528 redraw_all_later(NOT_VALID);
4529}
4530
4531#if defined(FEAT_WINDOWS) || defined(PROTO)
4532
4533/*
4534 * Set the height of a frame to "height" and take care that all frames and
4535 * windows inside it are resized. Also resize frames on the left and right if
4536 * the are in the same FR_ROW frame.
4537 *
4538 * Strategy:
4539 * If the frame is part of a FR_COL frame, try fitting the frame in that
4540 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4541 * go to containing frames to resize them and make room.
4542 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4543 * Check for the minimal height of the FR_ROW frame.
4544 * At the top level we can also use change the command line height.
4545 */
4546 static void
4547frame_setheight(curfrp, height)
4548 frame_T *curfrp;
4549 int height;
4550{
4551 int room; /* total number of lines available */
4552 int take; /* number of lines taken from other windows */
4553 int room_cmdline; /* lines available from cmdline */
4554 int run;
4555 frame_T *frp;
4556 int h;
4557 int room_reserved;
4558
4559 /* If the height already is the desired value, nothing to do. */
4560 if (curfrp->fr_height == height)
4561 return;
4562
4563 if (curfrp->fr_parent == NULL)
4564 {
4565 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004566 if (height > ROWS_AVAIL)
4567 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (height > 0)
4569 frame_new_height(curfrp, height, FALSE, FALSE);
4570 }
4571 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4572 {
4573 /* Row of frames: Also need to resize frames left and right of this
4574 * one. First check for the minimal height of these. */
4575 h = frame_minheight(curfrp->fr_parent, NULL);
4576 if (height < h)
4577 height = h;
4578 frame_setheight(curfrp->fr_parent, height);
4579 }
4580 else
4581 {
4582 /*
4583 * Column of frames: try to change only frames in this column.
4584 */
4585#ifdef FEAT_VERTSPLIT
4586 /*
4587 * Do this twice:
4588 * 1: compute room available, if it's not enough try resizing the
4589 * containing frame.
4590 * 2: compute the room available and adjust the height to it.
4591 * Try not to reduce the height of a window with 'winfixheight' set.
4592 */
4593 for (run = 1; run <= 2; ++run)
4594#else
4595 for (;;)
4596#endif
4597 {
4598 room = 0;
4599 room_reserved = 0;
4600 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4601 frp = frp->fr_next)
4602 {
4603 if (frp != curfrp
4604 && frp->fr_win != NULL
4605 && frp->fr_win->w_p_wfh)
4606 room_reserved += frp->fr_height;
4607 room += frp->fr_height;
4608 if (frp != curfrp)
4609 room -= frame_minheight(frp, NULL);
4610 }
4611#ifdef FEAT_VERTSPLIT
4612 if (curfrp->fr_width != Columns)
4613 room_cmdline = 0;
4614 else
4615#endif
4616 {
4617 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4618 + lastwin->w_height + lastwin->w_status_height);
4619 if (room_cmdline < 0)
4620 room_cmdline = 0;
4621 }
4622
4623 if (height <= room + room_cmdline)
4624 break;
4625#ifdef FEAT_VERTSPLIT
4626 if (run == 2 || curfrp->fr_width == Columns)
4627#endif
4628 {
4629 if (height > room + room_cmdline)
4630 height = room + room_cmdline;
4631 break;
4632 }
4633#ifdef FEAT_VERTSPLIT
4634 frame_setheight(curfrp->fr_parent, height
4635 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4636#endif
4637 /*NOTREACHED*/
4638 }
4639
4640 /*
4641 * Compute the number of lines we will take from others frames (can be
4642 * negative!).
4643 */
4644 take = height - curfrp->fr_height;
4645
4646 /* If there is not enough room, also reduce the height of a window
4647 * with 'winfixheight' set. */
4648 if (height > room + room_cmdline - room_reserved)
4649 room_reserved = room + room_cmdline - height;
4650 /* If there is only a 'winfixheight' window and making the
4651 * window smaller, need to make the other window taller. */
4652 if (take < 0 && room - curfrp->fr_height < room_reserved)
4653 room_reserved = 0;
4654
4655 if (take > 0 && room_cmdline > 0)
4656 {
4657 /* use lines from cmdline first */
4658 if (take < room_cmdline)
4659 room_cmdline = take;
4660 take -= room_cmdline;
4661 topframe->fr_height += room_cmdline;
4662 }
4663
4664 /*
4665 * set the current frame to the new height
4666 */
4667 frame_new_height(curfrp, height, FALSE, FALSE);
4668
4669 /*
4670 * First take lines from the frames after the current frame. If
4671 * that is not enough, takes lines from frames above the current
4672 * frame.
4673 */
4674 for (run = 0; run < 2; ++run)
4675 {
4676 if (run == 0)
4677 frp = curfrp->fr_next; /* 1st run: start with next window */
4678 else
4679 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4680 while (frp != NULL && take != 0)
4681 {
4682 h = frame_minheight(frp, NULL);
4683 if (room_reserved > 0
4684 && frp->fr_win != NULL
4685 && frp->fr_win->w_p_wfh)
4686 {
4687 if (room_reserved >= frp->fr_height)
4688 room_reserved -= frp->fr_height;
4689 else
4690 {
4691 if (frp->fr_height - room_reserved > take)
4692 room_reserved = frp->fr_height - take;
4693 take -= frp->fr_height - room_reserved;
4694 frame_new_height(frp, room_reserved, FALSE, FALSE);
4695 room_reserved = 0;
4696 }
4697 }
4698 else
4699 {
4700 if (frp->fr_height - take < h)
4701 {
4702 take -= frp->fr_height - h;
4703 frame_new_height(frp, h, FALSE, FALSE);
4704 }
4705 else
4706 {
4707 frame_new_height(frp, frp->fr_height - take,
4708 FALSE, FALSE);
4709 take = 0;
4710 }
4711 }
4712 if (run == 0)
4713 frp = frp->fr_next;
4714 else
4715 frp = frp->fr_prev;
4716 }
4717 }
4718 }
4719}
4720
4721#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4722/*
4723 * Set current window width and take care of repositioning other windows to
4724 * fit around it.
4725 */
4726 void
4727win_setwidth(width)
4728 int width;
4729{
4730 win_setwidth_win(width, curwin);
4731}
4732
4733 void
4734win_setwidth_win(width, wp)
4735 int width;
4736 win_T *wp;
4737{
4738 /* Always keep current window at least one column wide, even when
4739 * 'winminwidth' is zero. */
4740 if (wp == curwin)
4741 {
4742 if (width < p_wmw)
4743 width = p_wmw;
4744 if (width == 0)
4745 width = 1;
4746 }
4747
4748 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4749
4750 /* recompute the window positions */
4751 (void)win_comp_pos();
4752
4753 redraw_all_later(NOT_VALID);
4754}
4755
4756/*
4757 * Set the width of a frame to "width" and take care that all frames and
4758 * windows inside it are resized. Also resize frames above and below if the
4759 * are in the same FR_ROW frame.
4760 *
4761 * Strategy is similar to frame_setheight().
4762 */
4763 static void
4764frame_setwidth(curfrp, width)
4765 frame_T *curfrp;
4766 int width;
4767{
4768 int room; /* total number of lines available */
4769 int take; /* number of lines taken from other windows */
4770 int run;
4771 frame_T *frp;
4772 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004773 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774
4775 /* If the width already is the desired value, nothing to do. */
4776 if (curfrp->fr_width == width)
4777 return;
4778
4779 if (curfrp->fr_parent == NULL)
4780 /* topframe: can't change width */
4781 return;
4782
4783 if (curfrp->fr_parent->fr_layout == FR_COL)
4784 {
4785 /* Column of frames: Also need to resize frames above and below of
4786 * this one. First check for the minimal width of these. */
4787 w = frame_minwidth(curfrp->fr_parent, NULL);
4788 if (width < w)
4789 width = w;
4790 frame_setwidth(curfrp->fr_parent, width);
4791 }
4792 else
4793 {
4794 /*
4795 * Row of frames: try to change only frames in this row.
4796 *
4797 * Do this twice:
4798 * 1: compute room available, if it's not enough try resizing the
4799 * containing frame.
4800 * 2: compute the room available and adjust the width to it.
4801 */
4802 for (run = 1; run <= 2; ++run)
4803 {
4804 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004805 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4807 frp = frp->fr_next)
4808 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004809 if (frp != curfrp
4810 && frp->fr_win != NULL
4811 && frp->fr_win->w_p_wfw)
4812 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 room += frp->fr_width;
4814 if (frp != curfrp)
4815 room -= frame_minwidth(frp, NULL);
4816 }
4817
4818 if (width <= room)
4819 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004820 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 if (width > room)
4823 width = room;
4824 break;
4825 }
4826 frame_setwidth(curfrp->fr_parent, width
4827 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4828 }
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 /*
4831 * Compute the number of lines we will take from others frames (can be
4832 * negative!).
4833 */
4834 take = width - curfrp->fr_width;
4835
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004836 /* If there is not enough room, also reduce the width of a window
4837 * with 'winfixwidth' set. */
4838 if (width > room - room_reserved)
4839 room_reserved = room - width;
4840 /* If there is only a 'winfixwidth' window and making the
4841 * window smaller, need to make the other window narrower. */
4842 if (take < 0 && room - curfrp->fr_width < room_reserved)
4843 room_reserved = 0;
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 /*
4846 * set the current frame to the new width
4847 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004848 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 /*
4851 * First take lines from the frames right of the current frame. If
4852 * that is not enough, takes lines from frames left of the current
4853 * frame.
4854 */
4855 for (run = 0; run < 2; ++run)
4856 {
4857 if (run == 0)
4858 frp = curfrp->fr_next; /* 1st run: start with next window */
4859 else
4860 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4861 while (frp != NULL && take != 0)
4862 {
4863 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004864 if (room_reserved > 0
4865 && frp->fr_win != NULL
4866 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004868 if (room_reserved >= frp->fr_width)
4869 room_reserved -= frp->fr_width;
4870 else
4871 {
4872 if (frp->fr_width - room_reserved > take)
4873 room_reserved = frp->fr_width - take;
4874 take -= frp->fr_width - room_reserved;
4875 frame_new_width(frp, room_reserved, FALSE, FALSE);
4876 room_reserved = 0;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 else
4880 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004881 if (frp->fr_width - take < w)
4882 {
4883 take -= frp->fr_width - w;
4884 frame_new_width(frp, w, FALSE, FALSE);
4885 }
4886 else
4887 {
4888 frame_new_width(frp, frp->fr_width - take,
4889 FALSE, FALSE);
4890 take = 0;
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 if (run == 0)
4894 frp = frp->fr_next;
4895 else
4896 frp = frp->fr_prev;
4897 }
4898 }
4899 }
4900}
4901#endif /* FEAT_VERTSPLIT */
4902
4903/*
4904 * Check 'winminheight' for a valid value.
4905 */
4906 void
4907win_setminheight()
4908{
4909 int room;
4910 int first = TRUE;
4911 win_T *wp;
4912
4913 /* loop until there is a 'winminheight' that is possible */
4914 while (p_wmh > 0)
4915 {
4916 /* TODO: handle vertical splits */
4917 room = -p_wh;
4918 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4919 room += wp->w_height - p_wmh;
4920 if (room >= 0)
4921 break;
4922 --p_wmh;
4923 if (first)
4924 {
4925 EMSG(_(e_noroom));
4926 first = FALSE;
4927 }
4928 }
4929}
4930
4931#ifdef FEAT_MOUSE
4932
4933/*
4934 * Status line of dragwin is dragged "offset" lines down (negative is up).
4935 */
4936 void
4937win_drag_status_line(dragwin, offset)
4938 win_T *dragwin;
4939 int offset;
4940{
4941 frame_T *curfr;
4942 frame_T *fr;
4943 int room;
4944 int row;
4945 int up; /* if TRUE, drag status line up, otherwise down */
4946 int n;
4947
4948 fr = dragwin->w_frame;
4949 curfr = fr;
4950 if (fr != topframe) /* more than one window */
4951 {
4952 fr = fr->fr_parent;
4953 /* When the parent frame is not a column of frames, its parent should
4954 * be. */
4955 if (fr->fr_layout != FR_COL)
4956 {
4957 curfr = fr;
4958 if (fr != topframe) /* only a row of windows, may drag statusline */
4959 fr = fr->fr_parent;
4960 }
4961 }
4962
4963 /* If this is the last frame in a column, may want to resize the parent
4964 * frame instead (go two up to skip a row of frames). */
4965 while (curfr != topframe && curfr->fr_next == NULL)
4966 {
4967 if (fr != topframe)
4968 fr = fr->fr_parent;
4969 curfr = fr;
4970 if (fr != topframe)
4971 fr = fr->fr_parent;
4972 }
4973
4974 if (offset < 0) /* drag up */
4975 {
4976 up = TRUE;
4977 offset = -offset;
4978 /* sum up the room of the current frame and above it */
4979 if (fr == curfr)
4980 {
4981 /* only one window */
4982 room = fr->fr_height - frame_minheight(fr, NULL);
4983 }
4984 else
4985 {
4986 room = 0;
4987 for (fr = fr->fr_child; ; fr = fr->fr_next)
4988 {
4989 room += fr->fr_height - frame_minheight(fr, NULL);
4990 if (fr == curfr)
4991 break;
4992 }
4993 }
4994 fr = curfr->fr_next; /* put fr at frame that grows */
4995 }
4996 else /* drag down */
4997 {
4998 up = FALSE;
4999 /*
5000 * Only dragging the last status line can reduce p_ch.
5001 */
5002 room = Rows - cmdline_row;
5003 if (curfr->fr_next == NULL)
5004 room -= 1;
5005 else
5006 room -= p_ch;
5007 if (room < 0)
5008 room = 0;
5009 /* sum up the room of frames below of the current one */
5010 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5011 room += fr->fr_height - frame_minheight(fr, NULL);
5012 fr = curfr; /* put fr at window that grows */
5013 }
5014
5015 if (room < offset) /* Not enough room */
5016 offset = room; /* Move as far as we can */
5017 if (offset <= 0)
5018 return;
5019
5020 /*
5021 * Grow frame fr by "offset" lines.
5022 * Doesn't happen when dragging the last status line up.
5023 */
5024 if (fr != NULL)
5025 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5026
5027 if (up)
5028 fr = curfr; /* current frame gets smaller */
5029 else
5030 fr = curfr->fr_next; /* next frame gets smaller */
5031
5032 /*
5033 * Now make the other frames smaller.
5034 */
5035 while (fr != NULL && offset > 0)
5036 {
5037 n = frame_minheight(fr, NULL);
5038 if (fr->fr_height - offset <= n)
5039 {
5040 offset -= fr->fr_height - n;
5041 frame_new_height(fr, n, !up, FALSE);
5042 }
5043 else
5044 {
5045 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5046 break;
5047 }
5048 if (up)
5049 fr = fr->fr_prev;
5050 else
5051 fr = fr->fr_next;
5052 }
5053 row = win_comp_pos();
5054 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5055 cmdline_row = row;
5056 p_ch = Rows - cmdline_row;
5057 if (p_ch < 1)
5058 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005059 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005060 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 showmode();
5062}
5063
5064#ifdef FEAT_VERTSPLIT
5065/*
5066 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5067 */
5068 void
5069win_drag_vsep_line(dragwin, offset)
5070 win_T *dragwin;
5071 int offset;
5072{
5073 frame_T *curfr;
5074 frame_T *fr;
5075 int room;
5076 int left; /* if TRUE, drag separator line left, otherwise right */
5077 int n;
5078
5079 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005080 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 return;
5082 curfr = fr;
5083 fr = fr->fr_parent;
5084 /* When the parent frame is not a row of frames, its parent should be. */
5085 if (fr->fr_layout != FR_ROW)
5086 {
5087 if (fr == topframe) /* only a column of windows (cannot happen?) */
5088 return;
5089 curfr = fr;
5090 fr = fr->fr_parent;
5091 }
5092
5093 /* If this is the last frame in a row, may want to resize a parent
5094 * frame instead. */
5095 while (curfr->fr_next == NULL)
5096 {
5097 if (fr == topframe)
5098 break;
5099 curfr = fr;
5100 fr = fr->fr_parent;
5101 if (fr != topframe)
5102 {
5103 curfr = fr;
5104 fr = fr->fr_parent;
5105 }
5106 }
5107
5108 if (offset < 0) /* drag left */
5109 {
5110 left = TRUE;
5111 offset = -offset;
5112 /* sum up the room of the current frame and left of it */
5113 room = 0;
5114 for (fr = fr->fr_child; ; fr = fr->fr_next)
5115 {
5116 room += fr->fr_width - frame_minwidth(fr, NULL);
5117 if (fr == curfr)
5118 break;
5119 }
5120 fr = curfr->fr_next; /* put fr at frame that grows */
5121 }
5122 else /* drag right */
5123 {
5124 left = FALSE;
5125 /* sum up the room of frames right of the current one */
5126 room = 0;
5127 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5128 room += fr->fr_width - frame_minwidth(fr, NULL);
5129 fr = curfr; /* put fr at window that grows */
5130 }
5131
5132 if (room < offset) /* Not enough room */
5133 offset = room; /* Move as far as we can */
5134 if (offset <= 0) /* No room at all, quit. */
5135 return;
5136
5137 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005138 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 /* shrink other frames: current and at the left or at the right */
5141 if (left)
5142 fr = curfr; /* current frame gets smaller */
5143 else
5144 fr = curfr->fr_next; /* next frame gets smaller */
5145
5146 while (fr != NULL && offset > 0)
5147 {
5148 n = frame_minwidth(fr, NULL);
5149 if (fr->fr_width - offset <= n)
5150 {
5151 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005152 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 else
5155 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005156 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158 }
5159 if (left)
5160 fr = fr->fr_prev;
5161 else
5162 fr = fr->fr_next;
5163 }
5164 (void)win_comp_pos();
5165 redraw_all_later(NOT_VALID);
5166}
5167#endif /* FEAT_VERTSPLIT */
5168#endif /* FEAT_MOUSE */
5169
5170#endif /* FEAT_WINDOWS */
5171
5172/*
5173 * Set the height of a window.
5174 * This takes care of the things inside the window, not what happens to the
5175 * window position, the frame or to other windows.
5176 */
5177 static void
5178win_new_height(wp, height)
5179 win_T *wp;
5180 int height;
5181{
5182 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005183 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005185 int space;
5186 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#define FRACTION_MULT 16384L
5188
5189 /* Don't want a negative height. Happens when splitting a tiny window.
5190 * Will equalize heights soon to fix it. */
5191 if (height < 0)
5192 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005193 if (wp->w_height == height)
5194 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5197 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5198 + FRACTION_MULT / 2) / (long)wp->w_height;
5199
5200 wp->w_height = height;
5201 wp->w_skipcol = 0;
5202
5203 /* Don't change w_topline when height is zero. Don't set w_topline when
5204 * 'scrollbind' is set and this isn't the current window. */
5205 if (height > 0
5206#ifdef FEAT_SCROLLBIND
5207 && (!wp->w_p_scb || wp == curwin)
5208#endif
5209 )
5210 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005211 /*
5212 * Find a value for w_topline that shows the cursor at the same
5213 * relative position in the window as before (more or less).
5214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 lnum = wp->w_cursor.lnum;
5216 if (lnum < 1) /* can happen when starting up */
5217 lnum = 1;
5218 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5219 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5220 sline = wp->w_wrow - line_size;
5221 if (sline < 0)
5222 {
5223 /*
5224 * Cursor line would go off top of screen if w_wrow was this high.
5225 */
5226 wp->w_wrow = line_size;
5227 }
5228 else
5229 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005230 space = height;
5231 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005233 space -= line_size;
5234 if (space > 0 && sline <= 0 && !did_below)
5235 {
5236 /* Try to use "~" lines below the text to avoid that text
5237 * is above the window while there are empty lines.
5238 * Subtract the rows below the cursor from "space" and
5239 * give the rest to "sline". */
5240 did_below = TRUE;
5241 bot = wp->w_cursor.lnum;
5242 while (space > 0)
5243 {
5244 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5245 space = 0;
5246 else
5247 {
5248#ifdef FEAT_FOLDING
5249 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5250#endif
5251 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5252 break;
5253 ++bot;
5254 space -= plines_win(wp, bot, TRUE);
5255 }
5256 }
5257 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5258 sline += space;
5259 }
5260 if (sline <= 0)
5261 break;
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#ifdef FEAT_FOLDING
5264 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5265 if (lnum == 1)
5266 {
5267 /* first line in buffer is folded */
5268 line_size = 1;
5269 --sline;
5270 break;
5271 }
5272#endif
5273 --lnum;
5274#ifdef FEAT_DIFF
5275 if (lnum == wp->w_topline)
5276 line_size = plines_win_nofill(wp, lnum, TRUE)
5277 + wp->w_topfill;
5278 else
5279#endif
5280 line_size = plines_win(wp, lnum, TRUE);
5281 sline -= line_size;
5282 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (sline < 0)
5285 {
5286 /*
5287 * Line we want at top would go off top of screen. Use next
5288 * line instead.
5289 */
5290#ifdef FEAT_FOLDING
5291 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5292#endif
5293 lnum++;
5294 wp->w_wrow -= line_size + sline;
5295 }
5296 else if (sline > 0)
5297 {
5298 /* First line of file reached, use that as topline. */
5299 lnum = 1;
5300 wp->w_wrow -= sline;
5301 }
5302 }
5303 set_topline(wp, lnum);
5304 }
5305
5306 if (wp == curwin)
5307 {
5308 if (p_so)
5309 update_topline();
5310 curs_columns(FALSE); /* validate w_wrow */
5311 }
5312 wp->w_prev_fraction_row = wp->w_wrow;
5313
5314 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005315 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#ifdef FEAT_WINDOWS
5317 wp->w_redr_status = TRUE;
5318#endif
5319 invalidate_botline_win(wp);
5320}
5321
5322#ifdef FEAT_VERTSPLIT
5323/*
5324 * Set the width of a window.
5325 */
5326 static void
5327win_new_width(wp, width)
5328 win_T *wp;
5329 int width;
5330{
5331 wp->w_width = width;
5332 wp->w_lines_valid = 0;
5333 changed_line_abv_curs_win(wp);
5334 invalidate_botline_win(wp);
5335 if (wp == curwin)
5336 {
5337 update_topline();
5338 curs_columns(TRUE); /* validate w_wrow */
5339 }
5340 redraw_win_later(wp, NOT_VALID);
5341 wp->w_redr_status = TRUE;
5342}
5343#endif
5344
5345 void
5346win_comp_scroll(wp)
5347 win_T *wp;
5348{
5349 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5350 if (wp->w_p_scr == 0)
5351 wp->w_p_scr = 1;
5352}
5353
5354/*
5355 * command_height: called whenever p_ch has been changed
5356 */
5357 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005358command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360#ifdef FEAT_WINDOWS
5361 int h;
5362 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005363 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005365 /* Use the value of p_ch that we remembered. This is needed for when the
5366 * GUI starts up, we can't be sure in what order things happen. And when
5367 * p_ch was changed in another tab page. */
5368 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 /* Find bottom frame with width of screen. */
5371 frp = lastwin->w_frame;
5372# ifdef FEAT_VERTSPLIT
5373 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5374 frp = frp->fr_parent;
5375# endif
5376
5377 /* Avoid changing the height of a window with 'winfixheight' set. */
5378 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5379 && frp->fr_win->w_p_wfh)
5380 frp = frp->fr_prev;
5381
5382 if (starting != NO_SCREEN)
5383 {
5384 cmdline_row = Rows - p_ch;
5385
5386 if (p_ch > old_p_ch) /* p_ch got bigger */
5387 {
5388 while (p_ch > old_p_ch)
5389 {
5390 if (frp == NULL)
5391 {
5392 EMSG(_(e_noroom));
5393 p_ch = old_p_ch;
5394 cmdline_row = Rows - p_ch;
5395 break;
5396 }
5397 h = frp->fr_height - frame_minheight(frp, NULL);
5398 if (h > p_ch - old_p_ch)
5399 h = p_ch - old_p_ch;
5400 old_p_ch += h;
5401 frame_add_height(frp, -h);
5402 frp = frp->fr_prev;
5403 }
5404
5405 /* Recompute window positions. */
5406 (void)win_comp_pos();
5407
5408 /* clear the lines added to cmdline */
5409 if (full_screen)
5410 screen_fill((int)(cmdline_row), (int)Rows, 0,
5411 (int)Columns, ' ', ' ', 0);
5412 msg_row = cmdline_row;
5413 redraw_cmdline = TRUE;
5414 return;
5415 }
5416
5417 if (msg_row < cmdline_row)
5418 msg_row = cmdline_row;
5419 redraw_cmdline = TRUE;
5420 }
5421 frame_add_height(frp, (int)(old_p_ch - p_ch));
5422
5423 /* Recompute window positions. */
5424 if (frp != lastwin->w_frame)
5425 (void)win_comp_pos();
5426#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005428 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
5430}
5431
5432#if defined(FEAT_WINDOWS) || defined(PROTO)
5433/*
5434 * Resize frame "frp" to be "n" lines higher (negative for less high).
5435 * Also resize the frames it is contained in.
5436 */
5437 static void
5438frame_add_height(frp, n)
5439 frame_T *frp;
5440 int n;
5441{
5442 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5443 for (;;)
5444 {
5445 frp = frp->fr_parent;
5446 if (frp == NULL)
5447 break;
5448 frp->fr_height += n;
5449 }
5450}
5451
5452/*
5453 * Add or remove a status line for the bottom window(s), according to the
5454 * value of 'laststatus'.
5455 */
5456 void
5457last_status(morewin)
5458 int morewin; /* pretend there are two or more windows */
5459{
5460 /* Don't make a difference between horizontal or vertical split. */
5461 last_status_rec(topframe, (p_ls == 2
5462 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5463}
5464
5465 static void
5466last_status_rec(fr, statusline)
5467 frame_T *fr;
5468 int statusline;
5469{
5470 frame_T *fp;
5471 win_T *wp;
5472
5473 if (fr->fr_layout == FR_LEAF)
5474 {
5475 wp = fr->fr_win;
5476 if (wp->w_status_height != 0 && !statusline)
5477 {
5478 /* remove status line */
5479 win_new_height(wp, wp->w_height + 1);
5480 wp->w_status_height = 0;
5481 comp_col();
5482 }
5483 else if (wp->w_status_height == 0 && statusline)
5484 {
5485 /* Find a frame to take a line from. */
5486 fp = fr;
5487 while (fp->fr_height <= frame_minheight(fp, NULL))
5488 {
5489 if (fp == topframe)
5490 {
5491 EMSG(_(e_noroom));
5492 return;
5493 }
5494 /* In a column of frames: go to frame above. If already at
5495 * the top or in a row of frames: go to parent. */
5496 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5497 fp = fp->fr_prev;
5498 else
5499 fp = fp->fr_parent;
5500 }
5501 wp->w_status_height = 1;
5502 if (fp != fr)
5503 {
5504 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5505 frame_fix_height(wp);
5506 (void)win_comp_pos();
5507 }
5508 else
5509 win_new_height(wp, wp->w_height - 1);
5510 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005511 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513 }
5514#ifdef FEAT_VERTSPLIT
5515 else if (fr->fr_layout == FR_ROW)
5516 {
5517 /* vertically split windows, set status line for each one */
5518 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5519 last_status_rec(fp, statusline);
5520 }
5521#endif
5522 else
5523 {
5524 /* horizontally split window, set status line for last one */
5525 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5526 ;
5527 last_status_rec(fp, statusline);
5528 }
5529}
5530
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005531/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005532 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005533 */
5534 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005535tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005536{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005537#ifdef FEAT_GUI_TABLINE
5538 /* When the GUI has the tabline then this always returns zero. */
5539 if (gui_use_tabline())
5540 return 0;
5541#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005542 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005543 {
5544 case 0: return 0;
5545 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5546 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005547 return 1;
5548}
5549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#endif /* FEAT_WINDOWS */
5551
5552#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5553/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005554 * Get the file name at the cursor.
5555 * If Visual mode is active, use the selected text if it's in one line.
5556 * Returns the name in allocated memory, NULL for failure.
5557 */
5558 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005559grab_file_name(count, file_lnum)
5560 long count;
5561 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005562{
5563# ifdef FEAT_VISUAL
5564 if (VIsual_active)
5565 {
5566 int len;
5567 char_u *ptr;
5568
5569 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5570 return NULL;
5571 return find_file_name_in_path(ptr, len,
5572 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5573 }
5574# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005575 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5576 file_lnum);
5577
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005578}
5579
5580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 * Return the file name under or after the cursor.
5582 *
5583 * The 'path' option is searched if the file name is not absolute.
5584 * The string returned has been alloc'ed and should be freed by the caller.
5585 * NULL is returned if the file name or file is not found.
5586 *
5587 * options:
5588 * FNAME_MESS give error messages
5589 * FNAME_EXP expand to path
5590 * FNAME_HYP check for hypertext link
5591 * FNAME_INCL apply "includeexpr"
5592 */
5593 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005594file_name_at_cursor(options, count, file_lnum)
5595 int options;
5596 long count;
5597 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005600 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5601 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602}
5603
5604/*
5605 * Return the name of the file under or after ptr[col].
5606 * Otherwise like file_name_at_cursor().
5607 */
5608 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005609file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 char_u *line;
5611 int col;
5612 int options;
5613 long count;
5614 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005615 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 char_u *ptr;
5618 int len;
5619
5620 /*
5621 * search forward for what could be the start of a file name
5622 */
5623 ptr = line + col;
5624 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005625 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 if (*ptr == NUL) /* nothing found */
5627 {
5628 if (options & FNAME_MESS)
5629 EMSG(_("E446: No file name under cursor"));
5630 return NULL;
5631 }
5632
5633 /*
5634 * Search backward for first char of the file name.
5635 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5636 */
5637 while (ptr > line)
5638 {
5639#ifdef FEAT_MBYTE
5640 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5641 ptr -= len + 1;
5642 else
5643#endif
5644 if (vim_isfilec(ptr[-1])
5645 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5646 --ptr;
5647 else
5648 break;
5649 }
5650
5651 /*
5652 * Search forward for the last char of the file name.
5653 * Also allow "://" when ':' is not in 'isfname'.
5654 */
5655 len = 0;
5656 while (vim_isfilec(ptr[len])
5657 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5658#ifdef FEAT_MBYTE
5659 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005660 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
5662#endif
5663 ++len;
5664
5665 /*
5666 * If there is trailing punctuation, remove it.
5667 * But don't remove "..", could be a directory name.
5668 */
5669 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5670 && ptr[len - 2] != '.')
5671 --len;
5672
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005673 if (file_lnum != NULL)
5674 {
5675 char_u *p;
5676
5677 /* Get the number after the file name and a separator character */
5678 p = ptr + len;
5679 p = skipwhite(p);
5680 if (*p != NUL)
5681 {
5682 if (!isdigit(*p))
5683 ++p; /* skip the separator */
5684 p = skipwhite(p);
5685 if (isdigit(*p))
5686 *file_lnum = (int)getdigits(&p);
5687 }
5688 }
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5691}
5692
5693# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5694static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5695
5696 static char_u *
5697eval_includeexpr(ptr, len)
5698 char_u *ptr;
5699 int len;
5700{
5701 char_u *res;
5702
5703 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005704 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005705 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 set_vim_var_string(VV_FNAME, NULL, 0);
5707 return res;
5708}
5709#endif
5710
5711/*
5712 * Return the name of the file ptr[len] in 'path'.
5713 * Otherwise like file_name_at_cursor().
5714 */
5715 char_u *
5716find_file_name_in_path(ptr, len, options, count, rel_fname)
5717 char_u *ptr;
5718 int len;
5719 int options;
5720 long count;
5721 char_u *rel_fname; /* file we are searching relative to */
5722{
5723 char_u *file_name;
5724 int c;
5725# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5726 char_u *tofree = NULL;
5727
5728 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5729 {
5730 tofree = eval_includeexpr(ptr, len);
5731 if (tofree != NULL)
5732 {
5733 ptr = tofree;
5734 len = (int)STRLEN(ptr);
5735 }
5736 }
5737# endif
5738
5739 if (options & FNAME_EXP)
5740 {
5741 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5742 TRUE, rel_fname);
5743
5744# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5745 /*
5746 * If the file could not be found in a normal way, try applying
5747 * 'includeexpr' (unless done already).
5748 */
5749 if (file_name == NULL
5750 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5751 {
5752 tofree = eval_includeexpr(ptr, len);
5753 if (tofree != NULL)
5754 {
5755 ptr = tofree;
5756 len = (int)STRLEN(ptr);
5757 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5758 TRUE, rel_fname);
5759 }
5760 }
5761# endif
5762 if (file_name == NULL && (options & FNAME_MESS))
5763 {
5764 c = ptr[len];
5765 ptr[len] = NUL;
5766 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5767 ptr[len] = c;
5768 }
5769
5770 /* Repeat finding the file "count" times. This matters when it
5771 * appears several times in the path. */
5772 while (file_name != NULL && --count > 0)
5773 {
5774 vim_free(file_name);
5775 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5776 }
5777 }
5778 else
5779 file_name = vim_strnsave(ptr, len);
5780
5781# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5782 vim_free(tofree);
5783# endif
5784
5785 return file_name;
5786}
5787#endif /* FEAT_SEARCHPATH */
5788
5789/*
5790 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5791 * Also check for ":\\", which MS Internet Explorer accepts, return
5792 * URL_BACKSLASH.
5793 */
5794 static int
5795path_is_url(p)
5796 char_u *p;
5797{
5798 if (STRNCMP(p, "://", (size_t)3) == 0)
5799 return URL_SLASH;
5800 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5801 return URL_BACKSLASH;
5802 return 0;
5803}
5804
5805/*
5806 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5807 * Return URL_BACKSLASH for "name:\\".
5808 * Return zero otherwise.
5809 */
5810 int
5811path_with_url(fname)
5812 char_u *fname;
5813{
5814 char_u *p;
5815
5816 for (p = fname; isalpha(*p); ++p)
5817 ;
5818 return path_is_url(p);
5819}
5820
5821/*
5822 * Return TRUE if "name" is a full (absolute) path name or URL.
5823 */
5824 int
5825vim_isAbsName(name)
5826 char_u *name;
5827{
5828 return (path_with_url(name) != 0 || mch_isFullName(name));
5829}
5830
5831/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005832 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 *
5834 * return FAIL for failure, OK otherwise
5835 */
5836 int
5837vim_FullName(fname, buf, len, force)
5838 char_u *fname, *buf;
5839 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005840 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 int retval = OK;
5843 int url;
5844
5845 *buf = NUL;
5846 if (fname == NULL)
5847 return FAIL;
5848
5849 url = path_with_url(fname);
5850 if (!url)
5851 retval = mch_FullName(fname, buf, len, force);
5852 if (url || retval == FAIL)
5853 {
5854 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005855 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5858 slash_adjust(buf);
5859#endif
5860 return retval;
5861}
5862
5863/*
5864 * Return the minimal number of rows that is needed on the screen to display
5865 * the current number of windows.
5866 */
5867 int
5868min_rows()
5869{
5870 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005871#ifdef FEAT_WINDOWS
5872 tabpage_T *tp;
5873 int n;
5874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 if (firstwin == NULL) /* not initialized yet */
5877 return MIN_LINES;
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005880 total = 0;
5881 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5882 {
5883 n = frame_minheight(tp->tp_topframe, NULL);
5884 if (total < n)
5885 total = n;
5886 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005887 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005889 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005891 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 return total;
5893}
5894
5895/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005896 * Return TRUE if there is only one window (in the current tab page), not
5897 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 */
5899 int
5900only_one_window()
5901{
5902#ifdef FEAT_WINDOWS
5903 int count = 0;
5904 win_T *wp;
5905
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906 /* If there is another tab page there always is another window. */
5907 if (first_tabpage->tp_next != NULL)
5908 return FALSE;
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005911 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912# ifdef FEAT_QUICKFIX
5913 || wp->w_p_pvw
5914# endif
5915 ) || wp == curwin)
5916 ++count;
5917 return (count <= 1);
5918#else
5919 return TRUE;
5920#endif
5921}
5922
5923#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5924/*
5925 * Correct the cursor line number in other windows. Used after changing the
5926 * current buffer, and before applying autocommands.
5927 * When "do_curwin" is TRUE, also check current window.
5928 */
5929 void
5930check_lnums(do_curwin)
5931 int do_curwin;
5932{
5933 win_T *wp;
5934
5935#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005936 tabpage_T *tp;
5937
5938 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5940#else
5941 wp = curwin;
5942 if (do_curwin)
5943#endif
5944 {
5945 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5946 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5947 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5948 wp->w_topline = curbuf->b_ml.ml_line_count;
5949 }
5950}
5951#endif
5952
5953#if defined(FEAT_WINDOWS) || defined(PROTO)
5954
5955/*
5956 * A snapshot of the window sizes, to restore them after closing the help
5957 * window.
5958 * Only these fields are used:
5959 * fr_layout
5960 * fr_width
5961 * fr_height
5962 * fr_next
5963 * fr_child
5964 * fr_win (only valid for the old curwin, NULL otherwise)
5965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
5967/*
5968 * Create a snapshot of the current frame sizes.
5969 */
5970 static void
5971make_snapshot()
5972{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005973 clear_snapshot(curtab);
5974 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975}
5976
5977 static void
5978make_snapshot_rec(fr, frp)
5979 frame_T *fr;
5980 frame_T **frp;
5981{
5982 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5983 if (*frp == NULL)
5984 return;
5985 (*frp)->fr_layout = fr->fr_layout;
5986# ifdef FEAT_VERTSPLIT
5987 (*frp)->fr_width = fr->fr_width;
5988# endif
5989 (*frp)->fr_height = fr->fr_height;
5990 if (fr->fr_next != NULL)
5991 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5992 if (fr->fr_child != NULL)
5993 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5994 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5995 (*frp)->fr_win = curwin;
5996}
5997
5998/*
5999 * Remove any existing snapshot.
6000 */
6001 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006002clear_snapshot(tp)
6003 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006005 clear_snapshot_rec(tp->tp_snapshot);
6006 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007}
6008
6009 static void
6010clear_snapshot_rec(fr)
6011 frame_T *fr;
6012{
6013 if (fr != NULL)
6014 {
6015 clear_snapshot_rec(fr->fr_next);
6016 clear_snapshot_rec(fr->fr_child);
6017 vim_free(fr);
6018 }
6019}
6020
6021/*
6022 * Restore a previously created snapshot, if there is any.
6023 * This is only done if the screen size didn't change and the window layout is
6024 * still the same.
6025 */
6026 static void
6027restore_snapshot(close_curwin)
6028 int close_curwin; /* closing current window */
6029{
6030 win_T *wp;
6031
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006032 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006034 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006036 && curtab->tp_snapshot->fr_height == topframe->fr_height
6037 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006039 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 win_comp_pos();
6041 if (wp != NULL && close_curwin)
6042 win_goto(wp);
6043 redraw_all_later(CLEAR);
6044 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006045 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046}
6047
6048/*
6049 * Check if frames "sn" and "fr" have the same layout, same following frames
6050 * and same children.
6051 */
6052 static int
6053check_snapshot_rec(sn, fr)
6054 frame_T *sn;
6055 frame_T *fr;
6056{
6057 if (sn->fr_layout != fr->fr_layout
6058 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6059 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6060 || (sn->fr_next != NULL
6061 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6062 || (sn->fr_child != NULL
6063 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6064 return FAIL;
6065 return OK;
6066}
6067
6068/*
6069 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6070 * following frames and children.
6071 * Returns a pointer to the old current window, or NULL.
6072 */
6073 static win_T *
6074restore_snapshot_rec(sn, fr)
6075 frame_T *sn;
6076 frame_T *fr;
6077{
6078 win_T *wp = NULL;
6079 win_T *wp2;
6080
6081 fr->fr_height = sn->fr_height;
6082# ifdef FEAT_VERTSPLIT
6083 fr->fr_width = sn->fr_width;
6084# endif
6085 if (fr->fr_layout == FR_LEAF)
6086 {
6087 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6088# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006089 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090# endif
6091 wp = sn->fr_win;
6092 }
6093 if (sn->fr_next != NULL)
6094 {
6095 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6096 if (wp2 != NULL)
6097 wp = wp2;
6098 }
6099 if (sn->fr_child != NULL)
6100 {
6101 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6102 if (wp2 != NULL)
6103 wp = wp2;
6104 }
6105 return wp;
6106}
6107
6108#endif
6109
6110#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6111/*
6112 * Return TRUE if there is any vertically split window.
6113 */
6114 int
6115win_hasvertsplit()
6116{
6117 frame_T *fr;
6118
6119 if (topframe->fr_layout == FR_ROW)
6120 return TRUE;
6121
6122 if (topframe->fr_layout == FR_COL)
6123 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6124 if (fr->fr_layout == FR_ROW)
6125 return TRUE;
6126
6127 return FALSE;
6128}
6129#endif