diff options
Diffstat (limited to 'src/nvim/window.c')
-rw-r--r-- | src/nvim/window.c | 308 |
1 files changed, 241 insertions, 67 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index 1e6de73549..0fff93d984 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -70,8 +70,8 @@ static char *m_onlyone = N_("Already only one window"); /* * all CTRL-W window commands are handled here, called from normal_cmd(). */ -void -do_window ( +void +do_window( int nchar, long Prenum, int xchar /* extra char from ":wincmd gx" or NUL */ @@ -85,10 +85,7 @@ do_window ( size_t len; char cbuf[40]; - if (Prenum == 0) - Prenum1 = 1; - else - Prenum1 = Prenum; + Prenum1 = Prenum == 0 ? 1 : Prenum; # define CHECK_CMDWIN \ do { \ @@ -131,8 +128,20 @@ do_window ( case '^': CHECK_CMDWIN; reset_VIsual_and_resel(); // stop Visual mode - cmd_with_count("split #", (char_u *)cbuf, sizeof(cbuf), Prenum); - do_cmdline_cmd(cbuf); + + if (buflist_findnr(Prenum == 0 ? curwin->w_alt_fnum : Prenum) == NULL) { + if (Prenum == 0) { + EMSG(_(e_noalt)); + } else { + EMSGN(_("E92: Buffer %" PRId64 " not found"), Prenum); + } + break; + } + + if (!curbuf_locked() && win_split(0, 0) == OK) { + (void)buflist_getfile(Prenum == 0 ? curwin->w_alt_fnum : Prenum, + (linenr_T)0, GETF_ALT, false); + } break; /* open new window */ @@ -507,11 +516,22 @@ wingotofile: do_nv_ident('g', xchar); break; + case TAB: + goto_tabpage_lastused(); + break; + case 'f': /* CTRL-W gf: "gf" in a new tab page */ case 'F': /* CTRL-W gF: "gF" in a new tab page */ cmdmod.tab = tabpage_index(curtab) + 1; nchar = xchar; goto wingotofile; + case 't': // CTRL-W gt: go to next tab page + goto_tabpage((int)Prenum); + break; + + case 'T': // CTRL-W gT: go to previous tab page + goto_tabpage(-(int)Prenum1); + break; case 'e': if (curwin->w_floating || !ui_has(kUIMultigrid)) { @@ -598,7 +618,6 @@ void win_set_minimal_style(win_T *wp) wp->w_p_cuc = false; wp->w_p_spell = false; wp->w_p_list = false; - wp->w_p_fdc = 0; // Hide EOB region: use " " fillchar and cleared highlighting if (wp->w_p_fcs_chars.eob != ' ') { @@ -621,6 +640,18 @@ void win_set_minimal_style(win_T *wp) xfree(wp->w_p_scl); wp->w_p_scl = (char_u *)xstrdup("auto"); } + + // foldcolumn: use 'auto' + if (wp->w_p_fdc[0] != '0') { + xfree(wp->w_p_fdc); + wp->w_p_fdc = (char_u *)xstrdup("0"); + } + + // colorcolumn: cleared + if (wp->w_p_cc != NULL && *wp->w_p_cc != NUL) { + xfree(wp->w_p_cc); + wp->w_p_cc = (char_u *)xstrdup(""); + } } void win_config_float(win_T *wp, FloatConfig fconfig) @@ -663,6 +694,21 @@ void win_check_anchored_floats(win_T *win) } } +/// Return the number of fold columns to display +int win_fdccol_count(win_T *wp) +{ + const char *fdc = (const char *)wp->w_p_fdc; + + // auto:<NUM> + if (strncmp(fdc, "auto:", 5) == 0) { + int needed_fdccols = getDeepestNesting(wp); + return MIN(fdc[5] - '0', needed_fdccols); + } else { + return fdc[0] - '0'; + } +} + + static void ui_ext_win_position(win_T *wp) { if (!wp->w_floating) { @@ -727,6 +773,21 @@ static void ui_ext_win_position(win_T *wp) } +void ui_ext_win_viewport(win_T *wp) +{ + if ((wp == curwin || ui_has(kUIMultigrid)) && wp->w_viewport_invalid) { + int botline = wp->w_botline; + if (botline == wp->w_buffer->b_ml.ml_line_count+1 + && wp->w_empty_rows == 0) { + // TODO(bfredl): The might be more cases to consider, like how does this + // interact with incomplete final line? Diff filler lines? + botline = wp->w_buffer->b_ml.ml_line_count; + } + ui_call_win_viewport(wp->w_grid.handle, wp->handle, wp->w_topline-1, + botline, wp->w_cursor.lnum-1, wp->w_cursor.col); + wp->w_viewport_invalid = false; + } +} static bool parse_float_anchor(String anchor, FloatAnchor *out) { @@ -1051,7 +1112,8 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) // add a status line when p_ls == 1 and splitting the first window if (one_nonfloat() && p_ls == 1 && oldwin->w_status_height == 0) { - if (oldwin->w_height <= p_wmh && new_in_layout) { + if ((oldwin->w_height + oldwin->w_winbar_height) <= p_wmh + && new_in_layout) { EMSG(_(e_noroom)); return FAIL; } @@ -1148,7 +1210,7 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) * height. */ // Current window requires at least 1 space. - wmh1 = (p_wmh == 0 ? 1 : p_wmh); + wmh1 = (p_wmh == 0 ? 1 : p_wmh) + curwin->w_winbar_height; needed = wmh1 + STATUS_HEIGHT; if (flags & WSP_ROOM) { needed += p_wh - wmh1; @@ -1346,12 +1408,12 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) if (flags & (WSP_TOP | WSP_BOT)) { /* set height and row of new window to full height */ wp->w_winrow = tabline_height(); - win_new_height(wp, curfrp->fr_height - (p_ls > 0)); + win_new_height(wp, curfrp->fr_height - (p_ls > 0) - wp->w_winbar_height); wp->w_status_height = (p_ls > 0); } else { /* height and row of new window is same as current window */ wp->w_winrow = oldwin->w_winrow; - win_new_height(wp, oldwin->w_height); + win_new_height(wp, oldwin->w_height + oldwin->w_winbar_height); wp->w_status_height = oldwin->w_status_height; } frp->fr_height = curfrp->fr_height; @@ -1398,7 +1460,7 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) * one row for the status line */ win_new_height(wp, new_size); if (flags & (WSP_TOP | WSP_BOT)) { - int new_fr_height = curfrp->fr_height - new_size; + int new_fr_height = curfrp->fr_height - new_size + wp->w_winbar_height; if (!((flags & WSP_BOT) && p_ls == 0)) { new_fr_height -= STATUS_HEIGHT; @@ -1411,8 +1473,9 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) wp->w_winrow = oldwin->w_winrow; wp->w_status_height = STATUS_HEIGHT; oldwin->w_winrow += wp->w_height + STATUS_HEIGHT; - } else { /* new window below current one */ - wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT; + } else { // new window below current one + wp->w_winrow = oldwin->w_winrow + oldwin->w_height + + STATUS_HEIGHT + oldwin->w_winbar_height; wp->w_status_height = oldwin->w_status_height; if (!(flags & WSP_BOT)) { oldwin->w_status_height = STATUS_HEIGHT; @@ -1518,17 +1581,22 @@ static void win_init(win_T *newp, win_T *oldp, int flags) /* Don't copy the location list. */ newp->w_llist = NULL; newp->w_llist_ref = NULL; - } else - copy_loclist(oldp, newp); + } else { + copy_loclist_stack(oldp, newp); + } newp->w_localdir = (oldp->w_localdir == NULL) ? NULL : vim_strsave(oldp->w_localdir); /* copy tagstack and folds */ for (i = 0; i < oldp->w_tagstacklen; i++) { - newp->w_tagstack[i] = oldp->w_tagstack[i]; - if (newp->w_tagstack[i].tagname != NULL) - newp->w_tagstack[i].tagname = - vim_strsave(newp->w_tagstack[i].tagname); + taggy_T *tag = &newp->w_tagstack[i]; + *tag = oldp->w_tagstack[i]; + if (tag->tagname != NULL) { + tag->tagname = vim_strsave(tag->tagname); + } + if (tag->user_data != NULL) { + tag->user_data = vim_strsave(tag->user_data); + } } newp->w_tagstackidx = oldp->w_tagstackidx; newp->w_tagstacklen = oldp->w_tagstacklen; @@ -1558,7 +1626,7 @@ static void win_init_some(win_T *newp, win_T *oldp) /// Check if "win" is a pointer to an existing window in the current tabpage. /// /// @param win window to check -bool win_valid(win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +bool win_valid(const win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { if (win == NULL) { return false; @@ -1623,8 +1691,9 @@ make_windows ( maxcount = (curwin->w_width + curwin->w_vsep_width - (p_wiw - p_wmw)) / (p_wmw + 1); } else { - /* Each window needs at least 'winminheight' lines and a status line. */ - maxcount = (curwin->w_height + curwin->w_status_height + // Each window needs at least 'winminheight' lines and a status line. + maxcount = (curwin->w_height + curwin->w_winbar_height + + curwin->w_status_height - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT); } @@ -2401,6 +2470,7 @@ int win_close(win_T *win, bool free_buf) bool help_window = false; tabpage_T *prev_curtab = curtab; frame_T *win_frame = win->w_floating ? NULL : win->w_frame->fr_parent; + const bool had_diffmode = win->w_p_diff; if (last_window() && !win->w_floating) { EMSG(_("E444: Cannot close last window")); @@ -2470,9 +2540,10 @@ int win_close(win_T *win, bool free_buf) return FAIL; } win->w_closing = true; - apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf); - if (!win_valid(win)) + apply_autocmds(EVENT_WINLEAVE, NULL, NULL, false, curbuf); + if (!win_valid(win)) { return FAIL; + } win->w_closing = false; if (last_window()) return FAIL; @@ -2502,6 +2573,12 @@ int win_close(win_T *win, bool free_buf) } } + // Fire WinClosed just before starting to free window-related resources. + do_autocmd_winclosed(win); + // autocmd may have freed the window already. + if (!win_valid_any_tab(win)) { + return OK; + } /* Free independent synblock before the buffer is freed. */ if (win->w_buffer != NULL) @@ -2544,6 +2621,7 @@ int win_close(win_T *win, bool free_buf) win_close_othertab(win, false, prev_curtab); return FAIL; } + // Autocommands may have closed the window already, or closed the only // other window or moved to another tab page. if (!win_valid(win) || (!win->w_floating && last_window()) @@ -2553,8 +2631,9 @@ int win_close(win_T *win, bool free_buf) // let terminal buffers know that this window dimensions may be ignored win->w_closing = true; - /* Free the memory used for the window and get the window that received - * the screen space. */ + + // Free the memory used for the window and get the window that received + // the screen space. wp = win_free_mem(win, &dir, NULL); if (help_window) { @@ -2625,11 +2704,41 @@ int win_close(win_T *win, bool free_buf) if (help_window) restore_snapshot(SNAP_HELP_IDX, close_curwin); + // If the window had 'diff' set and now there is only one window left in + // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then + // execute ":diffoff!". + if (diffopt_closeoff() && had_diffmode && curtab == prev_curtab) { + int diffcount = 0; + + FOR_ALL_WINDOWS_IN_TAB(dwin, curtab) { + if (dwin->w_p_diff) { + diffcount++; + } + } + if (diffcount == 1) { + do_cmdline_cmd("diffoff!"); + } + } + curwin->w_pos_changed = true; redraw_all_later(NOT_VALID); return OK; } +static void do_autocmd_winclosed(win_T *win) + FUNC_ATTR_NONNULL_ALL +{ + static bool recursive = false; + if (recursive || !has_event(EVENT_WINCLOSED)) { + return; + } + recursive = true; + char_u winid[NUMBUFLEN]; + vim_snprintf((char *)winid, sizeof(winid), "%i", win->handle); + apply_autocmds(EVENT_WINCLOSED, winid, winid, false, win->w_buffer); + recursive = false; +} + /* * Close window "win" in tab page "tp", which is not the current tab page. * This may be the last window in that tab page and result in closing the tab, @@ -2650,6 +2759,13 @@ void win_close_othertab(win_T *win, int free_buf, tabpage_T *tp) return; // window is already being closed } + // Fire WinClosed just before starting to free window-related resources. + do_autocmd_winclosed(win); + // autocmd may have freed the window already. + if (!win_valid_any_tab(win)) { + return; + } + if (win->w_buffer != NULL) { // Close the link to the buffer. close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, false); @@ -3031,15 +3147,18 @@ frame_new_height ( int wfh /* obey 'winfixheight' when there is a choice; may cause the height not to be set */ ) + FUNC_ATTR_NONNULL_ALL { frame_T *frp; int extra_lines; int h; if (topfrp->fr_win != NULL) { - /* Simple case: just one window. */ + // Simple case: just one window. win_new_height(topfrp->fr_win, - height - topfrp->fr_win->w_status_height); + height + - topfrp->fr_win->w_status_height + - topfrp->fr_win->w_winbar_height); } else if (topfrp->fr_layout == FR_ROW) { do { // All frames in this row get the same new height. @@ -3344,8 +3463,10 @@ static void frame_fix_width(win_T *wp) * Set frame height from the window it contains. */ static void frame_fix_height(win_T *wp) + FUNC_ATTR_NONNULL_ALL { - wp->w_frame->fr_height = wp->w_height + wp->w_status_height; + wp->w_frame->fr_height = + wp->w_height + wp->w_status_height + wp->w_winbar_height; } /* @@ -3362,14 +3483,18 @@ static int frame_minheight(frame_T *topfrp, win_T *next_curwin) int n; if (topfrp->fr_win != NULL) { - if (topfrp->fr_win == next_curwin) + if (topfrp->fr_win == next_curwin) { m = p_wh + topfrp->fr_win->w_status_height; - else { - /* window: minimal height of the window plus status line */ + } else { + // window: minimal height of the window plus status line m = p_wmh + topfrp->fr_win->w_status_height; - /* Current window is minimal one line high */ - if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL) - ++m; + if (topfrp->fr_win == curwin && next_curwin == NULL) { + // Current window is minimal one line high and WinBar is visible. + if (p_wmh == 0) { + m++; + } + m += curwin->w_winbar_height; + } } } else if (topfrp->fr_layout == FR_ROW) { /* get the minimal height from each frame in this row */ @@ -3651,6 +3776,10 @@ void free_tabpage(tabpage_T *tp) hash_init(&tp->tp_vars->dv_hashtab); unref_var_dict(tp->tp_vars); + if (tp == lastused_tabpage) { + lastused_tabpage = NULL; + } + xfree(tp->tp_localdir); xfree(tp); } @@ -3710,6 +3839,8 @@ int win_new_tabpage(int after, char_u *filename) tabpage_check_windows(tp); + lastused_tabpage = tp; + apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf); apply_autocmds(EVENT_WINENTER, NULL, NULL, false, curbuf); apply_autocmds(EVENT_TABNEW, filename, filename, false, curbuf); @@ -3936,6 +4067,7 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_au if (curtab->tp_old_Columns != Columns && starting == 0) shell_new_columns(); /* update window widths */ + lastused_tabpage = old_curtab; /* Apply autocommands after updating the display, when 'rows' and * 'columns' have been set correctly. */ @@ -4055,6 +4187,15 @@ void goto_tabpage_tp(tabpage_T *tp, int trigger_enter_autocmds, int trigger_leav } } +// Go to the last accessed tab page, if there is one. +void goto_tabpage_lastused(void) +{ + int index = tabpage_index(lastused_tabpage); + if (index < tabpage_index(NULL)) { + goto_tabpage(index); + } +} + /* * Enter window "wp" in tab page "tp". * Also updates the GUI tab. @@ -4332,9 +4473,10 @@ static void win_goto_hor(bool left, long count) } } -/* - * Make window "wp" the current window. - */ +/// Make window `wp` the current window. +/// +/// @warning Autocmds may close the window immediately, so caller must check +/// win_valid(wp). void win_enter(win_T *wp, bool undo_sync) { win_enter_ext(wp, undo_sync, false, false, true, true); @@ -4573,6 +4715,11 @@ static win_T *win_alloc(win_T *after, int hidden) new_wp->w_scbind_pos = 1; new_wp->w_floating = 0; new_wp->w_float_config = FLOAT_CONFIG_INIT; + new_wp->w_viewport_invalid = true; + + // use global option for global-local options + new_wp->w_p_so = -1; + new_wp->w_p_siso = -1; /* We won't calculate w_fraction until resizing the window */ new_wp->w_fraction = 0; @@ -4626,8 +4773,10 @@ win_free ( xfree(wp->w_lines); - for (i = 0; i < wp->w_tagstacklen; ++i) + for (i = 0; i < wp->w_tagstacklen; i++) { xfree(wp->w_tagstack[i].tagname); + xfree(wp->w_tagstack[i].user_data); + } xfree(wp->w_localdir); @@ -4645,6 +4794,7 @@ win_free ( qf_free_all(wp); + remove_winbar(wp); xfree(wp->w_p_cc_cols); @@ -4911,7 +5061,9 @@ static void frame_comp_pos(frame_T *topfrp, int *row, int *col) wp->w_redr_status = true; wp->w_pos_changed = true; } - *row += wp->w_height + wp->w_status_height; + // WinBar will not show if the window height is zero + const int h = wp->w_height + wp->w_winbar_height + wp->w_status_height; + *row += h > topfrp->fr_height ? topfrp->fr_height : h; *col += wp->w_width + wp->w_vsep_width; } else { startrow = *row; @@ -4944,12 +5096,15 @@ void win_setheight(int height) void win_setheight_win(int height, win_T *win) { if (win == curwin) { - /* Always keep current window at least one line high, even when - * 'winminheight' is zero. */ - if (height < p_wmh) + // Always keep current window at least one line high, even when + // 'winminheight' is zero. + if (height < p_wmh) { height = p_wmh; - if (height == 0) + } + if (height == 0) { height = 1; + } + height += curwin->w_winbar_height; } if (win->w_floating) { @@ -5046,7 +5201,7 @@ static void frame_setheight(frame_T *curfrp, int height) } else { win_T *wp = lastwin_nofloating(); room_cmdline = Rows - p_ch - (wp->w_winrow - + wp->w_height + + + wp->w_height + wp->w_winbar_height + wp->w_status_height); if (room_cmdline < 0) { room_cmdline = 0; @@ -5571,11 +5726,10 @@ void set_fraction(win_T *wp) } } -/* - * Set the height of a window. - * This takes care of the things inside the window, not what happens to the - * window position, the frame or to other windows. - */ +// Set the height of a window. +// "height" excludes any window toolbar. +// This takes care of the things inside the window, not what happens to the +// window position, the frame or to other windows. void win_new_height(win_T *wp, int height) { // Don't want a negative height. Happens when splitting a tiny window. @@ -5598,10 +5752,14 @@ void scroll_to_fraction(win_T *wp, int prev_height) int sline, line_size; int height = wp->w_height_inner; - // Don't change w_topline when height is zero. Don't set w_topline when - // 'scrollbind' is set and this isn't the current window. + // Don't change w_topline in any of these cases: + // - window height is 0 + // - 'scrollbind' is set and this isn't the current window + // - window height is sufficient to display the whole buffer and first line + // is visible. if (height > 0 && (!wp->w_p_scb || wp == curwin) + && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1) ) { /* * Find a value for w_topline that shows the cursor at the same @@ -5678,9 +5836,10 @@ void scroll_to_fraction(win_T *wp, int prev_height) } if (wp == curwin) { - if (p_so) + if (get_scrolloff_value()) { update_topline(); - curs_columns(FALSE); /* validate w_wrow */ + } + curs_columns(false); // validate w_wrow } if (prev_height > 0) { wp->w_prev_fraction_row = wp->w_wrow; @@ -5973,10 +6132,20 @@ file_name_in_line ( if (file_lnum != NULL) { char_u *p; + const char *line_english = " line "; + const char *line_transl = _(line_msg); // Get the number after the file name and a separator character. + // Also accept " line 999" with and without the same translation as + // used in last_set_msg(). p = ptr + len; - p = skipwhite(p); + if (STRNCMP(p, line_english, STRLEN(line_english)) == 0) { + p += STRLEN(line_english); + } else if (STRNCMP(p, line_transl, STRLEN(line_transl)) == 0) { + p += STRLEN(line_transl); + } else { + p = skipwhite(p); + } if (*p != NUL) { if (!isdigit(*p)) { p++; // skip the separator @@ -6392,10 +6561,12 @@ void restore_buffer(bufref_T *save_curbuf) /// @param[in] id a desired ID 'id' can be specified /// (greater than or equal to 1). -1 must be specified if no /// particular ID is desired +/// @param[in] conceal_char pointer to conceal replacement char /// @return ID of added match, -1 on failure. int match_add(win_T *wp, const char *const grp, const char *const pat, int prio, int id, list_T *pos_list, const char *const conceal_char) + FUNC_ATTR_NONNULL_ARG(1, 2) { matchitem_T *cur; matchitem_T *prev; @@ -6432,7 +6603,7 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, return -1; } - /* Find available match ID. */ + // Find available match ID. while (id == -1) { cur = wp->w_match_head; while (cur != NULL && cur->id != wp->w_next_match_id) @@ -6442,7 +6613,7 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, wp->w_next_match_id++; } - /* Build new match. */ + // Build new match. m = xcalloc(1, sizeof(matchitem_T)); m->id = id; m->priority = prio; @@ -6550,9 +6721,9 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, rtype = VALID; } } - - /* Insert new match. The match list is in ascending order with regard to - * the match priorities. */ + + // Insert new match. The match list is in ascending order with regard to + // the match priorities. cur = wp->w_match_head; prev = cur; while (cur != NULL && prio >= cur->priority) { @@ -6565,7 +6736,7 @@ int match_add(win_T *wp, const char *const grp, const char *const pat, prev->next = m; m->next = cur; - redraw_later(rtype); + redraw_win_later(wp, rtype); return id; fail: @@ -6623,7 +6794,7 @@ int match_delete(win_T *wp, int id, int perr) rtype = VALID; } xfree(cur); - redraw_later(rtype); + redraw_win_later(wp, rtype); return 0; } @@ -6641,7 +6812,7 @@ void clear_matches(win_T *wp) xfree(wp->w_match_head); wp->w_match_head = m; } - redraw_later(SOME_VALID); + redraw_win_later(wp, SOME_VALID); } /* @@ -6856,7 +7027,7 @@ void get_framelayout(const frame_T *fr, list_T *l, bool outer) } } -void win_ui_flush_positions(void) +void win_ui_flush(void) { FOR_ALL_TAB_WINDOWS(tp, wp) { if (wp->w_pos_changed && wp->w_grid.chars != NULL) { @@ -6867,6 +7038,9 @@ void win_ui_flush_positions(void) } wp->w_pos_changed = false; } + if (tp == curtab) { + ui_ext_win_viewport(wp); + } } } |