diff options
Diffstat (limited to 'src/nvim/window.c')
-rw-r--r-- | src/nvim/window.c | 317 |
1 files changed, 253 insertions, 64 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index bea55c465f..801969a594 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -28,7 +28,6 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/misc1.h" -#include "nvim/misc2.h" #include "nvim/file_search.h" #include "nvim/garray.h" #include "nvim/move.h" @@ -41,6 +40,7 @@ #include "nvim/regexp.h" #include "nvim/screen.h" #include "nvim/search.h" +#include "nvim/state.h" #include "nvim/strings.h" #include "nvim/syntax.h" #include "nvim/terminal.h" @@ -129,9 +129,10 @@ newwindow: vim_snprintf(cbuf, sizeof(cbuf) - 5, "%" PRId64, (int64_t)Prenum); else cbuf[0] = NUL; - if (nchar == 'v' || nchar == Ctrl_V) - strcat(cbuf, "v"); - strcat(cbuf, "new"); + if (nchar == 'v' || nchar == Ctrl_V) { + xstrlcat(cbuf, "v", sizeof(cbuf)); + } + xstrlcat(cbuf, "new", sizeof(cbuf)); do_cmdline_cmd(cbuf); break; @@ -251,11 +252,14 @@ newwindow: if (win_new_tabpage((int)Prenum, NULL) == OK && valid_tabpage(oldtab)) { newtab = curtab; - goto_tabpage_tp(oldtab, TRUE, TRUE); - if (curwin == wp) - win_close(curwin, FALSE); - if (valid_tabpage(newtab)) - goto_tabpage_tp(newtab, TRUE, TRUE); + goto_tabpage_tp(oldtab, true, true); + if (curwin == wp) { + win_close(curwin, false); + } + if (valid_tabpage(newtab)) { + goto_tabpage_tp(newtab, true, true); + apply_autocmds(EVENT_TABNEWENTERED, NULL, NULL, false, curbuf); + } } } break; @@ -275,10 +279,11 @@ newwindow: /* cursor to last accessed (previous) window */ case 'p': case Ctrl_P: - if (prevwin == NULL) + if (!win_valid(prevwin)) { beep_flush(); - else + } else { win_goto(prevwin); + } break; /* exchange current and next window */ @@ -381,12 +386,16 @@ wingotofile: ptr = grab_file_name(Prenum1, &lnum); if (ptr != NULL) { + tabpage_T *oldtab = curtab; + win_T *oldwin = curwin; setpcmark(); if (win_split(0, 0) == OK) { RESET_BINDING(curwin); - (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, - ECMD_HIDE, NULL); - if (nchar == 'F' && lnum >= 0) { + if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) { + // Failed to open the file, close the window opened for it. + win_close(curwin, false); + goto_tabpage_win(oldtab, oldwin); + } else if (nchar == 'F' && lnum >= 0) { curwin->w_cursor.lnum = lnum; check_cursor_lnum(); beginline(BL_SOL | BL_FIX); @@ -479,7 +488,7 @@ wingotofile: } static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, - long Prenum) + int64_t Prenum) { size_t len = xstrlcpy((char *)bufp, cmd, bufsize); @@ -890,31 +899,31 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) /* "new_size" of the current window goes to the new window, use * one row for the status line */ win_new_height(wp, new_size); - if (flags & (WSP_TOP | WSP_BOT)) - frame_new_height(curfrp, curfrp->fr_height - - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE); - else + if (flags & (WSP_TOP | WSP_BOT)) { + int new_fr_height = curfrp->fr_height - new_size; + + if (!((flags & WSP_BOT) && p_ls == 0)) { + new_fr_height -= STATUS_HEIGHT; + } + frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, false); + } else { win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT)); - if (before) { /* new window above current one */ + } + if (before) { // new window above current one 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; wp->w_status_height = oldwin->w_status_height; - // Don't set the status_height for oldwin yet, this might break - // frame_fix_height(oldwin), therefore will be set below. + if (!(flags & WSP_BOT)) { + oldwin->w_status_height = STATUS_HEIGHT; + } } if (flags & WSP_BOT) frame_add_statusline(curfrp); frame_fix_height(wp); frame_fix_height(oldwin); - - if (!before) { - // New window above current one, set the status_height after - // frame_fix_height(oldwin) - oldwin->w_status_height = STATUS_HEIGHT; - } } if (flags & (WSP_TOP | WSP_BOT)) @@ -965,11 +974,12 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) /* * make the new window the current window */ - win_enter(wp, false); - if (flags & WSP_VERT) + win_enter_ext(wp, false, false, true, true, true); + if (flags & WSP_VERT) { p_wiw = i; - else + } else { p_wh = i; + } return OK; } @@ -1061,6 +1071,23 @@ bool win_valid(win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT return false; } +/// Check if "win" is a pointer to an existing window in any tabpage. +/// +/// @param win window to check +bool win_valid_any_tab(win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT +{ + if (win == NULL) { + return false; + } + + FOR_ALL_TAB_WINDOWS(tp, wp) { + if (wp == win) { + return true; + } + } + return false; +} + /* * Return the number of windows. */ @@ -1693,6 +1720,7 @@ close_windows ( { tabpage_T *tp, *nexttp; int h = tabline_height(); + int count = tabpage_index(NULL); ++RedrawingDisabled; @@ -1729,9 +1757,14 @@ close_windows ( --RedrawingDisabled; - redraw_tabline = TRUE; - if (h != tabline_height()) + if (count != tabpage_index(NULL)) { + apply_autocmds(EVENT_TABCLOSED, NULL, NULL, false, curbuf); + } + + redraw_tabline = true; + if (h != tabline_height()) { shell_new_rows(); + } } /// Check that current window is the last one. @@ -1914,7 +1947,7 @@ int win_close(win_T *win, int free_buf) if (win->w_buffer != NULL) { win->w_closing = true; close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, true); - if (win_valid(win)) { + if (win_valid_any_tab(win)) { win->w_closing = false; } @@ -1934,11 +1967,19 @@ int win_close(win_T *win, int free_buf) curwin->w_buffer = curbuf; getout(0); } - /* Autocommands may have closed the window already, or closed the only - * other window or moved to another tab page. */ - else if (!win_valid(win) || last_window() || curtab != prev_curtab - || close_last_window_tabpage(win, free_buf, prev_curtab)) + // Autocommands may have moved to another tab page. + if (curtab != prev_curtab && win_valid_any_tab(win) + && win->w_buffer == NULL) { + // Need to close the window anyway, since the buffer is NULL. + 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) || last_window() + || close_last_window_tabpage(win, free_buf, prev_curtab)) { + return FAIL; + } // let terminal buffers know that this window dimensions may be ignored win->w_closing = true; @@ -1981,10 +2022,11 @@ int win_close(win_T *win, int free_buf) } if (close_curwin) { - win_enter_ext(wp, false, TRUE, TRUE, TRUE); - if (other_buffer) - /* careful: after this wp and win may be invalid! */ - apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); + win_enter_ext(wp, false, true, false, true, true); + if (other_buffer) { + // careful: after this wp and win may be invalid! + apply_autocmds(EVENT_BUFENTER, NULL, NULL, false, curbuf); + } } /* @@ -2015,12 +2057,16 @@ void win_close_othertab(win_T *win, int free_buf, tabpage_T *tp) tabpage_T *ptp = NULL; int free_tp = FALSE; - assert(win->w_buffer); // to avoid np dereference warning in next line - if (win->w_closing || win->w_buffer->b_closing) - return; /* window is already being closed */ + // Get here with win->w_buffer == NULL when win_close() detects the tab page + // changed. + if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing)) { + return; // window is already being closed + } - /* Close the link to the buffer. */ - close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE); + if (win->w_buffer != NULL) { + // Close the link to the buffer. + close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, false); + } /* Careful: Autocommands may have closed the tab page or made it the * current tab page. */ @@ -2871,8 +2917,9 @@ static int win_alloc_firstwin(win_T *oldwin) /* Very first window, need to create an empty buffer for it and * initialize from scratch. */ curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED); - if (curbuf == NULL) + if (curbuf == NULL) { return FAIL; + } curwin->w_buffer = curbuf; curwin->w_s = &(curbuf->b_s); curbuf->b_nwindows = 1; /* there is one window */ @@ -2923,7 +2970,9 @@ void win_init_size(void) */ static tabpage_T *alloc_tabpage(void) { + static int last_tp_handle = 0; tabpage_T *tp = xcalloc(1, sizeof(tabpage_T)); + tp->handle = ++last_tp_handle; handle_register_tabpage(tp); /* init t: variables */ @@ -2973,6 +3022,9 @@ int win_new_tabpage(int after, char_u *filename) xfree(newtp); return FAIL; } + + newtp->localdir = tp->localdir ? vim_strsave(tp->localdir) : NULL; + curtab = newtp; /* Create a new empty window. */ @@ -3002,8 +3054,9 @@ int win_new_tabpage(int after, char_u *filename) redraw_all_later(CLEAR); - apply_autocmds(EVENT_TABNEW, filename, filename, false, curbuf); + apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf); apply_autocmds(EVENT_WINENTER, NULL, NULL, false, curbuf); + apply_autocmds(EVENT_TABNEW, filename, filename, false, curbuf); apply_autocmds(EVENT_TABENTER, NULL, NULL, false, curbuf); return OK; @@ -3161,8 +3214,8 @@ static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_au /* We would like doing the TabEnter event first, but we don't have a * valid current window yet, which may break some commands. * This triggers autocommands, thus may make "tp" invalid. */ - win_enter_ext(tp->tp_curwin, false, TRUE, - trigger_enter_autocmds, trigger_leave_autocmds); + win_enter_ext(tp->tp_curwin, false, true, false, + trigger_enter_autocmds, trigger_leave_autocmds); prevwin = next_prevwin; last_status(FALSE); /* status line may appear or disappear */ @@ -3204,11 +3257,8 @@ void goto_tabpage(int n) int i; if (text_locked()) { - /* Not allowed when editing the command line. */ - if (cmdwin_type != 0) - EMSG(_(e_cmdwin)); - else - EMSG(_(e_secure)); + // Not allowed when editing the command line. + text_locked_msg(); return; } @@ -3293,8 +3343,11 @@ void tabpage_move(int nr) tabpage_T *tp; tabpage_T *tp_dst; - if (first_tabpage->tp_next == NULL) + assert(curtab != NULL); + + if (first_tabpage->tp_next == NULL) { return; + } for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next) { ++n; @@ -3503,7 +3556,7 @@ end: */ void win_enter(win_T *wp, bool undo_sync) { - win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE); + win_enter_ext(wp, undo_sync, false, false, true, true); } /* @@ -3511,7 +3564,9 @@ void win_enter(win_T *wp, bool undo_sync) * Can be called with "curwin_invalid" TRUE, which means that curwin has just * been closed and isn't valid. */ -static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid, int trigger_enter_autocmds, int trigger_leave_autocmds) +static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid, + int trigger_new_autocmds, int trigger_enter_autocmds, + int trigger_leave_autocmds) { int other_buffer = FALSE; @@ -3587,6 +3642,9 @@ static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid, int tri shorten_fnames(TRUE); } + if (trigger_new_autocmds) { + apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf); + } if (trigger_enter_autocmds) { apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf); if (other_buffer) @@ -3613,6 +3671,10 @@ static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid, int tri /* Change directories when the 'acd' option is set. */ do_autochdir(); + + if (curbuf->terminal) { + terminal_resize(curbuf->terminal, curwin->w_width, curwin->w_height); + } } @@ -3679,13 +3741,15 @@ win_T *buf_jump_open_tab(buf_T *buf) */ static win_T *win_alloc(win_T *after, int hidden) { - /* - * allocate window structure and linesizes arrays - */ + static int last_win_id = LOWEST_WIN_ID - 1; + + // allocate window structure and linesizes arrays win_T *new_wp = xcalloc(1, sizeof(win_T)); - handle_register_window(new_wp); win_alloc_lines(new_wp); + new_wp->handle = ++last_win_id; + handle_register_window(new_wp); + /* init w: variables */ new_wp->w_vars = dict_alloc(); init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE); @@ -3751,8 +3815,15 @@ win_free ( hash_init(&wp->w_vars->dv_hashtab); unref_var_dict(wp->w_vars); - if (prevwin == wp) + if (prevwin == wp) { prevwin = NULL; + } + FOR_ALL_TABS(ttp) { + if (ttp->tp_prevwin == wp) { + ttp->tp_prevwin = NULL; + } + } + win_free_lsize(wp); for (i = 0; i < wp->w_tagstacklen; ++i) @@ -5664,3 +5735,121 @@ static bool frame_check_width(frame_T *topfrp, int width) } return true; } + +int win_getid(typval_T *argvars) +{ + if (argvars[0].v_type == VAR_UNKNOWN) { + return curwin->handle; + } + int winnr = get_tv_number(&argvars[0]); + win_T *wp; + if (winnr > 0) { + if (argvars[1].v_type == VAR_UNKNOWN) { + wp = firstwin; + } else { + tabpage_T *tp; + int tabnr = get_tv_number(&argvars[1]); + for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) { + if (--tabnr == 0) { + break; + } + } + if (tp == NULL) { + return -1; + } + wp = tp->tp_firstwin; + } + for ( ; wp != NULL; wp = wp->w_next) { + if (--winnr == 0) { + return wp->handle; + } + } + } + return 0; +} + +int win_gotoid(typval_T *argvars) +{ + int id = get_tv_number(&argvars[0]); + + FOR_ALL_TAB_WINDOWS(tp, wp) { + if (wp->handle == id) { + goto_tabpage_win(tp, wp); + return 1; + } + } + return 0; +} + +void win_get_tabwin(handle_T id, int *tabnr, int *winnr) +{ + *tabnr = 0; + *winnr = 0; + + int tnum = 1, wnum = 1; + FOR_ALL_TABS(tp) { + FOR_ALL_WINDOWS_IN_TAB(wp, tp) { + if (wp->handle == id) { + *winnr = wnum; + *tabnr = tnum; + return; + } + wnum++; + } + tnum++; + wnum = 1; + } +} + +void win_id2tabwin(typval_T *argvars, list_T *list) +{ + int winnr = 1; + int tabnr = 1; + int id = get_tv_number(&argvars[0]); + + win_get_tabwin(id, &tabnr, &winnr); + list_append_number(list, tabnr); + list_append_number(list, winnr); +} + +win_T * win_id2wp(typval_T *argvars) +{ + int id = get_tv_number(&argvars[0]); + + FOR_ALL_TAB_WINDOWS(tp, wp) { + if (wp->handle == id) { + return wp; + } + } + + return NULL; +} + +int win_id2win(typval_T *argvars) +{ + win_T *wp; + int nr = 1; + int id = get_tv_number(&argvars[0]); + + for (wp = firstwin; wp != NULL; wp = wp->w_next) { + if (wp->handle == id) { + return nr; + } + nr++; + } + return 0; +} + +void win_findbuf(typval_T *argvars, list_T *list) +{ + int bufnr = get_tv_number(&argvars[0]); + + for (tabpage_T *tp = first_tabpage; tp != NULL; tp = tp->tp_next) { + for (win_T *wp = tp == curtab ? firstwin : tp->tp_firstwin; + wp != NULL; wp = wp->w_next) { + if (wp->w_buffer->b_fnum == bufnr) { + list_append_number(list, wp->handle); + } + } + } +} |