blob: 8c031bf6257d2316a7980bcdd0c4a7f3f1e01bc1 [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;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002006 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002008 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 EMSG(_("E444: Cannot close last window"));
2011 return;
2012 }
2013
2014 /* When closing the help window, try restoring a snapshot after closing
2015 * the window. Otherwise clear the snapshot, it's now invalid. */
2016 if (win->w_buffer->b_help)
2017 help_window = TRUE;
2018 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002019 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021#ifdef FEAT_AUTOCMD
2022 if (win == curwin)
2023 {
2024 /*
2025 * Guess which window is going to be the new current window.
2026 * This may change because of the autocommands (sigh).
2027 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029
2030 /*
2031 * Be careful: If autocommands delete the window, return now.
2032 */
2033 if (wp->w_buffer != curbuf)
2034 {
2035 other_buffer = TRUE;
2036 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002037 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 return;
2039 }
2040 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002041 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 return;
2043# ifdef FEAT_EVAL
2044 /* autocmds may abort script processing */
2045 if (aborting())
2046 return;
2047# endif
2048 }
2049#endif
2050
2051 /*
2052 * Close the link to the buffer.
2053 */
2054 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002057 * other window or moved to another tab page. */
2058 if (!win_valid(win) || last_window() || curtab != prev_curtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 return;
2060
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002061 /* When closing the last window in a tab page go to another tab page. This
2062 * must be done before freeing memory to avoid that "topframe" becomes
2063 * invalid (it may be used in GUI events). */
2064 if (firstwin == lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002065 {
2066 tabpage_T *ptp = NULL;
2067 tabpage_T *tp;
2068 tabpage_T *atp = alt_tabpage();
2069
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002070 /* We don't do the window resizing stuff, let enter_tabpage() take
2071 * care of entering a window in another tab page. */
2072 enter_tabpage(atp, old_curbuf);
2073
2074 for (tp = first_tabpage; tp != NULL && tp != prev_curtab;
2075 tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002076 ptp = tp;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002077 if (tp != prev_curtab || tp->tp_firstwin != win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002078 {
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002079 /* Autocommands must have closed it when jumping to the other tab
2080 * page. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002081 return;
2082 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002083
2084 (void)win_free_mem(win, &dir, tp);
2085
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002086 if (ptp == NULL)
2087 first_tabpage = tp->tp_next;
2088 else
2089 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002090 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002091
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002092 return;
2093 }
2094
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002095 /* Free the memory used for the window. */
2096 wp = win_free_mem(win, &dir, NULL);
2097
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 /* Make sure curwin isn't invalid. It can cause severe trouble when
2099 * printing an error message. For win_equal() curbuf needs to be valid
2100 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002101 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 curwin = wp;
2104#ifdef FEAT_QUICKFIX
2105 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2106 {
2107 /*
2108 * The cursor goes to the preview or the quickfix window, try
2109 * finding another window to go to.
2110 */
2111 for (;;)
2112 {
2113 if (wp->w_next == NULL)
2114 wp = firstwin;
2115 else
2116 wp = wp->w_next;
2117 if (wp == curwin)
2118 break;
2119 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2120 {
2121 curwin = wp;
2122 break;
2123 }
2124 }
2125 }
2126#endif
2127 curbuf = curwin->w_buffer;
2128 close_curwin = TRUE;
2129 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002130 if (p_ea
2131#ifdef FEAT_VERTSPLIT
2132 && (*p_ead == 'b' || *p_ead == dir)
2133#endif
2134 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 win_equal(curwin, TRUE,
2136#ifdef FEAT_VERTSPLIT
2137 dir
2138#else
2139 0
2140#endif
2141 );
2142 else
2143 win_comp_pos();
2144 if (close_curwin)
2145 {
2146 win_enter_ext(wp, FALSE, TRUE);
2147#ifdef FEAT_AUTOCMD
2148 if (other_buffer)
2149 /* careful: after this wp and win may be invalid! */
2150 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2151#endif
2152 }
2153
2154 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002155 * If last window has a status line now and we don't want one,
2156 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
2158 last_status(FALSE);
2159
2160 /* After closing the help window, try restoring the window layout from
2161 * before it was opened. */
2162 if (help_window)
2163 restore_snapshot(close_curwin);
2164
2165#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2166 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2167 if (gui.in_use && !win_hasvertsplit())
2168 gui_init_which_components(NULL);
2169#endif
2170
2171 redraw_all_later(NOT_VALID);
2172}
2173
2174/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002175 * Close window "win" in tab page "tp", which is not the current tab page.
2176 * This may be the last window ih that tab page and result in closing the tab,
2177 * thus "tp" may become invalid!
2178 * Called must check if buffer is hidden.
2179 */
2180 void
2181win_close_othertab(win, free_buf, tp)
2182 win_T *win;
2183 int free_buf;
2184 tabpage_T *tp;
2185{
2186 win_T *wp;
2187 int dir;
2188 tabpage_T *ptp = NULL;
2189
2190 /* Close the link to the buffer. */
2191 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2192
2193 /* Careful: Autocommands may have closed the tab page or made it the
2194 * current tab page. */
2195 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2196 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002197 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002198 return;
2199
2200 /* Autocommands may have closed the window already. */
2201 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2202 ;
2203 if (wp == NULL)
2204 return;
2205
2206 /* Free the memory used for the window. */
2207 wp = win_free_mem(win, &dir, tp);
2208
2209 /* When closing the last window in a tab page remove the tab page. */
2210 if (wp == NULL)
2211 {
2212 if (tp == first_tabpage)
2213 first_tabpage = tp->tp_next;
2214 else
2215 {
2216 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2217 ptp = ptp->tp_next)
2218 ;
2219 if (ptp == NULL)
2220 {
2221 EMSG2(_(e_intern2), "win_close_othertab()");
2222 return;
2223 }
2224 ptp->tp_next = tp->tp_next;
2225 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002226 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002227 }
2228}
2229
2230/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002231 * Free the memory used for a window.
2232 * Returns a pointer to the window that got the freed up space.
2233 */
2234 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002235win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002236 win_T *win;
2237 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002238 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002239{
2240 frame_T *frp;
2241 win_T *wp;
2242
Bram Moolenaarea408852005-06-25 22:49:46 +00002243#ifdef FEAT_FOLDING
2244 clearFolding(win);
2245#endif
2246
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002247 /* reduce the reference count to the argument list. */
2248 alist_unlink(win->w_alist);
2249
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002250 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002251 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002252 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002253 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002254 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002255
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256 /* When deleting the current window of another tab page select a new
2257 * current window. */
2258 if (tp != NULL && win == tp->tp_curwin)
2259 tp->tp_curwin = wp;
2260
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002261 return wp;
2262}
2263
2264#if defined(EXITFREE) || defined(PROTO)
2265 void
2266win_free_all()
2267{
2268 int dummy;
2269
Bram Moolenaarf740b292006-02-16 22:11:02 +00002270# ifdef FEAT_WINDOWS
2271 while (first_tabpage->tp_next != NULL)
2272 tabpage_close(TRUE);
2273# endif
2274
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002275 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002276 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002277}
2278#endif
2279
2280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * Remove a window and its frame from the tree of frames.
2282 * Returns a pointer to the window that got the freed up space.
2283 */
2284/*ARGSUSED*/
2285 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 win_T *win;
2288 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002289 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 frame_T *frp, *frp2, *frp3;
2292 frame_T *frp_close = win->w_frame;
2293 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002294 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002297 * If there is only one window there is nothing to remove.
2298 */
2299 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2300 return NULL;
2301
2302 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 * Remove the window from its frame.
2304 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002305 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 wp = frame2win(frp2);
2307
2308 /* Remove this frame from the list of frames. */
2309 frame_remove(frp_close);
2310
2311#ifdef FEAT_VERTSPLIT
2312 if (frp_close->fr_parent->fr_layout == FR_COL)
2313 {
2314#endif
2315 /* When 'winfixheight' is set, remember its old size and restore
2316 * it later (it's a simplistic solution...). Don't do this if the
2317 * window will occupy the full height of the screen. */
2318 if (frp2->fr_win != NULL
2319 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2320 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002321 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2323 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002324 if (old_size != 0)
2325 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#ifdef FEAT_VERTSPLIT
2327 *dirp = 'v';
2328 }
2329 else
2330 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002331 /* When 'winfixwidth' is set, remember its old size and restore
2332 * it later (it's a simplistic solution...). Don't do this if the
2333 * window will occupy the full width of the screen. */
2334 if (frp2->fr_win != NULL
2335 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2336 && frp2->fr_win->w_p_wfw)
2337 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002339 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2340 if (old_size != 0)
2341 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 *dirp = 'h';
2343 }
2344#endif
2345
2346 /* If rows/columns go to a window below/right its positions need to be
2347 * updated. Can only be done after the sizes have been updated. */
2348 if (frp2 == frp_close->fr_next)
2349 {
2350 int row = win->w_winrow;
2351 int col = W_WINCOL(win);
2352
2353 frame_comp_pos(frp2, &row, &col);
2354 }
2355
2356 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2357 {
2358 /* There is no other frame in this list, move its info to the parent
2359 * and remove it. */
2360 frp2->fr_parent->fr_layout = frp2->fr_layout;
2361 frp2->fr_parent->fr_child = frp2->fr_child;
2362 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2363 frp->fr_parent = frp2->fr_parent;
2364 frp2->fr_parent->fr_win = frp2->fr_win;
2365 if (frp2->fr_win != NULL)
2366 frp2->fr_win->w_frame = frp2->fr_parent;
2367 frp = frp2->fr_parent;
2368 vim_free(frp2);
2369
2370 frp2 = frp->fr_parent;
2371 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2372 {
2373 /* The frame above the parent has the same layout, have to merge
2374 * the frames into this list. */
2375 if (frp2->fr_child == frp)
2376 frp2->fr_child = frp->fr_child;
2377 frp->fr_child->fr_prev = frp->fr_prev;
2378 if (frp->fr_prev != NULL)
2379 frp->fr_prev->fr_next = frp->fr_child;
2380 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2381 {
2382 frp3->fr_parent = frp2;
2383 if (frp3->fr_next == NULL)
2384 {
2385 frp3->fr_next = frp->fr_next;
2386 if (frp->fr_next != NULL)
2387 frp->fr_next->fr_prev = frp3;
2388 break;
2389 }
2390 }
2391 vim_free(frp);
2392 }
2393 }
2394
2395 return wp;
2396}
2397
2398/*
2399 * Find out which frame is going to get the freed up space when "win" is
2400 * closed.
2401 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2402 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2403 * This makes opening a window and closing it immediately keep the same window
2404 * layout.
2405 */
2406 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002409 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 frame_T *frp;
2412 int b;
2413
Bram Moolenaarf740b292006-02-16 22:11:02 +00002414 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002415 /* Last window in this tab page, will go to next tab page. */
2416 return alt_tabpage()->tp_curwin->w_frame;
2417
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 frp = win->w_frame;
2419#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002420 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 b = p_spr;
2422 else
2423#endif
2424 b = p_sb;
2425 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2426 return frp->fr_next;
2427 return frp->fr_prev;
2428}
2429
2430/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002431 * Return the tabpage that will be used if the current one is closed.
2432 */
2433 static tabpage_T *
2434alt_tabpage()
2435{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002436 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002437
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002438 /* Use the next tab page if possible. */
2439 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002440 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002441
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002442 /* Find the last but one tab page. */
2443 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2444 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002445 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002446}
2447
2448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 * Find the left-upper window in frame "frp".
2450 */
2451 static win_T *
2452frame2win(frp)
2453 frame_T *frp;
2454{
2455 while (frp->fr_win == NULL)
2456 frp = frp->fr_child;
2457 return frp->fr_win;
2458}
2459
2460/*
2461 * Return TRUE if frame "frp" contains window "wp".
2462 */
2463 static int
2464frame_has_win(frp, wp)
2465 frame_T *frp;
2466 win_T *wp;
2467{
2468 frame_T *p;
2469
2470 if (frp->fr_layout == FR_LEAF)
2471 return frp->fr_win == wp;
2472
2473 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2474 if (frame_has_win(p, wp))
2475 return TRUE;
2476 return FALSE;
2477}
2478
2479/*
2480 * Set a new height for a frame. Recursively sets the height for contained
2481 * frames and windows. Caller must take care of positions.
2482 */
2483 static void
2484frame_new_height(topfrp, height, topfirst, wfh)
2485 frame_T *topfrp;
2486 int height;
2487 int topfirst; /* resize topmost contained frame first */
2488 int wfh; /* obey 'winfixheight' when there is a choice;
2489 may cause the height not to be set */
2490{
2491 frame_T *frp;
2492 int extra_lines;
2493 int h;
2494
2495 if (topfrp->fr_win != NULL)
2496 {
2497 /* Simple case: just one window. */
2498 win_new_height(topfrp->fr_win,
2499 height - topfrp->fr_win->w_status_height);
2500 }
2501#ifdef FEAT_VERTSPLIT
2502 else if (topfrp->fr_layout == FR_ROW)
2503 {
2504 do
2505 {
2506 /* All frames in this row get the same new height. */
2507 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2508 {
2509 frame_new_height(frp, height, topfirst, wfh);
2510 if (frp->fr_height > height)
2511 {
2512 /* Could not fit the windows, make the whole row higher. */
2513 height = frp->fr_height;
2514 break;
2515 }
2516 }
2517 }
2518 while (frp != NULL);
2519 }
2520#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002521 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 /* Complicated case: Resize a column of frames. Resize the bottom
2524 * frame first, frames above that when needed. */
2525
2526 frp = topfrp->fr_child;
2527 if (wfh)
2528 /* Advance past frames with one window with 'wfh' set. */
2529 while (frame_fixed_height(frp))
2530 {
2531 frp = frp->fr_next;
2532 if (frp == NULL)
2533 return; /* no frame without 'wfh', give up */
2534 }
2535 if (!topfirst)
2536 {
2537 /* Find the bottom frame of this column */
2538 while (frp->fr_next != NULL)
2539 frp = frp->fr_next;
2540 if (wfh)
2541 /* Advance back for frames with one window with 'wfh' set. */
2542 while (frame_fixed_height(frp))
2543 frp = frp->fr_prev;
2544 }
2545
2546 extra_lines = height - topfrp->fr_height;
2547 if (extra_lines < 0)
2548 {
2549 /* reduce height of contained frames, bottom or top frame first */
2550 while (frp != NULL)
2551 {
2552 h = frame_minheight(frp, NULL);
2553 if (frp->fr_height + extra_lines < h)
2554 {
2555 extra_lines += frp->fr_height - h;
2556 frame_new_height(frp, h, topfirst, wfh);
2557 }
2558 else
2559 {
2560 frame_new_height(frp, frp->fr_height + extra_lines,
2561 topfirst, wfh);
2562 break;
2563 }
2564 if (topfirst)
2565 {
2566 do
2567 frp = frp->fr_next;
2568 while (wfh && frp != NULL && frame_fixed_height(frp));
2569 }
2570 else
2571 {
2572 do
2573 frp = frp->fr_prev;
2574 while (wfh && frp != NULL && frame_fixed_height(frp));
2575 }
2576 /* Increase "height" if we could not reduce enough frames. */
2577 if (frp == NULL)
2578 height -= extra_lines;
2579 }
2580 }
2581 else if (extra_lines > 0)
2582 {
2583 /* increase height of bottom or top frame */
2584 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2585 }
2586 }
2587 topfrp->fr_height = height;
2588}
2589
2590/*
2591 * Return TRUE if height of frame "frp" should not be changed because of
2592 * the 'winfixheight' option.
2593 */
2594 static int
2595frame_fixed_height(frp)
2596 frame_T *frp;
2597{
2598 /* frame with one window: fixed height if 'winfixheight' set. */
2599 if (frp->fr_win != NULL)
2600 return frp->fr_win->w_p_wfh;
2601
2602 if (frp->fr_layout == FR_ROW)
2603 {
2604 /* The frame is fixed height if one of the frames in the row is fixed
2605 * height. */
2606 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2607 if (frame_fixed_height(frp))
2608 return TRUE;
2609 return FALSE;
2610 }
2611
2612 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2613 * frames in the row are fixed height. */
2614 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2615 if (!frame_fixed_height(frp))
2616 return FALSE;
2617 return TRUE;
2618}
2619
2620#ifdef FEAT_VERTSPLIT
2621/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002622 * Return TRUE if width of frame "frp" should not be changed because of
2623 * the 'winfixwidth' option.
2624 */
2625 static int
2626frame_fixed_width(frp)
2627 frame_T *frp;
2628{
2629 /* frame with one window: fixed width if 'winfixwidth' set. */
2630 if (frp->fr_win != NULL)
2631 return frp->fr_win->w_p_wfw;
2632
2633 if (frp->fr_layout == FR_COL)
2634 {
2635 /* The frame is fixed width if one of the frames in the row is fixed
2636 * width. */
2637 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2638 if (frame_fixed_width(frp))
2639 return TRUE;
2640 return FALSE;
2641 }
2642
2643 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2644 * frames in the row are fixed width. */
2645 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2646 if (!frame_fixed_width(frp))
2647 return FALSE;
2648 return TRUE;
2649}
2650
2651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * Add a status line to windows at the bottom of "frp".
2653 * Note: Does not check if there is room!
2654 */
2655 static void
2656frame_add_statusline(frp)
2657 frame_T *frp;
2658{
2659 win_T *wp;
2660
2661 if (frp->fr_layout == FR_LEAF)
2662 {
2663 wp = frp->fr_win;
2664 if (wp->w_status_height == 0)
2665 {
2666 if (wp->w_height > 0) /* don't make it negative */
2667 --wp->w_height;
2668 wp->w_status_height = STATUS_HEIGHT;
2669 }
2670 }
2671 else if (frp->fr_layout == FR_ROW)
2672 {
2673 /* Handle all the frames in the row. */
2674 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2675 frame_add_statusline(frp);
2676 }
2677 else /* frp->fr_layout == FR_COL */
2678 {
2679 /* Only need to handle the last frame in the column. */
2680 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2681 ;
2682 frame_add_statusline(frp);
2683 }
2684}
2685
2686/*
2687 * Set width of a frame. Handles recursively going through contained frames.
2688 * May remove separator line for windows at the right side (for win_close()).
2689 */
2690 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002691frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 frame_T *topfrp;
2693 int width;
2694 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002695 int wfw; /* obey 'winfixwidth' when there is a choice;
2696 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 frame_T *frp;
2699 int extra_cols;
2700 int w;
2701 win_T *wp;
2702
2703 if (topfrp->fr_layout == FR_LEAF)
2704 {
2705 /* Simple case: just one window. */
2706 wp = topfrp->fr_win;
2707 /* Find out if there are any windows right of this one. */
2708 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2709 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2710 break;
2711 if (frp->fr_parent == NULL)
2712 wp->w_vsep_width = 0;
2713 win_new_width(wp, width - wp->w_vsep_width);
2714 }
2715 else if (topfrp->fr_layout == FR_COL)
2716 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002717 do
2718 {
2719 /* All frames in this column get the same new width. */
2720 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2721 {
2722 frame_new_width(frp, width, leftfirst, wfw);
2723 if (frp->fr_width > width)
2724 {
2725 /* Could not fit the windows, make whole column wider. */
2726 width = frp->fr_width;
2727 break;
2728 }
2729 }
2730 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 else /* fr_layout == FR_ROW */
2733 {
2734 /* Complicated case: Resize a row of frames. Resize the rightmost
2735 * frame first, frames left of it when needed. */
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002738 if (wfw)
2739 /* Advance past frames with one window with 'wfw' set. */
2740 while (frame_fixed_width(frp))
2741 {
2742 frp = frp->fr_next;
2743 if (frp == NULL)
2744 return; /* no frame without 'wfw', give up */
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002747 {
2748 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 while (frp->fr_next != NULL)
2750 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002751 if (wfw)
2752 /* Advance back for frames with one window with 'wfw' set. */
2753 while (frame_fixed_width(frp))
2754 frp = frp->fr_prev;
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 extra_cols = width - topfrp->fr_width;
2758 if (extra_cols < 0)
2759 {
2760 /* reduce frame width, rightmost frame first */
2761 while (frp != NULL)
2762 {
2763 w = frame_minwidth(frp, NULL);
2764 if (frp->fr_width + extra_cols < w)
2765 {
2766 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002767 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002771 frame_new_width(frp, frp->fr_width + extra_cols,
2772 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 break;
2774 }
2775 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002776 {
2777 do
2778 frp = frp->fr_next;
2779 while (wfw && frp != NULL && frame_fixed_width(frp));
2780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002782 {
2783 do
2784 frp = frp->fr_prev;
2785 while (wfw && frp != NULL && frame_fixed_width(frp));
2786 }
2787 /* Increase "width" if we could not reduce enough frames. */
2788 if (frp == NULL)
2789 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 }
2792 else if (extra_cols > 0)
2793 {
2794 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002795 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 }
2798 topfrp->fr_width = width;
2799}
2800
2801/*
2802 * Add the vertical separator to windows at the right side of "frp".
2803 * Note: Does not check if there is room!
2804 */
2805 static void
2806frame_add_vsep(frp)
2807 frame_T *frp;
2808{
2809 win_T *wp;
2810
2811 if (frp->fr_layout == FR_LEAF)
2812 {
2813 wp = frp->fr_win;
2814 if (wp->w_vsep_width == 0)
2815 {
2816 if (wp->w_width > 0) /* don't make it negative */
2817 --wp->w_width;
2818 wp->w_vsep_width = 1;
2819 }
2820 }
2821 else if (frp->fr_layout == FR_COL)
2822 {
2823 /* Handle all the frames in the column. */
2824 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2825 frame_add_vsep(frp);
2826 }
2827 else /* frp->fr_layout == FR_ROW */
2828 {
2829 /* Only need to handle the last frame in the row. */
2830 frp = frp->fr_child;
2831 while (frp->fr_next != NULL)
2832 frp = frp->fr_next;
2833 frame_add_vsep(frp);
2834 }
2835}
2836
2837/*
2838 * Set frame width from the window it contains.
2839 */
2840 static void
2841frame_fix_width(wp)
2842 win_T *wp;
2843{
2844 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2845}
2846#endif
2847
2848/*
2849 * Set frame height from the window it contains.
2850 */
2851 static void
2852frame_fix_height(wp)
2853 win_T *wp;
2854{
2855 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2856}
2857
2858/*
2859 * Compute the minimal height for frame "topfrp".
2860 * Uses the 'winminheight' option.
2861 * When "next_curwin" isn't NULL, use p_wh for this window.
2862 * When "next_curwin" is NOWIN, don't use at least one line for the current
2863 * window.
2864 */
2865 static int
2866frame_minheight(topfrp, next_curwin)
2867 frame_T *topfrp;
2868 win_T *next_curwin;
2869{
2870 frame_T *frp;
2871 int m;
2872#ifdef FEAT_VERTSPLIT
2873 int n;
2874#endif
2875
2876 if (topfrp->fr_win != NULL)
2877 {
2878 if (topfrp->fr_win == next_curwin)
2879 m = p_wh + topfrp->fr_win->w_status_height;
2880 else
2881 {
2882 /* window: minimal height of the window plus status line */
2883 m = p_wmh + topfrp->fr_win->w_status_height;
2884 /* Current window is minimal one line high */
2885 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2886 ++m;
2887 }
2888 }
2889#ifdef FEAT_VERTSPLIT
2890 else if (topfrp->fr_layout == FR_ROW)
2891 {
2892 /* get the minimal height from each frame in this row */
2893 m = 0;
2894 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2895 {
2896 n = frame_minheight(frp, next_curwin);
2897 if (n > m)
2898 m = n;
2899 }
2900 }
2901#endif
2902 else
2903 {
2904 /* Add up the minimal heights for all frames in this column. */
2905 m = 0;
2906 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2907 m += frame_minheight(frp, next_curwin);
2908 }
2909
2910 return m;
2911}
2912
2913#ifdef FEAT_VERTSPLIT
2914/*
2915 * Compute the minimal width for frame "topfrp".
2916 * When "next_curwin" isn't NULL, use p_wiw for this window.
2917 * When "next_curwin" is NOWIN, don't use at least one column for the current
2918 * window.
2919 */
2920 static int
2921frame_minwidth(topfrp, next_curwin)
2922 frame_T *topfrp;
2923 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2924{
2925 frame_T *frp;
2926 int m, n;
2927
2928 if (topfrp->fr_win != NULL)
2929 {
2930 if (topfrp->fr_win == next_curwin)
2931 m = p_wiw + topfrp->fr_win->w_vsep_width;
2932 else
2933 {
2934 /* window: minimal width of the window plus separator column */
2935 m = p_wmw + topfrp->fr_win->w_vsep_width;
2936 /* Current window is minimal one column wide */
2937 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2938 ++m;
2939 }
2940 }
2941 else if (topfrp->fr_layout == FR_COL)
2942 {
2943 /* get the minimal width from each frame in this column */
2944 m = 0;
2945 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2946 {
2947 n = frame_minwidth(frp, next_curwin);
2948 if (n > m)
2949 m = n;
2950 }
2951 }
2952 else
2953 {
2954 /* Add up the minimal widths for all frames in this row. */
2955 m = 0;
2956 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2957 m += frame_minwidth(frp, next_curwin);
2958 }
2959
2960 return m;
2961}
2962#endif
2963
2964
2965/*
2966 * Try to close all windows except current one.
2967 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2968 * used and the buffer was modified.
2969 *
2970 * Used by ":bdel" and ":only".
2971 */
2972 void
2973close_others(message, forceit)
2974 int message;
2975 int forceit; /* always hide all other windows */
2976{
2977 win_T *wp;
2978 win_T *nextwp;
2979 int r;
2980
2981 if (lastwin == firstwin)
2982 {
2983 if (message
2984#ifdef FEAT_AUTOCMD
2985 && !autocmd_busy
2986#endif
2987 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00002988 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 return;
2990 }
2991
2992 /* Be very careful here: autocommands may change the window layout. */
2993 for (wp = firstwin; win_valid(wp); wp = nextwp)
2994 {
2995 nextwp = wp->w_next;
2996 if (wp != curwin) /* don't close current window */
2997 {
2998
2999 /* Check if it's allowed to abandon this window */
3000 r = can_abandon(wp->w_buffer, forceit);
3001#ifdef FEAT_AUTOCMD
3002 if (!win_valid(wp)) /* autocommands messed wp up */
3003 {
3004 nextwp = firstwin;
3005 continue;
3006 }
3007#endif
3008 if (!r)
3009 {
3010#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3011 if (message && (p_confirm || cmdmod.confirm) && p_write)
3012 {
3013 dialog_changed(wp->w_buffer, FALSE);
3014# ifdef FEAT_AUTOCMD
3015 if (!win_valid(wp)) /* autocommands messed wp up */
3016 {
3017 nextwp = firstwin;
3018 continue;
3019 }
3020# endif
3021 }
3022 if (bufIsChanged(wp->w_buffer))
3023#endif
3024 continue;
3025 }
3026 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3027 }
3028 }
3029
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003030 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 EMSG(_("E445: Other window contains changes"));
3032}
3033
3034#endif /* FEAT_WINDOWS */
3035
3036/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003037 * Init the current window "curwin".
3038 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 */
3040 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003041curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003043 redraw_win_later(curwin, NOT_VALID);
3044 curwin->w_lines_valid = 0;
3045 curwin->w_cursor.lnum = 1;
3046 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003048 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003050 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3051 curwin->w_pcmark.col = 0;
3052 curwin->w_prev_pcmark.lnum = 0;
3053 curwin->w_prev_pcmark.col = 0;
3054 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003056 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003058 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059#ifdef FEAT_FKMAP
3060 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003061 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003063 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif
3065}
3066
3067/*
3068 * Allocate the first window and put an empty buffer in it.
3069 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003070 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003072 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073win_alloc_first()
3074{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003075 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003076 return FAIL;
3077
3078#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003079 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003080 if (first_tabpage == NULL)
3081 return FAIL;
3082 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003083 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084#endif
3085 return OK;
3086}
3087
3088/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003089 * Allocate the first window or the first window in a new tab page.
3090 * When "oldwin" is NULL create an empty buffer for it.
3091 * When "oldwin" is not NULL copy info from it to the new window (only with
3092 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003093 * Return FAIL when something goes wrong (out of memory).
3094 */
3095 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003096win_alloc_firstwin(oldwin)
3097 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003098{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003100 if (oldwin == NULL)
3101 {
3102 /* Very first window, need to create an empty buffer for it and
3103 * initialize from scratch. */
3104 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3105 if (curwin == NULL || curbuf == NULL)
3106 return FAIL;
3107 curwin->w_buffer = curbuf;
3108 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003110 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003112 curwin_init(); /* init current window */
3113 }
3114#ifdef FEAT_WINDOWS
3115 else
3116 {
3117 /* First window in new tab page, initialize it from "oldwin". */
3118 win_init(curwin, oldwin);
3119
3120# ifdef FEAT_SCROLLBIND
3121 /* We don't want scroll-binding in the first window. */
3122 curwin->w_p_scb = FALSE;
3123# endif
3124 }
3125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126
3127 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3128 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003129 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 topframe->fr_layout = FR_LEAF;
3131#ifdef FEAT_VERTSPLIT
3132 topframe->fr_width = Columns;
3133#endif
3134 topframe->fr_height = Rows - p_ch;
3135 topframe->fr_win = curwin;
3136 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003137
3138 return OK;
3139}
3140
3141/*
3142 * Initialize the window and frame size to the maximum.
3143 */
3144 void
3145win_init_size()
3146{
3147 firstwin->w_height = ROWS_AVAIL;
3148 topframe->fr_height = ROWS_AVAIL;
3149#ifdef FEAT_VERTSPLIT
3150 firstwin->w_width = Columns;
3151 topframe->fr_width = Columns;
3152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153}
3154
3155#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003156
3157/*
3158 * Allocate a new tabpage_T and init the values.
3159 * Returns NULL when out of memory.
3160 */
3161 static tabpage_T *
3162alloc_tabpage()
3163{
3164 tabpage_T *tp;
3165
3166 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3167 if (tp != NULL)
3168 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003169# ifdef FEAT_GUI
3170 int i;
3171
3172 for (i = 0; i < 3; i++)
3173 tp->tp_prev_which_scrollbars[i] = -1;
3174# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003175# ifdef FEAT_DIFF
3176 tp->tp_diff_invalid = TRUE;
3177# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003178#ifdef FEAT_EVAL
3179 /* init t: variables */
3180 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3181#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003182 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003183 }
3184 return tp;
3185}
3186
3187 static void
3188free_tabpage(tp)
3189 tabpage_T *tp;
3190{
3191# ifdef FEAT_DIFF
3192 diff_clear(tp);
3193# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003194 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003195#ifdef FEAT_EVAL
3196 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3197#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003198 vim_free(tp);
3199}
3200
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003201/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003202 * Create a new Tab page with one window.
3203 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003204 * When "after" is 0 put it just after the current Tab page.
3205 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003206 * Return FAIL or OK.
3207 */
3208 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003209win_new_tabpage(after)
3210 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003211{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003212 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003213 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003214 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003215
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003216 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003217 if (newtp == NULL)
3218 return FAIL;
3219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003220 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003221 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003222 {
3223 vim_free(newtp);
3224 return FAIL;
3225 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003226 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003227
3228 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003229 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003231 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003232 if (after == 1)
3233 {
3234 /* New tab page becomes the first one. */
3235 newtp->tp_next = first_tabpage;
3236 first_tabpage = newtp;
3237 }
3238 else
3239 {
3240 if (after > 0)
3241 {
3242 /* Put new tab page before tab page "after". */
3243 n = 2;
3244 for (tp = first_tabpage; tp->tp_next != NULL
3245 && n < after; tp = tp->tp_next)
3246 ++n;
3247 }
3248 newtp->tp_next = tp->tp_next;
3249 tp->tp_next = newtp;
3250 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003251 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003253 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003254
3255 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003256 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003257
3258#if defined(FEAT_GUI)
3259 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3260 * scrollbars. Have to update them anyway. */
3261 if (gui.in_use && starting == 0)
3262 {
3263 gui_init_which_components(NULL);
3264 gui_update_scrollbars(TRUE);
3265 }
3266 need_mouse_correct = TRUE;
3267#endif
3268
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003269 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003270#ifdef FEAT_AUTOCMD
3271 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3272 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3273#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274 return OK;
3275 }
3276
3277 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003278 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003279 return FAIL;
3280}
3281
3282/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003283 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3284 * like with ":split".
3285 * Returns OK if a new tab page was created, FAIL otherwise.
3286 */
3287 int
3288may_open_tabpage()
3289{
3290 int n = cmdmod.tab;
3291
3292 if (cmdmod.tab != 0)
3293 {
3294 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3295 return win_new_tabpage(n);
3296 }
3297 return FAIL;
3298}
3299
3300/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003301 * Create up to "maxcount" tabpages with empty windows.
3302 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003303 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003304 int
3305make_tabpages(maxcount)
3306 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003307{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003308 int count = maxcount;
3309 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003310
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003311 /* Limit to 'tabpagemax' tabs. */
3312 if (count > p_tpm)
3313 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003314
3315#ifdef FEAT_AUTOCMD
3316 /*
3317 * Don't execute autocommands while creating the tab pages. Must do that
3318 * when putting the buffers in the windows.
3319 */
3320 ++autocmd_block;
3321#endif
3322
3323 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003324 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003325 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003326
3327#ifdef FEAT_AUTOCMD
3328 --autocmd_block;
3329#endif
3330
3331 /* return actual number of tab pages */
3332 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003333}
3334
3335/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003336 * Return TRUE when "tpc" points to a valid tab page.
3337 */
3338 int
3339valid_tabpage(tpc)
3340 tabpage_T *tpc;
3341{
3342 tabpage_T *tp;
3343
3344 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3345 if (tp == tpc)
3346 return TRUE;
3347 return FALSE;
3348}
3349
3350/*
3351 * Find tab page "n" (first one is 1). Returns NULL when not found.
3352 */
3353 tabpage_T *
3354find_tabpage(n)
3355 int n;
3356{
3357 tabpage_T *tp;
3358 int i = 1;
3359
3360 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3361 ++i;
3362 return tp;
3363}
3364
3365/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003366 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003367 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368 */
3369 int
3370tabpage_index(ftp)
3371 tabpage_T *ftp;
3372{
3373 int i = 1;
3374 tabpage_T *tp;
3375
3376 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3377 ++i;
3378 return i;
3379}
3380
3381/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003382 * Prepare for leaving the current tab page.
3383 * When autocomands change "curtab" we don't leave the tab page and return
3384 * FAIL.
3385 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003387/*ARGSUSED*/
3388 static int
3389leave_tabpage(new_curbuf)
3390 buf_T *new_curbuf; /* what is going to be the new curbuf,
3391 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003393 tabpage_T *tp = curtab;
3394
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003395#ifdef FEAT_VISUAL
3396 reset_VIsual_and_resel(); /* stop Visual mode */
3397#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003398#ifdef FEAT_AUTOCMD
3399 if (new_curbuf != curbuf)
3400 {
3401 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3402 if (curtab != tp)
3403 return FAIL;
3404 }
3405 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3406 if (curtab != tp)
3407 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003408 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003409 if (curtab != tp)
3410 return FAIL;
3411#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003412#if defined(FEAT_GUI)
3413 /* Remove the scrollbars. They may be added back later. */
3414 if (gui.in_use)
3415 gui_remove_scrollbars();
3416#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003417 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003418 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419 tp->tp_firstwin = firstwin;
3420 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003421 tp->tp_old_Rows = Rows;
3422 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423 firstwin = NULL;
3424 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003425 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426}
3427
3428/*
3429 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003430 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431 */
3432/*ARGSUSED*/
3433 static void
3434enter_tabpage(tp, old_curbuf)
3435 tabpage_T *tp;
3436 buf_T *old_curbuf;
3437{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003438 int old_off = tp->tp_firstwin->w_winrow;
3439
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003440 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 firstwin = tp->tp_firstwin;
3442 lastwin = tp->tp_lastwin;
3443 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003444#ifdef FEAT_AUTOCMD
3445 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3446#endif
3447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003449 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003450
3451#ifdef FEAT_AUTOCMD
3452 if (old_curbuf != curbuf)
3453 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3454#endif
3455
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003456 last_status(FALSE); /* status line may appear or disappear */
3457 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003458 must_redraw = CLEAR; /* need to redraw everything */
3459#ifdef FEAT_DIFF
3460 diff_need_scrollbind = TRUE;
3461#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003463 /* The tabpage line may have appeared or disappeared, may need to resize
3464 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003465 * frame sizes too. Use the stored value of p_ch, so that it can be
3466 * different for each tab page. */
3467 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3469#ifdef FEAT_GUI_TABLINE
3470 && !gui_use_tabline()
3471#endif
3472 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003473 shell_new_rows();
3474#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003475 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003476 shell_new_columns(); /* update window widths */
3477#endif
3478
3479#if defined(FEAT_GUI)
3480 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3481 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003482 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003483 {
3484 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003485 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003486 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003487 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003488#endif
3489
3490 redraw_all_later(CLEAR);
3491}
3492
3493/*
3494 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003495 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003496 */
3497 void
3498goto_tabpage(n)
3499 int n;
3500{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003501 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003502 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 int i;
3504
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003505 /* If there is only one it can't work. */
3506 if (first_tabpage->tp_next == NULL)
3507 {
3508 if (n > 1)
3509 beep_flush();
3510 return;
3511 }
3512
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003513 if (n == 0)
3514 {
3515 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003516 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003517 tp = first_tabpage;
3518 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003519 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003521 else if (n < 0)
3522 {
3523 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3524 * this N times. */
3525 ttp = curtab;
3526 for (i = n; i < 0; ++i)
3527 {
3528 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3529 tp = tp->tp_next)
3530 ;
3531 ttp = tp;
3532 }
3533 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003534 else if (n == 9999)
3535 {
3536 /* Go to last tab page. */
3537 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3538 ;
3539 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003540 else
3541 {
3542 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003543 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003544 if (tp == NULL)
3545 {
3546 beep_flush();
3547 return;
3548 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549 }
3550
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003551 goto_tabpage_tp(tp);
3552
3553#ifdef FEAT_GUI_TABLINE
3554 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003555 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003556#endif
3557}
3558
3559/*
3560 * Go to tabpage "tp".
3561 * Note: doesn't update the GUI tab.
3562 */
3563 void
3564goto_tabpage_tp(tp)
3565 tabpage_T *tp;
3566{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003567 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003568 {
3569 if (valid_tabpage(tp))
3570 enter_tabpage(tp, curbuf);
3571 else
3572 enter_tabpage(curtab, curbuf);
3573 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003577 * Enter window "wp" in tab page "tp".
3578 * Also updates the GUI tab.
3579 */
3580 void
3581goto_tabpage_win(tp, wp)
3582 tabpage_T *tp;
3583 win_T *wp;
3584{
3585 goto_tabpage_tp(tp);
3586 if (curtab == tp && win_valid(wp))
3587 {
3588 win_enter(wp, TRUE);
3589# ifdef FEAT_GUI_TABLINE
3590 if (gui_use_tabline())
3591 gui_mch_set_curtab(tabpage_index(curtab));
3592# endif
3593 }
3594}
3595
3596/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003597 * Move the current tab page to before tab page "nr".
3598 */
3599 void
3600tabpage_move(nr)
3601 int nr;
3602{
3603 int n = nr;
3604 tabpage_T *tp;
3605
3606 if (first_tabpage->tp_next == NULL)
3607 return;
3608
3609 /* Remove the current tab page from the list of tab pages. */
3610 if (curtab == first_tabpage)
3611 first_tabpage = curtab->tp_next;
3612 else
3613 {
3614 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3615 if (tp->tp_next == curtab)
3616 break;
3617 if (tp == NULL) /* "cannot happen" */
3618 return;
3619 tp->tp_next = curtab->tp_next;
3620 }
3621
3622 /* Re-insert it at the specified position. */
3623 if (n == 0)
3624 {
3625 curtab->tp_next = first_tabpage;
3626 first_tabpage = curtab;
3627 }
3628 else
3629 {
3630 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3631 --n;
3632 curtab->tp_next = tp->tp_next;
3633 tp->tp_next = curtab;
3634 }
3635
3636 /* Need to redraw the tabline. Tab page contents doesn't change. */
3637 redraw_tabline = TRUE;
3638}
3639
3640
3641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 * Go to another window.
3643 * When jumping to another buffer, stop Visual mode. Do this before
3644 * changing windows so we can yank the selection into the '*' register.
3645 * When jumping to another window on the same buffer, adjust its cursor
3646 * position to keep the same Visual area.
3647 */
3648 void
3649win_goto(wp)
3650 win_T *wp;
3651{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003652 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003655 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 return;
3657 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003658#ifdef FEAT_AUTOCMD
3659 if (curbuf_locked())
3660 return;
3661#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663#ifdef FEAT_VISUAL
3664 if (wp->w_buffer != curbuf)
3665 reset_VIsual_and_resel();
3666 else if (VIsual_active)
3667 wp->w_cursor = curwin->w_cursor;
3668#endif
3669
3670#ifdef FEAT_GUI
3671 need_mouse_correct = TRUE;
3672#endif
3673 win_enter(wp, TRUE);
3674}
3675
3676#if defined(FEAT_PERL) || defined(PROTO)
3677/*
3678 * Find window number "winnr" (counting top to bottom).
3679 */
3680 win_T *
3681win_find_nr(winnr)
3682 int winnr;
3683{
3684 win_T *wp;
3685
3686# ifdef FEAT_WINDOWS
3687 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3688 if (--winnr == 0)
3689 break;
3690 return wp;
3691# else
3692 return curwin;
3693# endif
3694}
3695#endif
3696
3697#ifdef FEAT_VERTSPLIT
3698/*
3699 * Move to window above or below "count" times.
3700 */
3701 static void
3702win_goto_ver(up, count)
3703 int up; /* TRUE to go to win above */
3704 long count;
3705{
3706 frame_T *fr;
3707 frame_T *nfr;
3708 frame_T *foundfr;
3709
3710 foundfr = curwin->w_frame;
3711 while (count--)
3712 {
3713 /*
3714 * First go upwards in the tree of frames until we find a upwards or
3715 * downwards neighbor.
3716 */
3717 fr = foundfr;
3718 for (;;)
3719 {
3720 if (fr == topframe)
3721 goto end;
3722 if (up)
3723 nfr = fr->fr_prev;
3724 else
3725 nfr = fr->fr_next;
3726 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3727 break;
3728 fr = fr->fr_parent;
3729 }
3730
3731 /*
3732 * Now go downwards to find the bottom or top frame in it.
3733 */
3734 for (;;)
3735 {
3736 if (nfr->fr_layout == FR_LEAF)
3737 {
3738 foundfr = nfr;
3739 break;
3740 }
3741 fr = nfr->fr_child;
3742 if (nfr->fr_layout == FR_ROW)
3743 {
3744 /* Find the frame at the cursor row. */
3745 while (fr->fr_next != NULL
3746 && frame2win(fr)->w_wincol + fr->fr_width
3747 <= curwin->w_wincol + curwin->w_wcol)
3748 fr = fr->fr_next;
3749 }
3750 if (nfr->fr_layout == FR_COL && up)
3751 while (fr->fr_next != NULL)
3752 fr = fr->fr_next;
3753 nfr = fr;
3754 }
3755 }
3756end:
3757 if (foundfr != NULL)
3758 win_goto(foundfr->fr_win);
3759}
3760
3761/*
3762 * Move to left or right window.
3763 */
3764 static void
3765win_goto_hor(left, count)
3766 int left; /* TRUE to go to left win */
3767 long count;
3768{
3769 frame_T *fr;
3770 frame_T *nfr;
3771 frame_T *foundfr;
3772
3773 foundfr = curwin->w_frame;
3774 while (count--)
3775 {
3776 /*
3777 * First go upwards in the tree of frames until we find a left or
3778 * right neighbor.
3779 */
3780 fr = foundfr;
3781 for (;;)
3782 {
3783 if (fr == topframe)
3784 goto end;
3785 if (left)
3786 nfr = fr->fr_prev;
3787 else
3788 nfr = fr->fr_next;
3789 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3790 break;
3791 fr = fr->fr_parent;
3792 }
3793
3794 /*
3795 * Now go downwards to find the leftmost or rightmost frame in it.
3796 */
3797 for (;;)
3798 {
3799 if (nfr->fr_layout == FR_LEAF)
3800 {
3801 foundfr = nfr;
3802 break;
3803 }
3804 fr = nfr->fr_child;
3805 if (nfr->fr_layout == FR_COL)
3806 {
3807 /* Find the frame at the cursor row. */
3808 while (fr->fr_next != NULL
3809 && frame2win(fr)->w_winrow + fr->fr_height
3810 <= curwin->w_winrow + curwin->w_wrow)
3811 fr = fr->fr_next;
3812 }
3813 if (nfr->fr_layout == FR_ROW && left)
3814 while (fr->fr_next != NULL)
3815 fr = fr->fr_next;
3816 nfr = fr;
3817 }
3818 }
3819end:
3820 if (foundfr != NULL)
3821 win_goto(foundfr->fr_win);
3822}
3823#endif
3824
3825/*
3826 * Make window "wp" the current window.
3827 */
3828 void
3829win_enter(wp, undo_sync)
3830 win_T *wp;
3831 int undo_sync;
3832{
3833 win_enter_ext(wp, undo_sync, FALSE);
3834}
3835
3836/*
3837 * Make window wp the current window.
3838 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3839 * been closed and isn't valid.
3840 */
3841 static void
3842win_enter_ext(wp, undo_sync, curwin_invalid)
3843 win_T *wp;
3844 int undo_sync;
3845 int curwin_invalid;
3846{
3847#ifdef FEAT_AUTOCMD
3848 int other_buffer = FALSE;
3849#endif
3850
3851 if (wp == curwin && !curwin_invalid) /* nothing to do */
3852 return;
3853
3854#ifdef FEAT_AUTOCMD
3855 if (!curwin_invalid)
3856 {
3857 /*
3858 * Be careful: If autocommands delete the window, return now.
3859 */
3860 if (wp->w_buffer != curbuf)
3861 {
3862 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3863 other_buffer = TRUE;
3864 if (!win_valid(wp))
3865 return;
3866 }
3867 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3868 if (!win_valid(wp))
3869 return;
3870# ifdef FEAT_EVAL
3871 /* autocmds may abort script processing */
3872 if (aborting())
3873 return;
3874# endif
3875 }
3876#endif
3877
3878 /* sync undo before leaving the current buffer */
3879 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003880 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 /* may have to copy the buffer options when 'cpo' contains 'S' */
3882 if (wp->w_buffer != curbuf)
3883 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3884 if (!curwin_invalid)
3885 {
3886 prevwin = curwin; /* remember for CTRL-W p */
3887 curwin->w_redr_status = TRUE;
3888 }
3889 curwin = wp;
3890 curbuf = wp->w_buffer;
3891 check_cursor();
3892#ifdef FEAT_VIRTUALEDIT
3893 if (!virtual_active())
3894 curwin->w_cursor.coladd = 0;
3895#endif
3896 changed_line_abv_curs(); /* assume cursor position needs updating */
3897
3898 if (curwin->w_localdir != NULL)
3899 {
3900 /* Window has a local directory: Save current directory as global
3901 * directory (unless that was done already) and change to the local
3902 * directory. */
3903 if (globaldir == NULL)
3904 {
3905 char_u cwd[MAXPATHL];
3906
3907 if (mch_dirname(cwd, MAXPATHL) == OK)
3908 globaldir = vim_strsave(cwd);
3909 }
3910 mch_chdir((char *)curwin->w_localdir);
3911 shorten_fnames(TRUE);
3912 }
3913 else if (globaldir != NULL)
3914 {
3915 /* Window doesn't have a local directory and we are not in the global
3916 * directory: Change to the global directory. */
3917 mch_chdir((char *)globaldir);
3918 vim_free(globaldir);
3919 globaldir = NULL;
3920 shorten_fnames(TRUE);
3921 }
3922
3923#ifdef FEAT_AUTOCMD
3924 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3925 if (other_buffer)
3926 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3927#endif
3928
3929#ifdef FEAT_TITLE
3930 maketitle();
3931#endif
3932 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003933 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (restart_edit)
3935 redraw_later(VALID); /* causes status line redraw */
3936
3937 /* set window height to desired minimal value */
3938 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3939 win_setheight((int)p_wh);
3940 else if (curwin->w_height == 0)
3941 win_setheight(1);
3942
3943#ifdef FEAT_VERTSPLIT
3944 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003945 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 win_setwidth((int)p_wiw);
3947#endif
3948
3949#ifdef FEAT_MOUSE
3950 setmouse(); /* in case jumped to/from help buffer */
3951#endif
3952
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003953#ifdef FEAT_AUTOCHDIR
3954 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * switching windows. */
3956 if (p_acd && curbuf->b_ffname != NULL
3957 && vim_chdirfile(curbuf->b_ffname) == OK)
3958 shorten_fnames(TRUE);
3959#endif
3960}
3961
3962#endif /* FEAT_WINDOWS */
3963
3964#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3965/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003966 * Jump to the first open window that contains buffer "buf", if one exists.
3967 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
3969 win_T *
3970buf_jump_open_win(buf)
3971 buf_T *buf;
3972{
3973# ifdef FEAT_WINDOWS
3974 win_T *wp;
3975
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003976 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (wp->w_buffer == buf)
3978 break;
3979 if (wp != NULL)
3980 win_enter(wp, FALSE);
3981 return wp;
3982# else
3983 if (curwin->w_buffer == buf)
3984 return curwin;
3985 return NULL;
3986# endif
3987}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003988
3989/*
3990 * Jump to the first open window in any tab page that contains buffer "buf",
3991 * if one exists.
3992 * Returns a pointer to the window found, otherwise NULL.
3993 */
3994 win_T *
3995buf_jump_open_tab(buf)
3996 buf_T *buf;
3997{
3998# ifdef FEAT_WINDOWS
3999 win_T *wp;
4000 tabpage_T *tp;
4001
4002 /* First try the current tab page. */
4003 wp = buf_jump_open_win(buf);
4004 if (wp != NULL)
4005 return wp;
4006
4007 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4008 if (tp != curtab)
4009 {
4010 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4011 if (wp->w_buffer == buf)
4012 break;
4013 if (wp != NULL)
4014 {
4015 goto_tabpage_win(tp, wp);
4016 if (curwin != wp)
4017 wp = NULL; /* something went wrong */
4018 break;
4019 }
4020 }
4021
4022 return wp;
4023# else
4024 if (curwin->w_buffer == buf)
4025 return curwin;
4026 return NULL;
4027# endif
4028}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#endif
4030
4031/*
4032 * allocate a window structure and link it in the window list
4033 */
4034/*ARGSUSED*/
4035 static win_T *
4036win_alloc(after)
4037 win_T *after;
4038{
4039 win_T *newwin;
4040
4041 /*
4042 * allocate window structure and linesizes arrays
4043 */
4044 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4045 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
4046 {
4047 vim_free(newwin);
4048 newwin = NULL;
4049 }
4050
4051 if (newwin != NULL)
4052 {
4053 /*
4054 * link the window in the window list
4055 */
4056#ifdef FEAT_WINDOWS
4057 win_append(after, newwin);
4058#endif
4059#ifdef FEAT_VERTSPLIT
4060 newwin->w_wincol = 0;
4061 newwin->w_width = Columns;
4062#endif
4063
4064 /* position the display and the cursor at the top of the file. */
4065 newwin->w_topline = 1;
4066#ifdef FEAT_DIFF
4067 newwin->w_topfill = 0;
4068#endif
4069 newwin->w_botline = 2;
4070 newwin->w_cursor.lnum = 1;
4071#ifdef FEAT_SCROLLBIND
4072 newwin->w_scbind_pos = 1;
4073#endif
4074
4075 /* We won't calculate w_fraction until resizing the window */
4076 newwin->w_fraction = 0;
4077 newwin->w_prev_fraction_row = -1;
4078
4079#ifdef FEAT_GUI
4080 if (gui.in_use)
4081 {
4082 out_flush();
4083 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4084 SBAR_LEFT, newwin);
4085 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4086 SBAR_RIGHT, newwin);
4087 }
4088#endif
4089#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004090 /* init w: variables */
4091 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#endif
4093#ifdef FEAT_FOLDING
4094 foldInitWin(newwin);
4095#endif
4096 }
4097 return newwin;
4098}
4099
4100#if defined(FEAT_WINDOWS) || defined(PROTO)
4101
4102/*
4103 * remove window 'wp' from the window list and free the structure
4104 */
4105 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004106win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004108 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 int i;
4111
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004112#ifdef FEAT_MZSCHEME
4113 mzscheme_window_free(wp);
4114#endif
4115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#ifdef FEAT_PERL
4117 perl_win_free(wp);
4118#endif
4119
4120#ifdef FEAT_PYTHON
4121 python_window_free(wp);
4122#endif
4123
4124#ifdef FEAT_TCL
4125 tcl_window_free(wp);
4126#endif
4127
4128#ifdef FEAT_RUBY
4129 ruby_window_free(wp);
4130#endif
4131
4132 clear_winopt(&wp->w_onebuf_opt);
4133 clear_winopt(&wp->w_allbuf_opt);
4134
4135#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004136 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137#endif
4138
4139 if (prevwin == wp)
4140 prevwin = NULL;
4141 win_free_lsize(wp);
4142
4143 for (i = 0; i < wp->w_tagstacklen; ++i)
4144 vim_free(wp->w_tagstack[i].tagname);
4145
4146 vim_free(wp->w_localdir);
4147#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004148 vim_free(wp->w_match[0].regprog);
4149 vim_free(wp->w_match[1].regprog);
4150 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152#ifdef FEAT_JUMPLIST
4153 free_jumplist(wp);
4154#endif
4155
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004156#ifdef FEAT_QUICKFIX
4157 qf_free_all(wp);
4158#endif
4159
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160#ifdef FEAT_GUI
4161 if (gui.in_use)
4162 {
4163 out_flush();
4164 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4165 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4166 }
4167#endif /* FEAT_GUI */
4168
Bram Moolenaarf740b292006-02-16 22:11:02 +00004169 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 vim_free(wp);
4171}
4172
4173/*
4174 * Append window "wp" in the window list after window "after".
4175 */
4176 static void
4177win_append(after, wp)
4178 win_T *after, *wp;
4179{
4180 win_T *before;
4181
4182 if (after == NULL) /* after NULL is in front of the first */
4183 before = firstwin;
4184 else
4185 before = after->w_next;
4186
4187 wp->w_next = before;
4188 wp->w_prev = after;
4189 if (after == NULL)
4190 firstwin = wp;
4191 else
4192 after->w_next = wp;
4193 if (before == NULL)
4194 lastwin = wp;
4195 else
4196 before->w_prev = wp;
4197}
4198
4199/*
4200 * Remove a window from the window list.
4201 */
4202 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004203win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004205 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 if (wp->w_prev != NULL)
4208 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004209 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004211 else
4212 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (wp->w_next != NULL)
4214 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004215 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004217 else
4218 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219}
4220
4221/*
4222 * Append frame "frp" in a frame list after frame "after".
4223 */
4224 static void
4225frame_append(after, frp)
4226 frame_T *after, *frp;
4227{
4228 frp->fr_next = after->fr_next;
4229 after->fr_next = frp;
4230 if (frp->fr_next != NULL)
4231 frp->fr_next->fr_prev = frp;
4232 frp->fr_prev = after;
4233}
4234
4235/*
4236 * Insert frame "frp" in a frame list before frame "before".
4237 */
4238 static void
4239frame_insert(before, frp)
4240 frame_T *before, *frp;
4241{
4242 frp->fr_next = before;
4243 frp->fr_prev = before->fr_prev;
4244 before->fr_prev = frp;
4245 if (frp->fr_prev != NULL)
4246 frp->fr_prev->fr_next = frp;
4247 else
4248 frp->fr_parent->fr_child = frp;
4249}
4250
4251/*
4252 * Remove a frame from a frame list.
4253 */
4254 static void
4255frame_remove(frp)
4256 frame_T *frp;
4257{
4258 if (frp->fr_prev != NULL)
4259 frp->fr_prev->fr_next = frp->fr_next;
4260 else
4261 frp->fr_parent->fr_child = frp->fr_next;
4262 if (frp->fr_next != NULL)
4263 frp->fr_next->fr_prev = frp->fr_prev;
4264}
4265
4266#endif /* FEAT_WINDOWS */
4267
4268/*
4269 * Allocate w_lines[] for window "wp".
4270 * Return FAIL for failure, OK for success.
4271 */
4272 int
4273win_alloc_lines(wp)
4274 win_T *wp;
4275{
4276 wp->w_lines_valid = 0;
4277 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4278 if (wp->w_lines == NULL)
4279 return FAIL;
4280 return OK;
4281}
4282
4283/*
4284 * free lsize arrays for a window
4285 */
4286 void
4287win_free_lsize(wp)
4288 win_T *wp;
4289{
4290 vim_free(wp->w_lines);
4291 wp->w_lines = NULL;
4292}
4293
4294/*
4295 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004296 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 */
4298 void
4299shell_new_rows()
4300{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004301 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 if (firstwin == NULL) /* not initialized yet */
4304 return;
4305#ifdef FEAT_WINDOWS
4306 if (h < frame_minheight(topframe, NULL))
4307 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004308
4309 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 * that doesn't result in the right height, forget about that option. */
4311 frame_new_height(topframe, h, FALSE, TRUE);
4312 if (topframe->fr_height != h)
4313 frame_new_height(topframe, h, FALSE, FALSE);
4314
4315 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4316#else
4317 if (h < 1)
4318 h = 1;
4319 win_new_height(firstwin, h);
4320#endif
4321 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004322#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004323 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004324#endif
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#if 0
4327 /* Disabled: don't want making the screen smaller make a window larger. */
4328 if (p_ea)
4329 win_equal(curwin, FALSE, 'v');
4330#endif
4331}
4332
4333#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4334/*
4335 * Called from win_new_shellsize() after Columns changed.
4336 */
4337 void
4338shell_new_columns()
4339{
4340 if (firstwin == NULL) /* not initialized yet */
4341 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004342
4343 /* First try setting the widths of windows with 'winfixwidth'. If that
4344 * doesn't result in the right width, forget about that option. */
4345 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4346 if (topframe->fr_width != Columns)
4347 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4348
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4350#if 0
4351 /* Disabled: don't want making the screen smaller make a window larger. */
4352 if (p_ea)
4353 win_equal(curwin, FALSE, 'h');
4354#endif
4355}
4356#endif
4357
4358#if defined(FEAT_CMDWIN) || defined(PROTO)
4359/*
4360 * Save the size of all windows in "gap".
4361 */
4362 void
4363win_size_save(gap)
4364 garray_T *gap;
4365
4366{
4367 win_T *wp;
4368
4369 ga_init2(gap, (int)sizeof(int), 1);
4370 if (ga_grow(gap, win_count() * 2) == OK)
4371 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4372 {
4373 ((int *)gap->ga_data)[gap->ga_len++] =
4374 wp->w_width + wp->w_vsep_width;
4375 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4376 }
4377}
4378
4379/*
4380 * Restore window sizes, but only if the number of windows is still the same.
4381 * Does not free the growarray.
4382 */
4383 void
4384win_size_restore(gap)
4385 garray_T *gap;
4386{
4387 win_T *wp;
4388 int i;
4389
4390 if (win_count() * 2 == gap->ga_len)
4391 {
4392 i = 0;
4393 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4394 {
4395 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4396 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4397 }
4398 /* recompute the window positions */
4399 (void)win_comp_pos();
4400 }
4401}
4402#endif /* FEAT_CMDWIN */
4403
4404#if defined(FEAT_WINDOWS) || defined(PROTO)
4405/*
4406 * Update the position for all windows, using the width and height of the
4407 * frames.
4408 * Returns the row just after the last window.
4409 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004410 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411win_comp_pos()
4412{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004413 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 int col = 0;
4415
4416 frame_comp_pos(topframe, &row, &col);
4417 return row;
4418}
4419
4420/*
4421 * Update the position of the windows in frame "topfrp", using the width and
4422 * height of the frames.
4423 * "*row" and "*col" are the top-left position of the frame. They are updated
4424 * to the bottom-right position plus one.
4425 */
4426 static void
4427frame_comp_pos(topfrp, row, col)
4428 frame_T *topfrp;
4429 int *row;
4430 int *col;
4431{
4432 win_T *wp;
4433 frame_T *frp;
4434#ifdef FEAT_VERTSPLIT
4435 int startcol;
4436 int startrow;
4437#endif
4438
4439 wp = topfrp->fr_win;
4440 if (wp != NULL)
4441 {
4442 if (wp->w_winrow != *row
4443#ifdef FEAT_VERTSPLIT
4444 || wp->w_wincol != *col
4445#endif
4446 )
4447 {
4448 /* position changed, redraw */
4449 wp->w_winrow = *row;
4450#ifdef FEAT_VERTSPLIT
4451 wp->w_wincol = *col;
4452#endif
4453 redraw_win_later(wp, NOT_VALID);
4454 wp->w_redr_status = TRUE;
4455 }
4456 *row += wp->w_height + wp->w_status_height;
4457#ifdef FEAT_VERTSPLIT
4458 *col += wp->w_width + wp->w_vsep_width;
4459#endif
4460 }
4461 else
4462 {
4463#ifdef FEAT_VERTSPLIT
4464 startrow = *row;
4465 startcol = *col;
4466#endif
4467 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4468 {
4469#ifdef FEAT_VERTSPLIT
4470 if (topfrp->fr_layout == FR_ROW)
4471 *row = startrow; /* all frames are at the same row */
4472 else
4473 *col = startcol; /* all frames are at the same col */
4474#endif
4475 frame_comp_pos(frp, row, col);
4476 }
4477 }
4478}
4479
4480#endif /* FEAT_WINDOWS */
4481
4482/*
4483 * Set current window height and take care of repositioning other windows to
4484 * fit around it.
4485 */
4486 void
4487win_setheight(height)
4488 int height;
4489{
4490 win_setheight_win(height, curwin);
4491}
4492
4493/*
4494 * Set the window height of window "win" and take care of repositioning other
4495 * windows to fit around it.
4496 */
4497 void
4498win_setheight_win(height, win)
4499 int height;
4500 win_T *win;
4501{
4502 int row;
4503
4504 if (win == curwin)
4505 {
4506 /* Always keep current window at least one line high, even when
4507 * 'winminheight' is zero. */
4508#ifdef FEAT_WINDOWS
4509 if (height < p_wmh)
4510 height = p_wmh;
4511#endif
4512 if (height == 0)
4513 height = 1;
4514 }
4515
4516#ifdef FEAT_WINDOWS
4517 frame_setheight(win->w_frame, height + win->w_status_height);
4518
4519 /* recompute the window positions */
4520 row = win_comp_pos();
4521#else
4522 if (height > topframe->fr_height)
4523 height = topframe->fr_height;
4524 win->w_height = height;
4525 row = height;
4526#endif
4527
4528 /*
4529 * If there is extra space created between the last window and the command
4530 * line, clear it.
4531 */
4532 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4533 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4534 cmdline_row = row;
4535 msg_row = row;
4536 msg_col = 0;
4537
4538 redraw_all_later(NOT_VALID);
4539}
4540
4541#if defined(FEAT_WINDOWS) || defined(PROTO)
4542
4543/*
4544 * Set the height of a frame to "height" and take care that all frames and
4545 * windows inside it are resized. Also resize frames on the left and right if
4546 * the are in the same FR_ROW frame.
4547 *
4548 * Strategy:
4549 * If the frame is part of a FR_COL frame, try fitting the frame in that
4550 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4551 * go to containing frames to resize them and make room.
4552 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4553 * Check for the minimal height of the FR_ROW frame.
4554 * At the top level we can also use change the command line height.
4555 */
4556 static void
4557frame_setheight(curfrp, height)
4558 frame_T *curfrp;
4559 int height;
4560{
4561 int room; /* total number of lines available */
4562 int take; /* number of lines taken from other windows */
4563 int room_cmdline; /* lines available from cmdline */
4564 int run;
4565 frame_T *frp;
4566 int h;
4567 int room_reserved;
4568
4569 /* If the height already is the desired value, nothing to do. */
4570 if (curfrp->fr_height == height)
4571 return;
4572
4573 if (curfrp->fr_parent == NULL)
4574 {
4575 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004576 if (height > ROWS_AVAIL)
4577 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (height > 0)
4579 frame_new_height(curfrp, height, FALSE, FALSE);
4580 }
4581 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4582 {
4583 /* Row of frames: Also need to resize frames left and right of this
4584 * one. First check for the minimal height of these. */
4585 h = frame_minheight(curfrp->fr_parent, NULL);
4586 if (height < h)
4587 height = h;
4588 frame_setheight(curfrp->fr_parent, height);
4589 }
4590 else
4591 {
4592 /*
4593 * Column of frames: try to change only frames in this column.
4594 */
4595#ifdef FEAT_VERTSPLIT
4596 /*
4597 * Do this twice:
4598 * 1: compute room available, if it's not enough try resizing the
4599 * containing frame.
4600 * 2: compute the room available and adjust the height to it.
4601 * Try not to reduce the height of a window with 'winfixheight' set.
4602 */
4603 for (run = 1; run <= 2; ++run)
4604#else
4605 for (;;)
4606#endif
4607 {
4608 room = 0;
4609 room_reserved = 0;
4610 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4611 frp = frp->fr_next)
4612 {
4613 if (frp != curfrp
4614 && frp->fr_win != NULL
4615 && frp->fr_win->w_p_wfh)
4616 room_reserved += frp->fr_height;
4617 room += frp->fr_height;
4618 if (frp != curfrp)
4619 room -= frame_minheight(frp, NULL);
4620 }
4621#ifdef FEAT_VERTSPLIT
4622 if (curfrp->fr_width != Columns)
4623 room_cmdline = 0;
4624 else
4625#endif
4626 {
4627 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4628 + lastwin->w_height + lastwin->w_status_height);
4629 if (room_cmdline < 0)
4630 room_cmdline = 0;
4631 }
4632
4633 if (height <= room + room_cmdline)
4634 break;
4635#ifdef FEAT_VERTSPLIT
4636 if (run == 2 || curfrp->fr_width == Columns)
4637#endif
4638 {
4639 if (height > room + room_cmdline)
4640 height = room + room_cmdline;
4641 break;
4642 }
4643#ifdef FEAT_VERTSPLIT
4644 frame_setheight(curfrp->fr_parent, height
4645 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4646#endif
4647 /*NOTREACHED*/
4648 }
4649
4650 /*
4651 * Compute the number of lines we will take from others frames (can be
4652 * negative!).
4653 */
4654 take = height - curfrp->fr_height;
4655
4656 /* If there is not enough room, also reduce the height of a window
4657 * with 'winfixheight' set. */
4658 if (height > room + room_cmdline - room_reserved)
4659 room_reserved = room + room_cmdline - height;
4660 /* If there is only a 'winfixheight' window and making the
4661 * window smaller, need to make the other window taller. */
4662 if (take < 0 && room - curfrp->fr_height < room_reserved)
4663 room_reserved = 0;
4664
4665 if (take > 0 && room_cmdline > 0)
4666 {
4667 /* use lines from cmdline first */
4668 if (take < room_cmdline)
4669 room_cmdline = take;
4670 take -= room_cmdline;
4671 topframe->fr_height += room_cmdline;
4672 }
4673
4674 /*
4675 * set the current frame to the new height
4676 */
4677 frame_new_height(curfrp, height, FALSE, FALSE);
4678
4679 /*
4680 * First take lines from the frames after the current frame. If
4681 * that is not enough, takes lines from frames above the current
4682 * frame.
4683 */
4684 for (run = 0; run < 2; ++run)
4685 {
4686 if (run == 0)
4687 frp = curfrp->fr_next; /* 1st run: start with next window */
4688 else
4689 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4690 while (frp != NULL && take != 0)
4691 {
4692 h = frame_minheight(frp, NULL);
4693 if (room_reserved > 0
4694 && frp->fr_win != NULL
4695 && frp->fr_win->w_p_wfh)
4696 {
4697 if (room_reserved >= frp->fr_height)
4698 room_reserved -= frp->fr_height;
4699 else
4700 {
4701 if (frp->fr_height - room_reserved > take)
4702 room_reserved = frp->fr_height - take;
4703 take -= frp->fr_height - room_reserved;
4704 frame_new_height(frp, room_reserved, FALSE, FALSE);
4705 room_reserved = 0;
4706 }
4707 }
4708 else
4709 {
4710 if (frp->fr_height - take < h)
4711 {
4712 take -= frp->fr_height - h;
4713 frame_new_height(frp, h, FALSE, FALSE);
4714 }
4715 else
4716 {
4717 frame_new_height(frp, frp->fr_height - take,
4718 FALSE, FALSE);
4719 take = 0;
4720 }
4721 }
4722 if (run == 0)
4723 frp = frp->fr_next;
4724 else
4725 frp = frp->fr_prev;
4726 }
4727 }
4728 }
4729}
4730
4731#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4732/*
4733 * Set current window width and take care of repositioning other windows to
4734 * fit around it.
4735 */
4736 void
4737win_setwidth(width)
4738 int width;
4739{
4740 win_setwidth_win(width, curwin);
4741}
4742
4743 void
4744win_setwidth_win(width, wp)
4745 int width;
4746 win_T *wp;
4747{
4748 /* Always keep current window at least one column wide, even when
4749 * 'winminwidth' is zero. */
4750 if (wp == curwin)
4751 {
4752 if (width < p_wmw)
4753 width = p_wmw;
4754 if (width == 0)
4755 width = 1;
4756 }
4757
4758 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4759
4760 /* recompute the window positions */
4761 (void)win_comp_pos();
4762
4763 redraw_all_later(NOT_VALID);
4764}
4765
4766/*
4767 * Set the width of a frame to "width" and take care that all frames and
4768 * windows inside it are resized. Also resize frames above and below if the
4769 * are in the same FR_ROW frame.
4770 *
4771 * Strategy is similar to frame_setheight().
4772 */
4773 static void
4774frame_setwidth(curfrp, width)
4775 frame_T *curfrp;
4776 int width;
4777{
4778 int room; /* total number of lines available */
4779 int take; /* number of lines taken from other windows */
4780 int run;
4781 frame_T *frp;
4782 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004783 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 /* If the width already is the desired value, nothing to do. */
4786 if (curfrp->fr_width == width)
4787 return;
4788
4789 if (curfrp->fr_parent == NULL)
4790 /* topframe: can't change width */
4791 return;
4792
4793 if (curfrp->fr_parent->fr_layout == FR_COL)
4794 {
4795 /* Column of frames: Also need to resize frames above and below of
4796 * this one. First check for the minimal width of these. */
4797 w = frame_minwidth(curfrp->fr_parent, NULL);
4798 if (width < w)
4799 width = w;
4800 frame_setwidth(curfrp->fr_parent, width);
4801 }
4802 else
4803 {
4804 /*
4805 * Row of frames: try to change only frames in this row.
4806 *
4807 * Do this twice:
4808 * 1: compute room available, if it's not enough try resizing the
4809 * containing frame.
4810 * 2: compute the room available and adjust the width to it.
4811 */
4812 for (run = 1; run <= 2; ++run)
4813 {
4814 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004815 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4817 frp = frp->fr_next)
4818 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004819 if (frp != curfrp
4820 && frp->fr_win != NULL
4821 && frp->fr_win->w_p_wfw)
4822 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 room += frp->fr_width;
4824 if (frp != curfrp)
4825 room -= frame_minwidth(frp, NULL);
4826 }
4827
4828 if (width <= room)
4829 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004830 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 if (width > room)
4833 width = room;
4834 break;
4835 }
4836 frame_setwidth(curfrp->fr_parent, width
4837 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4838 }
4839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 /*
4841 * Compute the number of lines we will take from others frames (can be
4842 * negative!).
4843 */
4844 take = width - curfrp->fr_width;
4845
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004846 /* If there is not enough room, also reduce the width of a window
4847 * with 'winfixwidth' set. */
4848 if (width > room - room_reserved)
4849 room_reserved = room - width;
4850 /* If there is only a 'winfixwidth' window and making the
4851 * window smaller, need to make the other window narrower. */
4852 if (take < 0 && room - curfrp->fr_width < room_reserved)
4853 room_reserved = 0;
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /*
4856 * set the current frame to the new width
4857 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004858 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 /*
4861 * First take lines from the frames right of the current frame. If
4862 * that is not enough, takes lines from frames left of the current
4863 * frame.
4864 */
4865 for (run = 0; run < 2; ++run)
4866 {
4867 if (run == 0)
4868 frp = curfrp->fr_next; /* 1st run: start with next window */
4869 else
4870 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4871 while (frp != NULL && take != 0)
4872 {
4873 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004874 if (room_reserved > 0
4875 && frp->fr_win != NULL
4876 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004878 if (room_reserved >= frp->fr_width)
4879 room_reserved -= frp->fr_width;
4880 else
4881 {
4882 if (frp->fr_width - room_reserved > take)
4883 room_reserved = frp->fr_width - take;
4884 take -= frp->fr_width - room_reserved;
4885 frame_new_width(frp, room_reserved, FALSE, FALSE);
4886 room_reserved = 0;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else
4890 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004891 if (frp->fr_width - take < w)
4892 {
4893 take -= frp->fr_width - w;
4894 frame_new_width(frp, w, FALSE, FALSE);
4895 }
4896 else
4897 {
4898 frame_new_width(frp, frp->fr_width - take,
4899 FALSE, FALSE);
4900 take = 0;
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 if (run == 0)
4904 frp = frp->fr_next;
4905 else
4906 frp = frp->fr_prev;
4907 }
4908 }
4909 }
4910}
4911#endif /* FEAT_VERTSPLIT */
4912
4913/*
4914 * Check 'winminheight' for a valid value.
4915 */
4916 void
4917win_setminheight()
4918{
4919 int room;
4920 int first = TRUE;
4921 win_T *wp;
4922
4923 /* loop until there is a 'winminheight' that is possible */
4924 while (p_wmh > 0)
4925 {
4926 /* TODO: handle vertical splits */
4927 room = -p_wh;
4928 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4929 room += wp->w_height - p_wmh;
4930 if (room >= 0)
4931 break;
4932 --p_wmh;
4933 if (first)
4934 {
4935 EMSG(_(e_noroom));
4936 first = FALSE;
4937 }
4938 }
4939}
4940
4941#ifdef FEAT_MOUSE
4942
4943/*
4944 * Status line of dragwin is dragged "offset" lines down (negative is up).
4945 */
4946 void
4947win_drag_status_line(dragwin, offset)
4948 win_T *dragwin;
4949 int offset;
4950{
4951 frame_T *curfr;
4952 frame_T *fr;
4953 int room;
4954 int row;
4955 int up; /* if TRUE, drag status line up, otherwise down */
4956 int n;
4957
4958 fr = dragwin->w_frame;
4959 curfr = fr;
4960 if (fr != topframe) /* more than one window */
4961 {
4962 fr = fr->fr_parent;
4963 /* When the parent frame is not a column of frames, its parent should
4964 * be. */
4965 if (fr->fr_layout != FR_COL)
4966 {
4967 curfr = fr;
4968 if (fr != topframe) /* only a row of windows, may drag statusline */
4969 fr = fr->fr_parent;
4970 }
4971 }
4972
4973 /* If this is the last frame in a column, may want to resize the parent
4974 * frame instead (go two up to skip a row of frames). */
4975 while (curfr != topframe && curfr->fr_next == NULL)
4976 {
4977 if (fr != topframe)
4978 fr = fr->fr_parent;
4979 curfr = fr;
4980 if (fr != topframe)
4981 fr = fr->fr_parent;
4982 }
4983
4984 if (offset < 0) /* drag up */
4985 {
4986 up = TRUE;
4987 offset = -offset;
4988 /* sum up the room of the current frame and above it */
4989 if (fr == curfr)
4990 {
4991 /* only one window */
4992 room = fr->fr_height - frame_minheight(fr, NULL);
4993 }
4994 else
4995 {
4996 room = 0;
4997 for (fr = fr->fr_child; ; fr = fr->fr_next)
4998 {
4999 room += fr->fr_height - frame_minheight(fr, NULL);
5000 if (fr == curfr)
5001 break;
5002 }
5003 }
5004 fr = curfr->fr_next; /* put fr at frame that grows */
5005 }
5006 else /* drag down */
5007 {
5008 up = FALSE;
5009 /*
5010 * Only dragging the last status line can reduce p_ch.
5011 */
5012 room = Rows - cmdline_row;
5013 if (curfr->fr_next == NULL)
5014 room -= 1;
5015 else
5016 room -= p_ch;
5017 if (room < 0)
5018 room = 0;
5019 /* sum up the room of frames below of the current one */
5020 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5021 room += fr->fr_height - frame_minheight(fr, NULL);
5022 fr = curfr; /* put fr at window that grows */
5023 }
5024
5025 if (room < offset) /* Not enough room */
5026 offset = room; /* Move as far as we can */
5027 if (offset <= 0)
5028 return;
5029
5030 /*
5031 * Grow frame fr by "offset" lines.
5032 * Doesn't happen when dragging the last status line up.
5033 */
5034 if (fr != NULL)
5035 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5036
5037 if (up)
5038 fr = curfr; /* current frame gets smaller */
5039 else
5040 fr = curfr->fr_next; /* next frame gets smaller */
5041
5042 /*
5043 * Now make the other frames smaller.
5044 */
5045 while (fr != NULL && offset > 0)
5046 {
5047 n = frame_minheight(fr, NULL);
5048 if (fr->fr_height - offset <= n)
5049 {
5050 offset -= fr->fr_height - n;
5051 frame_new_height(fr, n, !up, FALSE);
5052 }
5053 else
5054 {
5055 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5056 break;
5057 }
5058 if (up)
5059 fr = fr->fr_prev;
5060 else
5061 fr = fr->fr_next;
5062 }
5063 row = win_comp_pos();
5064 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5065 cmdline_row = row;
5066 p_ch = Rows - cmdline_row;
5067 if (p_ch < 1)
5068 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005069 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005070 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 showmode();
5072}
5073
5074#ifdef FEAT_VERTSPLIT
5075/*
5076 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5077 */
5078 void
5079win_drag_vsep_line(dragwin, offset)
5080 win_T *dragwin;
5081 int offset;
5082{
5083 frame_T *curfr;
5084 frame_T *fr;
5085 int room;
5086 int left; /* if TRUE, drag separator line left, otherwise right */
5087 int n;
5088
5089 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005090 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 return;
5092 curfr = fr;
5093 fr = fr->fr_parent;
5094 /* When the parent frame is not a row of frames, its parent should be. */
5095 if (fr->fr_layout != FR_ROW)
5096 {
5097 if (fr == topframe) /* only a column of windows (cannot happen?) */
5098 return;
5099 curfr = fr;
5100 fr = fr->fr_parent;
5101 }
5102
5103 /* If this is the last frame in a row, may want to resize a parent
5104 * frame instead. */
5105 while (curfr->fr_next == NULL)
5106 {
5107 if (fr == topframe)
5108 break;
5109 curfr = fr;
5110 fr = fr->fr_parent;
5111 if (fr != topframe)
5112 {
5113 curfr = fr;
5114 fr = fr->fr_parent;
5115 }
5116 }
5117
5118 if (offset < 0) /* drag left */
5119 {
5120 left = TRUE;
5121 offset = -offset;
5122 /* sum up the room of the current frame and left of it */
5123 room = 0;
5124 for (fr = fr->fr_child; ; fr = fr->fr_next)
5125 {
5126 room += fr->fr_width - frame_minwidth(fr, NULL);
5127 if (fr == curfr)
5128 break;
5129 }
5130 fr = curfr->fr_next; /* put fr at frame that grows */
5131 }
5132 else /* drag right */
5133 {
5134 left = FALSE;
5135 /* sum up the room of frames right of the current one */
5136 room = 0;
5137 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5138 room += fr->fr_width - frame_minwidth(fr, NULL);
5139 fr = curfr; /* put fr at window that grows */
5140 }
5141
5142 if (room < offset) /* Not enough room */
5143 offset = room; /* Move as far as we can */
5144 if (offset <= 0) /* No room at all, quit. */
5145 return;
5146
5147 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005148 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 /* shrink other frames: current and at the left or at the right */
5151 if (left)
5152 fr = curfr; /* current frame gets smaller */
5153 else
5154 fr = curfr->fr_next; /* next frame gets smaller */
5155
5156 while (fr != NULL && offset > 0)
5157 {
5158 n = frame_minwidth(fr, NULL);
5159 if (fr->fr_width - offset <= n)
5160 {
5161 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005162 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 else
5165 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005166 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 break;
5168 }
5169 if (left)
5170 fr = fr->fr_prev;
5171 else
5172 fr = fr->fr_next;
5173 }
5174 (void)win_comp_pos();
5175 redraw_all_later(NOT_VALID);
5176}
5177#endif /* FEAT_VERTSPLIT */
5178#endif /* FEAT_MOUSE */
5179
5180#endif /* FEAT_WINDOWS */
5181
5182/*
5183 * Set the height of a window.
5184 * This takes care of the things inside the window, not what happens to the
5185 * window position, the frame or to other windows.
5186 */
5187 static void
5188win_new_height(wp, height)
5189 win_T *wp;
5190 int height;
5191{
5192 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005193 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005195 int space;
5196 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197#define FRACTION_MULT 16384L
5198
5199 /* Don't want a negative height. Happens when splitting a tiny window.
5200 * Will equalize heights soon to fix it. */
5201 if (height < 0)
5202 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005203 if (wp->w_height == height)
5204 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5207 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5208 + FRACTION_MULT / 2) / (long)wp->w_height;
5209
5210 wp->w_height = height;
5211 wp->w_skipcol = 0;
5212
5213 /* Don't change w_topline when height is zero. Don't set w_topline when
5214 * 'scrollbind' is set and this isn't the current window. */
5215 if (height > 0
5216#ifdef FEAT_SCROLLBIND
5217 && (!wp->w_p_scb || wp == curwin)
5218#endif
5219 )
5220 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005221 /*
5222 * Find a value for w_topline that shows the cursor at the same
5223 * relative position in the window as before (more or less).
5224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 lnum = wp->w_cursor.lnum;
5226 if (lnum < 1) /* can happen when starting up */
5227 lnum = 1;
5228 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5229 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5230 sline = wp->w_wrow - line_size;
5231 if (sline < 0)
5232 {
5233 /*
5234 * Cursor line would go off top of screen if w_wrow was this high.
5235 */
5236 wp->w_wrow = line_size;
5237 }
5238 else
5239 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005240 space = height;
5241 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005243 space -= line_size;
5244 if (space > 0 && sline <= 0 && !did_below)
5245 {
5246 /* Try to use "~" lines below the text to avoid that text
5247 * is above the window while there are empty lines.
5248 * Subtract the rows below the cursor from "space" and
5249 * give the rest to "sline". */
5250 did_below = TRUE;
5251 bot = wp->w_cursor.lnum;
5252 while (space > 0)
5253 {
5254 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5255 space = 0;
5256 else
5257 {
5258#ifdef FEAT_FOLDING
5259 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5260#endif
5261 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5262 break;
5263 ++bot;
5264 space -= plines_win(wp, bot, TRUE);
5265 }
5266 }
5267 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5268 sline += space;
5269 }
5270 if (sline <= 0)
5271 break;
5272
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#ifdef FEAT_FOLDING
5274 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5275 if (lnum == 1)
5276 {
5277 /* first line in buffer is folded */
5278 line_size = 1;
5279 --sline;
5280 break;
5281 }
5282#endif
5283 --lnum;
5284#ifdef FEAT_DIFF
5285 if (lnum == wp->w_topline)
5286 line_size = plines_win_nofill(wp, lnum, TRUE)
5287 + wp->w_topfill;
5288 else
5289#endif
5290 line_size = plines_win(wp, lnum, TRUE);
5291 sline -= line_size;
5292 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (sline < 0)
5295 {
5296 /*
5297 * Line we want at top would go off top of screen. Use next
5298 * line instead.
5299 */
5300#ifdef FEAT_FOLDING
5301 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5302#endif
5303 lnum++;
5304 wp->w_wrow -= line_size + sline;
5305 }
5306 else if (sline > 0)
5307 {
5308 /* First line of file reached, use that as topline. */
5309 lnum = 1;
5310 wp->w_wrow -= sline;
5311 }
5312 }
5313 set_topline(wp, lnum);
5314 }
5315
5316 if (wp == curwin)
5317 {
5318 if (p_so)
5319 update_topline();
5320 curs_columns(FALSE); /* validate w_wrow */
5321 }
5322 wp->w_prev_fraction_row = wp->w_wrow;
5323
5324 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005325 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#ifdef FEAT_WINDOWS
5327 wp->w_redr_status = TRUE;
5328#endif
5329 invalidate_botline_win(wp);
5330}
5331
5332#ifdef FEAT_VERTSPLIT
5333/*
5334 * Set the width of a window.
5335 */
5336 static void
5337win_new_width(wp, width)
5338 win_T *wp;
5339 int width;
5340{
5341 wp->w_width = width;
5342 wp->w_lines_valid = 0;
5343 changed_line_abv_curs_win(wp);
5344 invalidate_botline_win(wp);
5345 if (wp == curwin)
5346 {
5347 update_topline();
5348 curs_columns(TRUE); /* validate w_wrow */
5349 }
5350 redraw_win_later(wp, NOT_VALID);
5351 wp->w_redr_status = TRUE;
5352}
5353#endif
5354
5355 void
5356win_comp_scroll(wp)
5357 win_T *wp;
5358{
5359 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5360 if (wp->w_p_scr == 0)
5361 wp->w_p_scr = 1;
5362}
5363
5364/*
5365 * command_height: called whenever p_ch has been changed
5366 */
5367 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005368command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370#ifdef FEAT_WINDOWS
5371 int h;
5372 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005373 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005375 /* Use the value of p_ch that we remembered. This is needed for when the
5376 * GUI starts up, we can't be sure in what order things happen. And when
5377 * p_ch was changed in another tab page. */
5378 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /* Find bottom frame with width of screen. */
5381 frp = lastwin->w_frame;
5382# ifdef FEAT_VERTSPLIT
5383 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5384 frp = frp->fr_parent;
5385# endif
5386
5387 /* Avoid changing the height of a window with 'winfixheight' set. */
5388 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5389 && frp->fr_win->w_p_wfh)
5390 frp = frp->fr_prev;
5391
5392 if (starting != NO_SCREEN)
5393 {
5394 cmdline_row = Rows - p_ch;
5395
5396 if (p_ch > old_p_ch) /* p_ch got bigger */
5397 {
5398 while (p_ch > old_p_ch)
5399 {
5400 if (frp == NULL)
5401 {
5402 EMSG(_(e_noroom));
5403 p_ch = old_p_ch;
5404 cmdline_row = Rows - p_ch;
5405 break;
5406 }
5407 h = frp->fr_height - frame_minheight(frp, NULL);
5408 if (h > p_ch - old_p_ch)
5409 h = p_ch - old_p_ch;
5410 old_p_ch += h;
5411 frame_add_height(frp, -h);
5412 frp = frp->fr_prev;
5413 }
5414
5415 /* Recompute window positions. */
5416 (void)win_comp_pos();
5417
5418 /* clear the lines added to cmdline */
5419 if (full_screen)
5420 screen_fill((int)(cmdline_row), (int)Rows, 0,
5421 (int)Columns, ' ', ' ', 0);
5422 msg_row = cmdline_row;
5423 redraw_cmdline = TRUE;
5424 return;
5425 }
5426
5427 if (msg_row < cmdline_row)
5428 msg_row = cmdline_row;
5429 redraw_cmdline = TRUE;
5430 }
5431 frame_add_height(frp, (int)(old_p_ch - p_ch));
5432
5433 /* Recompute window positions. */
5434 if (frp != lastwin->w_frame)
5435 (void)win_comp_pos();
5436#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005438 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
5440}
5441
5442#if defined(FEAT_WINDOWS) || defined(PROTO)
5443/*
5444 * Resize frame "frp" to be "n" lines higher (negative for less high).
5445 * Also resize the frames it is contained in.
5446 */
5447 static void
5448frame_add_height(frp, n)
5449 frame_T *frp;
5450 int n;
5451{
5452 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5453 for (;;)
5454 {
5455 frp = frp->fr_parent;
5456 if (frp == NULL)
5457 break;
5458 frp->fr_height += n;
5459 }
5460}
5461
5462/*
5463 * Add or remove a status line for the bottom window(s), according to the
5464 * value of 'laststatus'.
5465 */
5466 void
5467last_status(morewin)
5468 int morewin; /* pretend there are two or more windows */
5469{
5470 /* Don't make a difference between horizontal or vertical split. */
5471 last_status_rec(topframe, (p_ls == 2
5472 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5473}
5474
5475 static void
5476last_status_rec(fr, statusline)
5477 frame_T *fr;
5478 int statusline;
5479{
5480 frame_T *fp;
5481 win_T *wp;
5482
5483 if (fr->fr_layout == FR_LEAF)
5484 {
5485 wp = fr->fr_win;
5486 if (wp->w_status_height != 0 && !statusline)
5487 {
5488 /* remove status line */
5489 win_new_height(wp, wp->w_height + 1);
5490 wp->w_status_height = 0;
5491 comp_col();
5492 }
5493 else if (wp->w_status_height == 0 && statusline)
5494 {
5495 /* Find a frame to take a line from. */
5496 fp = fr;
5497 while (fp->fr_height <= frame_minheight(fp, NULL))
5498 {
5499 if (fp == topframe)
5500 {
5501 EMSG(_(e_noroom));
5502 return;
5503 }
5504 /* In a column of frames: go to frame above. If already at
5505 * the top or in a row of frames: go to parent. */
5506 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5507 fp = fp->fr_prev;
5508 else
5509 fp = fp->fr_parent;
5510 }
5511 wp->w_status_height = 1;
5512 if (fp != fr)
5513 {
5514 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5515 frame_fix_height(wp);
5516 (void)win_comp_pos();
5517 }
5518 else
5519 win_new_height(wp, wp->w_height - 1);
5520 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005521 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523 }
5524#ifdef FEAT_VERTSPLIT
5525 else if (fr->fr_layout == FR_ROW)
5526 {
5527 /* vertically split windows, set status line for each one */
5528 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5529 last_status_rec(fp, statusline);
5530 }
5531#endif
5532 else
5533 {
5534 /* horizontally split window, set status line for last one */
5535 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5536 ;
5537 last_status_rec(fp, statusline);
5538 }
5539}
5540
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005541/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005542 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005543 */
5544 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005545tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005546{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005547#ifdef FEAT_GUI_TABLINE
5548 /* When the GUI has the tabline then this always returns zero. */
5549 if (gui_use_tabline())
5550 return 0;
5551#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005552 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005553 {
5554 case 0: return 0;
5555 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5556 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005557 return 1;
5558}
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560#endif /* FEAT_WINDOWS */
5561
5562#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5563/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005564 * Get the file name at the cursor.
5565 * If Visual mode is active, use the selected text if it's in one line.
5566 * Returns the name in allocated memory, NULL for failure.
5567 */
5568 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005569grab_file_name(count, file_lnum)
5570 long count;
5571 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005572{
5573# ifdef FEAT_VISUAL
5574 if (VIsual_active)
5575 {
5576 int len;
5577 char_u *ptr;
5578
5579 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5580 return NULL;
5581 return find_file_name_in_path(ptr, len,
5582 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5583 }
5584# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005585 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5586 file_lnum);
5587
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005588}
5589
5590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 * Return the file name under or after the cursor.
5592 *
5593 * The 'path' option is searched if the file name is not absolute.
5594 * The string returned has been alloc'ed and should be freed by the caller.
5595 * NULL is returned if the file name or file is not found.
5596 *
5597 * options:
5598 * FNAME_MESS give error messages
5599 * FNAME_EXP expand to path
5600 * FNAME_HYP check for hypertext link
5601 * FNAME_INCL apply "includeexpr"
5602 */
5603 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005604file_name_at_cursor(options, count, file_lnum)
5605 int options;
5606 long count;
5607 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005610 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5611 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
5614/*
5615 * Return the name of the file under or after ptr[col].
5616 * Otherwise like file_name_at_cursor().
5617 */
5618 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005619file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 char_u *line;
5621 int col;
5622 int options;
5623 long count;
5624 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005625 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 char_u *ptr;
5628 int len;
5629
5630 /*
5631 * search forward for what could be the start of a file name
5632 */
5633 ptr = line + col;
5634 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005635 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (*ptr == NUL) /* nothing found */
5637 {
5638 if (options & FNAME_MESS)
5639 EMSG(_("E446: No file name under cursor"));
5640 return NULL;
5641 }
5642
5643 /*
5644 * Search backward for first char of the file name.
5645 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5646 */
5647 while (ptr > line)
5648 {
5649#ifdef FEAT_MBYTE
5650 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5651 ptr -= len + 1;
5652 else
5653#endif
5654 if (vim_isfilec(ptr[-1])
5655 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5656 --ptr;
5657 else
5658 break;
5659 }
5660
5661 /*
5662 * Search forward for the last char of the file name.
5663 * Also allow "://" when ':' is not in 'isfname'.
5664 */
5665 len = 0;
5666 while (vim_isfilec(ptr[len])
5667 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5668#ifdef FEAT_MBYTE
5669 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005670 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
5672#endif
5673 ++len;
5674
5675 /*
5676 * If there is trailing punctuation, remove it.
5677 * But don't remove "..", could be a directory name.
5678 */
5679 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5680 && ptr[len - 2] != '.')
5681 --len;
5682
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005683 if (file_lnum != NULL)
5684 {
5685 char_u *p;
5686
5687 /* Get the number after the file name and a separator character */
5688 p = ptr + len;
5689 p = skipwhite(p);
5690 if (*p != NUL)
5691 {
5692 if (!isdigit(*p))
5693 ++p; /* skip the separator */
5694 p = skipwhite(p);
5695 if (isdigit(*p))
5696 *file_lnum = (int)getdigits(&p);
5697 }
5698 }
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5701}
5702
5703# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5704static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5705
5706 static char_u *
5707eval_includeexpr(ptr, len)
5708 char_u *ptr;
5709 int len;
5710{
5711 char_u *res;
5712
5713 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005714 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005715 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 set_vim_var_string(VV_FNAME, NULL, 0);
5717 return res;
5718}
5719#endif
5720
5721/*
5722 * Return the name of the file ptr[len] in 'path'.
5723 * Otherwise like file_name_at_cursor().
5724 */
5725 char_u *
5726find_file_name_in_path(ptr, len, options, count, rel_fname)
5727 char_u *ptr;
5728 int len;
5729 int options;
5730 long count;
5731 char_u *rel_fname; /* file we are searching relative to */
5732{
5733 char_u *file_name;
5734 int c;
5735# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5736 char_u *tofree = NULL;
5737
5738 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5739 {
5740 tofree = eval_includeexpr(ptr, len);
5741 if (tofree != NULL)
5742 {
5743 ptr = tofree;
5744 len = (int)STRLEN(ptr);
5745 }
5746 }
5747# endif
5748
5749 if (options & FNAME_EXP)
5750 {
5751 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5752 TRUE, rel_fname);
5753
5754# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5755 /*
5756 * If the file could not be found in a normal way, try applying
5757 * 'includeexpr' (unless done already).
5758 */
5759 if (file_name == NULL
5760 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5761 {
5762 tofree = eval_includeexpr(ptr, len);
5763 if (tofree != NULL)
5764 {
5765 ptr = tofree;
5766 len = (int)STRLEN(ptr);
5767 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5768 TRUE, rel_fname);
5769 }
5770 }
5771# endif
5772 if (file_name == NULL && (options & FNAME_MESS))
5773 {
5774 c = ptr[len];
5775 ptr[len] = NUL;
5776 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5777 ptr[len] = c;
5778 }
5779
5780 /* Repeat finding the file "count" times. This matters when it
5781 * appears several times in the path. */
5782 while (file_name != NULL && --count > 0)
5783 {
5784 vim_free(file_name);
5785 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5786 }
5787 }
5788 else
5789 file_name = vim_strnsave(ptr, len);
5790
5791# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5792 vim_free(tofree);
5793# endif
5794
5795 return file_name;
5796}
5797#endif /* FEAT_SEARCHPATH */
5798
5799/*
5800 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5801 * Also check for ":\\", which MS Internet Explorer accepts, return
5802 * URL_BACKSLASH.
5803 */
5804 static int
5805path_is_url(p)
5806 char_u *p;
5807{
5808 if (STRNCMP(p, "://", (size_t)3) == 0)
5809 return URL_SLASH;
5810 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5811 return URL_BACKSLASH;
5812 return 0;
5813}
5814
5815/*
5816 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5817 * Return URL_BACKSLASH for "name:\\".
5818 * Return zero otherwise.
5819 */
5820 int
5821path_with_url(fname)
5822 char_u *fname;
5823{
5824 char_u *p;
5825
5826 for (p = fname; isalpha(*p); ++p)
5827 ;
5828 return path_is_url(p);
5829}
5830
5831/*
5832 * Return TRUE if "name" is a full (absolute) path name or URL.
5833 */
5834 int
5835vim_isAbsName(name)
5836 char_u *name;
5837{
5838 return (path_with_url(name) != 0 || mch_isFullName(name));
5839}
5840
5841/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005842 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 *
5844 * return FAIL for failure, OK otherwise
5845 */
5846 int
5847vim_FullName(fname, buf, len, force)
5848 char_u *fname, *buf;
5849 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005850 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 int retval = OK;
5853 int url;
5854
5855 *buf = NUL;
5856 if (fname == NULL)
5857 return FAIL;
5858
5859 url = path_with_url(fname);
5860 if (!url)
5861 retval = mch_FullName(fname, buf, len, force);
5862 if (url || retval == FAIL)
5863 {
5864 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005865 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 }
5867#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5868 slash_adjust(buf);
5869#endif
5870 return retval;
5871}
5872
5873/*
5874 * Return the minimal number of rows that is needed on the screen to display
5875 * the current number of windows.
5876 */
5877 int
5878min_rows()
5879{
5880 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005881#ifdef FEAT_WINDOWS
5882 tabpage_T *tp;
5883 int n;
5884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 if (firstwin == NULL) /* not initialized yet */
5887 return MIN_LINES;
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005890 total = 0;
5891 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5892 {
5893 n = frame_minheight(tp->tp_topframe, NULL);
5894 if (total < n)
5895 total = n;
5896 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005897 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005899 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005901 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 return total;
5903}
5904
5905/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005906 * Return TRUE if there is only one window (in the current tab page), not
5907 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 */
5909 int
5910only_one_window()
5911{
5912#ifdef FEAT_WINDOWS
5913 int count = 0;
5914 win_T *wp;
5915
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916 /* If there is another tab page there always is another window. */
5917 if (first_tabpage->tp_next != NULL)
5918 return FALSE;
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005921 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922# ifdef FEAT_QUICKFIX
5923 || wp->w_p_pvw
5924# endif
5925 ) || wp == curwin)
5926 ++count;
5927 return (count <= 1);
5928#else
5929 return TRUE;
5930#endif
5931}
5932
5933#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5934/*
5935 * Correct the cursor line number in other windows. Used after changing the
5936 * current buffer, and before applying autocommands.
5937 * When "do_curwin" is TRUE, also check current window.
5938 */
5939 void
5940check_lnums(do_curwin)
5941 int do_curwin;
5942{
5943 win_T *wp;
5944
5945#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005946 tabpage_T *tp;
5947
5948 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5950#else
5951 wp = curwin;
5952 if (do_curwin)
5953#endif
5954 {
5955 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5956 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5957 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5958 wp->w_topline = curbuf->b_ml.ml_line_count;
5959 }
5960}
5961#endif
5962
5963#if defined(FEAT_WINDOWS) || defined(PROTO)
5964
5965/*
5966 * A snapshot of the window sizes, to restore them after closing the help
5967 * window.
5968 * Only these fields are used:
5969 * fr_layout
5970 * fr_width
5971 * fr_height
5972 * fr_next
5973 * fr_child
5974 * fr_win (only valid for the old curwin, NULL otherwise)
5975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977/*
5978 * Create a snapshot of the current frame sizes.
5979 */
5980 static void
5981make_snapshot()
5982{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005983 clear_snapshot(curtab);
5984 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985}
5986
5987 static void
5988make_snapshot_rec(fr, frp)
5989 frame_T *fr;
5990 frame_T **frp;
5991{
5992 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5993 if (*frp == NULL)
5994 return;
5995 (*frp)->fr_layout = fr->fr_layout;
5996# ifdef FEAT_VERTSPLIT
5997 (*frp)->fr_width = fr->fr_width;
5998# endif
5999 (*frp)->fr_height = fr->fr_height;
6000 if (fr->fr_next != NULL)
6001 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6002 if (fr->fr_child != NULL)
6003 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6004 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6005 (*frp)->fr_win = curwin;
6006}
6007
6008/*
6009 * Remove any existing snapshot.
6010 */
6011 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006012clear_snapshot(tp)
6013 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006015 clear_snapshot_rec(tp->tp_snapshot);
6016 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018
6019 static void
6020clear_snapshot_rec(fr)
6021 frame_T *fr;
6022{
6023 if (fr != NULL)
6024 {
6025 clear_snapshot_rec(fr->fr_next);
6026 clear_snapshot_rec(fr->fr_child);
6027 vim_free(fr);
6028 }
6029}
6030
6031/*
6032 * Restore a previously created snapshot, if there is any.
6033 * This is only done if the screen size didn't change and the window layout is
6034 * still the same.
6035 */
6036 static void
6037restore_snapshot(close_curwin)
6038 int close_curwin; /* closing current window */
6039{
6040 win_T *wp;
6041
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006042 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006044 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006046 && curtab->tp_snapshot->fr_height == topframe->fr_height
6047 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006049 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 win_comp_pos();
6051 if (wp != NULL && close_curwin)
6052 win_goto(wp);
6053 redraw_all_later(CLEAR);
6054 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006055 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056}
6057
6058/*
6059 * Check if frames "sn" and "fr" have the same layout, same following frames
6060 * and same children.
6061 */
6062 static int
6063check_snapshot_rec(sn, fr)
6064 frame_T *sn;
6065 frame_T *fr;
6066{
6067 if (sn->fr_layout != fr->fr_layout
6068 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6069 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6070 || (sn->fr_next != NULL
6071 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6072 || (sn->fr_child != NULL
6073 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6074 return FAIL;
6075 return OK;
6076}
6077
6078/*
6079 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6080 * following frames and children.
6081 * Returns a pointer to the old current window, or NULL.
6082 */
6083 static win_T *
6084restore_snapshot_rec(sn, fr)
6085 frame_T *sn;
6086 frame_T *fr;
6087{
6088 win_T *wp = NULL;
6089 win_T *wp2;
6090
6091 fr->fr_height = sn->fr_height;
6092# ifdef FEAT_VERTSPLIT
6093 fr->fr_width = sn->fr_width;
6094# endif
6095 if (fr->fr_layout == FR_LEAF)
6096 {
6097 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6098# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006099 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100# endif
6101 wp = sn->fr_win;
6102 }
6103 if (sn->fr_next != NULL)
6104 {
6105 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6106 if (wp2 != NULL)
6107 wp = wp2;
6108 }
6109 if (sn->fr_child != NULL)
6110 {
6111 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6112 if (wp2 != NULL)
6113 wp = wp2;
6114 }
6115 return wp;
6116}
6117
6118#endif
6119
6120#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6121/*
6122 * Return TRUE if there is any vertically split window.
6123 */
6124 int
6125win_hasvertsplit()
6126{
6127 frame_T *fr;
6128
6129 if (topframe->fr_layout == FR_ROW)
6130 return TRUE;
6131
6132 if (topframe->fr_layout == FR_COL)
6133 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6134 if (fr->fr_layout == FR_ROW)
6135 return TRUE;
6136
6137 return FALSE;
6138}
6139#endif