blob: b401fc1eba0df18e246f265ddf11339058b5a871 [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 Moolenaar779b74b2006-04-10 14:55:34 +00003530 * Enter window "wp" in tab page "tp".
3531 * Also updates the GUI tab.
3532 */
3533 void
3534goto_tabpage_win(tp, wp)
3535 tabpage_T *tp;
3536 win_T *wp;
3537{
3538 goto_tabpage_tp(tp);
3539 if (curtab == tp && win_valid(wp))
3540 {
3541 win_enter(wp, TRUE);
3542# ifdef FEAT_GUI_TABLINE
3543 if (gui_use_tabline())
3544 gui_mch_set_curtab(tabpage_index(curtab));
3545# endif
3546 }
3547}
3548
3549/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003550 * Move the current tab page to before tab page "nr".
3551 */
3552 void
3553tabpage_move(nr)
3554 int nr;
3555{
3556 int n = nr;
3557 tabpage_T *tp;
3558
3559 if (first_tabpage->tp_next == NULL)
3560 return;
3561
3562 /* Remove the current tab page from the list of tab pages. */
3563 if (curtab == first_tabpage)
3564 first_tabpage = curtab->tp_next;
3565 else
3566 {
3567 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3568 if (tp->tp_next == curtab)
3569 break;
3570 if (tp == NULL) /* "cannot happen" */
3571 return;
3572 tp->tp_next = curtab->tp_next;
3573 }
3574
3575 /* Re-insert it at the specified position. */
3576 if (n == 0)
3577 {
3578 curtab->tp_next = first_tabpage;
3579 first_tabpage = curtab;
3580 }
3581 else
3582 {
3583 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3584 --n;
3585 curtab->tp_next = tp->tp_next;
3586 tp->tp_next = curtab;
3587 }
3588
3589 /* Need to redraw the tabline. Tab page contents doesn't change. */
3590 redraw_tabline = TRUE;
3591}
3592
3593
3594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 * Go to another window.
3596 * When jumping to another buffer, stop Visual mode. Do this before
3597 * changing windows so we can yank the selection into the '*' register.
3598 * When jumping to another window on the same buffer, adjust its cursor
3599 * position to keep the same Visual area.
3600 */
3601 void
3602win_goto(wp)
3603 win_T *wp;
3604{
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003605 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
3607 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003608 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 return;
3610 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003611#ifdef FEAT_AUTOCMD
3612 if (curbuf_locked())
3613 return;
3614#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616#ifdef FEAT_VISUAL
3617 if (wp->w_buffer != curbuf)
3618 reset_VIsual_and_resel();
3619 else if (VIsual_active)
3620 wp->w_cursor = curwin->w_cursor;
3621#endif
3622
3623#ifdef FEAT_GUI
3624 need_mouse_correct = TRUE;
3625#endif
3626 win_enter(wp, TRUE);
3627}
3628
3629#if defined(FEAT_PERL) || defined(PROTO)
3630/*
3631 * Find window number "winnr" (counting top to bottom).
3632 */
3633 win_T *
3634win_find_nr(winnr)
3635 int winnr;
3636{
3637 win_T *wp;
3638
3639# ifdef FEAT_WINDOWS
3640 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3641 if (--winnr == 0)
3642 break;
3643 return wp;
3644# else
3645 return curwin;
3646# endif
3647}
3648#endif
3649
3650#ifdef FEAT_VERTSPLIT
3651/*
3652 * Move to window above or below "count" times.
3653 */
3654 static void
3655win_goto_ver(up, count)
3656 int up; /* TRUE to go to win above */
3657 long count;
3658{
3659 frame_T *fr;
3660 frame_T *nfr;
3661 frame_T *foundfr;
3662
3663 foundfr = curwin->w_frame;
3664 while (count--)
3665 {
3666 /*
3667 * First go upwards in the tree of frames until we find a upwards or
3668 * downwards neighbor.
3669 */
3670 fr = foundfr;
3671 for (;;)
3672 {
3673 if (fr == topframe)
3674 goto end;
3675 if (up)
3676 nfr = fr->fr_prev;
3677 else
3678 nfr = fr->fr_next;
3679 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3680 break;
3681 fr = fr->fr_parent;
3682 }
3683
3684 /*
3685 * Now go downwards to find the bottom or top frame in it.
3686 */
3687 for (;;)
3688 {
3689 if (nfr->fr_layout == FR_LEAF)
3690 {
3691 foundfr = nfr;
3692 break;
3693 }
3694 fr = nfr->fr_child;
3695 if (nfr->fr_layout == FR_ROW)
3696 {
3697 /* Find the frame at the cursor row. */
3698 while (fr->fr_next != NULL
3699 && frame2win(fr)->w_wincol + fr->fr_width
3700 <= curwin->w_wincol + curwin->w_wcol)
3701 fr = fr->fr_next;
3702 }
3703 if (nfr->fr_layout == FR_COL && up)
3704 while (fr->fr_next != NULL)
3705 fr = fr->fr_next;
3706 nfr = fr;
3707 }
3708 }
3709end:
3710 if (foundfr != NULL)
3711 win_goto(foundfr->fr_win);
3712}
3713
3714/*
3715 * Move to left or right window.
3716 */
3717 static void
3718win_goto_hor(left, count)
3719 int left; /* TRUE to go to left win */
3720 long count;
3721{
3722 frame_T *fr;
3723 frame_T *nfr;
3724 frame_T *foundfr;
3725
3726 foundfr = curwin->w_frame;
3727 while (count--)
3728 {
3729 /*
3730 * First go upwards in the tree of frames until we find a left or
3731 * right neighbor.
3732 */
3733 fr = foundfr;
3734 for (;;)
3735 {
3736 if (fr == topframe)
3737 goto end;
3738 if (left)
3739 nfr = fr->fr_prev;
3740 else
3741 nfr = fr->fr_next;
3742 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3743 break;
3744 fr = fr->fr_parent;
3745 }
3746
3747 /*
3748 * Now go downwards to find the leftmost or rightmost frame in it.
3749 */
3750 for (;;)
3751 {
3752 if (nfr->fr_layout == FR_LEAF)
3753 {
3754 foundfr = nfr;
3755 break;
3756 }
3757 fr = nfr->fr_child;
3758 if (nfr->fr_layout == FR_COL)
3759 {
3760 /* Find the frame at the cursor row. */
3761 while (fr->fr_next != NULL
3762 && frame2win(fr)->w_winrow + fr->fr_height
3763 <= curwin->w_winrow + curwin->w_wrow)
3764 fr = fr->fr_next;
3765 }
3766 if (nfr->fr_layout == FR_ROW && left)
3767 while (fr->fr_next != NULL)
3768 fr = fr->fr_next;
3769 nfr = fr;
3770 }
3771 }
3772end:
3773 if (foundfr != NULL)
3774 win_goto(foundfr->fr_win);
3775}
3776#endif
3777
3778/*
3779 * Make window "wp" the current window.
3780 */
3781 void
3782win_enter(wp, undo_sync)
3783 win_T *wp;
3784 int undo_sync;
3785{
3786 win_enter_ext(wp, undo_sync, FALSE);
3787}
3788
3789/*
3790 * Make window wp the current window.
3791 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3792 * been closed and isn't valid.
3793 */
3794 static void
3795win_enter_ext(wp, undo_sync, curwin_invalid)
3796 win_T *wp;
3797 int undo_sync;
3798 int curwin_invalid;
3799{
3800#ifdef FEAT_AUTOCMD
3801 int other_buffer = FALSE;
3802#endif
3803
3804 if (wp == curwin && !curwin_invalid) /* nothing to do */
3805 return;
3806
3807#ifdef FEAT_AUTOCMD
3808 if (!curwin_invalid)
3809 {
3810 /*
3811 * Be careful: If autocommands delete the window, return now.
3812 */
3813 if (wp->w_buffer != curbuf)
3814 {
3815 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3816 other_buffer = TRUE;
3817 if (!win_valid(wp))
3818 return;
3819 }
3820 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3821 if (!win_valid(wp))
3822 return;
3823# ifdef FEAT_EVAL
3824 /* autocmds may abort script processing */
3825 if (aborting())
3826 return;
3827# endif
3828 }
3829#endif
3830
3831 /* sync undo before leaving the current buffer */
3832 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003833 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 /* may have to copy the buffer options when 'cpo' contains 'S' */
3835 if (wp->w_buffer != curbuf)
3836 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3837 if (!curwin_invalid)
3838 {
3839 prevwin = curwin; /* remember for CTRL-W p */
3840 curwin->w_redr_status = TRUE;
3841 }
3842 curwin = wp;
3843 curbuf = wp->w_buffer;
3844 check_cursor();
3845#ifdef FEAT_VIRTUALEDIT
3846 if (!virtual_active())
3847 curwin->w_cursor.coladd = 0;
3848#endif
3849 changed_line_abv_curs(); /* assume cursor position needs updating */
3850
3851 if (curwin->w_localdir != NULL)
3852 {
3853 /* Window has a local directory: Save current directory as global
3854 * directory (unless that was done already) and change to the local
3855 * directory. */
3856 if (globaldir == NULL)
3857 {
3858 char_u cwd[MAXPATHL];
3859
3860 if (mch_dirname(cwd, MAXPATHL) == OK)
3861 globaldir = vim_strsave(cwd);
3862 }
3863 mch_chdir((char *)curwin->w_localdir);
3864 shorten_fnames(TRUE);
3865 }
3866 else if (globaldir != NULL)
3867 {
3868 /* Window doesn't have a local directory and we are not in the global
3869 * directory: Change to the global directory. */
3870 mch_chdir((char *)globaldir);
3871 vim_free(globaldir);
3872 globaldir = NULL;
3873 shorten_fnames(TRUE);
3874 }
3875
3876#ifdef FEAT_AUTOCMD
3877 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3878 if (other_buffer)
3879 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3880#endif
3881
3882#ifdef FEAT_TITLE
3883 maketitle();
3884#endif
3885 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003886 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (restart_edit)
3888 redraw_later(VALID); /* causes status line redraw */
3889
3890 /* set window height to desired minimal value */
3891 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3892 win_setheight((int)p_wh);
3893 else if (curwin->w_height == 0)
3894 win_setheight(1);
3895
3896#ifdef FEAT_VERTSPLIT
3897 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003898 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 win_setwidth((int)p_wiw);
3900#endif
3901
3902#ifdef FEAT_MOUSE
3903 setmouse(); /* in case jumped to/from help buffer */
3904#endif
3905
Bram Moolenaar8dff8182006-04-06 20:18:50 +00003906#ifdef FEAT_AUTOCHDIR
3907 /* Change directories when the 'acd' option is set on and after
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 * switching windows. */
3909 if (p_acd && curbuf->b_ffname != NULL
3910 && vim_chdirfile(curbuf->b_ffname) == OK)
3911 shorten_fnames(TRUE);
3912#endif
3913}
3914
3915#endif /* FEAT_WINDOWS */
3916
3917#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3918/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003919 * Jump to the first open window that contains buffer "buf", if one exists.
3920 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 */
3922 win_T *
3923buf_jump_open_win(buf)
3924 buf_T *buf;
3925{
3926# ifdef FEAT_WINDOWS
3927 win_T *wp;
3928
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003929 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 if (wp->w_buffer == buf)
3931 break;
3932 if (wp != NULL)
3933 win_enter(wp, FALSE);
3934 return wp;
3935# else
3936 if (curwin->w_buffer == buf)
3937 return curwin;
3938 return NULL;
3939# endif
3940}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003941
3942/*
3943 * Jump to the first open window in any tab page that contains buffer "buf",
3944 * if one exists.
3945 * Returns a pointer to the window found, otherwise NULL.
3946 */
3947 win_T *
3948buf_jump_open_tab(buf)
3949 buf_T *buf;
3950{
3951# ifdef FEAT_WINDOWS
3952 win_T *wp;
3953 tabpage_T *tp;
3954
3955 /* First try the current tab page. */
3956 wp = buf_jump_open_win(buf);
3957 if (wp != NULL)
3958 return wp;
3959
3960 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3961 if (tp != curtab)
3962 {
3963 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
3964 if (wp->w_buffer == buf)
3965 break;
3966 if (wp != NULL)
3967 {
3968 goto_tabpage_win(tp, wp);
3969 if (curwin != wp)
3970 wp = NULL; /* something went wrong */
3971 break;
3972 }
3973 }
3974
3975 return wp;
3976# else
3977 if (curwin->w_buffer == buf)
3978 return curwin;
3979 return NULL;
3980# endif
3981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982#endif
3983
3984/*
3985 * allocate a window structure and link it in the window list
3986 */
3987/*ARGSUSED*/
3988 static win_T *
3989win_alloc(after)
3990 win_T *after;
3991{
3992 win_T *newwin;
3993
3994 /*
3995 * allocate window structure and linesizes arrays
3996 */
3997 newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3998 if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3999 {
4000 vim_free(newwin);
4001 newwin = NULL;
4002 }
4003
4004 if (newwin != NULL)
4005 {
4006 /*
4007 * link the window in the window list
4008 */
4009#ifdef FEAT_WINDOWS
4010 win_append(after, newwin);
4011#endif
4012#ifdef FEAT_VERTSPLIT
4013 newwin->w_wincol = 0;
4014 newwin->w_width = Columns;
4015#endif
4016
4017 /* position the display and the cursor at the top of the file. */
4018 newwin->w_topline = 1;
4019#ifdef FEAT_DIFF
4020 newwin->w_topfill = 0;
4021#endif
4022 newwin->w_botline = 2;
4023 newwin->w_cursor.lnum = 1;
4024#ifdef FEAT_SCROLLBIND
4025 newwin->w_scbind_pos = 1;
4026#endif
4027
4028 /* We won't calculate w_fraction until resizing the window */
4029 newwin->w_fraction = 0;
4030 newwin->w_prev_fraction_row = -1;
4031
4032#ifdef FEAT_GUI
4033 if (gui.in_use)
4034 {
4035 out_flush();
4036 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
4037 SBAR_LEFT, newwin);
4038 gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
4039 SBAR_RIGHT, newwin);
4040 }
4041#endif
4042#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004043 /* init w: variables */
4044 init_var_dict(&newwin->w_vars, &newwin->w_winvar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045#endif
4046#ifdef FEAT_FOLDING
4047 foldInitWin(newwin);
4048#endif
4049 }
4050 return newwin;
4051}
4052
4053#if defined(FEAT_WINDOWS) || defined(PROTO)
4054
4055/*
4056 * remove window 'wp' from the window list and free the structure
4057 */
4058 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004059win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004061 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
4063 int i;
4064
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004065#ifdef FEAT_MZSCHEME
4066 mzscheme_window_free(wp);
4067#endif
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069#ifdef FEAT_PERL
4070 perl_win_free(wp);
4071#endif
4072
4073#ifdef FEAT_PYTHON
4074 python_window_free(wp);
4075#endif
4076
4077#ifdef FEAT_TCL
4078 tcl_window_free(wp);
4079#endif
4080
4081#ifdef FEAT_RUBY
4082 ruby_window_free(wp);
4083#endif
4084
4085 clear_winopt(&wp->w_onebuf_opt);
4086 clear_winopt(&wp->w_allbuf_opt);
4087
4088#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00004089 vars_clear(&wp->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#endif
4091
4092 if (prevwin == wp)
4093 prevwin = NULL;
4094 win_free_lsize(wp);
4095
4096 for (i = 0; i < wp->w_tagstacklen; ++i)
4097 vim_free(wp->w_tagstack[i].tagname);
4098
4099 vim_free(wp->w_localdir);
4100#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004101 vim_free(wp->w_match[0].regprog);
4102 vim_free(wp->w_match[1].regprog);
4103 vim_free(wp->w_match[2].regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
4105#ifdef FEAT_JUMPLIST
4106 free_jumplist(wp);
4107#endif
4108
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004109#ifdef FEAT_QUICKFIX
4110 qf_free_all(wp);
4111#endif
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#ifdef FEAT_GUI
4114 if (gui.in_use)
4115 {
4116 out_flush();
4117 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4118 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4119 }
4120#endif /* FEAT_GUI */
4121
Bram Moolenaarf740b292006-02-16 22:11:02 +00004122 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 vim_free(wp);
4124}
4125
4126/*
4127 * Append window "wp" in the window list after window "after".
4128 */
4129 static void
4130win_append(after, wp)
4131 win_T *after, *wp;
4132{
4133 win_T *before;
4134
4135 if (after == NULL) /* after NULL is in front of the first */
4136 before = firstwin;
4137 else
4138 before = after->w_next;
4139
4140 wp->w_next = before;
4141 wp->w_prev = after;
4142 if (after == NULL)
4143 firstwin = wp;
4144 else
4145 after->w_next = wp;
4146 if (before == NULL)
4147 lastwin = wp;
4148 else
4149 before->w_prev = wp;
4150}
4151
4152/*
4153 * Remove a window from the window list.
4154 */
4155 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004156win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004158 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 if (wp->w_prev != NULL)
4161 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004162 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004164 else
4165 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (wp->w_next != NULL)
4167 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004168 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004170 else
4171 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172}
4173
4174/*
4175 * Append frame "frp" in a frame list after frame "after".
4176 */
4177 static void
4178frame_append(after, frp)
4179 frame_T *after, *frp;
4180{
4181 frp->fr_next = after->fr_next;
4182 after->fr_next = frp;
4183 if (frp->fr_next != NULL)
4184 frp->fr_next->fr_prev = frp;
4185 frp->fr_prev = after;
4186}
4187
4188/*
4189 * Insert frame "frp" in a frame list before frame "before".
4190 */
4191 static void
4192frame_insert(before, frp)
4193 frame_T *before, *frp;
4194{
4195 frp->fr_next = before;
4196 frp->fr_prev = before->fr_prev;
4197 before->fr_prev = frp;
4198 if (frp->fr_prev != NULL)
4199 frp->fr_prev->fr_next = frp;
4200 else
4201 frp->fr_parent->fr_child = frp;
4202}
4203
4204/*
4205 * Remove a frame from a frame list.
4206 */
4207 static void
4208frame_remove(frp)
4209 frame_T *frp;
4210{
4211 if (frp->fr_prev != NULL)
4212 frp->fr_prev->fr_next = frp->fr_next;
4213 else
4214 frp->fr_parent->fr_child = frp->fr_next;
4215 if (frp->fr_next != NULL)
4216 frp->fr_next->fr_prev = frp->fr_prev;
4217}
4218
4219#endif /* FEAT_WINDOWS */
4220
4221/*
4222 * Allocate w_lines[] for window "wp".
4223 * Return FAIL for failure, OK for success.
4224 */
4225 int
4226win_alloc_lines(wp)
4227 win_T *wp;
4228{
4229 wp->w_lines_valid = 0;
4230 wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4231 if (wp->w_lines == NULL)
4232 return FAIL;
4233 return OK;
4234}
4235
4236/*
4237 * free lsize arrays for a window
4238 */
4239 void
4240win_free_lsize(wp)
4241 win_T *wp;
4242{
4243 vim_free(wp->w_lines);
4244 wp->w_lines = NULL;
4245}
4246
4247/*
4248 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004249 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 */
4251 void
4252shell_new_rows()
4253{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004254 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 if (firstwin == NULL) /* not initialized yet */
4257 return;
4258#ifdef FEAT_WINDOWS
4259 if (h < frame_minheight(topframe, NULL))
4260 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004261
4262 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 * that doesn't result in the right height, forget about that option. */
4264 frame_new_height(topframe, h, FALSE, TRUE);
4265 if (topframe->fr_height != h)
4266 frame_new_height(topframe, h, FALSE, FALSE);
4267
4268 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4269#else
4270 if (h < 1)
4271 h = 1;
4272 win_new_height(firstwin, h);
4273#endif
4274 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004275#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004276 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004277#endif
4278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279#if 0
4280 /* Disabled: don't want making the screen smaller make a window larger. */
4281 if (p_ea)
4282 win_equal(curwin, FALSE, 'v');
4283#endif
4284}
4285
4286#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4287/*
4288 * Called from win_new_shellsize() after Columns changed.
4289 */
4290 void
4291shell_new_columns()
4292{
4293 if (firstwin == NULL) /* not initialized yet */
4294 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004295
4296 /* First try setting the widths of windows with 'winfixwidth'. If that
4297 * doesn't result in the right width, forget about that option. */
4298 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4299 if (topframe->fr_width != Columns)
4300 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4303#if 0
4304 /* Disabled: don't want making the screen smaller make a window larger. */
4305 if (p_ea)
4306 win_equal(curwin, FALSE, 'h');
4307#endif
4308}
4309#endif
4310
4311#if defined(FEAT_CMDWIN) || defined(PROTO)
4312/*
4313 * Save the size of all windows in "gap".
4314 */
4315 void
4316win_size_save(gap)
4317 garray_T *gap;
4318
4319{
4320 win_T *wp;
4321
4322 ga_init2(gap, (int)sizeof(int), 1);
4323 if (ga_grow(gap, win_count() * 2) == OK)
4324 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4325 {
4326 ((int *)gap->ga_data)[gap->ga_len++] =
4327 wp->w_width + wp->w_vsep_width;
4328 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4329 }
4330}
4331
4332/*
4333 * Restore window sizes, but only if the number of windows is still the same.
4334 * Does not free the growarray.
4335 */
4336 void
4337win_size_restore(gap)
4338 garray_T *gap;
4339{
4340 win_T *wp;
4341 int i;
4342
4343 if (win_count() * 2 == gap->ga_len)
4344 {
4345 i = 0;
4346 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4347 {
4348 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4349 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4350 }
4351 /* recompute the window positions */
4352 (void)win_comp_pos();
4353 }
4354}
4355#endif /* FEAT_CMDWIN */
4356
4357#if defined(FEAT_WINDOWS) || defined(PROTO)
4358/*
4359 * Update the position for all windows, using the width and height of the
4360 * frames.
4361 * Returns the row just after the last window.
4362 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004363 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364win_comp_pos()
4365{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004366 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 int col = 0;
4368
4369 frame_comp_pos(topframe, &row, &col);
4370 return row;
4371}
4372
4373/*
4374 * Update the position of the windows in frame "topfrp", using the width and
4375 * height of the frames.
4376 * "*row" and "*col" are the top-left position of the frame. They are updated
4377 * to the bottom-right position plus one.
4378 */
4379 static void
4380frame_comp_pos(topfrp, row, col)
4381 frame_T *topfrp;
4382 int *row;
4383 int *col;
4384{
4385 win_T *wp;
4386 frame_T *frp;
4387#ifdef FEAT_VERTSPLIT
4388 int startcol;
4389 int startrow;
4390#endif
4391
4392 wp = topfrp->fr_win;
4393 if (wp != NULL)
4394 {
4395 if (wp->w_winrow != *row
4396#ifdef FEAT_VERTSPLIT
4397 || wp->w_wincol != *col
4398#endif
4399 )
4400 {
4401 /* position changed, redraw */
4402 wp->w_winrow = *row;
4403#ifdef FEAT_VERTSPLIT
4404 wp->w_wincol = *col;
4405#endif
4406 redraw_win_later(wp, NOT_VALID);
4407 wp->w_redr_status = TRUE;
4408 }
4409 *row += wp->w_height + wp->w_status_height;
4410#ifdef FEAT_VERTSPLIT
4411 *col += wp->w_width + wp->w_vsep_width;
4412#endif
4413 }
4414 else
4415 {
4416#ifdef FEAT_VERTSPLIT
4417 startrow = *row;
4418 startcol = *col;
4419#endif
4420 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4421 {
4422#ifdef FEAT_VERTSPLIT
4423 if (topfrp->fr_layout == FR_ROW)
4424 *row = startrow; /* all frames are at the same row */
4425 else
4426 *col = startcol; /* all frames are at the same col */
4427#endif
4428 frame_comp_pos(frp, row, col);
4429 }
4430 }
4431}
4432
4433#endif /* FEAT_WINDOWS */
4434
4435/*
4436 * Set current window height and take care of repositioning other windows to
4437 * fit around it.
4438 */
4439 void
4440win_setheight(height)
4441 int height;
4442{
4443 win_setheight_win(height, curwin);
4444}
4445
4446/*
4447 * Set the window height of window "win" and take care of repositioning other
4448 * windows to fit around it.
4449 */
4450 void
4451win_setheight_win(height, win)
4452 int height;
4453 win_T *win;
4454{
4455 int row;
4456
4457 if (win == curwin)
4458 {
4459 /* Always keep current window at least one line high, even when
4460 * 'winminheight' is zero. */
4461#ifdef FEAT_WINDOWS
4462 if (height < p_wmh)
4463 height = p_wmh;
4464#endif
4465 if (height == 0)
4466 height = 1;
4467 }
4468
4469#ifdef FEAT_WINDOWS
4470 frame_setheight(win->w_frame, height + win->w_status_height);
4471
4472 /* recompute the window positions */
4473 row = win_comp_pos();
4474#else
4475 if (height > topframe->fr_height)
4476 height = topframe->fr_height;
4477 win->w_height = height;
4478 row = height;
4479#endif
4480
4481 /*
4482 * If there is extra space created between the last window and the command
4483 * line, clear it.
4484 */
4485 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4486 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4487 cmdline_row = row;
4488 msg_row = row;
4489 msg_col = 0;
4490
4491 redraw_all_later(NOT_VALID);
4492}
4493
4494#if defined(FEAT_WINDOWS) || defined(PROTO)
4495
4496/*
4497 * Set the height of a frame to "height" and take care that all frames and
4498 * windows inside it are resized. Also resize frames on the left and right if
4499 * the are in the same FR_ROW frame.
4500 *
4501 * Strategy:
4502 * If the frame is part of a FR_COL frame, try fitting the frame in that
4503 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4504 * go to containing frames to resize them and make room.
4505 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4506 * Check for the minimal height of the FR_ROW frame.
4507 * At the top level we can also use change the command line height.
4508 */
4509 static void
4510frame_setheight(curfrp, height)
4511 frame_T *curfrp;
4512 int height;
4513{
4514 int room; /* total number of lines available */
4515 int take; /* number of lines taken from other windows */
4516 int room_cmdline; /* lines available from cmdline */
4517 int run;
4518 frame_T *frp;
4519 int h;
4520 int room_reserved;
4521
4522 /* If the height already is the desired value, nothing to do. */
4523 if (curfrp->fr_height == height)
4524 return;
4525
4526 if (curfrp->fr_parent == NULL)
4527 {
4528 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004529 if (height > ROWS_AVAIL)
4530 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 if (height > 0)
4532 frame_new_height(curfrp, height, FALSE, FALSE);
4533 }
4534 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4535 {
4536 /* Row of frames: Also need to resize frames left and right of this
4537 * one. First check for the minimal height of these. */
4538 h = frame_minheight(curfrp->fr_parent, NULL);
4539 if (height < h)
4540 height = h;
4541 frame_setheight(curfrp->fr_parent, height);
4542 }
4543 else
4544 {
4545 /*
4546 * Column of frames: try to change only frames in this column.
4547 */
4548#ifdef FEAT_VERTSPLIT
4549 /*
4550 * Do this twice:
4551 * 1: compute room available, if it's not enough try resizing the
4552 * containing frame.
4553 * 2: compute the room available and adjust the height to it.
4554 * Try not to reduce the height of a window with 'winfixheight' set.
4555 */
4556 for (run = 1; run <= 2; ++run)
4557#else
4558 for (;;)
4559#endif
4560 {
4561 room = 0;
4562 room_reserved = 0;
4563 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4564 frp = frp->fr_next)
4565 {
4566 if (frp != curfrp
4567 && frp->fr_win != NULL
4568 && frp->fr_win->w_p_wfh)
4569 room_reserved += frp->fr_height;
4570 room += frp->fr_height;
4571 if (frp != curfrp)
4572 room -= frame_minheight(frp, NULL);
4573 }
4574#ifdef FEAT_VERTSPLIT
4575 if (curfrp->fr_width != Columns)
4576 room_cmdline = 0;
4577 else
4578#endif
4579 {
4580 room_cmdline = Rows - p_ch - (lastwin->w_winrow
4581 + lastwin->w_height + lastwin->w_status_height);
4582 if (room_cmdline < 0)
4583 room_cmdline = 0;
4584 }
4585
4586 if (height <= room + room_cmdline)
4587 break;
4588#ifdef FEAT_VERTSPLIT
4589 if (run == 2 || curfrp->fr_width == Columns)
4590#endif
4591 {
4592 if (height > room + room_cmdline)
4593 height = room + room_cmdline;
4594 break;
4595 }
4596#ifdef FEAT_VERTSPLIT
4597 frame_setheight(curfrp->fr_parent, height
4598 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4599#endif
4600 /*NOTREACHED*/
4601 }
4602
4603 /*
4604 * Compute the number of lines we will take from others frames (can be
4605 * negative!).
4606 */
4607 take = height - curfrp->fr_height;
4608
4609 /* If there is not enough room, also reduce the height of a window
4610 * with 'winfixheight' set. */
4611 if (height > room + room_cmdline - room_reserved)
4612 room_reserved = room + room_cmdline - height;
4613 /* If there is only a 'winfixheight' window and making the
4614 * window smaller, need to make the other window taller. */
4615 if (take < 0 && room - curfrp->fr_height < room_reserved)
4616 room_reserved = 0;
4617
4618 if (take > 0 && room_cmdline > 0)
4619 {
4620 /* use lines from cmdline first */
4621 if (take < room_cmdline)
4622 room_cmdline = take;
4623 take -= room_cmdline;
4624 topframe->fr_height += room_cmdline;
4625 }
4626
4627 /*
4628 * set the current frame to the new height
4629 */
4630 frame_new_height(curfrp, height, FALSE, FALSE);
4631
4632 /*
4633 * First take lines from the frames after the current frame. If
4634 * that is not enough, takes lines from frames above the current
4635 * frame.
4636 */
4637 for (run = 0; run < 2; ++run)
4638 {
4639 if (run == 0)
4640 frp = curfrp->fr_next; /* 1st run: start with next window */
4641 else
4642 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4643 while (frp != NULL && take != 0)
4644 {
4645 h = frame_minheight(frp, NULL);
4646 if (room_reserved > 0
4647 && frp->fr_win != NULL
4648 && frp->fr_win->w_p_wfh)
4649 {
4650 if (room_reserved >= frp->fr_height)
4651 room_reserved -= frp->fr_height;
4652 else
4653 {
4654 if (frp->fr_height - room_reserved > take)
4655 room_reserved = frp->fr_height - take;
4656 take -= frp->fr_height - room_reserved;
4657 frame_new_height(frp, room_reserved, FALSE, FALSE);
4658 room_reserved = 0;
4659 }
4660 }
4661 else
4662 {
4663 if (frp->fr_height - take < h)
4664 {
4665 take -= frp->fr_height - h;
4666 frame_new_height(frp, h, FALSE, FALSE);
4667 }
4668 else
4669 {
4670 frame_new_height(frp, frp->fr_height - take,
4671 FALSE, FALSE);
4672 take = 0;
4673 }
4674 }
4675 if (run == 0)
4676 frp = frp->fr_next;
4677 else
4678 frp = frp->fr_prev;
4679 }
4680 }
4681 }
4682}
4683
4684#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4685/*
4686 * Set current window width and take care of repositioning other windows to
4687 * fit around it.
4688 */
4689 void
4690win_setwidth(width)
4691 int width;
4692{
4693 win_setwidth_win(width, curwin);
4694}
4695
4696 void
4697win_setwidth_win(width, wp)
4698 int width;
4699 win_T *wp;
4700{
4701 /* Always keep current window at least one column wide, even when
4702 * 'winminwidth' is zero. */
4703 if (wp == curwin)
4704 {
4705 if (width < p_wmw)
4706 width = p_wmw;
4707 if (width == 0)
4708 width = 1;
4709 }
4710
4711 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4712
4713 /* recompute the window positions */
4714 (void)win_comp_pos();
4715
4716 redraw_all_later(NOT_VALID);
4717}
4718
4719/*
4720 * Set the width of a frame to "width" and take care that all frames and
4721 * windows inside it are resized. Also resize frames above and below if the
4722 * are in the same FR_ROW frame.
4723 *
4724 * Strategy is similar to frame_setheight().
4725 */
4726 static void
4727frame_setwidth(curfrp, width)
4728 frame_T *curfrp;
4729 int width;
4730{
4731 int room; /* total number of lines available */
4732 int take; /* number of lines taken from other windows */
4733 int run;
4734 frame_T *frp;
4735 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004736 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 /* If the width already is the desired value, nothing to do. */
4739 if (curfrp->fr_width == width)
4740 return;
4741
4742 if (curfrp->fr_parent == NULL)
4743 /* topframe: can't change width */
4744 return;
4745
4746 if (curfrp->fr_parent->fr_layout == FR_COL)
4747 {
4748 /* Column of frames: Also need to resize frames above and below of
4749 * this one. First check for the minimal width of these. */
4750 w = frame_minwidth(curfrp->fr_parent, NULL);
4751 if (width < w)
4752 width = w;
4753 frame_setwidth(curfrp->fr_parent, width);
4754 }
4755 else
4756 {
4757 /*
4758 * Row of frames: try to change only frames in this row.
4759 *
4760 * Do this twice:
4761 * 1: compute room available, if it's not enough try resizing the
4762 * containing frame.
4763 * 2: compute the room available and adjust the width to it.
4764 */
4765 for (run = 1; run <= 2; ++run)
4766 {
4767 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004768 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4770 frp = frp->fr_next)
4771 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004772 if (frp != curfrp
4773 && frp->fr_win != NULL
4774 && frp->fr_win->w_p_wfw)
4775 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 room += frp->fr_width;
4777 if (frp != curfrp)
4778 room -= frame_minwidth(frp, NULL);
4779 }
4780
4781 if (width <= room)
4782 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004783 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 if (width > room)
4786 width = room;
4787 break;
4788 }
4789 frame_setwidth(curfrp->fr_parent, width
4790 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4791 }
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /*
4794 * Compute the number of lines we will take from others frames (can be
4795 * negative!).
4796 */
4797 take = width - curfrp->fr_width;
4798
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004799 /* If there is not enough room, also reduce the width of a window
4800 * with 'winfixwidth' set. */
4801 if (width > room - room_reserved)
4802 room_reserved = room - width;
4803 /* If there is only a 'winfixwidth' window and making the
4804 * window smaller, need to make the other window narrower. */
4805 if (take < 0 && room - curfrp->fr_width < room_reserved)
4806 room_reserved = 0;
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /*
4809 * set the current frame to the new width
4810 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004811 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 /*
4814 * First take lines from the frames right of the current frame. If
4815 * that is not enough, takes lines from frames left of the current
4816 * frame.
4817 */
4818 for (run = 0; run < 2; ++run)
4819 {
4820 if (run == 0)
4821 frp = curfrp->fr_next; /* 1st run: start with next window */
4822 else
4823 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
4824 while (frp != NULL && take != 0)
4825 {
4826 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004827 if (room_reserved > 0
4828 && frp->fr_win != NULL
4829 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004831 if (room_reserved >= frp->fr_width)
4832 room_reserved -= frp->fr_width;
4833 else
4834 {
4835 if (frp->fr_width - room_reserved > take)
4836 room_reserved = frp->fr_width - take;
4837 take -= frp->fr_width - room_reserved;
4838 frame_new_width(frp, room_reserved, FALSE, FALSE);
4839 room_reserved = 0;
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 else
4843 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004844 if (frp->fr_width - take < w)
4845 {
4846 take -= frp->fr_width - w;
4847 frame_new_width(frp, w, FALSE, FALSE);
4848 }
4849 else
4850 {
4851 frame_new_width(frp, frp->fr_width - take,
4852 FALSE, FALSE);
4853 take = 0;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 if (run == 0)
4857 frp = frp->fr_next;
4858 else
4859 frp = frp->fr_prev;
4860 }
4861 }
4862 }
4863}
4864#endif /* FEAT_VERTSPLIT */
4865
4866/*
4867 * Check 'winminheight' for a valid value.
4868 */
4869 void
4870win_setminheight()
4871{
4872 int room;
4873 int first = TRUE;
4874 win_T *wp;
4875
4876 /* loop until there is a 'winminheight' that is possible */
4877 while (p_wmh > 0)
4878 {
4879 /* TODO: handle vertical splits */
4880 room = -p_wh;
4881 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4882 room += wp->w_height - p_wmh;
4883 if (room >= 0)
4884 break;
4885 --p_wmh;
4886 if (first)
4887 {
4888 EMSG(_(e_noroom));
4889 first = FALSE;
4890 }
4891 }
4892}
4893
4894#ifdef FEAT_MOUSE
4895
4896/*
4897 * Status line of dragwin is dragged "offset" lines down (negative is up).
4898 */
4899 void
4900win_drag_status_line(dragwin, offset)
4901 win_T *dragwin;
4902 int offset;
4903{
4904 frame_T *curfr;
4905 frame_T *fr;
4906 int room;
4907 int row;
4908 int up; /* if TRUE, drag status line up, otherwise down */
4909 int n;
4910
4911 fr = dragwin->w_frame;
4912 curfr = fr;
4913 if (fr != topframe) /* more than one window */
4914 {
4915 fr = fr->fr_parent;
4916 /* When the parent frame is not a column of frames, its parent should
4917 * be. */
4918 if (fr->fr_layout != FR_COL)
4919 {
4920 curfr = fr;
4921 if (fr != topframe) /* only a row of windows, may drag statusline */
4922 fr = fr->fr_parent;
4923 }
4924 }
4925
4926 /* If this is the last frame in a column, may want to resize the parent
4927 * frame instead (go two up to skip a row of frames). */
4928 while (curfr != topframe && curfr->fr_next == NULL)
4929 {
4930 if (fr != topframe)
4931 fr = fr->fr_parent;
4932 curfr = fr;
4933 if (fr != topframe)
4934 fr = fr->fr_parent;
4935 }
4936
4937 if (offset < 0) /* drag up */
4938 {
4939 up = TRUE;
4940 offset = -offset;
4941 /* sum up the room of the current frame and above it */
4942 if (fr == curfr)
4943 {
4944 /* only one window */
4945 room = fr->fr_height - frame_minheight(fr, NULL);
4946 }
4947 else
4948 {
4949 room = 0;
4950 for (fr = fr->fr_child; ; fr = fr->fr_next)
4951 {
4952 room += fr->fr_height - frame_minheight(fr, NULL);
4953 if (fr == curfr)
4954 break;
4955 }
4956 }
4957 fr = curfr->fr_next; /* put fr at frame that grows */
4958 }
4959 else /* drag down */
4960 {
4961 up = FALSE;
4962 /*
4963 * Only dragging the last status line can reduce p_ch.
4964 */
4965 room = Rows - cmdline_row;
4966 if (curfr->fr_next == NULL)
4967 room -= 1;
4968 else
4969 room -= p_ch;
4970 if (room < 0)
4971 room = 0;
4972 /* sum up the room of frames below of the current one */
4973 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4974 room += fr->fr_height - frame_minheight(fr, NULL);
4975 fr = curfr; /* put fr at window that grows */
4976 }
4977
4978 if (room < offset) /* Not enough room */
4979 offset = room; /* Move as far as we can */
4980 if (offset <= 0)
4981 return;
4982
4983 /*
4984 * Grow frame fr by "offset" lines.
4985 * Doesn't happen when dragging the last status line up.
4986 */
4987 if (fr != NULL)
4988 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4989
4990 if (up)
4991 fr = curfr; /* current frame gets smaller */
4992 else
4993 fr = curfr->fr_next; /* next frame gets smaller */
4994
4995 /*
4996 * Now make the other frames smaller.
4997 */
4998 while (fr != NULL && offset > 0)
4999 {
5000 n = frame_minheight(fr, NULL);
5001 if (fr->fr_height - offset <= n)
5002 {
5003 offset -= fr->fr_height - n;
5004 frame_new_height(fr, n, !up, FALSE);
5005 }
5006 else
5007 {
5008 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5009 break;
5010 }
5011 if (up)
5012 fr = fr->fr_prev;
5013 else
5014 fr = fr->fr_next;
5015 }
5016 row = win_comp_pos();
5017 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5018 cmdline_row = row;
5019 p_ch = Rows - cmdline_row;
5020 if (p_ch < 1)
5021 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005022 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005023 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 showmode();
5025}
5026
5027#ifdef FEAT_VERTSPLIT
5028/*
5029 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5030 */
5031 void
5032win_drag_vsep_line(dragwin, offset)
5033 win_T *dragwin;
5034 int offset;
5035{
5036 frame_T *curfr;
5037 frame_T *fr;
5038 int room;
5039 int left; /* if TRUE, drag separator line left, otherwise right */
5040 int n;
5041
5042 fr = dragwin->w_frame;
5043 if (fr == topframe) /* only one window (cannot happe?) */
5044 return;
5045 curfr = fr;
5046 fr = fr->fr_parent;
5047 /* When the parent frame is not a row of frames, its parent should be. */
5048 if (fr->fr_layout != FR_ROW)
5049 {
5050 if (fr == topframe) /* only a column of windows (cannot happen?) */
5051 return;
5052 curfr = fr;
5053 fr = fr->fr_parent;
5054 }
5055
5056 /* If this is the last frame in a row, may want to resize a parent
5057 * frame instead. */
5058 while (curfr->fr_next == NULL)
5059 {
5060 if (fr == topframe)
5061 break;
5062 curfr = fr;
5063 fr = fr->fr_parent;
5064 if (fr != topframe)
5065 {
5066 curfr = fr;
5067 fr = fr->fr_parent;
5068 }
5069 }
5070
5071 if (offset < 0) /* drag left */
5072 {
5073 left = TRUE;
5074 offset = -offset;
5075 /* sum up the room of the current frame and left of it */
5076 room = 0;
5077 for (fr = fr->fr_child; ; fr = fr->fr_next)
5078 {
5079 room += fr->fr_width - frame_minwidth(fr, NULL);
5080 if (fr == curfr)
5081 break;
5082 }
5083 fr = curfr->fr_next; /* put fr at frame that grows */
5084 }
5085 else /* drag right */
5086 {
5087 left = FALSE;
5088 /* sum up the room of frames right of the current one */
5089 room = 0;
5090 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5091 room += fr->fr_width - frame_minwidth(fr, NULL);
5092 fr = curfr; /* put fr at window that grows */
5093 }
5094
5095 if (room < offset) /* Not enough room */
5096 offset = room; /* Move as far as we can */
5097 if (offset <= 0) /* No room at all, quit. */
5098 return;
5099
5100 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005101 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 /* shrink other frames: current and at the left or at the right */
5104 if (left)
5105 fr = curfr; /* current frame gets smaller */
5106 else
5107 fr = curfr->fr_next; /* next frame gets smaller */
5108
5109 while (fr != NULL && offset > 0)
5110 {
5111 n = frame_minwidth(fr, NULL);
5112 if (fr->fr_width - offset <= n)
5113 {
5114 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005115 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117 else
5118 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005119 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 break;
5121 }
5122 if (left)
5123 fr = fr->fr_prev;
5124 else
5125 fr = fr->fr_next;
5126 }
5127 (void)win_comp_pos();
5128 redraw_all_later(NOT_VALID);
5129}
5130#endif /* FEAT_VERTSPLIT */
5131#endif /* FEAT_MOUSE */
5132
5133#endif /* FEAT_WINDOWS */
5134
5135/*
5136 * Set the height of a window.
5137 * This takes care of the things inside the window, not what happens to the
5138 * window position, the frame or to other windows.
5139 */
5140 static void
5141win_new_height(wp, height)
5142 win_T *wp;
5143 int height;
5144{
5145 linenr_T lnum;
Bram Moolenaar34114692005-01-02 11:28:13 +00005146 linenr_T bot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 int sline, line_size;
Bram Moolenaar34114692005-01-02 11:28:13 +00005148 int space;
5149 int did_below = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#define FRACTION_MULT 16384L
5151
5152 /* Don't want a negative height. Happens when splitting a tiny window.
5153 * Will equalize heights soon to fix it. */
5154 if (height < 0)
5155 height = 0;
5156
5157 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5158 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5159 + FRACTION_MULT / 2) / (long)wp->w_height;
5160
5161 wp->w_height = height;
5162 wp->w_skipcol = 0;
5163
5164 /* Don't change w_topline when height is zero. Don't set w_topline when
5165 * 'scrollbind' is set and this isn't the current window. */
5166 if (height > 0
5167#ifdef FEAT_SCROLLBIND
5168 && (!wp->w_p_scb || wp == curwin)
5169#endif
5170 )
5171 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005172 /*
5173 * Find a value for w_topline that shows the cursor at the same
5174 * relative position in the window as before (more or less).
5175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 lnum = wp->w_cursor.lnum;
5177 if (lnum < 1) /* can happen when starting up */
5178 lnum = 1;
5179 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5180 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5181 sline = wp->w_wrow - line_size;
5182 if (sline < 0)
5183 {
5184 /*
5185 * Cursor line would go off top of screen if w_wrow was this high.
5186 */
5187 wp->w_wrow = line_size;
5188 }
5189 else
5190 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005191 space = height;
5192 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005194 space -= line_size;
5195 if (space > 0 && sline <= 0 && !did_below)
5196 {
5197 /* Try to use "~" lines below the text to avoid that text
5198 * is above the window while there are empty lines.
5199 * Subtract the rows below the cursor from "space" and
5200 * give the rest to "sline". */
5201 did_below = TRUE;
5202 bot = wp->w_cursor.lnum;
5203 while (space > 0)
5204 {
5205 if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5206 space = 0;
5207 else
5208 {
5209#ifdef FEAT_FOLDING
5210 hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5211#endif
5212 if (bot >= wp->w_buffer->b_ml.ml_line_count)
5213 break;
5214 ++bot;
5215 space -= plines_win(wp, bot, TRUE);
5216 }
5217 }
5218 if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5219 sline += space;
5220 }
5221 if (sline <= 0)
5222 break;
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#ifdef FEAT_FOLDING
5225 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5226 if (lnum == 1)
5227 {
5228 /* first line in buffer is folded */
5229 line_size = 1;
5230 --sline;
5231 break;
5232 }
5233#endif
5234 --lnum;
5235#ifdef FEAT_DIFF
5236 if (lnum == wp->w_topline)
5237 line_size = plines_win_nofill(wp, lnum, TRUE)
5238 + wp->w_topfill;
5239 else
5240#endif
5241 line_size = plines_win(wp, lnum, TRUE);
5242 sline -= line_size;
5243 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (sline < 0)
5246 {
5247 /*
5248 * Line we want at top would go off top of screen. Use next
5249 * line instead.
5250 */
5251#ifdef FEAT_FOLDING
5252 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5253#endif
5254 lnum++;
5255 wp->w_wrow -= line_size + sline;
5256 }
5257 else if (sline > 0)
5258 {
5259 /* First line of file reached, use that as topline. */
5260 lnum = 1;
5261 wp->w_wrow -= sline;
5262 }
5263 }
5264 set_topline(wp, lnum);
5265 }
5266
5267 if (wp == curwin)
5268 {
5269 if (p_so)
5270 update_topline();
5271 curs_columns(FALSE); /* validate w_wrow */
5272 }
5273 wp->w_prev_fraction_row = wp->w_wrow;
5274
5275 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005276 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277#ifdef FEAT_WINDOWS
5278 wp->w_redr_status = TRUE;
5279#endif
5280 invalidate_botline_win(wp);
5281}
5282
5283#ifdef FEAT_VERTSPLIT
5284/*
5285 * Set the width of a window.
5286 */
5287 static void
5288win_new_width(wp, width)
5289 win_T *wp;
5290 int width;
5291{
5292 wp->w_width = width;
5293 wp->w_lines_valid = 0;
5294 changed_line_abv_curs_win(wp);
5295 invalidate_botline_win(wp);
5296 if (wp == curwin)
5297 {
5298 update_topline();
5299 curs_columns(TRUE); /* validate w_wrow */
5300 }
5301 redraw_win_later(wp, NOT_VALID);
5302 wp->w_redr_status = TRUE;
5303}
5304#endif
5305
5306 void
5307win_comp_scroll(wp)
5308 win_T *wp;
5309{
5310 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5311 if (wp->w_p_scr == 0)
5312 wp->w_p_scr = 1;
5313}
5314
5315/*
5316 * command_height: called whenever p_ch has been changed
5317 */
5318 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005319command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
5321#ifdef FEAT_WINDOWS
5322 int h;
5323 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005324 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005326 /* Use the value of p_ch that we remembered. This is needed for when the
5327 * GUI starts up, we can't be sure in what order things happen. And when
5328 * p_ch was changed in another tab page. */
5329 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /* Find bottom frame with width of screen. */
5332 frp = lastwin->w_frame;
5333# ifdef FEAT_VERTSPLIT
5334 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5335 frp = frp->fr_parent;
5336# endif
5337
5338 /* Avoid changing the height of a window with 'winfixheight' set. */
5339 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5340 && frp->fr_win->w_p_wfh)
5341 frp = frp->fr_prev;
5342
5343 if (starting != NO_SCREEN)
5344 {
5345 cmdline_row = Rows - p_ch;
5346
5347 if (p_ch > old_p_ch) /* p_ch got bigger */
5348 {
5349 while (p_ch > old_p_ch)
5350 {
5351 if (frp == NULL)
5352 {
5353 EMSG(_(e_noroom));
5354 p_ch = old_p_ch;
5355 cmdline_row = Rows - p_ch;
5356 break;
5357 }
5358 h = frp->fr_height - frame_minheight(frp, NULL);
5359 if (h > p_ch - old_p_ch)
5360 h = p_ch - old_p_ch;
5361 old_p_ch += h;
5362 frame_add_height(frp, -h);
5363 frp = frp->fr_prev;
5364 }
5365
5366 /* Recompute window positions. */
5367 (void)win_comp_pos();
5368
5369 /* clear the lines added to cmdline */
5370 if (full_screen)
5371 screen_fill((int)(cmdline_row), (int)Rows, 0,
5372 (int)Columns, ' ', ' ', 0);
5373 msg_row = cmdline_row;
5374 redraw_cmdline = TRUE;
5375 return;
5376 }
5377
5378 if (msg_row < cmdline_row)
5379 msg_row = cmdline_row;
5380 redraw_cmdline = TRUE;
5381 }
5382 frame_add_height(frp, (int)(old_p_ch - p_ch));
5383
5384 /* Recompute window positions. */
5385 if (frp != lastwin->w_frame)
5386 (void)win_comp_pos();
5387#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005389 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390#endif
5391}
5392
5393#if defined(FEAT_WINDOWS) || defined(PROTO)
5394/*
5395 * Resize frame "frp" to be "n" lines higher (negative for less high).
5396 * Also resize the frames it is contained in.
5397 */
5398 static void
5399frame_add_height(frp, n)
5400 frame_T *frp;
5401 int n;
5402{
5403 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5404 for (;;)
5405 {
5406 frp = frp->fr_parent;
5407 if (frp == NULL)
5408 break;
5409 frp->fr_height += n;
5410 }
5411}
5412
5413/*
5414 * Add or remove a status line for the bottom window(s), according to the
5415 * value of 'laststatus'.
5416 */
5417 void
5418last_status(morewin)
5419 int morewin; /* pretend there are two or more windows */
5420{
5421 /* Don't make a difference between horizontal or vertical split. */
5422 last_status_rec(topframe, (p_ls == 2
5423 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5424}
5425
5426 static void
5427last_status_rec(fr, statusline)
5428 frame_T *fr;
5429 int statusline;
5430{
5431 frame_T *fp;
5432 win_T *wp;
5433
5434 if (fr->fr_layout == FR_LEAF)
5435 {
5436 wp = fr->fr_win;
5437 if (wp->w_status_height != 0 && !statusline)
5438 {
5439 /* remove status line */
5440 win_new_height(wp, wp->w_height + 1);
5441 wp->w_status_height = 0;
5442 comp_col();
5443 }
5444 else if (wp->w_status_height == 0 && statusline)
5445 {
5446 /* Find a frame to take a line from. */
5447 fp = fr;
5448 while (fp->fr_height <= frame_minheight(fp, NULL))
5449 {
5450 if (fp == topframe)
5451 {
5452 EMSG(_(e_noroom));
5453 return;
5454 }
5455 /* In a column of frames: go to frame above. If already at
5456 * the top or in a row of frames: go to parent. */
5457 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5458 fp = fp->fr_prev;
5459 else
5460 fp = fp->fr_parent;
5461 }
5462 wp->w_status_height = 1;
5463 if (fp != fr)
5464 {
5465 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5466 frame_fix_height(wp);
5467 (void)win_comp_pos();
5468 }
5469 else
5470 win_new_height(wp, wp->w_height - 1);
5471 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005472 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 }
5475#ifdef FEAT_VERTSPLIT
5476 else if (fr->fr_layout == FR_ROW)
5477 {
5478 /* vertically split windows, set status line for each one */
5479 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5480 last_status_rec(fp, statusline);
5481 }
5482#endif
5483 else
5484 {
5485 /* horizontally split window, set status line for last one */
5486 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5487 ;
5488 last_status_rec(fp, statusline);
5489 }
5490}
5491
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005492/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005493 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005494 */
5495 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005496tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005497{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005498#ifdef FEAT_GUI_TABLINE
5499 /* When the GUI has the tabline then this always returns zero. */
5500 if (gui_use_tabline())
5501 return 0;
5502#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005503 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005504 {
5505 case 0: return 0;
5506 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5507 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005508 return 1;
5509}
5510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511#endif /* FEAT_WINDOWS */
5512
5513#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5514/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005515 * Get the file name at the cursor.
5516 * If Visual mode is active, use the selected text if it's in one line.
5517 * Returns the name in allocated memory, NULL for failure.
5518 */
5519 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005520grab_file_name(count, file_lnum)
5521 long count;
5522 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005523{
5524# ifdef FEAT_VISUAL
5525 if (VIsual_active)
5526 {
5527 int len;
5528 char_u *ptr;
5529
5530 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5531 return NULL;
5532 return find_file_name_in_path(ptr, len,
5533 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5534 }
5535# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005536 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5537 file_lnum);
5538
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005539}
5540
5541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 * Return the file name under or after the cursor.
5543 *
5544 * The 'path' option is searched if the file name is not absolute.
5545 * The string returned has been alloc'ed and should be freed by the caller.
5546 * NULL is returned if the file name or file is not found.
5547 *
5548 * options:
5549 * FNAME_MESS give error messages
5550 * FNAME_EXP expand to path
5551 * FNAME_HYP check for hypertext link
5552 * FNAME_INCL apply "includeexpr"
5553 */
5554 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005555file_name_at_cursor(options, count, file_lnum)
5556 int options;
5557 long count;
5558 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005561 curwin->w_cursor.col, options, count, curbuf->b_ffname,
5562 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
5565/*
5566 * Return the name of the file under or after ptr[col].
5567 * Otherwise like file_name_at_cursor().
5568 */
5569 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005570file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 char_u *line;
5572 int col;
5573 int options;
5574 long count;
5575 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005576 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 char_u *ptr;
5579 int len;
5580
5581 /*
5582 * search forward for what could be the start of a file name
5583 */
5584 ptr = line + col;
5585 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00005586 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 if (*ptr == NUL) /* nothing found */
5588 {
5589 if (options & FNAME_MESS)
5590 EMSG(_("E446: No file name under cursor"));
5591 return NULL;
5592 }
5593
5594 /*
5595 * Search backward for first char of the file name.
5596 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5597 */
5598 while (ptr > line)
5599 {
5600#ifdef FEAT_MBYTE
5601 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5602 ptr -= len + 1;
5603 else
5604#endif
5605 if (vim_isfilec(ptr[-1])
5606 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5607 --ptr;
5608 else
5609 break;
5610 }
5611
5612 /*
5613 * Search forward for the last char of the file name.
5614 * Also allow "://" when ':' is not in 'isfname'.
5615 */
5616 len = 0;
5617 while (vim_isfilec(ptr[len])
5618 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5619#ifdef FEAT_MBYTE
5620 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005621 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 else
5623#endif
5624 ++len;
5625
5626 /*
5627 * If there is trailing punctuation, remove it.
5628 * But don't remove "..", could be a directory name.
5629 */
5630 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5631 && ptr[len - 2] != '.')
5632 --len;
5633
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005634 if (file_lnum != NULL)
5635 {
5636 char_u *p;
5637
5638 /* Get the number after the file name and a separator character */
5639 p = ptr + len;
5640 p = skipwhite(p);
5641 if (*p != NUL)
5642 {
5643 if (!isdigit(*p))
5644 ++p; /* skip the separator */
5645 p = skipwhite(p);
5646 if (isdigit(*p))
5647 *file_lnum = (int)getdigits(&p);
5648 }
5649 }
5650
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 return find_file_name_in_path(ptr, len, options, count, rel_fname);
5652}
5653
5654# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5655static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5656
5657 static char_u *
5658eval_includeexpr(ptr, len)
5659 char_u *ptr;
5660 int len;
5661{
5662 char_u *res;
5663
5664 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005665 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005666 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 set_vim_var_string(VV_FNAME, NULL, 0);
5668 return res;
5669}
5670#endif
5671
5672/*
5673 * Return the name of the file ptr[len] in 'path'.
5674 * Otherwise like file_name_at_cursor().
5675 */
5676 char_u *
5677find_file_name_in_path(ptr, len, options, count, rel_fname)
5678 char_u *ptr;
5679 int len;
5680 int options;
5681 long count;
5682 char_u *rel_fname; /* file we are searching relative to */
5683{
5684 char_u *file_name;
5685 int c;
5686# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5687 char_u *tofree = NULL;
5688
5689 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5690 {
5691 tofree = eval_includeexpr(ptr, len);
5692 if (tofree != NULL)
5693 {
5694 ptr = tofree;
5695 len = (int)STRLEN(ptr);
5696 }
5697 }
5698# endif
5699
5700 if (options & FNAME_EXP)
5701 {
5702 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5703 TRUE, rel_fname);
5704
5705# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5706 /*
5707 * If the file could not be found in a normal way, try applying
5708 * 'includeexpr' (unless done already).
5709 */
5710 if (file_name == NULL
5711 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5712 {
5713 tofree = eval_includeexpr(ptr, len);
5714 if (tofree != NULL)
5715 {
5716 ptr = tofree;
5717 len = (int)STRLEN(ptr);
5718 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5719 TRUE, rel_fname);
5720 }
5721 }
5722# endif
5723 if (file_name == NULL && (options & FNAME_MESS))
5724 {
5725 c = ptr[len];
5726 ptr[len] = NUL;
5727 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5728 ptr[len] = c;
5729 }
5730
5731 /* Repeat finding the file "count" times. This matters when it
5732 * appears several times in the path. */
5733 while (file_name != NULL && --count > 0)
5734 {
5735 vim_free(file_name);
5736 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5737 }
5738 }
5739 else
5740 file_name = vim_strnsave(ptr, len);
5741
5742# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5743 vim_free(tofree);
5744# endif
5745
5746 return file_name;
5747}
5748#endif /* FEAT_SEARCHPATH */
5749
5750/*
5751 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5752 * Also check for ":\\", which MS Internet Explorer accepts, return
5753 * URL_BACKSLASH.
5754 */
5755 static int
5756path_is_url(p)
5757 char_u *p;
5758{
5759 if (STRNCMP(p, "://", (size_t)3) == 0)
5760 return URL_SLASH;
5761 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5762 return URL_BACKSLASH;
5763 return 0;
5764}
5765
5766/*
5767 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
5768 * Return URL_BACKSLASH for "name:\\".
5769 * Return zero otherwise.
5770 */
5771 int
5772path_with_url(fname)
5773 char_u *fname;
5774{
5775 char_u *p;
5776
5777 for (p = fname; isalpha(*p); ++p)
5778 ;
5779 return path_is_url(p);
5780}
5781
5782/*
5783 * Return TRUE if "name" is a full (absolute) path name or URL.
5784 */
5785 int
5786vim_isAbsName(name)
5787 char_u *name;
5788{
5789 return (path_with_url(name) != 0 || mch_isFullName(name));
5790}
5791
5792/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005793 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 *
5795 * return FAIL for failure, OK otherwise
5796 */
5797 int
5798vim_FullName(fname, buf, len, force)
5799 char_u *fname, *buf;
5800 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00005801 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 int retval = OK;
5804 int url;
5805
5806 *buf = NUL;
5807 if (fname == NULL)
5808 return FAIL;
5809
5810 url = path_with_url(fname);
5811 if (!url)
5812 retval = mch_FullName(fname, buf, len, force);
5813 if (url || retval == FAIL)
5814 {
5815 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00005816 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5819 slash_adjust(buf);
5820#endif
5821 return retval;
5822}
5823
5824/*
5825 * Return the minimal number of rows that is needed on the screen to display
5826 * the current number of windows.
5827 */
5828 int
5829min_rows()
5830{
5831 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005832#ifdef FEAT_WINDOWS
5833 tabpage_T *tp;
5834 int n;
5835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837 if (firstwin == NULL) /* not initialized yet */
5838 return MIN_LINES;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005841 total = 0;
5842 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5843 {
5844 n = frame_minheight(tp->tp_topframe, NULL);
5845 if (total < n)
5846 total = n;
5847 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005848 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00005850 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005852 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 return total;
5854}
5855
5856/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005857 * Return TRUE if there is only one window (in the current tab page), not
5858 * counting a help or preview window, unless it is the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 */
5860 int
5861only_one_window()
5862{
5863#ifdef FEAT_WINDOWS
5864 int count = 0;
5865 win_T *wp;
5866
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005867 /* If there is another tab page there always is another window. */
5868 if (first_tabpage->tp_next != NULL)
5869 return FALSE;
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar92922402005-01-31 18:57:18 +00005872 if (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873# ifdef FEAT_QUICKFIX
5874 || wp->w_p_pvw
5875# endif
5876 ) || wp == curwin)
5877 ++count;
5878 return (count <= 1);
5879#else
5880 return TRUE;
5881#endif
5882}
5883
5884#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5885/*
5886 * Correct the cursor line number in other windows. Used after changing the
5887 * current buffer, and before applying autocommands.
5888 * When "do_curwin" is TRUE, also check current window.
5889 */
5890 void
5891check_lnums(do_curwin)
5892 int do_curwin;
5893{
5894 win_T *wp;
5895
5896#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 tabpage_T *tp;
5898
5899 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5901#else
5902 wp = curwin;
5903 if (do_curwin)
5904#endif
5905 {
5906 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5907 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5908 if (wp->w_topline > curbuf->b_ml.ml_line_count)
5909 wp->w_topline = curbuf->b_ml.ml_line_count;
5910 }
5911}
5912#endif
5913
5914#if defined(FEAT_WINDOWS) || defined(PROTO)
5915
5916/*
5917 * A snapshot of the window sizes, to restore them after closing the help
5918 * window.
5919 * Only these fields are used:
5920 * fr_layout
5921 * fr_width
5922 * fr_height
5923 * fr_next
5924 * fr_child
5925 * fr_win (only valid for the old curwin, NULL otherwise)
5926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
5928/*
5929 * Create a snapshot of the current frame sizes.
5930 */
5931 static void
5932make_snapshot()
5933{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005934 clear_snapshot(curtab);
5935 make_snapshot_rec(topframe, &curtab->tp_snapshot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936}
5937
5938 static void
5939make_snapshot_rec(fr, frp)
5940 frame_T *fr;
5941 frame_T **frp;
5942{
5943 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5944 if (*frp == NULL)
5945 return;
5946 (*frp)->fr_layout = fr->fr_layout;
5947# ifdef FEAT_VERTSPLIT
5948 (*frp)->fr_width = fr->fr_width;
5949# endif
5950 (*frp)->fr_height = fr->fr_height;
5951 if (fr->fr_next != NULL)
5952 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5953 if (fr->fr_child != NULL)
5954 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5955 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5956 (*frp)->fr_win = curwin;
5957}
5958
5959/*
5960 * Remove any existing snapshot.
5961 */
5962 static void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005963clear_snapshot(tp)
5964 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005966 clear_snapshot_rec(tp->tp_snapshot);
5967 tp->tp_snapshot = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970 static void
5971clear_snapshot_rec(fr)
5972 frame_T *fr;
5973{
5974 if (fr != NULL)
5975 {
5976 clear_snapshot_rec(fr->fr_next);
5977 clear_snapshot_rec(fr->fr_child);
5978 vim_free(fr);
5979 }
5980}
5981
5982/*
5983 * Restore a previously created snapshot, if there is any.
5984 * This is only done if the screen size didn't change and the window layout is
5985 * still the same.
5986 */
5987 static void
5988restore_snapshot(close_curwin)
5989 int close_curwin; /* closing current window */
5990{
5991 win_T *wp;
5992
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005993 if (curtab->tp_snapshot != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994# ifdef FEAT_VERTSPLIT
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005995 && curtab->tp_snapshot->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996# endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005997 && curtab->tp_snapshot->fr_height == topframe->fr_height
5998 && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006000 wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 win_comp_pos();
6002 if (wp != NULL && close_curwin)
6003 win_goto(wp);
6004 redraw_all_later(CLEAR);
6005 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006006 clear_snapshot(curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007}
6008
6009/*
6010 * Check if frames "sn" and "fr" have the same layout, same following frames
6011 * and same children.
6012 */
6013 static int
6014check_snapshot_rec(sn, fr)
6015 frame_T *sn;
6016 frame_T *fr;
6017{
6018 if (sn->fr_layout != fr->fr_layout
6019 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6020 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6021 || (sn->fr_next != NULL
6022 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6023 || (sn->fr_child != NULL
6024 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6025 return FAIL;
6026 return OK;
6027}
6028
6029/*
6030 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6031 * following frames and children.
6032 * Returns a pointer to the old current window, or NULL.
6033 */
6034 static win_T *
6035restore_snapshot_rec(sn, fr)
6036 frame_T *sn;
6037 frame_T *fr;
6038{
6039 win_T *wp = NULL;
6040 win_T *wp2;
6041
6042 fr->fr_height = sn->fr_height;
6043# ifdef FEAT_VERTSPLIT
6044 fr->fr_width = sn->fr_width;
6045# endif
6046 if (fr->fr_layout == FR_LEAF)
6047 {
6048 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6049# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006050 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051# endif
6052 wp = sn->fr_win;
6053 }
6054 if (sn->fr_next != NULL)
6055 {
6056 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6057 if (wp2 != NULL)
6058 wp = wp2;
6059 }
6060 if (sn->fr_child != NULL)
6061 {
6062 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6063 if (wp2 != NULL)
6064 wp = wp2;
6065 }
6066 return wp;
6067}
6068
6069#endif
6070
6071#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6072/*
6073 * Return TRUE if there is any vertically split window.
6074 */
6075 int
6076win_hasvertsplit()
6077{
6078 frame_T *fr;
6079
6080 if (topframe->fr_layout == FR_ROW)
6081 return TRUE;
6082
6083 if (topframe->fr_layout == FR_COL)
6084 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6085 if (fr->fr_layout == FR_ROW)
6086 return TRUE;
6087
6088 return FALSE;
6089}
6090#endif