1 /* vi:set ts=8 sts=4 sw=4 noet:
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 static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
13 static void win_init(win_T *newp, win_T *oldp, int flags);
14 static void win_init_some(win_T *newp, win_T *oldp);
15 static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
16 static void frame_setheight(frame_T *curfrp, int height);
17 static void frame_setwidth(frame_T *curfrp, int width);
18 static void win_exchange(long);
19 static void win_rotate(int, int);
20 static void win_totop(int size, int flags);
21 static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
22 static void trigger_winclosed(win_T *win);
23 static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
24 static frame_T *win_altframe(win_T *win, tabpage_T *tp);
25 static tabpage_T *alt_tabpage(void);
26 static win_T *frame2win(frame_T *frp);
27 static int frame_has_win(frame_T *frp, win_T *wp);
28 static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
29 static int frame_fixed_height(frame_T *frp);
30 static int frame_fixed_width(frame_T *frp);
31 static void frame_add_statusline(frame_T *frp);
32 static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
33 static void frame_add_vsep(frame_T *frp);
34 static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
35 static void frame_fix_width(win_T *wp);
36 static int win_alloc_firstwin(win_T *oldwin);
37 static void new_frame(win_T *wp);
38 static tabpage_T *alloc_tabpage(void);
39 static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
40 static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
41 static void frame_fix_height(win_T *wp);
42 static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
43 static int may_open_tabpage(void);
44 static int win_enter_ext(win_T *wp, int flags);
45 static void win_free(win_T *wp, tabpage_T *tp);
46 static int win_unlisted(win_T *wp);
47 static void win_append(win_T *after, win_T *wp);
48 static void frame_append(frame_T *after, frame_T *frp);
49 static void frame_insert(frame_T *before, frame_T *frp);
50 static void frame_remove(frame_T *frp);
51 static void win_goto_ver(int up, long count);
52 static void win_goto_hor(int left, long count);
53 static void frame_add_height(frame_T *frp, int n);
54 static void last_status_rec(frame_T *fr, int statusline);
55
56 static void make_snapshot_rec(frame_T *fr, frame_T **frp);
57 static void clear_snapshot(tabpage_T *tp, int idx);
58 static void clear_snapshot_rec(frame_T *fr);
59 static int check_snapshot_rec(frame_T *sn, frame_T *fr);
60 static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
61
62 static int frame_check_height(frame_T *topfrp, int height);
63 static int frame_check_width(frame_T *topfrp, int width);
64
65 static win_T *win_alloc(win_T *after, int hidden);
66
67 #define NOWIN (win_T *)-1 // non-existing window
68
69 #define ROWS_AVAIL (Rows - p_ch - tabline_height())
70
71 // flags for win_enter_ext()
72 #define WEE_UNDO_SYNC 0x01
73 #define WEE_CURWIN_INVALID 0x02
74 #define WEE_TRIGGER_NEW_AUTOCMDS 0x04
75 #define WEE_TRIGGER_ENTER_AUTOCMDS 0x08
76 #define WEE_TRIGGER_LEAVE_AUTOCMDS 0x10
77 #define WEE_ALLOW_PARSE_MESSAGES 0x20
78
79 static char *m_onlyone = N_("Already only one window");
80
81 // When non-zero splitting a window is forbidden. Used to avoid that nasty
82 // autocommands mess up the window structure.
83 static int split_disallowed = 0;
84
85 // #define WIN_DEBUG
86 #ifdef WIN_DEBUG
87 /*
88 * Call this method to log the current window layout.
89 */
90 static void
log_frame_layout(frame_T * frame)91 log_frame_layout(frame_T *frame)
92 {
93 ch_log(NULL, "layout %s, wi: %d, he: %d, wwi: %d, whe: %d, id: %d",
94 frame->fr_layout == FR_LEAF ? "LEAF"
95 : frame->fr_layout == FR_ROW ? "ROW" : "COL",
96 frame->fr_width,
97 frame->fr_height,
98 frame->fr_win == NULL ? -1 : frame->fr_win->w_width,
99 frame->fr_win == NULL ? -1 : frame->fr_win->w_height,
100 frame->fr_win == NULL ? -1 : frame->fr_win->w_id);
101 if (frame->fr_child != NULL)
102 {
103 ch_log(NULL, "children");
104 log_frame_layout(frame->fr_child);
105 if (frame->fr_next != NULL)
106 ch_log(NULL, "END of children");
107 }
108 if (frame->fr_next != NULL)
109 log_frame_layout(frame->fr_next);
110 }
111 #endif
112
113 /*
114 * All CTRL-W window commands are handled here, called from normal_cmd().
115 */
116 void
do_window(int nchar,long Prenum,int xchar)117 do_window(
118 int nchar,
119 long Prenum,
120 int xchar) // extra char from ":wincmd gx" or NUL
121 {
122 long Prenum1;
123 win_T *wp;
124 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
125 char_u *ptr;
126 linenr_T lnum = -1;
127 #endif
128 #ifdef FEAT_FIND_ID
129 int type = FIND_DEFINE;
130 int len;
131 #endif
132 char_u cbuf[40];
133
134 if (ERROR_IF_ANY_POPUP_WINDOW)
135 return;
136
137 #ifdef FEAT_CMDWIN
138 # define CHECK_CMDWIN \
139 do { \
140 if (cmdwin_type != 0) \
141 { \
142 emsg(_(e_invalid_in_cmdline_window)); \
143 return; \
144 } \
145 } while (0)
146 #else
147 # define CHECK_CMDWIN do { /**/ } while (0)
148 #endif
149
150 Prenum1 = Prenum == 0 ? 1 : Prenum;
151
152 switch (nchar)
153 {
154 // split current window in two parts, horizontally
155 case 'S':
156 case Ctrl_S:
157 case 's':
158 CHECK_CMDWIN;
159 reset_VIsual_and_resel(); // stop Visual mode
160 #ifdef FEAT_QUICKFIX
161 // When splitting the quickfix window open a new buffer in it,
162 // don't replicate the quickfix buffer.
163 if (bt_quickfix(curbuf))
164 goto newwindow;
165 #endif
166 #ifdef FEAT_GUI
167 need_mouse_correct = TRUE;
168 #endif
169 (void)win_split((int)Prenum, 0);
170 break;
171
172 // split current window in two parts, vertically
173 case Ctrl_V:
174 case 'v':
175 CHECK_CMDWIN;
176 reset_VIsual_and_resel(); // stop Visual mode
177 #ifdef FEAT_QUICKFIX
178 // When splitting the quickfix window open a new buffer in it,
179 // don't replicate the quickfix buffer.
180 if (bt_quickfix(curbuf))
181 goto newwindow;
182 #endif
183 #ifdef FEAT_GUI
184 need_mouse_correct = TRUE;
185 #endif
186 (void)win_split((int)Prenum, WSP_VERT);
187 break;
188
189 // split current window and edit alternate file
190 case Ctrl_HAT:
191 case '^':
192 CHECK_CMDWIN;
193 reset_VIsual_and_resel(); // stop Visual mode
194
195 if (buflist_findnr(Prenum == 0
196 ? curwin->w_alt_fnum : Prenum) == NULL)
197 {
198 if (Prenum == 0)
199 emsg(_(e_no_alternate_file));
200 else
201 semsg(_("E92: Buffer %ld not found"), Prenum);
202 break;
203 }
204
205 if (!curbuf_locked() && win_split(0, 0) == OK)
206 (void)buflist_getfile(
207 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
208 (linenr_T)0, GETF_ALT, FALSE);
209 break;
210
211 // open new window
212 case Ctrl_N:
213 case 'n':
214 CHECK_CMDWIN;
215 reset_VIsual_and_resel(); // stop Visual mode
216 #ifdef FEAT_QUICKFIX
217 newwindow:
218 #endif
219 if (Prenum)
220 // window height
221 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
222 else
223 cbuf[0] = NUL;
224 #if defined(FEAT_QUICKFIX)
225 if (nchar == 'v' || nchar == Ctrl_V)
226 STRCAT(cbuf, "v");
227 #endif
228 STRCAT(cbuf, "new");
229 do_cmdline_cmd(cbuf);
230 break;
231
232 // quit current window
233 case Ctrl_Q:
234 case 'q':
235 reset_VIsual_and_resel(); // stop Visual mode
236 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
237 do_cmdline_cmd(cbuf);
238 break;
239
240 // close current window
241 case Ctrl_C:
242 case 'c':
243 reset_VIsual_and_resel(); // stop Visual mode
244 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
245 do_cmdline_cmd(cbuf);
246 break;
247
248 #if defined(FEAT_QUICKFIX)
249 // close preview window
250 case Ctrl_Z:
251 case 'z':
252 CHECK_CMDWIN;
253 reset_VIsual_and_resel(); // stop Visual mode
254 do_cmdline_cmd((char_u *)"pclose");
255 break;
256
257 // cursor to preview window
258 case 'P':
259 FOR_ALL_WINDOWS(wp)
260 if (wp->w_p_pvw)
261 break;
262 if (wp == NULL)
263 emsg(_("E441: There is no preview window"));
264 else
265 win_goto(wp);
266 break;
267 #endif
268
269 // close all but current window
270 case Ctrl_O:
271 case 'o':
272 CHECK_CMDWIN;
273 reset_VIsual_and_resel(); // stop Visual mode
274 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
275 do_cmdline_cmd(cbuf);
276 break;
277
278 // cursor to next window with wrap around
279 case Ctrl_W:
280 case 'w':
281 // cursor to previous window with wrap around
282 case 'W':
283 CHECK_CMDWIN;
284 if (ONE_WINDOW && Prenum != 1) // just one window
285 beep_flush();
286 else
287 {
288 if (Prenum) // go to specified window
289 {
290 for (wp = firstwin; --Prenum > 0; )
291 {
292 if (wp->w_next == NULL)
293 break;
294 else
295 wp = wp->w_next;
296 }
297 }
298 else
299 {
300 if (nchar == 'W') // go to previous window
301 {
302 wp = curwin->w_prev;
303 if (wp == NULL)
304 wp = lastwin; // wrap around
305 }
306 else // go to next window
307 {
308 wp = curwin->w_next;
309 if (wp == NULL)
310 wp = firstwin; // wrap around
311 }
312 }
313 win_goto(wp);
314 }
315 break;
316
317 // cursor to window below
318 case 'j':
319 case K_DOWN:
320 case Ctrl_J:
321 CHECK_CMDWIN;
322 win_goto_ver(FALSE, Prenum1);
323 break;
324
325 // cursor to window above
326 case 'k':
327 case K_UP:
328 case Ctrl_K:
329 CHECK_CMDWIN;
330 win_goto_ver(TRUE, Prenum1);
331 break;
332
333 // cursor to left window
334 case 'h':
335 case K_LEFT:
336 case Ctrl_H:
337 case K_BS:
338 CHECK_CMDWIN;
339 win_goto_hor(TRUE, Prenum1);
340 break;
341
342 // cursor to right window
343 case 'l':
344 case K_RIGHT:
345 case Ctrl_L:
346 CHECK_CMDWIN;
347 win_goto_hor(FALSE, Prenum1);
348 break;
349
350 // move window to new tab page
351 case 'T':
352 CHECK_CMDWIN;
353 if (one_window())
354 msg(_(m_onlyone));
355 else
356 {
357 tabpage_T *oldtab = curtab;
358 tabpage_T *newtab;
359
360 // First create a new tab with the window, then go back to
361 // the old tab and close the window there.
362 wp = curwin;
363 if (win_new_tabpage((int)Prenum) == OK
364 && valid_tabpage(oldtab))
365 {
366 newtab = curtab;
367 goto_tabpage_tp(oldtab, TRUE, TRUE);
368 if (curwin == wp)
369 win_close(curwin, FALSE);
370 if (valid_tabpage(newtab))
371 goto_tabpage_tp(newtab, TRUE, TRUE);
372 }
373 }
374 break;
375
376 // cursor to top-left window
377 case 't':
378 case Ctrl_T:
379 win_goto(firstwin);
380 break;
381
382 // cursor to bottom-right window
383 case 'b':
384 case Ctrl_B:
385 win_goto(lastwin);
386 break;
387
388 // cursor to last accessed (previous) window
389 case 'p':
390 case Ctrl_P:
391 if (!win_valid(prevwin))
392 beep_flush();
393 else
394 win_goto(prevwin);
395 break;
396
397 // exchange current and next window
398 case 'x':
399 case Ctrl_X:
400 CHECK_CMDWIN;
401 win_exchange(Prenum);
402 break;
403
404 // rotate windows downwards
405 case Ctrl_R:
406 case 'r':
407 CHECK_CMDWIN;
408 reset_VIsual_and_resel(); // stop Visual mode
409 win_rotate(FALSE, (int)Prenum1); // downwards
410 break;
411
412 // rotate windows upwards
413 case 'R':
414 CHECK_CMDWIN;
415 reset_VIsual_and_resel(); // stop Visual mode
416 win_rotate(TRUE, (int)Prenum1); // upwards
417 break;
418
419 // move window to the very top/bottom/left/right
420 case 'K':
421 case 'J':
422 case 'H':
423 case 'L':
424 CHECK_CMDWIN;
425 win_totop((int)Prenum,
426 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
427 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
428 break;
429
430 // make all windows the same height
431 case '=':
432 #ifdef FEAT_GUI
433 need_mouse_correct = TRUE;
434 #endif
435 win_equal(NULL, FALSE, 'b');
436 break;
437
438 // increase current window height
439 case '+':
440 #ifdef FEAT_GUI
441 need_mouse_correct = TRUE;
442 #endif
443 win_setheight(curwin->w_height + (int)Prenum1);
444 break;
445
446 // decrease current window height
447 case '-':
448 #ifdef FEAT_GUI
449 need_mouse_correct = TRUE;
450 #endif
451 win_setheight(curwin->w_height - (int)Prenum1);
452 break;
453
454 // set current window height
455 case Ctrl__:
456 case '_':
457 #ifdef FEAT_GUI
458 need_mouse_correct = TRUE;
459 #endif
460 win_setheight(Prenum ? (int)Prenum : 9999);
461 break;
462
463 // increase current window width
464 case '>':
465 #ifdef FEAT_GUI
466 need_mouse_correct = TRUE;
467 #endif
468 win_setwidth(curwin->w_width + (int)Prenum1);
469 break;
470
471 // decrease current window width
472 case '<':
473 #ifdef FEAT_GUI
474 need_mouse_correct = TRUE;
475 #endif
476 win_setwidth(curwin->w_width - (int)Prenum1);
477 break;
478
479 // set current window width
480 case '|':
481 #ifdef FEAT_GUI
482 need_mouse_correct = TRUE;
483 #endif
484 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
485 break;
486
487 // jump to tag and split window if tag exists (in preview window)
488 #if defined(FEAT_QUICKFIX)
489 case '}':
490 CHECK_CMDWIN;
491 if (Prenum)
492 g_do_tagpreview = Prenum;
493 else
494 g_do_tagpreview = p_pvh;
495 #endif
496 // FALLTHROUGH
497 case ']':
498 case Ctrl_RSB:
499 CHECK_CMDWIN;
500 // keep Visual mode, can select words to use as a tag
501 if (Prenum)
502 postponed_split = Prenum;
503 else
504 postponed_split = -1;
505 #ifdef FEAT_QUICKFIX
506 if (nchar != '}')
507 g_do_tagpreview = 0;
508 #endif
509
510 // Execute the command right here, required when "wincmd ]"
511 // was used in a function.
512 do_nv_ident(Ctrl_RSB, NUL);
513 break;
514
515 #ifdef FEAT_SEARCHPATH
516 // edit file name under cursor in a new window
517 case 'f':
518 case 'F':
519 case Ctrl_F:
520 wingotofile:
521 CHECK_CMDWIN;
522
523 ptr = grab_file_name(Prenum1, &lnum);
524 if (ptr != NULL)
525 {
526 tabpage_T *oldtab = curtab;
527 win_T *oldwin = curwin;
528 # ifdef FEAT_GUI
529 need_mouse_correct = TRUE;
530 # endif
531 setpcmark();
532 if (win_split(0, 0) == OK)
533 {
534 RESET_BINDING(curwin);
535 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
536 ECMD_HIDE, NULL) == FAIL)
537 {
538 // Failed to open the file, close the window
539 // opened for it.
540 win_close(curwin, FALSE);
541 goto_tabpage_win(oldtab, oldwin);
542 }
543 else if (nchar == 'F' && lnum >= 0)
544 {
545 curwin->w_cursor.lnum = lnum;
546 check_cursor_lnum();
547 beginline(BL_SOL | BL_FIX);
548 }
549 }
550 vim_free(ptr);
551 }
552 break;
553 #endif
554
555 #ifdef FEAT_FIND_ID
556 // Go to the first occurrence of the identifier under cursor along path in a
557 // new window -- webb
558 case 'i': // Go to any match
559 case Ctrl_I:
560 type = FIND_ANY;
561 // FALLTHROUGH
562 case 'd': // Go to definition, using 'define'
563 case Ctrl_D:
564 CHECK_CMDWIN;
565 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
566 break;
567 find_pattern_in_path(ptr, 0, len, TRUE,
568 Prenum == 0 ? TRUE : FALSE, type,
569 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
570 curwin->w_set_curswant = TRUE;
571 break;
572 #endif
573
574 // Quickfix window only: view the result under the cursor in a new split.
575 #if defined(FEAT_QUICKFIX)
576 case K_KENTER:
577 case CAR:
578 if (bt_quickfix(curbuf))
579 qf_view_result(TRUE);
580 break;
581 #endif
582
583 // CTRL-W g extended commands
584 case 'g':
585 case Ctrl_G:
586 CHECK_CMDWIN;
587 #ifdef USE_ON_FLY_SCROLL
588 dont_scroll = TRUE; // disallow scrolling here
589 #endif
590 ++no_mapping;
591 ++allow_keys; // no mapping for xchar, but allow key codes
592 if (xchar == NUL)
593 xchar = plain_vgetc();
594 LANGMAP_ADJUST(xchar, TRUE);
595 --no_mapping;
596 --allow_keys;
597 #ifdef FEAT_CMDL_INFO
598 (void)add_to_showcmd(xchar);
599 #endif
600 switch (xchar)
601 {
602 #if defined(FEAT_QUICKFIX)
603 case '}':
604 xchar = Ctrl_RSB;
605 if (Prenum)
606 g_do_tagpreview = Prenum;
607 else
608 g_do_tagpreview = p_pvh;
609 #endif
610 // FALLTHROUGH
611 case ']':
612 case Ctrl_RSB:
613 // keep Visual mode, can select words to use as a tag
614 if (Prenum)
615 postponed_split = Prenum;
616 else
617 postponed_split = -1;
618
619 // Execute the command right here, required when
620 // "wincmd g}" was used in a function.
621 do_nv_ident('g', xchar);
622 break;
623
624 #ifdef FEAT_SEARCHPATH
625 case 'f': // CTRL-W gf: "gf" in a new tab page
626 case 'F': // CTRL-W gF: "gF" in a new tab page
627 cmdmod.cmod_tab = tabpage_index(curtab) + 1;
628 nchar = xchar;
629 goto wingotofile;
630 #endif
631 case 't': // CTRL-W gt: go to next tab page
632 goto_tabpage((int)Prenum);
633 break;
634
635 case 'T': // CTRL-W gT: go to previous tab page
636 goto_tabpage(-(int)Prenum1);
637 break;
638
639 case TAB: // CTRL-W g<Tab>: go to last used tab page
640 if (goto_tabpage_lastused() == FAIL)
641 beep_flush();
642 break;
643
644 default:
645 beep_flush();
646 break;
647 }
648 break;
649
650 default: beep_flush();
651 break;
652 }
653 }
654
655 /*
656 * Figure out the address type for ":wincmd".
657 */
658 void
get_wincmd_addr_type(char_u * arg,exarg_T * eap)659 get_wincmd_addr_type(char_u *arg, exarg_T *eap)
660 {
661 switch (*arg)
662 {
663 case 'S':
664 case Ctrl_S:
665 case 's':
666 case Ctrl_N:
667 case 'n':
668 case 'j':
669 case Ctrl_J:
670 case 'k':
671 case Ctrl_K:
672 case 'T':
673 case Ctrl_R:
674 case 'r':
675 case 'R':
676 case 'K':
677 case 'J':
678 case '+':
679 case '-':
680 case Ctrl__:
681 case '_':
682 case '|':
683 case ']':
684 case Ctrl_RSB:
685 case 'g':
686 case Ctrl_G:
687 case Ctrl_V:
688 case 'v':
689 case 'h':
690 case Ctrl_H:
691 case 'l':
692 case Ctrl_L:
693 case 'H':
694 case 'L':
695 case '>':
696 case '<':
697 #if defined(FEAT_QUICKFIX)
698 case '}':
699 #endif
700 #ifdef FEAT_SEARCHPATH
701 case 'f':
702 case 'F':
703 case Ctrl_F:
704 #endif
705 #ifdef FEAT_FIND_ID
706 case 'i':
707 case Ctrl_I:
708 case 'd':
709 case Ctrl_D:
710 #endif
711 // window size or any count
712 eap->addr_type = ADDR_OTHER;
713 break;
714
715 case Ctrl_HAT:
716 case '^':
717 // buffer number
718 eap->addr_type = ADDR_BUFFERS;
719 break;
720
721 case Ctrl_Q:
722 case 'q':
723 case Ctrl_C:
724 case 'c':
725 case Ctrl_O:
726 case 'o':
727 case Ctrl_W:
728 case 'w':
729 case 'W':
730 case 'x':
731 case Ctrl_X:
732 // window number
733 eap->addr_type = ADDR_WINDOWS;
734 break;
735
736 #if defined(FEAT_QUICKFIX)
737 case Ctrl_Z:
738 case 'z':
739 case 'P':
740 #endif
741 case 't':
742 case Ctrl_T:
743 case 'b':
744 case Ctrl_B:
745 case 'p':
746 case Ctrl_P:
747 case '=':
748 case CAR:
749 // no count
750 eap->addr_type = ADDR_NONE;
751 break;
752 }
753 }
754
755 static void
cmd_with_count(char * cmd,char_u * bufp,size_t bufsize,long Prenum)756 cmd_with_count(
757 char *cmd,
758 char_u *bufp,
759 size_t bufsize,
760 long Prenum)
761 {
762 if (Prenum > 0)
763 vim_snprintf((char *)bufp, bufsize, "%s %ld", cmd, Prenum);
764 else
765 STRCPY(bufp, cmd);
766 }
767
768 /*
769 * If "split_disallowed" is set given an error and return FAIL.
770 * Otherwise return OK.
771 */
772 static int
check_split_disallowed()773 check_split_disallowed()
774 {
775 if (split_disallowed > 0)
776 {
777 emsg(_("E242: Can't split a window while closing another"));
778 return FAIL;
779 }
780 if (curwin->w_buffer->b_locked_split)
781 {
782 emsg(_(e_cannot_split_window_when_closing_buffer));
783 return FAIL;
784 }
785 return OK;
786 }
787
788 /*
789 * split the current window, implements CTRL-W s and :split
790 *
791 * "size" is the height or width for the new window, 0 to use half of current
792 * height or width.
793 *
794 * "flags":
795 * WSP_ROOM: require enough room for new window
796 * WSP_VERT: vertical split.
797 * WSP_TOP: open window at the top-left of the shell (help window).
798 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
799 * WSP_HELP: creating the help window, keep layout snapshot
800 *
801 * return FAIL for failure, OK otherwise
802 */
803 int
win_split(int size,int flags)804 win_split(int size, int flags)
805 {
806 if (ERROR_IF_ANY_POPUP_WINDOW)
807 return FAIL;
808
809 if (check_split_disallowed() == FAIL)
810 return FAIL;
811
812 // When the ":tab" modifier was used open a new tab page instead.
813 if (may_open_tabpage() == OK)
814 return OK;
815
816 // Add flags from ":vertical", ":topleft" and ":botright".
817 flags |= cmdmod.cmod_split;
818 if ((flags & WSP_TOP) && (flags & WSP_BOT))
819 {
820 emsg(_("E442: Can't split topleft and botright at the same time"));
821 return FAIL;
822 }
823
824 // When creating the help window make a snapshot of the window layout.
825 // Otherwise clear the snapshot, it's now invalid.
826 if (flags & WSP_HELP)
827 make_snapshot(SNAP_HELP_IDX);
828 else
829 clear_snapshot(curtab, SNAP_HELP_IDX);
830
831 return win_split_ins(size, flags, NULL, 0);
832 }
833
834 /*
835 * When "new_wp" is NULL: split the current window in two.
836 * When "new_wp" is not NULL: insert this window at the far
837 * top/left/right/bottom.
838 * return FAIL for failure, OK otherwise
839 */
840 int
win_split_ins(int size,int flags,win_T * new_wp,int dir)841 win_split_ins(
842 int size,
843 int flags,
844 win_T *new_wp,
845 int dir)
846 {
847 win_T *wp = new_wp;
848 win_T *oldwin;
849 int new_size = size;
850 int i;
851 int need_status = 0;
852 int do_equal = FALSE;
853 int needed;
854 int available;
855 int oldwin_height = 0;
856 int layout;
857 frame_T *frp, *curfrp, *frp2, *prevfrp;
858 int before;
859 int minheight;
860 int wmh1;
861 int did_set_fraction = FALSE;
862
863 if (flags & WSP_TOP)
864 oldwin = firstwin;
865 else if (flags & WSP_BOT)
866 oldwin = lastwin;
867 else
868 oldwin = curwin;
869
870 // add a status line when p_ls == 1 and splitting the first window
871 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
872 {
873 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
874 {
875 emsg(_(e_not_enough_room));
876 return FAIL;
877 }
878 need_status = STATUS_HEIGHT;
879 }
880
881 #ifdef FEAT_GUI
882 // May be needed for the scrollbars that are going to change.
883 if (gui.in_use)
884 out_flush();
885 #endif
886
887 if (flags & WSP_VERT)
888 {
889 int wmw1;
890 int minwidth;
891
892 layout = FR_ROW;
893
894 /*
895 * Check if we are able to split the current window and compute its
896 * width.
897 */
898 // Current window requires at least 1 space.
899 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
900 needed = wmw1 + 1;
901 if (flags & WSP_ROOM)
902 needed += p_wiw - wmw1;
903 if (flags & (WSP_BOT | WSP_TOP))
904 {
905 minwidth = frame_minwidth(topframe, NOWIN);
906 available = topframe->fr_width;
907 needed += minwidth;
908 }
909 else if (p_ea)
910 {
911 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
912 prevfrp = oldwin->w_frame;
913 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
914 frp = frp->fr_parent)
915 {
916 if (frp->fr_layout == FR_ROW)
917 FOR_ALL_FRAMES(frp2, frp->fr_child)
918 if (frp2 != prevfrp)
919 minwidth += frame_minwidth(frp2, NOWIN);
920 prevfrp = frp;
921 }
922 available = topframe->fr_width;
923 needed += minwidth;
924 }
925 else
926 {
927 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
928 available = oldwin->w_frame->fr_width;
929 needed += minwidth;
930 }
931 if (available < needed && new_wp == NULL)
932 {
933 emsg(_(e_not_enough_room));
934 return FAIL;
935 }
936 if (new_size == 0)
937 new_size = oldwin->w_width / 2;
938 if (new_size > available - minwidth - 1)
939 new_size = available - minwidth - 1;
940 if (new_size < wmw1)
941 new_size = wmw1;
942
943 // if it doesn't fit in the current window, need win_equal()
944 if (oldwin->w_width - new_size - 1 < p_wmw)
945 do_equal = TRUE;
946
947 // We don't like to take lines for the new window from a
948 // 'winfixwidth' window. Take them from a window to the left or right
949 // instead, if possible. Add one for the separator.
950 if (oldwin->w_p_wfw)
951 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
952
953 // Only make all windows the same width if one of them (except oldwin)
954 // is wider than one of the split windows.
955 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
956 && oldwin->w_frame->fr_parent != NULL)
957 {
958 frp = oldwin->w_frame->fr_parent->fr_child;
959 while (frp != NULL)
960 {
961 if (frp->fr_win != oldwin && frp->fr_win != NULL
962 && (frp->fr_win->w_width > new_size
963 || frp->fr_win->w_width > oldwin->w_width
964 - new_size - 1))
965 {
966 do_equal = TRUE;
967 break;
968 }
969 frp = frp->fr_next;
970 }
971 }
972 }
973 else
974 {
975 layout = FR_COL;
976
977 /*
978 * Check if we are able to split the current window and compute its
979 * height.
980 */
981 // Current window requires at least 1 space.
982 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
983 needed = wmh1 + STATUS_HEIGHT;
984 if (flags & WSP_ROOM)
985 needed += p_wh - wmh1;
986 if (flags & (WSP_BOT | WSP_TOP))
987 {
988 minheight = frame_minheight(topframe, NOWIN) + need_status;
989 available = topframe->fr_height;
990 needed += minheight;
991 }
992 else if (p_ea)
993 {
994 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
995 prevfrp = oldwin->w_frame;
996 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
997 frp = frp->fr_parent)
998 {
999 if (frp->fr_layout == FR_COL)
1000 FOR_ALL_FRAMES(frp2, frp->fr_child)
1001 if (frp2 != prevfrp)
1002 minheight += frame_minheight(frp2, NOWIN);
1003 prevfrp = frp;
1004 }
1005 available = topframe->fr_height;
1006 needed += minheight;
1007 }
1008 else
1009 {
1010 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
1011 available = oldwin->w_frame->fr_height;
1012 needed += minheight;
1013 }
1014 if (available < needed && new_wp == NULL)
1015 {
1016 emsg(_(e_not_enough_room));
1017 return FAIL;
1018 }
1019 oldwin_height = oldwin->w_height;
1020 if (need_status)
1021 {
1022 oldwin->w_status_height = STATUS_HEIGHT;
1023 oldwin_height -= STATUS_HEIGHT;
1024 }
1025 if (new_size == 0)
1026 new_size = oldwin_height / 2;
1027 if (new_size > available - minheight - STATUS_HEIGHT)
1028 new_size = available - minheight - STATUS_HEIGHT;
1029 if (new_size < wmh1)
1030 new_size = wmh1;
1031
1032 // if it doesn't fit in the current window, need win_equal()
1033 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
1034 do_equal = TRUE;
1035
1036 // We don't like to take lines for the new window from a
1037 // 'winfixheight' window. Take them from a window above or below
1038 // instead, if possible.
1039 if (oldwin->w_p_wfh)
1040 {
1041 // Set w_fraction now so that the cursor keeps the same relative
1042 // vertical position using the old height.
1043 set_fraction(oldwin);
1044 did_set_fraction = TRUE;
1045
1046 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
1047 oldwin);
1048 oldwin_height = oldwin->w_height;
1049 if (need_status)
1050 oldwin_height -= STATUS_HEIGHT;
1051 }
1052
1053 // Only make all windows the same height if one of them (except oldwin)
1054 // is higher than one of the split windows.
1055 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
1056 && oldwin->w_frame->fr_parent != NULL)
1057 {
1058 frp = oldwin->w_frame->fr_parent->fr_child;
1059 while (frp != NULL)
1060 {
1061 if (frp->fr_win != oldwin && frp->fr_win != NULL
1062 && (frp->fr_win->w_height > new_size
1063 || frp->fr_win->w_height > oldwin_height - new_size
1064 - STATUS_HEIGHT))
1065 {
1066 do_equal = TRUE;
1067 break;
1068 }
1069 frp = frp->fr_next;
1070 }
1071 }
1072 }
1073
1074 /*
1075 * allocate new window structure and link it in the window list
1076 */
1077 if ((flags & WSP_TOP) == 0
1078 && ((flags & WSP_BOT)
1079 || (flags & WSP_BELOW)
1080 || (!(flags & WSP_ABOVE)
1081 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
1082 {
1083 // new window below/right of current one
1084 if (new_wp == NULL)
1085 wp = win_alloc(oldwin, FALSE);
1086 else
1087 win_append(oldwin, wp);
1088 }
1089 else
1090 {
1091 if (new_wp == NULL)
1092 wp = win_alloc(oldwin->w_prev, FALSE);
1093 else
1094 win_append(oldwin->w_prev, wp);
1095 }
1096
1097 if (new_wp == NULL)
1098 {
1099 if (wp == NULL)
1100 return FAIL;
1101
1102 new_frame(wp);
1103 if (wp->w_frame == NULL)
1104 {
1105 win_free(wp, NULL);
1106 return FAIL;
1107 }
1108
1109 // make the contents of the new window the same as the current one
1110 win_init(wp, curwin, flags);
1111 }
1112
1113 /*
1114 * Reorganise the tree of frames to insert the new window.
1115 */
1116 if (flags & (WSP_TOP | WSP_BOT))
1117 {
1118 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1119 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1120 {
1121 curfrp = topframe->fr_child;
1122 if (flags & WSP_BOT)
1123 while (curfrp->fr_next != NULL)
1124 curfrp = curfrp->fr_next;
1125 }
1126 else
1127 curfrp = topframe;
1128 before = (flags & WSP_TOP);
1129 }
1130 else
1131 {
1132 curfrp = oldwin->w_frame;
1133 if (flags & WSP_BELOW)
1134 before = FALSE;
1135 else if (flags & WSP_ABOVE)
1136 before = TRUE;
1137 else if (flags & WSP_VERT)
1138 before = !p_spr;
1139 else
1140 before = !p_sb;
1141 }
1142 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1143 {
1144 // Need to create a new frame in the tree to make a branch.
1145 frp = ALLOC_CLEAR_ONE(frame_T);
1146 *frp = *curfrp;
1147 curfrp->fr_layout = layout;
1148 frp->fr_parent = curfrp;
1149 frp->fr_next = NULL;
1150 frp->fr_prev = NULL;
1151 curfrp->fr_child = frp;
1152 curfrp->fr_win = NULL;
1153 curfrp = frp;
1154 if (frp->fr_win != NULL)
1155 oldwin->w_frame = frp;
1156 else
1157 FOR_ALL_FRAMES(frp, frp->fr_child)
1158 frp->fr_parent = curfrp;
1159 }
1160
1161 if (new_wp == NULL)
1162 frp = wp->w_frame;
1163 else
1164 frp = new_wp->w_frame;
1165 frp->fr_parent = curfrp->fr_parent;
1166
1167 // Insert the new frame at the right place in the frame list.
1168 if (before)
1169 frame_insert(curfrp, frp);
1170 else
1171 frame_append(curfrp, frp);
1172
1173 // Set w_fraction now so that the cursor keeps the same relative
1174 // vertical position.
1175 if (!did_set_fraction)
1176 set_fraction(oldwin);
1177 wp->w_fraction = oldwin->w_fraction;
1178
1179 if (flags & WSP_VERT)
1180 {
1181 wp->w_p_scr = curwin->w_p_scr;
1182
1183 if (need_status)
1184 {
1185 win_new_height(oldwin, oldwin->w_height - 1);
1186 oldwin->w_status_height = need_status;
1187 }
1188 if (flags & (WSP_TOP | WSP_BOT))
1189 {
1190 // set height and row of new window to full height
1191 wp->w_winrow = tabline_height();
1192 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
1193 - WINBAR_HEIGHT(wp));
1194 wp->w_status_height = (p_ls > 0);
1195 }
1196 else
1197 {
1198 // height and row of new window is same as current window
1199 wp->w_winrow = oldwin->w_winrow;
1200 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
1201 wp->w_status_height = oldwin->w_status_height;
1202 }
1203 frp->fr_height = curfrp->fr_height;
1204
1205 // "new_size" of the current window goes to the new window, use
1206 // one column for the vertical separator
1207 win_new_width(wp, new_size);
1208 if (before)
1209 wp->w_vsep_width = 1;
1210 else
1211 {
1212 wp->w_vsep_width = oldwin->w_vsep_width;
1213 oldwin->w_vsep_width = 1;
1214 }
1215 if (flags & (WSP_TOP | WSP_BOT))
1216 {
1217 if (flags & WSP_BOT)
1218 frame_add_vsep(curfrp);
1219 // Set width of neighbor frame
1220 frame_new_width(curfrp, curfrp->fr_width
1221 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1222 FALSE);
1223 }
1224 else
1225 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1226 if (before) // new window left of current one
1227 {
1228 wp->w_wincol = oldwin->w_wincol;
1229 oldwin->w_wincol += new_size + 1;
1230 }
1231 else // new window right of current one
1232 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1233 frame_fix_width(oldwin);
1234 frame_fix_width(wp);
1235 }
1236 else
1237 {
1238 // width and column of new window is same as current window
1239 if (flags & (WSP_TOP | WSP_BOT))
1240 {
1241 wp->w_wincol = 0;
1242 win_new_width(wp, Columns);
1243 wp->w_vsep_width = 0;
1244 }
1245 else
1246 {
1247 wp->w_wincol = oldwin->w_wincol;
1248 win_new_width(wp, oldwin->w_width);
1249 wp->w_vsep_width = oldwin->w_vsep_width;
1250 }
1251 frp->fr_width = curfrp->fr_width;
1252
1253 // "new_size" of the current window goes to the new window, use
1254 // one row for the status line
1255 win_new_height(wp, new_size);
1256 if (flags & (WSP_TOP | WSP_BOT))
1257 {
1258 int new_fr_height = curfrp->fr_height - new_size
1259 + WINBAR_HEIGHT(wp) ;
1260
1261 if (!((flags & WSP_BOT) && p_ls == 0))
1262 new_fr_height -= STATUS_HEIGHT;
1263 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1264 }
1265 else
1266 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1267 if (before) // new window above current one
1268 {
1269 wp->w_winrow = oldwin->w_winrow;
1270 wp->w_status_height = STATUS_HEIGHT;
1271 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1272 }
1273 else // new window below current one
1274 {
1275 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1276 + STATUS_HEIGHT;
1277 wp->w_status_height = oldwin->w_status_height;
1278 if (!(flags & WSP_BOT))
1279 oldwin->w_status_height = STATUS_HEIGHT;
1280 }
1281 if (flags & WSP_BOT)
1282 frame_add_statusline(curfrp);
1283 frame_fix_height(wp);
1284 frame_fix_height(oldwin);
1285 }
1286
1287 if (flags & (WSP_TOP | WSP_BOT))
1288 (void)win_comp_pos();
1289
1290 // Both windows need redrawing. Update all status lines, in case they
1291 // show something related to the window count or position.
1292 redraw_win_later(wp, NOT_VALID);
1293 redraw_win_later(oldwin, NOT_VALID);
1294 status_redraw_all();
1295
1296 if (need_status)
1297 {
1298 msg_row = Rows - 1;
1299 msg_col = sc_col;
1300 msg_clr_eos_force(); // Old command/ruler may still be there
1301 comp_col();
1302 msg_row = Rows - 1;
1303 msg_col = 0; // put position back at start of line
1304 }
1305
1306 /*
1307 * equalize the window sizes.
1308 */
1309 if (do_equal || dir != 0)
1310 win_equal(wp, TRUE,
1311 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1312 : dir == 'h' ? 'b' : 'v');
1313
1314 // Don't change the window height/width to 'winheight' / 'winwidth' if a
1315 // size was given.
1316 if (flags & WSP_VERT)
1317 {
1318 i = p_wiw;
1319 if (size != 0)
1320 p_wiw = size;
1321
1322 # ifdef FEAT_GUI
1323 // When 'guioptions' includes 'L' or 'R' may have to add scrollbars.
1324 if (gui.in_use)
1325 gui_init_which_components(NULL);
1326 # endif
1327 }
1328 else
1329 {
1330 i = p_wh;
1331 if (size != 0)
1332 p_wh = size;
1333 }
1334
1335 #ifdef FEAT_JUMPLIST
1336 // Keep same changelist position in new window.
1337 wp->w_changelistidx = oldwin->w_changelistidx;
1338 #endif
1339
1340 /*
1341 * make the new window the current window
1342 */
1343 (void)win_enter_ext(wp, WEE_TRIGGER_NEW_AUTOCMDS
1344 | WEE_TRIGGER_ENTER_AUTOCMDS | WEE_TRIGGER_LEAVE_AUTOCMDS);
1345 if (flags & WSP_VERT)
1346 p_wiw = i;
1347 else
1348 p_wh = i;
1349
1350 return OK;
1351 }
1352
1353
1354 /*
1355 * Initialize window "newp" from window "oldp".
1356 * Used when splitting a window and when creating a new tab page.
1357 * The windows will both edit the same buffer.
1358 * WSP_NEWLOC may be specified in flags to prevent the location list from
1359 * being copied.
1360 */
1361 static void
win_init(win_T * newp,win_T * oldp,int flags UNUSED)1362 win_init(win_T *newp, win_T *oldp, int flags UNUSED)
1363 {
1364 int i;
1365
1366 newp->w_buffer = oldp->w_buffer;
1367 #ifdef FEAT_SYN_HL
1368 newp->w_s = &(oldp->w_buffer->b_s);
1369 #endif
1370 oldp->w_buffer->b_nwindows++;
1371 newp->w_cursor = oldp->w_cursor;
1372 newp->w_valid = 0;
1373 newp->w_curswant = oldp->w_curswant;
1374 newp->w_set_curswant = oldp->w_set_curswant;
1375 newp->w_topline = oldp->w_topline;
1376 #ifdef FEAT_DIFF
1377 newp->w_topfill = oldp->w_topfill;
1378 #endif
1379 newp->w_leftcol = oldp->w_leftcol;
1380 newp->w_pcmark = oldp->w_pcmark;
1381 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1382 newp->w_alt_fnum = oldp->w_alt_fnum;
1383 newp->w_wrow = oldp->w_wrow;
1384 newp->w_fraction = oldp->w_fraction;
1385 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1386 #ifdef FEAT_JUMPLIST
1387 copy_jumplist(oldp, newp);
1388 #endif
1389 #ifdef FEAT_QUICKFIX
1390 if (flags & WSP_NEWLOC)
1391 {
1392 // Don't copy the location list.
1393 newp->w_llist = NULL;
1394 newp->w_llist_ref = NULL;
1395 }
1396 else
1397 copy_loclist_stack(oldp, newp);
1398 #endif
1399 newp->w_localdir = (oldp->w_localdir == NULL)
1400 ? NULL : vim_strsave(oldp->w_localdir);
1401 newp->w_prevdir = (oldp->w_prevdir == NULL)
1402 ? NULL : vim_strsave(oldp->w_prevdir);
1403
1404 // copy tagstack and folds
1405 for (i = 0; i < oldp->w_tagstacklen; i++)
1406 {
1407 taggy_T *tag = &newp->w_tagstack[i];
1408 *tag = oldp->w_tagstack[i];
1409 if (tag->tagname != NULL)
1410 tag->tagname = vim_strsave(tag->tagname);
1411 if (tag->user_data != NULL)
1412 tag->user_data = vim_strsave(tag->user_data);
1413 }
1414 newp->w_tagstackidx = oldp->w_tagstackidx;
1415 newp->w_tagstacklen = oldp->w_tagstacklen;
1416 #ifdef FEAT_FOLDING
1417 copyFoldingState(oldp, newp);
1418 #endif
1419
1420 win_init_some(newp, oldp);
1421
1422 #ifdef FEAT_SYN_HL
1423 check_colorcolumn(newp);
1424 #endif
1425 #ifdef FEAT_TERMINAL
1426 term_update_wincolor(newp);
1427 #endif
1428 }
1429
1430 /*
1431 * Initialize window "newp" from window "old".
1432 * Only the essential things are copied.
1433 */
1434 static void
win_init_some(win_T * newp,win_T * oldp)1435 win_init_some(win_T *newp, win_T *oldp)
1436 {
1437 // Use the same argument list.
1438 newp->w_alist = oldp->w_alist;
1439 ++newp->w_alist->al_refcount;
1440 newp->w_arg_idx = oldp->w_arg_idx;
1441
1442 // copy options from existing window
1443 win_copy_options(oldp, newp);
1444 }
1445
1446 /*
1447 * Return TRUE if "win" is a global popup or a popup in the current tab page.
1448 */
1449 int
win_valid_popup(win_T * win UNUSED)1450 win_valid_popup(win_T *win UNUSED)
1451 {
1452 #ifdef FEAT_PROP_POPUP
1453 win_T *wp;
1454
1455 FOR_ALL_POPUPWINS(wp)
1456 if (wp == win)
1457 return TRUE;
1458 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1459 if (wp == win)
1460 return TRUE;
1461 #endif
1462 return FALSE;
1463 }
1464
1465 /*
1466 * Check if "win" is a pointer to an existing window in the current tab page.
1467 */
1468 int
win_valid(win_T * win)1469 win_valid(win_T *win)
1470 {
1471 win_T *wp;
1472
1473 if (win == NULL)
1474 return FALSE;
1475 FOR_ALL_WINDOWS(wp)
1476 if (wp == win)
1477 return TRUE;
1478 return win_valid_popup(win);
1479 }
1480
1481 /*
1482 * Find window "id" in the current tab page.
1483 * Also find popup windows.
1484 * Return NULL if not found.
1485 */
1486 win_T *
win_find_by_id(int id)1487 win_find_by_id(int id)
1488 {
1489 win_T *wp;
1490
1491 FOR_ALL_WINDOWS(wp)
1492 if (wp->w_id == id)
1493 return wp;
1494 #ifdef FEAT_PROP_POPUP
1495 FOR_ALL_POPUPWINS(wp)
1496 if (wp->w_id == id)
1497 return wp;
1498 FOR_ALL_POPUPWINS_IN_TAB(curtab, wp)
1499 if (wp->w_id == id)
1500 return wp;
1501 #endif
1502 return NULL;
1503 }
1504
1505 /*
1506 * Check if "win" is a pointer to an existing window in any tab page.
1507 */
1508 int
win_valid_any_tab(win_T * win)1509 win_valid_any_tab(win_T *win)
1510 {
1511 win_T *wp;
1512 tabpage_T *tp;
1513
1514 if (win == NULL)
1515 return FALSE;
1516 FOR_ALL_TABPAGES(tp)
1517 {
1518 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1519 {
1520 if (wp == win)
1521 return TRUE;
1522 }
1523 #ifdef FEAT_PROP_POPUP
1524 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
1525 if (wp == win)
1526 return TRUE;
1527 #endif
1528 }
1529 return win_valid_popup(win);
1530 }
1531
1532 /*
1533 * Return the number of windows.
1534 */
1535 int
win_count(void)1536 win_count(void)
1537 {
1538 win_T *wp;
1539 int count = 0;
1540
1541 FOR_ALL_WINDOWS(wp)
1542 ++count;
1543 return count;
1544 }
1545
1546 /*
1547 * Make "count" windows on the screen.
1548 * Return actual number of windows on the screen.
1549 * Must be called when there is just one window, filling the whole screen
1550 * (excluding the command line).
1551 */
1552 int
make_windows(int count,int vertical UNUSED)1553 make_windows(
1554 int count,
1555 int vertical UNUSED) // split windows vertically if TRUE
1556 {
1557 int maxcount;
1558 int todo;
1559
1560 if (vertical)
1561 {
1562 // Each windows needs at least 'winminwidth' lines and a separator
1563 // column.
1564 maxcount = (curwin->w_width + curwin->w_vsep_width
1565 - (p_wiw - p_wmw)) / (p_wmw + 1);
1566 }
1567 else
1568 {
1569 // Each window needs at least 'winminheight' lines and a status line.
1570 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
1571 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1572 }
1573
1574 if (maxcount < 2)
1575 maxcount = 2;
1576 if (count > maxcount)
1577 count = maxcount;
1578
1579 /*
1580 * add status line now, otherwise first window will be too big
1581 */
1582 if (count > 1)
1583 last_status(TRUE);
1584
1585 /*
1586 * Don't execute autocommands while creating the windows. Must do that
1587 * when putting the buffers in the windows.
1588 */
1589 block_autocmds();
1590
1591 // todo is number of windows left to create
1592 for (todo = count - 1; todo > 0; --todo)
1593 if (vertical)
1594 {
1595 if (win_split(curwin->w_width - (curwin->w_width - todo)
1596 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1597 break;
1598 }
1599 else
1600 {
1601 if (win_split(curwin->w_height - (curwin->w_height - todo
1602 * STATUS_HEIGHT) / (todo + 1)
1603 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1604 break;
1605 }
1606
1607 unblock_autocmds();
1608
1609 // return actual number of windows
1610 return (count - todo);
1611 }
1612
1613 /*
1614 * Exchange current and next window
1615 */
1616 static void
win_exchange(long Prenum)1617 win_exchange(long Prenum)
1618 {
1619 frame_T *frp;
1620 frame_T *frp2;
1621 win_T *wp;
1622 win_T *wp2;
1623 int temp;
1624
1625 if (ERROR_IF_ANY_POPUP_WINDOW)
1626 return;
1627 if (ONE_WINDOW) // just one window
1628 {
1629 beep_flush();
1630 return;
1631 }
1632
1633 #ifdef FEAT_GUI
1634 need_mouse_correct = TRUE;
1635 #endif
1636
1637 /*
1638 * find window to exchange with
1639 */
1640 if (Prenum)
1641 {
1642 frp = curwin->w_frame->fr_parent->fr_child;
1643 while (frp != NULL && --Prenum > 0)
1644 frp = frp->fr_next;
1645 }
1646 else if (curwin->w_frame->fr_next != NULL) // Swap with next
1647 frp = curwin->w_frame->fr_next;
1648 else // Swap last window in row/col with previous
1649 frp = curwin->w_frame->fr_prev;
1650
1651 // We can only exchange a window with another window, not with a frame
1652 // containing windows.
1653 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1654 return;
1655 wp = frp->fr_win;
1656
1657 /*
1658 * 1. remove curwin from the list. Remember after which window it was in wp2
1659 * 2. insert curwin before wp in the list
1660 * if wp != wp2
1661 * 3. remove wp from the list
1662 * 4. insert wp after wp2
1663 * 5. exchange the status line height and vsep width.
1664 */
1665 wp2 = curwin->w_prev;
1666 frp2 = curwin->w_frame->fr_prev;
1667 if (wp->w_prev != curwin)
1668 {
1669 win_remove(curwin, NULL);
1670 frame_remove(curwin->w_frame);
1671 win_append(wp->w_prev, curwin);
1672 frame_insert(frp, curwin->w_frame);
1673 }
1674 if (wp != wp2)
1675 {
1676 win_remove(wp, NULL);
1677 frame_remove(wp->w_frame);
1678 win_append(wp2, wp);
1679 if (frp2 == NULL)
1680 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1681 else
1682 frame_append(frp2, wp->w_frame);
1683 }
1684 temp = curwin->w_status_height;
1685 curwin->w_status_height = wp->w_status_height;
1686 wp->w_status_height = temp;
1687 temp = curwin->w_vsep_width;
1688 curwin->w_vsep_width = wp->w_vsep_width;
1689 wp->w_vsep_width = temp;
1690
1691 frame_fix_height(curwin);
1692 frame_fix_height(wp);
1693 frame_fix_width(curwin);
1694 frame_fix_width(wp);
1695
1696 (void)win_comp_pos(); // recompute window positions
1697
1698 win_enter(wp, TRUE);
1699 redraw_all_later(NOT_VALID);
1700 }
1701
1702 /*
1703 * rotate windows: if upwards TRUE the second window becomes the first one
1704 * if upwards FALSE the first window becomes the second one
1705 */
1706 static void
win_rotate(int upwards,int count)1707 win_rotate(int upwards, int count)
1708 {
1709 win_T *wp1;
1710 win_T *wp2;
1711 frame_T *frp;
1712 int n;
1713
1714 if (ONE_WINDOW) // nothing to do
1715 {
1716 beep_flush();
1717 return;
1718 }
1719
1720 #ifdef FEAT_GUI
1721 need_mouse_correct = TRUE;
1722 #endif
1723
1724 // Check if all frames in this row/col have one window.
1725 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child)
1726 if (frp->fr_win == NULL)
1727 {
1728 emsg(_("E443: Cannot rotate when another window is split"));
1729 return;
1730 }
1731
1732 while (count--)
1733 {
1734 if (upwards) // first window becomes last window
1735 {
1736 // remove first window/frame from the list
1737 frp = curwin->w_frame->fr_parent->fr_child;
1738 wp1 = frp->fr_win;
1739 win_remove(wp1, NULL);
1740 frame_remove(frp);
1741
1742 // find last frame and append removed window/frame after it
1743 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1744 ;
1745 win_append(frp->fr_win, wp1);
1746 frame_append(frp, wp1->w_frame);
1747
1748 wp2 = frp->fr_win; // previously last window
1749 }
1750 else // last window becomes first window
1751 {
1752 // find last window/frame in the list and remove it
1753 for (frp = curwin->w_frame; frp->fr_next != NULL;
1754 frp = frp->fr_next)
1755 ;
1756 wp1 = frp->fr_win;
1757 wp2 = wp1->w_prev; // will become last window
1758 win_remove(wp1, NULL);
1759 frame_remove(frp);
1760
1761 // append the removed window/frame before the first in the list
1762 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1763 frame_insert(frp->fr_parent->fr_child, frp);
1764 }
1765
1766 // exchange status height and vsep width of old and new last window
1767 n = wp2->w_status_height;
1768 wp2->w_status_height = wp1->w_status_height;
1769 wp1->w_status_height = n;
1770 frame_fix_height(wp1);
1771 frame_fix_height(wp2);
1772 n = wp2->w_vsep_width;
1773 wp2->w_vsep_width = wp1->w_vsep_width;
1774 wp1->w_vsep_width = n;
1775 frame_fix_width(wp1);
1776 frame_fix_width(wp2);
1777
1778 // recompute w_winrow and w_wincol for all windows
1779 (void)win_comp_pos();
1780 }
1781
1782 redraw_all_later(NOT_VALID);
1783 }
1784
1785 /*
1786 * Move the current window to the very top/bottom/left/right of the screen.
1787 */
1788 static void
win_totop(int size,int flags)1789 win_totop(int size, int flags)
1790 {
1791 int dir;
1792 int height = curwin->w_height;
1793
1794 if (ONE_WINDOW)
1795 {
1796 beep_flush();
1797 return;
1798 }
1799 if (check_split_disallowed() == FAIL)
1800 return;
1801
1802 // Remove the window and frame from the tree of frames.
1803 (void)winframe_remove(curwin, &dir, NULL);
1804 win_remove(curwin, NULL);
1805 last_status(FALSE); // may need to remove last status line
1806 (void)win_comp_pos(); // recompute window positions
1807
1808 // Split a window on the desired side and put the window there.
1809 (void)win_split_ins(size, flags, curwin, dir);
1810 if (!(flags & WSP_VERT))
1811 {
1812 win_setheight(height);
1813 if (p_ea)
1814 win_equal(curwin, TRUE, 'v');
1815 }
1816
1817 #if defined(FEAT_GUI)
1818 // When 'guioptions' includes 'L' or 'R' may have to remove or add
1819 // scrollbars. Have to update them anyway.
1820 gui_may_update_scrollbars();
1821 #endif
1822 }
1823
1824 /*
1825 * Move window "win1" to below/right of "win2" and make "win1" the current
1826 * window. Only works within the same frame!
1827 */
1828 void
win_move_after(win_T * win1,win_T * win2)1829 win_move_after(win_T *win1, win_T *win2)
1830 {
1831 int height;
1832
1833 // check if the arguments are reasonable
1834 if (win1 == win2)
1835 return;
1836
1837 // check if there is something to do
1838 if (win2->w_next != win1)
1839 {
1840 if (win1->w_frame->fr_parent != win2->w_frame->fr_parent)
1841 {
1842 iemsg("INTERNAL: trying to move a window into another frame");
1843 return;
1844 }
1845
1846 // may need to move the status line/vertical separator of the last
1847 // window
1848 if (win1 == lastwin)
1849 {
1850 height = win1->w_prev->w_status_height;
1851 win1->w_prev->w_status_height = win1->w_status_height;
1852 win1->w_status_height = height;
1853 if (win1->w_prev->w_vsep_width == 1)
1854 {
1855 // Remove the vertical separator from the last-but-one window,
1856 // add it to the last window. Adjust the frame widths.
1857 win1->w_prev->w_vsep_width = 0;
1858 win1->w_prev->w_frame->fr_width -= 1;
1859 win1->w_vsep_width = 1;
1860 win1->w_frame->fr_width += 1;
1861 }
1862 }
1863 else if (win2 == lastwin)
1864 {
1865 height = win1->w_status_height;
1866 win1->w_status_height = win2->w_status_height;
1867 win2->w_status_height = height;
1868 if (win1->w_vsep_width == 1)
1869 {
1870 // Remove the vertical separator from win1, add it to the last
1871 // window, win2. Adjust the frame widths.
1872 win2->w_vsep_width = 1;
1873 win2->w_frame->fr_width += 1;
1874 win1->w_vsep_width = 0;
1875 win1->w_frame->fr_width -= 1;
1876 }
1877 }
1878 win_remove(win1, NULL);
1879 frame_remove(win1->w_frame);
1880 win_append(win2, win1);
1881 frame_append(win2->w_frame, win1->w_frame);
1882
1883 (void)win_comp_pos(); // recompute w_winrow for all windows
1884 redraw_later(NOT_VALID);
1885 }
1886 win_enter(win1, FALSE);
1887 }
1888
1889 /*
1890 * Make all windows the same height.
1891 * 'next_curwin' will soon be the current window, make sure it has enough
1892 * rows.
1893 */
1894 void
win_equal(win_T * next_curwin,int current,int dir)1895 win_equal(
1896 win_T *next_curwin, // pointer to current window to be or NULL
1897 int current, // do only frame with current window
1898 int dir) // 'v' for vertically, 'h' for horizontally,
1899 // 'b' for both, 0 for using p_ead
1900 {
1901 if (dir == 0)
1902 dir = *p_ead;
1903 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1904 topframe, dir, 0, tabline_height(),
1905 (int)Columns, topframe->fr_height);
1906 }
1907
1908 /*
1909 * Set a frame to a new position and height, spreading the available room
1910 * equally over contained frames.
1911 * The window "next_curwin" (if not NULL) should at least get the size from
1912 * 'winheight' and 'winwidth' if possible.
1913 */
1914 static void
win_equal_rec(win_T * next_curwin,int current,frame_T * topfr,int dir,int col,int row,int width,int height)1915 win_equal_rec(
1916 win_T *next_curwin, // pointer to current window to be or NULL
1917 int current, // do only frame with current window
1918 frame_T *topfr, // frame to set size off
1919 int dir, // 'v', 'h' or 'b', see win_equal()
1920 int col, // horizontal position for frame
1921 int row, // vertical position for frame
1922 int width, // new width of frame
1923 int height) // new height of frame
1924 {
1925 int n, m;
1926 int extra_sep = 0;
1927 int wincount, totwincount = 0;
1928 frame_T *fr;
1929 int next_curwin_size = 0;
1930 int room = 0;
1931 int new_size;
1932 int has_next_curwin = 0;
1933 int hnc;
1934
1935 if (topfr->fr_layout == FR_LEAF)
1936 {
1937 // Set the width/height of this frame.
1938 // Redraw when size or position changes
1939 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1940 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1941 )
1942 {
1943 topfr->fr_win->w_winrow = row;
1944 frame_new_height(topfr, height, FALSE, FALSE);
1945 topfr->fr_win->w_wincol = col;
1946 frame_new_width(topfr, width, FALSE, FALSE);
1947 redraw_all_later(NOT_VALID);
1948 }
1949 }
1950 else if (topfr->fr_layout == FR_ROW)
1951 {
1952 topfr->fr_width = width;
1953 topfr->fr_height = height;
1954
1955 if (dir != 'v') // equalize frame widths
1956 {
1957 // Compute the maximum number of windows horizontally in this
1958 // frame.
1959 n = frame_minwidth(topfr, NOWIN);
1960 // add one for the rightmost window, it doesn't have a separator
1961 if (col + width == Columns)
1962 extra_sep = 1;
1963 else
1964 extra_sep = 0;
1965 totwincount = (n + extra_sep) / (p_wmw + 1);
1966 has_next_curwin = frame_has_win(topfr, next_curwin);
1967
1968 /*
1969 * Compute width for "next_curwin" window and room available for
1970 * other windows.
1971 * "m" is the minimal width when counting p_wiw for "next_curwin".
1972 */
1973 m = frame_minwidth(topfr, next_curwin);
1974 room = width - m;
1975 if (room < 0)
1976 {
1977 next_curwin_size = p_wiw + room;
1978 room = 0;
1979 }
1980 else
1981 {
1982 next_curwin_size = -1;
1983 FOR_ALL_FRAMES(fr, topfr->fr_child)
1984 {
1985 // If 'winfixwidth' set keep the window width if
1986 // possible.
1987 // Watch out for this window being the next_curwin.
1988 if (frame_fixed_width(fr))
1989 {
1990 n = frame_minwidth(fr, NOWIN);
1991 new_size = fr->fr_width;
1992 if (frame_has_win(fr, next_curwin))
1993 {
1994 room += p_wiw - p_wmw;
1995 next_curwin_size = 0;
1996 if (new_size < p_wiw)
1997 new_size = p_wiw;
1998 }
1999 else
2000 // These windows don't use up room.
2001 totwincount -= (n + (fr->fr_next == NULL
2002 ? extra_sep : 0)) / (p_wmw + 1);
2003 room -= new_size - n;
2004 if (room < 0)
2005 {
2006 new_size += room;
2007 room = 0;
2008 }
2009 fr->fr_newwidth = new_size;
2010 }
2011 }
2012 if (next_curwin_size == -1)
2013 {
2014 if (!has_next_curwin)
2015 next_curwin_size = 0;
2016 else if (totwincount > 1
2017 && (room + (totwincount - 2))
2018 / (totwincount - 1) > p_wiw)
2019 {
2020 // Can make all windows wider than 'winwidth', spread
2021 // the room equally.
2022 next_curwin_size = (room + p_wiw
2023 + (totwincount - 1) * p_wmw
2024 + (totwincount - 1)) / totwincount;
2025 room -= next_curwin_size - p_wiw;
2026 }
2027 else
2028 next_curwin_size = p_wiw;
2029 }
2030 }
2031
2032 if (has_next_curwin)
2033 --totwincount; // don't count curwin
2034 }
2035
2036 FOR_ALL_FRAMES(fr, topfr->fr_child)
2037 {
2038 wincount = 1;
2039 if (fr->fr_next == NULL)
2040 // last frame gets all that remains (avoid roundoff error)
2041 new_size = width;
2042 else if (dir == 'v')
2043 new_size = fr->fr_width;
2044 else if (frame_fixed_width(fr))
2045 {
2046 new_size = fr->fr_newwidth;
2047 wincount = 0; // doesn't count as a sizeable window
2048 }
2049 else
2050 {
2051 // Compute the maximum number of windows horiz. in "fr".
2052 n = frame_minwidth(fr, NOWIN);
2053 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2054 / (p_wmw + 1);
2055 m = frame_minwidth(fr, next_curwin);
2056 if (has_next_curwin)
2057 hnc = frame_has_win(fr, next_curwin);
2058 else
2059 hnc = FALSE;
2060 if (hnc) // don't count next_curwin
2061 --wincount;
2062 if (totwincount == 0)
2063 new_size = room;
2064 else
2065 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2066 / totwincount;
2067 if (hnc) // add next_curwin size
2068 {
2069 next_curwin_size -= p_wiw - (m - n);
2070 new_size += next_curwin_size;
2071 room -= new_size - next_curwin_size;
2072 }
2073 else
2074 room -= new_size;
2075 new_size += n;
2076 }
2077
2078 // Skip frame that is full width when splitting or closing a
2079 // window, unless equalizing all frames.
2080 if (!current || dir != 'v' || topfr->fr_parent != NULL
2081 || (new_size != fr->fr_width)
2082 || frame_has_win(fr, next_curwin))
2083 win_equal_rec(next_curwin, current, fr, dir, col, row,
2084 new_size, height);
2085 col += new_size;
2086 width -= new_size;
2087 totwincount -= wincount;
2088 }
2089 }
2090 else // topfr->fr_layout == FR_COL
2091 {
2092 topfr->fr_width = width;
2093 topfr->fr_height = height;
2094
2095 if (dir != 'h') // equalize frame heights
2096 {
2097 // Compute maximum number of windows vertically in this frame.
2098 n = frame_minheight(topfr, NOWIN);
2099 // add one for the bottom window if it doesn't have a statusline
2100 if (row + height == cmdline_row && p_ls == 0)
2101 extra_sep = 1;
2102 else
2103 extra_sep = 0;
2104 totwincount = (n + extra_sep) / (p_wmh + 1);
2105 has_next_curwin = frame_has_win(topfr, next_curwin);
2106
2107 /*
2108 * Compute height for "next_curwin" window and room available for
2109 * other windows.
2110 * "m" is the minimal height when counting p_wh for "next_curwin".
2111 */
2112 m = frame_minheight(topfr, next_curwin);
2113 room = height - m;
2114 if (room < 0)
2115 {
2116 // The room is less then 'winheight', use all space for the
2117 // current window.
2118 next_curwin_size = p_wh + room;
2119 room = 0;
2120 }
2121 else
2122 {
2123 next_curwin_size = -1;
2124 FOR_ALL_FRAMES(fr, topfr->fr_child)
2125 {
2126 // If 'winfixheight' set keep the window height if
2127 // possible.
2128 // Watch out for this window being the next_curwin.
2129 if (frame_fixed_height(fr))
2130 {
2131 n = frame_minheight(fr, NOWIN);
2132 new_size = fr->fr_height;
2133 if (frame_has_win(fr, next_curwin))
2134 {
2135 room += p_wh - p_wmh;
2136 next_curwin_size = 0;
2137 if (new_size < p_wh)
2138 new_size = p_wh;
2139 }
2140 else
2141 // These windows don't use up room.
2142 totwincount -= (n + (fr->fr_next == NULL
2143 ? extra_sep : 0)) / (p_wmh + 1);
2144 room -= new_size - n;
2145 if (room < 0)
2146 {
2147 new_size += room;
2148 room = 0;
2149 }
2150 fr->fr_newheight = new_size;
2151 }
2152 }
2153 if (next_curwin_size == -1)
2154 {
2155 if (!has_next_curwin)
2156 next_curwin_size = 0;
2157 else if (totwincount > 1
2158 && (room + (totwincount - 2))
2159 / (totwincount - 1) > p_wh)
2160 {
2161 // can make all windows higher than 'winheight',
2162 // spread the room equally.
2163 next_curwin_size = (room + p_wh
2164 + (totwincount - 1) * p_wmh
2165 + (totwincount - 1)) / totwincount;
2166 room -= next_curwin_size - p_wh;
2167 }
2168 else
2169 next_curwin_size = p_wh;
2170 }
2171 }
2172
2173 if (has_next_curwin)
2174 --totwincount; // don't count curwin
2175 }
2176
2177 FOR_ALL_FRAMES(fr, topfr->fr_child)
2178 {
2179 wincount = 1;
2180 if (fr->fr_next == NULL)
2181 // last frame gets all that remains (avoid roundoff error)
2182 new_size = height;
2183 else if (dir == 'h')
2184 new_size = fr->fr_height;
2185 else if (frame_fixed_height(fr))
2186 {
2187 new_size = fr->fr_newheight;
2188 wincount = 0; // doesn't count as a sizeable window
2189 }
2190 else
2191 {
2192 // Compute the maximum number of windows vert. in "fr".
2193 n = frame_minheight(fr, NOWIN);
2194 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2195 / (p_wmh + 1);
2196 m = frame_minheight(fr, next_curwin);
2197 if (has_next_curwin)
2198 hnc = frame_has_win(fr, next_curwin);
2199 else
2200 hnc = FALSE;
2201 if (hnc) // don't count next_curwin
2202 --wincount;
2203 if (totwincount == 0)
2204 new_size = room;
2205 else
2206 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2207 / totwincount;
2208 if (hnc) // add next_curwin size
2209 {
2210 next_curwin_size -= p_wh - (m - n);
2211 new_size += next_curwin_size;
2212 room -= new_size - next_curwin_size;
2213 }
2214 else
2215 room -= new_size;
2216 new_size += n;
2217 }
2218 // Skip frame that is full width when splitting or closing a
2219 // window, unless equalizing all frames.
2220 if (!current || dir != 'h' || topfr->fr_parent != NULL
2221 || (new_size != fr->fr_height)
2222 || frame_has_win(fr, next_curwin))
2223 win_equal_rec(next_curwin, current, fr, dir, col, row,
2224 width, new_size);
2225 row += new_size;
2226 height -= new_size;
2227 totwincount -= wincount;
2228 }
2229 }
2230 }
2231
2232 #ifdef FEAT_JOB_CHANNEL
2233 static void
leaving_window(win_T * win)2234 leaving_window(win_T *win)
2235 {
2236 // Only matters for a prompt window.
2237 if (!bt_prompt(win->w_buffer))
2238 return;
2239
2240 // When leaving a prompt window stop Insert mode and perhaps restart
2241 // it when entering that window again.
2242 win->w_buffer->b_prompt_insert = restart_edit;
2243 if (restart_edit != 0 && mode_displayed)
2244 clear_cmdline = TRUE; // unshow mode later
2245 restart_edit = NUL;
2246
2247 // When leaving the window (or closing the window) was done from a
2248 // callback we need to break out of the Insert mode loop and restart Insert
2249 // mode when entering the window again.
2250 if (State & INSERT)
2251 {
2252 stop_insert_mode = TRUE;
2253 if (win->w_buffer->b_prompt_insert == NUL)
2254 win->w_buffer->b_prompt_insert = 'A';
2255 }
2256 }
2257
2258 void
entering_window(win_T * win)2259 entering_window(win_T *win)
2260 {
2261 // Only matters for a prompt window.
2262 if (!bt_prompt(win->w_buffer))
2263 return;
2264
2265 // When switching to a prompt buffer that was in Insert mode, don't stop
2266 // Insert mode, it may have been set in leaving_window().
2267 if (win->w_buffer->b_prompt_insert != NUL)
2268 stop_insert_mode = FALSE;
2269
2270 // When entering the prompt window restart Insert mode if we were in Insert
2271 // mode when we left it.
2272 restart_edit = win->w_buffer->b_prompt_insert;
2273 }
2274 #endif
2275
2276 /*
2277 * Close all windows for buffer "buf".
2278 */
2279 void
close_windows(buf_T * buf,int keep_curwin)2280 close_windows(
2281 buf_T *buf,
2282 int keep_curwin) // don't close "curwin"
2283 {
2284 win_T *wp;
2285 tabpage_T *tp, *nexttp;
2286 int h = tabline_height();
2287 int count = tabpage_index(NULL);
2288
2289 ++RedrawingDisabled;
2290
2291 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
2292 {
2293 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2294 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
2295 {
2296 if (win_close(wp, FALSE) == FAIL)
2297 // If closing the window fails give up, to avoid looping
2298 // forever.
2299 break;
2300
2301 // Start all over, autocommands may change the window layout.
2302 wp = firstwin;
2303 }
2304 else
2305 wp = wp->w_next;
2306 }
2307
2308 // Also check windows in other tab pages.
2309 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2310 {
2311 nexttp = tp->tp_next;
2312 if (tp != curtab)
2313 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
2314 if (wp->w_buffer == buf
2315 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
2316 {
2317 win_close_othertab(wp, FALSE, tp);
2318
2319 // Start all over, the tab page may be closed and
2320 // autocommands may change the window layout.
2321 nexttp = first_tabpage;
2322 break;
2323 }
2324 }
2325
2326 --RedrawingDisabled;
2327
2328 if (count != tabpage_index(NULL))
2329 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2330
2331 redraw_tabline = TRUE;
2332 if (h != tabline_height())
2333 shell_new_rows();
2334 }
2335
2336 /*
2337 * Return TRUE if the current window is the only window that exists (ignoring
2338 * "aucmd_win").
2339 * Returns FALSE if there is a window, possibly in another tab page.
2340 */
2341 static int
last_window(void)2342 last_window(void)
2343 {
2344 return (one_window() && first_tabpage->tp_next == NULL);
2345 }
2346
2347 /*
2348 * Return TRUE if there is only one window other than "aucmd_win" in the
2349 * current tab page.
2350 */
2351 int
one_window(void)2352 one_window(void)
2353 {
2354 win_T *wp;
2355 int seen_one = FALSE;
2356
2357 FOR_ALL_WINDOWS(wp)
2358 {
2359 if (wp != aucmd_win)
2360 {
2361 if (seen_one)
2362 return FALSE;
2363 seen_one = TRUE;
2364 }
2365 }
2366 return TRUE;
2367 }
2368
2369 /*
2370 * Close the possibly last window in a tab page.
2371 * Returns TRUE when the window was closed already.
2372 */
2373 static int
close_last_window_tabpage(win_T * win,int free_buf,tabpage_T * prev_curtab)2374 close_last_window_tabpage(
2375 win_T *win,
2376 int free_buf,
2377 tabpage_T *prev_curtab)
2378 {
2379 if (ONE_WINDOW)
2380 {
2381 buf_T *old_curbuf = curbuf;
2382
2383 /*
2384 * Closing the last window in a tab page. First go to another tab
2385 * page and then close the window and the tab page. This avoids that
2386 * curwin and curtab are invalid while we are freeing memory, they may
2387 * be used in GUI events.
2388 * Don't trigger autocommands yet, they may use wrong values, so do
2389 * that below.
2390 */
2391 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
2392 redraw_tabline = TRUE;
2393
2394 // Safety check: Autocommands may have closed the window when jumping
2395 // to the other tab page.
2396 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2397 {
2398 int h = tabline_height();
2399
2400 win_close_othertab(win, free_buf, prev_curtab);
2401 if (h != tabline_height())
2402 shell_new_rows();
2403 }
2404 #ifdef FEAT_JOB_CHANNEL
2405 entering_window(curwin);
2406 #endif
2407 // Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2408 // that now.
2409 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2410 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2411 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2412 if (old_curbuf != curbuf)
2413 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2414 return TRUE;
2415 }
2416 return FALSE;
2417 }
2418
2419 /*
2420 * Close the buffer of "win" and unload it if "action" is DOBUF_UNLOAD.
2421 * "action" can also be zero (do nothing) or DOBUF_WIPE.
2422 * "abort_if_last" is passed to close_buffer(): abort closing if all other
2423 * windows are closed.
2424 */
2425 static void
win_close_buffer(win_T * win,int action,int abort_if_last)2426 win_close_buffer(win_T *win, int action, int abort_if_last)
2427 {
2428 #ifdef FEAT_SYN_HL
2429 // Free independent synblock before the buffer is freed.
2430 if (win->w_buffer != NULL)
2431 reset_synblock(win);
2432 #endif
2433
2434 #ifdef FEAT_QUICKFIX
2435 // When the quickfix/location list window is closed, unlist the buffer.
2436 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
2437 win->w_buffer->b_p_bl = FALSE;
2438 #endif
2439
2440 // Close the link to the buffer.
2441 if (win->w_buffer != NULL)
2442 {
2443 bufref_T bufref;
2444
2445 set_bufref(&bufref, curbuf);
2446 win->w_closing = TRUE;
2447 close_buffer(win, win->w_buffer, action, abort_if_last, FALSE);
2448 if (win_valid_any_tab(win))
2449 win->w_closing = FALSE;
2450 // Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2451 // "wipe".
2452 if (!bufref_valid(&bufref))
2453 curbuf = firstbuf;
2454 }
2455 }
2456
2457 /*
2458 * Close window "win". Only works for the current tab page.
2459 * If "free_buf" is TRUE related buffer may be unloaded.
2460 *
2461 * Called by :quit, :close, :xit, :wq and findtag().
2462 * Returns FAIL when the window was not closed.
2463 */
2464 int
win_close(win_T * win,int free_buf)2465 win_close(win_T *win, int free_buf)
2466 {
2467 win_T *wp;
2468 int other_buffer = FALSE;
2469 int close_curwin = FALSE;
2470 int dir;
2471 int help_window = FALSE;
2472 tabpage_T *prev_curtab = curtab;
2473 frame_T *win_frame = win->w_frame->fr_parent;
2474 #ifdef FEAT_DIFF
2475 int had_diffmode = win->w_p_diff;
2476 #endif
2477 #ifdef MESSAGE_QUEUE
2478 int did_decrement = FALSE;
2479 #endif
2480
2481 #if defined(FEAT_TERMINAL) && defined(FEAT_PROP_POPUP)
2482 // Can close a popup window with a terminal if the job has finished.
2483 if (may_close_term_popup() == OK)
2484 return OK;
2485 #endif
2486 if (ERROR_IF_ANY_POPUP_WINDOW)
2487 return FAIL;
2488
2489 if (last_window())
2490 {
2491 emsg(_("E444: Cannot close last window"));
2492 return FAIL;
2493 }
2494
2495 if (win->w_closing || (win->w_buffer != NULL
2496 && win->w_buffer->b_locked > 0))
2497 return FAIL; // window is already being closed
2498 if (win_unlisted(win))
2499 {
2500 emsg(_(e_autocmd_close));
2501 return FAIL;
2502 }
2503 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2504 {
2505 emsg(_("E814: Cannot close window, only autocmd window would remain"));
2506 return FAIL;
2507 }
2508
2509 // When closing the last window in a tab page first go to another tab page
2510 // and then close the window and the tab page to avoid that curwin and
2511 // curtab are invalid while we are freeing memory.
2512 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2513 return FAIL;
2514
2515 // When closing the help window, try restoring a snapshot after closing
2516 // the window. Otherwise clear the snapshot, it's now invalid.
2517 if (bt_help(win->w_buffer))
2518 help_window = TRUE;
2519 else
2520 clear_snapshot(curtab, SNAP_HELP_IDX);
2521
2522 if (win == curwin)
2523 {
2524 #ifdef FEAT_JOB_CHANNEL
2525 leaving_window(curwin);
2526 #endif
2527 /*
2528 * Guess which window is going to be the new current window.
2529 * This may change because of the autocommands (sigh).
2530 */
2531 wp = frame2win(win_altframe(win, NULL));
2532
2533 /*
2534 * Be careful: If autocommands delete the window or cause this window
2535 * to be the last one left, return now.
2536 */
2537 if (wp->w_buffer != curbuf)
2538 {
2539 other_buffer = TRUE;
2540 win->w_closing = TRUE;
2541 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2542 if (!win_valid(win))
2543 return FAIL;
2544 win->w_closing = FALSE;
2545 if (last_window())
2546 return FAIL;
2547 }
2548 win->w_closing = TRUE;
2549 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2550 if (!win_valid(win))
2551 return FAIL;
2552 win->w_closing = FALSE;
2553 if (last_window())
2554 return FAIL;
2555 #ifdef FEAT_EVAL
2556 // autocmds may abort script processing
2557 if (aborting())
2558 return FAIL;
2559 #endif
2560 }
2561
2562 #ifdef FEAT_GUI
2563 // Avoid trouble with scrollbars that are going to be deleted in
2564 // win_free().
2565 if (gui.in_use)
2566 out_flush();
2567 #endif
2568
2569 #ifdef FEAT_PROP_POPUP
2570 if (popup_win_closed(win) && !win_valid(win))
2571 return FAIL;
2572 #endif
2573
2574 // Trigger WinClosed just before starting to free window-related resources.
2575 trigger_winclosed(win);
2576 // autocmd may have freed the window already.
2577 if (!win_valid_any_tab(win))
2578 return OK;
2579
2580 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE);
2581
2582 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2583 && (last_window() || curtab != prev_curtab
2584 || close_last_window_tabpage(win, free_buf, prev_curtab)))
2585 {
2586 // Autocommands have closed all windows, quit now. Restore
2587 // curwin->w_buffer, otherwise writing viminfo may fail.
2588 if (curwin->w_buffer == NULL)
2589 curwin->w_buffer = curbuf;
2590 getout(0);
2591 }
2592
2593 // Autocommands may have moved to another tab page.
2594 if (curtab != prev_curtab && win_valid_any_tab(win)
2595 && win->w_buffer == NULL)
2596 {
2597 // Need to close the window anyway, since the buffer is NULL.
2598 win_close_othertab(win, FALSE, prev_curtab);
2599 return FAIL;
2600 }
2601
2602 // Autocommands may have closed the window already or closed the only
2603 // other window.
2604 if (!win_valid(win) || last_window()
2605 || close_last_window_tabpage(win, free_buf, prev_curtab))
2606 return FAIL;
2607
2608 // Now we are really going to close the window. Disallow any autocommand
2609 // to split a window to avoid trouble.
2610 // Also bail out of parse_queued_messages() to avoid it tries to update the
2611 // screen.
2612 ++split_disallowed;
2613 #ifdef MESSAGE_QUEUE
2614 ++dont_parse_messages;
2615 #endif
2616
2617 // Free the memory used for the window and get the window that received
2618 // the screen space.
2619 wp = win_free_mem(win, &dir, NULL);
2620
2621 // Make sure curwin isn't invalid. It can cause severe trouble when
2622 // printing an error message. For win_equal() curbuf needs to be valid
2623 // too.
2624 if (win == curwin)
2625 {
2626 curwin = wp;
2627 #ifdef FEAT_QUICKFIX
2628 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2629 {
2630 /*
2631 * If the cursor goes to the preview or the quickfix window, try
2632 * finding another window to go to.
2633 */
2634 for (;;)
2635 {
2636 if (wp->w_next == NULL)
2637 wp = firstwin;
2638 else
2639 wp = wp->w_next;
2640 if (wp == curwin)
2641 break;
2642 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2643 {
2644 curwin = wp;
2645 break;
2646 }
2647 }
2648 }
2649 #endif
2650 curbuf = curwin->w_buffer;
2651 close_curwin = TRUE;
2652
2653 // The cursor position may be invalid if the buffer changed after last
2654 // using the window.
2655 check_cursor();
2656 }
2657 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
2658 // If the frame of the closed window contains the new current window,
2659 // only resize that frame. Otherwise resize all windows.
2660 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
2661 else
2662 win_comp_pos();
2663 if (close_curwin)
2664 {
2665 // Pass WEE_ALLOW_PARSE_MESSAGES to decrement dont_parse_messages
2666 // before autocommands.
2667 #ifdef MESSAGE_QUEUE
2668 did_decrement =
2669 #else
2670 (void)
2671 #endif
2672 win_enter_ext(wp,
2673 WEE_CURWIN_INVALID | WEE_TRIGGER_ENTER_AUTOCMDS
2674 | WEE_TRIGGER_LEAVE_AUTOCMDS | WEE_ALLOW_PARSE_MESSAGES);
2675 if (other_buffer)
2676 // careful: after this wp and win may be invalid!
2677 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2678 }
2679
2680 --split_disallowed;
2681 #ifdef MESSAGE_QUEUE
2682 if (!did_decrement)
2683 --dont_parse_messages;
2684 #endif
2685
2686 /*
2687 * If last window has a status line now and we don't want one,
2688 * remove the status line.
2689 */
2690 last_status(FALSE);
2691
2692 // After closing the help window, try restoring the window layout from
2693 // before it was opened.
2694 if (help_window)
2695 restore_snapshot(SNAP_HELP_IDX, close_curwin);
2696
2697 #ifdef FEAT_DIFF
2698 // If the window had 'diff' set and now there is only one window left in
2699 // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then
2700 // execute ":diffoff!".
2701 if (diffopt_closeoff() && had_diffmode && curtab == prev_curtab)
2702 {
2703 int diffcount = 0;
2704 win_T *dwin;
2705
2706 FOR_ALL_WINDOWS(dwin)
2707 if (dwin->w_p_diff)
2708 ++diffcount;
2709 if (diffcount == 1)
2710 do_cmdline_cmd((char_u *)"diffoff!");
2711 }
2712 #endif
2713
2714 #if defined(FEAT_GUI)
2715 // When 'guioptions' includes 'L' or 'R' may have to remove scrollbars.
2716 if (gui.in_use && !win_hasvertsplit())
2717 gui_init_which_components(NULL);
2718 #endif
2719
2720 redraw_all_later(NOT_VALID);
2721 return OK;
2722 }
2723
2724 static void
trigger_winclosed(win_T * win)2725 trigger_winclosed(win_T *win)
2726 {
2727 static int recursive = FALSE;
2728 char_u winid[NUMBUFLEN];
2729
2730 if (recursive)
2731 return;
2732 recursive = TRUE;
2733 vim_snprintf((char *)winid, sizeof(winid), "%i", win->w_id);
2734 apply_autocmds(EVENT_WINCLOSED, winid, winid, FALSE, win->w_buffer);
2735 recursive = FALSE;
2736 }
2737
2738 /*
2739 * Close window "win" in tab page "tp", which is not the current tab page.
2740 * This may be the last window in that tab page and result in closing the tab,
2741 * thus "tp" may become invalid!
2742 * Caller must check if buffer is hidden and whether the tabline needs to be
2743 * updated.
2744 */
2745 void
win_close_othertab(win_T * win,int free_buf,tabpage_T * tp)2746 win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
2747 {
2748 win_T *wp;
2749 int dir;
2750 tabpage_T *ptp = NULL;
2751 int free_tp = FALSE;
2752
2753 // Get here with win->w_buffer == NULL when win_close() detects the tab
2754 // page changed.
2755 if (win->w_closing || (win->w_buffer != NULL
2756 && win->w_buffer->b_locked > 0))
2757 return; // window is already being closed
2758
2759 // Trigger WinClosed just before starting to free window-related resources.
2760 trigger_winclosed(win);
2761 // autocmd may have freed the window already.
2762 if (!win_valid_any_tab(win))
2763 return;
2764
2765 if (win->w_buffer != NULL)
2766 // Close the link to the buffer.
2767 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0,
2768 FALSE, FALSE);
2769
2770 // Careful: Autocommands may have closed the tab page or made it the
2771 // current tab page.
2772 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2773 ;
2774 if (ptp == NULL || tp == curtab)
2775 return;
2776
2777 // Autocommands may have closed the window already.
2778 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2779 ;
2780 if (wp == NULL)
2781 return;
2782
2783 // When closing the last window in a tab page remove the tab page.
2784 if (tp->tp_firstwin == tp->tp_lastwin)
2785 {
2786 if (tp == first_tabpage)
2787 first_tabpage = tp->tp_next;
2788 else
2789 {
2790 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2791 ptp = ptp->tp_next)
2792 ;
2793 if (ptp == NULL)
2794 {
2795 internal_error("win_close_othertab()");
2796 return;
2797 }
2798 ptp->tp_next = tp->tp_next;
2799 }
2800 free_tp = TRUE;
2801 }
2802
2803 // Free the memory used for the window.
2804 win_free_mem(win, &dir, tp);
2805
2806 if (free_tp)
2807 free_tabpage(tp);
2808 }
2809
2810 /*
2811 * Free the memory used for a window.
2812 * Returns a pointer to the window that got the freed up space.
2813 */
2814 static win_T *
win_free_mem(win_T * win,int * dirp,tabpage_T * tp)2815 win_free_mem(
2816 win_T *win,
2817 int *dirp, // set to 'v' or 'h' for direction if 'ea'
2818 tabpage_T *tp) // tab page "win" is in, NULL for current
2819 {
2820 frame_T *frp;
2821 win_T *wp;
2822 tabpage_T *win_tp = tp == NULL ? curtab : tp;
2823
2824 // Remove the window and its frame from the tree of frames.
2825 frp = win->w_frame;
2826 wp = winframe_remove(win, dirp, tp);
2827 vim_free(frp);
2828 win_free(win, tp);
2829
2830 // When deleting the current window in the tab, select a new current
2831 // window.
2832 if (win == win_tp->tp_curwin)
2833 win_tp->tp_curwin = wp;
2834
2835 return wp;
2836 }
2837
2838 #if defined(EXITFREE) || defined(PROTO)
2839 void
win_free_all(void)2840 win_free_all(void)
2841 {
2842 int dummy;
2843
2844 while (first_tabpage->tp_next != NULL)
2845 tabpage_close(TRUE);
2846
2847 if (aucmd_win != NULL)
2848 {
2849 (void)win_free_mem(aucmd_win, &dummy, NULL);
2850 aucmd_win = NULL;
2851 }
2852
2853 while (firstwin != NULL)
2854 (void)win_free_mem(firstwin, &dummy, NULL);
2855
2856 // No window should be used after this. Set curwin to NULL to crash
2857 // instead of using freed memory.
2858 curwin = NULL;
2859 }
2860 #endif
2861
2862 /*
2863 * Remove a window and its frame from the tree of frames.
2864 * Returns a pointer to the window that got the freed up space.
2865 */
2866 win_T *
winframe_remove(win_T * win,int * dirp UNUSED,tabpage_T * tp)2867 winframe_remove(
2868 win_T *win,
2869 int *dirp UNUSED, // set to 'v' or 'h' for direction if 'ea'
2870 tabpage_T *tp) // tab page "win" is in, NULL for current
2871 {
2872 frame_T *frp, *frp2, *frp3;
2873 frame_T *frp_close = win->w_frame;
2874 win_T *wp;
2875
2876 /*
2877 * If there is only one window there is nothing to remove.
2878 */
2879 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
2880 return NULL;
2881
2882 /*
2883 * Remove the window from its frame.
2884 */
2885 frp2 = win_altframe(win, tp);
2886 wp = frame2win(frp2);
2887
2888 // Remove this frame from the list of frames.
2889 frame_remove(frp_close);
2890
2891 if (frp_close->fr_parent->fr_layout == FR_COL)
2892 {
2893 // When 'winfixheight' is set, try to find another frame in the column
2894 // (as close to the closed frame as possible) to distribute the height
2895 // to.
2896 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2897 {
2898 frp = frp_close->fr_prev;
2899 frp3 = frp_close->fr_next;
2900 while (frp != NULL || frp3 != NULL)
2901 {
2902 if (frp != NULL)
2903 {
2904 if (!frame_fixed_height(frp))
2905 {
2906 frp2 = frp;
2907 wp = frame2win(frp2);
2908 break;
2909 }
2910 frp = frp->fr_prev;
2911 }
2912 if (frp3 != NULL)
2913 {
2914 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2915 {
2916 frp2 = frp3;
2917 wp = frp3->fr_win;
2918 break;
2919 }
2920 frp3 = frp3->fr_next;
2921 }
2922 }
2923 }
2924 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2925 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2926 *dirp = 'v';
2927 }
2928 else
2929 {
2930 // When 'winfixwidth' is set, try to find another frame in the column
2931 // (as close to the closed frame as possible) to distribute the width
2932 // to.
2933 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2934 {
2935 frp = frp_close->fr_prev;
2936 frp3 = frp_close->fr_next;
2937 while (frp != NULL || frp3 != NULL)
2938 {
2939 if (frp != NULL)
2940 {
2941 if (!frame_fixed_width(frp))
2942 {
2943 frp2 = frp;
2944 wp = frame2win(frp2);
2945 break;
2946 }
2947 frp = frp->fr_prev;
2948 }
2949 if (frp3 != NULL)
2950 {
2951 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2952 {
2953 frp2 = frp3;
2954 wp = frp3->fr_win;
2955 break;
2956 }
2957 frp3 = frp3->fr_next;
2958 }
2959 }
2960 }
2961 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2962 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2963 *dirp = 'h';
2964 }
2965
2966 // If rows/columns go to a window below/right its positions need to be
2967 // updated. Can only be done after the sizes have been updated.
2968 if (frp2 == frp_close->fr_next)
2969 {
2970 int row = win->w_winrow;
2971 int col = win->w_wincol;
2972
2973 frame_comp_pos(frp2, &row, &col);
2974 }
2975
2976 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2977 {
2978 // There is no other frame in this list, move its info to the parent
2979 // and remove it.
2980 frp2->fr_parent->fr_layout = frp2->fr_layout;
2981 frp2->fr_parent->fr_child = frp2->fr_child;
2982 FOR_ALL_FRAMES(frp, frp2->fr_child)
2983 frp->fr_parent = frp2->fr_parent;
2984 frp2->fr_parent->fr_win = frp2->fr_win;
2985 if (frp2->fr_win != NULL)
2986 frp2->fr_win->w_frame = frp2->fr_parent;
2987 frp = frp2->fr_parent;
2988 if (topframe->fr_child == frp2)
2989 topframe->fr_child = frp;
2990 vim_free(frp2);
2991
2992 frp2 = frp->fr_parent;
2993 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2994 {
2995 // The frame above the parent has the same layout, have to merge
2996 // the frames into this list.
2997 if (frp2->fr_child == frp)
2998 frp2->fr_child = frp->fr_child;
2999 frp->fr_child->fr_prev = frp->fr_prev;
3000 if (frp->fr_prev != NULL)
3001 frp->fr_prev->fr_next = frp->fr_child;
3002 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
3003 {
3004 frp3->fr_parent = frp2;
3005 if (frp3->fr_next == NULL)
3006 {
3007 frp3->fr_next = frp->fr_next;
3008 if (frp->fr_next != NULL)
3009 frp->fr_next->fr_prev = frp3;
3010 break;
3011 }
3012 }
3013 if (topframe->fr_child == frp)
3014 topframe->fr_child = frp2;
3015 vim_free(frp);
3016 }
3017 }
3018
3019 return wp;
3020 }
3021
3022 /*
3023 * Return a pointer to the frame that will receive the empty screen space that
3024 * is left over after "win" is closed.
3025 *
3026 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
3027 * by default. Otherwise, the free space goes below or to the right. The
3028 * result is that opening a window and then immediately closing it will
3029 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
3030 * respected when possible.
3031 */
3032 static frame_T *
win_altframe(win_T * win,tabpage_T * tp)3033 win_altframe(
3034 win_T *win,
3035 tabpage_T *tp) // tab page "win" is in, NULL for current
3036 {
3037 frame_T *frp;
3038 frame_T *other_fr, *target_fr;
3039
3040 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
3041 return alt_tabpage()->tp_curwin->w_frame;
3042
3043 frp = win->w_frame;
3044
3045 if (frp->fr_prev == NULL)
3046 return frp->fr_next;
3047 if (frp->fr_next == NULL)
3048 return frp->fr_prev;
3049
3050 // By default the next window will get the space that was abandoned by this
3051 // window
3052 target_fr = frp->fr_next;
3053 other_fr = frp->fr_prev;
3054
3055 // If this is part of a column of windows and 'splitbelow' is true then the
3056 // previous window will get the space.
3057 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_COL && p_sb)
3058 {
3059 target_fr = frp->fr_prev;
3060 other_fr = frp->fr_next;
3061 }
3062
3063 // If this is part of a row of windows, and 'splitright' is true then the
3064 // previous window will get the space.
3065 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW && p_spr)
3066 {
3067 target_fr = frp->fr_prev;
3068 other_fr = frp->fr_next;
3069 }
3070
3071 // If 'wfh' or 'wfw' is set for the target and not for the alternate
3072 // window, reverse the selection.
3073 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
3074 {
3075 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
3076 target_fr = other_fr;
3077 }
3078 else
3079 {
3080 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
3081 target_fr = other_fr;
3082 }
3083
3084 return target_fr;
3085 }
3086
3087 /*
3088 * Return the tabpage that will be used if the current one is closed.
3089 */
3090 static tabpage_T *
alt_tabpage(void)3091 alt_tabpage(void)
3092 {
3093 tabpage_T *tp;
3094
3095 // Use the next tab page if possible.
3096 if (curtab->tp_next != NULL)
3097 return curtab->tp_next;
3098
3099 // Find the last but one tab page.
3100 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
3101 ;
3102 return tp;
3103 }
3104
3105 /*
3106 * Find the left-upper window in frame "frp".
3107 */
3108 static win_T *
frame2win(frame_T * frp)3109 frame2win(frame_T *frp)
3110 {
3111 while (frp->fr_win == NULL)
3112 frp = frp->fr_child;
3113 return frp->fr_win;
3114 }
3115
3116 /*
3117 * Return TRUE if frame "frp" contains window "wp".
3118 */
3119 static int
frame_has_win(frame_T * frp,win_T * wp)3120 frame_has_win(frame_T *frp, win_T *wp)
3121 {
3122 frame_T *p;
3123
3124 if (frp->fr_layout == FR_LEAF)
3125 return frp->fr_win == wp;
3126
3127 FOR_ALL_FRAMES(p, frp->fr_child)
3128 if (frame_has_win(p, wp))
3129 return TRUE;
3130 return FALSE;
3131 }
3132
3133 /*
3134 * Set a new height for a frame. Recursively sets the height for contained
3135 * frames and windows. Caller must take care of positions.
3136 */
3137 static void
frame_new_height(frame_T * topfrp,int height,int topfirst,int wfh)3138 frame_new_height(
3139 frame_T *topfrp,
3140 int height,
3141 int topfirst, // resize topmost contained frame first
3142 int wfh) // obey 'winfixheight' when there is a choice;
3143 // may cause the height not to be set
3144 {
3145 frame_T *frp;
3146 int extra_lines;
3147 int h;
3148
3149 if (topfrp->fr_win != NULL)
3150 {
3151 // Simple case: just one window.
3152 win_new_height(topfrp->fr_win,
3153 height - topfrp->fr_win->w_status_height
3154 - WINBAR_HEIGHT(topfrp->fr_win));
3155 }
3156 else if (topfrp->fr_layout == FR_ROW)
3157 {
3158 do
3159 {
3160 // All frames in this row get the same new height.
3161 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3162 {
3163 frame_new_height(frp, height, topfirst, wfh);
3164 if (frp->fr_height > height)
3165 {
3166 // Could not fit the windows, make the whole row higher.
3167 height = frp->fr_height;
3168 break;
3169 }
3170 }
3171 }
3172 while (frp != NULL);
3173 }
3174 else // fr_layout == FR_COL
3175 {
3176 // Complicated case: Resize a column of frames. Resize the bottom
3177 // frame first, frames above that when needed.
3178
3179 frp = topfrp->fr_child;
3180 if (wfh)
3181 // Advance past frames with one window with 'wfh' set.
3182 while (frame_fixed_height(frp))
3183 {
3184 frp = frp->fr_next;
3185 if (frp == NULL)
3186 return; // no frame without 'wfh', give up
3187 }
3188 if (!topfirst)
3189 {
3190 // Find the bottom frame of this column
3191 while (frp->fr_next != NULL)
3192 frp = frp->fr_next;
3193 if (wfh)
3194 // Advance back for frames with one window with 'wfh' set.
3195 while (frame_fixed_height(frp))
3196 frp = frp->fr_prev;
3197 }
3198
3199 extra_lines = height - topfrp->fr_height;
3200 if (extra_lines < 0)
3201 {
3202 // reduce height of contained frames, bottom or top frame first
3203 while (frp != NULL)
3204 {
3205 h = frame_minheight(frp, NULL);
3206 if (frp->fr_height + extra_lines < h)
3207 {
3208 extra_lines += frp->fr_height - h;
3209 frame_new_height(frp, h, topfirst, wfh);
3210 }
3211 else
3212 {
3213 frame_new_height(frp, frp->fr_height + extra_lines,
3214 topfirst, wfh);
3215 break;
3216 }
3217 if (topfirst)
3218 {
3219 do
3220 frp = frp->fr_next;
3221 while (wfh && frp != NULL && frame_fixed_height(frp));
3222 }
3223 else
3224 {
3225 do
3226 frp = frp->fr_prev;
3227 while (wfh && frp != NULL && frame_fixed_height(frp));
3228 }
3229 // Increase "height" if we could not reduce enough frames.
3230 if (frp == NULL)
3231 height -= extra_lines;
3232 }
3233 }
3234 else if (extra_lines > 0)
3235 {
3236 // increase height of bottom or top frame
3237 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3238 }
3239 }
3240 topfrp->fr_height = height;
3241 }
3242
3243 /*
3244 * Return TRUE if height of frame "frp" should not be changed because of
3245 * the 'winfixheight' option.
3246 */
3247 static int
frame_fixed_height(frame_T * frp)3248 frame_fixed_height(frame_T *frp)
3249 {
3250 // frame with one window: fixed height if 'winfixheight' set.
3251 if (frp->fr_win != NULL)
3252 return frp->fr_win->w_p_wfh;
3253
3254 if (frp->fr_layout == FR_ROW)
3255 {
3256 // The frame is fixed height if one of the frames in the row is fixed
3257 // height.
3258 FOR_ALL_FRAMES(frp, frp->fr_child)
3259 if (frame_fixed_height(frp))
3260 return TRUE;
3261 return FALSE;
3262 }
3263
3264 // frp->fr_layout == FR_COL: The frame is fixed height if all of the
3265 // frames in the row are fixed height.
3266 FOR_ALL_FRAMES(frp, frp->fr_child)
3267 if (!frame_fixed_height(frp))
3268 return FALSE;
3269 return TRUE;
3270 }
3271
3272 /*
3273 * Return TRUE if width of frame "frp" should not be changed because of
3274 * the 'winfixwidth' option.
3275 */
3276 static int
frame_fixed_width(frame_T * frp)3277 frame_fixed_width(frame_T *frp)
3278 {
3279 // frame with one window: fixed width if 'winfixwidth' set.
3280 if (frp->fr_win != NULL)
3281 return frp->fr_win->w_p_wfw;
3282
3283 if (frp->fr_layout == FR_COL)
3284 {
3285 // The frame is fixed width if one of the frames in the row is fixed
3286 // width.
3287 FOR_ALL_FRAMES(frp, frp->fr_child)
3288 if (frame_fixed_width(frp))
3289 return TRUE;
3290 return FALSE;
3291 }
3292
3293 // frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3294 // frames in the row are fixed width.
3295 FOR_ALL_FRAMES(frp, frp->fr_child)
3296 if (!frame_fixed_width(frp))
3297 return FALSE;
3298 return TRUE;
3299 }
3300
3301 /*
3302 * Add a status line to windows at the bottom of "frp".
3303 * Note: Does not check if there is room!
3304 */
3305 static void
frame_add_statusline(frame_T * frp)3306 frame_add_statusline(frame_T *frp)
3307 {
3308 win_T *wp;
3309
3310 if (frp->fr_layout == FR_LEAF)
3311 {
3312 wp = frp->fr_win;
3313 if (wp->w_status_height == 0)
3314 {
3315 if (wp->w_height > 0) // don't make it negative
3316 --wp->w_height;
3317 wp->w_status_height = STATUS_HEIGHT;
3318 }
3319 }
3320 else if (frp->fr_layout == FR_ROW)
3321 {
3322 // Handle all the frames in the row.
3323 FOR_ALL_FRAMES(frp, frp->fr_child)
3324 frame_add_statusline(frp);
3325 }
3326 else // frp->fr_layout == FR_COL
3327 {
3328 // Only need to handle the last frame in the column.
3329 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3330 ;
3331 frame_add_statusline(frp);
3332 }
3333 }
3334
3335 /*
3336 * Set width of a frame. Handles recursively going through contained frames.
3337 * May remove separator line for windows at the right side (for win_close()).
3338 */
3339 static void
frame_new_width(frame_T * topfrp,int width,int leftfirst,int wfw)3340 frame_new_width(
3341 frame_T *topfrp,
3342 int width,
3343 int leftfirst, // resize leftmost contained frame first
3344 int wfw) // obey 'winfixwidth' when there is a choice;
3345 // may cause the width not to be set
3346 {
3347 frame_T *frp;
3348 int extra_cols;
3349 int w;
3350 win_T *wp;
3351
3352 if (topfrp->fr_layout == FR_LEAF)
3353 {
3354 // Simple case: just one window.
3355 wp = topfrp->fr_win;
3356 // Find out if there are any windows right of this one.
3357 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3358 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3359 break;
3360 if (frp->fr_parent == NULL)
3361 wp->w_vsep_width = 0;
3362 win_new_width(wp, width - wp->w_vsep_width);
3363 }
3364 else if (topfrp->fr_layout == FR_COL)
3365 {
3366 do
3367 {
3368 // All frames in this column get the same new width.
3369 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3370 {
3371 frame_new_width(frp, width, leftfirst, wfw);
3372 if (frp->fr_width > width)
3373 {
3374 // Could not fit the windows, make whole column wider.
3375 width = frp->fr_width;
3376 break;
3377 }
3378 }
3379 } while (frp != NULL);
3380 }
3381 else // fr_layout == FR_ROW
3382 {
3383 // Complicated case: Resize a row of frames. Resize the rightmost
3384 // frame first, frames left of it when needed.
3385
3386 frp = topfrp->fr_child;
3387 if (wfw)
3388 // Advance past frames with one window with 'wfw' set.
3389 while (frame_fixed_width(frp))
3390 {
3391 frp = frp->fr_next;
3392 if (frp == NULL)
3393 return; // no frame without 'wfw', give up
3394 }
3395 if (!leftfirst)
3396 {
3397 // Find the rightmost frame of this row
3398 while (frp->fr_next != NULL)
3399 frp = frp->fr_next;
3400 if (wfw)
3401 // Advance back for frames with one window with 'wfw' set.
3402 while (frame_fixed_width(frp))
3403 frp = frp->fr_prev;
3404 }
3405
3406 extra_cols = width - topfrp->fr_width;
3407 if (extra_cols < 0)
3408 {
3409 // reduce frame width, rightmost frame first
3410 while (frp != NULL)
3411 {
3412 w = frame_minwidth(frp, NULL);
3413 if (frp->fr_width + extra_cols < w)
3414 {
3415 extra_cols += frp->fr_width - w;
3416 frame_new_width(frp, w, leftfirst, wfw);
3417 }
3418 else
3419 {
3420 frame_new_width(frp, frp->fr_width + extra_cols,
3421 leftfirst, wfw);
3422 break;
3423 }
3424 if (leftfirst)
3425 {
3426 do
3427 frp = frp->fr_next;
3428 while (wfw && frp != NULL && frame_fixed_width(frp));
3429 }
3430 else
3431 {
3432 do
3433 frp = frp->fr_prev;
3434 while (wfw && frp != NULL && frame_fixed_width(frp));
3435 }
3436 // Increase "width" if we could not reduce enough frames.
3437 if (frp == NULL)
3438 width -= extra_cols;
3439 }
3440 }
3441 else if (extra_cols > 0)
3442 {
3443 // increase width of rightmost frame
3444 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
3445 }
3446 }
3447 topfrp->fr_width = width;
3448 }
3449
3450 /*
3451 * Add the vertical separator to windows at the right side of "frp".
3452 * Note: Does not check if there is room!
3453 */
3454 static void
frame_add_vsep(frame_T * frp)3455 frame_add_vsep(frame_T *frp)
3456 {
3457 win_T *wp;
3458
3459 if (frp->fr_layout == FR_LEAF)
3460 {
3461 wp = frp->fr_win;
3462 if (wp->w_vsep_width == 0)
3463 {
3464 if (wp->w_width > 0) // don't make it negative
3465 --wp->w_width;
3466 wp->w_vsep_width = 1;
3467 }
3468 }
3469 else if (frp->fr_layout == FR_COL)
3470 {
3471 // Handle all the frames in the column.
3472 FOR_ALL_FRAMES(frp, frp->fr_child)
3473 frame_add_vsep(frp);
3474 }
3475 else // frp->fr_layout == FR_ROW
3476 {
3477 // Only need to handle the last frame in the row.
3478 frp = frp->fr_child;
3479 while (frp->fr_next != NULL)
3480 frp = frp->fr_next;
3481 frame_add_vsep(frp);
3482 }
3483 }
3484
3485 /*
3486 * Set frame width from the window it contains.
3487 */
3488 static void
frame_fix_width(win_T * wp)3489 frame_fix_width(win_T *wp)
3490 {
3491 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3492 }
3493
3494 /*
3495 * Set frame height from the window it contains.
3496 */
3497 static void
frame_fix_height(win_T * wp)3498 frame_fix_height(win_T *wp)
3499 {
3500 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
3501 }
3502
3503 /*
3504 * Compute the minimal height for frame "topfrp".
3505 * Uses the 'winminheight' option.
3506 * When "next_curwin" isn't NULL, use p_wh for this window.
3507 * When "next_curwin" is NOWIN, don't use at least one line for the current
3508 * window.
3509 */
3510 static int
frame_minheight(frame_T * topfrp,win_T * next_curwin)3511 frame_minheight(frame_T *topfrp, win_T *next_curwin)
3512 {
3513 frame_T *frp;
3514 int m;
3515 int n;
3516
3517 if (topfrp->fr_win != NULL)
3518 {
3519 if (topfrp->fr_win == next_curwin)
3520 m = p_wh + topfrp->fr_win->w_status_height;
3521 else
3522 {
3523 // window: minimal height of the window plus status line
3524 m = p_wmh + topfrp->fr_win->w_status_height;
3525 if (topfrp->fr_win == curwin && next_curwin == NULL)
3526 {
3527 // Current window is minimal one line high and WinBar is
3528 // visible.
3529 if (p_wmh == 0)
3530 ++m;
3531 m += WINBAR_HEIGHT(curwin);
3532 }
3533 }
3534 }
3535 else if (topfrp->fr_layout == FR_ROW)
3536 {
3537 // get the minimal height from each frame in this row
3538 m = 0;
3539 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3540 {
3541 n = frame_minheight(frp, next_curwin);
3542 if (n > m)
3543 m = n;
3544 }
3545 }
3546 else
3547 {
3548 // Add up the minimal heights for all frames in this column.
3549 m = 0;
3550 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3551 m += frame_minheight(frp, next_curwin);
3552 }
3553
3554 return m;
3555 }
3556
3557 /*
3558 * Compute the minimal width for frame "topfrp".
3559 * When "next_curwin" isn't NULL, use p_wiw for this window.
3560 * When "next_curwin" is NOWIN, don't use at least one column for the current
3561 * window.
3562 */
3563 static int
frame_minwidth(frame_T * topfrp,win_T * next_curwin)3564 frame_minwidth(
3565 frame_T *topfrp,
3566 win_T *next_curwin) // use p_wh and p_wiw for next_curwin
3567 {
3568 frame_T *frp;
3569 int m, n;
3570
3571 if (topfrp->fr_win != NULL)
3572 {
3573 if (topfrp->fr_win == next_curwin)
3574 m = p_wiw + topfrp->fr_win->w_vsep_width;
3575 else
3576 {
3577 // window: minimal width of the window plus separator column
3578 m = p_wmw + topfrp->fr_win->w_vsep_width;
3579 // Current window is minimal one column wide
3580 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3581 ++m;
3582 }
3583 }
3584 else if (topfrp->fr_layout == FR_COL)
3585 {
3586 // get the minimal width from each frame in this column
3587 m = 0;
3588 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3589 {
3590 n = frame_minwidth(frp, next_curwin);
3591 if (n > m)
3592 m = n;
3593 }
3594 }
3595 else
3596 {
3597 // Add up the minimal widths for all frames in this row.
3598 m = 0;
3599 FOR_ALL_FRAMES(frp, topfrp->fr_child)
3600 m += frame_minwidth(frp, next_curwin);
3601 }
3602
3603 return m;
3604 }
3605
3606
3607 /*
3608 * Try to close all windows except current one.
3609 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3610 * used and the buffer was modified.
3611 *
3612 * Used by ":bdel" and ":only".
3613 */
3614 void
close_others(int message,int forceit)3615 close_others(
3616 int message,
3617 int forceit) // always hide all other windows
3618 {
3619 win_T *wp;
3620 win_T *nextwp;
3621 int r;
3622
3623 if (one_window())
3624 {
3625 if (message && !autocmd_busy)
3626 msg(_(m_onlyone));
3627 return;
3628 }
3629
3630 // Be very careful here: autocommands may change the window layout.
3631 for (wp = firstwin; win_valid(wp); wp = nextwp)
3632 {
3633 nextwp = wp->w_next;
3634 if (wp != curwin) // don't close current window
3635 {
3636
3637 // Check if it's allowed to abandon this window
3638 r = can_abandon(wp->w_buffer, forceit);
3639 if (!win_valid(wp)) // autocommands messed wp up
3640 {
3641 nextwp = firstwin;
3642 continue;
3643 }
3644 if (!r)
3645 {
3646 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3647 if (message && (p_confirm
3648 || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
3649 {
3650 dialog_changed(wp->w_buffer, FALSE);
3651 if (!win_valid(wp)) // autocommands messed wp up
3652 {
3653 nextwp = firstwin;
3654 continue;
3655 }
3656 }
3657 if (bufIsChanged(wp->w_buffer))
3658 #endif
3659 continue;
3660 }
3661 win_close(wp, !buf_hide(wp->w_buffer)
3662 && !bufIsChanged(wp->w_buffer));
3663 }
3664 }
3665
3666 if (message && !ONE_WINDOW)
3667 emsg(_("E445: Other window contains changes"));
3668 }
3669
3670 static void
win_init_empty(win_T * wp)3671 win_init_empty(win_T *wp)
3672 {
3673 redraw_win_later(wp, NOT_VALID);
3674 wp->w_lines_valid = 0;
3675 wp->w_cursor.lnum = 1;
3676 wp->w_curswant = wp->w_cursor.col = 0;
3677 wp->w_cursor.coladd = 0;
3678 wp->w_pcmark.lnum = 1; // pcmark not cleared but set to line 1
3679 wp->w_pcmark.col = 0;
3680 wp->w_prev_pcmark.lnum = 0;
3681 wp->w_prev_pcmark.col = 0;
3682 wp->w_topline = 1;
3683 #ifdef FEAT_DIFF
3684 wp->w_topfill = 0;
3685 #endif
3686 wp->w_botline = 2;
3687 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
3688 wp->w_s = &wp->w_buffer->b_s;
3689 #endif
3690 #ifdef FEAT_TERMINAL
3691 term_reset_wincolor(wp);
3692 #endif
3693 }
3694
3695 /*
3696 * Init the current window "curwin".
3697 * Called when a new file is being edited.
3698 */
3699 void
curwin_init(void)3700 curwin_init(void)
3701 {
3702 win_init_empty(curwin);
3703 }
3704
3705 /*
3706 * Allocate the first window and put an empty buffer in it.
3707 * Called from main().
3708 * Return FAIL when something goes wrong (out of memory).
3709 */
3710 int
win_alloc_first(void)3711 win_alloc_first(void)
3712 {
3713 if (win_alloc_firstwin(NULL) == FAIL)
3714 return FAIL;
3715
3716 first_tabpage = alloc_tabpage();
3717 if (first_tabpage == NULL)
3718 return FAIL;
3719 first_tabpage->tp_topframe = topframe;
3720 curtab = first_tabpage;
3721 curtab->tp_firstwin = firstwin;
3722 curtab->tp_lastwin = lastwin;
3723 curtab->tp_curwin = curwin;
3724
3725 return OK;
3726 }
3727
3728 /*
3729 * Allocate and init a window that is not a regular window.
3730 * This can only be done after the first window is fully initialized, thus it
3731 * can't be in win_alloc_first().
3732 */
3733 win_T *
win_alloc_popup_win(void)3734 win_alloc_popup_win(void)
3735 {
3736 win_T *wp;
3737
3738 wp = win_alloc(NULL, TRUE);
3739 if (wp != NULL)
3740 {
3741 // We need to initialize options with something, using the current
3742 // window makes most sense.
3743 win_init_some(wp, curwin);
3744
3745 RESET_BINDING(wp);
3746 new_frame(wp);
3747 }
3748 return wp;
3749 }
3750
3751 /*
3752 * Initialize window "wp" to display buffer "buf".
3753 */
3754 void
win_init_popup_win(win_T * wp,buf_T * buf)3755 win_init_popup_win(win_T *wp, buf_T *buf)
3756 {
3757 wp->w_buffer = buf;
3758 ++buf->b_nwindows;
3759 win_init_empty(wp); // set cursor and topline to safe values
3760
3761 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3762 // win_enter_ext().
3763 VIM_CLEAR(wp->w_localdir);
3764 }
3765
3766 /*
3767 * Allocate the first window or the first window in a new tab page.
3768 * When "oldwin" is NULL create an empty buffer for it.
3769 * When "oldwin" is not NULL copy info from it to the new window.
3770 * Return FAIL when something goes wrong (out of memory).
3771 */
3772 static int
win_alloc_firstwin(win_T * oldwin)3773 win_alloc_firstwin(win_T *oldwin)
3774 {
3775 curwin = win_alloc(NULL, FALSE);
3776 if (oldwin == NULL)
3777 {
3778 // Very first window, need to create an empty buffer for it and
3779 // initialize from scratch.
3780 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3781 if (curwin == NULL || curbuf == NULL)
3782 return FAIL;
3783 curwin->w_buffer = curbuf;
3784 #ifdef FEAT_SYN_HL
3785 curwin->w_s = &(curbuf->b_s);
3786 #endif
3787 curbuf->b_nwindows = 1; // there is one window
3788 curwin->w_alist = &global_alist;
3789 curwin_init(); // init current window
3790 }
3791 else
3792 {
3793 // First window in new tab page, initialize it from "oldwin".
3794 win_init(curwin, oldwin, 0);
3795
3796 // We don't want cursor- and scroll-binding in the first window.
3797 RESET_BINDING(curwin);
3798 }
3799
3800 new_frame(curwin);
3801 if (curwin->w_frame == NULL)
3802 return FAIL;
3803 topframe = curwin->w_frame;
3804 topframe->fr_width = Columns;
3805 topframe->fr_height = Rows - p_ch;
3806
3807 return OK;
3808 }
3809
3810 /*
3811 * Create a frame for window "wp".
3812 */
3813 static void
new_frame(win_T * wp)3814 new_frame(win_T *wp)
3815 {
3816 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
3817
3818 wp->w_frame = frp;
3819 if (frp != NULL)
3820 {
3821 frp->fr_layout = FR_LEAF;
3822 frp->fr_win = wp;
3823 }
3824 }
3825
3826 /*
3827 * Initialize the window and frame size to the maximum.
3828 */
3829 void
win_init_size(void)3830 win_init_size(void)
3831 {
3832 firstwin->w_height = ROWS_AVAIL;
3833 topframe->fr_height = ROWS_AVAIL;
3834 firstwin->w_width = Columns;
3835 topframe->fr_width = Columns;
3836 }
3837
3838 /*
3839 * Allocate a new tabpage_T and init the values.
3840 * Returns NULL when out of memory.
3841 */
3842 static tabpage_T *
alloc_tabpage(void)3843 alloc_tabpage(void)
3844 {
3845 tabpage_T *tp;
3846 # ifdef FEAT_GUI
3847 int i;
3848 # endif
3849
3850
3851 tp = ALLOC_CLEAR_ONE(tabpage_T);
3852 if (tp == NULL)
3853 return NULL;
3854
3855 # ifdef FEAT_EVAL
3856 // init t: variables
3857 tp->tp_vars = dict_alloc();
3858 if (tp->tp_vars == NULL)
3859 {
3860 vim_free(tp);
3861 return NULL;
3862 }
3863 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3864 # endif
3865
3866 # ifdef FEAT_GUI
3867 for (i = 0; i < 3; i++)
3868 tp->tp_prev_which_scrollbars[i] = -1;
3869 # endif
3870 # ifdef FEAT_DIFF
3871 tp->tp_diff_invalid = TRUE;
3872 # endif
3873 tp->tp_ch_used = p_ch;
3874
3875 return tp;
3876 }
3877
3878 void
free_tabpage(tabpage_T * tp)3879 free_tabpage(tabpage_T *tp)
3880 {
3881 int idx;
3882
3883 # ifdef FEAT_DIFF
3884 diff_clear(tp);
3885 # endif
3886 # ifdef FEAT_PROP_POPUP
3887 while (tp->tp_first_popupwin != NULL)
3888 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id, TRUE);
3889 #endif
3890 for (idx = 0; idx < SNAP_COUNT; ++idx)
3891 clear_snapshot(tp, idx);
3892 #ifdef FEAT_EVAL
3893 vars_clear(&tp->tp_vars->dv_hashtab); // free all t: variables
3894 hash_init(&tp->tp_vars->dv_hashtab);
3895 unref_var_dict(tp->tp_vars);
3896 #endif
3897
3898 if (tp == lastused_tabpage)
3899 lastused_tabpage = NULL;
3900
3901 vim_free(tp->tp_localdir);
3902 vim_free(tp->tp_prevdir);
3903
3904 #ifdef FEAT_PYTHON
3905 python_tabpage_free(tp);
3906 #endif
3907
3908 #ifdef FEAT_PYTHON3
3909 python3_tabpage_free(tp);
3910 #endif
3911
3912 vim_free(tp);
3913 }
3914
3915 /*
3916 * Create a new Tab page with one window.
3917 * It will edit the current buffer, like after ":split".
3918 * When "after" is 0 put it just after the current Tab page.
3919 * Otherwise put it just before tab page "after".
3920 * Return FAIL or OK.
3921 */
3922 int
win_new_tabpage(int after)3923 win_new_tabpage(int after)
3924 {
3925 tabpage_T *tp = curtab;
3926 tabpage_T *prev_tp = curtab;
3927 tabpage_T *newtp;
3928 int n;
3929
3930 newtp = alloc_tabpage();
3931 if (newtp == NULL)
3932 return FAIL;
3933
3934 // Remember the current windows in this Tab page.
3935 if (leave_tabpage(curbuf, TRUE) == FAIL)
3936 {
3937 vim_free(newtp);
3938 return FAIL;
3939 }
3940 curtab = newtp;
3941
3942 newtp->tp_localdir = (tp->tp_localdir == NULL)
3943 ? NULL : vim_strsave(tp->tp_localdir);
3944 // Create a new empty window.
3945 if (win_alloc_firstwin(tp->tp_curwin) == OK)
3946 {
3947 // Make the new Tab page the new topframe.
3948 if (after == 1)
3949 {
3950 // New tab page becomes the first one.
3951 newtp->tp_next = first_tabpage;
3952 first_tabpage = newtp;
3953 }
3954 else
3955 {
3956 if (after > 0)
3957 {
3958 // Put new tab page before tab page "after".
3959 n = 2;
3960 for (tp = first_tabpage; tp->tp_next != NULL
3961 && n < after; tp = tp->tp_next)
3962 ++n;
3963 }
3964 newtp->tp_next = tp->tp_next;
3965 tp->tp_next = newtp;
3966 }
3967 newtp->tp_firstwin = newtp->tp_lastwin = newtp->tp_curwin = curwin;
3968
3969 win_init_size();
3970 firstwin->w_winrow = tabline_height();
3971 win_comp_scroll(curwin);
3972
3973 newtp->tp_topframe = topframe;
3974 last_status(FALSE);
3975
3976 lastused_tabpage = prev_tp;
3977
3978 #if defined(FEAT_GUI)
3979 // When 'guioptions' includes 'L' or 'R' may have to remove or add
3980 // scrollbars. Have to update them anyway.
3981 gui_may_update_scrollbars();
3982 #endif
3983 #ifdef FEAT_JOB_CHANNEL
3984 entering_window(curwin);
3985 #endif
3986
3987 redraw_all_later(NOT_VALID);
3988 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
3989 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3990 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
3991 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3992 return OK;
3993 }
3994
3995 // Failed, get back the previous Tab page
3996 enter_tabpage(curtab, curbuf, TRUE, TRUE);
3997 return FAIL;
3998 }
3999
4000 /*
4001 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
4002 * like with ":split".
4003 * Returns OK if a new tab page was created, FAIL otherwise.
4004 */
4005 static int
may_open_tabpage(void)4006 may_open_tabpage(void)
4007 {
4008 int n = (cmdmod.cmod_tab == 0)
4009 ? postponed_split_tab : cmdmod.cmod_tab;
4010
4011 if (n != 0)
4012 {
4013 cmdmod.cmod_tab = 0; // reset it to avoid doing it twice
4014 postponed_split_tab = 0;
4015 return win_new_tabpage(n);
4016 }
4017 return FAIL;
4018 }
4019
4020 /*
4021 * Create up to "maxcount" tabpages with empty windows.
4022 * Returns the number of resulting tab pages.
4023 */
4024 int
make_tabpages(int maxcount)4025 make_tabpages(int maxcount)
4026 {
4027 int count = maxcount;
4028 int todo;
4029
4030 // Limit to 'tabpagemax' tabs.
4031 if (count > p_tpm)
4032 count = p_tpm;
4033
4034 /*
4035 * Don't execute autocommands while creating the tab pages. Must do that
4036 * when putting the buffers in the windows.
4037 */
4038 block_autocmds();
4039
4040 for (todo = count - 1; todo > 0; --todo)
4041 if (win_new_tabpage(0) == FAIL)
4042 break;
4043
4044 unblock_autocmds();
4045
4046 // return actual number of tab pages
4047 return (count - todo);
4048 }
4049
4050 /*
4051 * Return TRUE when "tpc" points to a valid tab page.
4052 */
4053 int
valid_tabpage(tabpage_T * tpc)4054 valid_tabpage(tabpage_T *tpc)
4055 {
4056 tabpage_T *tp;
4057
4058 FOR_ALL_TABPAGES(tp)
4059 if (tp == tpc)
4060 return TRUE;
4061 return FALSE;
4062 }
4063
4064 /*
4065 * Return TRUE when "tpc" points to a valid tab page and at least one window is
4066 * valid.
4067 */
4068 int
valid_tabpage_win(tabpage_T * tpc)4069 valid_tabpage_win(tabpage_T *tpc)
4070 {
4071 tabpage_T *tp;
4072 win_T *wp;
4073
4074 FOR_ALL_TABPAGES(tp)
4075 {
4076 if (tp == tpc)
4077 {
4078 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
4079 {
4080 if (win_valid_any_tab(wp))
4081 return TRUE;
4082 }
4083 return FALSE;
4084 }
4085 }
4086 // shouldn't happen
4087 return FALSE;
4088 }
4089
4090 /*
4091 * Close tabpage "tab", assuming it has no windows in it.
4092 * There must be another tabpage or this will crash.
4093 */
4094 void
close_tabpage(tabpage_T * tab)4095 close_tabpage(tabpage_T *tab)
4096 {
4097 tabpage_T *ptp;
4098
4099 if (tab == first_tabpage)
4100 {
4101 first_tabpage = tab->tp_next;
4102 ptp = first_tabpage;
4103 }
4104 else
4105 {
4106 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
4107 ptp = ptp->tp_next)
4108 ;
4109 assert(ptp != NULL);
4110 ptp->tp_next = tab->tp_next;
4111 }
4112
4113 goto_tabpage_tp(ptp, FALSE, FALSE);
4114 free_tabpage(tab);
4115 }
4116
4117 /*
4118 * Find tab page "n" (first one is 1). Returns NULL when not found.
4119 */
4120 tabpage_T *
find_tabpage(int n)4121 find_tabpage(int n)
4122 {
4123 tabpage_T *tp;
4124 int i = 1;
4125
4126 if (n == 0)
4127 return curtab;
4128
4129 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
4130 ++i;
4131 return tp;
4132 }
4133
4134 /*
4135 * Get index of tab page "tp". First one has index 1.
4136 * When not found returns number of tab pages plus one.
4137 */
4138 int
tabpage_index(tabpage_T * ftp)4139 tabpage_index(tabpage_T *ftp)
4140 {
4141 int i = 1;
4142 tabpage_T *tp;
4143
4144 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
4145 ++i;
4146 return i;
4147 }
4148
4149 /*
4150 * Prepare for leaving the current tab page.
4151 * When autocommands change "curtab" we don't leave the tab page and return
4152 * FAIL.
4153 * Careful: When OK is returned need to get a new tab page very very soon!
4154 */
4155 static int
leave_tabpage(buf_T * new_curbuf UNUSED,int trigger_leave_autocmds UNUSED)4156 leave_tabpage(
4157 buf_T *new_curbuf UNUSED, // what is going to be the new curbuf,
4158 // NULL if unknown
4159 int trigger_leave_autocmds UNUSED)
4160 {
4161 tabpage_T *tp = curtab;
4162
4163 #ifdef FEAT_JOB_CHANNEL
4164 leaving_window(curwin);
4165 #endif
4166 reset_VIsual_and_resel(); // stop Visual mode
4167 if (trigger_leave_autocmds)
4168 {
4169 if (new_curbuf != curbuf)
4170 {
4171 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4172 if (curtab != tp)
4173 return FAIL;
4174 }
4175 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4176 if (curtab != tp)
4177 return FAIL;
4178 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
4179 if (curtab != tp)
4180 return FAIL;
4181 }
4182 #if defined(FEAT_GUI)
4183 // Remove the scrollbars. They may be added back later.
4184 if (gui.in_use)
4185 gui_remove_scrollbars();
4186 #endif
4187 tp->tp_curwin = curwin;
4188 tp->tp_prevwin = prevwin;
4189 tp->tp_firstwin = firstwin;
4190 tp->tp_lastwin = lastwin;
4191 tp->tp_old_Rows = Rows;
4192 tp->tp_old_Columns = Columns;
4193 firstwin = NULL;
4194 lastwin = NULL;
4195 return OK;
4196 }
4197
4198 /*
4199 * Start using tab page "tp".
4200 * Only to be used after leave_tabpage() or freeing the current tab page.
4201 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4202 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
4203 */
4204 static void
enter_tabpage(tabpage_T * tp,buf_T * old_curbuf UNUSED,int trigger_enter_autocmds,int trigger_leave_autocmds)4205 enter_tabpage(
4206 tabpage_T *tp,
4207 buf_T *old_curbuf UNUSED,
4208 int trigger_enter_autocmds,
4209 int trigger_leave_autocmds)
4210 {
4211 int row;
4212 int old_off = tp->tp_firstwin->w_winrow;
4213 win_T *next_prevwin = tp->tp_prevwin;
4214 tabpage_T *last_tab = curtab;
4215
4216 curtab = tp;
4217 firstwin = tp->tp_firstwin;
4218 lastwin = tp->tp_lastwin;
4219 topframe = tp->tp_topframe;
4220
4221 // We would like doing the TabEnter event first, but we don't have a
4222 // valid current window yet, which may break some commands.
4223 // This triggers autocommands, thus may make "tp" invalid.
4224 (void)win_enter_ext(tp->tp_curwin, WEE_CURWIN_INVALID
4225 | (trigger_enter_autocmds ? WEE_TRIGGER_ENTER_AUTOCMDS : 0)
4226 | (trigger_leave_autocmds ? WEE_TRIGGER_LEAVE_AUTOCMDS : 0));
4227 prevwin = next_prevwin;
4228
4229 last_status(FALSE); // status line may appear or disappear
4230 row = win_comp_pos(); // recompute w_winrow for all windows
4231 #ifdef FEAT_DIFF
4232 diff_need_scrollbind = TRUE;
4233 #endif
4234
4235 // The tabpage line may have appeared or disappeared, may need to resize
4236 // the frames for that. When the Vim window was resized need to update
4237 // frame sizes too. Use the stored value of p_ch, so that it can be
4238 // different for each tab page.
4239 if (p_ch != curtab->tp_ch_used)
4240 clear_cmdline = TRUE;
4241 p_ch = curtab->tp_ch_used;
4242
4243 // When cmdheight is changed in a tab page with '<C-w>-', cmdline_row is
4244 // changed but p_ch and tp_ch_used are not changed. Thus we also need to
4245 // check cmdline_row.
4246 if ((row < cmdline_row) && (cmdline_row <= Rows - p_ch))
4247 clear_cmdline = TRUE;
4248
4249 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4250 #ifdef FEAT_GUI_TABLINE
4251 && !gui_use_tabline()
4252 #endif
4253 ))
4254 shell_new_rows();
4255 if (curtab->tp_old_Columns != Columns && starting == 0)
4256 shell_new_columns(); // update window widths
4257
4258 lastused_tabpage = last_tab;
4259
4260 #if defined(FEAT_GUI)
4261 // When 'guioptions' includes 'L' or 'R' may have to remove or add
4262 // scrollbars. Have to update them anyway.
4263 gui_may_update_scrollbars();
4264 #endif
4265
4266 // Apply autocommands after updating the display, when 'rows' and
4267 // 'columns' have been set correctly.
4268 if (trigger_enter_autocmds)
4269 {
4270 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4271 if (old_curbuf != curbuf)
4272 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4273 }
4274
4275 redraw_all_later(NOT_VALID);
4276 }
4277
4278 /*
4279 * Go to tab page "n". For ":tab N" and "Ngt".
4280 * When "n" is 9999 go to the last tab page.
4281 */
4282 void
goto_tabpage(int n)4283 goto_tabpage(int n)
4284 {
4285 tabpage_T *tp = NULL; // shut up compiler
4286 tabpage_T *ttp;
4287 int i;
4288
4289 if (text_locked())
4290 {
4291 // Not allowed when editing the command line.
4292 text_locked_msg();
4293 return;
4294 }
4295
4296 // If there is only one it can't work.
4297 if (first_tabpage->tp_next == NULL)
4298 {
4299 if (n > 1)
4300 beep_flush();
4301 return;
4302 }
4303
4304 if (n == 0)
4305 {
4306 // No count, go to next tab page, wrap around end.
4307 if (curtab->tp_next == NULL)
4308 tp = first_tabpage;
4309 else
4310 tp = curtab->tp_next;
4311 }
4312 else if (n < 0)
4313 {
4314 // "gT": go to previous tab page, wrap around end. "N gT" repeats
4315 // this N times.
4316 ttp = curtab;
4317 for (i = n; i < 0; ++i)
4318 {
4319 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4320 tp = tp->tp_next)
4321 ;
4322 ttp = tp;
4323 }
4324 }
4325 else if (n == 9999)
4326 {
4327 // Go to last tab page.
4328 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4329 ;
4330 }
4331 else
4332 {
4333 // Go to tab page "n".
4334 tp = find_tabpage(n);
4335 if (tp == NULL)
4336 {
4337 beep_flush();
4338 return;
4339 }
4340 }
4341
4342 goto_tabpage_tp(tp, TRUE, TRUE);
4343
4344 #ifdef FEAT_GUI_TABLINE
4345 if (gui_use_tabline())
4346 gui_mch_set_curtab(tabpage_index(curtab));
4347 #endif
4348 }
4349
4350 /*
4351 * Go to tabpage "tp".
4352 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4353 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
4354 * Note: doesn't update the GUI tab.
4355 */
4356 void
goto_tabpage_tp(tabpage_T * tp,int trigger_enter_autocmds,int trigger_leave_autocmds)4357 goto_tabpage_tp(
4358 tabpage_T *tp,
4359 int trigger_enter_autocmds,
4360 int trigger_leave_autocmds)
4361 {
4362 // Don't repeat a message in another tab page.
4363 set_keep_msg(NULL, 0);
4364
4365 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4366 trigger_leave_autocmds) == OK)
4367 {
4368 if (valid_tabpage(tp))
4369 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4370 trigger_leave_autocmds);
4371 else
4372 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4373 trigger_leave_autocmds);
4374 }
4375 }
4376
4377 /*
4378 * Go to the last accessed tab page, if there is one.
4379 * Return OK or FAIL
4380 */
4381 int
goto_tabpage_lastused(void)4382 goto_tabpage_lastused(void)
4383 {
4384 if (valid_tabpage(lastused_tabpage))
4385 {
4386 goto_tabpage_tp(lastused_tabpage, TRUE, TRUE);
4387 return OK;
4388 }
4389 return FAIL;
4390 }
4391
4392 /*
4393 * Enter window "wp" in tab page "tp".
4394 * Also updates the GUI tab.
4395 */
4396 void
goto_tabpage_win(tabpage_T * tp,win_T * wp)4397 goto_tabpage_win(tabpage_T *tp, win_T *wp)
4398 {
4399 goto_tabpage_tp(tp, TRUE, TRUE);
4400 if (curtab == tp && win_valid(wp))
4401 {
4402 win_enter(wp, TRUE);
4403 # ifdef FEAT_GUI_TABLINE
4404 if (gui_use_tabline())
4405 gui_mch_set_curtab(tabpage_index(curtab));
4406 # endif
4407 }
4408 }
4409
4410 /*
4411 * Move the current tab page to after tab page "nr".
4412 */
4413 void
tabpage_move(int nr)4414 tabpage_move(int nr)
4415 {
4416 int n = 1;
4417 tabpage_T *tp, *tp_dst;
4418
4419 if (first_tabpage->tp_next == NULL)
4420 return;
4421
4422 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4423 ++n;
4424
4425 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4426 && tp->tp_next == curtab))
4427 return;
4428
4429 tp_dst = tp;
4430
4431 // Remove the current tab page from the list of tab pages.
4432 if (curtab == first_tabpage)
4433 first_tabpage = curtab->tp_next;
4434 else
4435 {
4436 FOR_ALL_TABPAGES(tp)
4437 if (tp->tp_next == curtab)
4438 break;
4439 if (tp == NULL) // "cannot happen"
4440 return;
4441 tp->tp_next = curtab->tp_next;
4442 }
4443
4444 // Re-insert it at the specified position.
4445 if (nr <= 0)
4446 {
4447 curtab->tp_next = first_tabpage;
4448 first_tabpage = curtab;
4449 }
4450 else
4451 {
4452 curtab->tp_next = tp_dst->tp_next;
4453 tp_dst->tp_next = curtab;
4454 }
4455
4456 // Need to redraw the tabline. Tab page contents doesn't change.
4457 redraw_tabline = TRUE;
4458 }
4459
4460
4461 /*
4462 * Go to another window.
4463 * When jumping to another buffer, stop Visual mode. Do this before
4464 * changing windows so we can yank the selection into the '*' register.
4465 * When jumping to another window on the same buffer, adjust its cursor
4466 * position to keep the same Visual area.
4467 */
4468 void
win_goto(win_T * wp)4469 win_goto(win_T *wp)
4470 {
4471 #ifdef FEAT_CONCEAL
4472 win_T *owp = curwin;
4473 #endif
4474
4475 #ifdef FEAT_PROP_POPUP
4476 if (ERROR_IF_ANY_POPUP_WINDOW)
4477 return;
4478 if (popup_is_popup(wp))
4479 {
4480 emsg(_("E366: Not allowed to enter a popup window"));
4481 return;
4482 }
4483 #endif
4484 if (text_and_win_locked())
4485 {
4486 beep_flush();
4487 text_locked_msg();
4488 return;
4489 }
4490 if (curbuf_locked())
4491 return;
4492
4493 if (wp->w_buffer != curbuf)
4494 reset_VIsual_and_resel();
4495 else if (VIsual_active)
4496 wp->w_cursor = curwin->w_cursor;
4497
4498 #ifdef FEAT_GUI
4499 need_mouse_correct = TRUE;
4500 #endif
4501 win_enter(wp, TRUE);
4502
4503 #ifdef FEAT_CONCEAL
4504 // Conceal cursor line in previous window, unconceal in current window.
4505 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
4506 redrawWinline(owp, owp->w_cursor.lnum);
4507 if (curwin->w_p_cole > 0 && !msg_scrolled)
4508 need_cursor_line_redraw = TRUE;
4509 #endif
4510 }
4511
4512 #if defined(FEAT_PERL) || defined(PROTO)
4513 /*
4514 * Find window number "winnr" (counting top to bottom).
4515 */
4516 win_T *
win_find_nr(int winnr)4517 win_find_nr(int winnr)
4518 {
4519 win_T *wp;
4520
4521 FOR_ALL_WINDOWS(wp)
4522 if (--winnr == 0)
4523 break;
4524 return wp;
4525 }
4526 #endif
4527
4528 #if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
4529 /*
4530 * Find the tabpage for window "win".
4531 */
4532 tabpage_T *
win_find_tabpage(win_T * win)4533 win_find_tabpage(win_T *win)
4534 {
4535 win_T *wp;
4536 tabpage_T *tp;
4537
4538 FOR_ALL_TAB_WINDOWS(tp, wp)
4539 if (wp == win)
4540 return tp;
4541 return NULL;
4542 }
4543 #endif
4544
4545 /*
4546 * Get the above or below neighbor window of the specified window.
4547 * up - TRUE for the above neighbor
4548 * count - nth neighbor window
4549 * Returns the specified window if the neighbor is not found.
4550 */
4551 win_T *
win_vert_neighbor(tabpage_T * tp,win_T * wp,int up,long count)4552 win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
4553 {
4554 frame_T *fr;
4555 frame_T *nfr;
4556 frame_T *foundfr;
4557
4558 #ifdef FEAT_PROP_POPUP
4559 if (popup_is_popup(wp))
4560 // popups don't have neighbors.
4561 return NULL;
4562 #endif
4563 foundfr = wp->w_frame;
4564 while (count--)
4565 {
4566 /*
4567 * First go upwards in the tree of frames until we find a upwards or
4568 * downwards neighbor.
4569 */
4570 fr = foundfr;
4571 for (;;)
4572 {
4573 if (fr == tp->tp_topframe)
4574 goto end;
4575 if (up)
4576 nfr = fr->fr_prev;
4577 else
4578 nfr = fr->fr_next;
4579 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4580 break;
4581 fr = fr->fr_parent;
4582 }
4583
4584 /*
4585 * Now go downwards to find the bottom or top frame in it.
4586 */
4587 for (;;)
4588 {
4589 if (nfr->fr_layout == FR_LEAF)
4590 {
4591 foundfr = nfr;
4592 break;
4593 }
4594 fr = nfr->fr_child;
4595 if (nfr->fr_layout == FR_ROW)
4596 {
4597 // Find the frame at the cursor row.
4598 while (fr->fr_next != NULL
4599 && frame2win(fr)->w_wincol + fr->fr_width
4600 <= wp->w_wincol + wp->w_wcol)
4601 fr = fr->fr_next;
4602 }
4603 if (nfr->fr_layout == FR_COL && up)
4604 while (fr->fr_next != NULL)
4605 fr = fr->fr_next;
4606 nfr = fr;
4607 }
4608 }
4609 end:
4610 return foundfr != NULL ? foundfr->fr_win : NULL;
4611 }
4612
4613 /*
4614 * Move to window above or below "count" times.
4615 */
4616 static void
win_goto_ver(int up,long count)4617 win_goto_ver(
4618 int up, // TRUE to go to win above
4619 long count)
4620 {
4621 win_T *win;
4622
4623 #ifdef FEAT_PROP_POPUP
4624 if (ERROR_IF_TERM_POPUP_WINDOW)
4625 return;
4626 #endif
4627 win = win_vert_neighbor(curtab, curwin, up, count);
4628 if (win != NULL)
4629 win_goto(win);
4630 }
4631
4632 /*
4633 * Get the left or right neighbor window of the specified window.
4634 * left - TRUE for the left neighbor
4635 * count - nth neighbor window
4636 * Returns the specified window if the neighbor is not found.
4637 */
4638 win_T *
win_horz_neighbor(tabpage_T * tp,win_T * wp,int left,long count)4639 win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
4640 {
4641 frame_T *fr;
4642 frame_T *nfr;
4643 frame_T *foundfr;
4644
4645 #ifdef FEAT_PROP_POPUP
4646 if (popup_is_popup(wp))
4647 // popups don't have neighbors.
4648 return NULL;
4649 #endif
4650 foundfr = wp->w_frame;
4651 while (count--)
4652 {
4653 /*
4654 * First go upwards in the tree of frames until we find a left or
4655 * right neighbor.
4656 */
4657 fr = foundfr;
4658 for (;;)
4659 {
4660 if (fr == tp->tp_topframe)
4661 goto end;
4662 if (left)
4663 nfr = fr->fr_prev;
4664 else
4665 nfr = fr->fr_next;
4666 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4667 break;
4668 fr = fr->fr_parent;
4669 }
4670
4671 /*
4672 * Now go downwards to find the leftmost or rightmost frame in it.
4673 */
4674 for (;;)
4675 {
4676 if (nfr->fr_layout == FR_LEAF)
4677 {
4678 foundfr = nfr;
4679 break;
4680 }
4681 fr = nfr->fr_child;
4682 if (nfr->fr_layout == FR_COL)
4683 {
4684 // Find the frame at the cursor row.
4685 while (fr->fr_next != NULL
4686 && frame2win(fr)->w_winrow + fr->fr_height
4687 <= wp->w_winrow + wp->w_wrow)
4688 fr = fr->fr_next;
4689 }
4690 if (nfr->fr_layout == FR_ROW && left)
4691 while (fr->fr_next != NULL)
4692 fr = fr->fr_next;
4693 nfr = fr;
4694 }
4695 }
4696 end:
4697 return foundfr != NULL ? foundfr->fr_win : NULL;
4698 }
4699
4700 /*
4701 * Move to left or right window.
4702 */
4703 static void
win_goto_hor(int left,long count)4704 win_goto_hor(
4705 int left, // TRUE to go to left win
4706 long count)
4707 {
4708 win_T *win;
4709
4710 #ifdef FEAT_PROP_POPUP
4711 if (ERROR_IF_TERM_POPUP_WINDOW)
4712 return;
4713 #endif
4714 win = win_horz_neighbor(curtab, curwin, left, count);
4715 if (win != NULL)
4716 win_goto(win);
4717 }
4718
4719 /*
4720 * Make window "wp" the current window.
4721 */
4722 void
win_enter(win_T * wp,int undo_sync)4723 win_enter(win_T *wp, int undo_sync)
4724 {
4725 (void)win_enter_ext(wp, (undo_sync ? WEE_UNDO_SYNC : 0)
4726 | WEE_TRIGGER_ENTER_AUTOCMDS | WEE_TRIGGER_LEAVE_AUTOCMDS);
4727 }
4728
4729 /*
4730 * Make window "wp" the current window.
4731 * Can be called with "flags" containing WEE_CURWIN_INVALID, which means that
4732 * curwin has just been closed and isn't valid.
4733 * Returns TRUE when dont_parse_messages was decremented.
4734 */
4735 static int
win_enter_ext(win_T * wp,int flags)4736 win_enter_ext(win_T *wp, int flags)
4737 {
4738 int other_buffer = FALSE;
4739 int curwin_invalid = (flags & WEE_CURWIN_INVALID);
4740 int did_decrement = FALSE;
4741
4742 if (wp == curwin && !curwin_invalid) // nothing to do
4743 return FALSE;
4744
4745 #ifdef FEAT_JOB_CHANNEL
4746 if (!curwin_invalid)
4747 leaving_window(curwin);
4748 #endif
4749
4750 if (!curwin_invalid && (flags & WEE_TRIGGER_LEAVE_AUTOCMDS))
4751 {
4752 /*
4753 * Be careful: If autocommands delete the window, return now.
4754 */
4755 if (wp->w_buffer != curbuf)
4756 {
4757 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4758 other_buffer = TRUE;
4759 if (!win_valid(wp))
4760 return FALSE;
4761 }
4762 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4763 if (!win_valid(wp))
4764 return FALSE;
4765 #ifdef FEAT_EVAL
4766 // autocmds may abort script processing
4767 if (aborting())
4768 return FALSE;
4769 #endif
4770 }
4771
4772 // sync undo before leaving the current buffer
4773 if ((flags & WEE_UNDO_SYNC) && curbuf != wp->w_buffer)
4774 u_sync(FALSE);
4775
4776 // Might need to scroll the old window before switching, e.g., when the
4777 // cursor was moved.
4778 update_topline();
4779
4780 // may have to copy the buffer options when 'cpo' contains 'S'
4781 if (wp->w_buffer != curbuf)
4782 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4783 if (!curwin_invalid)
4784 {
4785 prevwin = curwin; // remember for CTRL-W p
4786 curwin->w_redr_status = TRUE;
4787 }
4788 curwin = wp;
4789 curbuf = wp->w_buffer;
4790 check_cursor();
4791 if (!virtual_active())
4792 curwin->w_cursor.coladd = 0;
4793 changed_line_abv_curs(); // assume cursor position needs updating
4794
4795 // Now it is OK to parse messages again, which may be needed in
4796 // autocommands.
4797 #ifdef MESSAGE_QUEUE
4798 if (flags & WEE_ALLOW_PARSE_MESSAGES)
4799 {
4800 --dont_parse_messages;
4801 did_decrement = TRUE;
4802 }
4803 #endif
4804
4805 fix_current_dir();
4806
4807 #ifdef FEAT_JOB_CHANNEL
4808 entering_window(curwin);
4809 #endif
4810 // Careful: autocommands may close the window and make "wp" invalid
4811 if (flags & WEE_TRIGGER_NEW_AUTOCMDS)
4812 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
4813 if (flags & WEE_TRIGGER_ENTER_AUTOCMDS)
4814 {
4815 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4816 if (other_buffer)
4817 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4818 }
4819
4820 #ifdef FEAT_TITLE
4821 maketitle();
4822 #endif
4823 curwin->w_redr_status = TRUE;
4824 #ifdef FEAT_TERMINAL
4825 if (bt_terminal(curwin->w_buffer))
4826 // terminal is likely in another mode
4827 redraw_mode = TRUE;
4828 #endif
4829 redraw_tabline = TRUE;
4830 if (restart_edit)
4831 redraw_later(VALID); // causes status line redraw
4832
4833 // set window height to desired minimal value
4834 if (curwin->w_height < p_wh && !curwin->w_p_wfh
4835 #ifdef FEAT_PROP_POPUP
4836 && !popup_is_popup(curwin)
4837 #endif
4838 )
4839 win_setheight((int)p_wh);
4840 else if (curwin->w_height == 0)
4841 win_setheight(1);
4842
4843 // set window width to desired minimal value
4844 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4845 win_setwidth((int)p_wiw);
4846
4847 setmouse(); // in case jumped to/from help buffer
4848
4849 // Change directories when the 'acd' option is set.
4850 DO_AUTOCHDIR;
4851
4852 return did_decrement;
4853 }
4854
4855 /*
4856 * Used after making another window the current one: change directory if
4857 * needed.
4858 */
4859 void
fix_current_dir(void)4860 fix_current_dir(void)
4861 {
4862 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
4863 {
4864 char_u *dirname;
4865
4866 // Window or tab has a local directory: Save current directory as
4867 // global directory (unless that was done already) and change to the
4868 // local directory.
4869 if (globaldir == NULL)
4870 {
4871 char_u cwd[MAXPATHL];
4872
4873 if (mch_dirname(cwd, MAXPATHL) == OK)
4874 globaldir = vim_strsave(cwd);
4875 }
4876 if (curwin->w_localdir != NULL)
4877 dirname = curwin->w_localdir;
4878 else
4879 dirname = curtab->tp_localdir;
4880
4881 if (mch_chdir((char *)dirname) == 0)
4882 {
4883 last_chdir_reason = NULL;
4884 shorten_fnames(TRUE);
4885 }
4886 }
4887 else if (globaldir != NULL)
4888 {
4889 // Window doesn't have a local directory and we are not in the global
4890 // directory: Change to the global directory.
4891 vim_ignored = mch_chdir((char *)globaldir);
4892 VIM_CLEAR(globaldir);
4893 last_chdir_reason = NULL;
4894 shorten_fnames(TRUE);
4895 }
4896 }
4897
4898 /*
4899 * Jump to the first open window that contains buffer "buf", if one exists.
4900 * Returns a pointer to the window found, otherwise NULL.
4901 */
4902 win_T *
buf_jump_open_win(buf_T * buf)4903 buf_jump_open_win(buf_T *buf)
4904 {
4905 win_T *wp = NULL;
4906
4907 if (curwin->w_buffer == buf)
4908 wp = curwin;
4909 else
4910 FOR_ALL_WINDOWS(wp)
4911 if (wp->w_buffer == buf)
4912 break;
4913 if (wp != NULL)
4914 win_enter(wp, FALSE);
4915 return wp;
4916 }
4917
4918 /*
4919 * Jump to the first open window in any tab page that contains buffer "buf",
4920 * if one exists.
4921 * Returns a pointer to the window found, otherwise NULL.
4922 */
4923 win_T *
buf_jump_open_tab(buf_T * buf)4924 buf_jump_open_tab(buf_T *buf)
4925 {
4926 win_T *wp = buf_jump_open_win(buf);
4927 tabpage_T *tp;
4928
4929 if (wp != NULL)
4930 return wp;
4931
4932 FOR_ALL_TABPAGES(tp)
4933 if (tp != curtab)
4934 {
4935 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
4936 if (wp->w_buffer == buf)
4937 break;
4938 if (wp != NULL)
4939 {
4940 goto_tabpage_win(tp, wp);
4941 if (curwin != wp)
4942 wp = NULL; // something went wrong
4943 break;
4944 }
4945 }
4946 return wp;
4947 }
4948
4949 static int last_win_id = LOWEST_WIN_ID - 1;
4950
4951 /*
4952 * Allocate a window structure and link it in the window list when "hidden" is
4953 * FALSE.
4954 */
4955 static win_T *
win_alloc(win_T * after UNUSED,int hidden UNUSED)4956 win_alloc(win_T *after UNUSED, int hidden UNUSED)
4957 {
4958 win_T *new_wp;
4959
4960 /*
4961 * allocate window structure and linesizes arrays
4962 */
4963 new_wp = ALLOC_CLEAR_ONE(win_T);
4964 if (new_wp == NULL)
4965 return NULL;
4966
4967 if (win_alloc_lines(new_wp) == FAIL)
4968 {
4969 vim_free(new_wp);
4970 return NULL;
4971 }
4972
4973 new_wp->w_id = ++last_win_id;
4974
4975 #ifdef FEAT_EVAL
4976 // init w: variables
4977 new_wp->w_vars = dict_alloc();
4978 if (new_wp->w_vars == NULL)
4979 {
4980 win_free_lsize(new_wp);
4981 vim_free(new_wp);
4982 return NULL;
4983 }
4984 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
4985 #endif
4986
4987 // Don't execute autocommands while the window is not properly
4988 // initialized yet. gui_create_scrollbar() may trigger a FocusGained
4989 // event.
4990 block_autocmds();
4991
4992 /*
4993 * link the window in the window list
4994 */
4995 if (!hidden)
4996 win_append(after, new_wp);
4997 new_wp->w_wincol = 0;
4998 new_wp->w_width = Columns;
4999
5000 // position the display and the cursor at the top of the file.
5001 new_wp->w_topline = 1;
5002 #ifdef FEAT_DIFF
5003 new_wp->w_topfill = 0;
5004 #endif
5005 new_wp->w_botline = 2;
5006 new_wp->w_cursor.lnum = 1;
5007 new_wp->w_scbind_pos = 1;
5008
5009 // use global option value for global-local options
5010 new_wp->w_p_so = -1;
5011 new_wp->w_p_siso = -1;
5012
5013 // We won't calculate w_fraction until resizing the window
5014 new_wp->w_fraction = 0;
5015 new_wp->w_prev_fraction_row = -1;
5016
5017 #ifdef FEAT_GUI
5018 if (gui.in_use)
5019 {
5020 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
5021 SBAR_LEFT, new_wp);
5022 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
5023 SBAR_RIGHT, new_wp);
5024 }
5025 #endif
5026 #ifdef FEAT_FOLDING
5027 foldInitWin(new_wp);
5028 #endif
5029 unblock_autocmds();
5030 #ifdef FEAT_SEARCH_EXTRA
5031 new_wp->w_match_head = NULL;
5032 new_wp->w_next_match_id = 4;
5033 #endif
5034 return new_wp;
5035 }
5036
5037 /*
5038 * Remove window 'wp' from the window list and free the structure.
5039 */
5040 static void
win_free(win_T * wp,tabpage_T * tp)5041 win_free(
5042 win_T *wp,
5043 tabpage_T *tp) // tab page "win" is in, NULL for current
5044 {
5045 int i;
5046 buf_T *buf;
5047 wininfo_T *wip;
5048
5049 #ifdef FEAT_FOLDING
5050 clearFolding(wp);
5051 #endif
5052
5053 // reduce the reference count to the argument list.
5054 alist_unlink(wp->w_alist);
5055
5056 // Don't execute autocommands while the window is halfway being deleted.
5057 // gui_mch_destroy_scrollbar() may trigger a FocusGained event.
5058 block_autocmds();
5059
5060 #ifdef FEAT_LUA
5061 lua_window_free(wp);
5062 #endif
5063
5064 #ifdef FEAT_MZSCHEME
5065 mzscheme_window_free(wp);
5066 #endif
5067
5068 #ifdef FEAT_PERL
5069 perl_win_free(wp);
5070 #endif
5071
5072 #ifdef FEAT_PYTHON
5073 python_window_free(wp);
5074 #endif
5075
5076 #ifdef FEAT_PYTHON3
5077 python3_window_free(wp);
5078 #endif
5079
5080 #ifdef FEAT_TCL
5081 tcl_window_free(wp);
5082 #endif
5083
5084 #ifdef FEAT_RUBY
5085 ruby_window_free(wp);
5086 #endif
5087
5088 clear_winopt(&wp->w_onebuf_opt);
5089 clear_winopt(&wp->w_allbuf_opt);
5090
5091 vim_free(wp->w_lcs_chars.multispace);
5092
5093 #ifdef FEAT_EVAL
5094 vars_clear(&wp->w_vars->dv_hashtab); // free all w: variables
5095 hash_init(&wp->w_vars->dv_hashtab);
5096 unref_var_dict(wp->w_vars);
5097 #endif
5098
5099 {
5100 tabpage_T *ttp;
5101
5102 if (prevwin == wp)
5103 prevwin = NULL;
5104 FOR_ALL_TABPAGES(ttp)
5105 if (ttp->tp_prevwin == wp)
5106 ttp->tp_prevwin = NULL;
5107 }
5108 win_free_lsize(wp);
5109
5110 for (i = 0; i < wp->w_tagstacklen; ++i)
5111 {
5112 vim_free(wp->w_tagstack[i].tagname);
5113 vim_free(wp->w_tagstack[i].user_data);
5114 }
5115 vim_free(wp->w_localdir);
5116 vim_free(wp->w_prevdir);
5117
5118 // Remove the window from the b_wininfo lists, it may happen that the
5119 // freed memory is re-used for another window.
5120 FOR_ALL_BUFFERS(buf)
5121 FOR_ALL_BUF_WININFO(buf, wip)
5122 if (wip->wi_win == wp)
5123 {
5124 wininfo_T *wip2;
5125
5126 // If there already is an entry with "wi_win" set to NULL it
5127 // must be removed, it would never be used.
5128 // Skip "wip" itself, otherwise Coverity complains.
5129 for (wip2 = buf->b_wininfo; wip2 != NULL; wip2 = wip2->wi_next)
5130 if (wip2 != wip && wip2->wi_win == NULL)
5131 {
5132 if (wip2->wi_next != NULL)
5133 wip2->wi_next->wi_prev = wip2->wi_prev;
5134 if (wip2->wi_prev == NULL)
5135 buf->b_wininfo = wip2->wi_next;
5136 else
5137 wip2->wi_prev->wi_next = wip2->wi_next;
5138 free_wininfo(wip2);
5139 break;
5140 }
5141
5142 wip->wi_win = NULL;
5143 }
5144
5145 #ifdef FEAT_SEARCH_EXTRA
5146 clear_matches(wp);
5147 #endif
5148
5149 #ifdef FEAT_JUMPLIST
5150 free_jumplist(wp);
5151 #endif
5152
5153 #ifdef FEAT_QUICKFIX
5154 qf_free_all(wp);
5155 #endif
5156
5157 #ifdef FEAT_GUI
5158 if (gui.in_use)
5159 {
5160 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
5161 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
5162 }
5163 #endif // FEAT_GUI
5164
5165 #ifdef FEAT_MENU
5166 remove_winbar(wp);
5167 #endif
5168 #ifdef FEAT_PROP_POPUP
5169 free_callback(&wp->w_close_cb);
5170 free_callback(&wp->w_filter_cb);
5171 for (i = 0; i < 4; ++i)
5172 VIM_CLEAR(wp->w_border_highlight[i]);
5173 vim_free(wp->w_scrollbar_highlight);
5174 vim_free(wp->w_thumb_highlight);
5175 vim_free(wp->w_popup_title);
5176 list_unref(wp->w_popup_mask);
5177 vim_free(wp->w_popup_mask_cells);
5178 #endif
5179
5180 #ifdef FEAT_SYN_HL
5181 vim_free(wp->w_p_cc_cols);
5182 #endif
5183
5184 if (win_valid_any_tab(wp))
5185 win_remove(wp, tp);
5186 if (autocmd_busy)
5187 {
5188 wp->w_next = au_pending_free_win;
5189 au_pending_free_win = wp;
5190 }
5191 else
5192 vim_free(wp);
5193
5194 unblock_autocmds();
5195 }
5196
5197 /*
5198 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
5199 * popup window.
5200 */
5201 static int
win_unlisted(win_T * wp)5202 win_unlisted(win_T *wp)
5203 {
5204 return wp == aucmd_win || WIN_IS_POPUP(wp);
5205 }
5206
5207 #if defined(FEAT_PROP_POPUP) || defined(PROTO)
5208 /*
5209 * Free a popup window. This does not take the window out of the window list
5210 * and assumes there is only one toplevel frame, no split.
5211 */
5212 void
win_free_popup(win_T * win)5213 win_free_popup(win_T *win)
5214 {
5215 if (bt_popup(win->w_buffer))
5216 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
5217 else
5218 close_buffer(win, win->w_buffer, 0, FALSE, FALSE);
5219 # if defined(FEAT_TIMERS)
5220 if (win->w_popup_timer != NULL)
5221 stop_timer(win->w_popup_timer);
5222 # endif
5223 vim_free(win->w_frame);
5224 win_free(win, NULL);
5225 }
5226 #endif
5227
5228 /*
5229 * Append window "wp" in the window list after window "after".
5230 */
5231 static void
win_append(win_T * after,win_T * wp)5232 win_append(win_T *after, win_T *wp)
5233 {
5234 win_T *before;
5235
5236 if (after == NULL) // after NULL is in front of the first
5237 before = firstwin;
5238 else
5239 before = after->w_next;
5240
5241 wp->w_next = before;
5242 wp->w_prev = after;
5243 if (after == NULL)
5244 firstwin = wp;
5245 else
5246 after->w_next = wp;
5247 if (before == NULL)
5248 lastwin = wp;
5249 else
5250 before->w_prev = wp;
5251 }
5252
5253 /*
5254 * Remove a window from the window list.
5255 */
5256 void
win_remove(win_T * wp,tabpage_T * tp)5257 win_remove(
5258 win_T *wp,
5259 tabpage_T *tp) // tab page "win" is in, NULL for current
5260 {
5261 if (wp->w_prev != NULL)
5262 wp->w_prev->w_next = wp->w_next;
5263 else if (tp == NULL)
5264 firstwin = curtab->tp_firstwin = wp->w_next;
5265 else
5266 tp->tp_firstwin = wp->w_next;
5267
5268 if (wp->w_next != NULL)
5269 wp->w_next->w_prev = wp->w_prev;
5270 else if (tp == NULL)
5271 lastwin = curtab->tp_lastwin = wp->w_prev;
5272 else
5273 tp->tp_lastwin = wp->w_prev;
5274 }
5275
5276 /*
5277 * Append frame "frp" in a frame list after frame "after".
5278 */
5279 static void
frame_append(frame_T * after,frame_T * frp)5280 frame_append(frame_T *after, frame_T *frp)
5281 {
5282 frp->fr_next = after->fr_next;
5283 after->fr_next = frp;
5284 if (frp->fr_next != NULL)
5285 frp->fr_next->fr_prev = frp;
5286 frp->fr_prev = after;
5287 }
5288
5289 /*
5290 * Insert frame "frp" in a frame list before frame "before".
5291 */
5292 static void
frame_insert(frame_T * before,frame_T * frp)5293 frame_insert(frame_T *before, frame_T *frp)
5294 {
5295 frp->fr_next = before;
5296 frp->fr_prev = before->fr_prev;
5297 before->fr_prev = frp;
5298 if (frp->fr_prev != NULL)
5299 frp->fr_prev->fr_next = frp;
5300 else
5301 frp->fr_parent->fr_child = frp;
5302 }
5303
5304 /*
5305 * Remove a frame from a frame list.
5306 */
5307 static void
frame_remove(frame_T * frp)5308 frame_remove(frame_T *frp)
5309 {
5310 if (frp->fr_prev != NULL)
5311 frp->fr_prev->fr_next = frp->fr_next;
5312 else
5313 frp->fr_parent->fr_child = frp->fr_next;
5314 if (frp->fr_next != NULL)
5315 frp->fr_next->fr_prev = frp->fr_prev;
5316 }
5317
5318 /*
5319 * Allocate w_lines[] for window "wp".
5320 * Return FAIL for failure, OK for success.
5321 */
5322 int
win_alloc_lines(win_T * wp)5323 win_alloc_lines(win_T *wp)
5324 {
5325 wp->w_lines_valid = 0;
5326 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
5327 if (wp->w_lines == NULL)
5328 return FAIL;
5329 return OK;
5330 }
5331
5332 /*
5333 * free lsize arrays for a window
5334 */
5335 void
win_free_lsize(win_T * wp)5336 win_free_lsize(win_T *wp)
5337 {
5338 // TODO: why would wp be NULL here?
5339 if (wp != NULL)
5340 VIM_CLEAR(wp->w_lines);
5341 }
5342
5343 /*
5344 * Called from win_new_shellsize() after Rows changed.
5345 * This only does the current tab page, others must be done when made active.
5346 */
5347 void
shell_new_rows(void)5348 shell_new_rows(void)
5349 {
5350 int h = (int)ROWS_AVAIL;
5351
5352 if (firstwin == NULL) // not initialized yet
5353 return;
5354 if (h < frame_minheight(topframe, NULL))
5355 h = frame_minheight(topframe, NULL);
5356
5357 // First try setting the heights of windows with 'winfixheight'. If
5358 // that doesn't result in the right height, forget about that option.
5359 frame_new_height(topframe, h, FALSE, TRUE);
5360 if (!frame_check_height(topframe, h))
5361 frame_new_height(topframe, h, FALSE, FALSE);
5362
5363 (void)win_comp_pos(); // recompute w_winrow and w_wincol
5364 compute_cmdrow();
5365 curtab->tp_ch_used = p_ch;
5366
5367 #if 0
5368 // Disabled: don't want making the screen smaller make a window larger.
5369 if (p_ea)
5370 win_equal(curwin, FALSE, 'v');
5371 #endif
5372 }
5373
5374 /*
5375 * Called from win_new_shellsize() after Columns changed.
5376 */
5377 void
shell_new_columns(void)5378 shell_new_columns(void)
5379 {
5380 if (firstwin == NULL) // not initialized yet
5381 return;
5382
5383 // First try setting the widths of windows with 'winfixwidth'. If that
5384 // doesn't result in the right width, forget about that option.
5385 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
5386 if (!frame_check_width(topframe, Columns))
5387 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5388
5389 (void)win_comp_pos(); // recompute w_winrow and w_wincol
5390 #if 0
5391 // Disabled: don't want making the screen smaller make a window larger.
5392 if (p_ea)
5393 win_equal(curwin, FALSE, 'h');
5394 #endif
5395 }
5396
5397 #if defined(FEAT_CMDWIN) || defined(PROTO)
5398 /*
5399 * Save the size of all windows in "gap".
5400 */
5401 void
win_size_save(garray_T * gap)5402 win_size_save(garray_T *gap)
5403
5404 {
5405 win_T *wp;
5406
5407 ga_init2(gap, (int)sizeof(int), 1);
5408 if (ga_grow(gap, win_count() * 2 + 1) == OK)
5409 {
5410 // first entry is value of 'lines'
5411 ((int *)gap->ga_data)[gap->ga_len++] = Rows;
5412
5413 FOR_ALL_WINDOWS(wp)
5414 {
5415 ((int *)gap->ga_data)[gap->ga_len++] =
5416 wp->w_width + wp->w_vsep_width;
5417 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5418 }
5419 }
5420 }
5421
5422 /*
5423 * Restore window sizes, but only if the number of windows is still the same
5424 * and 'lines' didn't change.
5425 * Does not free the growarray.
5426 */
5427 void
win_size_restore(garray_T * gap)5428 win_size_restore(garray_T *gap)
5429 {
5430 win_T *wp;
5431 int i, j;
5432
5433 if (win_count() * 2 + 1 == gap->ga_len
5434 && ((int *)gap->ga_data)[0] == Rows)
5435 {
5436 // The order matters, because frames contain other frames, but it's
5437 // difficult to get right. The easy way out is to do it twice.
5438 for (j = 0; j < 2; ++j)
5439 {
5440 i = 1;
5441 FOR_ALL_WINDOWS(wp)
5442 {
5443 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5444 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5445 }
5446 }
5447 // recompute the window positions
5448 (void)win_comp_pos();
5449 }
5450 }
5451 #endif // FEAT_CMDWIN
5452
5453 /*
5454 * Update the position for all windows, using the width and height of the
5455 * frames.
5456 * Returns the row just after the last window.
5457 */
5458 int
win_comp_pos(void)5459 win_comp_pos(void)
5460 {
5461 int row = tabline_height();
5462 int col = 0;
5463
5464 frame_comp_pos(topframe, &row, &col);
5465 return row;
5466 }
5467
5468 /*
5469 * Update the position of the windows in frame "topfrp", using the width and
5470 * height of the frames.
5471 * "*row" and "*col" are the top-left position of the frame. They are updated
5472 * to the bottom-right position plus one.
5473 */
5474 static void
frame_comp_pos(frame_T * topfrp,int * row,int * col)5475 frame_comp_pos(frame_T *topfrp, int *row, int *col)
5476 {
5477 win_T *wp;
5478 frame_T *frp;
5479 int startcol;
5480 int startrow;
5481 int h;
5482
5483 wp = topfrp->fr_win;
5484 if (wp != NULL)
5485 {
5486 if (wp->w_winrow != *row || wp->w_wincol != *col)
5487 {
5488 // position changed, redraw
5489 wp->w_winrow = *row;
5490 wp->w_wincol = *col;
5491 redraw_win_later(wp, NOT_VALID);
5492 wp->w_redr_status = TRUE;
5493 }
5494 // WinBar will not show if the window height is zero
5495 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5496 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
5497 *col += wp->w_width + wp->w_vsep_width;
5498 }
5499 else
5500 {
5501 startrow = *row;
5502 startcol = *col;
5503 FOR_ALL_FRAMES(frp, topfrp->fr_child)
5504 {
5505 if (topfrp->fr_layout == FR_ROW)
5506 *row = startrow; // all frames are at the same row
5507 else
5508 *col = startcol; // all frames are at the same col
5509 frame_comp_pos(frp, row, col);
5510 }
5511 }
5512 }
5513
5514 /*
5515 * Set current window height and take care of repositioning other windows to
5516 * fit around it.
5517 */
5518 void
win_setheight(int height)5519 win_setheight(int height)
5520 {
5521 win_setheight_win(height, curwin);
5522 }
5523
5524 /*
5525 * Set the window height of window "win" and take care of repositioning other
5526 * windows to fit around it.
5527 */
5528 void
win_setheight_win(int height,win_T * win)5529 win_setheight_win(int height, win_T *win)
5530 {
5531 int row;
5532
5533 if (win == curwin)
5534 {
5535 // Always keep current window at least one line high, even when
5536 // 'winminheight' is zero.
5537 if (height < p_wmh)
5538 height = p_wmh;
5539 if (height == 0)
5540 height = 1;
5541 height += WINBAR_HEIGHT(curwin);
5542 }
5543
5544 frame_setheight(win->w_frame, height + win->w_status_height);
5545
5546 // recompute the window positions
5547 row = win_comp_pos();
5548
5549 /*
5550 * If there is extra space created between the last window and the command
5551 * line, clear it.
5552 */
5553 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5554 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5555 cmdline_row = row;
5556 msg_row = row;
5557 msg_col = 0;
5558
5559 redraw_all_later(NOT_VALID);
5560 }
5561
5562 /*
5563 * Set the height of a frame to "height" and take care that all frames and
5564 * windows inside it are resized. Also resize frames on the left and right if
5565 * the are in the same FR_ROW frame.
5566 *
5567 * Strategy:
5568 * If the frame is part of a FR_COL frame, try fitting the frame in that
5569 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5570 * go to containing frames to resize them and make room.
5571 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5572 * Check for the minimal height of the FR_ROW frame.
5573 * At the top level we can also use change the command line height.
5574 */
5575 static void
frame_setheight(frame_T * curfrp,int height)5576 frame_setheight(frame_T *curfrp, int height)
5577 {
5578 int room; // total number of lines available
5579 int take; // number of lines taken from other windows
5580 int room_cmdline; // lines available from cmdline
5581 int run;
5582 frame_T *frp;
5583 int h;
5584 int room_reserved;
5585
5586 // If the height already is the desired value, nothing to do.
5587 if (curfrp->fr_height == height)
5588 return;
5589
5590 if (curfrp->fr_parent == NULL)
5591 {
5592 // topframe: can only change the command line
5593 if (height > ROWS_AVAIL)
5594 height = ROWS_AVAIL;
5595 if (height > 0)
5596 frame_new_height(curfrp, height, FALSE, FALSE);
5597 }
5598 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5599 {
5600 // Row of frames: Also need to resize frames left and right of this
5601 // one. First check for the minimal height of these.
5602 h = frame_minheight(curfrp->fr_parent, NULL);
5603 if (height < h)
5604 height = h;
5605 frame_setheight(curfrp->fr_parent, height);
5606 }
5607 else
5608 {
5609 /*
5610 * Column of frames: try to change only frames in this column.
5611 */
5612 /*
5613 * Do this twice:
5614 * 1: compute room available, if it's not enough try resizing the
5615 * containing frame.
5616 * 2: compute the room available and adjust the height to it.
5617 * Try not to reduce the height of a window with 'winfixheight' set.
5618 */
5619 for (run = 1; run <= 2; ++run)
5620 {
5621 room = 0;
5622 room_reserved = 0;
5623 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
5624 {
5625 if (frp != curfrp
5626 && frp->fr_win != NULL
5627 && frp->fr_win->w_p_wfh)
5628 room_reserved += frp->fr_height;
5629 room += frp->fr_height;
5630 if (frp != curfrp)
5631 room -= frame_minheight(frp, NULL);
5632 }
5633 if (curfrp->fr_width != Columns)
5634 room_cmdline = 0;
5635 else
5636 {
5637 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5638 + VISIBLE_HEIGHT(lastwin)
5639 + lastwin->w_status_height);
5640 if (room_cmdline < 0)
5641 room_cmdline = 0;
5642 }
5643
5644 if (height <= room + room_cmdline)
5645 break;
5646 if (run == 2 || curfrp->fr_width == Columns)
5647 {
5648 if (height > room + room_cmdline)
5649 height = room + room_cmdline;
5650 break;
5651 }
5652 frame_setheight(curfrp->fr_parent, height
5653 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5654 }
5655
5656 /*
5657 * Compute the number of lines we will take from others frames (can be
5658 * negative!).
5659 */
5660 take = height - curfrp->fr_height;
5661
5662 // If there is not enough room, also reduce the height of a window
5663 // with 'winfixheight' set.
5664 if (height > room + room_cmdline - room_reserved)
5665 room_reserved = room + room_cmdline - height;
5666 // If there is only a 'winfixheight' window and making the
5667 // window smaller, need to make the other window taller.
5668 if (take < 0 && room - curfrp->fr_height < room_reserved)
5669 room_reserved = 0;
5670
5671 if (take > 0 && room_cmdline > 0)
5672 {
5673 // use lines from cmdline first
5674 if (take < room_cmdline)
5675 room_cmdline = take;
5676 take -= room_cmdline;
5677 topframe->fr_height += room_cmdline;
5678 }
5679
5680 /*
5681 * set the current frame to the new height
5682 */
5683 frame_new_height(curfrp, height, FALSE, FALSE);
5684
5685 /*
5686 * First take lines from the frames after the current frame. If
5687 * that is not enough, takes lines from frames above the current
5688 * frame.
5689 */
5690 for (run = 0; run < 2; ++run)
5691 {
5692 if (run == 0)
5693 frp = curfrp->fr_next; // 1st run: start with next window
5694 else
5695 frp = curfrp->fr_prev; // 2nd run: start with prev window
5696 while (frp != NULL && take != 0)
5697 {
5698 h = frame_minheight(frp, NULL);
5699 if (room_reserved > 0
5700 && frp->fr_win != NULL
5701 && frp->fr_win->w_p_wfh)
5702 {
5703 if (room_reserved >= frp->fr_height)
5704 room_reserved -= frp->fr_height;
5705 else
5706 {
5707 if (frp->fr_height - room_reserved > take)
5708 room_reserved = frp->fr_height - take;
5709 take -= frp->fr_height - room_reserved;
5710 frame_new_height(frp, room_reserved, FALSE, FALSE);
5711 room_reserved = 0;
5712 }
5713 }
5714 else
5715 {
5716 if (frp->fr_height - take < h)
5717 {
5718 take -= frp->fr_height - h;
5719 frame_new_height(frp, h, FALSE, FALSE);
5720 }
5721 else
5722 {
5723 frame_new_height(frp, frp->fr_height - take,
5724 FALSE, FALSE);
5725 take = 0;
5726 }
5727 }
5728 if (run == 0)
5729 frp = frp->fr_next;
5730 else
5731 frp = frp->fr_prev;
5732 }
5733 }
5734 }
5735 }
5736
5737 /*
5738 * Set current window width and take care of repositioning other windows to
5739 * fit around it.
5740 */
5741 void
win_setwidth(int width)5742 win_setwidth(int width)
5743 {
5744 win_setwidth_win(width, curwin);
5745 }
5746
5747 void
win_setwidth_win(int width,win_T * wp)5748 win_setwidth_win(int width, win_T *wp)
5749 {
5750 // Always keep current window at least one column wide, even when
5751 // 'winminwidth' is zero.
5752 if (wp == curwin)
5753 {
5754 if (width < p_wmw)
5755 width = p_wmw;
5756 if (width == 0)
5757 width = 1;
5758 }
5759 else if (width < 0)
5760 width = 0;
5761
5762 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5763
5764 // recompute the window positions
5765 (void)win_comp_pos();
5766
5767 redraw_all_later(NOT_VALID);
5768 }
5769
5770 /*
5771 * Set the width of a frame to "width" and take care that all frames and
5772 * windows inside it are resized. Also resize frames above and below if the
5773 * are in the same FR_ROW frame.
5774 *
5775 * Strategy is similar to frame_setheight().
5776 */
5777 static void
frame_setwidth(frame_T * curfrp,int width)5778 frame_setwidth(frame_T *curfrp, int width)
5779 {
5780 int room; // total number of lines available
5781 int take; // number of lines taken from other windows
5782 int run;
5783 frame_T *frp;
5784 int w;
5785 int room_reserved;
5786
5787 // If the width already is the desired value, nothing to do.
5788 if (curfrp->fr_width == width)
5789 return;
5790
5791 if (curfrp->fr_parent == NULL)
5792 // topframe: can't change width
5793 return;
5794
5795 if (curfrp->fr_parent->fr_layout == FR_COL)
5796 {
5797 // Column of frames: Also need to resize frames above and below of
5798 // this one. First check for the minimal width of these.
5799 w = frame_minwidth(curfrp->fr_parent, NULL);
5800 if (width < w)
5801 width = w;
5802 frame_setwidth(curfrp->fr_parent, width);
5803 }
5804 else
5805 {
5806 /*
5807 * Row of frames: try to change only frames in this row.
5808 *
5809 * Do this twice:
5810 * 1: compute room available, if it's not enough try resizing the
5811 * containing frame.
5812 * 2: compute the room available and adjust the width to it.
5813 */
5814 for (run = 1; run <= 2; ++run)
5815 {
5816 room = 0;
5817 room_reserved = 0;
5818 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
5819 {
5820 if (frp != curfrp
5821 && frp->fr_win != NULL
5822 && frp->fr_win->w_p_wfw)
5823 room_reserved += frp->fr_width;
5824 room += frp->fr_width;
5825 if (frp != curfrp)
5826 room -= frame_minwidth(frp, NULL);
5827 }
5828
5829 if (width <= room)
5830 break;
5831 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5832 {
5833 if (width > room)
5834 width = room;
5835 break;
5836 }
5837 frame_setwidth(curfrp->fr_parent, width
5838 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5839 }
5840
5841 /*
5842 * Compute the number of lines we will take from others frames (can be
5843 * negative!).
5844 */
5845 take = width - curfrp->fr_width;
5846
5847 // If there is not enough room, also reduce the width of a window
5848 // with 'winfixwidth' set.
5849 if (width > room - room_reserved)
5850 room_reserved = room - width;
5851 // If there is only a 'winfixwidth' window and making the
5852 // window smaller, need to make the other window narrower.
5853 if (take < 0 && room - curfrp->fr_width < room_reserved)
5854 room_reserved = 0;
5855
5856 /*
5857 * set the current frame to the new width
5858 */
5859 frame_new_width(curfrp, width, FALSE, FALSE);
5860
5861 /*
5862 * First take lines from the frames right of the current frame. If
5863 * that is not enough, takes lines from frames left of the current
5864 * frame.
5865 */
5866 for (run = 0; run < 2; ++run)
5867 {
5868 if (run == 0)
5869 frp = curfrp->fr_next; // 1st run: start with next window
5870 else
5871 frp = curfrp->fr_prev; // 2nd run: start with prev window
5872 while (frp != NULL && take != 0)
5873 {
5874 w = frame_minwidth(frp, NULL);
5875 if (room_reserved > 0
5876 && frp->fr_win != NULL
5877 && frp->fr_win->w_p_wfw)
5878 {
5879 if (room_reserved >= frp->fr_width)
5880 room_reserved -= frp->fr_width;
5881 else
5882 {
5883 if (frp->fr_width - room_reserved > take)
5884 room_reserved = frp->fr_width - take;
5885 take -= frp->fr_width - room_reserved;
5886 frame_new_width(frp, room_reserved, FALSE, FALSE);
5887 room_reserved = 0;
5888 }
5889 }
5890 else
5891 {
5892 if (frp->fr_width - take < w)
5893 {
5894 take -= frp->fr_width - w;
5895 frame_new_width(frp, w, FALSE, FALSE);
5896 }
5897 else
5898 {
5899 frame_new_width(frp, frp->fr_width - take,
5900 FALSE, FALSE);
5901 take = 0;
5902 }
5903 }
5904 if (run == 0)
5905 frp = frp->fr_next;
5906 else
5907 frp = frp->fr_prev;
5908 }
5909 }
5910 }
5911 }
5912
5913 /*
5914 * Check 'winminheight' for a valid value and reduce it if needed.
5915 */
5916 void
win_setminheight(void)5917 win_setminheight(void)
5918 {
5919 int room;
5920 int needed;
5921 int first = TRUE;
5922
5923 // loop until there is a 'winminheight' that is possible
5924 while (p_wmh > 0)
5925 {
5926 room = Rows - p_ch;
5927 needed = min_rows() - 1; // 1 was added for the cmdline
5928 if (room >= needed)
5929 break;
5930 --p_wmh;
5931 if (first)
5932 {
5933 emsg(_(e_not_enough_room));
5934 first = FALSE;
5935 }
5936 }
5937 }
5938
5939 /*
5940 * Check 'winminwidth' for a valid value and reduce it if needed.
5941 */
5942 void
win_setminwidth(void)5943 win_setminwidth(void)
5944 {
5945 int room;
5946 int needed;
5947 int first = TRUE;
5948
5949 // loop until there is a 'winminheight' that is possible
5950 while (p_wmw > 0)
5951 {
5952 room = Columns;
5953 needed = frame_minwidth(topframe, NULL);
5954 if (room >= needed)
5955 break;
5956 --p_wmw;
5957 if (first)
5958 {
5959 emsg(_(e_not_enough_room));
5960 first = FALSE;
5961 }
5962 }
5963 }
5964
5965 /*
5966 * Status line of dragwin is dragged "offset" lines down (negative is up).
5967 */
5968 void
win_drag_status_line(win_T * dragwin,int offset)5969 win_drag_status_line(win_T *dragwin, int offset)
5970 {
5971 frame_T *curfr;
5972 frame_T *fr;
5973 int room;
5974 int row;
5975 int up; // if TRUE, drag status line up, otherwise down
5976 int n;
5977
5978 fr = dragwin->w_frame;
5979 curfr = fr;
5980 if (fr != topframe) // more than one window
5981 {
5982 fr = fr->fr_parent;
5983 // When the parent frame is not a column of frames, its parent should
5984 // be.
5985 if (fr->fr_layout != FR_COL)
5986 {
5987 curfr = fr;
5988 if (fr != topframe) // only a row of windows, may drag statusline
5989 fr = fr->fr_parent;
5990 }
5991 }
5992
5993 // If this is the last frame in a column, may want to resize the parent
5994 // frame instead (go two up to skip a row of frames).
5995 while (curfr != topframe && curfr->fr_next == NULL)
5996 {
5997 if (fr != topframe)
5998 fr = fr->fr_parent;
5999 curfr = fr;
6000 if (fr != topframe)
6001 fr = fr->fr_parent;
6002 }
6003
6004 if (offset < 0) // drag up
6005 {
6006 up = TRUE;
6007 offset = -offset;
6008 // sum up the room of the current frame and above it
6009 if (fr == curfr)
6010 {
6011 // only one window
6012 room = fr->fr_height - frame_minheight(fr, NULL);
6013 }
6014 else
6015 {
6016 room = 0;
6017 for (fr = fr->fr_child; ; fr = fr->fr_next)
6018 {
6019 room += fr->fr_height - frame_minheight(fr, NULL);
6020 if (fr == curfr)
6021 break;
6022 }
6023 }
6024 fr = curfr->fr_next; // put fr at frame that grows
6025 }
6026 else // drag down
6027 {
6028 up = FALSE;
6029 /*
6030 * Only dragging the last status line can reduce p_ch.
6031 */
6032 room = Rows - cmdline_row;
6033 if (curfr->fr_next == NULL)
6034 room -= 1;
6035 else
6036 room -= p_ch;
6037 if (room < 0)
6038 room = 0;
6039 // sum up the room of frames below of the current one
6040 FOR_ALL_FRAMES(fr, curfr->fr_next)
6041 room += fr->fr_height - frame_minheight(fr, NULL);
6042 fr = curfr; // put fr at window that grows
6043 }
6044
6045 if (room < offset) // Not enough room
6046 offset = room; // Move as far as we can
6047 if (offset <= 0)
6048 return;
6049
6050 /*
6051 * Grow frame fr by "offset" lines.
6052 * Doesn't happen when dragging the last status line up.
6053 */
6054 if (fr != NULL)
6055 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
6056
6057 if (up)
6058 fr = curfr; // current frame gets smaller
6059 else
6060 fr = curfr->fr_next; // next frame gets smaller
6061
6062 /*
6063 * Now make the other frames smaller.
6064 */
6065 while (fr != NULL && offset > 0)
6066 {
6067 n = frame_minheight(fr, NULL);
6068 if (fr->fr_height - offset <= n)
6069 {
6070 offset -= fr->fr_height - n;
6071 frame_new_height(fr, n, !up, FALSE);
6072 }
6073 else
6074 {
6075 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
6076 break;
6077 }
6078 if (up)
6079 fr = fr->fr_prev;
6080 else
6081 fr = fr->fr_next;
6082 }
6083 row = win_comp_pos();
6084 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
6085 cmdline_row = row;
6086 p_ch = Rows - cmdline_row;
6087 if (p_ch < 1)
6088 p_ch = 1;
6089 curtab->tp_ch_used = p_ch;
6090 redraw_all_later(SOME_VALID);
6091 showmode();
6092 }
6093
6094 /*
6095 * Separator line of dragwin is dragged "offset" lines right (negative is left).
6096 */
6097 void
win_drag_vsep_line(win_T * dragwin,int offset)6098 win_drag_vsep_line(win_T *dragwin, int offset)
6099 {
6100 frame_T *curfr;
6101 frame_T *fr;
6102 int room;
6103 int left; // if TRUE, drag separator line left, otherwise right
6104 int n;
6105
6106 fr = dragwin->w_frame;
6107 if (fr == topframe) // only one window (cannot happen?)
6108 return;
6109 curfr = fr;
6110 fr = fr->fr_parent;
6111 // When the parent frame is not a row of frames, its parent should be.
6112 if (fr->fr_layout != FR_ROW)
6113 {
6114 if (fr == topframe) // only a column of windows (cannot happen?)
6115 return;
6116 curfr = fr;
6117 fr = fr->fr_parent;
6118 }
6119
6120 // If this is the last frame in a row, may want to resize a parent
6121 // frame instead.
6122 while (curfr->fr_next == NULL)
6123 {
6124 if (fr == topframe)
6125 break;
6126 curfr = fr;
6127 fr = fr->fr_parent;
6128 if (fr != topframe)
6129 {
6130 curfr = fr;
6131 fr = fr->fr_parent;
6132 }
6133 }
6134
6135 if (offset < 0) // drag left
6136 {
6137 left = TRUE;
6138 offset = -offset;
6139 // sum up the room of the current frame and left of it
6140 room = 0;
6141 for (fr = fr->fr_child; ; fr = fr->fr_next)
6142 {
6143 room += fr->fr_width - frame_minwidth(fr, NULL);
6144 if (fr == curfr)
6145 break;
6146 }
6147 fr = curfr->fr_next; // put fr at frame that grows
6148 }
6149 else // drag right
6150 {
6151 left = FALSE;
6152 // sum up the room of frames right of the current one
6153 room = 0;
6154 FOR_ALL_FRAMES(fr, curfr->fr_next)
6155 room += fr->fr_width - frame_minwidth(fr, NULL);
6156 fr = curfr; // put fr at window that grows
6157 }
6158
6159 if (room < offset) // Not enough room
6160 offset = room; // Move as far as we can
6161 if (offset <= 0) // No room at all, quit.
6162 return;
6163 if (fr == NULL)
6164 return; // Safety check, should not happen.
6165
6166 // grow frame fr by offset lines
6167 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
6168
6169 // shrink other frames: current and at the left or at the right
6170 if (left)
6171 fr = curfr; // current frame gets smaller
6172 else
6173 fr = curfr->fr_next; // next frame gets smaller
6174
6175 while (fr != NULL && offset > 0)
6176 {
6177 n = frame_minwidth(fr, NULL);
6178 if (fr->fr_width - offset <= n)
6179 {
6180 offset -= fr->fr_width - n;
6181 frame_new_width(fr, n, !left, FALSE);
6182 }
6183 else
6184 {
6185 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
6186 break;
6187 }
6188 if (left)
6189 fr = fr->fr_prev;
6190 else
6191 fr = fr->fr_next;
6192 }
6193 (void)win_comp_pos();
6194 redraw_all_later(NOT_VALID);
6195 }
6196
6197 #define FRACTION_MULT 16384L
6198
6199 /*
6200 * Set wp->w_fraction for the current w_wrow and w_height.
6201 * Has no effect when the window is less than two lines.
6202 */
6203 void
set_fraction(win_T * wp)6204 set_fraction(win_T *wp)
6205 {
6206 if (wp->w_height > 1)
6207 // When cursor is in the first line the percentage is computed as if
6208 // it's halfway that line. Thus with two lines it is 25%, with three
6209 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
6210 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
6211 + FRACTION_MULT / 2) / (long)wp->w_height;
6212 }
6213
6214 /*
6215 * Set the height of a window.
6216 * "height" excludes any window toolbar.
6217 * This takes care of the things inside the window, not what happens to the
6218 * window position, the frame or to other windows.
6219 */
6220 void
win_new_height(win_T * wp,int height)6221 win_new_height(win_T *wp, int height)
6222 {
6223 int prev_height = wp->w_height;
6224
6225 // Don't want a negative height. Happens when splitting a tiny window.
6226 // Will equalize heights soon to fix it.
6227 if (height < 0)
6228 height = 0;
6229 if (wp->w_height == height)
6230 return; // nothing to do
6231
6232 if (wp->w_height > 0)
6233 {
6234 if (wp == curwin)
6235 // w_wrow needs to be valid. When setting 'laststatus' this may
6236 // call win_new_height() recursively.
6237 validate_cursor();
6238 if (wp->w_height != prev_height)
6239 return; // Recursive call already changed the size, bail out here
6240 // to avoid the following to mess things up.
6241 if (wp->w_wrow != wp->w_prev_fraction_row)
6242 set_fraction(wp);
6243 }
6244
6245 wp->w_height = height;
6246 wp->w_skipcol = 0;
6247
6248 // There is no point in adjusting the scroll position when exiting. Some
6249 // values might be invalid.
6250 if (!exiting)
6251 scroll_to_fraction(wp, prev_height);
6252 }
6253
6254 void
scroll_to_fraction(win_T * wp,int prev_height)6255 scroll_to_fraction(win_T *wp, int prev_height)
6256 {
6257 linenr_T lnum;
6258 int sline, line_size;
6259 int height = wp->w_height;
6260
6261 // Don't change w_topline in any of these cases:
6262 // - window height is 0
6263 // - 'scrollbind' is set and this isn't the current window
6264 // - window height is sufficient to display the whole buffer and first line
6265 // is visible.
6266 if (height > 0
6267 && (!wp->w_p_scb || wp == curwin)
6268 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
6269 {
6270 /*
6271 * Find a value for w_topline that shows the cursor at the same
6272 * relative position in the window as before (more or less).
6273 */
6274 lnum = wp->w_cursor.lnum;
6275 if (lnum < 1) // can happen when starting up
6276 lnum = 1;
6277 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
6278 / FRACTION_MULT;
6279 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
6280 sline = wp->w_wrow - line_size;
6281
6282 if (sline >= 0)
6283 {
6284 // Make sure the whole cursor line is visible, if possible.
6285 int rows = plines_win(wp, lnum, FALSE);
6286
6287 if (sline > wp->w_height - rows)
6288 {
6289 sline = wp->w_height - rows;
6290 wp->w_wrow -= rows - line_size;
6291 }
6292 }
6293
6294 if (sline < 0)
6295 {
6296 /*
6297 * Cursor line would go off top of screen if w_wrow was this high.
6298 * Make cursor line the first line in the window. If not enough
6299 * room use w_skipcol;
6300 */
6301 wp->w_wrow = line_size;
6302 if (wp->w_wrow >= wp->w_height
6303 && (wp->w_width - win_col_off(wp)) > 0)
6304 {
6305 wp->w_skipcol += wp->w_width - win_col_off(wp);
6306 --wp->w_wrow;
6307 while (wp->w_wrow >= wp->w_height)
6308 {
6309 wp->w_skipcol += wp->w_width - win_col_off(wp)
6310 + win_col_off2(wp);
6311 --wp->w_wrow;
6312 }
6313 }
6314 }
6315 else if (sline > 0)
6316 {
6317 while (sline > 0 && lnum > 1)
6318 {
6319 #ifdef FEAT_FOLDING
6320 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
6321 if (lnum == 1)
6322 {
6323 // first line in buffer is folded
6324 line_size = 1;
6325 --sline;
6326 break;
6327 }
6328 #endif
6329 --lnum;
6330 #ifdef FEAT_DIFF
6331 if (lnum == wp->w_topline)
6332 line_size = plines_win_nofill(wp, lnum, TRUE)
6333 + wp->w_topfill;
6334 else
6335 #endif
6336 line_size = plines_win(wp, lnum, TRUE);
6337 sline -= line_size;
6338 }
6339
6340 if (sline < 0)
6341 {
6342 /*
6343 * Line we want at top would go off top of screen. Use next
6344 * line instead.
6345 */
6346 #ifdef FEAT_FOLDING
6347 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6348 #endif
6349 lnum++;
6350 wp->w_wrow -= line_size + sline;
6351 }
6352 else if (sline > 0)
6353 {
6354 // First line of file reached, use that as topline.
6355 lnum = 1;
6356 wp->w_wrow -= sline;
6357 }
6358 }
6359 set_topline(wp, lnum);
6360 }
6361
6362 if (wp == curwin)
6363 {
6364 if (get_scrolloff_value())
6365 update_topline();
6366 curs_columns(FALSE); // validate w_wrow
6367 }
6368 if (prev_height > 0)
6369 wp->w_prev_fraction_row = wp->w_wrow;
6370
6371 win_comp_scroll(wp);
6372 redraw_win_later(wp, SOME_VALID);
6373 wp->w_redr_status = TRUE;
6374 invalidate_botline_win(wp);
6375 }
6376
6377 /*
6378 * Set the width of a window.
6379 */
6380 void
win_new_width(win_T * wp,int width)6381 win_new_width(win_T *wp, int width)
6382 {
6383 wp->w_width = width;
6384 wp->w_lines_valid = 0;
6385 changed_line_abv_curs_win(wp);
6386 invalidate_botline_win(wp);
6387 if (wp == curwin)
6388 {
6389 update_topline();
6390 curs_columns(TRUE); // validate w_wrow
6391 }
6392 redraw_win_later(wp, NOT_VALID);
6393 wp->w_redr_status = TRUE;
6394 }
6395
6396 void
win_comp_scroll(win_T * wp)6397 win_comp_scroll(win_T *wp)
6398 {
6399 #if defined(FEAT_EVAL)
6400 int old_w_p_scr = wp->w_p_scr;
6401 #endif
6402
6403 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6404 if (wp->w_p_scr == 0)
6405 wp->w_p_scr = 1;
6406 #if defined(FEAT_EVAL)
6407 if (wp->w_p_scr != old_w_p_scr)
6408 {
6409 // Used by "verbose set scroll".
6410 wp->w_p_script_ctx[WV_SCROLL].sc_sid = SID_WINLAYOUT;
6411 wp->w_p_script_ctx[WV_SCROLL].sc_lnum = 0;
6412 }
6413 #endif
6414 }
6415
6416 /*
6417 * command_height: called whenever p_ch has been changed
6418 */
6419 void
command_height(void)6420 command_height(void)
6421 {
6422 int h;
6423 frame_T *frp;
6424 int old_p_ch = curtab->tp_ch_used;
6425
6426 // Use the value of p_ch that we remembered. This is needed for when the
6427 // GUI starts up, we can't be sure in what order things happen. And when
6428 // p_ch was changed in another tab page.
6429 curtab->tp_ch_used = p_ch;
6430
6431 // Find bottom frame with width of screen.
6432 frp = lastwin->w_frame;
6433 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6434 frp = frp->fr_parent;
6435
6436 // Avoid changing the height of a window with 'winfixheight' set.
6437 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6438 && frp->fr_win->w_p_wfh)
6439 frp = frp->fr_prev;
6440
6441 if (starting != NO_SCREEN)
6442 {
6443 cmdline_row = Rows - p_ch;
6444
6445 if (p_ch > old_p_ch) // p_ch got bigger
6446 {
6447 while (p_ch > old_p_ch)
6448 {
6449 if (frp == NULL)
6450 {
6451 emsg(_(e_not_enough_room));
6452 p_ch = old_p_ch;
6453 curtab->tp_ch_used = p_ch;
6454 cmdline_row = Rows - p_ch;
6455 break;
6456 }
6457 h = frp->fr_height - frame_minheight(frp, NULL);
6458 if (h > p_ch - old_p_ch)
6459 h = p_ch - old_p_ch;
6460 old_p_ch += h;
6461 frame_add_height(frp, -h);
6462 frp = frp->fr_prev;
6463 }
6464
6465 // Recompute window positions.
6466 (void)win_comp_pos();
6467
6468 // clear the lines added to cmdline
6469 if (full_screen)
6470 screen_fill((int)(cmdline_row), (int)Rows, 0,
6471 (int)Columns, ' ', ' ', 0);
6472 msg_row = cmdline_row;
6473 redraw_cmdline = TRUE;
6474 return;
6475 }
6476
6477 if (msg_row < cmdline_row)
6478 msg_row = cmdline_row;
6479 redraw_cmdline = TRUE;
6480 }
6481 frame_add_height(frp, (int)(old_p_ch - p_ch));
6482
6483 // Recompute window positions.
6484 if (frp != lastwin->w_frame)
6485 (void)win_comp_pos();
6486 }
6487
6488 /*
6489 * Resize frame "frp" to be "n" lines higher (negative for less high).
6490 * Also resize the frames it is contained in.
6491 */
6492 static void
frame_add_height(frame_T * frp,int n)6493 frame_add_height(frame_T *frp, int n)
6494 {
6495 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6496 for (;;)
6497 {
6498 frp = frp->fr_parent;
6499 if (frp == NULL)
6500 break;
6501 frp->fr_height += n;
6502 }
6503 }
6504
6505 /*
6506 * Add or remove a status line for the bottom window(s), according to the
6507 * value of 'laststatus'.
6508 */
6509 void
last_status(int morewin)6510 last_status(
6511 int morewin) // pretend there are two or more windows
6512 {
6513 // Don't make a difference between horizontal or vertical split.
6514 last_status_rec(topframe, (p_ls == 2
6515 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
6516 }
6517
6518 static void
last_status_rec(frame_T * fr,int statusline)6519 last_status_rec(frame_T *fr, int statusline)
6520 {
6521 frame_T *fp;
6522 win_T *wp;
6523
6524 if (fr->fr_layout == FR_LEAF)
6525 {
6526 wp = fr->fr_win;
6527 if (wp->w_status_height != 0 && !statusline)
6528 {
6529 // remove status line
6530 win_new_height(wp, wp->w_height + 1);
6531 wp->w_status_height = 0;
6532 comp_col();
6533 }
6534 else if (wp->w_status_height == 0 && statusline)
6535 {
6536 // Find a frame to take a line from.
6537 fp = fr;
6538 while (fp->fr_height <= frame_minheight(fp, NULL))
6539 {
6540 if (fp == topframe)
6541 {
6542 emsg(_(e_not_enough_room));
6543 return;
6544 }
6545 // In a column of frames: go to frame above. If already at
6546 // the top or in a row of frames: go to parent.
6547 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6548 fp = fp->fr_prev;
6549 else
6550 fp = fp->fr_parent;
6551 }
6552 wp->w_status_height = 1;
6553 if (fp != fr)
6554 {
6555 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6556 frame_fix_height(wp);
6557 (void)win_comp_pos();
6558 }
6559 else
6560 win_new_height(wp, wp->w_height - 1);
6561 comp_col();
6562 redraw_all_later(SOME_VALID);
6563 }
6564 }
6565 else if (fr->fr_layout == FR_ROW)
6566 {
6567 // vertically split windows, set status line for each one
6568 FOR_ALL_FRAMES(fp, fr->fr_child)
6569 last_status_rec(fp, statusline);
6570 }
6571 else
6572 {
6573 // horizontally split window, set status line for last one
6574 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6575 ;
6576 last_status_rec(fp, statusline);
6577 }
6578 }
6579
6580 /*
6581 * Return the number of lines used by the tab page line.
6582 */
6583 int
tabline_height(void)6584 tabline_height(void)
6585 {
6586 #ifdef FEAT_GUI_TABLINE
6587 // When the GUI has the tabline then this always returns zero.
6588 if (gui_use_tabline())
6589 return 0;
6590 #endif
6591 switch (p_stal)
6592 {
6593 case 0: return 0;
6594 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6595 }
6596 return 1;
6597 }
6598
6599 /*
6600 * Return the minimal number of rows that is needed on the screen to display
6601 * the current number of windows.
6602 */
6603 int
min_rows(void)6604 min_rows(void)
6605 {
6606 int total;
6607 tabpage_T *tp;
6608 int n;
6609
6610 if (firstwin == NULL) // not initialized yet
6611 return MIN_LINES;
6612
6613 total = 0;
6614 FOR_ALL_TABPAGES(tp)
6615 {
6616 n = frame_minheight(tp->tp_topframe, NULL);
6617 if (total < n)
6618 total = n;
6619 }
6620 total += tabline_height();
6621 total += 1; // count the room for the command line
6622 return total;
6623 }
6624
6625 /*
6626 * Return TRUE if there is only one window and only one tab page, not
6627 * counting a help or preview window, unless it is the current window.
6628 * Does not count unlisted windows.
6629 */
6630 int
only_one_window(void)6631 only_one_window(void)
6632 {
6633 int count = 0;
6634 win_T *wp;
6635
6636 #if defined(FEAT_PROP_POPUP)
6637 // If the current window is a popup then there always is another window.
6638 if (popup_is_popup(curwin))
6639 return FALSE;
6640 #endif
6641
6642 // If there is another tab page there always is another window.
6643 if (first_tabpage->tp_next != NULL)
6644 return FALSE;
6645
6646 FOR_ALL_WINDOWS(wp)
6647 if (wp->w_buffer != NULL
6648 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
6649 # ifdef FEAT_QUICKFIX
6650 || wp->w_p_pvw
6651 # endif
6652 ) || wp == curwin) && wp != aucmd_win)
6653 ++count;
6654 return (count <= 1);
6655 }
6656
6657 /*
6658 * Correct the cursor line number in other windows. Used after changing the
6659 * current buffer, and before applying autocommands.
6660 * When "do_curwin" is TRUE, also check current window.
6661 */
6662 void
check_lnums(int do_curwin)6663 check_lnums(int do_curwin)
6664 {
6665 win_T *wp;
6666 tabpage_T *tp;
6667
6668 FOR_ALL_TAB_WINDOWS(tp, wp)
6669 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6670 {
6671 // save the original cursor position and topline
6672 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6673 wp->w_save_cursor.w_topline_save = wp->w_topline;
6674
6675 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6676 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6677 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6678 wp->w_topline = curbuf->b_ml.ml_line_count;
6679
6680 // save the corrected cursor position and topline
6681 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6682 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6683 }
6684 }
6685
6686 /*
6687 * Reset cursor and topline to its stored values from check_lnums().
6688 * check_lnums() must have been called first!
6689 */
6690 void
reset_lnums()6691 reset_lnums()
6692 {
6693 win_T *wp;
6694 tabpage_T *tp;
6695
6696 FOR_ALL_TAB_WINDOWS(tp, wp)
6697 if (wp->w_buffer == curbuf)
6698 {
6699 // Restore the value if the autocommand didn't change it.
6700 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6701 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6702 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6703 wp->w_topline = wp->w_save_cursor.w_topline_save;
6704 }
6705 }
6706
6707 /*
6708 * A snapshot of the window sizes, to restore them after closing the help
6709 * window.
6710 * Only these fields are used:
6711 * fr_layout
6712 * fr_width
6713 * fr_height
6714 * fr_next
6715 * fr_child
6716 * fr_win (only valid for the old curwin, NULL otherwise)
6717 */
6718
6719 /*
6720 * Create a snapshot of the current frame sizes.
6721 */
6722 void
make_snapshot(int idx)6723 make_snapshot(int idx)
6724 {
6725 clear_snapshot(curtab, idx);
6726 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6727 }
6728
6729 static void
make_snapshot_rec(frame_T * fr,frame_T ** frp)6730 make_snapshot_rec(frame_T *fr, frame_T **frp)
6731 {
6732 *frp = ALLOC_CLEAR_ONE(frame_T);
6733 if (*frp == NULL)
6734 return;
6735 (*frp)->fr_layout = fr->fr_layout;
6736 (*frp)->fr_width = fr->fr_width;
6737 (*frp)->fr_height = fr->fr_height;
6738 if (fr->fr_next != NULL)
6739 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6740 if (fr->fr_child != NULL)
6741 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6742 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6743 (*frp)->fr_win = curwin;
6744 }
6745
6746 /*
6747 * Remove any existing snapshot.
6748 */
6749 static void
clear_snapshot(tabpage_T * tp,int idx)6750 clear_snapshot(tabpage_T *tp, int idx)
6751 {
6752 clear_snapshot_rec(tp->tp_snapshot[idx]);
6753 tp->tp_snapshot[idx] = NULL;
6754 }
6755
6756 static void
clear_snapshot_rec(frame_T * fr)6757 clear_snapshot_rec(frame_T *fr)
6758 {
6759 if (fr != NULL)
6760 {
6761 clear_snapshot_rec(fr->fr_next);
6762 clear_snapshot_rec(fr->fr_child);
6763 vim_free(fr);
6764 }
6765 }
6766
6767 /*
6768 * Restore a previously created snapshot, if there is any.
6769 * This is only done if the screen size didn't change and the window layout is
6770 * still the same.
6771 */
6772 void
restore_snapshot(int idx,int close_curwin)6773 restore_snapshot(
6774 int idx,
6775 int close_curwin) // closing current window
6776 {
6777 win_T *wp;
6778
6779 if (curtab->tp_snapshot[idx] != NULL
6780 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6781 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6782 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6783 {
6784 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6785 win_comp_pos();
6786 if (wp != NULL && close_curwin)
6787 win_goto(wp);
6788 redraw_all_later(NOT_VALID);
6789 }
6790 clear_snapshot(curtab, idx);
6791 }
6792
6793 /*
6794 * Check if frames "sn" and "fr" have the same layout, same following frames
6795 * and same children. And the window pointer is valid.
6796 */
6797 static int
check_snapshot_rec(frame_T * sn,frame_T * fr)6798 check_snapshot_rec(frame_T *sn, frame_T *fr)
6799 {
6800 if (sn->fr_layout != fr->fr_layout
6801 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6802 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6803 || (sn->fr_next != NULL
6804 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6805 || (sn->fr_child != NULL
6806 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
6807 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
6808 return FAIL;
6809 return OK;
6810 }
6811
6812 /*
6813 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6814 * following frames and children.
6815 * Returns a pointer to the old current window, or NULL.
6816 */
6817 static win_T *
restore_snapshot_rec(frame_T * sn,frame_T * fr)6818 restore_snapshot_rec(frame_T *sn, frame_T *fr)
6819 {
6820 win_T *wp = NULL;
6821 win_T *wp2;
6822
6823 fr->fr_height = sn->fr_height;
6824 fr->fr_width = sn->fr_width;
6825 if (fr->fr_layout == FR_LEAF)
6826 {
6827 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6828 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6829 wp = sn->fr_win;
6830 }
6831 if (sn->fr_next != NULL)
6832 {
6833 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6834 if (wp2 != NULL)
6835 wp = wp2;
6836 }
6837 if (sn->fr_child != NULL)
6838 {
6839 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6840 if (wp2 != NULL)
6841 wp = wp2;
6842 }
6843 return wp;
6844 }
6845
6846 #if defined(FEAT_GUI) || defined(PROTO)
6847 /*
6848 * Return TRUE if there is any vertically split window.
6849 */
6850 int
win_hasvertsplit(void)6851 win_hasvertsplit(void)
6852 {
6853 frame_T *fr;
6854
6855 if (topframe->fr_layout == FR_ROW)
6856 return TRUE;
6857
6858 if (topframe->fr_layout == FR_COL)
6859 FOR_ALL_FRAMES(fr, topframe->fr_child)
6860 if (fr->fr_layout == FR_ROW)
6861 return TRUE;
6862
6863 return FALSE;
6864 }
6865 #endif
6866
6867 #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6868 int
get_win_number(win_T * wp,win_T * first_win)6869 get_win_number(win_T *wp, win_T *first_win)
6870 {
6871 int i = 1;
6872 win_T *w;
6873
6874 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
6875 ++i;
6876
6877 if (w == NULL)
6878 return 0;
6879 else
6880 return i;
6881 }
6882
6883 int
get_tab_number(tabpage_T * tp UNUSED)6884 get_tab_number(tabpage_T *tp UNUSED)
6885 {
6886 int i = 1;
6887 tabpage_T *t;
6888
6889 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6890 ++i;
6891
6892 if (t == NULL)
6893 return 0;
6894 else
6895 return i;
6896 }
6897 #endif
6898
6899 /*
6900 * Return TRUE if "topfrp" and its children are at the right height.
6901 */
6902 static int
frame_check_height(frame_T * topfrp,int height)6903 frame_check_height(frame_T *topfrp, int height)
6904 {
6905 frame_T *frp;
6906
6907 if (topfrp->fr_height != height)
6908 return FALSE;
6909
6910 if (topfrp->fr_layout == FR_ROW)
6911 FOR_ALL_FRAMES(frp, topfrp->fr_child)
6912 if (frp->fr_height != height)
6913 return FALSE;
6914
6915 return TRUE;
6916 }
6917
6918 /*
6919 * Return TRUE if "topfrp" and its children are at the right width.
6920 */
6921 static int
frame_check_width(frame_T * topfrp,int width)6922 frame_check_width(frame_T *topfrp, int width)
6923 {
6924 frame_T *frp;
6925
6926 if (topfrp->fr_width != width)
6927 return FALSE;
6928
6929 if (topfrp->fr_layout == FR_COL)
6930 FOR_ALL_FRAMES(frp, topfrp->fr_child)
6931 if (frp->fr_width != width)
6932 return FALSE;
6933
6934 return TRUE;
6935 }
6936
6937 #if defined(FEAT_SYN_HL) || defined(PROTO)
6938 /*
6939 * Simple int comparison function for use with qsort()
6940 */
6941 static int
int_cmp(const void * a,const void * b)6942 int_cmp(const void *a, const void *b)
6943 {
6944 return *(const int *)a - *(const int *)b;
6945 }
6946
6947 /*
6948 * Handle setting 'colorcolumn' or 'textwidth' in window "wp".
6949 * Returns error message, NULL if it's OK.
6950 */
6951 char *
check_colorcolumn(win_T * wp)6952 check_colorcolumn(win_T *wp)
6953 {
6954 char_u *s;
6955 int col;
6956 int count = 0;
6957 int color_cols[256];
6958 int i;
6959 int j = 0;
6960
6961 if (wp->w_buffer == NULL)
6962 return NULL; // buffer was closed
6963
6964 for (s = wp->w_p_cc; *s != NUL && count < 255;)
6965 {
6966 if (*s == '-' || *s == '+')
6967 {
6968 // -N and +N: add to 'textwidth'
6969 col = (*s == '-') ? -1 : 1;
6970 ++s;
6971 if (!VIM_ISDIGIT(*s))
6972 return e_invarg;
6973 col = col * getdigits(&s);
6974 if (wp->w_buffer->b_p_tw == 0)
6975 goto skip; // 'textwidth' not set, skip this item
6976 col += wp->w_buffer->b_p_tw;
6977 if (col < 0)
6978 goto skip;
6979 }
6980 else if (VIM_ISDIGIT(*s))
6981 col = getdigits(&s);
6982 else
6983 return e_invarg;
6984 color_cols[count++] = col - 1; // 1-based to 0-based
6985 skip:
6986 if (*s == NUL)
6987 break;
6988 if (*s != ',')
6989 return e_invarg;
6990 if (*++s == NUL)
6991 return e_invarg; // illegal trailing comma as in "set cc=80,"
6992 }
6993
6994 vim_free(wp->w_p_cc_cols);
6995 if (count == 0)
6996 wp->w_p_cc_cols = NULL;
6997 else
6998 {
6999 wp->w_p_cc_cols = ALLOC_MULT(int, count + 1);
7000 if (wp->w_p_cc_cols != NULL)
7001 {
7002 // sort the columns for faster usage on screen redraw inside
7003 // win_line()
7004 qsort(color_cols, count, sizeof(int), int_cmp);
7005
7006 for (i = 0; i < count; ++i)
7007 // skip duplicates
7008 if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i])
7009 wp->w_p_cc_cols[j++] = color_cols[i];
7010 wp->w_p_cc_cols[j] = -1; // end marker
7011 }
7012 }
7013
7014 return NULL; // no error
7015 }
7016 #endif
7017