blob: 5e9553b6e56c266e3b2b491c192608638a05c33f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
12#ifdef HAVE_FCNTL_H
13# include <fcntl.h> /* for chdir() */
14#endif
15
16static int path_is_url __ARGS((char_u *p));
17#if defined(FEAT_WINDOWS) || defined(PROTO)
18static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000019static void win_init __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21static void frame_setheight __ARGS((frame_T *curfrp, int height));
22#ifdef FEAT_VERTSPLIT
23static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24#endif
25static void win_exchange __ARGS((long));
26static void win_rotate __ARGS((int, int));
27static void win_totop __ARGS((int size, int flags));
28static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000029static int last_window __ARGS((void));
Bram Moolenaarf740b292006-02-16 22:11:02 +000030static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000033static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static win_T *frame2win __ARGS((frame_T *frp));
35static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37static int frame_fixed_height __ARGS((frame_T *frp));
38#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000039static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000041static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000042static void frame_add_vsep __ARGS((frame_T *frp));
43static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44static void frame_fix_width __ARGS((win_T *wp));
45#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000047static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000048#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000049static tabpage_T *alloc_tabpage __ARGS((void));
50static void free_tabpage __ARGS((tabpage_T *tp));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000051static int leave_tabpage __ARGS((buf_T *new_curbuf));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000052static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_fix_height __ARGS((win_T *wp));
54static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
55static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
Bram Moolenaarf740b292006-02-16 22:11:02 +000056static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void win_append __ARGS((win_T *, win_T *));
Bram Moolenaarf740b292006-02-16 22:11:02 +000058static void win_remove __ARGS((win_T *, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000059static void frame_append __ARGS((frame_T *after, frame_T *frp));
60static void frame_insert __ARGS((frame_T *before, frame_T *frp));
61static void frame_remove __ARGS((frame_T *frp));
62#ifdef FEAT_VERTSPLIT
63static void win_new_width __ARGS((win_T *wp, int width));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void win_goto_ver __ARGS((int up, long count));
65static void win_goto_hor __ARGS((int left, long count));
66#endif
67static void frame_add_height __ARGS((frame_T *frp, int n));
68static void last_status_rec __ARGS((frame_T *fr, int statusline));
69
70static void make_snapshot __ARGS((void));
71static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000072static void clear_snapshot __ARGS((tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073static void clear_snapshot_rec __ARGS((frame_T *fr));
74static void restore_snapshot __ARGS((int close_curwin));
75static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
77
78#endif /* FEAT_WINDOWS */
79static win_T *win_alloc __ARGS((win_T *after));
80static void win_new_height __ARGS((win_T *, int));
81
82#define URL_SLASH 1 /* path_is_url() has found "://" */
83#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
84
85#define NOWIN (win_T *)-1 /* non-exisiting window */
86
Bram Moolenaar05159a02005-02-26 23:04:13 +000087#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000088# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000089#else
90# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000091#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#if defined(FEAT_WINDOWS) || defined(PROTO)
94/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
98do_window(nchar, Prenum, xchar)
99 int nchar;
100 long Prenum;
101 int xchar; /* extra char from ":wincmd gx" or NUL */
102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
133#ifdef FEAT_VISUAL
134 reset_VIsual_and_resel(); /* stop Visual mode */
135#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000136#ifdef FEAT_QUICKFIX
137 /* When splitting the quickfix window open a new buffer in it,
138 * don't replicate the quickfix buffer. */
139 if (bt_quickfix(curbuf))
140 goto newwindow;
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_GUI
143 need_mouse_correct = TRUE;
144#endif
145 win_split((int)Prenum, 0);
146 break;
147
148#ifdef FEAT_VERTSPLIT
149/* split current window in two parts, vertically */
150 case Ctrl_V:
151 case 'v':
152 CHECK_CMDWIN
153#ifdef FEAT_VISUAL
154 reset_VIsual_and_resel(); /* stop Visual mode */
155#endif
156#ifdef FEAT_GUI
157 need_mouse_correct = TRUE;
158#endif
159 win_split((int)Prenum, WSP_VERT);
160 break;
161#endif
162
163/* split current window and edit alternate file */
164 case Ctrl_HAT:
165 case '^':
166 CHECK_CMDWIN
167#ifdef FEAT_VISUAL
168 reset_VIsual_and_resel(); /* stop Visual mode */
169#endif
170 STRCPY(cbuf, "split #");
171 if (Prenum)
172 sprintf((char *)cbuf + 7, "%ld", Prenum);
173 do_cmdline_cmd(cbuf);
174 break;
175
176/* open new window */
177 case Ctrl_N:
178 case 'n':
179 CHECK_CMDWIN
180#ifdef FEAT_VISUAL
181 reset_VIsual_and_resel(); /* stop Visual mode */
182#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000183#ifdef FEAT_QUICKFIX
184newwindow:
185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 if (Prenum)
187 sprintf((char *)cbuf, "%ld", Prenum); /* window height */
188 else
189 cbuf[0] = NUL;
190 STRCAT(cbuf, "new");
191 do_cmdline_cmd(cbuf);
192 break;
193
194/* quit current window */
195 case Ctrl_Q:
196 case 'q':
197#ifdef FEAT_VISUAL
198 reset_VIsual_and_resel(); /* stop Visual mode */
199#endif
200 do_cmdline_cmd((char_u *)"quit");
201 break;
202
203/* close current window */
204 case Ctrl_C:
205 case 'c':
206#ifdef FEAT_VISUAL
207 reset_VIsual_and_resel(); /* stop Visual mode */
208#endif
209 do_cmdline_cmd((char_u *)"close");
210 break;
211
212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
213/* close preview window */
214 case Ctrl_Z:
215 case 'z':
216 CHECK_CMDWIN
217#ifdef FEAT_VISUAL
218 reset_VIsual_and_resel(); /* stop Visual mode */
219#endif
220 do_cmdline_cmd((char_u *)"pclose");
221 break;
222
223/* cursor to preview window */
224 case 'P':
225 for (wp = firstwin; wp != NULL; wp = wp->w_next)
226 if (wp->w_p_pvw)
227 break;
228 if (wp == NULL)
229 EMSG(_("E441: There is no preview window"));
230 else
231 win_goto(wp);
232 break;
233#endif
234
235/* close all but current window */
236 case Ctrl_O:
237 case 'o':
238 CHECK_CMDWIN
239#ifdef FEAT_VISUAL
240 reset_VIsual_and_resel(); /* stop Visual mode */
241#endif
242 do_cmdline_cmd((char_u *)"only");
243 break;
244
245/* cursor to next window with wrap around */
246 case Ctrl_W:
247 case 'w':
248/* cursor to previous window with wrap around */
249 case 'W':
250 CHECK_CMDWIN
251 if (lastwin == firstwin && Prenum != 1) /* just one window */
252 beep_flush();
253 else
254 {
255 if (Prenum) /* go to specified window */
256 {
257 for (wp = firstwin; --Prenum > 0; )
258 {
259 if (wp->w_next == NULL)
260 break;
261 else
262 wp = wp->w_next;
263 }
264 }
265 else
266 {
267 if (nchar == 'W') /* go to previous window */
268 {
269 wp = curwin->w_prev;
270 if (wp == NULL)
271 wp = lastwin; /* wrap around */
272 }
273 else /* go to next window */
274 {
275 wp = curwin->w_next;
276 if (wp == NULL)
277 wp = firstwin; /* wrap around */
278 }
279 }
280 win_goto(wp);
281 }
282 break;
283
284/* cursor to window below */
285 case 'j':
286 case K_DOWN:
287 case Ctrl_J:
288 CHECK_CMDWIN
289#ifdef FEAT_VERTSPLIT
290 win_goto_ver(FALSE, Prenum1);
291#else
292 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
293 wp = wp->w_next)
294 ;
295 win_goto(wp);
296#endif
297 break;
298
299/* cursor to window above */
300 case 'k':
301 case K_UP:
302 case Ctrl_K:
303 CHECK_CMDWIN
304#ifdef FEAT_VERTSPLIT
305 win_goto_ver(TRUE, Prenum1);
306#else
307 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
308 wp = wp->w_prev)
309 ;
310 win_goto(wp);
311#endif
312 break;
313
314#ifdef FEAT_VERTSPLIT
315/* cursor to left window */
316 case 'h':
317 case K_LEFT:
318 case Ctrl_H:
319 case K_BS:
320 CHECK_CMDWIN
321 win_goto_hor(TRUE, Prenum1);
322 break;
323
324/* cursor to right window */
325 case 'l':
326 case K_RIGHT:
327 case Ctrl_L:
328 CHECK_CMDWIN
329 win_goto_hor(FALSE, Prenum1);
330 break;
331#endif
332
333/* cursor to top-left window */
334 case 't':
335 case Ctrl_T:
336 win_goto(firstwin);
337 break;
338
339/* cursor to bottom-right window */
340 case 'b':
341 case Ctrl_B:
342 win_goto(lastwin);
343 break;
344
345/* cursor to last accessed (previous) window */
346 case 'p':
347 case Ctrl_P:
348 if (prevwin == NULL)
349 beep_flush();
350 else
351 win_goto(prevwin);
352 break;
353
354/* exchange current and next window */
355 case 'x':
356 case Ctrl_X:
357 CHECK_CMDWIN
358 win_exchange(Prenum);
359 break;
360
361/* rotate windows downwards */
362 case Ctrl_R:
363 case 'r':
364 CHECK_CMDWIN
365#ifdef FEAT_VISUAL
366 reset_VIsual_and_resel(); /* stop Visual mode */
367#endif
368 win_rotate(FALSE, (int)Prenum1); /* downwards */
369 break;
370
371/* rotate windows upwards */
372 case 'R':
373 CHECK_CMDWIN
374#ifdef FEAT_VISUAL
375 reset_VIsual_and_resel(); /* stop Visual mode */
376#endif
377 win_rotate(TRUE, (int)Prenum1); /* upwards */
378 break;
379
380/* move window to the very top/bottom/left/right */
381 case 'K':
382 case 'J':
383#ifdef FEAT_VERTSPLIT
384 case 'H':
385 case 'L':
386#endif
387 CHECK_CMDWIN
388 win_totop((int)Prenum,
389 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
390 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
391 break;
392
393/* make all windows the same height */
394 case '=':
395#ifdef FEAT_GUI
396 need_mouse_correct = TRUE;
397#endif
398 win_equal(NULL, FALSE, 'b');
399 break;
400
401/* increase current window height */
402 case '+':
403#ifdef FEAT_GUI
404 need_mouse_correct = TRUE;
405#endif
406 win_setheight(curwin->w_height + (int)Prenum1);
407 break;
408
409/* decrease current window height */
410 case '-':
411#ifdef FEAT_GUI
412 need_mouse_correct = TRUE;
413#endif
414 win_setheight(curwin->w_height - (int)Prenum1);
415 break;
416
417/* set current window height */
418 case Ctrl__:
419 case '_':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(Prenum ? (int)Prenum : 9999);
424 break;
425
426#ifdef FEAT_VERTSPLIT
427/* increase current window width */
428 case '>':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_setwidth(curwin->w_width + (int)Prenum1);
433 break;
434
435/* decrease current window width */
436 case '<':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setwidth(curwin->w_width - (int)Prenum1);
441 break;
442
443/* set current window width */
444 case '|':
445#ifdef FEAT_GUI
446 need_mouse_correct = TRUE;
447#endif
448 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
449 break;
450#endif
451
452/* jump to tag and split window if tag exists (in preview window) */
453#if defined(FEAT_QUICKFIX)
454 case '}':
455 CHECK_CMDWIN
456 if (Prenum)
457 g_do_tagpreview = Prenum;
458 else
459 g_do_tagpreview = p_pvh;
460 /*FALLTHROUGH*/
461#endif
462 case ']':
463 case Ctrl_RSB:
464 CHECK_CMDWIN
465#ifdef FEAT_VISUAL
466 reset_VIsual_and_resel(); /* stop Visual mode */
467#endif
468 if (Prenum)
469 postponed_split = Prenum;
470 else
471 postponed_split = -1;
472
473 /* Execute the command right here, required when
474 * "wincmd ]" was used in a function. */
475 do_nv_ident(Ctrl_RSB, NUL);
476 break;
477
478#ifdef FEAT_SEARCHPATH
479/* edit file name under cursor in a new window */
480 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000481 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000483wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 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 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000489# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 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
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000594#ifdef FEAT_SEARCHPATH
595 case 'f': /* CTRL-W gf: "gf" in a new tab page */
596 cmdmod.tab = TRUE;
597 goto wingotofile;
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 default:
600 beep_flush();
601 break;
602 }
603 break;
604
605 default: beep_flush();
606 break;
607 }
608}
609
610/*
611 * split the current window, implements CTRL-W s and :split
612 *
613 * "size" is the height or width for the new window, 0 to use half of current
614 * height or width.
615 *
616 * "flags":
617 * WSP_ROOM: require enough room for new window
618 * WSP_VERT: vertical split.
619 * WSP_TOP: open window at the top-left of the shell (help window).
620 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
621 * WSP_HELP: creating the help window, keep layout snapshot
622 *
623 * return FAIL for failure, OK otherwise
624 */
625 int
626win_split(size, flags)
627 int size;
628 int flags;
629{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000630 /* When the ":tab" modifier was used open a new tab page instead. */
631 if (may_open_tabpage() == OK)
632 return OK;
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 /* Add flags from ":vertical", ":topleft" and ":botright". */
635 flags |= cmdmod.split;
636 if ((flags & WSP_TOP) && (flags & WSP_BOT))
637 {
638 EMSG(_("E442: Can't split topleft and botright at the same time"));
639 return FAIL;
640 }
641
642 /* When creating the help window make a snapshot of the window layout.
643 * Otherwise clear the snapshot, it's now invalid. */
644 if (flags & WSP_HELP)
645 make_snapshot();
646 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000647 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 return win_split_ins(size, flags, NULL, 0);
650}
651
652/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000653 * When "newwin" is NULL: split the current window in two.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 * When "newwin" is not NULL: insert this window at the far
655 * top/left/right/bottom.
656 * return FAIL for failure, OK otherwise
657 */
658 static int
659win_split_ins(size, flags, newwin, dir)
660 int size;
661 int flags;
662 win_T *newwin;
663 int dir;
664{
665 win_T *wp = newwin;
666 win_T *oldwin;
667 int new_size = size;
668 int i;
669 int need_status = 0;
670 int do_equal = FALSE;
671 int needed;
672 int available;
673 int oldwin_height = 0;
674 int layout;
675 frame_T *frp, *curfrp;
676 int before;
677
678 if (flags & WSP_TOP)
679 oldwin = firstwin;
680 else if (flags & WSP_BOT)
681 oldwin = lastwin;
682 else
683 oldwin = curwin;
684
685 /* add a status line when p_ls == 1 and splitting the first window */
686 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
687 {
688 if (oldwin->w_height <= p_wmh && newwin == NULL)
689 {
690 EMSG(_(e_noroom));
691 return FAIL;
692 }
693 need_status = STATUS_HEIGHT;
694 }
695
696#ifdef FEAT_VERTSPLIT
697 if (flags & WSP_VERT)
698 {
699 layout = FR_ROW;
700 do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
701
702 /*
703 * Check if we are able to split the current window and compute its
704 * width.
705 */
706 needed = p_wmw + 1;
707 if (flags & WSP_ROOM)
708 needed += p_wiw - p_wmw;
709 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
710 {
711 available = topframe->fr_width;
712 needed += frame_minwidth(topframe, NULL);
713 }
714 else
715 available = oldwin->w_width;
716 if (available < needed && newwin == NULL)
717 {
718 EMSG(_(e_noroom));
719 return FAIL;
720 }
721 if (new_size == 0)
722 new_size = oldwin->w_width / 2;
723 if (new_size > oldwin->w_width - p_wmw - 1)
724 new_size = oldwin->w_width - p_wmw - 1;
725 if (new_size < p_wmw)
726 new_size = p_wmw;
727
728 /* if it doesn't fit in the current window, need win_equal() */
729 if (oldwin->w_width - new_size - 1 < p_wmw)
730 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000731
732 /* We don't like to take lines for the new window from a
733 * 'winfixwidth' window. Take them from a window to the left or right
734 * instead, if possible. */
735 if (oldwin->w_p_wfw)
736 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738 else
739#endif
740 {
741 layout = FR_COL;
742 do_equal = (p_ea && new_size == 0
743#ifdef FEAT_VERTSPLIT
744 && *p_ead != 'h'
745#endif
746 );
747
748 /*
749 * Check if we are able to split the current window and compute its
750 * height.
751 */
752 needed = p_wmh + STATUS_HEIGHT + need_status;
753 if (flags & WSP_ROOM)
754 needed += p_wh - p_wmh;
755 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
756 {
757 available = topframe->fr_height;
758 needed += frame_minheight(topframe, NULL);
759 }
760 else
761 {
762 available = oldwin->w_height;
763 needed += p_wmh;
764 }
765 if (available < needed && newwin == NULL)
766 {
767 EMSG(_(e_noroom));
768 return FAIL;
769 }
770 oldwin_height = oldwin->w_height;
771 if (need_status)
772 {
773 oldwin->w_status_height = STATUS_HEIGHT;
774 oldwin_height -= STATUS_HEIGHT;
775 }
776 if (new_size == 0)
777 new_size = oldwin_height / 2;
778
779 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
780 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
781 if (new_size < p_wmh)
782 new_size = p_wmh;
783
784 /* if it doesn't fit in the current window, need win_equal() */
785 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
786 do_equal = TRUE;
787
788 /* We don't like to take lines for the new window from a
789 * 'winfixheight' window. Take them from a window above or below
790 * instead, if possible. */
791 if (oldwin->w_p_wfh)
792 {
793 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
794 oldwin);
795 oldwin_height = oldwin->w_height;
796 if (need_status)
797 oldwin_height -= STATUS_HEIGHT;
798 }
799 }
800
801 /*
802 * allocate new window structure and link it in the window list
803 */
804 if ((flags & WSP_TOP) == 0
805 && ((flags & WSP_BOT)
806 || (flags & WSP_BELOW)
807 || (!(flags & WSP_ABOVE)
808 && (
809#ifdef FEAT_VERTSPLIT
810 (flags & WSP_VERT) ? p_spr :
811#endif
812 p_sb))))
813 {
814 /* new window below/right of current one */
815 if (newwin == NULL)
816 wp = win_alloc(oldwin);
817 else
818 win_append(oldwin, wp);
819 }
820 else
821 {
822 if (newwin == NULL)
823 wp = win_alloc(oldwin->w_prev);
824 else
825 win_append(oldwin->w_prev, wp);
826 }
827
828 if (newwin == NULL)
829 {
830 if (wp == NULL)
831 return FAIL;
832
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000833 /* make the contents of the new window the same as the current one */
834 win_init(wp, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 }
836
837 /*
838 * Reorganise the tree of frames to insert the new window.
839 */
840 if (flags & (WSP_TOP | WSP_BOT))
841 {
842#ifdef FEAT_VERTSPLIT
843 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
844 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
845#else
846 if (topframe->fr_layout == FR_COL)
847#endif
848 {
849 curfrp = topframe->fr_child;
850 if (flags & WSP_BOT)
851 while (curfrp->fr_next != NULL)
852 curfrp = curfrp->fr_next;
853 }
854 else
855 curfrp = topframe;
856 before = (flags & WSP_TOP);
857 }
858 else
859 {
860 curfrp = oldwin->w_frame;
861 if (flags & WSP_BELOW)
862 before = FALSE;
863 else if (flags & WSP_ABOVE)
864 before = TRUE;
865 else
866#ifdef FEAT_VERTSPLIT
867 if (flags & WSP_VERT)
868 before = !p_spr;
869 else
870#endif
871 before = !p_sb;
872 }
873 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
874 {
875 /* Need to create a new frame in the tree to make a branch. */
876 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
877 *frp = *curfrp;
878 curfrp->fr_layout = layout;
879 frp->fr_parent = curfrp;
880 frp->fr_next = NULL;
881 frp->fr_prev = NULL;
882 curfrp->fr_child = frp;
883 curfrp->fr_win = NULL;
884 curfrp = frp;
885 if (frp->fr_win != NULL)
886 oldwin->w_frame = frp;
887 else
888 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
889 frp->fr_parent = curfrp;
890 }
891
892 if (newwin == NULL)
893 {
894 /* Create a frame for the new window. */
895 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
896 frp->fr_layout = FR_LEAF;
897 frp->fr_win = wp;
898 wp->w_frame = frp;
899 }
900 else
901 frp = newwin->w_frame;
902 frp->fr_parent = curfrp->fr_parent;
903
904 /* Insert the new frame at the right place in the frame list. */
905 if (before)
906 frame_insert(curfrp, frp);
907 else
908 frame_append(curfrp, frp);
909
910#ifdef FEAT_VERTSPLIT
911 if (flags & WSP_VERT)
912 {
913 wp->w_p_scr = curwin->w_p_scr;
914 if (need_status)
915 {
916 --oldwin->w_height;
917 oldwin->w_status_height = need_status;
918 }
919 if (flags & (WSP_TOP | WSP_BOT))
920 {
921 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000922 wp->w_winrow = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 wp->w_height = curfrp->fr_height - (p_ls > 0);
924 wp->w_status_height = (p_ls > 0);
925 }
926 else
927 {
928 /* height and row of new window is same as current window */
929 wp->w_winrow = oldwin->w_winrow;
930 wp->w_height = oldwin->w_height;
931 wp->w_status_height = oldwin->w_status_height;
932 }
933 frp->fr_height = curfrp->fr_height;
934
935 /* "new_size" of the current window goes to the new window, use
936 * one column for the vertical separator */
937 wp->w_width = new_size;
938 if (before)
939 wp->w_vsep_width = 1;
940 else
941 {
942 wp->w_vsep_width = oldwin->w_vsep_width;
943 oldwin->w_vsep_width = 1;
944 }
945 if (flags & (WSP_TOP | WSP_BOT))
946 {
947 if (flags & WSP_BOT)
948 frame_add_vsep(curfrp);
949 /* Set width of neighbor frame */
950 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000951 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
952 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000955 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (before) /* new window left of current one */
957 {
958 wp->w_wincol = oldwin->w_wincol;
959 oldwin->w_wincol += new_size + 1;
960 }
961 else /* new window right of current one */
962 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
963 frame_fix_width(oldwin);
964 frame_fix_width(wp);
965 }
966 else
967#endif
968 {
969 /* width and column of new window is same as current window */
970#ifdef FEAT_VERTSPLIT
971 if (flags & (WSP_TOP | WSP_BOT))
972 {
973 wp->w_wincol = 0;
974 wp->w_width = Columns;
975 wp->w_vsep_width = 0;
976 }
977 else
978 {
979 wp->w_wincol = oldwin->w_wincol;
980 wp->w_width = oldwin->w_width;
981 wp->w_vsep_width = oldwin->w_vsep_width;
982 }
983 frp->fr_width = curfrp->fr_width;
984#endif
985
986 /* "new_size" of the current window goes to the new window, use
987 * one row for the status line */
988 win_new_height(wp, new_size);
989 if (flags & (WSP_TOP | WSP_BOT))
990 frame_new_height(curfrp, curfrp->fr_height
991 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
992 else
993 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
994 if (before) /* new window above current one */
995 {
996 wp->w_winrow = oldwin->w_winrow;
997 wp->w_status_height = STATUS_HEIGHT;
998 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
999 }
1000 else /* new window below current one */
1001 {
1002 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1003 wp->w_status_height = oldwin->w_status_height;
1004 oldwin->w_status_height = STATUS_HEIGHT;
1005 }
1006#ifdef FEAT_VERTSPLIT
1007 if (flags & WSP_BOT)
1008 frame_add_statusline(curfrp);
1009#endif
1010 frame_fix_height(wp);
1011 frame_fix_height(oldwin);
1012 }
1013
1014 if (flags & (WSP_TOP | WSP_BOT))
1015 (void)win_comp_pos();
1016
1017 /*
1018 * Both windows need redrawing
1019 */
1020 redraw_win_later(wp, NOT_VALID);
1021 wp->w_redr_status = TRUE;
1022 redraw_win_later(oldwin, NOT_VALID);
1023 oldwin->w_redr_status = TRUE;
1024
1025 if (need_status)
1026 {
1027 msg_row = Rows - 1;
1028 msg_col = sc_col;
1029 msg_clr_eos_force(); /* Old command/ruler may still be there */
1030 comp_col();
1031 msg_row = Rows - 1;
1032 msg_col = 0; /* put position back at start of line */
1033 }
1034
1035 /*
1036 * make the new window the current window and redraw
1037 */
1038 if (do_equal || dir != 0)
1039 win_equal(wp, TRUE,
1040#ifdef FEAT_VERTSPLIT
1041 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1042 : dir == 'h' ? 'b' :
1043#endif
1044 'v');
1045
1046 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1047 * size was given. */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & WSP_VERT)
1050 {
1051 i = p_wiw;
1052 if (size != 0)
1053 p_wiw = size;
1054
1055# ifdef FEAT_GUI
1056 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1057 if (gui.in_use)
1058 gui_init_which_components(NULL);
1059# endif
1060 }
1061 else
1062#endif
1063 {
1064 i = p_wh;
1065 if (size != 0)
1066 p_wh = size;
1067 }
1068 win_enter(wp, FALSE);
1069#ifdef FEAT_VERTSPLIT
1070 if (flags & WSP_VERT)
1071 p_wiw = i;
1072 else
1073#endif
1074 p_wh = i;
1075
1076 return OK;
1077}
1078
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001079/*
1080 * Initialize window "newp" from window "oldp".
1081 * Used when splitting a window and when creating a new tab page.
1082 * The windows will both edit the same buffer.
1083 */
1084 static void
1085win_init(newp, oldp)
1086 win_T *newp;
1087 win_T *oldp;
1088{
1089 int i;
1090
1091 newp->w_buffer = oldp->w_buffer;
1092 oldp->w_buffer->b_nwindows++;
1093 newp->w_cursor = oldp->w_cursor;
1094 newp->w_valid = 0;
1095 newp->w_curswant = oldp->w_curswant;
1096 newp->w_set_curswant = oldp->w_set_curswant;
1097 newp->w_topline = oldp->w_topline;
1098#ifdef FEAT_DIFF
1099 newp->w_topfill = oldp->w_topfill;
1100#endif
1101 newp->w_leftcol = oldp->w_leftcol;
1102 newp->w_pcmark = oldp->w_pcmark;
1103 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1104 newp->w_alt_fnum = oldp->w_alt_fnum;
1105 newp->w_fraction = oldp->w_fraction;
1106 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1107#ifdef FEAT_JUMPLIST
1108 copy_jumplist(oldp, newp);
1109#endif
1110#ifdef FEAT_QUICKFIX
1111 copy_loclist(oldp, newp);
1112#endif
1113 if (oldp->w_localdir != NULL)
1114 newp->w_localdir = vim_strsave(oldp->w_localdir);
1115
1116 /* Use the same argument list. */
1117 newp->w_alist = oldp->w_alist;
1118 ++newp->w_alist->al_refcount;
1119 newp->w_arg_idx = oldp->w_arg_idx;
1120
1121 /*
1122 * copy tagstack and options from existing window
1123 */
1124 for (i = 0; i < oldp->w_tagstacklen; i++)
1125 {
1126 newp->w_tagstack[i] = oldp->w_tagstack[i];
1127 if (newp->w_tagstack[i].tagname != NULL)
1128 newp->w_tagstack[i].tagname =
1129 vim_strsave(newp->w_tagstack[i].tagname);
1130 }
1131 newp->w_tagstackidx = oldp->w_tagstackidx;
1132 newp->w_tagstacklen = oldp->w_tagstacklen;
1133 win_copy_options(oldp, newp);
1134# ifdef FEAT_FOLDING
1135 copyFoldingState(oldp, newp);
1136# endif
1137}
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif /* FEAT_WINDOWS */
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#if defined(FEAT_WINDOWS) || defined(PROTO)
1142/*
1143 * Check if "win" is a pointer to an existing window.
1144 */
1145 int
1146win_valid(win)
1147 win_T *win;
1148{
1149 win_T *wp;
1150
1151 if (win == NULL)
1152 return FALSE;
1153 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1154 if (wp == win)
1155 return TRUE;
1156 return FALSE;
1157}
1158
1159/*
1160 * Return the number of windows.
1161 */
1162 int
1163win_count()
1164{
1165 win_T *wp;
1166 int count = 0;
1167
1168 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1169 ++count;
1170 return count;
1171}
1172
1173/*
1174 * Make "count" windows on the screen.
1175 * Return actual number of windows on the screen.
1176 * Must be called when there is just one window, filling the whole screen
1177 * (excluding the command line).
1178 */
1179/*ARGSUSED*/
1180 int
1181make_windows(count, vertical)
1182 int count;
1183 int vertical; /* split windows vertically if TRUE */
1184{
1185 int maxcount;
1186 int todo;
1187
1188#ifdef FEAT_VERTSPLIT
1189 if (vertical)
1190 {
1191 /* Each windows needs at least 'winminwidth' lines and a separator
1192 * column. */
1193 maxcount = (curwin->w_width + curwin->w_vsep_width
1194 - (p_wiw - p_wmw)) / (p_wmw + 1);
1195 }
1196 else
1197#endif
1198 {
1199 /* Each window needs at least 'winminheight' lines and a status line. */
1200 maxcount = (curwin->w_height + curwin->w_status_height
1201 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1202 }
1203
1204 if (maxcount < 2)
1205 maxcount = 2;
1206 if (count > maxcount)
1207 count = maxcount;
1208
1209 /*
1210 * add status line now, otherwise first window will be too big
1211 */
1212 if (count > 1)
1213 last_status(TRUE);
1214
1215#ifdef FEAT_AUTOCMD
1216 /*
1217 * Don't execute autocommands while creating the windows. Must do that
1218 * when putting the buffers in the windows.
1219 */
1220 ++autocmd_block;
1221#endif
1222
1223 /* todo is number of windows left to create */
1224 for (todo = count - 1; todo > 0; --todo)
1225#ifdef FEAT_VERTSPLIT
1226 if (vertical)
1227 {
1228 if (win_split(curwin->w_width - (curwin->w_width - todo)
1229 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1230 break;
1231 }
1232 else
1233#endif
1234 {
1235 if (win_split(curwin->w_height - (curwin->w_height - todo
1236 * STATUS_HEIGHT) / (todo + 1)
1237 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1238 break;
1239 }
1240
1241#ifdef FEAT_AUTOCMD
1242 --autocmd_block;
1243#endif
1244
1245 /* return actual number of windows */
1246 return (count - todo);
1247}
1248
1249/*
1250 * Exchange current and next window
1251 */
1252 static void
1253win_exchange(Prenum)
1254 long Prenum;
1255{
1256 frame_T *frp;
1257 frame_T *frp2;
1258 win_T *wp;
1259 win_T *wp2;
1260 int temp;
1261
1262 if (lastwin == firstwin) /* just one window */
1263 {
1264 beep_flush();
1265 return;
1266 }
1267
1268#ifdef FEAT_GUI
1269 need_mouse_correct = TRUE;
1270#endif
1271
1272 /*
1273 * find window to exchange with
1274 */
1275 if (Prenum)
1276 {
1277 frp = curwin->w_frame->fr_parent->fr_child;
1278 while (frp != NULL && --Prenum > 0)
1279 frp = frp->fr_next;
1280 }
1281 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1282 frp = curwin->w_frame->fr_next;
1283 else /* Swap last window in row/col with previous */
1284 frp = curwin->w_frame->fr_prev;
1285
1286 /* We can only exchange a window with another window, not with a frame
1287 * containing windows. */
1288 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1289 return;
1290 wp = frp->fr_win;
1291
1292/*
1293 * 1. remove curwin from the list. Remember after which window it was in wp2
1294 * 2. insert curwin before wp in the list
1295 * if wp != wp2
1296 * 3. remove wp from the list
1297 * 4. insert wp after wp2
1298 * 5. exchange the status line height and vsep width.
1299 */
1300 wp2 = curwin->w_prev;
1301 frp2 = curwin->w_frame->fr_prev;
1302 if (wp->w_prev != curwin)
1303 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001304 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 frame_remove(curwin->w_frame);
1306 win_append(wp->w_prev, curwin);
1307 frame_insert(frp, curwin->w_frame);
1308 }
1309 if (wp != wp2)
1310 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001311 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 frame_remove(wp->w_frame);
1313 win_append(wp2, wp);
1314 if (frp2 == NULL)
1315 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1316 else
1317 frame_append(frp2, wp->w_frame);
1318 }
1319 temp = curwin->w_status_height;
1320 curwin->w_status_height = wp->w_status_height;
1321 wp->w_status_height = temp;
1322#ifdef FEAT_VERTSPLIT
1323 temp = curwin->w_vsep_width;
1324 curwin->w_vsep_width = wp->w_vsep_width;
1325 wp->w_vsep_width = temp;
1326
1327 /* If the windows are not in the same frame, exchange the sizes to avoid
1328 * messing up the window layout. Otherwise fix the frame sizes. */
1329 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1330 {
1331 temp = curwin->w_height;
1332 curwin->w_height = wp->w_height;
1333 wp->w_height = temp;
1334 temp = curwin->w_width;
1335 curwin->w_width = wp->w_width;
1336 wp->w_width = temp;
1337 }
1338 else
1339 {
1340 frame_fix_height(curwin);
1341 frame_fix_height(wp);
1342 frame_fix_width(curwin);
1343 frame_fix_width(wp);
1344 }
1345#endif
1346
1347 (void)win_comp_pos(); /* recompute window positions */
1348
1349 win_enter(wp, TRUE);
1350 redraw_later(CLEAR);
1351}
1352
1353/*
1354 * rotate windows: if upwards TRUE the second window becomes the first one
1355 * if upwards FALSE the first window becomes the second one
1356 */
1357 static void
1358win_rotate(upwards, count)
1359 int upwards;
1360 int count;
1361{
1362 win_T *wp1;
1363 win_T *wp2;
1364 frame_T *frp;
1365 int n;
1366
1367 if (firstwin == lastwin) /* nothing to do */
1368 {
1369 beep_flush();
1370 return;
1371 }
1372
1373#ifdef FEAT_GUI
1374 need_mouse_correct = TRUE;
1375#endif
1376
1377#ifdef FEAT_VERTSPLIT
1378 /* Check if all frames in this row/col have one window. */
1379 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1380 frp = frp->fr_next)
1381 if (frp->fr_win == NULL)
1382 {
1383 EMSG(_("E443: Cannot rotate when another window is split"));
1384 return;
1385 }
1386#endif
1387
1388 while (count--)
1389 {
1390 if (upwards) /* first window becomes last window */
1391 {
1392 /* remove first window/frame from the list */
1393 frp = curwin->w_frame->fr_parent->fr_child;
1394 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 frame_remove(frp);
1397
1398 /* find last frame and append removed window/frame after it */
1399 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1400 ;
1401 win_append(frp->fr_win, wp1);
1402 frame_append(frp, wp1->w_frame);
1403
1404 wp2 = frp->fr_win; /* previously last window */
1405 }
1406 else /* last window becomes first window */
1407 {
1408 /* find last window/frame in the list and remove it */
1409 for (frp = curwin->w_frame; frp->fr_next != NULL;
1410 frp = frp->fr_next)
1411 ;
1412 wp1 = frp->fr_win;
1413 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 frame_remove(frp);
1416
1417 /* append the removed window/frame before the first in the list */
1418 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1419 frame_insert(frp->fr_parent->fr_child, frp);
1420 }
1421
1422 /* exchange status height and vsep width of old and new last window */
1423 n = wp2->w_status_height;
1424 wp2->w_status_height = wp1->w_status_height;
1425 wp1->w_status_height = n;
1426 frame_fix_height(wp1);
1427 frame_fix_height(wp2);
1428#ifdef FEAT_VERTSPLIT
1429 n = wp2->w_vsep_width;
1430 wp2->w_vsep_width = wp1->w_vsep_width;
1431 wp1->w_vsep_width = n;
1432 frame_fix_width(wp1);
1433 frame_fix_width(wp2);
1434#endif
1435
1436 /* recompute w_winrow and w_wincol for all windows */
1437 (void)win_comp_pos();
1438 }
1439
1440 redraw_later(CLEAR);
1441}
1442
1443/*
1444 * Move the current window to the very top/bottom/left/right of the screen.
1445 */
1446 static void
1447win_totop(size, flags)
1448 int size;
1449 int flags;
1450{
1451 int dir;
1452 int height = curwin->w_height;
1453
1454 if (lastwin == firstwin)
1455 {
1456 beep_flush();
1457 return;
1458 }
1459
1460 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001461 (void)winframe_remove(curwin, &dir, NULL);
1462 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 last_status(FALSE); /* may need to remove last status line */
1464 (void)win_comp_pos(); /* recompute window positions */
1465
1466 /* Split a window on the desired side and put the window there. */
1467 (void)win_split_ins(size, flags, curwin, dir);
1468 if (!(flags & WSP_VERT))
1469 {
1470 win_setheight(height);
1471 if (p_ea)
1472 win_equal(curwin, TRUE, 'v');
1473 }
1474
1475#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1476 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1477 * scrollbars. Have to update them anyway. */
1478 if (gui.in_use)
1479 {
1480 out_flush();
1481 gui_init_which_components(NULL);
1482 gui_update_scrollbars(TRUE);
1483 }
1484 need_mouse_correct = TRUE;
1485#endif
1486
1487}
1488
1489/*
1490 * Move window "win1" to below/right of "win2" and make "win1" the current
1491 * window. Only works within the same frame!
1492 */
1493 void
1494win_move_after(win1, win2)
1495 win_T *win1, *win2;
1496{
1497 int height;
1498
1499 /* check if the arguments are reasonable */
1500 if (win1 == win2)
1501 return;
1502
1503 /* check if there is something to do */
1504 if (win2->w_next != win1)
1505 {
1506 /* may need move the status line/vertical separator of the last window
1507 * */
1508 if (win1 == lastwin)
1509 {
1510 height = win1->w_prev->w_status_height;
1511 win1->w_prev->w_status_height = win1->w_status_height;
1512 win1->w_status_height = height;
1513#ifdef FEAT_VERTSPLIT
1514 win1->w_prev->w_vsep_width = 0;
1515 win1->w_vsep_width = 1;
1516#endif
1517 }
1518 else if (win2 == lastwin)
1519 {
1520 height = win1->w_status_height;
1521 win1->w_status_height = win2->w_status_height;
1522 win2->w_status_height = height;
1523#ifdef FEAT_VERTSPLIT
1524 win2->w_vsep_width = 1;
1525 win1->w_vsep_width = 0;
1526#endif
1527 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001528 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 frame_remove(win1->w_frame);
1530 win_append(win2, win1);
1531 frame_append(win2->w_frame, win1->w_frame);
1532
1533 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1534 redraw_later(NOT_VALID);
1535 }
1536 win_enter(win1, FALSE);
1537}
1538
1539/*
1540 * Make all windows the same height.
1541 * 'next_curwin' will soon be the current window, make sure it has enough
1542 * rows.
1543 */
1544 void
1545win_equal(next_curwin, current, dir)
1546 win_T *next_curwin; /* pointer to current window to be or NULL */
1547 int current; /* do only frame with current window */
1548 int dir; /* 'v' for vertically, 'h' for horizontally,
1549 'b' for both, 0 for using p_ead */
1550{
1551 if (dir == 0)
1552#ifdef FEAT_VERTSPLIT
1553 dir = *p_ead;
1554#else
1555 dir = 'b';
1556#endif
1557 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001558 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001559 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560}
1561
1562/*
1563 * Set a frame to a new position and height, spreading the available room
1564 * equally over contained frames.
1565 * The window "next_curwin" (if not NULL) should at least get the size from
1566 * 'winheight' and 'winwidth' if possible.
1567 */
1568 static void
1569win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1570 win_T *next_curwin; /* pointer to current window to be or NULL */
1571 int current; /* do only frame with current window */
1572 frame_T *topfr; /* frame to set size off */
1573 int dir; /* 'v', 'h' or 'b', see win_equal() */
1574 int col; /* horizontal position for frame */
1575 int row; /* vertical position for frame */
1576 int width; /* new width of frame */
1577 int height; /* new height of frame */
1578{
1579 int n, m;
1580 int extra_sep = 0;
1581 int wincount, totwincount = 0;
1582 frame_T *fr;
1583 int next_curwin_size = 0;
1584 int room = 0;
1585 int new_size;
1586 int has_next_curwin = 0;
1587 int hnc;
1588
1589 if (topfr->fr_layout == FR_LEAF)
1590 {
1591 /* Set the width/height of this frame.
1592 * Redraw when size or position changes */
1593 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1594#ifdef FEAT_VERTSPLIT
1595 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1596#endif
1597 )
1598 {
1599 topfr->fr_win->w_winrow = row;
1600 frame_new_height(topfr, height, FALSE, FALSE);
1601#ifdef FEAT_VERTSPLIT
1602 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001603 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604#endif
1605 redraw_all_later(CLEAR);
1606 }
1607 }
1608#ifdef FEAT_VERTSPLIT
1609 else if (topfr->fr_layout == FR_ROW)
1610 {
1611 topfr->fr_width = width;
1612 topfr->fr_height = height;
1613
1614 if (dir != 'v') /* equalize frame widths */
1615 {
1616 /* Compute the maximum number of windows horizontally in this
1617 * frame. */
1618 n = frame_minwidth(topfr, NOWIN);
1619 /* add one for the rightmost window, it doesn't have a separator */
1620 if (col + width == Columns)
1621 extra_sep = 1;
1622 else
1623 extra_sep = 0;
1624 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001625 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001627 /*
1628 * Compute width for "next_curwin" window and room available for
1629 * other windows.
1630 * "m" is the minimal width when counting p_wiw for "next_curwin".
1631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 m = frame_minwidth(topfr, next_curwin);
1633 room = width - m;
1634 if (room < 0)
1635 {
1636 next_curwin_size = p_wiw + room;
1637 room = 0;
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 else
1640 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001641 next_curwin_size = -1;
1642 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1643 {
1644 /* If 'winfixwidth' set keep the window width if
1645 * possible.
1646 * Watch out for this window being the next_curwin. */
1647 if (frame_fixed_width(fr))
1648 {
1649 n = frame_minwidth(fr, NOWIN);
1650 new_size = fr->fr_width;
1651 if (frame_has_win(fr, next_curwin))
1652 {
1653 room += p_wiw - p_wmw;
1654 next_curwin_size = 0;
1655 if (new_size < p_wiw)
1656 new_size = p_wiw;
1657 }
1658 else
1659 /* These windows don't use up room. */
1660 totwincount -= (n + (fr->fr_next == NULL
1661 ? extra_sep : 0)) / (p_wmw + 1);
1662 room -= new_size - n;
1663 if (room < 0)
1664 {
1665 new_size += room;
1666 room = 0;
1667 }
1668 fr->fr_newwidth = new_size;
1669 }
1670 }
1671 if (next_curwin_size == -1)
1672 {
1673 if (!has_next_curwin)
1674 next_curwin_size = 0;
1675 else if (totwincount > 1
1676 && (room + (totwincount - 2))
1677 / (totwincount - 1) > p_wiw)
1678 {
1679 next_curwin_size = (room + p_wiw + totwincount * p_wmw
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001681 room -= next_curwin_size - p_wiw;
1682 }
1683 else
1684 next_curwin_size = p_wiw;
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001687
1688 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 --totwincount; /* don't count curwin */
1690 }
1691
1692 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1693 {
1694 n = m = 0;
1695 wincount = 1;
1696 if (fr->fr_next == NULL)
1697 /* last frame gets all that remains (avoid roundoff error) */
1698 new_size = width;
1699 else if (dir == 'v')
1700 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001701 else if (frame_fixed_width(fr))
1702 {
1703 new_size = fr->fr_newwidth;
1704 wincount = 0; /* doesn't count as a sizeable window */
1705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 else
1707 {
1708 /* Compute the maximum number of windows horiz. in "fr". */
1709 n = frame_minwidth(fr, NOWIN);
1710 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1711 / (p_wmw + 1);
1712 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001713 if (has_next_curwin)
1714 hnc = frame_has_win(fr, next_curwin);
1715 else
1716 hnc = FALSE;
1717 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001719 if (totwincount == 0)
1720 new_size = room;
1721 else
1722 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001724 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 {
1726 next_curwin_size -= p_wiw - (m - n);
1727 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001728 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001730 else
1731 room -= new_size;
1732 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001735 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 * window, unless equalizing all frames. */
1737 if (!current || dir != 'v' || topfr->fr_parent != NULL
1738 || (new_size != fr->fr_width)
1739 || frame_has_win(fr, next_curwin))
1740 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001741 new_size, height);
1742 col += new_size;
1743 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 totwincount -= wincount;
1745 }
1746 }
1747#endif
1748 else /* topfr->fr_layout == FR_COL */
1749 {
1750#ifdef FEAT_VERTSPLIT
1751 topfr->fr_width = width;
1752#endif
1753 topfr->fr_height = height;
1754
1755 if (dir != 'h') /* equalize frame heights */
1756 {
1757 /* Compute maximum number of windows vertically in this frame. */
1758 n = frame_minheight(topfr, NOWIN);
1759 /* add one for the bottom window if it doesn't have a statusline */
1760 if (row + height == cmdline_row && p_ls == 0)
1761 extra_sep = 1;
1762 else
1763 extra_sep = 0;
1764 totwincount = (n + extra_sep) / (p_wmh + 1);
1765 has_next_curwin = frame_has_win(topfr, next_curwin);
1766
1767 /*
1768 * Compute height for "next_curwin" window and room available for
1769 * other windows.
1770 * "m" is the minimal height when counting p_wh for "next_curwin".
1771 */
1772 m = frame_minheight(topfr, next_curwin);
1773 room = height - m;
1774 if (room < 0)
1775 {
1776 /* The room is less then 'winheight', use all space for the
1777 * current window. */
1778 next_curwin_size = p_wh + room;
1779 room = 0;
1780 }
1781 else
1782 {
1783 next_curwin_size = -1;
1784 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1785 {
1786 /* If 'winfixheight' set keep the window height if
1787 * possible.
1788 * Watch out for this window being the next_curwin. */
1789 if (frame_fixed_height(fr))
1790 {
1791 n = frame_minheight(fr, NOWIN);
1792 new_size = fr->fr_height;
1793 if (frame_has_win(fr, next_curwin))
1794 {
1795 room += p_wh - p_wmh;
1796 next_curwin_size = 0;
1797 if (new_size < p_wh)
1798 new_size = p_wh;
1799 }
1800 else
1801 /* These windows don't use up room. */
1802 totwincount -= (n + (fr->fr_next == NULL
1803 ? extra_sep : 0)) / (p_wmh + 1);
1804 room -= new_size - n;
1805 if (room < 0)
1806 {
1807 new_size += room;
1808 room = 0;
1809 }
1810 fr->fr_newheight = new_size;
1811 }
1812 }
1813 if (next_curwin_size == -1)
1814 {
1815 if (!has_next_curwin)
1816 next_curwin_size = 0;
1817 else if (totwincount > 1
1818 && (room + (totwincount - 2))
1819 / (totwincount - 1) > p_wh)
1820 {
1821 next_curwin_size = (room + p_wh + totwincount * p_wmh
1822 + (totwincount - 1)) / totwincount;
1823 room -= next_curwin_size - p_wh;
1824 }
1825 else
1826 next_curwin_size = p_wh;
1827 }
1828 }
1829
1830 if (has_next_curwin)
1831 --totwincount; /* don't count curwin */
1832 }
1833
1834 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1835 {
1836 n = m = 0;
1837 wincount = 1;
1838 if (fr->fr_next == NULL)
1839 /* last frame gets all that remains (avoid roundoff error) */
1840 new_size = height;
1841 else if (dir == 'h')
1842 new_size = fr->fr_height;
1843 else if (frame_fixed_height(fr))
1844 {
1845 new_size = fr->fr_newheight;
1846 wincount = 0; /* doesn't count as a sizeable window */
1847 }
1848 else
1849 {
1850 /* Compute the maximum number of windows vert. in "fr". */
1851 n = frame_minheight(fr, NOWIN);
1852 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1853 / (p_wmh + 1);
1854 m = frame_minheight(fr, next_curwin);
1855 if (has_next_curwin)
1856 hnc = frame_has_win(fr, next_curwin);
1857 else
1858 hnc = FALSE;
1859 if (hnc) /* don't count next_curwin */
1860 --wincount;
1861 if (totwincount == 0)
1862 new_size = room;
1863 else
1864 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1865 / totwincount;
1866 if (hnc) /* add next_curwin size */
1867 {
1868 next_curwin_size -= p_wh - (m - n);
1869 new_size += next_curwin_size;
1870 room -= new_size - next_curwin_size;
1871 }
1872 else
1873 room -= new_size;
1874 new_size += n;
1875 }
1876 /* Skip frame that is full width when splitting or closing a
1877 * window, unless equalizing all frames. */
1878 if (!current || dir != 'h' || topfr->fr_parent != NULL
1879 || (new_size != fr->fr_height)
1880 || frame_has_win(fr, next_curwin))
1881 win_equal_rec(next_curwin, current, fr, dir, col, row,
1882 width, new_size);
1883 row += new_size;
1884 height -= new_size;
1885 totwincount -= wincount;
1886 }
1887 }
1888}
1889
1890/*
1891 * close all windows for buffer 'buf'
1892 */
1893 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00001894close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001896 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaarf740b292006-02-16 22:11:02 +00001898 win_T *wp;
1899 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001900 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001903
1904 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001906 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001908 win_close(wp, FALSE);
1909
1910 /* Start all over, autocommands may change the window layout. */
1911 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00001914 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001916
1917 /* Also check windows in other tab pages. */
1918 for (tp = first_tabpage; tp != NULL; tp = nexttp)
1919 {
1920 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001921 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001922 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1923 if (wp->w_buffer == buf)
1924 {
1925 win_close_othertab(wp, FALSE, tp);
1926
1927 /* Start all over, the tab page may be closed and
1928 * autocommands may change the window layout. */
1929 nexttp = first_tabpage;
1930 break;
1931 }
1932 }
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001935
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001936 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00001937 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938}
1939
1940/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001941 * Return TRUE if the current window is the only window that exists.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001942 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001943 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001944 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001945last_window()
1946{
1947 return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1948}
1949
1950/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001951 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 * If "free_buf" is TRUE related buffer may be unloaded.
1953 *
1954 * called by :quit, :close, :xit, :wq and findtag()
1955 */
1956 void
1957win_close(win, free_buf)
1958 win_T *win;
1959 int free_buf;
1960{
1961 win_T *wp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001962 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963#ifdef FEAT_AUTOCMD
1964 int other_buffer = FALSE;
1965#endif
1966 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 int dir;
1968 int help_window = FALSE;
1969
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001970 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
1972 EMSG(_("E444: Cannot close last window"));
1973 return;
1974 }
1975
1976 /* When closing the help window, try restoring a snapshot after closing
1977 * the window. Otherwise clear the snapshot, it's now invalid. */
1978 if (win->w_buffer->b_help)
1979 help_window = TRUE;
1980 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001981 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983#ifdef FEAT_AUTOCMD
1984 if (win == curwin)
1985 {
1986 /*
1987 * Guess which window is going to be the new current window.
1988 * This may change because of the autocommands (sigh).
1989 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001990 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 /*
1993 * Be careful: If autocommands delete the window, return now.
1994 */
1995 if (wp->w_buffer != curbuf)
1996 {
1997 other_buffer = TRUE;
1998 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001999 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return;
2001 }
2002 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002003 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 return;
2005# ifdef FEAT_EVAL
2006 /* autocmds may abort script processing */
2007 if (aborting())
2008 return;
2009# endif
2010 }
2011#endif
2012
2013 /*
2014 * Close the link to the buffer.
2015 */
2016 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2017 /* Autocommands may have closed the window already, or closed the only
2018 * other window. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002019 if (!win_valid(win) || last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 return;
2021
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002022 /* Free the memory used for the window. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023 wp = win_free_mem(win, &dir, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002025 /* When closing the last window in a tab page go to another tab page. */
2026 if (wp == NULL)
2027 {
2028 tabpage_T *ptp = NULL;
2029 tabpage_T *tp;
2030 tabpage_T *atp = alt_tabpage();
2031
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002032 for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002033 ptp = tp;
2034 if (tp == NULL)
2035 {
2036 EMSG2(_(e_intern2), "win_close()");
2037 return;
2038 }
2039 if (ptp == NULL)
2040 first_tabpage = tp->tp_next;
2041 else
2042 ptp->tp_next = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002043 free_tabpage(tp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002044
2045 /* We don't do the window resizing stuff, let enter_tabpage() take
2046 * care of entering a window in another tab page. */
2047 enter_tabpage(atp, old_curbuf);
2048 return;
2049 }
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 /* Make sure curwin isn't invalid. It can cause severe trouble when
2052 * printing an error message. For win_equal() curbuf needs to be valid
2053 * too. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002054 else if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 curwin = wp;
2057#ifdef FEAT_QUICKFIX
2058 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2059 {
2060 /*
2061 * The cursor goes to the preview or the quickfix window, try
2062 * finding another window to go to.
2063 */
2064 for (;;)
2065 {
2066 if (wp->w_next == NULL)
2067 wp = firstwin;
2068 else
2069 wp = wp->w_next;
2070 if (wp == curwin)
2071 break;
2072 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2073 {
2074 curwin = wp;
2075 break;
2076 }
2077 }
2078 }
2079#endif
2080 curbuf = curwin->w_buffer;
2081 close_curwin = TRUE;
2082 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002083 if (p_ea
2084#ifdef FEAT_VERTSPLIT
2085 && (*p_ead == 'b' || *p_ead == dir)
2086#endif
2087 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 win_equal(curwin, TRUE,
2089#ifdef FEAT_VERTSPLIT
2090 dir
2091#else
2092 0
2093#endif
2094 );
2095 else
2096 win_comp_pos();
2097 if (close_curwin)
2098 {
2099 win_enter_ext(wp, FALSE, TRUE);
2100#ifdef FEAT_AUTOCMD
2101 if (other_buffer)
2102 /* careful: after this wp and win may be invalid! */
2103 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2104#endif
2105 }
2106
2107 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002108 * If last window has a status line now and we don't want one,
2109 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
2111 last_status(FALSE);
2112
2113 /* After closing the help window, try restoring the window layout from
2114 * before it was opened. */
2115 if (help_window)
2116 restore_snapshot(close_curwin);
2117
2118#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2119 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2120 if (gui.in_use && !win_hasvertsplit())
2121 gui_init_which_components(NULL);
2122#endif
2123
2124 redraw_all_later(NOT_VALID);
2125}
2126
2127/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002128 * Close window "win" in tab page "tp", which is not the current tab page.
2129 * This may be the last window ih that tab page and result in closing the tab,
2130 * thus "tp" may become invalid!
2131 * Called must check if buffer is hidden.
2132 */
2133 void
2134win_close_othertab(win, free_buf, tp)
2135 win_T *win;
2136 int free_buf;
2137 tabpage_T *tp;
2138{
2139 win_T *wp;
2140 int dir;
2141 tabpage_T *ptp = NULL;
2142
2143 /* Close the link to the buffer. */
2144 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2145
2146 /* Careful: Autocommands may have closed the tab page or made it the
2147 * current tab page. */
2148 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2149 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002150 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002151 return;
2152
2153 /* Autocommands may have closed the window already. */
2154 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2155 ;
2156 if (wp == NULL)
2157 return;
2158
2159 /* Free the memory used for the window. */
2160 wp = win_free_mem(win, &dir, tp);
2161
2162 /* When closing the last window in a tab page remove the tab page. */
2163 if (wp == NULL)
2164 {
2165 if (tp == first_tabpage)
2166 first_tabpage = tp->tp_next;
2167 else
2168 {
2169 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2170 ptp = ptp->tp_next)
2171 ;
2172 if (ptp == NULL)
2173 {
2174 EMSG2(_(e_intern2), "win_close_othertab()");
2175 return;
2176 }
2177 ptp->tp_next = tp->tp_next;
2178 }
2179 vim_free(tp);
2180 }
2181}
2182
2183/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002184 * Free the memory used for a window.
2185 * Returns a pointer to the window that got the freed up space.
2186 */
2187 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002188win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002189 win_T *win;
2190 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002191 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002192{
2193 frame_T *frp;
2194 win_T *wp;
2195
Bram Moolenaarea408852005-06-25 22:49:46 +00002196#ifdef FEAT_FOLDING
2197 clearFolding(win);
2198#endif
2199
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002200 /* reduce the reference count to the argument list. */
2201 alist_unlink(win->w_alist);
2202
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002203 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002204 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002205 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002206 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002207 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002208
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209 /* When deleting the current window of another tab page select a new
2210 * current window. */
2211 if (tp != NULL && win == tp->tp_curwin)
2212 tp->tp_curwin = wp;
2213
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002214 return wp;
2215}
2216
2217#if defined(EXITFREE) || defined(PROTO)
2218 void
2219win_free_all()
2220{
2221 int dummy;
2222
Bram Moolenaarf740b292006-02-16 22:11:02 +00002223# ifdef FEAT_WINDOWS
2224 while (first_tabpage->tp_next != NULL)
2225 tabpage_close(TRUE);
2226# endif
2227
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002228 while (firstwin != NULL)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002229 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002230}
2231#endif
2232
2233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * Remove a window and its frame from the tree of frames.
2235 * Returns a pointer to the window that got the freed up space.
2236 */
2237/*ARGSUSED*/
2238 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002239winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 win_T *win;
2241 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002242 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 frame_T *frp, *frp2, *frp3;
2245 frame_T *frp_close = win->w_frame;
2246 win_T *wp;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002247 int old_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002250 * If there is only one window there is nothing to remove.
2251 */
2252 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2253 return NULL;
2254
2255 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * Remove the window from its frame.
2257 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002258 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 wp = frame2win(frp2);
2260
2261 /* Remove this frame from the list of frames. */
2262 frame_remove(frp_close);
2263
2264#ifdef FEAT_VERTSPLIT
2265 if (frp_close->fr_parent->fr_layout == FR_COL)
2266 {
2267#endif
2268 /* When 'winfixheight' is set, remember its old size and restore
2269 * it later (it's a simplistic solution...). Don't do this if the
2270 * window will occupy the full height of the screen. */
2271 if (frp2->fr_win != NULL
2272 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2273 && frp2->fr_win->w_p_wfh)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002274 old_size = frp2->fr_win->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2276 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002277 if (old_size != 0)
2278 win_setheight_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279#ifdef FEAT_VERTSPLIT
2280 *dirp = 'v';
2281 }
2282 else
2283 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002284 /* When 'winfixwidth' is set, remember its old size and restore
2285 * it later (it's a simplistic solution...). Don't do this if the
2286 * window will occupy the full width of the screen. */
2287 if (frp2->fr_win != NULL
2288 && (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2289 && frp2->fr_win->w_p_wfw)
2290 old_size = frp2->fr_win->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002292 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2293 if (old_size != 0)
2294 win_setwidth_win(old_size, frp2->fr_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 *dirp = 'h';
2296 }
2297#endif
2298
2299 /* If rows/columns go to a window below/right its positions need to be
2300 * updated. Can only be done after the sizes have been updated. */
2301 if (frp2 == frp_close->fr_next)
2302 {
2303 int row = win->w_winrow;
2304 int col = W_WINCOL(win);
2305
2306 frame_comp_pos(frp2, &row, &col);
2307 }
2308
2309 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2310 {
2311 /* There is no other frame in this list, move its info to the parent
2312 * and remove it. */
2313 frp2->fr_parent->fr_layout = frp2->fr_layout;
2314 frp2->fr_parent->fr_child = frp2->fr_child;
2315 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2316 frp->fr_parent = frp2->fr_parent;
2317 frp2->fr_parent->fr_win = frp2->fr_win;
2318 if (frp2->fr_win != NULL)
2319 frp2->fr_win->w_frame = frp2->fr_parent;
2320 frp = frp2->fr_parent;
2321 vim_free(frp2);
2322
2323 frp2 = frp->fr_parent;
2324 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2325 {
2326 /* The frame above the parent has the same layout, have to merge
2327 * the frames into this list. */
2328 if (frp2->fr_child == frp)
2329 frp2->fr_child = frp->fr_child;
2330 frp->fr_child->fr_prev = frp->fr_prev;
2331 if (frp->fr_prev != NULL)
2332 frp->fr_prev->fr_next = frp->fr_child;
2333 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2334 {
2335 frp3->fr_parent = frp2;
2336 if (frp3->fr_next == NULL)
2337 {
2338 frp3->fr_next = frp->fr_next;
2339 if (frp->fr_next != NULL)
2340 frp->fr_next->fr_prev = frp3;
2341 break;
2342 }
2343 }
2344 vim_free(frp);
2345 }
2346 }
2347
2348 return wp;
2349}
2350
2351/*
2352 * Find out which frame is going to get the freed up space when "win" is
2353 * closed.
2354 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2355 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2356 * This makes opening a window and closing it immediately keep the same window
2357 * layout.
2358 */
2359 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002360win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002362 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
2364 frame_T *frp;
2365 int b;
2366
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002368 /* Last window in this tab page, will go to next tab page. */
2369 return alt_tabpage()->tp_curwin->w_frame;
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 frp = win->w_frame;
2372#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002373 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 b = p_spr;
2375 else
2376#endif
2377 b = p_sb;
2378 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2379 return frp->fr_next;
2380 return frp->fr_prev;
2381}
2382
2383/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002384 * Return the tabpage that will be used if the current one is closed.
2385 */
2386 static tabpage_T *
2387alt_tabpage()
2388{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002389 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002390
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002391 /* Use the next tab page if possible. */
2392 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002393 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002394
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002395 /* Find the last but one tab page. */
2396 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2397 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002398 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002399}
2400
2401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 * Find the left-upper window in frame "frp".
2403 */
2404 static win_T *
2405frame2win(frp)
2406 frame_T *frp;
2407{
2408 while (frp->fr_win == NULL)
2409 frp = frp->fr_child;
2410 return frp->fr_win;
2411}
2412
2413/*
2414 * Return TRUE if frame "frp" contains window "wp".
2415 */
2416 static int
2417frame_has_win(frp, wp)
2418 frame_T *frp;
2419 win_T *wp;
2420{
2421 frame_T *p;
2422
2423 if (frp->fr_layout == FR_LEAF)
2424 return frp->fr_win == wp;
2425
2426 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2427 if (frame_has_win(p, wp))
2428 return TRUE;
2429 return FALSE;
2430}
2431
2432/*
2433 * Set a new height for a frame. Recursively sets the height for contained
2434 * frames and windows. Caller must take care of positions.
2435 */
2436 static void
2437frame_new_height(topfrp, height, topfirst, wfh)
2438 frame_T *topfrp;
2439 int height;
2440 int topfirst; /* resize topmost contained frame first */
2441 int wfh; /* obey 'winfixheight' when there is a choice;
2442 may cause the height not to be set */
2443{
2444 frame_T *frp;
2445 int extra_lines;
2446 int h;
2447
2448 if (topfrp->fr_win != NULL)
2449 {
2450 /* Simple case: just one window. */
2451 win_new_height(topfrp->fr_win,
2452 height - topfrp->fr_win->w_status_height);
2453 }
2454#ifdef FEAT_VERTSPLIT
2455 else if (topfrp->fr_layout == FR_ROW)
2456 {
2457 do
2458 {
2459 /* All frames in this row get the same new height. */
2460 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2461 {
2462 frame_new_height(frp, height, topfirst, wfh);
2463 if (frp->fr_height > height)
2464 {
2465 /* Could not fit the windows, make the whole row higher. */
2466 height = frp->fr_height;
2467 break;
2468 }
2469 }
2470 }
2471 while (frp != NULL);
2472 }
2473#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002474 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 /* Complicated case: Resize a column of frames. Resize the bottom
2477 * frame first, frames above that when needed. */
2478
2479 frp = topfrp->fr_child;
2480 if (wfh)
2481 /* Advance past frames with one window with 'wfh' set. */
2482 while (frame_fixed_height(frp))
2483 {
2484 frp = frp->fr_next;
2485 if (frp == NULL)
2486 return; /* no frame without 'wfh', give up */
2487 }
2488 if (!topfirst)
2489 {
2490 /* Find the bottom frame of this column */
2491 while (frp->fr_next != NULL)
2492 frp = frp->fr_next;
2493 if (wfh)
2494 /* Advance back for frames with one window with 'wfh' set. */
2495 while (frame_fixed_height(frp))
2496 frp = frp->fr_prev;
2497 }
2498
2499 extra_lines = height - topfrp->fr_height;
2500 if (extra_lines < 0)
2501 {
2502 /* reduce height of contained frames, bottom or top frame first */
2503 while (frp != NULL)
2504 {
2505 h = frame_minheight(frp, NULL);
2506 if (frp->fr_height + extra_lines < h)
2507 {
2508 extra_lines += frp->fr_height - h;
2509 frame_new_height(frp, h, topfirst, wfh);
2510 }
2511 else
2512 {
2513 frame_new_height(frp, frp->fr_height + extra_lines,
2514 topfirst, wfh);
2515 break;
2516 }
2517 if (topfirst)
2518 {
2519 do
2520 frp = frp->fr_next;
2521 while (wfh && frp != NULL && frame_fixed_height(frp));
2522 }
2523 else
2524 {
2525 do
2526 frp = frp->fr_prev;
2527 while (wfh && frp != NULL && frame_fixed_height(frp));
2528 }
2529 /* Increase "height" if we could not reduce enough frames. */
2530 if (frp == NULL)
2531 height -= extra_lines;
2532 }
2533 }
2534 else if (extra_lines > 0)
2535 {
2536 /* increase height of bottom or top frame */
2537 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2538 }
2539 }
2540 topfrp->fr_height = height;
2541}
2542
2543/*
2544 * Return TRUE if height of frame "frp" should not be changed because of
2545 * the 'winfixheight' option.
2546 */
2547 static int
2548frame_fixed_height(frp)
2549 frame_T *frp;
2550{
2551 /* frame with one window: fixed height if 'winfixheight' set. */
2552 if (frp->fr_win != NULL)
2553 return frp->fr_win->w_p_wfh;
2554
2555 if (frp->fr_layout == FR_ROW)
2556 {
2557 /* The frame is fixed height if one of the frames in the row is fixed
2558 * height. */
2559 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2560 if (frame_fixed_height(frp))
2561 return TRUE;
2562 return FALSE;
2563 }
2564
2565 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2566 * frames in the row are fixed height. */
2567 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2568 if (!frame_fixed_height(frp))
2569 return FALSE;
2570 return TRUE;
2571}
2572
2573#ifdef FEAT_VERTSPLIT
2574/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002575 * Return TRUE if width of frame "frp" should not be changed because of
2576 * the 'winfixwidth' option.
2577 */
2578 static int
2579frame_fixed_width(frp)
2580 frame_T *frp;
2581{
2582 /* frame with one window: fixed width if 'winfixwidth' set. */
2583 if (frp->fr_win != NULL)
2584 return frp->fr_win->w_p_wfw;
2585
2586 if (frp->fr_layout == FR_COL)
2587 {
2588 /* The frame is fixed width if one of the frames in the row is fixed
2589 * width. */
2590 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2591 if (frame_fixed_width(frp))
2592 return TRUE;
2593 return FALSE;
2594 }
2595
2596 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2597 * frames in the row are fixed width. */
2598 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2599 if (!frame_fixed_width(frp))
2600 return FALSE;
2601 return TRUE;
2602}
2603
2604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 * Add a status line to windows at the bottom of "frp".
2606 * Note: Does not check if there is room!
2607 */
2608 static void
2609frame_add_statusline(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_status_height == 0)
2618 {
2619 if (wp->w_height > 0) /* don't make it negative */
2620 --wp->w_height;
2621 wp->w_status_height = STATUS_HEIGHT;
2622 }
2623 }
2624 else if (frp->fr_layout == FR_ROW)
2625 {
2626 /* Handle all the frames in the row. */
2627 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2628 frame_add_statusline(frp);
2629 }
2630 else /* frp->fr_layout == FR_COL */
2631 {
2632 /* Only need to handle the last frame in the column. */
2633 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2634 ;
2635 frame_add_statusline(frp);
2636 }
2637}
2638
2639/*
2640 * Set width of a frame. Handles recursively going through contained frames.
2641 * May remove separator line for windows at the right side (for win_close()).
2642 */
2643 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002644frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 frame_T *topfrp;
2646 int width;
2647 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002648 int wfw; /* obey 'winfixwidth' when there is a choice;
2649 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 frame_T *frp;
2652 int extra_cols;
2653 int w;
2654 win_T *wp;
2655
2656 if (topfrp->fr_layout == FR_LEAF)
2657 {
2658 /* Simple case: just one window. */
2659 wp = topfrp->fr_win;
2660 /* Find out if there are any windows right of this one. */
2661 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2662 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2663 break;
2664 if (frp->fr_parent == NULL)
2665 wp->w_vsep_width = 0;
2666 win_new_width(wp, width - wp->w_vsep_width);
2667 }
2668 else if (topfrp->fr_layout == FR_COL)
2669 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002670 do
2671 {
2672 /* All frames in this column get the same new width. */
2673 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2674 {
2675 frame_new_width(frp, width, leftfirst, wfw);
2676 if (frp->fr_width > width)
2677 {
2678 /* Could not fit the windows, make whole column wider. */
2679 width = frp->fr_width;
2680 break;
2681 }
2682 }
2683 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 else /* fr_layout == FR_ROW */
2686 {
2687 /* Complicated case: Resize a row of frames. Resize the rightmost
2688 * frame first, frames left of it when needed. */
2689
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002691 if (wfw)
2692 /* Advance past frames with one window with 'wfw' set. */
2693 while (frame_fixed_width(frp))
2694 {
2695 frp = frp->fr_next;
2696 if (frp == NULL)
2697 return; /* no frame without 'wfw', give up */
2698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002700 {
2701 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 while (frp->fr_next != NULL)
2703 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002704 if (wfw)
2705 /* Advance back for frames with one window with 'wfw' set. */
2706 while (frame_fixed_width(frp))
2707 frp = frp->fr_prev;
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 extra_cols = width - topfrp->fr_width;
2711 if (extra_cols < 0)
2712 {
2713 /* reduce frame width, rightmost frame first */
2714 while (frp != NULL)
2715 {
2716 w = frame_minwidth(frp, NULL);
2717 if (frp->fr_width + extra_cols < w)
2718 {
2719 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002720 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 else
2723 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002724 frame_new_width(frp, frp->fr_width + extra_cols,
2725 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 break;
2727 }
2728 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002729 {
2730 do
2731 frp = frp->fr_next;
2732 while (wfw && frp != NULL && frame_fixed_width(frp));
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002735 {
2736 do
2737 frp = frp->fr_prev;
2738 while (wfw && frp != NULL && frame_fixed_width(frp));
2739 }
2740 /* Increase "width" if we could not reduce enough frames. */
2741 if (frp == NULL)
2742 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 }
2745 else if (extra_cols > 0)
2746 {
2747 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002748 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
2750 }
2751 topfrp->fr_width = width;
2752}
2753
2754/*
2755 * Add the vertical separator to windows at the right side of "frp".
2756 * Note: Does not check if there is room!
2757 */
2758 static void
2759frame_add_vsep(frp)
2760 frame_T *frp;
2761{
2762 win_T *wp;
2763
2764 if (frp->fr_layout == FR_LEAF)
2765 {
2766 wp = frp->fr_win;
2767 if (wp->w_vsep_width == 0)
2768 {
2769 if (wp->w_width > 0) /* don't make it negative */
2770 --wp->w_width;
2771 wp->w_vsep_width = 1;
2772 }
2773 }
2774 else if (frp->fr_layout == FR_COL)
2775 {
2776 /* Handle all the frames in the column. */
2777 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2778 frame_add_vsep(frp);
2779 }
2780 else /* frp->fr_layout == FR_ROW */
2781 {
2782 /* Only need to handle the last frame in the row. */
2783 frp = frp->fr_child;
2784 while (frp->fr_next != NULL)
2785 frp = frp->fr_next;
2786 frame_add_vsep(frp);
2787 }
2788}
2789
2790/*
2791 * Set frame width from the window it contains.
2792 */
2793 static void
2794frame_fix_width(wp)
2795 win_T *wp;
2796{
2797 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2798}
2799#endif
2800
2801/*
2802 * Set frame height from the window it contains.
2803 */
2804 static void
2805frame_fix_height(wp)
2806 win_T *wp;
2807{
2808 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2809}
2810
2811/*
2812 * Compute the minimal height for frame "topfrp".
2813 * Uses the 'winminheight' option.
2814 * When "next_curwin" isn't NULL, use p_wh for this window.
2815 * When "next_curwin" is NOWIN, don't use at least one line for the current
2816 * window.
2817 */
2818 static int
2819frame_minheight(topfrp, next_curwin)
2820 frame_T *topfrp;
2821 win_T *next_curwin;
2822{
2823 frame_T *frp;
2824 int m;
2825#ifdef FEAT_VERTSPLIT
2826 int n;
2827#endif
2828
2829 if (topfrp->fr_win != NULL)
2830 {
2831 if (topfrp->fr_win == next_curwin)
2832 m = p_wh + topfrp->fr_win->w_status_height;
2833 else
2834 {
2835 /* window: minimal height of the window plus status line */
2836 m = p_wmh + topfrp->fr_win->w_status_height;
2837 /* Current window is minimal one line high */
2838 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2839 ++m;
2840 }
2841 }
2842#ifdef FEAT_VERTSPLIT
2843 else if (topfrp->fr_layout == FR_ROW)
2844 {
2845 /* get the minimal height from each frame in this row */
2846 m = 0;
2847 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2848 {
2849 n = frame_minheight(frp, next_curwin);
2850 if (n > m)
2851 m = n;
2852 }
2853 }
2854#endif
2855 else
2856 {
2857 /* Add up the minimal heights for all frames in this column. */
2858 m = 0;
2859 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2860 m += frame_minheight(frp, next_curwin);
2861 }
2862
2863 return m;
2864}
2865
2866#ifdef FEAT_VERTSPLIT
2867/*
2868 * Compute the minimal width for frame "topfrp".
2869 * When "next_curwin" isn't NULL, use p_wiw for this window.
2870 * When "next_curwin" is NOWIN, don't use at least one column for the current
2871 * window.
2872 */
2873 static int
2874frame_minwidth(topfrp, next_curwin)
2875 frame_T *topfrp;
2876 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
2877{
2878 frame_T *frp;
2879 int m, n;
2880
2881 if (topfrp->fr_win != NULL)
2882 {
2883 if (topfrp->fr_win == next_curwin)
2884 m = p_wiw + topfrp->fr_win->w_vsep_width;
2885 else
2886 {
2887 /* window: minimal width of the window plus separator column */
2888 m = p_wmw + topfrp->fr_win->w_vsep_width;
2889 /* Current window is minimal one column wide */
2890 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2891 ++m;
2892 }
2893 }
2894 else if (topfrp->fr_layout == FR_COL)
2895 {
2896 /* get the minimal width from each frame in this column */
2897 m = 0;
2898 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2899 {
2900 n = frame_minwidth(frp, next_curwin);
2901 if (n > m)
2902 m = n;
2903 }
2904 }
2905 else
2906 {
2907 /* Add up the minimal widths for all frames in this row. */
2908 m = 0;
2909 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2910 m += frame_minwidth(frp, next_curwin);
2911 }
2912
2913 return m;
2914}
2915#endif
2916
2917
2918/*
2919 * Try to close all windows except current one.
2920 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2921 * used and the buffer was modified.
2922 *
2923 * Used by ":bdel" and ":only".
2924 */
2925 void
2926close_others(message, forceit)
2927 int message;
2928 int forceit; /* always hide all other windows */
2929{
2930 win_T *wp;
2931 win_T *nextwp;
2932 int r;
2933
2934 if (lastwin == firstwin)
2935 {
2936 if (message
2937#ifdef FEAT_AUTOCMD
2938 && !autocmd_busy
2939#endif
2940 )
2941 MSG(_("Already only one window"));
2942 return;
2943 }
2944
2945 /* Be very careful here: autocommands may change the window layout. */
2946 for (wp = firstwin; win_valid(wp); wp = nextwp)
2947 {
2948 nextwp = wp->w_next;
2949 if (wp != curwin) /* don't close current window */
2950 {
2951
2952 /* Check if it's allowed to abandon this window */
2953 r = can_abandon(wp->w_buffer, forceit);
2954#ifdef FEAT_AUTOCMD
2955 if (!win_valid(wp)) /* autocommands messed wp up */
2956 {
2957 nextwp = firstwin;
2958 continue;
2959 }
2960#endif
2961 if (!r)
2962 {
2963#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2964 if (message && (p_confirm || cmdmod.confirm) && p_write)
2965 {
2966 dialog_changed(wp->w_buffer, FALSE);
2967# ifdef FEAT_AUTOCMD
2968 if (!win_valid(wp)) /* autocommands messed wp up */
2969 {
2970 nextwp = firstwin;
2971 continue;
2972 }
2973# endif
2974 }
2975 if (bufIsChanged(wp->w_buffer))
2976#endif
2977 continue;
2978 }
2979 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2980 }
2981 }
2982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002983 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 EMSG(_("E445: Other window contains changes"));
2985}
2986
2987#endif /* FEAT_WINDOWS */
2988
2989/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002990 * Init the current window "curwin".
2991 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002994curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00002996 redraw_win_later(curwin, NOT_VALID);
2997 curwin->w_lines_valid = 0;
2998 curwin->w_cursor.lnum = 1;
2999 curwin->w_curswant = curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003001 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003003 curwin->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3004 curwin->w_pcmark.col = 0;
3005 curwin->w_prev_pcmark.lnum = 0;
3006 curwin->w_prev_pcmark.col = 0;
3007 curwin->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#ifdef FEAT_DIFF
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003009 curwin->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003011 curwin->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#ifdef FEAT_FKMAP
3013 if (curwin->w_p_rl)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003014 curwin->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003016 curwin->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017#endif
3018}
3019
3020/*
3021 * Allocate the first window and put an empty buffer in it.
3022 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003023 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026win_alloc_first()
3027{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003028 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003029 return FAIL;
3030
3031#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003032 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003033 if (first_tabpage == NULL)
3034 return FAIL;
3035 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003036 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003037#endif
3038 return OK;
3039}
3040
3041/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003042 * Allocate the first window or the first window in a new tab page.
3043 * When "oldwin" is NULL create an empty buffer for it.
3044 * When "oldwin" is not NULL copy info from it to the new window (only with
3045 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003046 * Return FAIL when something goes wrong (out of memory).
3047 */
3048 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003049win_alloc_firstwin(oldwin)
3050 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003051{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 curwin = win_alloc(NULL);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003053 if (oldwin == NULL)
3054 {
3055 /* Very first window, need to create an empty buffer for it and
3056 * initialize from scratch. */
3057 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3058 if (curwin == NULL || curbuf == NULL)
3059 return FAIL;
3060 curwin->w_buffer = curbuf;
3061 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003063 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003065 curwin_init(); /* init current window */
3066 }
3067#ifdef FEAT_WINDOWS
3068 else
3069 {
3070 /* First window in new tab page, initialize it from "oldwin". */
3071 win_init(curwin, oldwin);
3072
3073# ifdef FEAT_SCROLLBIND
3074 /* We don't want scroll-binding in the first window. */
3075 curwin->w_p_scb = FALSE;
3076# endif
3077 }
3078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3081 if (topframe == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003082 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 topframe->fr_layout = FR_LEAF;
3084#ifdef FEAT_VERTSPLIT
3085 topframe->fr_width = Columns;
3086#endif
3087 topframe->fr_height = Rows - p_ch;
3088 topframe->fr_win = curwin;
3089 curwin->w_frame = topframe;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003090
3091 return OK;
3092}
3093
3094/*
3095 * Initialize the window and frame size to the maximum.
3096 */
3097 void
3098win_init_size()
3099{
3100 firstwin->w_height = ROWS_AVAIL;
3101 topframe->fr_height = ROWS_AVAIL;
3102#ifdef FEAT_VERTSPLIT
3103 firstwin->w_width = Columns;
3104 topframe->fr_width = Columns;
3105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
3108#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003109
3110/*
3111 * Allocate a new tabpage_T and init the values.
3112 * Returns NULL when out of memory.
3113 */
3114 static tabpage_T *
3115alloc_tabpage()
3116{
3117 tabpage_T *tp;
3118
3119 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3120 if (tp != NULL)
3121 {
Bram Moolenaar371d5402006-03-20 21:47:49 +00003122# ifdef FEAT_GUI
3123 int i;
3124
3125 for (i = 0; i < 3; i++)
3126 tp->tp_prev_which_scrollbars[i] = -1;
3127# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003128# ifdef FEAT_DIFF
3129 tp->tp_diff_invalid = TRUE;
3130# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003131#ifdef FEAT_EVAL
3132 /* init t: variables */
3133 init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3134#endif
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003135 tp->tp_ch_used = p_ch;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003136 }
3137 return tp;
3138}
3139
3140 static void
3141free_tabpage(tp)
3142 tabpage_T *tp;
3143{
3144# ifdef FEAT_DIFF
3145 diff_clear(tp);
3146# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003147 clear_snapshot(tp);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003148#ifdef FEAT_EVAL
3149 vars_clear(&tp->tp_vars.dv_hashtab); /* free all t: variables */
3150#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003151 vim_free(tp);
3152}
3153
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003154/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003155 * Create a new Tab page with one window.
3156 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003157 * When "after" is 0 put it just after the current Tab page.
3158 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003159 * Return FAIL or OK.
3160 */
3161 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003162win_new_tabpage(after)
3163 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003164{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003165 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003166 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003167 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003168
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003169 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003170 if (newtp == NULL)
3171 return FAIL;
3172
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003173 /* Remember the current windows in this Tab page. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003174 if (leave_tabpage(curbuf) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003175 {
3176 vim_free(newtp);
3177 return FAIL;
3178 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003179 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003180
3181 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003182 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003183 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003184 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003185 if (after == 1)
3186 {
3187 /* New tab page becomes the first one. */
3188 newtp->tp_next = first_tabpage;
3189 first_tabpage = newtp;
3190 }
3191 else
3192 {
3193 if (after > 0)
3194 {
3195 /* Put new tab page before tab page "after". */
3196 n = 2;
3197 for (tp = first_tabpage; tp->tp_next != NULL
3198 && n < after; tp = tp->tp_next)
3199 ++n;
3200 }
3201 newtp->tp_next = tp->tp_next;
3202 tp->tp_next = newtp;
3203 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003204 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003205 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003206 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003207
3208 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003209 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003210
3211#if defined(FEAT_GUI)
3212 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3213 * scrollbars. Have to update them anyway. */
3214 if (gui.in_use && starting == 0)
3215 {
3216 gui_init_which_components(NULL);
3217 gui_update_scrollbars(TRUE);
3218 }
3219 need_mouse_correct = TRUE;
3220#endif
3221
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003222 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003223#ifdef FEAT_AUTOCMD
3224 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3225 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3226#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003227 return OK;
3228 }
3229
3230 /* Failed, get back the previous Tab page */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003231 enter_tabpage(curtab, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003232 return FAIL;
3233}
3234
3235/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003236 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3237 * like with ":split".
3238 * Returns OK if a new tab page was created, FAIL otherwise.
3239 */
3240 int
3241may_open_tabpage()
3242{
3243 int n = cmdmod.tab;
3244
3245 if (cmdmod.tab != 0)
3246 {
3247 cmdmod.tab = 0; /* reset it to avoid doing it twice */
3248 return win_new_tabpage(n);
3249 }
3250 return FAIL;
3251}
3252
3253/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003254 * Create up to "maxcount" tabpages with empty windows.
3255 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003256 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003257 int
3258make_tabpages(maxcount)
3259 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003260{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003261 int count = maxcount;
3262 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003263
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003264 /* Limit to 'tabpagemax' tabs. */
3265 if (count > p_tpm)
3266 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003267
3268#ifdef FEAT_AUTOCMD
3269 /*
3270 * Don't execute autocommands while creating the tab pages. Must do that
3271 * when putting the buffers in the windows.
3272 */
3273 ++autocmd_block;
3274#endif
3275
3276 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003277 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003278 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003279
3280#ifdef FEAT_AUTOCMD
3281 --autocmd_block;
3282#endif
3283
3284 /* return actual number of tab pages */
3285 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003286}
3287
3288/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003289 * Return TRUE when "tpc" points to a valid tab page.
3290 */
3291 int
3292valid_tabpage(tpc)
3293 tabpage_T *tpc;
3294{
3295 tabpage_T *tp;
3296
3297 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3298 if (tp == tpc)
3299 return TRUE;
3300 return FALSE;
3301}
3302
3303/*
3304 * Find tab page "n" (first one is 1). Returns NULL when not found.
3305 */
3306 tabpage_T *
3307find_tabpage(n)
3308 int n;
3309{
3310 tabpage_T *tp;
3311 int i = 1;
3312
3313 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3314 ++i;
3315 return tp;
3316}
3317
3318/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003319 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003320 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003321 */
3322 int
3323tabpage_index(ftp)
3324 tabpage_T *ftp;
3325{
3326 int i = 1;
3327 tabpage_T *tp;
3328
3329 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3330 ++i;
3331 return i;
3332}
3333
3334/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003335 * Prepare for leaving the current tab page.
3336 * When autocomands change "curtab" we don't leave the tab page and return
3337 * FAIL.
3338 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003340/*ARGSUSED*/
3341 static int
3342leave_tabpage(new_curbuf)
3343 buf_T *new_curbuf; /* what is going to be the new curbuf,
3344 NULL if unknown */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003346 tabpage_T *tp = curtab;
3347
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003348#ifdef FEAT_VISUAL
3349 reset_VIsual_and_resel(); /* stop Visual mode */
3350#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003351#ifdef FEAT_AUTOCMD
3352 if (new_curbuf != curbuf)
3353 {
3354 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3355 if (curtab != tp)
3356 return FAIL;
3357 }
3358 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3359 if (curtab != tp)
3360 return FAIL;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003361 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003362 if (curtab != tp)
3363 return FAIL;
3364#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003365#if defined(FEAT_GUI)
3366 /* Remove the scrollbars. They may be added back later. */
3367 if (gui.in_use)
3368 gui_remove_scrollbars();
3369#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003370 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003371 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372 tp->tp_firstwin = firstwin;
3373 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003374 tp->tp_old_Rows = Rows;
3375 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003376 firstwin = NULL;
3377 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003378 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379}
3380
3381/*
3382 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003383 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384 */
3385/*ARGSUSED*/
3386 static void
3387enter_tabpage(tp, old_curbuf)
3388 tabpage_T *tp;
3389 buf_T *old_curbuf;
3390{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003391 int old_off = tp->tp_firstwin->w_winrow;
3392
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394 firstwin = tp->tp_firstwin;
3395 lastwin = tp->tp_lastwin;
3396 topframe = tp->tp_topframe;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397#ifdef FEAT_AUTOCMD
3398 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3399#endif
3400
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003401 win_enter_ext(tp->tp_curwin, FALSE, TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003402 prevwin = tp->tp_prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003403
3404#ifdef FEAT_AUTOCMD
3405 if (old_curbuf != curbuf)
3406 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3407#endif
3408
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003409 last_status(FALSE); /* status line may appear or disappear */
3410 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411 must_redraw = CLEAR; /* need to redraw everything */
3412#ifdef FEAT_DIFF
3413 diff_need_scrollbind = TRUE;
3414#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003416 /* The tabpage line may have appeared or disappeared, may need to resize
3417 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003418 * frame sizes too. Use the stored value of p_ch, so that it can be
3419 * different for each tab page. */
3420 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003421 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3422#ifdef FEAT_GUI_TABLINE
3423 && !gui_use_tabline()
3424#endif
3425 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003426 shell_new_rows();
3427#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003428 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003429 shell_new_columns(); /* update window widths */
3430#endif
3431
3432#if defined(FEAT_GUI)
3433 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3434 * scrollbars. Have to update them anyway. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003435 if (gui.in_use && starting == 0)
Bram Moolenaar371d5402006-03-20 21:47:49 +00003436 {
3437 gui_init_which_components(NULL);
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003438 gui_update_scrollbars(TRUE);
Bram Moolenaar371d5402006-03-20 21:47:49 +00003439 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003440 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441#endif
3442
3443 redraw_all_later(CLEAR);
3444}
3445
3446/*
3447 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003448 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003449 */
3450 void
3451goto_tabpage(n)
3452 int n;
3453{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003455 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003456 int i;
3457
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458 /* If there is only one it can't work. */
3459 if (first_tabpage->tp_next == NULL)
3460 {
3461 if (n > 1)
3462 beep_flush();
3463 return;
3464 }
3465
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 if (n == 0)
3467 {
3468 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003469 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470 tp = first_tabpage;
3471 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003472 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003474 else if (n < 0)
3475 {
3476 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3477 * this N times. */
3478 ttp = curtab;
3479 for (i = n; i < 0; ++i)
3480 {
3481 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3482 tp = tp->tp_next)
3483 ;
3484 ttp = tp;
3485 }
3486 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003487 else if (n == 9999)
3488 {
3489 /* Go to last tab page. */
3490 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3491 ;
3492 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003493 else
3494 {
3495 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003496 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003497 if (tp == NULL)
3498 {
3499 beep_flush();
3500 return;
3501 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502 }
3503
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003504 goto_tabpage_tp(tp);
3505
3506#ifdef FEAT_GUI_TABLINE
3507 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003508 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003509#endif
3510}
3511
3512/*
3513 * Go to tabpage "tp".
3514 * Note: doesn't update the GUI tab.
3515 */
3516 void
3517goto_tabpage_tp(tp)
3518 tabpage_T *tp;
3519{
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003520 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003521 {
3522 if (valid_tabpage(tp))
3523 enter_tabpage(tp, curbuf);
3524 else
3525 enter_tabpage(curtab, curbuf);
3526 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003530 * Move the current tab page to before tab page "nr".
3531 */
3532 void
3533tabpage_move(nr)
3534 int nr;
3535{
3536 int n = nr;
3537 tabpage_T *tp;
3538
3539 if (first_tabpage->tp_next == NULL)
3540 return;
3541
3542 /* Remove the current tab page from the list of tab pages. */
3543 if (curtab == first_tabpage)
3544 first_tabpage = curtab->tp_next;
3545 else
3546 {
3547 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3548 if (tp->tp_next == curtab)
3549 break;
3550 if (tp == NULL) /* "cannot happen" */
3551 return;
3552 tp->tp_next = curtab->tp_next;
3553 }
3554
3555 /* Re-insert it at the specified position. */
3556 if (n == 0)
3557 {
3558 curtab->tp_next = first_tabpage;
3559 first_tabpage = curtab;
3560 }
3561 else
3562 {
3563 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3564 --n;
3565 curtab->tp_next = tp->tp_next;
3566 tp->tp_next = curtab;
3567 }
3568
3569 /* Need to redraw the tabline. Tab page contents doesn't change. */
3570 redraw_tabline = TRUE;
3571}
3572
3573
3574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 * Go to another window.
3576 * When jumping to another buffer, stop Visual mode. Do this before
3577 * changing windows so we can yank the selection into the '*' register.
3578 * When jumping to another window on the same buffer, adjust its cursor
3579 * position to keep the same Visual area.
3580 */
3581 void
3582win_goto(wp)
3583 win_T *wp;
3584{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003585 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003588 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 return;
3590 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003591#ifdef FEAT_AUTOCMD
3592 if (curbuf_locked())
3593 return;
3594#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#ifdef FEAT_VISUAL
3597 if (wp->w_buffer != curbuf)
3598 reset_VIsual_and_resel();
3599 else if (VIsual_active)
3600 wp->w_cursor = curwin->w_cursor;
3601#endif
3602
3603#ifdef FEAT_GUI
3604 need_mouse_correct = TRUE;
3605#endif
3606 win_enter(wp, TRUE);
3607}
3608
3609#if defined(FEAT_PERL) || defined(PROTO)
3610/*
3611 * Find window number "winnr" (counting top to bottom).
3612 */
3613 win_T *
3614win_find_nr(winnr)
3615 int winnr;
3616{
3617 win_T *wp;
3618
3619# ifdef FEAT_WINDOWS
3620 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3621 if (--winnr == 0)
3622 break;
3623 return wp;
3624# else
3625 return curwin;
3626# endif
3627}
3628#endif
3629
3630#ifdef FEAT_VERTSPLIT
3631/*
3632 * Move to window above or below "count" times.
3633 */
3634 static void
3635win_goto_ver(up, count)
3636 int up; /* TRUE to go to win above */
3637 long count;
3638{
3639 frame_T *fr;
3640 frame_T *nfr;
3641 frame_T *foundfr;
3642
3643 foundfr = curwin->w_frame;
3644 while (count--)
3645 {
3646 /*
3647 * First go upwards in the tree of frames until we find a upwards or
3648 * downwards neighbor.
3649 */
3650 fr = foundfr;
3651 for (;;)
3652 {
3653 if (fr == topframe)
3654 goto end;
3655 if (up)
3656 nfr = fr->fr_prev;
3657 else
3658 nfr = fr->fr_next;
3659 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3660 break;
3661 fr = fr->fr_parent;
3662 }
3663
3664 /*
3665 * Now go downwards to find the bottom or top frame in it.
3666 */
3667 for (;;)
3668 {
3669 if (nfr->fr_layout == FR_LEAF)
3670 {
3671 foundfr = nfr;
3672 break;
3673 }
3674 fr = nfr->fr_child;
3675 if (nfr->fr_layout == FR_ROW)
3676 {
3677 /* Find the frame at the cursor row. */
3678 while (fr->fr_next != NULL
3679 && frame2win(fr)->w_wincol + fr->fr_width
3680 <= curwin->w_wincol + curwin->w_wcol)
3681 fr = fr->fr_next;
3682 }
3683 if (nfr->fr_layout == FR_COL && up)
3684 while (fr->fr_next != NULL)
3685 fr = fr->fr_next;
3686 nfr = fr;
3687 }
3688 }
3689end:
3690 if (foundfr != NULL)
3691 win_goto(foundfr->fr_win);
3692}
3693
3694/*
3695 * Move to left or right window.
3696 */
3697 static void
3698win_goto_hor(left, count)
3699 int left; /* TRUE to go to left win */
3700 long count;
3701{
3702 frame_T *fr;
3703 frame_T *nfr;
3704 frame_T *foundfr;
3705
3706 foundfr = curwin->w_frame;
3707 while (count--)
3708 {
3709 /*
3710 * First go upwards in the tree of frames until we find a left or
3711 * right neighbor.
3712 */
3713 fr = foundfr;
3714 for (;;)
3715 {
3716 if (fr == topframe)
3717 goto end;
3718 if (left)
3719 nfr = fr->fr_prev;
3720 else
3721 nfr = fr->fr_next;
3722 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3723 break;
3724 fr = fr->fr_parent;
3725 }
3726
3727 /*
3728 * Now go downwards to find the leftmost or rightmost frame in it.
3729 */
3730 for (;;)
3731 {
3732 if (nfr->fr_layout == FR_LEAF)
3733 {
3734 foundfr = nfr;
3735 break;
3736 }
3737 fr = nfr->fr_child;
3738 if (nfr->fr_layout == FR_COL)
3739 {
3740 /* Find the frame at the cursor row. */
3741 while (fr->fr_next != NULL
3742 && frame2win(fr)->w_winrow + fr->fr_height
3743 <= curwin->w_winrow + curwin->w_wrow)
3744 fr = fr->fr_next;
3745 }
3746 if (nfr->fr_layout == FR_ROW && left)
3747 while (fr->fr_next != NULL)
3748 fr = fr->fr_next;
3749 nfr = fr;
3750 }
3751 }
3752end:
3753 if (foundfr != NULL)
3754 win_goto(foundfr->fr_win);
3755}
3756#endif
3757
3758/*
3759 * Make window "wp" the current window.
3760 */
3761 void
3762win_enter(wp, undo_sync)
3763 win_T *wp;
3764 int undo_sync;
3765{
3766 win_enter_ext(wp, undo_sync, FALSE);
3767}
3768
3769/*
3770 * Make window wp the current window.
3771 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3772 * been closed and isn't valid.
3773 */
3774 static void
3775win_enter_ext(wp, undo_sync, curwin_invalid)
3776 win_T *wp;
3777 int undo_sync;
3778 int curwin_invalid;
3779{
3780#ifdef FEAT_AUTOCMD
3781 int other_buffer = FALSE;
3782#endif
3783
3784 if (wp == curwin && !curwin_invalid) /* nothing to do */
3785 return;
3786
3787#ifdef FEAT_AUTOCMD
3788 if (!curwin_invalid)
3789 {
3790 /*
3791 * Be careful: If autocommands delete the window, return now.
3792 */
3793 if (wp->w_buffer != curbuf)
3794 {
3795 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3796 other_buffer = TRUE;
3797 if (!win_valid(wp))
3798 return;
3799 }
3800 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3801 if (!win_valid(wp))
3802 return;
3803# ifdef FEAT_EVAL
3804 /* autocmds may abort script processing */
3805 if (aborting())
3806 return;
3807# endif
3808 }
3809#endif
3810
3811 /* sync undo before leaving the current buffer */
3812 if (undo_sync && curbuf != wp->w_buffer)
3813 u_sync();
3814 /* may have to copy the buffer options when 'cpo' contains 'S' */
3815 if (wp->w_buffer != curbuf)
3816 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3817 if (!curwin_invalid)
3818 {
3819 prevwin = curwin; /* remember for CTRL-W p */
3820 curwin->w_redr_status = TRUE;
3821 }
3822 curwin = wp;
3823 curbuf = wp->w_buffer;
3824 check_cursor();
3825#ifdef FEAT_VIRTUALEDIT
3826 if (!virtual_active())
3827 curwin->w_cursor.coladd = 0;
3828#endif
3829 changed_line_abv_curs(); /* assume cursor position needs updating */
3830
3831 if (curwin->w_localdir != NULL)
3832 {
3833 /* Window has a local directory: Save current directory as global
3834 * directory (unless that was done already) and change to the local
3835 * directory. */
3836 if (globaldir == NULL)
3837 {
3838 char_u cwd[MAXPATHL];
3839
3840 if (mch_dirname(cwd, MAXPATHL) == OK)
3841 globaldir = vim_strsave(cwd);
3842 }
3843 mch_chdir((char *)curwin->w_localdir);
3844 shorten_fnames(TRUE);
3845 }
3846 else if (globaldir != NULL)
3847 {
3848 /* Window doesn't have a local directory and we are not in the global
3849 * directory: Change to the global directory. */
3850 mch_chdir((char *)globaldir);
3851 vim_free(globaldir);
3852 globaldir = NULL;
3853 shorten_fnames(TRUE);
3854 }
3855
3856#ifdef FEAT_AUTOCMD
3857 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3858 if (other_buffer)
3859 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3860#endif
3861
3862#ifdef FEAT_TITLE
3863 maketitle();
3864#endif
3865 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003866 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (restart_edit)
3868 redraw_later(VALID); /* causes status line redraw */
3869
3870 /* set window height to desired minimal value */
3871 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3872 win_setheight((int)p_wh);
3873 else if (curwin->w_height == 0)
3874 win_setheight(1);
3875
3876#ifdef FEAT_VERTSPLIT
3877 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003878 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 win_setwidth((int)p_wiw);
3880#endif
3881
3882#ifdef FEAT_MOUSE
3883 setmouse(); /* in case jumped to/from help buffer */
3884#endif
3885
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003886#ifdef FEAT_AUTOCHDIR
3887 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * switching windows. */
3889 if (p_acd && curbuf->b_ffname != NULL
3890 && vim_chdirfile(curbuf->b_ffname) == OK)
3891 shorten_fnames(TRUE);
3892#endif
3893}
3894
3895#endif /* FEAT_WINDOWS */
3896
3897#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3898/*
3899 * Jump to the first open window that contains buffer buf if one exists
3900 * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3901 * Returns pointer to window if it exists, otherwise NULL.
3902 */
3903 win_T *
3904buf_jump_open_win(buf)
3905 buf_T *buf;
3906{
3907# ifdef FEAT_WINDOWS
3908 win_T *wp;
3909
3910 for (wp = firstwin; wp; wp = wp->w_next)
3911 if (wp->w_buffer == buf)
3912 break;
3913 if (wp != NULL)
3914 win_enter(wp, FALSE);
3915 return wp;
3916# else
3917 if (curwin->w_buffer == buf)
3918 return curwin;
3919 return NULL;
3920# endif
3921}
3922#endif
3923
3924/*
3925 * allocate a window structure and link it in the window list
3926 */
3927/*ARGSUSED*/
3928 static win_T *
3929win_alloc(after)
3930 win_T *after;
3931{
3932 win_T *newwin;
3933
3934 /*
3935 * allocate window structure and linesizes arrays
3936 */
3937 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3938 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3939 {
3940 vim_free(newwin);
3941 newwin = NULL;
3942 }
3943
3944 if (newwin != NULL)
3945 {
3946 /*
3947 * link the window in the window list
3948 */
3949#ifdef FEAT_WINDOWS
3950 win_append(after, newwin);
3951#endif
3952#ifdef FEAT_VERTSPLIT
3953 newwin->w_wincol = 0;
3954 newwin->w_width = Columns;
3955#endif
3956
3957 /* position the display and the cursor at the top of the file. */
3958 newwin->w_topline = 1;
3959#ifdef FEAT_DIFF
3960 newwin->w_topfill = 0;
3961#endif
3962 newwin->w_botline = 2;
3963 newwin->w_cursor.lnum = 1;
3964#ifdef FEAT_SCROLLBIND
3965 newwin->w_scbind_pos = 1;
3966#endif
3967
3968 /* We won't calculate w_fraction until resizing the window */
3969 newwin->w_fraction = 0;
3970 newwin->w_prev_fraction_row = -1;
3971
3972#ifdef FEAT_GUI
3973 if (gui.in_use)
3974 {
3975 out_flush();
3976 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3977 SBAR_LEFT, newwin);
3978 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3979 SBAR_RIGHT, newwin);
3980 }
3981#endif
3982#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00003983 /* init w: variables */
3984 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#endif
3986#ifdef FEAT_FOLDING
3987 foldInitWin(newwin);
3988#endif
3989 }
3990 return newwin;
3991}
3992
3993#if defined(FEAT_WINDOWS) || defined(PROTO)
3994
3995/*
3996 * remove window 'wp' from the window list and free the structure
3997 */
3998 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00003999win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004001 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int i;
4004
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004005#ifdef FEAT_MZSCHEME
4006 mzscheme_window_free(wp);
4007#endif
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#ifdef FEAT_PERL
4010 perl_win_free(wp);
4011#endif
4012
4013#ifdef FEAT_PYTHON
4014 python_window_free(wp);
4015#endif
4016
4017#ifdef FEAT_TCL
4018 tcl_window_free(wp);
4019#endif
4020
4021#ifdef FEAT_RUBY
4022 ruby_window_free(wp);
4023#endif
4024
4025 clear_winopt(&wp->w_onebuf_opt);
4026 clear_winopt(&wp->w_allbuf_opt);
4027
4028#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004029 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030#endif
4031
4032 if (prevwin == wp)
4033 prevwin = NULL;
4034 win_free_lsize(wp);
4035
4036 for (i = 0; i < wp->w_tagstacklen; ++i)
4037 vim_free(wp->w_tagstack[i].tagname);
4038
4039 vim_free(wp->w_localdir);
4040#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004041 vim_free(wp->w_match[0].regprog);
4042 vim_free(wp->w_match[1].regprog);
4043 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#endif
4045#ifdef FEAT_JUMPLIST
4046 free_jumplist(wp);
4047#endif
4048
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004049#ifdef FEAT_QUICKFIX
4050 qf_free_all(wp);
4051#endif
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053#ifdef FEAT_GUI
4054 if (gui.in_use)
4055 {
4056 out_flush();
4057 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4058 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4059 }
4060#endif /* FEAT_GUI */
4061
Bram Moolenaarf740b292006-02-16 22:11:02 +00004062 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 vim_free(wp);
4064}
4065
4066/*
4067 * Append window "wp" in the window list after window "after".
4068 */
4069 static void
4070win_append(after, wp)
4071 win_T *after, *wp;
4072{
4073 win_T *before;
4074
4075 if (after == NULL) /* after NULL is in front of the first */
4076 before = firstwin;
4077 else
4078 before = after->w_next;
4079
4080 wp->w_next = before;
4081 wp->w_prev = after;
4082 if (after == NULL)
4083 firstwin = wp;
4084 else
4085 after->w_next = wp;
4086 if (before == NULL)
4087 lastwin = wp;
4088 else
4089 before->w_prev = wp;
4090}
4091
4092/*
4093 * Remove a window from the window list.
4094 */
4095 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004096win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004098 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 if (wp->w_prev != NULL)
4101 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004102 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004104 else
4105 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (wp->w_next != NULL)
4107 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004108 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004110 else
4111 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112}
4113
4114/*
4115 * Append frame "frp" in a frame list after frame "after".
4116 */
4117 static void
4118frame_append(after, frp)
4119 frame_T *after, *frp;
4120{
4121 frp->fr_next = after->fr_next;
4122 after->fr_next = frp;
4123 if (frp->fr_next != NULL)
4124 frp->fr_next->fr_prev = frp;
4125 frp->fr_prev = after;
4126}
4127
4128/*
4129 * Insert frame "frp" in a frame list before frame "before".
4130 */
4131 static void
4132frame_insert(before, frp)
4133 frame_T *before, *frp;
4134{
4135 frp->fr_next = before;
4136 frp->fr_prev = before->fr_prev;
4137 before->fr_prev = frp;
4138 if (frp->fr_prev != NULL)
4139 frp->fr_prev->fr_next = frp;
4140 else
4141 frp->fr_parent->fr_child = frp;
4142}
4143
4144/*
4145 * Remove a frame from a frame list.
4146 */
4147 static void
4148frame_remove(frp)
4149 frame_T *frp;
4150{
4151 if (frp->fr_prev != NULL)
4152 frp->fr_prev->fr_next = frp->fr_next;
4153 else
4154 frp->fr_parent->fr_child = frp->fr_next;
4155 if (frp->fr_next != NULL)
4156 frp->fr_next->fr_prev = frp->fr_prev;
4157}
4158
4159#endif /* FEAT_WINDOWS */
4160
4161/*
4162 * Allocate w_lines[] for window "wp".
4163 * Return FAIL for failure, OK for success.
4164 */
4165 int
4166win_alloc_lines(wp)
4167 win_T *wp;
4168{
4169 wp->w_lines_valid = 0;
4170 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4171 if (wp->w_lines == NULL)
4172 return FAIL;
4173 return OK;
4174}
4175
4176/*
4177 * free lsize arrays for a window
4178 */
4179 void
4180win_free_lsize(wp)
4181 win_T *wp;
4182{
4183 vim_free(wp->w_lines);
4184 wp->w_lines = NULL;
4185}
4186
4187/*
4188 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004189 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
4191 void
4192shell_new_rows()
4193{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004194 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 if (firstwin == NULL) /* not initialized yet */
4197 return;
4198#ifdef FEAT_WINDOWS
4199 if (h < frame_minheight(topframe, NULL))
4200 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004201
4202 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * that doesn't result in the right height, forget about that option. */
4204 frame_new_height(topframe, h, FALSE, TRUE);
4205 if (topframe->fr_height != h)
4206 frame_new_height(topframe, h, FALSE, FALSE);
4207
4208 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4209#else
4210 if (h < 1)
4211 h = 1;
4212 win_new_height(firstwin, h);
4213#endif
4214 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004216 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004217#endif
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#if 0
4220 /* Disabled: don't want making the screen smaller make a window larger. */
4221 if (p_ea)
4222 win_equal(curwin, FALSE, 'v');
4223#endif
4224}
4225
4226#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4227/*
4228 * Called from win_new_shellsize() after Columns changed.
4229 */
4230 void
4231shell_new_columns()
4232{
4233 if (firstwin == NULL) /* not initialized yet */
4234 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004235
4236 /* First try setting the widths of windows with 'winfixwidth'. If that
4237 * doesn't result in the right width, forget about that option. */
4238 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4239 if (topframe->fr_width != Columns)
4240 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4243#if 0
4244 /* Disabled: don't want making the screen smaller make a window larger. */
4245 if (p_ea)
4246 win_equal(curwin, FALSE, 'h');
4247#endif
4248}
4249#endif
4250
4251#if defined(FEAT_CMDWIN) || defined(PROTO)
4252/*
4253 * Save the size of all windows in "gap".
4254 */
4255 void
4256win_size_save(gap)
4257 garray_T *gap;
4258
4259{
4260 win_T *wp;
4261
4262 ga_init2(gap, (int)sizeof(int), 1);
4263 if (ga_grow(gap, win_count() * 2) == OK)
4264 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4265 {
4266 ((int *)gap->ga_data)[gap->ga_len++] =
4267 wp->w_width + wp->w_vsep_width;
4268 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4269 }
4270}
4271
4272/*
4273 * Restore window sizes, but only if the number of windows is still the same.
4274 * Does not free the growarray.
4275 */
4276 void
4277win_size_restore(gap)
4278 garray_T *gap;
4279{
4280 win_T *wp;
4281 int i;
4282
4283 if (win_count() * 2 == gap->ga_len)
4284 {
4285 i = 0;
4286 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4287 {
4288 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4289 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4290 }
4291 /* recompute the window positions */
4292 (void)win_comp_pos();
4293 }
4294}
4295#endif /* FEAT_CMDWIN */
4296
4297#if defined(FEAT_WINDOWS) || defined(PROTO)
4298/*
4299 * Update the position for all windows, using the width and height of the
4300 * frames.
4301 * Returns the row just after the last window.
4302 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004303 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304win_comp_pos()
4305{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004306 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 int col = 0;
4308
4309 frame_comp_pos(topframe, &row, &col);
4310 return row;
4311}
4312
4313/*
4314 * Update the position of the windows in frame "topfrp", using the width and
4315 * height of the frames.
4316 * "*row" and "*col" are the top-left position of the frame. They are updated
4317 * to the bottom-right position plus one.
4318 */
4319 static void
4320frame_comp_pos(topfrp, row, col)
4321 frame_T *topfrp;
4322 int *row;
4323 int *col;
4324{
4325 win_T *wp;
4326 frame_T *frp;
4327#ifdef FEAT_VERTSPLIT
4328 int startcol;
4329 int startrow;
4330#endif
4331
4332 wp = topfrp->fr_win;
4333 if (wp != NULL)
4334 {
4335 if (wp->w_winrow != *row
4336#ifdef FEAT_VERTSPLIT
4337 || wp->w_wincol != *col
4338#endif
4339 )
4340 {
4341 /* position changed, redraw */
4342 wp->w_winrow = *row;
4343#ifdef FEAT_VERTSPLIT
4344 wp->w_wincol = *col;
4345#endif
4346 redraw_win_later(wp, NOT_VALID);
4347 wp->w_redr_status = TRUE;
4348 }
4349 *row += wp->w_height + wp->w_status_height;
4350#ifdef FEAT_VERTSPLIT
4351 *col += wp->w_width + wp->w_vsep_width;
4352#endif
4353 }
4354 else
4355 {
4356#ifdef FEAT_VERTSPLIT
4357 startrow = *row;
4358 startcol = *col;
4359#endif
4360 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4361 {
4362#ifdef FEAT_VERTSPLIT
4363 if (topfrp->fr_layout == FR_ROW)
4364 *row = startrow; /* all frames are at the same row */
4365 else
4366 *col = startcol; /* all frames are at the same col */
4367#endif
4368 frame_comp_pos(frp, row, col);
4369 }
4370 }
4371}
4372
4373#endif /* FEAT_WINDOWS */
4374
4375/*
4376 * Set current window height and take care of repositioning other windows to
4377 * fit around it.
4378 */
4379 void
4380win_setheight(height)
4381 int height;
4382{
4383 win_setheight_win(height, curwin);
4384}
4385
4386/*
4387 * Set the window height of window "win" and take care of repositioning other
4388 * windows to fit around it.
4389 */
4390 void
4391win_setheight_win(height, win)
4392 int height;
4393 win_T *win;
4394{
4395 int row;
4396
4397 if (win == curwin)
4398 {
4399 /* Always keep current window at least one line high, even when
4400 * 'winminheight' is zero. */
4401#ifdef FEAT_WINDOWS
4402 if (height < p_wmh)
4403 height = p_wmh;
4404#endif
4405 if (height == 0)
4406 height = 1;
4407 }
4408
4409#ifdef FEAT_WINDOWS
4410 frame_setheight(win->w_frame, height + win->w_status_height);
4411
4412 /* recompute the window positions */
4413 row = win_comp_pos();
4414#else
4415 if (height > topframe->fr_height)
4416 height = topframe->fr_height;
4417 win->w_height = height;
4418 row = height;
4419#endif
4420
4421 /*
4422 * If there is extra space created between the last window and the command
4423 * line, clear it.
4424 */
4425 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4426 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4427 cmdline_row = row;
4428 msg_row = row;
4429 msg_col = 0;
4430
4431 redraw_all_later(NOT_VALID);
4432}
4433
4434#if defined(FEAT_WINDOWS) || defined(PROTO)
4435
4436/*
4437 * Set the height of a frame to "height" and take care that all frames and
4438 * windows inside it are resized. Also resize frames on the left and right if
4439 * the are in the same FR_ROW frame.
4440 *
4441 * Strategy:
4442 * If the frame is part of a FR_COL frame, try fitting the frame in that
4443 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4444 * go to containing frames to resize them and make room.
4445 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4446 * Check for the minimal height of the FR_ROW frame.
4447 * At the top level we can also use change the command line height.
4448 */
4449 static void
4450frame_setheight(curfrp, height)
4451 frame_T *curfrp;
4452 int height;
4453{
4454 int room; /* total number of lines available */
4455 int take; /* number of lines taken from other windows */
4456 int room_cmdline; /* lines available from cmdline */
4457 int run;
4458 frame_T *frp;
4459 int h;
4460 int room_reserved;
4461
4462 /* If the height already is the desired value, nothing to do. */
4463 if (curfrp->fr_height == height)
4464 return;
4465
4466 if (curfrp->fr_parent == NULL)
4467 {
4468 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004469 if (height > ROWS_AVAIL)
4470 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if (height > 0)
4472 frame_new_height(curfrp, height, FALSE, FALSE);
4473 }
4474 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4475 {
4476 /* Row of frames: Also need to resize frames left and right of this
4477 * one. First check for the minimal height of these. */
4478 h = frame_minheight(curfrp->fr_parent, NULL);
4479 if (height < h)
4480 height = h;
4481 frame_setheight(curfrp->fr_parent, height);
4482 }
4483 else
4484 {
4485 /*
4486 * Column of frames: try to change only frames in this column.
4487 */
4488#ifdef FEAT_VERTSPLIT
4489 /*
4490 * Do this twice:
4491 * 1: compute room available, if it's not enough try resizing the
4492 * containing frame.
4493 * 2: compute the room available and adjust the height to it.
4494 * Try not to reduce the height of a window with 'winfixheight' set.
4495 */
4496 for (run = 1; run <= 2; ++run)
4497#else
4498 for (;;)
4499#endif
4500 {
4501 room = 0;
4502 room_reserved = 0;
4503 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4504 frp = frp->fr_next)
4505 {
4506 if (frp != curfrp
4507 && frp->fr_win != NULL
4508 && frp->fr_win->w_p_wfh)
4509 room_reserved += frp->fr_height;
4510 room += frp->fr_height;
4511 if (frp != curfrp)
4512 room -= frame_minheight(frp, NULL);
4513 }
4514#ifdef FEAT_VERTSPLIT
4515 if (curfrp->fr_width != Columns)
4516 room_cmdline = 0;
4517 else
4518#endif
4519 {
4520 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4521 + lastwin->w_height + lastwin->w_status_height);
4522 if (room_cmdline < 0)
4523 room_cmdline = 0;
4524 }
4525
4526 if (height <= room + room_cmdline)
4527 break;
4528#ifdef FEAT_VERTSPLIT
4529 if (run == 2 || curfrp->fr_width == Columns)
4530#endif
4531 {
4532 if (height > room + room_cmdline)
4533 height = room + room_cmdline;
4534 break;
4535 }
4536#ifdef FEAT_VERTSPLIT
4537 frame_setheight(curfrp->fr_parent, height
4538 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4539#endif
4540 /*NOTREACHED*/
4541 }
4542
4543 /*
4544 * Compute the number of lines we will take from others frames (can be
4545 * negative!).
4546 */
4547 take = height - curfrp->fr_height;
4548
4549 /* If there is not enough room, also reduce the height of a window
4550 * with 'winfixheight' set. */
4551 if (height > room + room_cmdline - room_reserved)
4552 room_reserved = room + room_cmdline - height;
4553 /* If there is only a 'winfixheight' window and making the
4554 * window smaller, need to make the other window taller. */
4555 if (take < 0 && room - curfrp->fr_height < room_reserved)
4556 room_reserved = 0;
4557
4558 if (take > 0 && room_cmdline > 0)
4559 {
4560 /* use lines from cmdline first */
4561 if (take < room_cmdline)
4562 room_cmdline = take;
4563 take -= room_cmdline;
4564 topframe->fr_height += room_cmdline;
4565 }
4566
4567 /*
4568 * set the current frame to the new height
4569 */
4570 frame_new_height(curfrp, height, FALSE, FALSE);
4571
4572 /*
4573 * First take lines from the frames after the current frame. If
4574 * that is not enough, takes lines from frames above the current
4575 * frame.
4576 */
4577 for (run = 0; run < 2; ++run)
4578 {
4579 if (run == 0)
4580 frp = curfrp->fr_next; /* 1st run: start with next window */
4581 else
4582 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4583 while (frp != NULL && take != 0)
4584 {
4585 h = frame_minheight(frp, NULL);
4586 if (room_reserved > 0
4587 && frp->fr_win != NULL
4588 && frp->fr_win->w_p_wfh)
4589 {
4590 if (room_reserved >= frp->fr_height)
4591 room_reserved -= frp->fr_height;
4592 else
4593 {
4594 if (frp->fr_height - room_reserved > take)
4595 room_reserved = frp->fr_height - take;
4596 take -= frp->fr_height - room_reserved;
4597 frame_new_height(frp, room_reserved, FALSE, FALSE);
4598 room_reserved = 0;
4599 }
4600 }
4601 else
4602 {
4603 if (frp->fr_height - take < h)
4604 {
4605 take -= frp->fr_height - h;
4606 frame_new_height(frp, h, FALSE, FALSE);
4607 }
4608 else
4609 {
4610 frame_new_height(frp, frp->fr_height - take,
4611 FALSE, FALSE);
4612 take = 0;
4613 }
4614 }
4615 if (run == 0)
4616 frp = frp->fr_next;
4617 else
4618 frp = frp->fr_prev;
4619 }
4620 }
4621 }
4622}
4623
4624#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4625/*
4626 * Set current window width and take care of repositioning other windows to
4627 * fit around it.
4628 */
4629 void
4630win_setwidth(width)
4631 int width;
4632{
4633 win_setwidth_win(width, curwin);
4634}
4635
4636 void
4637win_setwidth_win(width, wp)
4638 int width;
4639 win_T *wp;
4640{
4641 /* Always keep current window at least one column wide, even when
4642 * 'winminwidth' is zero. */
4643 if (wp == curwin)
4644 {
4645 if (width < p_wmw)
4646 width = p_wmw;
4647 if (width == 0)
4648 width = 1;
4649 }
4650
4651 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4652
4653 /* recompute the window positions */
4654 (void)win_comp_pos();
4655
4656 redraw_all_later(NOT_VALID);
4657}
4658
4659/*
4660 * Set the width of a frame to "width" and take care that all frames and
4661 * windows inside it are resized. Also resize frames above and below if the
4662 * are in the same FR_ROW frame.
4663 *
4664 * Strategy is similar to frame_setheight().
4665 */
4666 static void
4667frame_setwidth(curfrp, width)
4668 frame_T *curfrp;
4669 int width;
4670{
4671 int room; /* total number of lines available */
4672 int take; /* number of lines taken from other windows */
4673 int run;
4674 frame_T *frp;
4675 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004676 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677
4678 /* If the width already is the desired value, nothing to do. */
4679 if (curfrp->fr_width == width)
4680 return;
4681
4682 if (curfrp->fr_parent == NULL)
4683 /* topframe: can't change width */
4684 return;
4685
4686 if (curfrp->fr_parent->fr_layout == FR_COL)
4687 {
4688 /* Column of frames: Also need to resize frames above and below of
4689 * this one. First check for the minimal width of these. */
4690 w = frame_minwidth(curfrp->fr_parent, NULL);
4691 if (width < w)
4692 width = w;
4693 frame_setwidth(curfrp->fr_parent, width);
4694 }
4695 else
4696 {
4697 /*
4698 * Row of frames: try to change only frames in this row.
4699 *
4700 * Do this twice:
4701 * 1: compute room available, if it's not enough try resizing the
4702 * containing frame.
4703 * 2: compute the room available and adjust the width to it.
4704 */
4705 for (run = 1; run <= 2; ++run)
4706 {
4707 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004708 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4710 frp = frp->fr_next)
4711 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004712 if (frp != curfrp
4713 && frp->fr_win != NULL
4714 && frp->fr_win->w_p_wfw)
4715 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 room += frp->fr_width;
4717 if (frp != curfrp)
4718 room -= frame_minwidth(frp, NULL);
4719 }
4720
4721 if (width <= room)
4722 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004723 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 if (width > room)
4726 width = room;
4727 break;
4728 }
4729 frame_setwidth(curfrp->fr_parent, width
4730 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4731 }
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 /*
4734 * Compute the number of lines we will take from others frames (can be
4735 * negative!).
4736 */
4737 take = width - curfrp->fr_width;
4738
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004739 /* If there is not enough room, also reduce the width of a window
4740 * with 'winfixwidth' set. */
4741 if (width > room - room_reserved)
4742 room_reserved = room - width;
4743 /* If there is only a 'winfixwidth' window and making the
4744 * window smaller, need to make the other window narrower. */
4745 if (take < 0 && room - curfrp->fr_width < room_reserved)
4746 room_reserved = 0;
4747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 /*
4749 * set the current frame to the new width
4750 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004751 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753 /*
4754 * First take lines from the frames right of the current frame. If
4755 * that is not enough, takes lines from frames left of the current
4756 * frame.
4757 */
4758 for (run = 0; run < 2; ++run)
4759 {
4760 if (run == 0)
4761 frp = curfrp->fr_next; /* 1st run: start with next window */
4762 else
4763 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4764 while (frp != NULL && take != 0)
4765 {
4766 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004767 if (room_reserved > 0
4768 && frp->fr_win != NULL
4769 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004771 if (room_reserved >= frp->fr_width)
4772 room_reserved -= frp->fr_width;
4773 else
4774 {
4775 if (frp->fr_width - room_reserved > take)
4776 room_reserved = frp->fr_width - take;
4777 take -= frp->fr_width - room_reserved;
4778 frame_new_width(frp, room_reserved, FALSE, FALSE);
4779 room_reserved = 0;
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else
4783 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004784 if (frp->fr_width - take < w)
4785 {
4786 take -= frp->fr_width - w;
4787 frame_new_width(frp, w, FALSE, FALSE);
4788 }
4789 else
4790 {
4791 frame_new_width(frp, frp->fr_width - take,
4792 FALSE, FALSE);
4793 take = 0;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 if (run == 0)
4797 frp = frp->fr_next;
4798 else
4799 frp = frp->fr_prev;
4800 }
4801 }
4802 }
4803}
4804#endif /* FEAT_VERTSPLIT */
4805
4806/*
4807 * Check 'winminheight' for a valid value.
4808 */
4809 void
4810win_setminheight()
4811{
4812 int room;
4813 int first = TRUE;
4814 win_T *wp;
4815
4816 /* loop until there is a 'winminheight' that is possible */
4817 while (p_wmh > 0)
4818 {
4819 /* TODO: handle vertical splits */
4820 room = -p_wh;
4821 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4822 room += wp->w_height - p_wmh;
4823 if (room >= 0)
4824 break;
4825 --p_wmh;
4826 if (first)
4827 {
4828 EMSG(_(e_noroom));
4829 first = FALSE;
4830 }
4831 }
4832}
4833
4834#ifdef FEAT_MOUSE
4835
4836/*
4837 * Status line of dragwin is dragged "offset" lines down (negative is up).
4838 */
4839 void
4840win_drag_status_line(dragwin, offset)
4841 win_T *dragwin;
4842 int offset;
4843{
4844 frame_T *curfr;
4845 frame_T *fr;
4846 int room;
4847 int row;
4848 int up; /* if TRUE, drag status line up, otherwise down */
4849 int n;
4850
4851 fr = dragwin->w_frame;
4852 curfr = fr;
4853 if (fr != topframe) /* more than one window */
4854 {
4855 fr = fr->fr_parent;
4856 /* When the parent frame is not a column of frames, its parent should
4857 * be. */
4858 if (fr->fr_layout != FR_COL)
4859 {
4860 curfr = fr;
4861 if (fr != topframe) /* only a row of windows, may drag statusline */
4862 fr = fr->fr_parent;
4863 }
4864 }
4865
4866 /* If this is the last frame in a column, may want to resize the parent
4867 * frame instead (go two up to skip a row of frames). */
4868 while (curfr != topframe && curfr->fr_next == NULL)
4869 {
4870 if (fr != topframe)
4871 fr = fr->fr_parent;
4872 curfr = fr;
4873 if (fr != topframe)
4874 fr = fr->fr_parent;
4875 }
4876
4877 if (offset < 0) /* drag up */
4878 {
4879 up = TRUE;
4880 offset = -offset;
4881 /* sum up the room of the current frame and above it */
4882 if (fr == curfr)
4883 {
4884 /* only one window */
4885 room = fr->fr_height - frame_minheight(fr, NULL);
4886 }
4887 else
4888 {
4889 room = 0;
4890 for (fr = fr->fr_child; ; fr = fr->fr_next)
4891 {
4892 room += fr->fr_height - frame_minheight(fr, NULL);
4893 if (fr == curfr)
4894 break;
4895 }
4896 }
4897 fr = curfr->fr_next; /* put fr at frame that grows */
4898 }
4899 else /* drag down */
4900 {
4901 up = FALSE;
4902 /*
4903 * Only dragging the last status line can reduce p_ch.
4904 */
4905 room = Rows - cmdline_row;
4906 if (curfr->fr_next == NULL)
4907 room -= 1;
4908 else
4909 room -= p_ch;
4910 if (room < 0)
4911 room = 0;
4912 /* sum up the room of frames below of the current one */
4913 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4914 room += fr->fr_height - frame_minheight(fr, NULL);
4915 fr = curfr; /* put fr at window that grows */
4916 }
4917
4918 if (room < offset) /* Not enough room */
4919 offset = room; /* Move as far as we can */
4920 if (offset <= 0)
4921 return;
4922
4923 /*
4924 * Grow frame fr by "offset" lines.
4925 * Doesn't happen when dragging the last status line up.
4926 */
4927 if (fr != NULL)
4928 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4929
4930 if (up)
4931 fr = curfr; /* current frame gets smaller */
4932 else
4933 fr = curfr->fr_next; /* next frame gets smaller */
4934
4935 /*
4936 * Now make the other frames smaller.
4937 */
4938 while (fr != NULL && offset > 0)
4939 {
4940 n = frame_minheight(fr, NULL);
4941 if (fr->fr_height - offset <= n)
4942 {
4943 offset -= fr->fr_height - n;
4944 frame_new_height(fr, n, !up, FALSE);
4945 }
4946 else
4947 {
4948 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4949 break;
4950 }
4951 if (up)
4952 fr = fr->fr_prev;
4953 else
4954 fr = fr->fr_next;
4955 }
4956 row = win_comp_pos();
4957 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4958 cmdline_row = row;
4959 p_ch = Rows - cmdline_row;
4960 if (p_ch < 1)
4961 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004962 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004963 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 showmode();
4965}
4966
4967#ifdef FEAT_VERTSPLIT
4968/*
4969 * Separator line of dragwin is dragged "offset" lines right (negative is left).
4970 */
4971 void
4972win_drag_vsep_line(dragwin, offset)
4973 win_T *dragwin;
4974 int offset;
4975{
4976 frame_T *curfr;
4977 frame_T *fr;
4978 int room;
4979 int left; /* if TRUE, drag separator line left, otherwise right */
4980 int n;
4981
4982 fr = dragwin->w_frame;
4983 if (fr == topframe) /* only one window (cannot happe?) */
4984 return;
4985 curfr = fr;
4986 fr = fr->fr_parent;
4987 /* When the parent frame is not a row of frames, its parent should be. */
4988 if (fr->fr_layout != FR_ROW)
4989 {
4990 if (fr == topframe) /* only a column of windows (cannot happen?) */
4991 return;
4992 curfr = fr;
4993 fr = fr->fr_parent;
4994 }
4995
4996 /* If this is the last frame in a row, may want to resize a parent
4997 * frame instead. */
4998 while (curfr->fr_next == NULL)
4999 {
5000 if (fr == topframe)
5001 break;
5002 curfr = fr;
5003 fr = fr->fr_parent;
5004 if (fr != topframe)
5005 {
5006 curfr = fr;
5007 fr = fr->fr_parent;
5008 }
5009 }
5010
5011 if (offset < 0) /* drag left */
5012 {
5013 left = TRUE;
5014 offset = -offset;
5015 /* sum up the room of the current frame and left of it */
5016 room = 0;
5017 for (fr = fr->fr_child; ; fr = fr->fr_next)
5018 {
5019 room += fr->fr_width - frame_minwidth(fr, NULL);
5020 if (fr == curfr)
5021 break;
5022 }
5023 fr = curfr->fr_next; /* put fr at frame that grows */
5024 }
5025 else /* drag right */
5026 {
5027 left = FALSE;
5028 /* sum up the room of frames right of the current one */
5029 room = 0;
5030 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5031 room += fr->fr_width - frame_minwidth(fr, NULL);
5032 fr = curfr; /* put fr at window that grows */
5033 }
5034
5035 if (room < offset) /* Not enough room */
5036 offset = room; /* Move as far as we can */
5037 if (offset <= 0) /* No room at all, quit. */
5038 return;
5039
5040 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005041 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 /* shrink other frames: current and at the left or at the right */
5044 if (left)
5045 fr = curfr; /* current frame gets smaller */
5046 else
5047 fr = curfr->fr_next; /* next frame gets smaller */
5048
5049 while (fr != NULL && offset > 0)
5050 {
5051 n = frame_minwidth(fr, NULL);
5052 if (fr->fr_width - offset <= n)
5053 {
5054 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005055 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 else
5058 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005059 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 }
5062 if (left)
5063 fr = fr->fr_prev;
5064 else
5065 fr = fr->fr_next;
5066 }
5067 (void)win_comp_pos();
5068 redraw_all_later(NOT_VALID);
5069}
5070#endif /* FEAT_VERTSPLIT */
5071#endif /* FEAT_MOUSE */
5072
5073#endif /* FEAT_WINDOWS */
5074
5075/*
5076 * Set the height of a window.
5077 * This takes care of the things inside the window, not what happens to the
5078 * window position, the frame or to other windows.
5079 */
5080 static void
5081win_new_height(wp, height)
5082 win_T *wp;
5083 int height;
5084{
5085 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005086 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005088 int space;
5089 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090#define FRACTION_MULT 16384L
5091
5092 /* Don't want a negative height. Happens when splitting a tiny window.
5093 * Will equalize heights soon to fix it. */
5094 if (height < 0)
5095 height = 0;
5096
5097 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5098 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5099 + FRACTION_MULT / 2) / (long)wp->w_height;
5100
5101 wp->w_height = height;
5102 wp->w_skipcol = 0;
5103
5104 /* Don't change w_topline when height is zero. Don't set w_topline when
5105 * 'scrollbind' is set and this isn't the current window. */
5106 if (height > 0
5107#ifdef FEAT_SCROLLBIND
5108 && (!wp->w_p_scb || wp == curwin)
5109#endif
5110 )
5111 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005112 /*
5113 * Find a value for w_topline that shows the cursor at the same
5114 * relative position in the window as before (more or less).
5115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 lnum = wp->w_cursor.lnum;
5117 if (lnum < 1) /* can happen when starting up */
5118 lnum = 1;
5119 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5120 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5121 sline = wp->w_wrow - line_size;
5122 if (sline < 0)
5123 {
5124 /*
5125 * Cursor line would go off top of screen if w_wrow was this high.
5126 */
5127 wp->w_wrow = line_size;
5128 }
5129 else
5130 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005131 space = height;
5132 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005134 space -= line_size;
5135 if (space > 0 && sline <= 0 && !did_below)
5136 {
5137 /* Try to use "~" lines below the text to avoid that text
5138 * is above the window while there are empty lines.
5139 * Subtract the rows below the cursor from "space" and
5140 * give the rest to "sline". */
5141 did_below = TRUE;
5142 bot = wp->w_cursor.lnum;
5143 while (space > 0)
5144 {
5145 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5146 space = 0;
5147 else
5148 {
5149#ifdef FEAT_FOLDING
5150 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5151#endif
5152 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5153 break;
5154 ++bot;
5155 space -= plines_win(wp, bot, TRUE);
5156 }
5157 }
5158 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5159 sline += space;
5160 }
5161 if (sline <= 0)
5162 break;
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#ifdef FEAT_FOLDING
5165 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5166 if (lnum == 1)
5167 {
5168 /* first line in buffer is folded */
5169 line_size = 1;
5170 --sline;
5171 break;
5172 }
5173#endif
5174 --lnum;
5175#ifdef FEAT_DIFF
5176 if (lnum == wp->w_topline)
5177 line_size = plines_win_nofill(wp, lnum, TRUE)
5178 + wp->w_topfill;
5179 else
5180#endif
5181 line_size = plines_win(wp, lnum, TRUE);
5182 sline -= line_size;
5183 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (sline < 0)
5186 {
5187 /*
5188 * Line we want at top would go off top of screen. Use next
5189 * line instead.
5190 */
5191#ifdef FEAT_FOLDING
5192 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5193#endif
5194 lnum++;
5195 wp->w_wrow -= line_size + sline;
5196 }
5197 else if (sline > 0)
5198 {
5199 /* First line of file reached, use that as topline. */
5200 lnum = 1;
5201 wp->w_wrow -= sline;
5202 }
5203 }
5204 set_topline(wp, lnum);
5205 }
5206
5207 if (wp == curwin)
5208 {
5209 if (p_so)
5210 update_topline();
5211 curs_columns(FALSE); /* validate w_wrow */
5212 }
5213 wp->w_prev_fraction_row = wp->w_wrow;
5214
5215 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005216 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217#ifdef FEAT_WINDOWS
5218 wp->w_redr_status = TRUE;
5219#endif
5220 invalidate_botline_win(wp);
5221}
5222
5223#ifdef FEAT_VERTSPLIT
5224/*
5225 * Set the width of a window.
5226 */
5227 static void
5228win_new_width(wp, width)
5229 win_T *wp;
5230 int width;
5231{
5232 wp->w_width = width;
5233 wp->w_lines_valid = 0;
5234 changed_line_abv_curs_win(wp);
5235 invalidate_botline_win(wp);
5236 if (wp == curwin)
5237 {
5238 update_topline();
5239 curs_columns(TRUE); /* validate w_wrow */
5240 }
5241 redraw_win_later(wp, NOT_VALID);
5242 wp->w_redr_status = TRUE;
5243}
5244#endif
5245
5246 void
5247win_comp_scroll(wp)
5248 win_T *wp;
5249{
5250 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5251 if (wp->w_p_scr == 0)
5252 wp->w_p_scr = 1;
5253}
5254
5255/*
5256 * command_height: called whenever p_ch has been changed
5257 */
5258 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005259command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260{
5261#ifdef FEAT_WINDOWS
5262 int h;
5263 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005264 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005266 /* Use the value of p_ch that we remembered. This is needed for when the
5267 * GUI starts up, we can't be sure in what order things happen. And when
5268 * p_ch was changed in another tab page. */
5269 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 /* Find bottom frame with width of screen. */
5272 frp = lastwin->w_frame;
5273# ifdef FEAT_VERTSPLIT
5274 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5275 frp = frp->fr_parent;
5276# endif
5277
5278 /* Avoid changing the height of a window with 'winfixheight' set. */
5279 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5280 && frp->fr_win->w_p_wfh)
5281 frp = frp->fr_prev;
5282
5283 if (starting != NO_SCREEN)
5284 {
5285 cmdline_row = Rows - p_ch;
5286
5287 if (p_ch > old_p_ch) /* p_ch got bigger */
5288 {
5289 while (p_ch > old_p_ch)
5290 {
5291 if (frp == NULL)
5292 {
5293 EMSG(_(e_noroom));
5294 p_ch = old_p_ch;
5295 cmdline_row = Rows - p_ch;
5296 break;
5297 }
5298 h = frp->fr_height - frame_minheight(frp, NULL);
5299 if (h > p_ch - old_p_ch)
5300 h = p_ch - old_p_ch;
5301 old_p_ch += h;
5302 frame_add_height(frp, -h);
5303 frp = frp->fr_prev;
5304 }
5305
5306 /* Recompute window positions. */
5307 (void)win_comp_pos();
5308
5309 /* clear the lines added to cmdline */
5310 if (full_screen)
5311 screen_fill((int)(cmdline_row), (int)Rows, 0,
5312 (int)Columns, ' ', ' ', 0);
5313 msg_row = cmdline_row;
5314 redraw_cmdline = TRUE;
5315 return;
5316 }
5317
5318 if (msg_row < cmdline_row)
5319 msg_row = cmdline_row;
5320 redraw_cmdline = TRUE;
5321 }
5322 frame_add_height(frp, (int)(old_p_ch - p_ch));
5323
5324 /* Recompute window positions. */
5325 if (frp != lastwin->w_frame)
5326 (void)win_comp_pos();
5327#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005329 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330#endif
5331}
5332
5333#if defined(FEAT_WINDOWS) || defined(PROTO)
5334/*
5335 * Resize frame "frp" to be "n" lines higher (negative for less high).
5336 * Also resize the frames it is contained in.
5337 */
5338 static void
5339frame_add_height(frp, n)
5340 frame_T *frp;
5341 int n;
5342{
5343 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5344 for (;;)
5345 {
5346 frp = frp->fr_parent;
5347 if (frp == NULL)
5348 break;
5349 frp->fr_height += n;
5350 }
5351}
5352
5353/*
5354 * Add or remove a status line for the bottom window(s), according to the
5355 * value of 'laststatus'.
5356 */
5357 void
5358last_status(morewin)
5359 int morewin; /* pretend there are two or more windows */
5360{
5361 /* Don't make a difference between horizontal or vertical split. */
5362 last_status_rec(topframe, (p_ls == 2
5363 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5364}
5365
5366 static void
5367last_status_rec(fr, statusline)
5368 frame_T *fr;
5369 int statusline;
5370{
5371 frame_T *fp;
5372 win_T *wp;
5373
5374 if (fr->fr_layout == FR_LEAF)
5375 {
5376 wp = fr->fr_win;
5377 if (wp->w_status_height != 0 && !statusline)
5378 {
5379 /* remove status line */
5380 win_new_height(wp, wp->w_height + 1);
5381 wp->w_status_height = 0;
5382 comp_col();
5383 }
5384 else if (wp->w_status_height == 0 && statusline)
5385 {
5386 /* Find a frame to take a line from. */
5387 fp = fr;
5388 while (fp->fr_height <= frame_minheight(fp, NULL))
5389 {
5390 if (fp == topframe)
5391 {
5392 EMSG(_(e_noroom));
5393 return;
5394 }
5395 /* In a column of frames: go to frame above. If already at
5396 * the top or in a row of frames: go to parent. */
5397 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5398 fp = fp->fr_prev;
5399 else
5400 fp = fp->fr_parent;
5401 }
5402 wp->w_status_height = 1;
5403 if (fp != fr)
5404 {
5405 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5406 frame_fix_height(wp);
5407 (void)win_comp_pos();
5408 }
5409 else
5410 win_new_height(wp, wp->w_height - 1);
5411 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005412 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414 }
5415#ifdef FEAT_VERTSPLIT
5416 else if (fr->fr_layout == FR_ROW)
5417 {
5418 /* vertically split windows, set status line for each one */
5419 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5420 last_status_rec(fp, statusline);
5421 }
5422#endif
5423 else
5424 {
5425 /* horizontally split window, set status line for last one */
5426 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5427 ;
5428 last_status_rec(fp, statusline);
5429 }
5430}
5431
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005432/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005433 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005434 */
5435 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005436tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005437{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005438#ifdef FEAT_GUI_TABLINE
5439 /* When the GUI has the tabline then this always returns zero. */
5440 if (gui_use_tabline())
5441 return 0;
5442#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005443 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005444 {
5445 case 0: return 0;
5446 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5447 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005448 return 1;
5449}
5450
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451#endif /* FEAT_WINDOWS */
5452
5453#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5454/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005455 * Get the file name at the cursor.
5456 * If Visual mode is active, use the selected text if it's in one line.
5457 * Returns the name in allocated memory, NULL for failure.
5458 */
5459 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005460grab_file_name(count, file_lnum)
5461 long count;
5462 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005463{
5464# ifdef FEAT_VISUAL
5465 if (VIsual_active)
5466 {
5467 int len;
5468 char_u *ptr;
5469
5470 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5471 return NULL;
5472 return find_file_name_in_path(ptr, len,
5473 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5474 }
5475# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005476 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5477 file_lnum);
5478
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005479}
5480
5481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * Return the file name under or after the cursor.
5483 *
5484 * The 'path' option is searched if the file name is not absolute.
5485 * The string returned has been alloc'ed and should be freed by the caller.
5486 * NULL is returned if the file name or file is not found.
5487 *
5488 * options:
5489 * FNAME_MESS give error messages
5490 * FNAME_EXP expand to path
5491 * FNAME_HYP check for hypertext link
5492 * FNAME_INCL apply "includeexpr"
5493 */
5494 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005495file_name_at_cursor(options, count, file_lnum)
5496 int options;
5497 long count;
5498 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005501 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5502 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503}
5504
5505/*
5506 * Return the name of the file under or after ptr[col].
5507 * Otherwise like file_name_at_cursor().
5508 */
5509 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005510file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 char_u *line;
5512 int col;
5513 int options;
5514 long count;
5515 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005516 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 char_u *ptr;
5519 int len;
5520
5521 /*
5522 * search forward for what could be the start of a file name
5523 */
5524 ptr = line + col;
5525 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005526 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 if (*ptr == NUL) /* nothing found */
5528 {
5529 if (options & FNAME_MESS)
5530 EMSG(_("E446: No file name under cursor"));
5531 return NULL;
5532 }
5533
5534 /*
5535 * Search backward for first char of the file name.
5536 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5537 */
5538 while (ptr > line)
5539 {
5540#ifdef FEAT_MBYTE
5541 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5542 ptr -= len + 1;
5543 else
5544#endif
5545 if (vim_isfilec(ptr[-1])
5546 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5547 --ptr;
5548 else
5549 break;
5550 }
5551
5552 /*
5553 * Search forward for the last char of the file name.
5554 * Also allow "://" when ':' is not in 'isfname'.
5555 */
5556 len = 0;
5557 while (vim_isfilec(ptr[len])
5558 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5559#ifdef FEAT_MBYTE
5560 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005561 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 else
5563#endif
5564 ++len;
5565
5566 /*
5567 * If there is trailing punctuation, remove it.
5568 * But don't remove "..", could be a directory name.
5569 */
5570 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5571 && ptr[len - 2] != '.')
5572 --len;
5573
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005574 if (file_lnum != NULL)
5575 {
5576 char_u *p;
5577
5578 /* Get the number after the file name and a separator character */
5579 p = ptr + len;
5580 p = skipwhite(p);
5581 if (*p != NUL)
5582 {
5583 if (!isdigit(*p))
5584 ++p; /* skip the separator */
5585 p = skipwhite(p);
5586 if (isdigit(*p))
5587 *file_lnum = (int)getdigits(&p);
5588 }
5589 }
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5592}
5593
5594# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5595static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5596
5597 static char_u *
5598eval_includeexpr(ptr, len)
5599 char_u *ptr;
5600 int len;
5601{
5602 char_u *res;
5603
5604 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005605 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005606 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 set_vim_var_string(VV_FNAME, NULL, 0);
5608 return res;
5609}
5610#endif
5611
5612/*
5613 * Return the name of the file ptr[len] in 'path'.
5614 * Otherwise like file_name_at_cursor().
5615 */
5616 char_u *
5617find_file_name_in_path(ptr, len, options, count, rel_fname)
5618 char_u *ptr;
5619 int len;
5620 int options;
5621 long count;
5622 char_u *rel_fname; /* file we are searching relative to */
5623{
5624 char_u *file_name;
5625 int c;
5626# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5627 char_u *tofree = NULL;
5628
5629 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5630 {
5631 tofree = eval_includeexpr(ptr, len);
5632 if (tofree != NULL)
5633 {
5634 ptr = tofree;
5635 len = (int)STRLEN(ptr);
5636 }
5637 }
5638# endif
5639
5640 if (options & FNAME_EXP)
5641 {
5642 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5643 TRUE, rel_fname);
5644
5645# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5646 /*
5647 * If the file could not be found in a normal way, try applying
5648 * 'includeexpr' (unless done already).
5649 */
5650 if (file_name == NULL
5651 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5652 {
5653 tofree = eval_includeexpr(ptr, len);
5654 if (tofree != NULL)
5655 {
5656 ptr = tofree;
5657 len = (int)STRLEN(ptr);
5658 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5659 TRUE, rel_fname);
5660 }
5661 }
5662# endif
5663 if (file_name == NULL && (options & FNAME_MESS))
5664 {
5665 c = ptr[len];
5666 ptr[len] = NUL;
5667 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5668 ptr[len] = c;
5669 }
5670
5671 /* Repeat finding the file "count" times. This matters when it
5672 * appears several times in the path. */
5673 while (file_name != NULL && --count > 0)
5674 {
5675 vim_free(file_name);
5676 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5677 }
5678 }
5679 else
5680 file_name = vim_strnsave(ptr, len);
5681
5682# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5683 vim_free(tofree);
5684# endif
5685
5686 return file_name;
5687}
5688#endif /* FEAT_SEARCHPATH */
5689
5690/*
5691 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5692 * Also check for ":\\", which MS Internet Explorer accepts, return
5693 * URL_BACKSLASH.
5694 */
5695 static int
5696path_is_url(p)
5697 char_u *p;
5698{
5699 if (STRNCMP(p, "://", (size_t)3) == 0)
5700 return URL_SLASH;
5701 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5702 return URL_BACKSLASH;
5703 return 0;
5704}
5705
5706/*
5707 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5708 * Return URL_BACKSLASH for "name:\\".
5709 * Return zero otherwise.
5710 */
5711 int
5712path_with_url(fname)
5713 char_u *fname;
5714{
5715 char_u *p;
5716
5717 for (p = fname; isalpha(*p); ++p)
5718 ;
5719 return path_is_url(p);
5720}
5721
5722/*
5723 * Return TRUE if "name" is a full (absolute) path name or URL.
5724 */
5725 int
5726vim_isAbsName(name)
5727 char_u *name;
5728{
5729 return (path_with_url(name) != 0 || mch_isFullName(name));
5730}
5731
5732/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005733 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 *
5735 * return FAIL for failure, OK otherwise
5736 */
5737 int
5738vim_FullName(fname, buf, len, force)
5739 char_u *fname, *buf;
5740 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005741 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742{
5743 int retval = OK;
5744 int url;
5745
5746 *buf = NUL;
5747 if (fname == NULL)
5748 return FAIL;
5749
5750 url = path_with_url(fname);
5751 if (!url)
5752 retval = mch_FullName(fname, buf, len, force);
5753 if (url || retval == FAIL)
5754 {
5755 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005756 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5759 slash_adjust(buf);
5760#endif
5761 return retval;
5762}
5763
5764/*
5765 * Return the minimal number of rows that is needed on the screen to display
5766 * the current number of windows.
5767 */
5768 int
5769min_rows()
5770{
5771 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005772#ifdef FEAT_WINDOWS
5773 tabpage_T *tp;
5774 int n;
5775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777 if (firstwin == NULL) /* not initialized yet */
5778 return MIN_LINES;
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005781 total = 0;
5782 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5783 {
5784 n = frame_minheight(tp->tp_topframe, NULL);
5785 if (total < n)
5786 total = n;
5787 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005788 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005790 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005792 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 return total;
5794}
5795
5796/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005797 * Return TRUE if there is only one window (in the current tab page), not
5798 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 */
5800 int
5801only_one_window()
5802{
5803#ifdef FEAT_WINDOWS
5804 int count = 0;
5805 win_T *wp;
5806
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005807 /* If there is another tab page there always is another window. */
5808 if (first_tabpage->tp_next != NULL)
5809 return FALSE;
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005812 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813# ifdef FEAT_QUICKFIX
5814 || wp->w_p_pvw
5815# endif
5816 ) || wp == curwin)
5817 ++count;
5818 return (count <= 1);
5819#else
5820 return TRUE;
5821#endif
5822}
5823
5824#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5825/*
5826 * Correct the cursor line number in other windows. Used after changing the
5827 * current buffer, and before applying autocommands.
5828 * When "do_curwin" is TRUE, also check current window.
5829 */
5830 void
5831check_lnums(do_curwin)
5832 int do_curwin;
5833{
5834 win_T *wp;
5835
5836#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005837 tabpage_T *tp;
5838
5839 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5841#else
5842 wp = curwin;
5843 if (do_curwin)
5844#endif
5845 {
5846 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5847 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5848 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5849 wp->w_topline = curbuf->b_ml.ml_line_count;
5850 }
5851}
5852#endif
5853
5854#if defined(FEAT_WINDOWS) || defined(PROTO)
5855
5856/*
5857 * A snapshot of the window sizes, to restore them after closing the help
5858 * window.
5859 * Only these fields are used:
5860 * fr_layout
5861 * fr_width
5862 * fr_height
5863 * fr_next
5864 * fr_child
5865 * fr_win (only valid for the old curwin, NULL otherwise)
5866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868/*
5869 * Create a snapshot of the current frame sizes.
5870 */
5871 static void
5872make_snapshot()
5873{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005874 clear_snapshot(curtab);
5875 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
5878 static void
5879make_snapshot_rec(fr, frp)
5880 frame_T *fr;
5881 frame_T **frp;
5882{
5883 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5884 if (*frp == NULL)
5885 return;
5886 (*frp)->fr_layout = fr->fr_layout;
5887# ifdef FEAT_VERTSPLIT
5888 (*frp)->fr_width = fr->fr_width;
5889# endif
5890 (*frp)->fr_height = fr->fr_height;
5891 if (fr->fr_next != NULL)
5892 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5893 if (fr->fr_child != NULL)
5894 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5895 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5896 (*frp)->fr_win = curwin;
5897}
5898
5899/*
5900 * Remove any existing snapshot.
5901 */
5902 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005903clear_snapshot(tp)
5904 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005906 clear_snapshot_rec(tp->tp_snapshot);
5907 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908}
5909
5910 static void
5911clear_snapshot_rec(fr)
5912 frame_T *fr;
5913{
5914 if (fr != NULL)
5915 {
5916 clear_snapshot_rec(fr->fr_next);
5917 clear_snapshot_rec(fr->fr_child);
5918 vim_free(fr);
5919 }
5920}
5921
5922/*
5923 * Restore a previously created snapshot, if there is any.
5924 * This is only done if the screen size didn't change and the window layout is
5925 * still the same.
5926 */
5927 static void
5928restore_snapshot(close_curwin)
5929 int close_curwin; /* closing current window */
5930{
5931 win_T *wp;
5932
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005933 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005935 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005937 && curtab->tp_snapshot->fr_height == topframe->fr_height
5938 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005940 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 win_comp_pos();
5942 if (wp != NULL && close_curwin)
5943 win_goto(wp);
5944 redraw_all_later(CLEAR);
5945 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005946 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947}
5948
5949/*
5950 * Check if frames "sn" and "fr" have the same layout, same following frames
5951 * and same children.
5952 */
5953 static int
5954check_snapshot_rec(sn, fr)
5955 frame_T *sn;
5956 frame_T *fr;
5957{
5958 if (sn->fr_layout != fr->fr_layout
5959 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5960 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5961 || (sn->fr_next != NULL
5962 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5963 || (sn->fr_child != NULL
5964 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5965 return FAIL;
5966 return OK;
5967}
5968
5969/*
5970 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
5971 * following frames and children.
5972 * Returns a pointer to the old current window, or NULL.
5973 */
5974 static win_T *
5975restore_snapshot_rec(sn, fr)
5976 frame_T *sn;
5977 frame_T *fr;
5978{
5979 win_T *wp = NULL;
5980 win_T *wp2;
5981
5982 fr->fr_height = sn->fr_height;
5983# ifdef FEAT_VERTSPLIT
5984 fr->fr_width = sn->fr_width;
5985# endif
5986 if (fr->fr_layout == FR_LEAF)
5987 {
5988 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5989# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005990 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991# endif
5992 wp = sn->fr_win;
5993 }
5994 if (sn->fr_next != NULL)
5995 {
5996 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5997 if (wp2 != NULL)
5998 wp = wp2;
5999 }
6000 if (sn->fr_child != NULL)
6001 {
6002 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6003 if (wp2 != NULL)
6004 wp = wp2;
6005 }
6006 return wp;
6007}
6008
6009#endif
6010
6011#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6012/*
6013 * Return TRUE if there is any vertically split window.
6014 */
6015 int
6016win_hasvertsplit()
6017{
6018 frame_T *fr;
6019
6020 if (topframe->fr_layout == FR_ROW)
6021 return TRUE;
6022
6023 if (topframe->fr_layout == FR_COL)
6024 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6025 if (fr->fr_layout == FR_ROW)
6026 return TRUE;
6027
6028 return FALSE;
6029}
6030#endif