blob: 0c0fb30d5d8dfb7a0e487aae38ae09d7c60d5429 [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
39static void frame_add_statusline __ARGS((frame_T *frp));
40static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst));
41static void frame_add_vsep __ARGS((frame_T *frp));
42static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
43static void frame_fix_width __ARGS((win_T *wp));
44#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000046static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000047#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000048static tabpage_T *alloc_tabpage __ARGS((void));
49static void free_tabpage __ARGS((tabpage_T *tp));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000050static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000051static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052static void frame_fix_height __ARGS((win_T *wp));
53static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
54static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000055static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000056static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000057static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void frame_append __ARGS((frame_T *after, frame_T *frp));
59static void frame_insert __ARGS((frame_T *before, frame_T *frp));
60static void frame_remove __ARGS((frame_T *frp));
61#ifdef FEAT_VERTSPLIT
62static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void win_goto_ver __ARGS((int up, long count));
64static void win_goto_hor __ARGS((int left, long count));
65#endif
66static void frame_add_height __ARGS((frame_T *frp, int n));
67static void last_status_rec __ARGS((frame_T *fr, int statusline));
68
69static void make_snapshot __ARGS((void));
70static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000071static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072static void clear_snapshot_rec __ARGS((frame_T *fr));
73static void restore_snapshot __ARGS((int close_curwin));
74static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
75static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76
77#endif /* FEAT_WINDOWS */
78static win_T *win_alloc __ARGS((win_T *after));
79static void win_new_height __ARGS((win_T *, int));
80
81#define URL_SLASH 1 /* path_is_url() has found "://" */
82#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
83
84#define NOWIN (win_T *)-1 /* non-exisiting window */
85
Bram Moolenaar05159a02005-02-26 23:04:13 +000086#ifdef FEAT_WINDOWS
87static long p_ch_used = 1L; /* value of 'cmdheight' when frame
88 size was set */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000089# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000090#else
91# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000092#endif
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#if defined(FEAT_WINDOWS) || defined(PROTO)
95/*
96 * all CTRL-W window commands are handled here, called from normal_cmd().
97 */
98 void
99do_window(nchar, Prenum, xchar)
100 int nchar;
101 long Prenum;
102 int xchar; /* extra char from ":wincmd gx" or NUL */
103{
104 long Prenum1;
105 win_T *wp;
106#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
107 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000108 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110#ifdef FEAT_FIND_ID
111 int type = FIND_DEFINE;
112 int len;
113#endif
114 char_u cbuf[40];
115
116 if (Prenum == 0)
117 Prenum1 = 1;
118 else
119 Prenum1 = Prenum;
120
121#ifdef FEAT_CMDWIN
122# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
123#else
124# define CHECK_CMDWIN
125#endif
126
127 switch (nchar)
128 {
129/* split current window in two parts, horizontally */
130 case 'S':
131 case Ctrl_S:
132 case 's':
133 CHECK_CMDWIN
134#ifdef FEAT_VISUAL
135 reset_VIsual_and_resel(); /* stop Visual mode */
136#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000137#ifdef FEAT_QUICKFIX
138 /* When splitting the quickfix window open a new buffer in it,
139 * don't replicate the quickfix buffer. */
140 if (bt_quickfix(curbuf))
141 goto newwindow;
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_GUI
144 need_mouse_correct = TRUE;
145#endif
146 win_split((int)Prenum, 0);
147 break;
148
149#ifdef FEAT_VERTSPLIT
150/* split current window in two parts, vertically */
151 case Ctrl_V:
152 case 'v':
153 CHECK_CMDWIN
154#ifdef FEAT_VISUAL
155 reset_VIsual_and_resel(); /* stop Visual mode */
156#endif
157#ifdef FEAT_GUI
158 need_mouse_correct = TRUE;
159#endif
160 win_split((int)Prenum, WSP_VERT);
161 break;
162#endif
163
164/* split current window and edit alternate file */
165 case Ctrl_HAT:
166 case '^':
167 CHECK_CMDWIN
168#ifdef FEAT_VISUAL
169 reset_VIsual_and_resel(); /* stop Visual mode */
170#endif
171 STRCPY(cbuf, "split #");
172 if (Prenum)
173 sprintf((char *)cbuf + 7, "%ld", Prenum);
174 do_cmdline_cmd(cbuf);
175 break;
176
177/* open new window */
178 case Ctrl_N:
179 case 'n':
180 CHECK_CMDWIN
181#ifdef FEAT_VISUAL
182 reset_VIsual_and_resel(); /* stop Visual mode */
183#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000184#ifdef FEAT_QUICKFIX
185newwindow:
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (Prenum)
188 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
189 else
190 cbuf[0] = NUL;
191 STRCAT(cbuf, "new");
192 do_cmdline_cmd(cbuf);
193 break;
194
195/* quit current window */
196 case Ctrl_Q:
197 case 'q':
198#ifdef FEAT_VISUAL
199 reset_VIsual_and_resel(); /* stop Visual mode */
200#endif
201 do_cmdline_cmd((char_u *)"quit");
202 break;
203
204/* close current window */
205 case Ctrl_C:
206 case 'c':
207#ifdef FEAT_VISUAL
208 reset_VIsual_and_resel(); /* stop Visual mode */
209#endif
210 do_cmdline_cmd((char_u *)"close");
211 break;
212
213#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
214/* close preview window */
215 case Ctrl_Z:
216 case 'z':
217 CHECK_CMDWIN
218#ifdef FEAT_VISUAL
219 reset_VIsual_and_resel(); /* stop Visual mode */
220#endif
221 do_cmdline_cmd((char_u *)"pclose");
222 break;
223
224/* cursor to preview window */
225 case 'P':
226 for (wp = firstwin; wp != NULL; wp = wp->w_next)
227 if (wp->w_p_pvw)
228 break;
229 if (wp == NULL)
230 EMSG(_("E441: There is no preview window"));
231 else
232 win_goto(wp);
233 break;
234#endif
235
236/* close all but current window */
237 case Ctrl_O:
238 case 'o':
239 CHECK_CMDWIN
240#ifdef FEAT_VISUAL
241 reset_VIsual_and_resel(); /* stop Visual mode */
242#endif
243 do_cmdline_cmd((char_u *)"only");
244 break;
245
246/* cursor to next window with wrap around */
247 case Ctrl_W:
248 case 'w':
249/* cursor to previous window with wrap around */
250 case 'W':
251 CHECK_CMDWIN
252 if (lastwin == firstwin && Prenum != 1) /* just one window */
253 beep_flush();
254 else
255 {
256 if (Prenum) /* go to specified window */
257 {
258 for (wp = firstwin; --Prenum > 0; )
259 {
260 if (wp->w_next == NULL)
261 break;
262 else
263 wp = wp->w_next;
264 }
265 }
266 else
267 {
268 if (nchar == 'W') /* go to previous window */
269 {
270 wp = curwin->w_prev;
271 if (wp == NULL)
272 wp = lastwin; /* wrap around */
273 }
274 else /* go to next window */
275 {
276 wp = curwin->w_next;
277 if (wp == NULL)
278 wp = firstwin; /* wrap around */
279 }
280 }
281 win_goto(wp);
282 }
283 break;
284
285/* cursor to window below */
286 case 'j':
287 case K_DOWN:
288 case Ctrl_J:
289 CHECK_CMDWIN
290#ifdef FEAT_VERTSPLIT
291 win_goto_ver(FALSE, Prenum1);
292#else
293 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
294 wp = wp->w_next)
295 ;
296 win_goto(wp);
297#endif
298 break;
299
300/* cursor to window above */
301 case 'k':
302 case K_UP:
303 case Ctrl_K:
304 CHECK_CMDWIN
305#ifdef FEAT_VERTSPLIT
306 win_goto_ver(TRUE, Prenum1);
307#else
308 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
309 wp = wp->w_prev)
310 ;
311 win_goto(wp);
312#endif
313 break;
314
315#ifdef FEAT_VERTSPLIT
316/* cursor to left window */
317 case 'h':
318 case K_LEFT:
319 case Ctrl_H:
320 case K_BS:
321 CHECK_CMDWIN
322 win_goto_hor(TRUE, Prenum1);
323 break;
324
325/* cursor to right window */
326 case 'l':
327 case K_RIGHT:
328 case Ctrl_L:
329 CHECK_CMDWIN
330 win_goto_hor(FALSE, Prenum1);
331 break;
332#endif
333
334/* cursor to top-left window */
335 case 't':
336 case Ctrl_T:
337 win_goto(firstwin);
338 break;
339
340/* cursor to bottom-right window */
341 case 'b':
342 case Ctrl_B:
343 win_goto(lastwin);
344 break;
345
346/* cursor to last accessed (previous) window */
347 case 'p':
348 case Ctrl_P:
349 if (prevwin == NULL)
350 beep_flush();
351 else
352 win_goto(prevwin);
353 break;
354
355/* exchange current and next window */
356 case 'x':
357 case Ctrl_X:
358 CHECK_CMDWIN
359 win_exchange(Prenum);
360 break;
361
362/* rotate windows downwards */
363 case Ctrl_R:
364 case 'r':
365 CHECK_CMDWIN
366#ifdef FEAT_VISUAL
367 reset_VIsual_and_resel(); /* stop Visual mode */
368#endif
369 win_rotate(FALSE, (int)Prenum1); /* downwards */
370 break;
371
372/* rotate windows upwards */
373 case 'R':
374 CHECK_CMDWIN
375#ifdef FEAT_VISUAL
376 reset_VIsual_and_resel(); /* stop Visual mode */
377#endif
378 win_rotate(TRUE, (int)Prenum1); /* upwards */
379 break;
380
381/* move window to the very top/bottom/left/right */
382 case 'K':
383 case 'J':
384#ifdef FEAT_VERTSPLIT
385 case 'H':
386 case 'L':
387#endif
388 CHECK_CMDWIN
389 win_totop((int)Prenum,
390 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
391 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
392 break;
393
394/* make all windows the same height */
395 case '=':
396#ifdef FEAT_GUI
397 need_mouse_correct = TRUE;
398#endif
399 win_equal(NULL, FALSE, 'b');
400 break;
401
402/* increase current window height */
403 case '+':
404#ifdef FEAT_GUI
405 need_mouse_correct = TRUE;
406#endif
407 win_setheight(curwin->w_height + (int)Prenum1);
408 break;
409
410/* decrease current window height */
411 case '-':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_setheight(curwin->w_height - (int)Prenum1);
416 break;
417
418/* set current window height */
419 case Ctrl__:
420 case '_':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setheight(Prenum ? (int)Prenum : 9999);
425 break;
426
427#ifdef FEAT_VERTSPLIT
428/* increase current window width */
429 case '>':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setwidth(curwin->w_width + (int)Prenum1);
434 break;
435
436/* decrease current window width */
437 case '<':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setwidth(curwin->w_width - (int)Prenum1);
442 break;
443
444/* set current window width */
445 case '|':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
450 break;
451#endif
452
453/* jump to tag and split window if tag exists (in preview window) */
454#if defined(FEAT_QUICKFIX)
455 case '}':
456 CHECK_CMDWIN
457 if (Prenum)
458 g_do_tagpreview = Prenum;
459 else
460 g_do_tagpreview = p_pvh;
461 /*FALLTHROUGH*/
462#endif
463 case ']':
464 case Ctrl_RSB:
465 CHECK_CMDWIN
466#ifdef FEAT_VISUAL
467 reset_VIsual_and_resel(); /* stop Visual mode */
468#endif
469 if (Prenum)
470 postponed_split = Prenum;
471 else
472 postponed_split = -1;
473
474 /* Execute the command right here, required when
475 * "wincmd ]" was used in a function. */
476 do_nv_ident(Ctrl_RSB, NUL);
477 break;
478
479#ifdef FEAT_SEARCHPATH
480/* edit file name under cursor in a new window */
481 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000482 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 case Ctrl_F:
484 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000486 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 if (ptr != NULL)
488 {
489#ifdef FEAT_GUI
490 need_mouse_correct = TRUE;
491#endif
492 setpcmark();
493 if (win_split(0, 0) == OK)
494 {
495# ifdef FEAT_SCROLLBIND
496 curwin->w_p_scb = FALSE;
497# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000498 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
499 if (nchar == 'F' && lnum >= 0)
500 {
501 curwin->w_cursor.lnum = lnum;
502 check_cursor_lnum();
503 beginline(BL_SOL | BL_FIX);
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506 vim_free(ptr);
507 }
508 break;
509#endif
510
511#ifdef FEAT_FIND_ID
512/* Go to the first occurence of the identifier under cursor along path in a
513 * new window -- webb
514 */
515 case 'i': /* Go to any match */
516 case Ctrl_I:
517 type = FIND_ANY;
518 /* FALLTHROUGH */
519 case 'd': /* Go to definition, using 'define' */
520 case Ctrl_D:
521 CHECK_CMDWIN
522 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
523 break;
524 find_pattern_in_path(ptr, 0, len, TRUE,
525 Prenum == 0 ? TRUE : FALSE, type,
526 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
527 curwin->w_set_curswant = TRUE;
528 break;
529#endif
530
Bram Moolenaar05159a02005-02-26 23:04:13 +0000531 case K_KENTER:
532 case CAR:
533#if defined(FEAT_QUICKFIX)
534 /*
535 * In a quickfix window a <CR> jumps to the error under the
536 * cursor in a new window.
537 */
538 if (bt_quickfix(curbuf))
539 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000540 sprintf((char *)cbuf, "split +%ld%s",
541 (long)curwin->w_cursor.lnum,
542 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000543 do_cmdline_cmd(cbuf);
544 }
545#endif
546 break;
547
548
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549/* CTRL-W g extended commands */
550 case 'g':
551 case Ctrl_G:
552 CHECK_CMDWIN
553#ifdef USE_ON_FLY_SCROLL
554 dont_scroll = TRUE; /* disallow scrolling here */
555#endif
556 ++no_mapping;
557 ++allow_keys; /* no mapping for xchar, but allow key codes */
558 if (xchar == NUL)
559 xchar = safe_vgetc();
560#ifdef FEAT_LANGMAP
561 LANGMAP_ADJUST(xchar, TRUE);
562#endif
563 --no_mapping;
564 --allow_keys;
565#ifdef FEAT_CMDL_INFO
566 (void)add_to_showcmd(xchar);
567#endif
568 switch (xchar)
569 {
570#if defined(FEAT_QUICKFIX)
571 case '}':
572 xchar = Ctrl_RSB;
573 if (Prenum)
574 g_do_tagpreview = Prenum;
575 else
576 g_do_tagpreview = p_pvh;
577 /*FALLTHROUGH*/
578#endif
579 case ']':
580 case Ctrl_RSB:
581#ifdef FEAT_VISUAL
582 reset_VIsual_and_resel(); /* stop Visual mode */
583#endif
584 if (Prenum)
585 postponed_split = Prenum;
586 else
587 postponed_split = -1;
588
589 /* Execute the command right here, required when
590 * "wincmd g}" was used in a function. */
591 do_nv_ident('g', xchar);
592 break;
593
594 default:
595 beep_flush();
596 break;
597 }
598 break;
599
600 default: beep_flush();
601 break;
602 }
603}
604
605/*
606 * split the current window, implements CTRL-W s and :split
607 *
608 * "size" is the height or width for the new window, 0 to use half of current
609 * height or width.
610 *
611 * "flags":
612 * WSP_ROOM: require enough room for new window
613 * WSP_VERT: vertical split.
614 * WSP_TOP: open window at the top-left of the shell (help window).
615 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
616 * WSP_HELP: creating the help window, keep layout snapshot
617 *
618 * return FAIL for failure, OK otherwise
619 */
620 int
621win_split(size, flags)
622 int size;
623 int flags;
624{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000625 /* When the ":tab" modifier was used open a new tab page instead. */
626 if (may_open_tabpage() == OK)
627 return OK;
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 /* Add flags from ":vertical", ":topleft" and ":botright". */
630 flags |= cmdmod.split;
631 if ((flags & WSP_TOP) && (flags & WSP_BOT))
632 {
633 EMSG(_("E442: Can't split topleft and botright at the same time"));
634 return FAIL;
635 }
636
637 /* When creating the help window make a snapshot of the window layout.
638 * Otherwise clear the snapshot, it's now invalid. */
639 if (flags & WSP_HELP)
640 make_snapshot();
641 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000642 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 return win_split_ins(size, flags, NULL, 0);
645}
646
647/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000648 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * When "newwin" is not NULL: insert this window at the far
650 * top/left/right/bottom.
651 * return FAIL for failure, OK otherwise
652 */
653 static int
654win_split_ins(size, flags, newwin, dir)
655 int size;
656 int flags;
657 win_T *newwin;
658 int dir;
659{
660 win_T *wp = newwin;
661 win_T *oldwin;
662 int new_size = size;
663 int i;
664 int need_status = 0;
665 int do_equal = FALSE;
666 int needed;
667 int available;
668 int oldwin_height = 0;
669 int layout;
670 frame_T *frp, *curfrp;
671 int before;
672
673 if (flags & WSP_TOP)
674 oldwin = firstwin;
675 else if (flags & WSP_BOT)
676 oldwin = lastwin;
677 else
678 oldwin = curwin;
679
680 /* add a status line when p_ls == 1 and splitting the first window */
681 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
682 {
683 if (oldwin->w_height <= p_wmh && newwin == NULL)
684 {
685 EMSG(_(e_noroom));
686 return FAIL;
687 }
688 need_status = STATUS_HEIGHT;
689 }
690
691#ifdef FEAT_VERTSPLIT
692 if (flags & WSP_VERT)
693 {
694 layout = FR_ROW;
695 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
696
697 /*
698 * Check if we are able to split the current window and compute its
699 * width.
700 */
701 needed = p_wmw + 1;
702 if (flags & WSP_ROOM)
703 needed += p_wiw - p_wmw;
704 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
705 {
706 available = topframe->fr_width;
707 needed += frame_minwidth(topframe, NULL);
708 }
709 else
710 available = oldwin->w_width;
711 if (available < needed && newwin == NULL)
712 {
713 EMSG(_(e_noroom));
714 return FAIL;
715 }
716 if (new_size == 0)
717 new_size = oldwin->w_width / 2;
718 if (new_size > oldwin->w_width - p_wmw - 1)
719 new_size = oldwin->w_width - p_wmw - 1;
720 if (new_size < p_wmw)
721 new_size = p_wmw;
722
723 /* if it doesn't fit in the current window, need win_equal() */
724 if (oldwin->w_width - new_size - 1 < p_wmw)
725 do_equal = TRUE;
726 }
727 else
728#endif
729 {
730 layout = FR_COL;
731 do_equal = (p_ea && new_size == 0
732#ifdef FEAT_VERTSPLIT
733 && *p_ead != 'h'
734#endif
735 );
736
737 /*
738 * Check if we are able to split the current window and compute its
739 * height.
740 */
741 needed = p_wmh + STATUS_HEIGHT + need_status;
742 if (flags & WSP_ROOM)
743 needed += p_wh - p_wmh;
744 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
745 {
746 available = topframe->fr_height;
747 needed += frame_minheight(topframe, NULL);
748 }
749 else
750 {
751 available = oldwin->w_height;
752 needed += p_wmh;
753 }
754 if (available < needed && newwin == NULL)
755 {
756 EMSG(_(e_noroom));
757 return FAIL;
758 }
759 oldwin_height = oldwin->w_height;
760 if (need_status)
761 {
762 oldwin->w_status_height = STATUS_HEIGHT;
763 oldwin_height -= STATUS_HEIGHT;
764 }
765 if (new_size == 0)
766 new_size = oldwin_height / 2;
767
768 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
769 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
770 if (new_size < p_wmh)
771 new_size = p_wmh;
772
773 /* if it doesn't fit in the current window, need win_equal() */
774 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
775 do_equal = TRUE;
776
777 /* We don't like to take lines for the new window from a
778 * 'winfixheight' window. Take them from a window above or below
779 * instead, if possible. */
780 if (oldwin->w_p_wfh)
781 {
782 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
783 oldwin);
784 oldwin_height = oldwin->w_height;
785 if (need_status)
786 oldwin_height -= STATUS_HEIGHT;
787 }
788 }
789
790 /*
791 * allocate new window structure and link it in the window list
792 */
793 if ((flags & WSP_TOP) == 0
794 && ((flags & WSP_BOT)
795 || (flags & WSP_BELOW)
796 || (!(flags & WSP_ABOVE)
797 && (
798#ifdef FEAT_VERTSPLIT
799 (flags & WSP_VERT) ? p_spr :
800#endif
801 p_sb))))
802 {
803 /* new window below/right of current one */
804 if (newwin == NULL)
805 wp = win_alloc(oldwin);
806 else
807 win_append(oldwin, wp);
808 }
809 else
810 {
811 if (newwin == NULL)
812 wp = win_alloc(oldwin->w_prev);
813 else
814 win_append(oldwin->w_prev, wp);
815 }
816
817 if (newwin == NULL)
818 {
819 if (wp == NULL)
820 return FAIL;
821
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000822 /* make the contents of the new window the same as the current one */
823 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825
826 /*
827 * Reorganise the tree of frames to insert the new window.
828 */
829 if (flags & (WSP_TOP | WSP_BOT))
830 {
831#ifdef FEAT_VERTSPLIT
832 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
833 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
834#else
835 if (topframe->fr_layout == FR_COL)
836#endif
837 {
838 curfrp = topframe->fr_child;
839 if (flags & WSP_BOT)
840 while (curfrp->fr_next != NULL)
841 curfrp = curfrp->fr_next;
842 }
843 else
844 curfrp = topframe;
845 before = (flags & WSP_TOP);
846 }
847 else
848 {
849 curfrp = oldwin->w_frame;
850 if (flags & WSP_BELOW)
851 before = FALSE;
852 else if (flags & WSP_ABOVE)
853 before = TRUE;
854 else
855#ifdef FEAT_VERTSPLIT
856 if (flags & WSP_VERT)
857 before = !p_spr;
858 else
859#endif
860 before = !p_sb;
861 }
862 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
863 {
864 /* Need to create a new frame in the tree to make a branch. */
865 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
866 *frp = *curfrp;
867 curfrp->fr_layout = layout;
868 frp->fr_parent = curfrp;
869 frp->fr_next = NULL;
870 frp->fr_prev = NULL;
871 curfrp->fr_child = frp;
872 curfrp->fr_win = NULL;
873 curfrp = frp;
874 if (frp->fr_win != NULL)
875 oldwin->w_frame = frp;
876 else
877 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
878 frp->fr_parent = curfrp;
879 }
880
881 if (newwin == NULL)
882 {
883 /* Create a frame for the new window. */
884 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
885 frp->fr_layout = FR_LEAF;
886 frp->fr_win = wp;
887 wp->w_frame = frp;
888 }
889 else
890 frp = newwin->w_frame;
891 frp->fr_parent = curfrp->fr_parent;
892
893 /* Insert the new frame at the right place in the frame list. */
894 if (before)
895 frame_insert(curfrp, frp);
896 else
897 frame_append(curfrp, frp);
898
899#ifdef FEAT_VERTSPLIT
900 if (flags & WSP_VERT)
901 {
902 wp->w_p_scr = curwin->w_p_scr;
903 if (need_status)
904 {
905 --oldwin->w_height;
906 oldwin->w_status_height = need_status;
907 }
908 if (flags & (WSP_TOP | WSP_BOT))
909 {
910 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000911 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 wp->w_height = curfrp->fr_height - (p_ls > 0);
913 wp->w_status_height = (p_ls > 0);
914 }
915 else
916 {
917 /* height and row of new window is same as current window */
918 wp->w_winrow = oldwin->w_winrow;
919 wp->w_height = oldwin->w_height;
920 wp->w_status_height = oldwin->w_status_height;
921 }
922 frp->fr_height = curfrp->fr_height;
923
924 /* "new_size" of the current window goes to the new window, use
925 * one column for the vertical separator */
926 wp->w_width = new_size;
927 if (before)
928 wp->w_vsep_width = 1;
929 else
930 {
931 wp->w_vsep_width = oldwin->w_vsep_width;
932 oldwin->w_vsep_width = 1;
933 }
934 if (flags & (WSP_TOP | WSP_BOT))
935 {
936 if (flags & WSP_BOT)
937 frame_add_vsep(curfrp);
938 /* Set width of neighbor frame */
939 frame_new_width(curfrp, curfrp->fr_width
940 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP);
941 }
942 else
943 oldwin->w_width -= new_size + 1;
944 if (before) /* new window left of current one */
945 {
946 wp->w_wincol = oldwin->w_wincol;
947 oldwin->w_wincol += new_size + 1;
948 }
949 else /* new window right of current one */
950 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
951 frame_fix_width(oldwin);
952 frame_fix_width(wp);
953 }
954 else
955#endif
956 {
957 /* width and column of new window is same as current window */
958#ifdef FEAT_VERTSPLIT
959 if (flags & (WSP_TOP | WSP_BOT))
960 {
961 wp->w_wincol = 0;
962 wp->w_width = Columns;
963 wp->w_vsep_width = 0;
964 }
965 else
966 {
967 wp->w_wincol = oldwin->w_wincol;
968 wp->w_width = oldwin->w_width;
969 wp->w_vsep_width = oldwin->w_vsep_width;
970 }
971 frp->fr_width = curfrp->fr_width;
972#endif
973
974 /* "new_size" of the current window goes to the new window, use
975 * one row for the status line */
976 win_new_height(wp, new_size);
977 if (flags & (WSP_TOP | WSP_BOT))
978 frame_new_height(curfrp, curfrp->fr_height
979 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
980 else
981 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
982 if (before) /* new window above current one */
983 {
984 wp->w_winrow = oldwin->w_winrow;
985 wp->w_status_height = STATUS_HEIGHT;
986 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
987 }
988 else /* new window below current one */
989 {
990 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
991 wp->w_status_height = oldwin->w_status_height;
992 oldwin->w_status_height = STATUS_HEIGHT;
993 }
994#ifdef FEAT_VERTSPLIT
995 if (flags & WSP_BOT)
996 frame_add_statusline(curfrp);
997#endif
998 frame_fix_height(wp);
999 frame_fix_height(oldwin);
1000 }
1001
1002 if (flags & (WSP_TOP | WSP_BOT))
1003 (void)win_comp_pos();
1004
1005 /*
1006 * Both windows need redrawing
1007 */
1008 redraw_win_later(wp, NOT_VALID);
1009 wp->w_redr_status = TRUE;
1010 redraw_win_later(oldwin, NOT_VALID);
1011 oldwin->w_redr_status = TRUE;
1012
1013 if (need_status)
1014 {
1015 msg_row = Rows - 1;
1016 msg_col = sc_col;
1017 msg_clr_eos_force(); /* Old command/ruler may still be there */
1018 comp_col();
1019 msg_row = Rows - 1;
1020 msg_col = 0; /* put position back at start of line */
1021 }
1022
1023 /*
1024 * make the new window the current window and redraw
1025 */
1026 if (do_equal || dir != 0)
1027 win_equal(wp, TRUE,
1028#ifdef FEAT_VERTSPLIT
1029 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1030 : dir == 'h' ? 'b' :
1031#endif
1032 'v');
1033
1034 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1035 * size was given. */
1036#ifdef FEAT_VERTSPLIT
1037 if (flags & WSP_VERT)
1038 {
1039 i = p_wiw;
1040 if (size != 0)
1041 p_wiw = size;
1042
1043# ifdef FEAT_GUI
1044 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1045 if (gui.in_use)
1046 gui_init_which_components(NULL);
1047# endif
1048 }
1049 else
1050#endif
1051 {
1052 i = p_wh;
1053 if (size != 0)
1054 p_wh = size;
1055 }
1056 win_enter(wp, FALSE);
1057#ifdef FEAT_VERTSPLIT
1058 if (flags & WSP_VERT)
1059 p_wiw = i;
1060 else
1061#endif
1062 p_wh = i;
1063
1064 return OK;
1065}
1066
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001067/*
1068 * Initialize window "newp" from window "oldp".
1069 * Used when splitting a window and when creating a new tab page.
1070 * The windows will both edit the same buffer.
1071 */
1072 static void
1073win_init(newp, oldp)
1074 win_T *newp;
1075 win_T *oldp;
1076{
1077 int i;
1078
1079 newp->w_buffer = oldp->w_buffer;
1080 oldp->w_buffer->b_nwindows++;
1081 newp->w_cursor = oldp->w_cursor;
1082 newp->w_valid = 0;
1083 newp->w_curswant = oldp->w_curswant;
1084 newp->w_set_curswant = oldp->w_set_curswant;
1085 newp->w_topline = oldp->w_topline;
1086#ifdef FEAT_DIFF
1087 newp->w_topfill = oldp->w_topfill;
1088#endif
1089 newp->w_leftcol = oldp->w_leftcol;
1090 newp->w_pcmark = oldp->w_pcmark;
1091 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1092 newp->w_alt_fnum = oldp->w_alt_fnum;
1093 newp->w_fraction = oldp->w_fraction;
1094 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1095#ifdef FEAT_JUMPLIST
1096 copy_jumplist(oldp, newp);
1097#endif
1098#ifdef FEAT_QUICKFIX
1099 copy_loclist(oldp, newp);
1100#endif
1101 if (oldp->w_localdir != NULL)
1102 newp->w_localdir = vim_strsave(oldp->w_localdir);
1103
1104 /* Use the same argument list. */
1105 newp->w_alist = oldp->w_alist;
1106 ++newp->w_alist->al_refcount;
1107 newp->w_arg_idx = oldp->w_arg_idx;
1108
1109 /*
1110 * copy tagstack and options from existing window
1111 */
1112 for (i = 0; i < oldp->w_tagstacklen; i++)
1113 {
1114 newp->w_tagstack[i] = oldp->w_tagstack[i];
1115 if (newp->w_tagstack[i].tagname != NULL)
1116 newp->w_tagstack[i].tagname =
1117 vim_strsave(newp->w_tagstack[i].tagname);
1118 }
1119 newp->w_tagstackidx = oldp->w_tagstackidx;
1120 newp->w_tagstacklen = oldp->w_tagstacklen;
1121 win_copy_options(oldp, newp);
1122# ifdef FEAT_FOLDING
1123 copyFoldingState(oldp, newp);
1124# endif
1125}
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#endif /* FEAT_WINDOWS */
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#if defined(FEAT_WINDOWS) || defined(PROTO)
1130/*
1131 * Check if "win" is a pointer to an existing window.
1132 */
1133 int
1134win_valid(win)
1135 win_T *win;
1136{
1137 win_T *wp;
1138
1139 if (win == NULL)
1140 return FALSE;
1141 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1142 if (wp == win)
1143 return TRUE;
1144 return FALSE;
1145}
1146
1147/*
1148 * Return the number of windows.
1149 */
1150 int
1151win_count()
1152{
1153 win_T *wp;
1154 int count = 0;
1155
1156 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1157 ++count;
1158 return count;
1159}
1160
1161/*
1162 * Make "count" windows on the screen.
1163 * Return actual number of windows on the screen.
1164 * Must be called when there is just one window, filling the whole screen
1165 * (excluding the command line).
1166 */
1167/*ARGSUSED*/
1168 int
1169make_windows(count, vertical)
1170 int count;
1171 int vertical; /* split windows vertically if TRUE */
1172{
1173 int maxcount;
1174 int todo;
1175
1176#ifdef FEAT_VERTSPLIT
1177 if (vertical)
1178 {
1179 /* Each windows needs at least 'winminwidth' lines and a separator
1180 * column. */
1181 maxcount = (curwin->w_width + curwin->w_vsep_width
1182 - (p_wiw - p_wmw)) / (p_wmw + 1);
1183 }
1184 else
1185#endif
1186 {
1187 /* Each window needs at least 'winminheight' lines and a status line. */
1188 maxcount = (curwin->w_height + curwin->w_status_height
1189 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1190 }
1191
1192 if (maxcount < 2)
1193 maxcount = 2;
1194 if (count > maxcount)
1195 count = maxcount;
1196
1197 /*
1198 * add status line now, otherwise first window will be too big
1199 */
1200 if (count > 1)
1201 last_status(TRUE);
1202
1203#ifdef FEAT_AUTOCMD
1204 /*
1205 * Don't execute autocommands while creating the windows. Must do that
1206 * when putting the buffers in the windows.
1207 */
1208 ++autocmd_block;
1209#endif
1210
1211 /* todo is number of windows left to create */
1212 for (todo = count - 1; todo > 0; --todo)
1213#ifdef FEAT_VERTSPLIT
1214 if (vertical)
1215 {
1216 if (win_split(curwin->w_width - (curwin->w_width - todo)
1217 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1218 break;
1219 }
1220 else
1221#endif
1222 {
1223 if (win_split(curwin->w_height - (curwin->w_height - todo
1224 * STATUS_HEIGHT) / (todo + 1)
1225 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1226 break;
1227 }
1228
1229#ifdef FEAT_AUTOCMD
1230 --autocmd_block;
1231#endif
1232
1233 /* return actual number of windows */
1234 return (count - todo);
1235}
1236
1237/*
1238 * Exchange current and next window
1239 */
1240 static void
1241win_exchange(Prenum)
1242 long Prenum;
1243{
1244 frame_T *frp;
1245 frame_T *frp2;
1246 win_T *wp;
1247 win_T *wp2;
1248 int temp;
1249
1250 if (lastwin == firstwin) /* just one window */
1251 {
1252 beep_flush();
1253 return;
1254 }
1255
1256#ifdef FEAT_GUI
1257 need_mouse_correct = TRUE;
1258#endif
1259
1260 /*
1261 * find window to exchange with
1262 */
1263 if (Prenum)
1264 {
1265 frp = curwin->w_frame->fr_parent->fr_child;
1266 while (frp != NULL && --Prenum > 0)
1267 frp = frp->fr_next;
1268 }
1269 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1270 frp = curwin->w_frame->fr_next;
1271 else /* Swap last window in row/col with previous */
1272 frp = curwin->w_frame->fr_prev;
1273
1274 /* We can only exchange a window with another window, not with a frame
1275 * containing windows. */
1276 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1277 return;
1278 wp = frp->fr_win;
1279
1280/*
1281 * 1. remove curwin from the list. Remember after which window it was in wp2
1282 * 2. insert curwin before wp in the list
1283 * if wp != wp2
1284 * 3. remove wp from the list
1285 * 4. insert wp after wp2
1286 * 5. exchange the status line height and vsep width.
1287 */
1288 wp2 = curwin->w_prev;
1289 frp2 = curwin->w_frame->fr_prev;
1290 if (wp->w_prev != curwin)
1291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001292 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 frame_remove(curwin->w_frame);
1294 win_append(wp->w_prev, curwin);
1295 frame_insert(frp, curwin->w_frame);
1296 }
1297 if (wp != wp2)
1298 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001299 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 frame_remove(wp->w_frame);
1301 win_append(wp2, wp);
1302 if (frp2 == NULL)
1303 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1304 else
1305 frame_append(frp2, wp->w_frame);
1306 }
1307 temp = curwin->w_status_height;
1308 curwin->w_status_height = wp->w_status_height;
1309 wp->w_status_height = temp;
1310#ifdef FEAT_VERTSPLIT
1311 temp = curwin->w_vsep_width;
1312 curwin->w_vsep_width = wp->w_vsep_width;
1313 wp->w_vsep_width = temp;
1314
1315 /* If the windows are not in the same frame, exchange the sizes to avoid
1316 * messing up the window layout. Otherwise fix the frame sizes. */
1317 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1318 {
1319 temp = curwin->w_height;
1320 curwin->w_height = wp->w_height;
1321 wp->w_height = temp;
1322 temp = curwin->w_width;
1323 curwin->w_width = wp->w_width;
1324 wp->w_width = temp;
1325 }
1326 else
1327 {
1328 frame_fix_height(curwin);
1329 frame_fix_height(wp);
1330 frame_fix_width(curwin);
1331 frame_fix_width(wp);
1332 }
1333#endif
1334
1335 (void)win_comp_pos(); /* recompute window positions */
1336
1337 win_enter(wp, TRUE);
1338 redraw_later(CLEAR);
1339}
1340
1341/*
1342 * rotate windows: if upwards TRUE the second window becomes the first one
1343 * if upwards FALSE the first window becomes the second one
1344 */
1345 static void
1346win_rotate(upwards, count)
1347 int upwards;
1348 int count;
1349{
1350 win_T *wp1;
1351 win_T *wp2;
1352 frame_T *frp;
1353 int n;
1354
1355 if (firstwin == lastwin) /* nothing to do */
1356 {
1357 beep_flush();
1358 return;
1359 }
1360
1361#ifdef FEAT_GUI
1362 need_mouse_correct = TRUE;
1363#endif
1364
1365#ifdef FEAT_VERTSPLIT
1366 /* Check if all frames in this row/col have one window. */
1367 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1368 frp = frp->fr_next)
1369 if (frp->fr_win == NULL)
1370 {
1371 EMSG(_("E443: Cannot rotate when another window is split"));
1372 return;
1373 }
1374#endif
1375
1376 while (count--)
1377 {
1378 if (upwards) /* first window becomes last window */
1379 {
1380 /* remove first window/frame from the list */
1381 frp = curwin->w_frame->fr_parent->fr_child;
1382 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001383 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 frame_remove(frp);
1385
1386 /* find last frame and append removed window/frame after it */
1387 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1388 ;
1389 win_append(frp->fr_win, wp1);
1390 frame_append(frp, wp1->w_frame);
1391
1392 wp2 = frp->fr_win; /* previously last window */
1393 }
1394 else /* last window becomes first window */
1395 {
1396 /* find last window/frame in the list and remove it */
1397 for (frp = curwin->w_frame; frp->fr_next != NULL;
1398 frp = frp->fr_next)
1399 ;
1400 wp1 = frp->fr_win;
1401 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001402 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 frame_remove(frp);
1404
1405 /* append the removed window/frame before the first in the list */
1406 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1407 frame_insert(frp->fr_parent->fr_child, frp);
1408 }
1409
1410 /* exchange status height and vsep width of old and new last window */
1411 n = wp2->w_status_height;
1412 wp2->w_status_height = wp1->w_status_height;
1413 wp1->w_status_height = n;
1414 frame_fix_height(wp1);
1415 frame_fix_height(wp2);
1416#ifdef FEAT_VERTSPLIT
1417 n = wp2->w_vsep_width;
1418 wp2->w_vsep_width = wp1->w_vsep_width;
1419 wp1->w_vsep_width = n;
1420 frame_fix_width(wp1);
1421 frame_fix_width(wp2);
1422#endif
1423
1424 /* recompute w_winrow and w_wincol for all windows */
1425 (void)win_comp_pos();
1426 }
1427
1428 redraw_later(CLEAR);
1429}
1430
1431/*
1432 * Move the current window to the very top/bottom/left/right of the screen.
1433 */
1434 static void
1435win_totop(size, flags)
1436 int size;
1437 int flags;
1438{
1439 int dir;
1440 int height = curwin->w_height;
1441
1442 if (lastwin == firstwin)
1443 {
1444 beep_flush();
1445 return;
1446 }
1447
1448 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001449 (void)winframe_remove(curwin, &dir, NULL);
1450 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 last_status(FALSE); /* may need to remove last status line */
1452 (void)win_comp_pos(); /* recompute window positions */
1453
1454 /* Split a window on the desired side and put the window there. */
1455 (void)win_split_ins(size, flags, curwin, dir);
1456 if (!(flags & WSP_VERT))
1457 {
1458 win_setheight(height);
1459 if (p_ea)
1460 win_equal(curwin, TRUE, 'v');
1461 }
1462
1463#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1464 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1465 * scrollbars. Have to update them anyway. */
1466 if (gui.in_use)
1467 {
1468 out_flush();
1469 gui_init_which_components(NULL);
1470 gui_update_scrollbars(TRUE);
1471 }
1472 need_mouse_correct = TRUE;
1473#endif
1474
1475}
1476
1477/*
1478 * Move window "win1" to below/right of "win2" and make "win1" the current
1479 * window. Only works within the same frame!
1480 */
1481 void
1482win_move_after(win1, win2)
1483 win_T *win1, *win2;
1484{
1485 int height;
1486
1487 /* check if the arguments are reasonable */
1488 if (win1 == win2)
1489 return;
1490
1491 /* check if there is something to do */
1492 if (win2->w_next != win1)
1493 {
1494 /* may need move the status line/vertical separator of the last window
1495 * */
1496 if (win1 == lastwin)
1497 {
1498 height = win1->w_prev->w_status_height;
1499 win1->w_prev->w_status_height = win1->w_status_height;
1500 win1->w_status_height = height;
1501#ifdef FEAT_VERTSPLIT
1502 win1->w_prev->w_vsep_width = 0;
1503 win1->w_vsep_width = 1;
1504#endif
1505 }
1506 else if (win2 == lastwin)
1507 {
1508 height = win1->w_status_height;
1509 win1->w_status_height = win2->w_status_height;
1510 win2->w_status_height = height;
1511#ifdef FEAT_VERTSPLIT
1512 win2->w_vsep_width = 1;
1513 win1->w_vsep_width = 0;
1514#endif
1515 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001516 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 frame_remove(win1->w_frame);
1518 win_append(win2, win1);
1519 frame_append(win2->w_frame, win1->w_frame);
1520
1521 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1522 redraw_later(NOT_VALID);
1523 }
1524 win_enter(win1, FALSE);
1525}
1526
1527/*
1528 * Make all windows the same height.
1529 * 'next_curwin' will soon be the current window, make sure it has enough
1530 * rows.
1531 */
1532 void
1533win_equal(next_curwin, current, dir)
1534 win_T *next_curwin; /* pointer to current window to be or NULL */
1535 int current; /* do only frame with current window */
1536 int dir; /* 'v' for vertically, 'h' for horizontally,
1537 'b' for both, 0 for using p_ead */
1538{
1539 if (dir == 0)
1540#ifdef FEAT_VERTSPLIT
1541 dir = *p_ead;
1542#else
1543 dir = 'b';
1544#endif
1545 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001546 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001547 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548}
1549
1550/*
1551 * Set a frame to a new position and height, spreading the available room
1552 * equally over contained frames.
1553 * The window "next_curwin" (if not NULL) should at least get the size from
1554 * 'winheight' and 'winwidth' if possible.
1555 */
1556 static void
1557win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1558 win_T *next_curwin; /* pointer to current window to be or NULL */
1559 int current; /* do only frame with current window */
1560 frame_T *topfr; /* frame to set size off */
1561 int dir; /* 'v', 'h' or 'b', see win_equal() */
1562 int col; /* horizontal position for frame */
1563 int row; /* vertical position for frame */
1564 int width; /* new width of frame */
1565 int height; /* new height of frame */
1566{
1567 int n, m;
1568 int extra_sep = 0;
1569 int wincount, totwincount = 0;
1570 frame_T *fr;
1571 int next_curwin_size = 0;
1572 int room = 0;
1573 int new_size;
1574 int has_next_curwin = 0;
1575 int hnc;
1576
1577 if (topfr->fr_layout == FR_LEAF)
1578 {
1579 /* Set the width/height of this frame.
1580 * Redraw when size or position changes */
1581 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1582#ifdef FEAT_VERTSPLIT
1583 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1584#endif
1585 )
1586 {
1587 topfr->fr_win->w_winrow = row;
1588 frame_new_height(topfr, height, FALSE, FALSE);
1589#ifdef FEAT_VERTSPLIT
1590 topfr->fr_win->w_wincol = col;
1591 frame_new_width(topfr, width, FALSE);
1592#endif
1593 redraw_all_later(CLEAR);
1594 }
1595 }
1596#ifdef FEAT_VERTSPLIT
1597 else if (topfr->fr_layout == FR_ROW)
1598 {
1599 topfr->fr_width = width;
1600 topfr->fr_height = height;
1601
1602 if (dir != 'v') /* equalize frame widths */
1603 {
1604 /* Compute the maximum number of windows horizontally in this
1605 * frame. */
1606 n = frame_minwidth(topfr, NOWIN);
1607 /* add one for the rightmost window, it doesn't have a separator */
1608 if (col + width == Columns)
1609 extra_sep = 1;
1610 else
1611 extra_sep = 0;
1612 totwincount = (n + extra_sep) / (p_wmw + 1);
1613
1614 /* Compute room available for windows other than "next_curwin" */
1615 m = frame_minwidth(topfr, next_curwin);
1616 room = width - m;
1617 if (room < 0)
1618 {
1619 next_curwin_size = p_wiw + room;
1620 room = 0;
1621 }
1622 else if (n == m) /* doesn't contain curwin */
1623 next_curwin_size = 0;
1624 else
1625 {
1626 next_curwin_size = (room + p_wiw + (totwincount - 1) * p_wmw
1627 + (totwincount - 1)) / totwincount;
1628 if (next_curwin_size > p_wiw)
1629 room -= next_curwin_size - p_wiw;
1630 else
1631 next_curwin_size = p_wiw;
1632 }
1633 if (n != m)
1634 --totwincount; /* don't count curwin */
1635 }
1636
1637 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1638 {
1639 n = m = 0;
1640 wincount = 1;
1641 if (fr->fr_next == NULL)
1642 /* last frame gets all that remains (avoid roundoff error) */
1643 new_size = width;
1644 else if (dir == 'v')
1645 new_size = fr->fr_width;
1646 else
1647 {
1648 /* Compute the maximum number of windows horiz. in "fr". */
1649 n = frame_minwidth(fr, NOWIN);
1650 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1651 / (p_wmw + 1);
1652 m = frame_minwidth(fr, next_curwin);
1653 if (n != m) /* don't count next_curwin */
1654 --wincount;
1655 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1656 / totwincount;
1657 if (n != m) /* add next_curwin size */
1658 {
1659 next_curwin_size -= p_wiw - (m - n);
1660 new_size += next_curwin_size;
1661 }
1662 }
1663
1664 /* Skip frame that is full height when splitting or closing a
1665 * window, unless equalizing all frames. */
1666 if (!current || dir != 'v' || topfr->fr_parent != NULL
1667 || (new_size != fr->fr_width)
1668 || frame_has_win(fr, next_curwin))
1669 win_equal_rec(next_curwin, current, fr, dir, col, row,
1670 new_size + n, height);
1671 col += new_size + n;
1672 width -= new_size + n;
1673 if (n != m) /* contains curwin */
1674 room -= new_size - next_curwin_size;
1675 else
1676 room -= new_size;
1677 totwincount -= wincount;
1678 }
1679 }
1680#endif
1681 else /* topfr->fr_layout == FR_COL */
1682 {
1683#ifdef FEAT_VERTSPLIT
1684 topfr->fr_width = width;
1685#endif
1686 topfr->fr_height = height;
1687
1688 if (dir != 'h') /* equalize frame heights */
1689 {
1690 /* Compute maximum number of windows vertically in this frame. */
1691 n = frame_minheight(topfr, NOWIN);
1692 /* add one for the bottom window if it doesn't have a statusline */
1693 if (row + height == cmdline_row && p_ls == 0)
1694 extra_sep = 1;
1695 else
1696 extra_sep = 0;
1697 totwincount = (n + extra_sep) / (p_wmh + 1);
1698 has_next_curwin = frame_has_win(topfr, next_curwin);
1699
1700 /*
1701 * Compute height for "next_curwin" window and room available for
1702 * other windows.
1703 * "m" is the minimal height when counting p_wh for "next_curwin".
1704 */
1705 m = frame_minheight(topfr, next_curwin);
1706 room = height - m;
1707 if (room < 0)
1708 {
1709 /* The room is less then 'winheight', use all space for the
1710 * current window. */
1711 next_curwin_size = p_wh + room;
1712 room = 0;
1713 }
1714 else
1715 {
1716 next_curwin_size = -1;
1717 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1718 {
1719 /* If 'winfixheight' set keep the window height if
1720 * possible.
1721 * Watch out for this window being the next_curwin. */
1722 if (frame_fixed_height(fr))
1723 {
1724 n = frame_minheight(fr, NOWIN);
1725 new_size = fr->fr_height;
1726 if (frame_has_win(fr, next_curwin))
1727 {
1728 room += p_wh - p_wmh;
1729 next_curwin_size = 0;
1730 if (new_size < p_wh)
1731 new_size = p_wh;
1732 }
1733 else
1734 /* These windows don't use up room. */
1735 totwincount -= (n + (fr->fr_next == NULL
1736 ? extra_sep : 0)) / (p_wmh + 1);
1737 room -= new_size - n;
1738 if (room < 0)
1739 {
1740 new_size += room;
1741 room = 0;
1742 }
1743 fr->fr_newheight = new_size;
1744 }
1745 }
1746 if (next_curwin_size == -1)
1747 {
1748 if (!has_next_curwin)
1749 next_curwin_size = 0;
1750 else if (totwincount > 1
1751 && (room + (totwincount - 2))
1752 / (totwincount - 1) > p_wh)
1753 {
1754 next_curwin_size = (room + p_wh + totwincount * p_wmh
1755 + (totwincount - 1)) / totwincount;
1756 room -= next_curwin_size - p_wh;
1757 }
1758 else
1759 next_curwin_size = p_wh;
1760 }
1761 }
1762
1763 if (has_next_curwin)
1764 --totwincount; /* don't count curwin */
1765 }
1766
1767 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1768 {
1769 n = m = 0;
1770 wincount = 1;
1771 if (fr->fr_next == NULL)
1772 /* last frame gets all that remains (avoid roundoff error) */
1773 new_size = height;
1774 else if (dir == 'h')
1775 new_size = fr->fr_height;
1776 else if (frame_fixed_height(fr))
1777 {
1778 new_size = fr->fr_newheight;
1779 wincount = 0; /* doesn't count as a sizeable window */
1780 }
1781 else
1782 {
1783 /* Compute the maximum number of windows vert. in "fr". */
1784 n = frame_minheight(fr, NOWIN);
1785 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1786 / (p_wmh + 1);
1787 m = frame_minheight(fr, next_curwin);
1788 if (has_next_curwin)
1789 hnc = frame_has_win(fr, next_curwin);
1790 else
1791 hnc = FALSE;
1792 if (hnc) /* don't count next_curwin */
1793 --wincount;
1794 if (totwincount == 0)
1795 new_size = room;
1796 else
1797 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1798 / totwincount;
1799 if (hnc) /* add next_curwin size */
1800 {
1801 next_curwin_size -= p_wh - (m - n);
1802 new_size += next_curwin_size;
1803 room -= new_size - next_curwin_size;
1804 }
1805 else
1806 room -= new_size;
1807 new_size += n;
1808 }
1809 /* Skip frame that is full width when splitting or closing a
1810 * window, unless equalizing all frames. */
1811 if (!current || dir != 'h' || topfr->fr_parent != NULL
1812 || (new_size != fr->fr_height)
1813 || frame_has_win(fr, next_curwin))
1814 win_equal_rec(next_curwin, current, fr, dir, col, row,
1815 width, new_size);
1816 row += new_size;
1817 height -= new_size;
1818 totwincount -= wincount;
1819 }
1820 }
1821}
1822
1823/*
1824 * close all windows for buffer 'buf'
1825 */
1826 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001827close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001829 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001831 win_T *wp;
1832 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001833 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
1835 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001836
1837 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001839 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001841 win_close(wp, FALSE);
1842
1843 /* Start all over, autocommands may change the window layout. */
1844 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001847 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001849
1850 /* Also check windows in other tab pages. */
1851 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1852 {
1853 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001854 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001855 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1856 if (wp->w_buffer == buf)
1857 {
1858 win_close_othertab(wp, FALSE, tp);
1859
1860 /* Start all over, the tab page may be closed and
1861 * autocommands may change the window layout. */
1862 nexttp = first_tabpage;
1863 break;
1864 }
1865 }
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001868
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001869 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001870 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871}
1872
1873/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001874 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001875 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001876 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001877 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001878last_window()
1879{
1880 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1881}
1882
1883/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00001884 * Close window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 * If "free_buf" is TRUE related buffer may be unloaded.
1886 *
1887 * called by :quit, :close, :xit, :wq and findtag()
1888 */
1889 void
1890win_close(win, free_buf)
1891 win_T *win;
1892 int free_buf;
1893{
1894 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001895 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#ifdef FEAT_AUTOCMD
1897 int other_buffer = FALSE;
1898#endif
1899 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int dir;
1901 int help_window = FALSE;
1902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001903 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 EMSG(_("E444: Cannot close last window"));
1906 return;
1907 }
1908
1909 /* When closing the help window, try restoring a snapshot after closing
1910 * the window. Otherwise clear the snapshot, it's now invalid. */
1911 if (win->w_buffer->b_help)
1912 help_window = TRUE;
1913 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001914 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916#ifdef FEAT_AUTOCMD
1917 if (win == curwin)
1918 {
1919 /*
1920 * Guess which window is going to be the new current window.
1921 * This may change because of the autocommands (sigh).
1922 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001923 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /*
1926 * Be careful: If autocommands delete the window, return now.
1927 */
1928 if (wp->w_buffer != curbuf)
1929 {
1930 other_buffer = TRUE;
1931 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001932 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 return;
1934 }
1935 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001936 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 return;
1938# ifdef FEAT_EVAL
1939 /* autocmds may abort script processing */
1940 if (aborting())
1941 return;
1942# endif
1943 }
1944#endif
1945
1946 /*
1947 * Close the link to the buffer.
1948 */
1949 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
1950 /* Autocommands may have closed the window already, or closed the only
1951 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001952 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return;
1954
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00001955 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001956 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001958 /* When closing the last window in a tab page go to another tab page. */
1959 if (wp == NULL)
1960 {
1961 tabpage_T *ptp = NULL;
1962 tabpage_T *tp;
1963 tabpage_T *atp = alt_tabpage();
1964
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001965 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001966 ptp = tp;
1967 if (tp == NULL)
1968 {
1969 EMSG2(_(e_intern2), "win_close()");
1970 return;
1971 }
1972 if (ptp == NULL)
1973 first_tabpage = tp->tp_next;
1974 else
1975 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001976 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001977
1978 /* We don't do the window resizing stuff, let enter_tabpage() take
1979 * care of entering a window in another tab page. */
1980 enter_tabpage(atp, old_curbuf);
1981 return;
1982 }
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 /* Make sure curwin isn't invalid. It can cause severe trouble when
1985 * printing an error message. For win_equal() curbuf needs to be valid
1986 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001987 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 curwin = wp;
1990#ifdef FEAT_QUICKFIX
1991 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
1992 {
1993 /*
1994 * The cursor goes to the preview or the quickfix window, try
1995 * finding another window to go to.
1996 */
1997 for (;;)
1998 {
1999 if (wp->w_next == NULL)
2000 wp = firstwin;
2001 else
2002 wp = wp->w_next;
2003 if (wp == curwin)
2004 break;
2005 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2006 {
2007 curwin = wp;
2008 break;
2009 }
2010 }
2011 }
2012#endif
2013 curbuf = curwin->w_buffer;
2014 close_curwin = TRUE;
2015 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002016 if (p_ea
2017#ifdef FEAT_VERTSPLIT
2018 && (*p_ead == 'b' || *p_ead == dir)
2019#endif
2020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 win_equal(curwin, TRUE,
2022#ifdef FEAT_VERTSPLIT
2023 dir
2024#else
2025 0
2026#endif
2027 );
2028 else
2029 win_comp_pos();
2030 if (close_curwin)
2031 {
2032 win_enter_ext(wp, FALSE, TRUE);
2033#ifdef FEAT_AUTOCMD
2034 if (other_buffer)
2035 /* careful: after this wp and win may be invalid! */
2036 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2037#endif
2038 }
2039
2040 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002041 * If last window has a status line now and we don't want one,
2042 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 */
2044 last_status(FALSE);
2045
2046 /* After closing the help window, try restoring the window layout from
2047 * before it was opened. */
2048 if (help_window)
2049 restore_snapshot(close_curwin);
2050
2051#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2052 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2053 if (gui.in_use && !win_hasvertsplit())
2054 gui_init_which_components(NULL);
2055#endif
2056
2057 redraw_all_later(NOT_VALID);
2058}
2059
2060/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002061 * Close window "win" in tab page "tp", which is not the current tab page.
2062 * This may be the last window ih that tab page and result in closing the tab,
2063 * thus "tp" may become invalid!
2064 * Called must check if buffer is hidden.
2065 */
2066 void
2067win_close_othertab(win, free_buf, tp)
2068 win_T *win;
2069 int free_buf;
2070 tabpage_T *tp;
2071{
2072 win_T *wp;
2073 int dir;
2074 tabpage_T *ptp = NULL;
2075
2076 /* Close the link to the buffer. */
2077 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2078
2079 /* Careful: Autocommands may have closed the tab page or made it the
2080 * current tab page. */
2081 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2082 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002083 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002084 return;
2085
2086 /* Autocommands may have closed the window already. */
2087 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2088 ;
2089 if (wp == NULL)
2090 return;
2091
2092 /* Free the memory used for the window. */
2093 wp = win_free_mem(win, &dir, tp);
2094
2095 /* When closing the last window in a tab page remove the tab page. */
2096 if (wp == NULL)
2097 {
2098 if (tp == first_tabpage)
2099 first_tabpage = tp->tp_next;
2100 else
2101 {
2102 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2103 ptp = ptp->tp_next)
2104 ;
2105 if (ptp == NULL)
2106 {
2107 EMSG2(_(e_intern2), "win_close_othertab()");
2108 return;
2109 }
2110 ptp->tp_next = tp->tp_next;
2111 }
2112 vim_free(tp);
2113 }
2114}
2115
2116/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002117 * Free the memory used for a window.
2118 * Returns a pointer to the window that got the freed up space.
2119 */
2120 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002121win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002122 win_T *win;
2123 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002125{
2126 frame_T *frp;
2127 win_T *wp;
2128
Bram Moolenaarea408852005-06-25 22:49:46 +00002129#ifdef FEAT_FOLDING
2130 clearFolding(win);
2131#endif
2132
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002133 /* reduce the reference count to the argument list. */
2134 alist_unlink(win->w_alist);
2135
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002136 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002137 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002138 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002139 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002140 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002141
2142 return wp;
2143}
2144
2145#if defined(EXITFREE) || defined(PROTO)
2146 void
2147win_free_all()
2148{
2149 int dummy;
2150
Bram Moolenaarf740b292006-02-16 22:11:02 +00002151# ifdef FEAT_WINDOWS
2152 while (first_tabpage->tp_next != NULL)
2153 tabpage_close(TRUE);
2154# endif
2155
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002156 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002157 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002158}
2159#endif
2160
2161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * Remove a window and its frame from the tree of frames.
2163 * Returns a pointer to the window that got the freed up space.
2164 */
2165/*ARGSUSED*/
2166 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002167winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 win_T *win;
2169 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002170 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 frame_T *frp, *frp2, *frp3;
2173 frame_T *frp_close = win->w_frame;
2174 win_T *wp;
2175 int old_height = 0;
2176
2177 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002178 * If there is only one window there is nothing to remove.
2179 */
2180 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2181 return NULL;
2182
2183 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * Remove the window from its frame.
2185 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002186 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 wp = frame2win(frp2);
2188
2189 /* Remove this frame from the list of frames. */
2190 frame_remove(frp_close);
2191
2192#ifdef FEAT_VERTSPLIT
2193 if (frp_close->fr_parent->fr_layout == FR_COL)
2194 {
2195#endif
2196 /* When 'winfixheight' is set, remember its old size and restore
2197 * it later (it's a simplistic solution...). Don't do this if the
2198 * window will occupy the full height of the screen. */
2199 if (frp2->fr_win != NULL
2200 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2201 && frp2->fr_win->w_p_wfh)
2202 old_height = frp2->fr_win->w_height;
2203 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2204 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2205 if (old_height != 0)
2206 win_setheight_win(old_height, frp2->fr_win);
2207#ifdef FEAT_VERTSPLIT
2208 *dirp = 'v';
2209 }
2210 else
2211 {
2212 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2213 frp2 == frp_close->fr_next ? TRUE : FALSE);
2214 *dirp = 'h';
2215 }
2216#endif
2217
2218 /* If rows/columns go to a window below/right its positions need to be
2219 * updated. Can only be done after the sizes have been updated. */
2220 if (frp2 == frp_close->fr_next)
2221 {
2222 int row = win->w_winrow;
2223 int col = W_WINCOL(win);
2224
2225 frame_comp_pos(frp2, &row, &col);
2226 }
2227
2228 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2229 {
2230 /* There is no other frame in this list, move its info to the parent
2231 * and remove it. */
2232 frp2->fr_parent->fr_layout = frp2->fr_layout;
2233 frp2->fr_parent->fr_child = frp2->fr_child;
2234 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2235 frp->fr_parent = frp2->fr_parent;
2236 frp2->fr_parent->fr_win = frp2->fr_win;
2237 if (frp2->fr_win != NULL)
2238 frp2->fr_win->w_frame = frp2->fr_parent;
2239 frp = frp2->fr_parent;
2240 vim_free(frp2);
2241
2242 frp2 = frp->fr_parent;
2243 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2244 {
2245 /* The frame above the parent has the same layout, have to merge
2246 * the frames into this list. */
2247 if (frp2->fr_child == frp)
2248 frp2->fr_child = frp->fr_child;
2249 frp->fr_child->fr_prev = frp->fr_prev;
2250 if (frp->fr_prev != NULL)
2251 frp->fr_prev->fr_next = frp->fr_child;
2252 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2253 {
2254 frp3->fr_parent = frp2;
2255 if (frp3->fr_next == NULL)
2256 {
2257 frp3->fr_next = frp->fr_next;
2258 if (frp->fr_next != NULL)
2259 frp->fr_next->fr_prev = frp3;
2260 break;
2261 }
2262 }
2263 vim_free(frp);
2264 }
2265 }
2266
2267 return wp;
2268}
2269
2270/*
2271 * Find out which frame is going to get the freed up space when "win" is
2272 * closed.
2273 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2274 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2275 * This makes opening a window and closing it immediately keep the same window
2276 * layout.
2277 */
2278 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002279win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002281 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 frame_T *frp;
2284 int b;
2285
Bram Moolenaarf740b292006-02-16 22:11:02 +00002286 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002287 /* Last window in this tab page, will go to next tab page. */
2288 return alt_tabpage()->tp_curwin->w_frame;
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 frp = win->w_frame;
2291#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002292 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 b = p_spr;
2294 else
2295#endif
2296 b = p_sb;
2297 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2298 return frp->fr_next;
2299 return frp->fr_prev;
2300}
2301
2302/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002303 * Return the tabpage that will be used if the current one is closed.
2304 */
2305 static tabpage_T *
2306alt_tabpage()
2307{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002308 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002309
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002310 /* Use the next tab page if possible. */
2311 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002312 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002314 /* Find the last but one tab page. */
2315 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2316 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002317 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002318}
2319
2320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * Find the left-upper window in frame "frp".
2322 */
2323 static win_T *
2324frame2win(frp)
2325 frame_T *frp;
2326{
2327 while (frp->fr_win == NULL)
2328 frp = frp->fr_child;
2329 return frp->fr_win;
2330}
2331
2332/*
2333 * Return TRUE if frame "frp" contains window "wp".
2334 */
2335 static int
2336frame_has_win(frp, wp)
2337 frame_T *frp;
2338 win_T *wp;
2339{
2340 frame_T *p;
2341
2342 if (frp->fr_layout == FR_LEAF)
2343 return frp->fr_win == wp;
2344
2345 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2346 if (frame_has_win(p, wp))
2347 return TRUE;
2348 return FALSE;
2349}
2350
2351/*
2352 * Set a new height for a frame. Recursively sets the height for contained
2353 * frames and windows. Caller must take care of positions.
2354 */
2355 static void
2356frame_new_height(topfrp, height, topfirst, wfh)
2357 frame_T *topfrp;
2358 int height;
2359 int topfirst; /* resize topmost contained frame first */
2360 int wfh; /* obey 'winfixheight' when there is a choice;
2361 may cause the height not to be set */
2362{
2363 frame_T *frp;
2364 int extra_lines;
2365 int h;
2366
2367 if (topfrp->fr_win != NULL)
2368 {
2369 /* Simple case: just one window. */
2370 win_new_height(topfrp->fr_win,
2371 height - topfrp->fr_win->w_status_height);
2372 }
2373#ifdef FEAT_VERTSPLIT
2374 else if (topfrp->fr_layout == FR_ROW)
2375 {
2376 do
2377 {
2378 /* All frames in this row get the same new height. */
2379 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2380 {
2381 frame_new_height(frp, height, topfirst, wfh);
2382 if (frp->fr_height > height)
2383 {
2384 /* Could not fit the windows, make the whole row higher. */
2385 height = frp->fr_height;
2386 break;
2387 }
2388 }
2389 }
2390 while (frp != NULL);
2391 }
2392#endif
2393 else
2394 {
2395 /* Complicated case: Resize a column of frames. Resize the bottom
2396 * frame first, frames above that when needed. */
2397
2398 frp = topfrp->fr_child;
2399 if (wfh)
2400 /* Advance past frames with one window with 'wfh' set. */
2401 while (frame_fixed_height(frp))
2402 {
2403 frp = frp->fr_next;
2404 if (frp == NULL)
2405 return; /* no frame without 'wfh', give up */
2406 }
2407 if (!topfirst)
2408 {
2409 /* Find the bottom frame of this column */
2410 while (frp->fr_next != NULL)
2411 frp = frp->fr_next;
2412 if (wfh)
2413 /* Advance back for frames with one window with 'wfh' set. */
2414 while (frame_fixed_height(frp))
2415 frp = frp->fr_prev;
2416 }
2417
2418 extra_lines = height - topfrp->fr_height;
2419 if (extra_lines < 0)
2420 {
2421 /* reduce height of contained frames, bottom or top frame first */
2422 while (frp != NULL)
2423 {
2424 h = frame_minheight(frp, NULL);
2425 if (frp->fr_height + extra_lines < h)
2426 {
2427 extra_lines += frp->fr_height - h;
2428 frame_new_height(frp, h, topfirst, wfh);
2429 }
2430 else
2431 {
2432 frame_new_height(frp, frp->fr_height + extra_lines,
2433 topfirst, wfh);
2434 break;
2435 }
2436 if (topfirst)
2437 {
2438 do
2439 frp = frp->fr_next;
2440 while (wfh && frp != NULL && frame_fixed_height(frp));
2441 }
2442 else
2443 {
2444 do
2445 frp = frp->fr_prev;
2446 while (wfh && frp != NULL && frame_fixed_height(frp));
2447 }
2448 /* Increase "height" if we could not reduce enough frames. */
2449 if (frp == NULL)
2450 height -= extra_lines;
2451 }
2452 }
2453 else if (extra_lines > 0)
2454 {
2455 /* increase height of bottom or top frame */
2456 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2457 }
2458 }
2459 topfrp->fr_height = height;
2460}
2461
2462/*
2463 * Return TRUE if height of frame "frp" should not be changed because of
2464 * the 'winfixheight' option.
2465 */
2466 static int
2467frame_fixed_height(frp)
2468 frame_T *frp;
2469{
2470 /* frame with one window: fixed height if 'winfixheight' set. */
2471 if (frp->fr_win != NULL)
2472 return frp->fr_win->w_p_wfh;
2473
2474 if (frp->fr_layout == FR_ROW)
2475 {
2476 /* The frame is fixed height if one of the frames in the row is fixed
2477 * height. */
2478 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2479 if (frame_fixed_height(frp))
2480 return TRUE;
2481 return FALSE;
2482 }
2483
2484 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2485 * frames in the row are fixed height. */
2486 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2487 if (!frame_fixed_height(frp))
2488 return FALSE;
2489 return TRUE;
2490}
2491
2492#ifdef FEAT_VERTSPLIT
2493/*
2494 * Add a status line to windows at the bottom of "frp".
2495 * Note: Does not check if there is room!
2496 */
2497 static void
2498frame_add_statusline(frp)
2499 frame_T *frp;
2500{
2501 win_T *wp;
2502
2503 if (frp->fr_layout == FR_LEAF)
2504 {
2505 wp = frp->fr_win;
2506 if (wp->w_status_height == 0)
2507 {
2508 if (wp->w_height > 0) /* don't make it negative */
2509 --wp->w_height;
2510 wp->w_status_height = STATUS_HEIGHT;
2511 }
2512 }
2513 else if (frp->fr_layout == FR_ROW)
2514 {
2515 /* Handle all the frames in the row. */
2516 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2517 frame_add_statusline(frp);
2518 }
2519 else /* frp->fr_layout == FR_COL */
2520 {
2521 /* Only need to handle the last frame in the column. */
2522 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2523 ;
2524 frame_add_statusline(frp);
2525 }
2526}
2527
2528/*
2529 * Set width of a frame. Handles recursively going through contained frames.
2530 * May remove separator line for windows at the right side (for win_close()).
2531 */
2532 static void
2533frame_new_width(topfrp, width, leftfirst)
2534 frame_T *topfrp;
2535 int width;
2536 int leftfirst; /* resize leftmost contained frame first */
2537{
2538 frame_T *frp;
2539 int extra_cols;
2540 int w;
2541 win_T *wp;
2542
2543 if (topfrp->fr_layout == FR_LEAF)
2544 {
2545 /* Simple case: just one window. */
2546 wp = topfrp->fr_win;
2547 /* Find out if there are any windows right of this one. */
2548 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2549 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2550 break;
2551 if (frp->fr_parent == NULL)
2552 wp->w_vsep_width = 0;
2553 win_new_width(wp, width - wp->w_vsep_width);
2554 }
2555 else if (topfrp->fr_layout == FR_COL)
2556 {
2557 /* All frames in this column get the same new width. */
2558 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2559 frame_new_width(frp, width, leftfirst);
2560 }
2561 else /* fr_layout == FR_ROW */
2562 {
2563 /* Complicated case: Resize a row of frames. Resize the rightmost
2564 * frame first, frames left of it when needed. */
2565
2566 /* Find the rightmost frame of this row */
2567 frp = topfrp->fr_child;
2568 if (!leftfirst)
2569 while (frp->fr_next != NULL)
2570 frp = frp->fr_next;
2571
2572 extra_cols = width - topfrp->fr_width;
2573 if (extra_cols < 0)
2574 {
2575 /* reduce frame width, rightmost frame first */
2576 while (frp != NULL)
2577 {
2578 w = frame_minwidth(frp, NULL);
2579 if (frp->fr_width + extra_cols < w)
2580 {
2581 extra_cols += frp->fr_width - w;
2582 frame_new_width(frp, w, leftfirst);
2583 }
2584 else
2585 {
2586 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2587 break;
2588 }
2589 if (leftfirst)
2590 frp = frp->fr_next;
2591 else
2592 frp = frp->fr_prev;
2593 }
2594 }
2595 else if (extra_cols > 0)
2596 {
2597 /* increase width of rightmost frame */
2598 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst);
2599 }
2600 }
2601 topfrp->fr_width = width;
2602}
2603
2604/*
2605 * Add the vertical separator to windows at the right side of "frp".
2606 * Note: Does not check if there is room!
2607 */
2608 static void
2609frame_add_vsep(frp)
2610 frame_T *frp;
2611{
2612 win_T *wp;
2613
2614 if (frp->fr_layout == FR_LEAF)
2615 {
2616 wp = frp->fr_win;
2617 if (wp->w_vsep_width == 0)
2618 {
2619 if (wp->w_width > 0) /* don't make it negative */
2620 --wp->w_width;
2621 wp->w_vsep_width = 1;
2622 }
2623 }
2624 else if (frp->fr_layout == FR_COL)
2625 {
2626 /* Handle all the frames in the column. */
2627 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2628 frame_add_vsep(frp);
2629 }
2630 else /* frp->fr_layout == FR_ROW */
2631 {
2632 /* Only need to handle the last frame in the row. */
2633 frp = frp->fr_child;
2634 while (frp->fr_next != NULL)
2635 frp = frp->fr_next;
2636 frame_add_vsep(frp);
2637 }
2638}
2639
2640/*
2641 * Set frame width from the window it contains.
2642 */
2643 static void
2644frame_fix_width(wp)
2645 win_T *wp;
2646{
2647 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2648}
2649#endif
2650
2651/*
2652 * Set frame height from the window it contains.
2653 */
2654 static void
2655frame_fix_height(wp)
2656 win_T *wp;
2657{
2658 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2659}
2660
2661/*
2662 * Compute the minimal height for frame "topfrp".
2663 * Uses the 'winminheight' option.
2664 * When "next_curwin" isn't NULL, use p_wh for this window.
2665 * When "next_curwin" is NOWIN, don't use at least one line for the current
2666 * window.
2667 */
2668 static int
2669frame_minheight(topfrp, next_curwin)
2670 frame_T *topfrp;
2671 win_T *next_curwin;
2672{
2673 frame_T *frp;
2674 int m;
2675#ifdef FEAT_VERTSPLIT
2676 int n;
2677#endif
2678
2679 if (topfrp->fr_win != NULL)
2680 {
2681 if (topfrp->fr_win == next_curwin)
2682 m = p_wh + topfrp->fr_win->w_status_height;
2683 else
2684 {
2685 /* window: minimal height of the window plus status line */
2686 m = p_wmh + topfrp->fr_win->w_status_height;
2687 /* Current window is minimal one line high */
2688 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2689 ++m;
2690 }
2691 }
2692#ifdef FEAT_VERTSPLIT
2693 else if (topfrp->fr_layout == FR_ROW)
2694 {
2695 /* get the minimal height from each frame in this row */
2696 m = 0;
2697 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2698 {
2699 n = frame_minheight(frp, next_curwin);
2700 if (n > m)
2701 m = n;
2702 }
2703 }
2704#endif
2705 else
2706 {
2707 /* Add up the minimal heights for all frames in this column. */
2708 m = 0;
2709 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2710 m += frame_minheight(frp, next_curwin);
2711 }
2712
2713 return m;
2714}
2715
2716#ifdef FEAT_VERTSPLIT
2717/*
2718 * Compute the minimal width for frame "topfrp".
2719 * When "next_curwin" isn't NULL, use p_wiw for this window.
2720 * When "next_curwin" is NOWIN, don't use at least one column for the current
2721 * window.
2722 */
2723 static int
2724frame_minwidth(topfrp, next_curwin)
2725 frame_T *topfrp;
2726 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2727{
2728 frame_T *frp;
2729 int m, n;
2730
2731 if (topfrp->fr_win != NULL)
2732 {
2733 if (topfrp->fr_win == next_curwin)
2734 m = p_wiw + topfrp->fr_win->w_vsep_width;
2735 else
2736 {
2737 /* window: minimal width of the window plus separator column */
2738 m = p_wmw + topfrp->fr_win->w_vsep_width;
2739 /* Current window is minimal one column wide */
2740 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2741 ++m;
2742 }
2743 }
2744 else if (topfrp->fr_layout == FR_COL)
2745 {
2746 /* get the minimal width from each frame in this column */
2747 m = 0;
2748 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2749 {
2750 n = frame_minwidth(frp, next_curwin);
2751 if (n > m)
2752 m = n;
2753 }
2754 }
2755 else
2756 {
2757 /* Add up the minimal widths for all frames in this row. */
2758 m = 0;
2759 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2760 m += frame_minwidth(frp, next_curwin);
2761 }
2762
2763 return m;
2764}
2765#endif
2766
2767
2768/*
2769 * Try to close all windows except current one.
2770 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2771 * used and the buffer was modified.
2772 *
2773 * Used by ":bdel" and ":only".
2774 */
2775 void
2776close_others(message, forceit)
2777 int message;
2778 int forceit; /* always hide all other windows */
2779{
2780 win_T *wp;
2781 win_T *nextwp;
2782 int r;
2783
2784 if (lastwin == firstwin)
2785 {
2786 if (message
2787#ifdef FEAT_AUTOCMD
2788 && !autocmd_busy
2789#endif
2790 )
2791 MSG(_("Already only one window"));
2792 return;
2793 }
2794
2795 /* Be very careful here: autocommands may change the window layout. */
2796 for (wp = firstwin; win_valid(wp); wp = nextwp)
2797 {
2798 nextwp = wp->w_next;
2799 if (wp != curwin) /* don't close current window */
2800 {
2801
2802 /* Check if it's allowed to abandon this window */
2803 r = can_abandon(wp->w_buffer, forceit);
2804#ifdef FEAT_AUTOCMD
2805 if (!win_valid(wp)) /* autocommands messed wp up */
2806 {
2807 nextwp = firstwin;
2808 continue;
2809 }
2810#endif
2811 if (!r)
2812 {
2813#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2814 if (message && (p_confirm || cmdmod.confirm) && p_write)
2815 {
2816 dialog_changed(wp->w_buffer, FALSE);
2817# ifdef FEAT_AUTOCMD
2818 if (!win_valid(wp)) /* autocommands messed wp up */
2819 {
2820 nextwp = firstwin;
2821 continue;
2822 }
2823# endif
2824 }
2825 if (bufIsChanged(wp->w_buffer))
2826#endif
2827 continue;
2828 }
2829 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2830 }
2831 }
2832
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002833 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 EMSG(_("E445: Other window contains changes"));
2835}
2836
2837#endif /* FEAT_WINDOWS */
2838
2839/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002840 * Init the current window "curwin".
2841 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 */
2843 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002844curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002846 redraw_win_later(curwin, NOT_VALID);
2847 curwin->w_lines_valid = 0;
2848 curwin->w_cursor.lnum = 1;
2849 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002851 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002853 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
2854 curwin->w_pcmark.col = 0;
2855 curwin->w_prev_pcmark.lnum = 0;
2856 curwin->w_prev_pcmark.col = 0;
2857 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002859 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002861 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef FEAT_FKMAP
2863 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002864 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002866 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
2868}
2869
2870/*
2871 * Allocate the first window and put an empty buffer in it.
2872 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002873 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002875 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876win_alloc_first()
2877{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002878 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002879 return FAIL;
2880
2881#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002882 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002883 if (first_tabpage == NULL)
2884 return FAIL;
2885 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002886 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002887#endif
2888 return OK;
2889}
2890
2891/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002892 * Allocate the first window or the first window in a new tab page.
2893 * When "oldwin" is NULL create an empty buffer for it.
2894 * When "oldwin" is not NULL copy info from it to the new window (only with
2895 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002896 * Return FAIL when something goes wrong (out of memory).
2897 */
2898 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002899win_alloc_firstwin(oldwin)
2900 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002903 if (oldwin == NULL)
2904 {
2905 /* Very first window, need to create an empty buffer for it and
2906 * initialize from scratch. */
2907 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
2908 if (curwin == NULL || curbuf == NULL)
2909 return FAIL;
2910 curwin->w_buffer = curbuf;
2911 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002913 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002915 curwin_init(); /* init current window */
2916 }
2917#ifdef FEAT_WINDOWS
2918 else
2919 {
2920 /* First window in new tab page, initialize it from "oldwin". */
2921 win_init(curwin, oldwin);
2922
2923# ifdef FEAT_SCROLLBIND
2924 /* We don't want scroll-binding in the first window. */
2925 curwin->w_p_scb = FALSE;
2926# endif
2927 }
2928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
2931 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 topframe->fr_layout = FR_LEAF;
2934#ifdef FEAT_VERTSPLIT
2935 topframe->fr_width = Columns;
2936#endif
2937 topframe->fr_height = Rows - p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002938#ifdef FEAT_WINDOWS
2939 p_ch_used = p_ch;
2940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 topframe->fr_win = curwin;
2942 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002943
2944 return OK;
2945}
2946
2947/*
2948 * Initialize the window and frame size to the maximum.
2949 */
2950 void
2951win_init_size()
2952{
2953 firstwin->w_height = ROWS_AVAIL;
2954 topframe->fr_height = ROWS_AVAIL;
2955#ifdef FEAT_VERTSPLIT
2956 firstwin->w_width = Columns;
2957 topframe->fr_width = Columns;
2958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959}
2960
2961#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002962
2963/*
2964 * Allocate a new tabpage_T and init the values.
2965 * Returns NULL when out of memory.
2966 */
2967 static tabpage_T *
2968alloc_tabpage()
2969{
2970 tabpage_T *tp;
2971
2972 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
2973 if (tp != NULL)
2974 {
2975# ifdef FEAT_DIFF
2976 tp->tp_diff_invalid = TRUE;
2977# endif
2978 }
2979 return tp;
2980}
2981
2982 static void
2983free_tabpage(tp)
2984 tabpage_T *tp;
2985{
2986# ifdef FEAT_DIFF
2987 diff_clear(tp);
2988# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002989 clear_snapshot(tp);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002990 vim_free(tp);
2991}
2992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002993/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002994 * Create a new Tab page with one window.
2995 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002996 * When "after" is 0 put it just after the current Tab page.
2997 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002998 * Return FAIL or OK.
2999 */
3000 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003001win_new_tabpage(after)
3002 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003003{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003004 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003005 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003006 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003007
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003008 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003009 if (newtp == NULL)
3010 return FAIL;
3011
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003012 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003013 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003014 {
3015 vim_free(newtp);
3016 return FAIL;
3017 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003018 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003019
3020 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003021 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003022 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003023 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003024 if (after == 1)
3025 {
3026 /* New tab page becomes the first one. */
3027 newtp->tp_next = first_tabpage;
3028 first_tabpage = newtp;
3029 }
3030 else
3031 {
3032 if (after > 0)
3033 {
3034 /* Put new tab page before tab page "after". */
3035 n = 2;
3036 for (tp = first_tabpage; tp->tp_next != NULL
3037 && n < after; tp = tp->tp_next)
3038 ++n;
3039 }
3040 newtp->tp_next = tp->tp_next;
3041 tp->tp_next = newtp;
3042 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003043 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003044 firstwin->w_winrow = tabline_height();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045
3046 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003047 last_status(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003048 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003049#ifdef FEAT_AUTOCMD
3050 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3051 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3052#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003053 return OK;
3054 }
3055
3056 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003057 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003058 return FAIL;
3059}
3060
3061/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003062 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3063 * like with ":split".
3064 * Returns OK if a new tab page was created, FAIL otherwise.
3065 */
3066 int
3067may_open_tabpage()
3068{
3069 int n = cmdmod.tab;
3070
3071 if (cmdmod.tab != 0)
3072 {
3073 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3074 return win_new_tabpage(n);
3075 }
3076 return FAIL;
3077}
3078
3079/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003080 * Create up to "maxcount" tabpages with empty windows.
3081 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003083 int
3084make_tabpages(maxcount)
3085 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003086{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003087 int count = maxcount;
3088 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003089
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003090 /* Limit to 10 tabs. */
3091 if (count > 10)
3092 count = 10;
3093
3094#ifdef FEAT_AUTOCMD
3095 /*
3096 * Don't execute autocommands while creating the tab pages. Must do that
3097 * when putting the buffers in the windows.
3098 */
3099 ++autocmd_block;
3100#endif
3101
3102 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003103 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003104 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003105
3106#ifdef FEAT_AUTOCMD
3107 --autocmd_block;
3108#endif
3109
3110 /* return actual number of tab pages */
3111 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003112}
3113
3114/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003115 * Return TRUE when "tpc" points to a valid tab page.
3116 */
3117 int
3118valid_tabpage(tpc)
3119 tabpage_T *tpc;
3120{
3121 tabpage_T *tp;
3122
3123 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3124 if (tp == tpc)
3125 return TRUE;
3126 return FALSE;
3127}
3128
3129/*
3130 * Find tab page "n" (first one is 1). Returns NULL when not found.
3131 */
3132 tabpage_T *
3133find_tabpage(n)
3134 int n;
3135{
3136 tabpage_T *tp;
3137 int i = 1;
3138
3139 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3140 ++i;
3141 return tp;
3142}
3143
3144/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003145 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003146 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003147 */
3148 int
3149tabpage_index(ftp)
3150 tabpage_T *ftp;
3151{
3152 int i = 1;
3153 tabpage_T *tp;
3154
3155 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3156 ++i;
3157 return i;
3158}
3159
3160/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003161 * Prepare for leaving the current tab page.
3162 * When autocomands change "curtab" we don't leave the tab page and return
3163 * FAIL.
3164 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003165 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003166/*ARGSUSED*/
3167 static int
3168leave_tabpage(new_curbuf)
3169 buf_T *new_curbuf; /* what is going to be the new curbuf,
3170 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003171{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003172 tabpage_T *tp = curtab;
3173
3174#ifdef FEAT_AUTOCMD
3175 if (new_curbuf != curbuf)
3176 {
3177 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3178 if (curtab != tp)
3179 return FAIL;
3180 }
3181 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3182 if (curtab != tp)
3183 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003184 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003185 if (curtab != tp)
3186 return FAIL;
3187#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003188#if defined(FEAT_GUI)
3189 /* Remove the scrollbars. They may be added back later. */
3190 if (gui.in_use)
3191 gui_remove_scrollbars();
3192#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003193 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003194 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003195 tp->tp_firstwin = firstwin;
3196 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003197 tp->tp_old_Rows = Rows;
3198 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003199 firstwin = NULL;
3200 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003201 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003202}
3203
3204/*
3205 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003206 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207 */
3208/*ARGSUSED*/
3209 static void
3210enter_tabpage(tp, old_curbuf)
3211 tabpage_T *tp;
3212 buf_T *old_curbuf;
3213{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003214 int old_off = tp->tp_firstwin->w_winrow;
3215
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003216 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003217 firstwin = tp->tp_firstwin;
3218 lastwin = tp->tp_lastwin;
3219 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003220#ifdef FEAT_AUTOCMD
3221 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3222#endif
3223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003224 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003225 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003226
3227#ifdef FEAT_AUTOCMD
3228 if (old_curbuf != curbuf)
3229 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3230#endif
3231
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003232 last_status(FALSE); /* status line may appear or disappear */
3233 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003234 must_redraw = CLEAR; /* need to redraw everything */
3235#ifdef FEAT_DIFF
3236 diff_need_scrollbind = TRUE;
3237#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003238
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003239 /* The tabpage line may have appeared or disappeared, may need to resize
3240 * the frames for that. When the Vim window was resized need to update
3241 * frame sizes too. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3243#ifdef FEAT_GUI_TABLINE
3244 && !gui_use_tabline()
3245#endif
3246 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003247 shell_new_rows();
3248#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003249 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003250 shell_new_columns(); /* update window widths */
3251#endif
3252
3253#if defined(FEAT_GUI)
3254 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3255 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003256 if (gui.in_use && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003257 gui_update_scrollbars(TRUE);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003258 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003259#endif
3260
3261 redraw_all_later(CLEAR);
3262}
3263
3264/*
3265 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003266 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003267 */
3268 void
3269goto_tabpage(n)
3270 int n;
3271{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003273 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003274 int i;
3275
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003276 /* If there is only one it can't work. */
3277 if (first_tabpage->tp_next == NULL)
3278 {
3279 if (n > 1)
3280 beep_flush();
3281 return;
3282 }
3283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003284 if (n == 0)
3285 {
3286 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003287 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003288 tp = first_tabpage;
3289 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003290 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003291 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003292 else if (n < 0)
3293 {
3294 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3295 * this N times. */
3296 ttp = curtab;
3297 for (i = n; i < 0; ++i)
3298 {
3299 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3300 tp = tp->tp_next)
3301 ;
3302 ttp = tp;
3303 }
3304 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003305 else if (n == 9999)
3306 {
3307 /* Go to last tab page. */
3308 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3309 ;
3310 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003311 else
3312 {
3313 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003314 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003315 if (tp == NULL)
3316 {
3317 beep_flush();
3318 return;
3319 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320 }
3321
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003322 goto_tabpage_tp(tp);
3323
3324#ifdef FEAT_GUI_TABLINE
3325 if (gui_use_tabline())
3326 gui_mch_set_curtab(tabpage_index(tp));
3327#endif
3328}
3329
3330/*
3331 * Go to tabpage "tp".
3332 * Note: doesn't update the GUI tab.
3333 */
3334 void
3335goto_tabpage_tp(tp)
3336 tabpage_T *tp;
3337{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003338 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003339 {
3340 if (valid_tabpage(tp))
3341 enter_tabpage(tp, curbuf);
3342 else
3343 enter_tabpage(curtab, curbuf);
3344 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
3347/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003348 * Move the current tab page to before tab page "nr".
3349 */
3350 void
3351tabpage_move(nr)
3352 int nr;
3353{
3354 int n = nr;
3355 tabpage_T *tp;
3356
3357 if (first_tabpage->tp_next == NULL)
3358 return;
3359
3360 /* Remove the current tab page from the list of tab pages. */
3361 if (curtab == first_tabpage)
3362 first_tabpage = curtab->tp_next;
3363 else
3364 {
3365 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3366 if (tp->tp_next == curtab)
3367 break;
3368 if (tp == NULL) /* "cannot happen" */
3369 return;
3370 tp->tp_next = curtab->tp_next;
3371 }
3372
3373 /* Re-insert it at the specified position. */
3374 if (n == 0)
3375 {
3376 curtab->tp_next = first_tabpage;
3377 first_tabpage = curtab;
3378 }
3379 else
3380 {
3381 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3382 --n;
3383 curtab->tp_next = tp->tp_next;
3384 tp->tp_next = curtab;
3385 }
3386
3387 /* Need to redraw the tabline. Tab page contents doesn't change. */
3388 redraw_tabline = TRUE;
3389}
3390
3391
3392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * Go to another window.
3394 * When jumping to another buffer, stop Visual mode. Do this before
3395 * changing windows so we can yank the selection into the '*' register.
3396 * When jumping to another window on the same buffer, adjust its cursor
3397 * position to keep the same Visual area.
3398 */
3399 void
3400win_goto(wp)
3401 win_T *wp;
3402{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003403 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003406 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 return;
3408 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_VISUAL
3411 if (wp->w_buffer != curbuf)
3412 reset_VIsual_and_resel();
3413 else if (VIsual_active)
3414 wp->w_cursor = curwin->w_cursor;
3415#endif
3416
3417#ifdef FEAT_GUI
3418 need_mouse_correct = TRUE;
3419#endif
3420 win_enter(wp, TRUE);
3421}
3422
3423#if defined(FEAT_PERL) || defined(PROTO)
3424/*
3425 * Find window number "winnr" (counting top to bottom).
3426 */
3427 win_T *
3428win_find_nr(winnr)
3429 int winnr;
3430{
3431 win_T *wp;
3432
3433# ifdef FEAT_WINDOWS
3434 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3435 if (--winnr == 0)
3436 break;
3437 return wp;
3438# else
3439 return curwin;
3440# endif
3441}
3442#endif
3443
3444#ifdef FEAT_VERTSPLIT
3445/*
3446 * Move to window above or below "count" times.
3447 */
3448 static void
3449win_goto_ver(up, count)
3450 int up; /* TRUE to go to win above */
3451 long count;
3452{
3453 frame_T *fr;
3454 frame_T *nfr;
3455 frame_T *foundfr;
3456
3457 foundfr = curwin->w_frame;
3458 while (count--)
3459 {
3460 /*
3461 * First go upwards in the tree of frames until we find a upwards or
3462 * downwards neighbor.
3463 */
3464 fr = foundfr;
3465 for (;;)
3466 {
3467 if (fr == topframe)
3468 goto end;
3469 if (up)
3470 nfr = fr->fr_prev;
3471 else
3472 nfr = fr->fr_next;
3473 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3474 break;
3475 fr = fr->fr_parent;
3476 }
3477
3478 /*
3479 * Now go downwards to find the bottom or top frame in it.
3480 */
3481 for (;;)
3482 {
3483 if (nfr->fr_layout == FR_LEAF)
3484 {
3485 foundfr = nfr;
3486 break;
3487 }
3488 fr = nfr->fr_child;
3489 if (nfr->fr_layout == FR_ROW)
3490 {
3491 /* Find the frame at the cursor row. */
3492 while (fr->fr_next != NULL
3493 && frame2win(fr)->w_wincol + fr->fr_width
3494 <= curwin->w_wincol + curwin->w_wcol)
3495 fr = fr->fr_next;
3496 }
3497 if (nfr->fr_layout == FR_COL && up)
3498 while (fr->fr_next != NULL)
3499 fr = fr->fr_next;
3500 nfr = fr;
3501 }
3502 }
3503end:
3504 if (foundfr != NULL)
3505 win_goto(foundfr->fr_win);
3506}
3507
3508/*
3509 * Move to left or right window.
3510 */
3511 static void
3512win_goto_hor(left, count)
3513 int left; /* TRUE to go to left win */
3514 long count;
3515{
3516 frame_T *fr;
3517 frame_T *nfr;
3518 frame_T *foundfr;
3519
3520 foundfr = curwin->w_frame;
3521 while (count--)
3522 {
3523 /*
3524 * First go upwards in the tree of frames until we find a left or
3525 * right neighbor.
3526 */
3527 fr = foundfr;
3528 for (;;)
3529 {
3530 if (fr == topframe)
3531 goto end;
3532 if (left)
3533 nfr = fr->fr_prev;
3534 else
3535 nfr = fr->fr_next;
3536 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3537 break;
3538 fr = fr->fr_parent;
3539 }
3540
3541 /*
3542 * Now go downwards to find the leftmost or rightmost frame in it.
3543 */
3544 for (;;)
3545 {
3546 if (nfr->fr_layout == FR_LEAF)
3547 {
3548 foundfr = nfr;
3549 break;
3550 }
3551 fr = nfr->fr_child;
3552 if (nfr->fr_layout == FR_COL)
3553 {
3554 /* Find the frame at the cursor row. */
3555 while (fr->fr_next != NULL
3556 && frame2win(fr)->w_winrow + fr->fr_height
3557 <= curwin->w_winrow + curwin->w_wrow)
3558 fr = fr->fr_next;
3559 }
3560 if (nfr->fr_layout == FR_ROW && left)
3561 while (fr->fr_next != NULL)
3562 fr = fr->fr_next;
3563 nfr = fr;
3564 }
3565 }
3566end:
3567 if (foundfr != NULL)
3568 win_goto(foundfr->fr_win);
3569}
3570#endif
3571
3572/*
3573 * Make window "wp" the current window.
3574 */
3575 void
3576win_enter(wp, undo_sync)
3577 win_T *wp;
3578 int undo_sync;
3579{
3580 win_enter_ext(wp, undo_sync, FALSE);
3581}
3582
3583/*
3584 * Make window wp the current window.
3585 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3586 * been closed and isn't valid.
3587 */
3588 static void
3589win_enter_ext(wp, undo_sync, curwin_invalid)
3590 win_T *wp;
3591 int undo_sync;
3592 int curwin_invalid;
3593{
3594#ifdef FEAT_AUTOCMD
3595 int other_buffer = FALSE;
3596#endif
3597
3598 if (wp == curwin && !curwin_invalid) /* nothing to do */
3599 return;
3600
3601#ifdef FEAT_AUTOCMD
3602 if (!curwin_invalid)
3603 {
3604 /*
3605 * Be careful: If autocommands delete the window, return now.
3606 */
3607 if (wp->w_buffer != curbuf)
3608 {
3609 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3610 other_buffer = TRUE;
3611 if (!win_valid(wp))
3612 return;
3613 }
3614 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3615 if (!win_valid(wp))
3616 return;
3617# ifdef FEAT_EVAL
3618 /* autocmds may abort script processing */
3619 if (aborting())
3620 return;
3621# endif
3622 }
3623#endif
3624
3625 /* sync undo before leaving the current buffer */
3626 if (undo_sync && curbuf != wp->w_buffer)
3627 u_sync();
3628 /* may have to copy the buffer options when 'cpo' contains 'S' */
3629 if (wp->w_buffer != curbuf)
3630 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3631 if (!curwin_invalid)
3632 {
3633 prevwin = curwin; /* remember for CTRL-W p */
3634 curwin->w_redr_status = TRUE;
3635 }
3636 curwin = wp;
3637 curbuf = wp->w_buffer;
3638 check_cursor();
3639#ifdef FEAT_VIRTUALEDIT
3640 if (!virtual_active())
3641 curwin->w_cursor.coladd = 0;
3642#endif
3643 changed_line_abv_curs(); /* assume cursor position needs updating */
3644
3645 if (curwin->w_localdir != NULL)
3646 {
3647 /* Window has a local directory: Save current directory as global
3648 * directory (unless that was done already) and change to the local
3649 * directory. */
3650 if (globaldir == NULL)
3651 {
3652 char_u cwd[MAXPATHL];
3653
3654 if (mch_dirname(cwd, MAXPATHL) == OK)
3655 globaldir = vim_strsave(cwd);
3656 }
3657 mch_chdir((char *)curwin->w_localdir);
3658 shorten_fnames(TRUE);
3659 }
3660 else if (globaldir != NULL)
3661 {
3662 /* Window doesn't have a local directory and we are not in the global
3663 * directory: Change to the global directory. */
3664 mch_chdir((char *)globaldir);
3665 vim_free(globaldir);
3666 globaldir = NULL;
3667 shorten_fnames(TRUE);
3668 }
3669
3670#ifdef FEAT_AUTOCMD
3671 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3672 if (other_buffer)
3673 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3674#endif
3675
3676#ifdef FEAT_TITLE
3677 maketitle();
3678#endif
3679 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (restart_edit)
3682 redraw_later(VALID); /* causes status line redraw */
3683
3684 /* set window height to desired minimal value */
3685 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3686 win_setheight((int)p_wh);
3687 else if (curwin->w_height == 0)
3688 win_setheight(1);
3689
3690#ifdef FEAT_VERTSPLIT
3691 /* set window width to desired minimal value */
3692 if (curwin->w_width < p_wiw)
3693 win_setwidth((int)p_wiw);
3694#endif
3695
3696#ifdef FEAT_MOUSE
3697 setmouse(); /* in case jumped to/from help buffer */
3698#endif
3699
3700#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3701 /* Change directories when the acd option is set on and after
3702 * switching windows. */
3703 if (p_acd && curbuf->b_ffname != NULL
3704 && vim_chdirfile(curbuf->b_ffname) == OK)
3705 shorten_fnames(TRUE);
3706#endif
3707}
3708
3709#endif /* FEAT_WINDOWS */
3710
3711#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3712/*
3713 * Jump to the first open window that contains buffer buf if one exists
3714 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3715 * Returns pointer to window if it exists, otherwise NULL.
3716 */
3717 win_T *
3718buf_jump_open_win(buf)
3719 buf_T *buf;
3720{
3721# ifdef FEAT_WINDOWS
3722 win_T *wp;
3723
3724 for (wp = firstwin; wp; wp = wp->w_next)
3725 if (wp->w_buffer == buf)
3726 break;
3727 if (wp != NULL)
3728 win_enter(wp, FALSE);
3729 return wp;
3730# else
3731 if (curwin->w_buffer == buf)
3732 return curwin;
3733 return NULL;
3734# endif
3735}
3736#endif
3737
3738/*
3739 * allocate a window structure and link it in the window list
3740 */
3741/*ARGSUSED*/
3742 static win_T *
3743win_alloc(after)
3744 win_T *after;
3745{
3746 win_T *newwin;
3747
3748 /*
3749 * allocate window structure and linesizes arrays
3750 */
3751 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3752 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3753 {
3754 vim_free(newwin);
3755 newwin = NULL;
3756 }
3757
3758 if (newwin != NULL)
3759 {
3760 /*
3761 * link the window in the window list
3762 */
3763#ifdef FEAT_WINDOWS
3764 win_append(after, newwin);
3765#endif
3766#ifdef FEAT_VERTSPLIT
3767 newwin->w_wincol = 0;
3768 newwin->w_width = Columns;
3769#endif
3770
3771 /* position the display and the cursor at the top of the file. */
3772 newwin->w_topline = 1;
3773#ifdef FEAT_DIFF
3774 newwin->w_topfill = 0;
3775#endif
3776 newwin->w_botline = 2;
3777 newwin->w_cursor.lnum = 1;
3778#ifdef FEAT_SCROLLBIND
3779 newwin->w_scbind_pos = 1;
3780#endif
3781
3782 /* We won't calculate w_fraction until resizing the window */
3783 newwin->w_fraction = 0;
3784 newwin->w_prev_fraction_row = -1;
3785
3786#ifdef FEAT_GUI
3787 if (gui.in_use)
3788 {
3789 out_flush();
3790 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3791 SBAR_LEFT, newwin);
3792 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3793 SBAR_RIGHT, newwin);
3794 }
3795#endif
3796#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003797 /* init w: variables */
3798 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
3800#ifdef FEAT_FOLDING
3801 foldInitWin(newwin);
3802#endif
3803 }
3804 return newwin;
3805}
3806
3807#if defined(FEAT_WINDOWS) || defined(PROTO)
3808
3809/*
3810 * remove window 'wp' from the window list and free the structure
3811 */
3812 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003813win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003815 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 int i;
3818
Bram Moolenaar325b7a22004-07-05 15:58:32 +00003819#ifdef FEAT_MZSCHEME
3820 mzscheme_window_free(wp);
3821#endif
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823#ifdef FEAT_PERL
3824 perl_win_free(wp);
3825#endif
3826
3827#ifdef FEAT_PYTHON
3828 python_window_free(wp);
3829#endif
3830
3831#ifdef FEAT_TCL
3832 tcl_window_free(wp);
3833#endif
3834
3835#ifdef FEAT_RUBY
3836 ruby_window_free(wp);
3837#endif
3838
3839 clear_winopt(&wp->w_onebuf_opt);
3840 clear_winopt(&wp->w_allbuf_opt);
3841
3842#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003843 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#endif
3845
3846 if (prevwin == wp)
3847 prevwin = NULL;
3848 win_free_lsize(wp);
3849
3850 for (i = 0; i < wp->w_tagstacklen; ++i)
3851 vim_free(wp->w_tagstack[i].tagname);
3852
3853 vim_free(wp->w_localdir);
3854#ifdef FEAT_SEARCH_EXTRA
3855 vim_free(wp->w_match.regprog);
3856#endif
3857#ifdef FEAT_JUMPLIST
3858 free_jumplist(wp);
3859#endif
3860
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003861#ifdef FEAT_QUICKFIX
3862 qf_free_all(wp);
3863#endif
3864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865#ifdef FEAT_GUI
3866 if (gui.in_use)
3867 {
3868 out_flush();
3869 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
3870 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
3871 }
3872#endif /* FEAT_GUI */
3873
Bram Moolenaarf740b292006-02-16 22:11:02 +00003874 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 vim_free(wp);
3876}
3877
3878/*
3879 * Append window "wp" in the window list after window "after".
3880 */
3881 static void
3882win_append(after, wp)
3883 win_T *after, *wp;
3884{
3885 win_T *before;
3886
3887 if (after == NULL) /* after NULL is in front of the first */
3888 before = firstwin;
3889 else
3890 before = after->w_next;
3891
3892 wp->w_next = before;
3893 wp->w_prev = after;
3894 if (after == NULL)
3895 firstwin = wp;
3896 else
3897 after->w_next = wp;
3898 if (before == NULL)
3899 lastwin = wp;
3900 else
3901 before->w_prev = wp;
3902}
3903
3904/*
3905 * Remove a window from the window list.
3906 */
3907 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003908win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003910 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
3912 if (wp->w_prev != NULL)
3913 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003914 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 else
3917 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 if (wp->w_next != NULL)
3919 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003920 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003922 else
3923 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924}
3925
3926/*
3927 * Append frame "frp" in a frame list after frame "after".
3928 */
3929 static void
3930frame_append(after, frp)
3931 frame_T *after, *frp;
3932{
3933 frp->fr_next = after->fr_next;
3934 after->fr_next = frp;
3935 if (frp->fr_next != NULL)
3936 frp->fr_next->fr_prev = frp;
3937 frp->fr_prev = after;
3938}
3939
3940/*
3941 * Insert frame "frp" in a frame list before frame "before".
3942 */
3943 static void
3944frame_insert(before, frp)
3945 frame_T *before, *frp;
3946{
3947 frp->fr_next = before;
3948 frp->fr_prev = before->fr_prev;
3949 before->fr_prev = frp;
3950 if (frp->fr_prev != NULL)
3951 frp->fr_prev->fr_next = frp;
3952 else
3953 frp->fr_parent->fr_child = frp;
3954}
3955
3956/*
3957 * Remove a frame from a frame list.
3958 */
3959 static void
3960frame_remove(frp)
3961 frame_T *frp;
3962{
3963 if (frp->fr_prev != NULL)
3964 frp->fr_prev->fr_next = frp->fr_next;
3965 else
3966 frp->fr_parent->fr_child = frp->fr_next;
3967 if (frp->fr_next != NULL)
3968 frp->fr_next->fr_prev = frp->fr_prev;
3969}
3970
3971#endif /* FEAT_WINDOWS */
3972
3973/*
3974 * Allocate w_lines[] for window "wp".
3975 * Return FAIL for failure, OK for success.
3976 */
3977 int
3978win_alloc_lines(wp)
3979 win_T *wp;
3980{
3981 wp->w_lines_valid = 0;
3982 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
3983 if (wp->w_lines == NULL)
3984 return FAIL;
3985 return OK;
3986}
3987
3988/*
3989 * free lsize arrays for a window
3990 */
3991 void
3992win_free_lsize(wp)
3993 win_T *wp;
3994{
3995 vim_free(wp->w_lines);
3996 wp->w_lines = NULL;
3997}
3998
3999/*
4000 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004001 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 */
4003 void
4004shell_new_rows()
4005{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004006 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 if (firstwin == NULL) /* not initialized yet */
4009 return;
4010#ifdef FEAT_WINDOWS
4011 if (h < frame_minheight(topframe, NULL))
4012 h = frame_minheight(topframe, NULL);
4013 /* First try setting the heights of windows without 'winfixheight'. If
4014 * that doesn't result in the right height, forget about that option. */
4015 frame_new_height(topframe, h, FALSE, TRUE);
4016 if (topframe->fr_height != h)
4017 frame_new_height(topframe, h, FALSE, FALSE);
4018
4019 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4020#else
4021 if (h < 1)
4022 h = 1;
4023 win_new_height(firstwin, h);
4024#endif
4025 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004026#ifdef FEAT_WINDOWS
4027 p_ch_used = p_ch;
4028#endif
4029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#if 0
4031 /* Disabled: don't want making the screen smaller make a window larger. */
4032 if (p_ea)
4033 win_equal(curwin, FALSE, 'v');
4034#endif
4035}
4036
4037#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4038/*
4039 * Called from win_new_shellsize() after Columns changed.
4040 */
4041 void
4042shell_new_columns()
4043{
4044 if (firstwin == NULL) /* not initialized yet */
4045 return;
4046 frame_new_width(topframe, (int)Columns, FALSE);
4047 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4048#if 0
4049 /* Disabled: don't want making the screen smaller make a window larger. */
4050 if (p_ea)
4051 win_equal(curwin, FALSE, 'h');
4052#endif
4053}
4054#endif
4055
4056#if defined(FEAT_CMDWIN) || defined(PROTO)
4057/*
4058 * Save the size of all windows in "gap".
4059 */
4060 void
4061win_size_save(gap)
4062 garray_T *gap;
4063
4064{
4065 win_T *wp;
4066
4067 ga_init2(gap, (int)sizeof(int), 1);
4068 if (ga_grow(gap, win_count() * 2) == OK)
4069 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4070 {
4071 ((int *)gap->ga_data)[gap->ga_len++] =
4072 wp->w_width + wp->w_vsep_width;
4073 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4074 }
4075}
4076
4077/*
4078 * Restore window sizes, but only if the number of windows is still the same.
4079 * Does not free the growarray.
4080 */
4081 void
4082win_size_restore(gap)
4083 garray_T *gap;
4084{
4085 win_T *wp;
4086 int i;
4087
4088 if (win_count() * 2 == gap->ga_len)
4089 {
4090 i = 0;
4091 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4092 {
4093 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4094 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4095 }
4096 /* recompute the window positions */
4097 (void)win_comp_pos();
4098 }
4099}
4100#endif /* FEAT_CMDWIN */
4101
4102#if defined(FEAT_WINDOWS) || defined(PROTO)
4103/*
4104 * Update the position for all windows, using the width and height of the
4105 * frames.
4106 * Returns the row just after the last window.
4107 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004108 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109win_comp_pos()
4110{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004111 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 int col = 0;
4113
4114 frame_comp_pos(topframe, &row, &col);
4115 return row;
4116}
4117
4118/*
4119 * Update the position of the windows in frame "topfrp", using the width and
4120 * height of the frames.
4121 * "*row" and "*col" are the top-left position of the frame. They are updated
4122 * to the bottom-right position plus one.
4123 */
4124 static void
4125frame_comp_pos(topfrp, row, col)
4126 frame_T *topfrp;
4127 int *row;
4128 int *col;
4129{
4130 win_T *wp;
4131 frame_T *frp;
4132#ifdef FEAT_VERTSPLIT
4133 int startcol;
4134 int startrow;
4135#endif
4136
4137 wp = topfrp->fr_win;
4138 if (wp != NULL)
4139 {
4140 if (wp->w_winrow != *row
4141#ifdef FEAT_VERTSPLIT
4142 || wp->w_wincol != *col
4143#endif
4144 )
4145 {
4146 /* position changed, redraw */
4147 wp->w_winrow = *row;
4148#ifdef FEAT_VERTSPLIT
4149 wp->w_wincol = *col;
4150#endif
4151 redraw_win_later(wp, NOT_VALID);
4152 wp->w_redr_status = TRUE;
4153 }
4154 *row += wp->w_height + wp->w_status_height;
4155#ifdef FEAT_VERTSPLIT
4156 *col += wp->w_width + wp->w_vsep_width;
4157#endif
4158 }
4159 else
4160 {
4161#ifdef FEAT_VERTSPLIT
4162 startrow = *row;
4163 startcol = *col;
4164#endif
4165 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4166 {
4167#ifdef FEAT_VERTSPLIT
4168 if (topfrp->fr_layout == FR_ROW)
4169 *row = startrow; /* all frames are at the same row */
4170 else
4171 *col = startcol; /* all frames are at the same col */
4172#endif
4173 frame_comp_pos(frp, row, col);
4174 }
4175 }
4176}
4177
4178#endif /* FEAT_WINDOWS */
4179
4180/*
4181 * Set current window height and take care of repositioning other windows to
4182 * fit around it.
4183 */
4184 void
4185win_setheight(height)
4186 int height;
4187{
4188 win_setheight_win(height, curwin);
4189}
4190
4191/*
4192 * Set the window height of window "win" and take care of repositioning other
4193 * windows to fit around it.
4194 */
4195 void
4196win_setheight_win(height, win)
4197 int height;
4198 win_T *win;
4199{
4200 int row;
4201
4202 if (win == curwin)
4203 {
4204 /* Always keep current window at least one line high, even when
4205 * 'winminheight' is zero. */
4206#ifdef FEAT_WINDOWS
4207 if (height < p_wmh)
4208 height = p_wmh;
4209#endif
4210 if (height == 0)
4211 height = 1;
4212 }
4213
4214#ifdef FEAT_WINDOWS
4215 frame_setheight(win->w_frame, height + win->w_status_height);
4216
4217 /* recompute the window positions */
4218 row = win_comp_pos();
4219#else
4220 if (height > topframe->fr_height)
4221 height = topframe->fr_height;
4222 win->w_height = height;
4223 row = height;
4224#endif
4225
4226 /*
4227 * If there is extra space created between the last window and the command
4228 * line, clear it.
4229 */
4230 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4231 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4232 cmdline_row = row;
4233 msg_row = row;
4234 msg_col = 0;
4235
4236 redraw_all_later(NOT_VALID);
4237}
4238
4239#if defined(FEAT_WINDOWS) || defined(PROTO)
4240
4241/*
4242 * Set the height of a frame to "height" and take care that all frames and
4243 * windows inside it are resized. Also resize frames on the left and right if
4244 * the are in the same FR_ROW frame.
4245 *
4246 * Strategy:
4247 * If the frame is part of a FR_COL frame, try fitting the frame in that
4248 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4249 * go to containing frames to resize them and make room.
4250 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4251 * Check for the minimal height of the FR_ROW frame.
4252 * At the top level we can also use change the command line height.
4253 */
4254 static void
4255frame_setheight(curfrp, height)
4256 frame_T *curfrp;
4257 int height;
4258{
4259 int room; /* total number of lines available */
4260 int take; /* number of lines taken from other windows */
4261 int room_cmdline; /* lines available from cmdline */
4262 int run;
4263 frame_T *frp;
4264 int h;
4265 int room_reserved;
4266
4267 /* If the height already is the desired value, nothing to do. */
4268 if (curfrp->fr_height == height)
4269 return;
4270
4271 if (curfrp->fr_parent == NULL)
4272 {
4273 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004274 if (height > ROWS_AVAIL)
4275 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (height > 0)
4277 frame_new_height(curfrp, height, FALSE, FALSE);
4278 }
4279 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4280 {
4281 /* Row of frames: Also need to resize frames left and right of this
4282 * one. First check for the minimal height of these. */
4283 h = frame_minheight(curfrp->fr_parent, NULL);
4284 if (height < h)
4285 height = h;
4286 frame_setheight(curfrp->fr_parent, height);
4287 }
4288 else
4289 {
4290 /*
4291 * Column of frames: try to change only frames in this column.
4292 */
4293#ifdef FEAT_VERTSPLIT
4294 /*
4295 * Do this twice:
4296 * 1: compute room available, if it's not enough try resizing the
4297 * containing frame.
4298 * 2: compute the room available and adjust the height to it.
4299 * Try not to reduce the height of a window with 'winfixheight' set.
4300 */
4301 for (run = 1; run <= 2; ++run)
4302#else
4303 for (;;)
4304#endif
4305 {
4306 room = 0;
4307 room_reserved = 0;
4308 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4309 frp = frp->fr_next)
4310 {
4311 if (frp != curfrp
4312 && frp->fr_win != NULL
4313 && frp->fr_win->w_p_wfh)
4314 room_reserved += frp->fr_height;
4315 room += frp->fr_height;
4316 if (frp != curfrp)
4317 room -= frame_minheight(frp, NULL);
4318 }
4319#ifdef FEAT_VERTSPLIT
4320 if (curfrp->fr_width != Columns)
4321 room_cmdline = 0;
4322 else
4323#endif
4324 {
4325 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4326 + lastwin->w_height + lastwin->w_status_height);
4327 if (room_cmdline < 0)
4328 room_cmdline = 0;
4329 }
4330
4331 if (height <= room + room_cmdline)
4332 break;
4333#ifdef FEAT_VERTSPLIT
4334 if (run == 2 || curfrp->fr_width == Columns)
4335#endif
4336 {
4337 if (height > room + room_cmdline)
4338 height = room + room_cmdline;
4339 break;
4340 }
4341#ifdef FEAT_VERTSPLIT
4342 frame_setheight(curfrp->fr_parent, height
4343 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4344#endif
4345 /*NOTREACHED*/
4346 }
4347
4348 /*
4349 * Compute the number of lines we will take from others frames (can be
4350 * negative!).
4351 */
4352 take = height - curfrp->fr_height;
4353
4354 /* If there is not enough room, also reduce the height of a window
4355 * with 'winfixheight' set. */
4356 if (height > room + room_cmdline - room_reserved)
4357 room_reserved = room + room_cmdline - height;
4358 /* If there is only a 'winfixheight' window and making the
4359 * window smaller, need to make the other window taller. */
4360 if (take < 0 && room - curfrp->fr_height < room_reserved)
4361 room_reserved = 0;
4362
4363 if (take > 0 && room_cmdline > 0)
4364 {
4365 /* use lines from cmdline first */
4366 if (take < room_cmdline)
4367 room_cmdline = take;
4368 take -= room_cmdline;
4369 topframe->fr_height += room_cmdline;
4370 }
4371
4372 /*
4373 * set the current frame to the new height
4374 */
4375 frame_new_height(curfrp, height, FALSE, FALSE);
4376
4377 /*
4378 * First take lines from the frames after the current frame. If
4379 * that is not enough, takes lines from frames above the current
4380 * frame.
4381 */
4382 for (run = 0; run < 2; ++run)
4383 {
4384 if (run == 0)
4385 frp = curfrp->fr_next; /* 1st run: start with next window */
4386 else
4387 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4388 while (frp != NULL && take != 0)
4389 {
4390 h = frame_minheight(frp, NULL);
4391 if (room_reserved > 0
4392 && frp->fr_win != NULL
4393 && frp->fr_win->w_p_wfh)
4394 {
4395 if (room_reserved >= frp->fr_height)
4396 room_reserved -= frp->fr_height;
4397 else
4398 {
4399 if (frp->fr_height - room_reserved > take)
4400 room_reserved = frp->fr_height - take;
4401 take -= frp->fr_height - room_reserved;
4402 frame_new_height(frp, room_reserved, FALSE, FALSE);
4403 room_reserved = 0;
4404 }
4405 }
4406 else
4407 {
4408 if (frp->fr_height - take < h)
4409 {
4410 take -= frp->fr_height - h;
4411 frame_new_height(frp, h, FALSE, FALSE);
4412 }
4413 else
4414 {
4415 frame_new_height(frp, frp->fr_height - take,
4416 FALSE, FALSE);
4417 take = 0;
4418 }
4419 }
4420 if (run == 0)
4421 frp = frp->fr_next;
4422 else
4423 frp = frp->fr_prev;
4424 }
4425 }
4426 }
4427}
4428
4429#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4430/*
4431 * Set current window width and take care of repositioning other windows to
4432 * fit around it.
4433 */
4434 void
4435win_setwidth(width)
4436 int width;
4437{
4438 win_setwidth_win(width, curwin);
4439}
4440
4441 void
4442win_setwidth_win(width, wp)
4443 int width;
4444 win_T *wp;
4445{
4446 /* Always keep current window at least one column wide, even when
4447 * 'winminwidth' is zero. */
4448 if (wp == curwin)
4449 {
4450 if (width < p_wmw)
4451 width = p_wmw;
4452 if (width == 0)
4453 width = 1;
4454 }
4455
4456 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4457
4458 /* recompute the window positions */
4459 (void)win_comp_pos();
4460
4461 redraw_all_later(NOT_VALID);
4462}
4463
4464/*
4465 * Set the width of a frame to "width" and take care that all frames and
4466 * windows inside it are resized. Also resize frames above and below if the
4467 * are in the same FR_ROW frame.
4468 *
4469 * Strategy is similar to frame_setheight().
4470 */
4471 static void
4472frame_setwidth(curfrp, width)
4473 frame_T *curfrp;
4474 int width;
4475{
4476 int room; /* total number of lines available */
4477 int take; /* number of lines taken from other windows */
4478 int run;
4479 frame_T *frp;
4480 int w;
4481
4482 /* If the width already is the desired value, nothing to do. */
4483 if (curfrp->fr_width == width)
4484 return;
4485
4486 if (curfrp->fr_parent == NULL)
4487 /* topframe: can't change width */
4488 return;
4489
4490 if (curfrp->fr_parent->fr_layout == FR_COL)
4491 {
4492 /* Column of frames: Also need to resize frames above and below of
4493 * this one. First check for the minimal width of these. */
4494 w = frame_minwidth(curfrp->fr_parent, NULL);
4495 if (width < w)
4496 width = w;
4497 frame_setwidth(curfrp->fr_parent, width);
4498 }
4499 else
4500 {
4501 /*
4502 * Row of frames: try to change only frames in this row.
4503 *
4504 * Do this twice:
4505 * 1: compute room available, if it's not enough try resizing the
4506 * containing frame.
4507 * 2: compute the room available and adjust the width to it.
4508 */
4509 for (run = 1; run <= 2; ++run)
4510 {
4511 room = 0;
4512 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4513 frp = frp->fr_next)
4514 {
4515 room += frp->fr_width;
4516 if (frp != curfrp)
4517 room -= frame_minwidth(frp, NULL);
4518 }
4519
4520 if (width <= room)
4521 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004522 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 if (width > room)
4525 width = room;
4526 break;
4527 }
4528 frame_setwidth(curfrp->fr_parent, width
4529 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4530 }
4531
4532
4533 /*
4534 * Compute the number of lines we will take from others frames (can be
4535 * negative!).
4536 */
4537 take = width - curfrp->fr_width;
4538
4539 /*
4540 * set the current frame to the new width
4541 */
4542 frame_new_width(curfrp, width, FALSE);
4543
4544 /*
4545 * First take lines from the frames right of the current frame. If
4546 * that is not enough, takes lines from frames left of the current
4547 * frame.
4548 */
4549 for (run = 0; run < 2; ++run)
4550 {
4551 if (run == 0)
4552 frp = curfrp->fr_next; /* 1st run: start with next window */
4553 else
4554 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4555 while (frp != NULL && take != 0)
4556 {
4557 w = frame_minwidth(frp, NULL);
4558 if (frp->fr_width - take < w)
4559 {
4560 take -= frp->fr_width - w;
4561 frame_new_width(frp, w, FALSE);
4562 }
4563 else
4564 {
4565 frame_new_width(frp, frp->fr_width - take, FALSE);
4566 take = 0;
4567 }
4568 if (run == 0)
4569 frp = frp->fr_next;
4570 else
4571 frp = frp->fr_prev;
4572 }
4573 }
4574 }
4575}
4576#endif /* FEAT_VERTSPLIT */
4577
4578/*
4579 * Check 'winminheight' for a valid value.
4580 */
4581 void
4582win_setminheight()
4583{
4584 int room;
4585 int first = TRUE;
4586 win_T *wp;
4587
4588 /* loop until there is a 'winminheight' that is possible */
4589 while (p_wmh > 0)
4590 {
4591 /* TODO: handle vertical splits */
4592 room = -p_wh;
4593 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4594 room += wp->w_height - p_wmh;
4595 if (room >= 0)
4596 break;
4597 --p_wmh;
4598 if (first)
4599 {
4600 EMSG(_(e_noroom));
4601 first = FALSE;
4602 }
4603 }
4604}
4605
4606#ifdef FEAT_MOUSE
4607
4608/*
4609 * Status line of dragwin is dragged "offset" lines down (negative is up).
4610 */
4611 void
4612win_drag_status_line(dragwin, offset)
4613 win_T *dragwin;
4614 int offset;
4615{
4616 frame_T *curfr;
4617 frame_T *fr;
4618 int room;
4619 int row;
4620 int up; /* if TRUE, drag status line up, otherwise down */
4621 int n;
4622
4623 fr = dragwin->w_frame;
4624 curfr = fr;
4625 if (fr != topframe) /* more than one window */
4626 {
4627 fr = fr->fr_parent;
4628 /* When the parent frame is not a column of frames, its parent should
4629 * be. */
4630 if (fr->fr_layout != FR_COL)
4631 {
4632 curfr = fr;
4633 if (fr != topframe) /* only a row of windows, may drag statusline */
4634 fr = fr->fr_parent;
4635 }
4636 }
4637
4638 /* If this is the last frame in a column, may want to resize the parent
4639 * frame instead (go two up to skip a row of frames). */
4640 while (curfr != topframe && curfr->fr_next == NULL)
4641 {
4642 if (fr != topframe)
4643 fr = fr->fr_parent;
4644 curfr = fr;
4645 if (fr != topframe)
4646 fr = fr->fr_parent;
4647 }
4648
4649 if (offset < 0) /* drag up */
4650 {
4651 up = TRUE;
4652 offset = -offset;
4653 /* sum up the room of the current frame and above it */
4654 if (fr == curfr)
4655 {
4656 /* only one window */
4657 room = fr->fr_height - frame_minheight(fr, NULL);
4658 }
4659 else
4660 {
4661 room = 0;
4662 for (fr = fr->fr_child; ; fr = fr->fr_next)
4663 {
4664 room += fr->fr_height - frame_minheight(fr, NULL);
4665 if (fr == curfr)
4666 break;
4667 }
4668 }
4669 fr = curfr->fr_next; /* put fr at frame that grows */
4670 }
4671 else /* drag down */
4672 {
4673 up = FALSE;
4674 /*
4675 * Only dragging the last status line can reduce p_ch.
4676 */
4677 room = Rows - cmdline_row;
4678 if (curfr->fr_next == NULL)
4679 room -= 1;
4680 else
4681 room -= p_ch;
4682 if (room < 0)
4683 room = 0;
4684 /* sum up the room of frames below of the current one */
4685 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4686 room += fr->fr_height - frame_minheight(fr, NULL);
4687 fr = curfr; /* put fr at window that grows */
4688 }
4689
4690 if (room < offset) /* Not enough room */
4691 offset = room; /* Move as far as we can */
4692 if (offset <= 0)
4693 return;
4694
4695 /*
4696 * Grow frame fr by "offset" lines.
4697 * Doesn't happen when dragging the last status line up.
4698 */
4699 if (fr != NULL)
4700 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4701
4702 if (up)
4703 fr = curfr; /* current frame gets smaller */
4704 else
4705 fr = curfr->fr_next; /* next frame gets smaller */
4706
4707 /*
4708 * Now make the other frames smaller.
4709 */
4710 while (fr != NULL && offset > 0)
4711 {
4712 n = frame_minheight(fr, NULL);
4713 if (fr->fr_height - offset <= n)
4714 {
4715 offset -= fr->fr_height - n;
4716 frame_new_height(fr, n, !up, FALSE);
4717 }
4718 else
4719 {
4720 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4721 break;
4722 }
4723 if (up)
4724 fr = fr->fr_prev;
4725 else
4726 fr = fr->fr_next;
4727 }
4728 row = win_comp_pos();
4729 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4730 cmdline_row = row;
4731 p_ch = Rows - cmdline_row;
4732 if (p_ch < 1)
4733 p_ch = 1;
4734 redraw_all_later(NOT_VALID);
4735 showmode();
4736}
4737
4738#ifdef FEAT_VERTSPLIT
4739/*
4740 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4741 */
4742 void
4743win_drag_vsep_line(dragwin, offset)
4744 win_T *dragwin;
4745 int offset;
4746{
4747 frame_T *curfr;
4748 frame_T *fr;
4749 int room;
4750 int left; /* if TRUE, drag separator line left, otherwise right */
4751 int n;
4752
4753 fr = dragwin->w_frame;
4754 if (fr == topframe) /* only one window (cannot happe?) */
4755 return;
4756 curfr = fr;
4757 fr = fr->fr_parent;
4758 /* When the parent frame is not a row of frames, its parent should be. */
4759 if (fr->fr_layout != FR_ROW)
4760 {
4761 if (fr == topframe) /* only a column of windows (cannot happen?) */
4762 return;
4763 curfr = fr;
4764 fr = fr->fr_parent;
4765 }
4766
4767 /* If this is the last frame in a row, may want to resize a parent
4768 * frame instead. */
4769 while (curfr->fr_next == NULL)
4770 {
4771 if (fr == topframe)
4772 break;
4773 curfr = fr;
4774 fr = fr->fr_parent;
4775 if (fr != topframe)
4776 {
4777 curfr = fr;
4778 fr = fr->fr_parent;
4779 }
4780 }
4781
4782 if (offset < 0) /* drag left */
4783 {
4784 left = TRUE;
4785 offset = -offset;
4786 /* sum up the room of the current frame and left of it */
4787 room = 0;
4788 for (fr = fr->fr_child; ; fr = fr->fr_next)
4789 {
4790 room += fr->fr_width - frame_minwidth(fr, NULL);
4791 if (fr == curfr)
4792 break;
4793 }
4794 fr = curfr->fr_next; /* put fr at frame that grows */
4795 }
4796 else /* drag right */
4797 {
4798 left = FALSE;
4799 /* sum up the room of frames right of the current one */
4800 room = 0;
4801 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4802 room += fr->fr_width - frame_minwidth(fr, NULL);
4803 fr = curfr; /* put fr at window that grows */
4804 }
4805
4806 if (room < offset) /* Not enough room */
4807 offset = room; /* Move as far as we can */
4808 if (offset <= 0) /* No room at all, quit. */
4809 return;
4810
4811 /* grow frame fr by offset lines */
4812 frame_new_width(fr, fr->fr_width + offset, left);
4813
4814 /* shrink other frames: current and at the left or at the right */
4815 if (left)
4816 fr = curfr; /* current frame gets smaller */
4817 else
4818 fr = curfr->fr_next; /* next frame gets smaller */
4819
4820 while (fr != NULL && offset > 0)
4821 {
4822 n = frame_minwidth(fr, NULL);
4823 if (fr->fr_width - offset <= n)
4824 {
4825 offset -= fr->fr_width - n;
4826 frame_new_width(fr, n, !left);
4827 }
4828 else
4829 {
4830 frame_new_width(fr, fr->fr_width - offset, !left);
4831 break;
4832 }
4833 if (left)
4834 fr = fr->fr_prev;
4835 else
4836 fr = fr->fr_next;
4837 }
4838 (void)win_comp_pos();
4839 redraw_all_later(NOT_VALID);
4840}
4841#endif /* FEAT_VERTSPLIT */
4842#endif /* FEAT_MOUSE */
4843
4844#endif /* FEAT_WINDOWS */
4845
4846/*
4847 * Set the height of a window.
4848 * This takes care of the things inside the window, not what happens to the
4849 * window position, the frame or to other windows.
4850 */
4851 static void
4852win_new_height(wp, height)
4853 win_T *wp;
4854 int height;
4855{
4856 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00004857 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00004859 int space;
4860 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#define FRACTION_MULT 16384L
4862
4863 /* Don't want a negative height. Happens when splitting a tiny window.
4864 * Will equalize heights soon to fix it. */
4865 if (height < 0)
4866 height = 0;
4867
4868 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
4869 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
4870 + FRACTION_MULT / 2) / (long)wp->w_height;
4871
4872 wp->w_height = height;
4873 wp->w_skipcol = 0;
4874
4875 /* Don't change w_topline when height is zero. Don't set w_topline when
4876 * 'scrollbind' is set and this isn't the current window. */
4877 if (height > 0
4878#ifdef FEAT_SCROLLBIND
4879 && (!wp->w_p_scb || wp == curwin)
4880#endif
4881 )
4882 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004883 /*
4884 * Find a value for w_topline that shows the cursor at the same
4885 * relative position in the window as before (more or less).
4886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 lnum = wp->w_cursor.lnum;
4888 if (lnum < 1) /* can happen when starting up */
4889 lnum = 1;
4890 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
4891 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
4892 sline = wp->w_wrow - line_size;
4893 if (sline < 0)
4894 {
4895 /*
4896 * Cursor line would go off top of screen if w_wrow was this high.
4897 */
4898 wp->w_wrow = line_size;
4899 }
4900 else
4901 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004902 space = height;
4903 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar34114692005-01-02 11:28:13 +00004905 space -= line_size;
4906 if (space > 0 && sline <= 0 && !did_below)
4907 {
4908 /* Try to use "~" lines below the text to avoid that text
4909 * is above the window while there are empty lines.
4910 * Subtract the rows below the cursor from "space" and
4911 * give the rest to "sline". */
4912 did_below = TRUE;
4913 bot = wp->w_cursor.lnum;
4914 while (space > 0)
4915 {
4916 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
4917 space = 0;
4918 else
4919 {
4920#ifdef FEAT_FOLDING
4921 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
4922#endif
4923 if (bot >= wp->w_buffer->b_ml.ml_line_count)
4924 break;
4925 ++bot;
4926 space -= plines_win(wp, bot, TRUE);
4927 }
4928 }
4929 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
4930 sline += space;
4931 }
4932 if (sline <= 0)
4933 break;
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#ifdef FEAT_FOLDING
4936 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
4937 if (lnum == 1)
4938 {
4939 /* first line in buffer is folded */
4940 line_size = 1;
4941 --sline;
4942 break;
4943 }
4944#endif
4945 --lnum;
4946#ifdef FEAT_DIFF
4947 if (lnum == wp->w_topline)
4948 line_size = plines_win_nofill(wp, lnum, TRUE)
4949 + wp->w_topfill;
4950 else
4951#endif
4952 line_size = plines_win(wp, lnum, TRUE);
4953 sline -= line_size;
4954 }
Bram Moolenaar34114692005-01-02 11:28:13 +00004955
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (sline < 0)
4957 {
4958 /*
4959 * Line we want at top would go off top of screen. Use next
4960 * line instead.
4961 */
4962#ifdef FEAT_FOLDING
4963 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
4964#endif
4965 lnum++;
4966 wp->w_wrow -= line_size + sline;
4967 }
4968 else if (sline > 0)
4969 {
4970 /* First line of file reached, use that as topline. */
4971 lnum = 1;
4972 wp->w_wrow -= sline;
4973 }
4974 }
4975 set_topline(wp, lnum);
4976 }
4977
4978 if (wp == curwin)
4979 {
4980 if (p_so)
4981 update_topline();
4982 curs_columns(FALSE); /* validate w_wrow */
4983 }
4984 wp->w_prev_fraction_row = wp->w_wrow;
4985
4986 win_comp_scroll(wp);
4987 redraw_win_later(wp, NOT_VALID);
4988#ifdef FEAT_WINDOWS
4989 wp->w_redr_status = TRUE;
4990#endif
4991 invalidate_botline_win(wp);
4992}
4993
4994#ifdef FEAT_VERTSPLIT
4995/*
4996 * Set the width of a window.
4997 */
4998 static void
4999win_new_width(wp, width)
5000 win_T *wp;
5001 int width;
5002{
5003 wp->w_width = width;
5004 wp->w_lines_valid = 0;
5005 changed_line_abv_curs_win(wp);
5006 invalidate_botline_win(wp);
5007 if (wp == curwin)
5008 {
5009 update_topline();
5010 curs_columns(TRUE); /* validate w_wrow */
5011 }
5012 redraw_win_later(wp, NOT_VALID);
5013 wp->w_redr_status = TRUE;
5014}
5015#endif
5016
5017 void
5018win_comp_scroll(wp)
5019 win_T *wp;
5020{
5021 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5022 if (wp->w_p_scr == 0)
5023 wp->w_p_scr = 1;
5024}
5025
5026/*
5027 * command_height: called whenever p_ch has been changed
5028 */
5029 void
5030command_height(old_p_ch)
5031 long old_p_ch;
5032{
5033#ifdef FEAT_WINDOWS
5034 int h;
5035 frame_T *frp;
5036
Bram Moolenaar05159a02005-02-26 23:04:13 +00005037 /* When passed a negative value use the value of p_ch that we remembered.
5038 * This is needed for when the GUI starts up, we can't be sure in what
5039 * order things happen. */
5040 if (old_p_ch < 0)
5041 old_p_ch = p_ch_used;
5042 p_ch_used = p_ch;
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /* Find bottom frame with width of screen. */
5045 frp = lastwin->w_frame;
5046# ifdef FEAT_VERTSPLIT
5047 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5048 frp = frp->fr_parent;
5049# endif
5050
5051 /* Avoid changing the height of a window with 'winfixheight' set. */
5052 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5053 && frp->fr_win->w_p_wfh)
5054 frp = frp->fr_prev;
5055
5056 if (starting != NO_SCREEN)
5057 {
5058 cmdline_row = Rows - p_ch;
5059
5060 if (p_ch > old_p_ch) /* p_ch got bigger */
5061 {
5062 while (p_ch > old_p_ch)
5063 {
5064 if (frp == NULL)
5065 {
5066 EMSG(_(e_noroom));
5067 p_ch = old_p_ch;
5068 cmdline_row = Rows - p_ch;
5069 break;
5070 }
5071 h = frp->fr_height - frame_minheight(frp, NULL);
5072 if (h > p_ch - old_p_ch)
5073 h = p_ch - old_p_ch;
5074 old_p_ch += h;
5075 frame_add_height(frp, -h);
5076 frp = frp->fr_prev;
5077 }
5078
5079 /* Recompute window positions. */
5080 (void)win_comp_pos();
5081
5082 /* clear the lines added to cmdline */
5083 if (full_screen)
5084 screen_fill((int)(cmdline_row), (int)Rows, 0,
5085 (int)Columns, ' ', ' ', 0);
5086 msg_row = cmdline_row;
5087 redraw_cmdline = TRUE;
5088 return;
5089 }
5090
5091 if (msg_row < cmdline_row)
5092 msg_row = cmdline_row;
5093 redraw_cmdline = TRUE;
5094 }
5095 frame_add_height(frp, (int)(old_p_ch - p_ch));
5096
5097 /* Recompute window positions. */
5098 if (frp != lastwin->w_frame)
5099 (void)win_comp_pos();
5100#else
5101 win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5102 cmdline_row = Rows - p_ch;
5103#endif
5104}
5105
5106#if defined(FEAT_WINDOWS) || defined(PROTO)
5107/*
5108 * Resize frame "frp" to be "n" lines higher (negative for less high).
5109 * Also resize the frames it is contained in.
5110 */
5111 static void
5112frame_add_height(frp, n)
5113 frame_T *frp;
5114 int n;
5115{
5116 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5117 for (;;)
5118 {
5119 frp = frp->fr_parent;
5120 if (frp == NULL)
5121 break;
5122 frp->fr_height += n;
5123 }
5124}
5125
5126/*
5127 * Add or remove a status line for the bottom window(s), according to the
5128 * value of 'laststatus'.
5129 */
5130 void
5131last_status(morewin)
5132 int morewin; /* pretend there are two or more windows */
5133{
5134 /* Don't make a difference between horizontal or vertical split. */
5135 last_status_rec(topframe, (p_ls == 2
5136 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5137}
5138
5139 static void
5140last_status_rec(fr, statusline)
5141 frame_T *fr;
5142 int statusline;
5143{
5144 frame_T *fp;
5145 win_T *wp;
5146
5147 if (fr->fr_layout == FR_LEAF)
5148 {
5149 wp = fr->fr_win;
5150 if (wp->w_status_height != 0 && !statusline)
5151 {
5152 /* remove status line */
5153 win_new_height(wp, wp->w_height + 1);
5154 wp->w_status_height = 0;
5155 comp_col();
5156 }
5157 else if (wp->w_status_height == 0 && statusline)
5158 {
5159 /* Find a frame to take a line from. */
5160 fp = fr;
5161 while (fp->fr_height <= frame_minheight(fp, NULL))
5162 {
5163 if (fp == topframe)
5164 {
5165 EMSG(_(e_noroom));
5166 return;
5167 }
5168 /* In a column of frames: go to frame above. If already at
5169 * the top or in a row of frames: go to parent. */
5170 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5171 fp = fp->fr_prev;
5172 else
5173 fp = fp->fr_parent;
5174 }
5175 wp->w_status_height = 1;
5176 if (fp != fr)
5177 {
5178 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5179 frame_fix_height(wp);
5180 (void)win_comp_pos();
5181 }
5182 else
5183 win_new_height(wp, wp->w_height - 1);
5184 comp_col();
5185 redraw_all_later(NOT_VALID);
5186 }
5187 }
5188#ifdef FEAT_VERTSPLIT
5189 else if (fr->fr_layout == FR_ROW)
5190 {
5191 /* vertically split windows, set status line for each one */
5192 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5193 last_status_rec(fp, statusline);
5194 }
5195#endif
5196 else
5197 {
5198 /* horizontally split window, set status line for last one */
5199 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5200 ;
5201 last_status_rec(fp, statusline);
5202 }
5203}
5204
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005205/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005206 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005207 */
5208 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005209tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005210{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005211#ifdef FEAT_GUI_TABLINE
5212 /* When the GUI has the tabline then this always returns zero. */
5213 if (gui_use_tabline())
5214 return 0;
5215#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005216 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005217 {
5218 case 0: return 0;
5219 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5220 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005221 return 1;
5222}
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif /* FEAT_WINDOWS */
5225
5226#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5227/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005228 * Get the file name at the cursor.
5229 * If Visual mode is active, use the selected text if it's in one line.
5230 * Returns the name in allocated memory, NULL for failure.
5231 */
5232 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005233grab_file_name(count, file_lnum)
5234 long count;
5235 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005236{
5237# ifdef FEAT_VISUAL
5238 if (VIsual_active)
5239 {
5240 int len;
5241 char_u *ptr;
5242
5243 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5244 return NULL;
5245 return find_file_name_in_path(ptr, len,
5246 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5247 }
5248# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005249 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5250 file_lnum);
5251
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005252}
5253
5254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 * Return the file name under or after the cursor.
5256 *
5257 * The 'path' option is searched if the file name is not absolute.
5258 * The string returned has been alloc'ed and should be freed by the caller.
5259 * NULL is returned if the file name or file is not found.
5260 *
5261 * options:
5262 * FNAME_MESS give error messages
5263 * FNAME_EXP expand to path
5264 * FNAME_HYP check for hypertext link
5265 * FNAME_INCL apply "includeexpr"
5266 */
5267 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005268file_name_at_cursor(options, count, file_lnum)
5269 int options;
5270 long count;
5271 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005274 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5275 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276}
5277
5278/*
5279 * Return the name of the file under or after ptr[col].
5280 * Otherwise like file_name_at_cursor().
5281 */
5282 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005283file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 char_u *line;
5285 int col;
5286 int options;
5287 long count;
5288 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005289 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
5291 char_u *ptr;
5292 int len;
5293
5294 /*
5295 * search forward for what could be the start of a file name
5296 */
5297 ptr = line + col;
5298 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005299 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (*ptr == NUL) /* nothing found */
5301 {
5302 if (options & FNAME_MESS)
5303 EMSG(_("E446: No file name under cursor"));
5304 return NULL;
5305 }
5306
5307 /*
5308 * Search backward for first char of the file name.
5309 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5310 */
5311 while (ptr > line)
5312 {
5313#ifdef FEAT_MBYTE
5314 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5315 ptr -= len + 1;
5316 else
5317#endif
5318 if (vim_isfilec(ptr[-1])
5319 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5320 --ptr;
5321 else
5322 break;
5323 }
5324
5325 /*
5326 * Search forward for the last char of the file name.
5327 * Also allow "://" when ':' is not in 'isfname'.
5328 */
5329 len = 0;
5330 while (vim_isfilec(ptr[len])
5331 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5332#ifdef FEAT_MBYTE
5333 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005334 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 else
5336#endif
5337 ++len;
5338
5339 /*
5340 * If there is trailing punctuation, remove it.
5341 * But don't remove "..", could be a directory name.
5342 */
5343 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5344 && ptr[len - 2] != '.')
5345 --len;
5346
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005347 if (file_lnum != NULL)
5348 {
5349 char_u *p;
5350
5351 /* Get the number after the file name and a separator character */
5352 p = ptr + len;
5353 p = skipwhite(p);
5354 if (*p != NUL)
5355 {
5356 if (!isdigit(*p))
5357 ++p; /* skip the separator */
5358 p = skipwhite(p);
5359 if (isdigit(*p))
5360 *file_lnum = (int)getdigits(&p);
5361 }
5362 }
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5365}
5366
5367# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5368static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5369
5370 static char_u *
5371eval_includeexpr(ptr, len)
5372 char_u *ptr;
5373 int len;
5374{
5375 char_u *res;
5376
5377 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005378 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005379 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 set_vim_var_string(VV_FNAME, NULL, 0);
5381 return res;
5382}
5383#endif
5384
5385/*
5386 * Return the name of the file ptr[len] in 'path'.
5387 * Otherwise like file_name_at_cursor().
5388 */
5389 char_u *
5390find_file_name_in_path(ptr, len, options, count, rel_fname)
5391 char_u *ptr;
5392 int len;
5393 int options;
5394 long count;
5395 char_u *rel_fname; /* file we are searching relative to */
5396{
5397 char_u *file_name;
5398 int c;
5399# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5400 char_u *tofree = NULL;
5401
5402 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5403 {
5404 tofree = eval_includeexpr(ptr, len);
5405 if (tofree != NULL)
5406 {
5407 ptr = tofree;
5408 len = (int)STRLEN(ptr);
5409 }
5410 }
5411# endif
5412
5413 if (options & FNAME_EXP)
5414 {
5415 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5416 TRUE, rel_fname);
5417
5418# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5419 /*
5420 * If the file could not be found in a normal way, try applying
5421 * 'includeexpr' (unless done already).
5422 */
5423 if (file_name == NULL
5424 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5425 {
5426 tofree = eval_includeexpr(ptr, len);
5427 if (tofree != NULL)
5428 {
5429 ptr = tofree;
5430 len = (int)STRLEN(ptr);
5431 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5432 TRUE, rel_fname);
5433 }
5434 }
5435# endif
5436 if (file_name == NULL && (options & FNAME_MESS))
5437 {
5438 c = ptr[len];
5439 ptr[len] = NUL;
5440 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5441 ptr[len] = c;
5442 }
5443
5444 /* Repeat finding the file "count" times. This matters when it
5445 * appears several times in the path. */
5446 while (file_name != NULL && --count > 0)
5447 {
5448 vim_free(file_name);
5449 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5450 }
5451 }
5452 else
5453 file_name = vim_strnsave(ptr, len);
5454
5455# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5456 vim_free(tofree);
5457# endif
5458
5459 return file_name;
5460}
5461#endif /* FEAT_SEARCHPATH */
5462
5463/*
5464 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5465 * Also check for ":\\", which MS Internet Explorer accepts, return
5466 * URL_BACKSLASH.
5467 */
5468 static int
5469path_is_url(p)
5470 char_u *p;
5471{
5472 if (STRNCMP(p, "://", (size_t)3) == 0)
5473 return URL_SLASH;
5474 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5475 return URL_BACKSLASH;
5476 return 0;
5477}
5478
5479/*
5480 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5481 * Return URL_BACKSLASH for "name:\\".
5482 * Return zero otherwise.
5483 */
5484 int
5485path_with_url(fname)
5486 char_u *fname;
5487{
5488 char_u *p;
5489
5490 for (p = fname; isalpha(*p); ++p)
5491 ;
5492 return path_is_url(p);
5493}
5494
5495/*
5496 * Return TRUE if "name" is a full (absolute) path name or URL.
5497 */
5498 int
5499vim_isAbsName(name)
5500 char_u *name;
5501{
5502 return (path_with_url(name) != 0 || mch_isFullName(name));
5503}
5504
5505/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005506 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 *
5508 * return FAIL for failure, OK otherwise
5509 */
5510 int
5511vim_FullName(fname, buf, len, force)
5512 char_u *fname, *buf;
5513 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005514 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 int retval = OK;
5517 int url;
5518
5519 *buf = NUL;
5520 if (fname == NULL)
5521 return FAIL;
5522
5523 url = path_with_url(fname);
5524 if (!url)
5525 retval = mch_FullName(fname, buf, len, force);
5526 if (url || retval == FAIL)
5527 {
5528 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005529 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
5531#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5532 slash_adjust(buf);
5533#endif
5534 return retval;
5535}
5536
5537/*
5538 * Return the minimal number of rows that is needed on the screen to display
5539 * the current number of windows.
5540 */
5541 int
5542min_rows()
5543{
5544 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005545#ifdef FEAT_WINDOWS
5546 tabpage_T *tp;
5547 int n;
5548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 if (firstwin == NULL) /* not initialized yet */
5551 return MIN_LINES;
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005554 total = 0;
5555 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5556 {
5557 n = frame_minheight(tp->tp_topframe, NULL);
5558 if (total < n)
5559 total = n;
5560 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005561 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005563 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005565 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return total;
5567}
5568
5569/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005570 * Return TRUE if there is only one window (in the current tab page), not
5571 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 */
5573 int
5574only_one_window()
5575{
5576#ifdef FEAT_WINDOWS
5577 int count = 0;
5578 win_T *wp;
5579
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005580 /* If there is another tab page there always is another window. */
5581 if (first_tabpage->tp_next != NULL)
5582 return FALSE;
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005585 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586# ifdef FEAT_QUICKFIX
5587 || wp->w_p_pvw
5588# endif
5589 ) || wp == curwin)
5590 ++count;
5591 return (count <= 1);
5592#else
5593 return TRUE;
5594#endif
5595}
5596
5597#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5598/*
5599 * Correct the cursor line number in other windows. Used after changing the
5600 * current buffer, and before applying autocommands.
5601 * When "do_curwin" is TRUE, also check current window.
5602 */
5603 void
5604check_lnums(do_curwin)
5605 int do_curwin;
5606{
5607 win_T *wp;
5608
5609#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005610 tabpage_T *tp;
5611
5612 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5614#else
5615 wp = curwin;
5616 if (do_curwin)
5617#endif
5618 {
5619 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5620 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5621 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5622 wp->w_topline = curbuf->b_ml.ml_line_count;
5623 }
5624}
5625#endif
5626
5627#if defined(FEAT_WINDOWS) || defined(PROTO)
5628
5629/*
5630 * A snapshot of the window sizes, to restore them after closing the help
5631 * window.
5632 * Only these fields are used:
5633 * fr_layout
5634 * fr_width
5635 * fr_height
5636 * fr_next
5637 * fr_child
5638 * fr_win (only valid for the old curwin, NULL otherwise)
5639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
5641/*
5642 * Create a snapshot of the current frame sizes.
5643 */
5644 static void
5645make_snapshot()
5646{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005647 clear_snapshot(curtab);
5648 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649}
5650
5651 static void
5652make_snapshot_rec(fr, frp)
5653 frame_T *fr;
5654 frame_T **frp;
5655{
5656 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5657 if (*frp == NULL)
5658 return;
5659 (*frp)->fr_layout = fr->fr_layout;
5660# ifdef FEAT_VERTSPLIT
5661 (*frp)->fr_width = fr->fr_width;
5662# endif
5663 (*frp)->fr_height = fr->fr_height;
5664 if (fr->fr_next != NULL)
5665 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5666 if (fr->fr_child != NULL)
5667 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5668 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5669 (*frp)->fr_win = curwin;
5670}
5671
5672/*
5673 * Remove any existing snapshot.
5674 */
5675 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005676clear_snapshot(tp)
5677 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005679 clear_snapshot_rec(tp->tp_snapshot);
5680 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681}
5682
5683 static void
5684clear_snapshot_rec(fr)
5685 frame_T *fr;
5686{
5687 if (fr != NULL)
5688 {
5689 clear_snapshot_rec(fr->fr_next);
5690 clear_snapshot_rec(fr->fr_child);
5691 vim_free(fr);
5692 }
5693}
5694
5695/*
5696 * Restore a previously created snapshot, if there is any.
5697 * This is only done if the screen size didn't change and the window layout is
5698 * still the same.
5699 */
5700 static void
5701restore_snapshot(close_curwin)
5702 int close_curwin; /* closing current window */
5703{
5704 win_T *wp;
5705
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005706 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005708 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005710 && curtab->tp_snapshot->fr_height == topframe->fr_height
5711 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005713 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 win_comp_pos();
5715 if (wp != NULL && close_curwin)
5716 win_goto(wp);
5717 redraw_all_later(CLEAR);
5718 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005719 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
5722/*
5723 * Check if frames "sn" and "fr" have the same layout, same following frames
5724 * and same children.
5725 */
5726 static int
5727check_snapshot_rec(sn, fr)
5728 frame_T *sn;
5729 frame_T *fr;
5730{
5731 if (sn->fr_layout != fr->fr_layout
5732 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5733 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5734 || (sn->fr_next != NULL
5735 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5736 || (sn->fr_child != NULL
5737 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5738 return FAIL;
5739 return OK;
5740}
5741
5742/*
5743 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5744 * following frames and children.
5745 * Returns a pointer to the old current window, or NULL.
5746 */
5747 static win_T *
5748restore_snapshot_rec(sn, fr)
5749 frame_T *sn;
5750 frame_T *fr;
5751{
5752 win_T *wp = NULL;
5753 win_T *wp2;
5754
5755 fr->fr_height = sn->fr_height;
5756# ifdef FEAT_VERTSPLIT
5757 fr->fr_width = sn->fr_width;
5758# endif
5759 if (fr->fr_layout == FR_LEAF)
5760 {
5761 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5762# ifdef FEAT_VERTSPLIT
5763 frame_new_width(fr, fr->fr_width, FALSE);
5764# endif
5765 wp = sn->fr_win;
5766 }
5767 if (sn->fr_next != NULL)
5768 {
5769 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5770 if (wp2 != NULL)
5771 wp = wp2;
5772 }
5773 if (sn->fr_child != NULL)
5774 {
5775 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
5776 if (wp2 != NULL)
5777 wp = wp2;
5778 }
5779 return wp;
5780}
5781
5782#endif
5783
5784#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
5785/*
5786 * Return TRUE if there is any vertically split window.
5787 */
5788 int
5789win_hasvertsplit()
5790{
5791 frame_T *fr;
5792
5793 if (topframe->fr_layout == FR_ROW)
5794 return TRUE;
5795
5796 if (topframe->fr_layout == FR_COL)
5797 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
5798 if (fr->fr_layout == FR_ROW)
5799 return TRUE;
5800
5801 return FALSE;
5802}
5803#endif