diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/api/vim.c | 2 | ||||
-rw-r--r-- | src/nvim/api/win_config.c | 2 | ||||
-rw-r--r-- | src/nvim/api/window.c | 2 | ||||
-rw-r--r-- | src/nvim/buffer.c | 12 | ||||
-rw-r--r-- | src/nvim/drawline.c | 4 | ||||
-rw-r--r-- | src/nvim/eval.lua | 2 | ||||
-rw-r--r-- | src/nvim/eval/buffer.c | 1 | ||||
-rw-r--r-- | src/nvim/eval/window.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 13 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 53 | ||||
-rw-r--r-- | src/nvim/globals.h | 2 | ||||
-rw-r--r-- | src/nvim/mouse.c | 6 | ||||
-rw-r--r-- | src/nvim/move.c | 2 | ||||
-rw-r--r-- | src/nvim/textformat.c | 2 | ||||
-rw-r--r-- | src/nvim/window.c | 5 |
15 files changed, 85 insertions, 25 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index eea9b54a5c..4bd8f551db 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -1022,7 +1022,7 @@ Integer nvim_open_term(Buffer buffer, Dict(open_term) *opts, Error *err) return 0; } - if (cmdwin_type != 0 && buf == curbuf) { + if (buf == cmdwin_buf) { api_set_error(err, kErrorTypeException, "%s", e_cmdwin); return 0; } diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index ccbd341fd2..6df86683c1 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -181,7 +181,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E if (!buf) { return 0; } - if (cmdwin_type != 0 && (enter || buf == curbuf)) { + if ((cmdwin_type != 0 && enter) || buf == cmdwin_buf) { api_set_error(err, kErrorTypeException, "%s", e_cmdwin); return 0; } diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 00126c64f1..4ac7e47832 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -61,7 +61,7 @@ void nvim_win_set_buf(Window window, Buffer buffer, Error *err) if (!win || !buf) { return; } - if (cmdwin_type != 0 && (win == curwin || win == cmdwin_old_curwin || buf == curbuf)) { + if (win == cmdwin_win || win == cmdwin_old_curwin || buf == cmdwin_buf) { api_set_error(err, kErrorTypeException, "%s", e_cmdwin); return; } diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index da1e27aebf..d597d82ee2 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -4012,6 +4012,9 @@ char *buf_spname(buf_T *buf) if (buf->b_fname != NULL) { return buf->b_fname; } + if (buf == cmdwin_buf) { + return _("[Command Line]"); + } if (bt_prompt(buf)) { return _("[Prompt]"); } @@ -4129,6 +4132,7 @@ void wipe_buffer(buf_T *buf, bool aucmd) /// - Always considered 'nomodified' /// /// @param bufnr Buffer to switch to, or 0 to create a new buffer. +/// @param bufname Buffer name, or NULL. /// /// @see curbufIsChanged() /// @@ -4138,9 +4142,11 @@ int buf_open_scratch(handle_T bufnr, char *bufname) if (do_ecmd((int)bufnr, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL) == FAIL) { return FAIL; } - apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, false, curbuf); - setfname(curbuf, bufname, NULL, true); - apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); + if (bufname != NULL) { + apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, false, curbuf); + setfname(curbuf, bufname, NULL, true); + apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, false, curbuf); + } set_option_value_give_err(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("hide"), OPT_LOCAL); set_option_value_give_err(kOptBuftype, STATIC_CSTR_AS_OPTVAL("nofile"), OPT_LOCAL); set_option_value_give_err(kOptSwapfile, BOOLEAN_OPTVAL(false), OPT_LOCAL); diff --git a/src/nvim/drawline.c b/src/nvim/drawline.c index 0dd8d6398b..0b88c307c7 100644 --- a/src/nvim/drawline.c +++ b/src/nvim/drawline.c @@ -1219,7 +1219,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s statuscol.draw = true; statuscol.sattrs = wlv.sattrs; statuscol.foldinfo = foldinfo; - statuscol.width = win_col_off(wp) - (cmdwin_type != 0 && wp == curwin); + statuscol.width = win_col_off(wp) - (wp == cmdwin_win); statuscol.use_cul = use_cursor_line_highlight(wp, lnum); statuscol.sign_cul_id = statuscol.use_cul ? sign_cul_attr : 0; statuscol.num_attr = sign_num_attr > 0 ? syn_id2attr(sign_num_attr) : 0; @@ -1511,7 +1511,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s assert(wlv.off == 0); - if (cmdwin_type != 0 && wp == curwin) { + if (wp == cmdwin_win) { // Draw the cmdline character. draw_col_fill(&wlv, schar_from_ascii(cmdwin_type), 1, win_hl_attr(wp, HLF_AT)); } diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 9f50fd1758..bc976eaa58 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -3219,6 +3219,8 @@ M.funcs = { bufnr Buffer number. changed TRUE if the buffer is modified. changedtick Number of changes made to the buffer. + command TRUE if the buffer belongs to the + command-line window |cmdwin|. hidden TRUE if the buffer is hidden. lastused Timestamp in seconds, like |localtime()|, when the buffer was diff --git a/src/nvim/eval/buffer.c b/src/nvim/eval/buffer.c index c43ef50410..7b8f71ef3f 100644 --- a/src/nvim/eval/buffer.c +++ b/src/nvim/eval/buffer.c @@ -494,6 +494,7 @@ static dict_T *get_buffer_info(buf_T *buf) tv_dict_add_nr(dict, S_LEN("changed"), bufIsChanged(buf)); tv_dict_add_nr(dict, S_LEN("changedtick"), buf_get_changedtick(buf)); tv_dict_add_nr(dict, S_LEN("hidden"), buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0); + tv_dict_add_nr(dict, S_LEN("command"), buf == cmdwin_buf); // Get a reference to buffer variables tv_dict_add_dict(dict, S_LEN("variables"), buf->b_vars); diff --git a/src/nvim/eval/window.c b/src/nvim/eval/window.c index b621cec5f4..b8aa0c9641 100644 --- a/src/nvim/eval/window.c +++ b/src/nvim/eval/window.c @@ -755,7 +755,7 @@ void f_win_gettype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_string = xstrdup("preview"); } else if (wp->w_floating) { rettv->vval.v_string = xstrdup("popup"); - } else if (wp == curwin && cmdwin_type != 0) { + } else if (wp == cmdwin_win) { rettv->vval.v_string = xstrdup("command"); } else if (bt_quickfix(wp->w_buffer)) { rettv->vval.v_string = xstrdup((wp->w_llist_ref != NULL ? "loclist" : "quickfix")); diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 373ff8feda..41d8e5d914 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -2305,10 +2305,19 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum // If the current buffer was empty and has no file name, curbuf // is returned by buflist_new(), nothing to do here. if (buf != curbuf) { + // Should only be possible to get here if the cmdwin is closed, or + // if it's opening and its buffer hasn't been set yet (the new + // buffer is for it). + assert(cmdwin_buf == NULL); + const int save_cmdwin_type = cmdwin_type; + win_T *const save_cmdwin_win = cmdwin_win; + win_T *const save_cmdwin_old_curwin = cmdwin_old_curwin; // BufLeave applies to the old buffer. cmdwin_type = 0; + cmdwin_win = NULL; + cmdwin_old_curwin = NULL; // Be careful: The autocommands may delete any buffer and change // the current buffer. @@ -2324,7 +2333,11 @@ int do_ecmd(int fnum, char *ffname, char *sfname, exarg_T *eap, linenr_T newlnum const bufref_T save_au_new_curbuf = au_new_curbuf; set_bufref(&au_new_curbuf, buf); apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, false, curbuf); + cmdwin_type = save_cmdwin_type; + cmdwin_win = save_cmdwin_win; + cmdwin_old_curwin = save_cmdwin_old_curwin; + if (!bufref_valid(&au_new_curbuf)) { // New buffer has been deleted. delbuf_msg(new_name); // Frees new_name. diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 2c5c291216..307fd480cc 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -219,6 +219,9 @@ static int cedit_key = -1; ///< key value of 'cedit' option static handle_T cmdpreview_bufnr = 0; static int cmdpreview_ns = 0; +static const char e_active_window_or_buffer_changed_or_deleted[] + = N_("E199: Active window or buffer changed or deleted"); + static void save_viewstate(win_T *wp, viewstate_T *vs) FUNC_ATTR_NONNULL_ALL { @@ -4323,23 +4326,50 @@ static int open_cmdwin(void) ga_clear(&winsizes); return K_IGNORE; } + // win_split() autocommands may have messed with the old window or buffer. + // Treat it as abandoning this command-line. + if (!win_valid(old_curwin) || curwin == old_curwin || !bufref_valid(&old_curbuf) + || old_curwin->w_buffer != old_curbuf.br_buf) { + beep_flush(); + ga_clear(&winsizes); + return Ctrl_C; + } // Don't let quitting the More prompt make this fail. got_int = false; - // Set "cmdwin_type" before any autocommands may mess things up. + // Set "cmdwin_..." variables before any autocommands may mess things up. cmdwin_type = get_cmdline_type(); cmdwin_level = ccline.level; + cmdwin_win = curwin; cmdwin_old_curwin = old_curwin; - // Create empty command-line buffer. - if (buf_open_scratch(0, _("[Command Line]")) == FAIL) { - // Some autocommand messed it up? - win_close(curwin, true, false); - ga_clear(&winsizes); + // Create empty command-line buffer. Be especially cautious of BufLeave + // autocommands from do_ecmd(), as cmdwin restrictions do not apply to them! + const int newbuf_status = buf_open_scratch(0, NULL); + const bool cmdwin_valid = win_valid(cmdwin_win); + if (newbuf_status == FAIL || !cmdwin_valid || curwin != cmdwin_win || !win_valid(old_curwin) + || !bufref_valid(&old_curbuf) || old_curwin->w_buffer != old_curbuf.br_buf) { + if (newbuf_status == OK) { + set_bufref(&bufref, curbuf); + } + if (cmdwin_valid && !last_window(cmdwin_win)) { + win_close(cmdwin_win, true, false); + } + // win_close() autocommands may have already deleted the buffer. + if (newbuf_status == OK && bufref_valid(&bufref) && bufref.br_buf != curbuf) { + close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, false, false); + } + cmdwin_type = 0; + cmdwin_level = 0; + cmdwin_win = NULL; cmdwin_old_curwin = NULL; + beep_flush(); + ga_clear(&winsizes); return Ctrl_C; } + cmdwin_buf = curbuf; + // Command-line buffer has bufhidden=wipe, unlike a true "scratch" buffer. set_option_value_give_err(kOptBufhidden, STATIC_CSTR_AS_OPTVAL("wipe"), OPT_LOCAL); curbuf->b_p_ma = true; @@ -4434,15 +4464,18 @@ static int open_cmdwin(void) cmdwin_type = 0; cmdwin_level = 0; + cmdwin_buf = NULL; + cmdwin_win = NULL; cmdwin_old_curwin = NULL; exmode_active = save_exmode; - // Safety check: The old window or buffer was deleted: It's a bug when - // this happens! - if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) { + // Safety check: The old window or buffer was changed or deleted: It's a bug + // when this happens! + if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf) + || old_curwin->w_buffer != old_curbuf.br_buf) { cmdwin_result = Ctrl_C; - emsg(_("E199: Active window or buffer deleted")); + emsg(_(e_active_window_or_buffer_changed_or_deleted)); } else { win_T *wp; // autocmds may abort script processing diff --git a/src/nvim/globals.h b/src/nvim/globals.h index f0cc9dba74..a06e9fe542 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -752,6 +752,8 @@ EXTERN bool km_startsel INIT( = false); EXTERN int cmdwin_type INIT( = 0); ///< type of cmdline window or 0 EXTERN int cmdwin_result INIT( = 0); ///< result of cmdline window or 0 EXTERN int cmdwin_level INIT( = 0); ///< cmdline recursion level +EXTERN buf_T *cmdwin_buf INIT( = NULL); ///< buffer of cmdline window or NULL +EXTERN win_T *cmdwin_win INIT( = NULL); ///< window of cmdline window or NULL EXTERN win_T *cmdwin_old_curwin INIT( = NULL); ///< curwin before opening cmdline window or NULL EXTERN char no_lines_msg[] INIT( = N_("--No lines in buffer--")); diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index b3f651296f..c0a928416d 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -1326,18 +1326,18 @@ retnomove: && !sep_line_offset && (wp->w_p_rl ? col < wp->w_width_inner - fdc - : col >= fdc + (cmdwin_type == 0 && wp == curwin ? 0 : 1)) + : col >= fdc + (wp != cmdwin_win ? 0 : 1)) && (flags & MOUSE_MAY_STOP_VIS)))) { end_visual_mode(); redraw_curbuf_later(UPD_INVERTED); // delete the inversion } - if (cmdwin_type != 0 && wp != curwin) { + if (cmdwin_type != 0 && wp != cmdwin_win) { // A click outside the command-line window: Use modeless // selection if possible. Allow dragging the status lines. sep_line_offset = 0; row = 0; col += wp->w_wincol; - wp = curwin; + wp = cmdwin_win; } // Only change window focus when not clicking on or dragging the // status line. Do change focus when releasing the mouse button diff --git a/src/nvim/move.c b/src/nvim/move.c index a7380577c4..a303d1757f 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -760,7 +760,7 @@ int win_col_off(win_T *wp) { return ((wp->w_p_nu || wp->w_p_rnu || *wp->w_p_stc != NUL) ? (number_width(wp) + (*wp->w_p_stc == NUL)) : 0) - + ((cmdwin_type == 0 || wp != curwin) ? 0 : 1) + + ((wp != cmdwin_win) ? 0 : 1) + win_fdccol_count(wp) + (wp->w_scwidth * SIGN_WIDTH); } diff --git a/src/nvim/textformat.c b/src/nvim/textformat.c index 64cccf5f3f..bfe3ed5972 100644 --- a/src/nvim/textformat.c +++ b/src/nvim/textformat.c @@ -763,7 +763,7 @@ int comp_textwidth(bool ff) // The width is the window width minus 'wrapmargin' minus all the // things that add to the margin. textwidth = curwin->w_width_inner - (int)curbuf->b_p_wm; - if (cmdwin_type != 0) { + if (curbuf == cmdwin_buf) { textwidth -= 1; } textwidth -= win_fdccol_count(curwin); diff --git a/src/nvim/window.c b/src/nvim/window.c index 23779d1e7b..112ac8bf3b 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -2485,7 +2485,7 @@ bool can_close_in_cmdwin(win_T *win, Error *err) FUNC_ATTR_NONNULL_ALL { if (cmdwin_type != 0) { - if (win == curwin) { + if (win == cmdwin_win) { cmdwin_result = Ctrl_C; return false; } else if (win == cmdwin_old_curwin) { @@ -3030,6 +3030,9 @@ void win_free_all(void) { // avoid an error for switching tabpage with the cmdline window open cmdwin_type = 0; + cmdwin_buf = NULL; + cmdwin_win = NULL; + cmdwin_old_curwin = NULL; while (first_tabpage->tp_next != NULL) { tabpage_close(true); |