From d391940b9a074bca7ee82460ccaaabf46b5f2ba9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 15 Jan 2022 19:21:17 +0800 Subject: [PATCH 1/3] vim-patch:8.2.3227: 'virtualedit' can only be set globally Problem: 'virtualedit' can only be set globally. Solution: Make 'virtualedit' global-local. (Gary Johnson, closes vim/vim#8638) https://github.com/vim/vim/commit/53ba05b09075f14227f9be831a22ed16f7cc26b2 I changed some macros to unsigned integer literals to avoid compiler warnings. --- runtime/doc/options.txt | 6 +- src/nvim/buffer.c | 1 + src/nvim/buffer_defs.h | 2 + src/nvim/change.c | 2 +- src/nvim/cursor.c | 8 +- src/nvim/edit.c | 7 +- src/nvim/normal.c | 6 +- src/nvim/ops.c | 27 +++--- src/nvim/option.c | 46 ++++++++-- src/nvim/option_defs.h | 13 +-- src/nvim/options.lua | 2 +- src/nvim/screen.c | 8 +- src/nvim/state.c | 9 +- src/nvim/testdir/test_virtualedit.vim | 120 ++++++++++++++++++++++++++ 14 files changed, 213 insertions(+), 44 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 06236741c2..08f719f16c 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6782,12 +6782,16 @@ A jump table for the options with a short description can be found at |Q_op|. *'virtualedit'* *'ve'* 'virtualedit' 've' string (default "") - global + global or local to buffer |global-local| A comma separated list of these words: block Allow virtual editing in Visual block mode. insert Allow virtual editing in Insert mode. all Allow virtual editing in all modes. onemore Allow the cursor to move just past the end of the line + none When used as the local value, do not allow virtual + editing even when the global value is set. When used + as the global value, "none" is the same as "". + NONE Alternative spelling of "none". Virtual editing means that the cursor can be positioned where there is no actual character. This can be halfway into a tab or beyond the end diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index abd22fba26..3753d67d54 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1941,6 +1941,7 @@ void free_buf_options(buf_T *buf, int free_p_ff) clear_string_option(&buf->b_p_lw); clear_string_option(&buf->b_p_bkc); clear_string_option(&buf->b_p_menc); + clear_string_option(&buf->b_p_ve); } diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 63a550c017..a14dfff085 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -772,6 +772,8 @@ struct file_buffer { long b_p_ul; ///< 'undolevels' local value int b_p_udf; ///< 'undofile' char_u *b_p_lw; ///< 'lispwords' local value + char_u *b_p_ve; ///< 'virtualedit' local value + unsigned b_ve_flags; ///< flags for 'virtualedit' // end of buffer options diff --git a/src/nvim/change.c b/src/nvim/change.c index 1dbbfff024..0c16b204e3 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -789,7 +789,7 @@ int del_bytes(colnr_T count, bool fixpos_arg, bool use_delcombine) // fixpos is true, we don't want to end up positioned at the NUL, // unless "restart_edit" is set or 'virtualedit' contains "onemore". if (col > 0 && fixpos && restart_edit == 0 - && (ve_flags & VE_ONEMORE) == 0) { + && (get_ve_flags() & VE_ONEMORE) == 0) { curwin->w_cursor.col--; curwin->w_cursor.coladd = 0; curwin->w_cursor.col -= utf_head_off(oldp, oldp + curwin->w_cursor.col); diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 6e2c6232d7..55f55a46b2 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -15,6 +15,7 @@ #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/move.h" +#include "nvim/option.h" #include "nvim/plines.h" #include "nvim/screen.h" #include "nvim/state.h" @@ -110,7 +111,7 @@ static int coladvance2(pos_T *pos, bool addspaces, bool finetune, colnr_T wcol_a || (State & TERM_FOCUS) || restart_edit != NUL || (VIsual_active && *p_sel != 'o') - || ((ve_flags & VE_ONEMORE) && wcol < MAXCOL); + || ((get_ve_flags() & VE_ONEMORE) && wcol < MAXCOL); line = ml_get_buf(curbuf, pos->lnum, false); if (wcol >= MAXCOL) { @@ -366,6 +367,7 @@ void check_cursor_col_win(win_T *win) colnr_T len; colnr_T oldcol = win->w_cursor.col; colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd; + unsigned int cur_ve_flags = get_ve_flags(); len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, false)); if (len == 0) { @@ -377,7 +379,7 @@ void check_cursor_col_win(win_T *win) * - 'virtualedit' is set */ if ((State & INSERT) || restart_edit || (VIsual_active && *p_sel != 'o') - || (ve_flags & VE_ONEMORE) + || (cur_ve_flags & VE_ONEMORE) || virtual_active()) { win->w_cursor.col = len; } else { @@ -394,7 +396,7 @@ void check_cursor_col_win(win_T *win) // line. if (oldcol == MAXCOL) { win->w_cursor.coladd = 0; - } else if (ve_flags == VE_ALL) { + } else if (cur_ve_flags == VE_ALL) { if (oldcoladd > win->w_cursor.col) { win->w_cursor.coladd = oldcoladd - win->w_cursor.col; diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 9efe5a27c4..abf704d554 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -909,7 +909,7 @@ static int insert_handle_key(InsertState *s) ins_ctrl_o(); // don't move the cursor left when 'virtualedit' has "onemore". - if (ve_flags & VE_ONEMORE) { + if (get_ve_flags() & VE_ONEMORE) { ins_at_eol = false; s->nomove = true; } @@ -6905,8 +6905,7 @@ int oneright(void) // move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' // contains "onemore". - if (ptr[l] == NUL - && (ve_flags & VE_ONEMORE) == 0) { + if (ptr[l] == NUL && (get_ve_flags() & VE_ONEMORE) == 0) { return FAIL; } curwin->w_cursor.col += l; @@ -8028,7 +8027,7 @@ static bool ins_esc(long *count, int cmdchar, bool nomove) && !VIsual_active )) && !revins_on) { - if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) { + if (curwin->w_cursor.coladd > 0 || get_ve_flags() == VE_ALL) { oneleft(); if (restart_edit != NUL) { curwin->w_cursor.coladd++; diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 2b5b47c0b3..98ed351bf1 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -6046,7 +6046,7 @@ static void n_start_visual_mode(int c) // Corner case: the 0 position in a tab may change when going into // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting. // - if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB) { + if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB) { validate_virtcol(); coladvance(curwin->w_virtcol); } @@ -6951,7 +6951,7 @@ static void adjust_cursor(oparg_T *oap) if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL && (!VIsual_active || *p_sel == 'o') && !virtual_active() - && (ve_flags & VE_ONEMORE) == 0) { + && (get_ve_flags() & VE_ONEMORE) == 0) { curwin->w_cursor.col--; // prevent cursor from moving on the trail byte mb_adjust_cursor(); @@ -7157,7 +7157,7 @@ static void nv_esc(cmdarg_T *cap) void set_cursor_for_append_to_line(void) { curwin->w_set_curswant = true; - if (ve_flags == VE_ALL) { + if (get_ve_flags() == VE_ALL) { const int save_State = State; // Pretend Insert mode here to allow the cursor on the diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 1a12cb636a..74033df313 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -2196,19 +2196,22 @@ void op_insert(oparg_T *oap, long count1) // doing block_prep(). When only "block" is used, virtual edit is // already disabled, but still need it when calling // coladvance_force(). + // coladvance_force() uses get_ve_flags() to get the 'virtualedit' + // state for the current buffer. To override that state, we need to + // set the buffer-local value of ve_flags rather than the global value. if (curwin->w_cursor.coladd > 0) { - unsigned old_ve_flags = ve_flags; + unsigned old_ve_flags = curbuf->b_ve_flags; - ve_flags = VE_ALL; if (u_save_cursor() == FAIL) { return; } + curbuf->b_ve_flags = VE_ALL; coladvance_force(oap->op_type == OP_APPEND ? oap->end_vcol + 1 : getviscol()); if (oap->op_type == OP_APPEND) { --curwin->w_cursor.col; } - ve_flags = old_ve_flags; + curbuf->b_ve_flags = old_ve_flags; } // Get the info about the block before entering the text block_prep(oap, &bd, oap->start.lnum, true); @@ -2906,6 +2909,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) char_u *insert_string = NULL; bool allocated = false; long cnt; + unsigned int cur_ve_flags = get_ve_flags(); if (flags & PUT_FIXINDENT) { orig_indent = get_indent(); @@ -2976,7 +2980,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) eol = (*(cursor_pos + utfc_ptr2len(cursor_pos)) == NUL); } - bool ve_allows = (ve_flags == VE_ALL || ve_flags == VE_ONEMORE); + bool ve_allows = (cur_ve_flags == VE_ALL || cur_ve_flags == VE_ONEMORE); bool eof = curbuf->b_ml.ml_line_count == curwin->w_cursor.lnum && one_past_line; if (ve_allows || !(eol || eof)) { @@ -3152,7 +3156,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) yanklen = (int)STRLEN(y_array[0]); - if (ve_flags == VE_ALL && y_type == kMTCharWise) { + if (cur_ve_flags == VE_ALL && y_type == kMTCharWise) { if (gchar_cursor() == TAB) { int viscol = getviscol(); long ts = curbuf->b_p_ts; @@ -3181,7 +3185,7 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) colnr_T endcol2 = 0; if (dir == FORWARD && c != NUL) { - if (ve_flags == VE_ALL) { + if (cur_ve_flags == VE_ALL) { getvcol(curwin, &curwin->w_cursor, &col, NULL, &endcol2); } else { getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col); @@ -3195,9 +3199,8 @@ void do_put(int regname, yankreg_T *reg, int dir, long count, int flags) } col += curwin->w_cursor.coladd; - if (ve_flags == VE_ALL - && (curwin->w_cursor.coladd > 0 - || endcol2 == curwin->w_cursor.col)) { + if (cur_ve_flags == VE_ALL + && (curwin->w_cursor.coladd > 0 || endcol2 == curwin->w_cursor.col)) { if (dir == FORWARD && c == NUL) { col++; } @@ -3629,14 +3632,16 @@ end: */ void adjust_cursor_eol(void) { + unsigned int cur_ve_flags = get_ve_flags(); + if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL - && (ve_flags & VE_ONEMORE) == 0 + && (cur_ve_flags & VE_ONEMORE) == 0 && !(restart_edit || (State & INSERT))) { // Put the cursor on the last character in the line. dec_cursor(); - if (ve_flags == VE_ALL) { + if (cur_ve_flags == VE_ALL) { colnr_T scol, ecol; // Coladd is set to the width of the last character. diff --git a/src/nvim/option.c b/src/nvim/option.c index 659965b64c..fd4cc9ebb2 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -2065,6 +2065,7 @@ void check_buf_options(buf_T *buf) check_string_option(&buf->b_p_menc); check_string_option(&buf->b_p_vsts); check_string_option(&buf->b_p_vts); + check_string_option(&buf->b_p_ve); } /// Free the string allocated for an option. @@ -3084,14 +3085,27 @@ ambw_end: if (foldmethodIsIndent(curwin)) { foldUpdateAll(curwin); } - } else if (varp == &p_ve) { // 'virtualedit' - if (opt_strings_flags(p_ve, p_ve_values, &ve_flags, true) != OK) { - errmsg = e_invarg; - } else if (STRCMP(p_ve, oldval) != 0) { - // Recompute cursor position in case the new 've' setting - // changes something. - validate_virtcol(); - coladvance(curwin->w_virtcol); + } else if (gvarp == &p_ve) { // 'virtualedit' + char_u *ve = p_ve; + unsigned int *flags = &ve_flags; + + if (opt_flags & OPT_LOCAL) { + ve = curbuf->b_p_ve; + flags = &curbuf->b_ve_flags; + } + + if ((opt_flags & OPT_LOCAL) && *ve == NUL) { + // make the local value empty: use the global value + *flags = 0; + } else { + if (opt_strings_flags(ve, p_ve_values, flags, true) != OK) { + errmsg = e_invarg; + } else if (STRCMP(p_ve, oldval) != 0) { + // Recompute cursor position in case the new 've' setting + // changes something. + validate_virtcol(); + coladvance(curwin->w_virtcol); + } } } else if (varp == &p_csqf) { if (p_csqf != NULL) { @@ -5748,6 +5762,10 @@ void unset_global_local_option(char *name, void *from) set_chars_option((win_T *)from, &((win_T *)from)->w_p_fcs, true); redraw_later((win_T *)from, NOT_VALID); break; + case PV_VE: + clear_string_option(&buf->b_p_ve); + buf->b_ve_flags = 0; + break; } } @@ -5814,6 +5832,8 @@ static char_u *get_varp_scope(vimoption_T *p, int opt_flags) return (char_u *)&(curwin->w_p_fcs); case PV_LCS: return (char_u *)&(curwin->w_p_lcs); + case PV_VE: + return (char_u *)&(curbuf->b_p_ve); } return NULL; // "cannot happen" } @@ -6106,6 +6126,8 @@ static char_u *get_varp(vimoption_T *p) return (char_u *)&(curbuf->b_p_vsts); case PV_VTS: return (char_u *)&(curbuf->b_p_vts); + case PV_VE: + return *curbuf->b_p_ve != NUL ? (char_u *)&curbuf->b_p_ve : p->var; case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap); case PV_SCL: @@ -6438,6 +6460,8 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_udf = p_udf; buf->b_p_lw = empty_option; buf->b_p_menc = empty_option; + buf->b_p_ve = empty_option; + buf->b_ve_flags = 0; /* * Don't copy the options set by ex_help(), use the saved values, @@ -7815,6 +7839,12 @@ unsigned int get_bkc_value(buf_T *buf) return buf->b_bkc_flags ? buf->b_bkc_flags : bkc_flags; } +/// Get the local or global value of the 'virtualedit' flags. +unsigned int get_ve_flags(void) +{ + return (curbuf->b_ve_flags ? curbuf->b_ve_flags : ve_flags) & ~(VE_NONE | VE_NONEU); +} + /// Get the local or global value of 'showbreak'. /// /// @param win If not NULL, the window to get the local option from; global diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index 09c3bf3800..aed2868a09 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -705,12 +705,14 @@ EXTERN int p_vb; ///< 'visualbell' EXTERN char_u *p_ve; ///< 'virtualedit' EXTERN unsigned ve_flags; #ifdef IN_OPTION_C -static char *(p_ve_values[]) = { "block", "insert", "all", "onemore", NULL }; +static char *(p_ve_values[]) = { "block", "insert", "all", "onemore", "none", "NONE", NULL }; #endif -#define VE_BLOCK 5 // includes "all" -#define VE_INSERT 6 // includes "all" -#define VE_ALL 4 -#define VE_ONEMORE 8 +#define VE_BLOCK 5U // includes "all" +#define VE_INSERT 6U // includes "all" +#define VE_ALL 4U +#define VE_ONEMORE 8U +#define VE_NONE 16U +#define VE_NONEU 32U // Upper-case NONE EXTERN long p_verbose; // 'verbose' #ifdef IN_OPTION_C char_u *p_vfile = (char_u *)""; // used before options are initialized @@ -839,6 +841,7 @@ enum { BV_WM, BV_VSTS, BV_VTS, + BV_VE, BV_COUNT, // must be the last one }; diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 5133fe7ac8..0f46d2de21 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2736,7 +2736,7 @@ return { { full_name='virtualedit', abbreviation='ve', short_desc=N_("when to use virtual editing"), - type='string', list='onecomma', scope={'global'}, + type='string', list='onecomma', scope={'global', 'buffer'}, deny_duplicates=true, redraw={'curswant'}, varname='p_ve', diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 538604cf79..9aea42cda3 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -1215,18 +1215,18 @@ static void win_update(win_T *wp, Providers *providers) */ if (VIsual_mode == Ctrl_V) { colnr_T fromc, toc; - int save_ve_flags = ve_flags; + unsigned int save_ve_flags = curbuf->b_ve_flags; if (curwin->w_p_lbr) { - ve_flags = VE_ALL; + curbuf->b_ve_flags = VE_ALL; } getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc); - ve_flags = save_ve_flags; + curbuf->b_ve_flags = save_ve_flags; toc++; // Highlight to the end of the line, unless 'virtualedit' has // "block". - if (curwin->w_curswant == MAXCOL && !(ve_flags & VE_BLOCK)) { + if (curwin->w_curswant == MAXCOL && !(get_ve_flags() & VE_BLOCK)) { toc = MAXCOL; } diff --git a/src/nvim/state.c b/src/nvim/state.c index 1fe8bb671d..9e4c9b2bad 100644 --- a/src/nvim/state.c +++ b/src/nvim/state.c @@ -12,6 +12,7 @@ #include "nvim/lib/kvec.h" #include "nvim/log.h" #include "nvim/main.h" +#include "nvim/option.h" #include "nvim/option_defs.h" #include "nvim/os/input.h" #include "nvim/state.h" @@ -107,15 +108,17 @@ void state_handle_k_event(void) /// Return true if in the current mode we need to use virtual. bool virtual_active(void) { + unsigned int cur_ve_flags = get_ve_flags(); + // While an operator is being executed we return "virtual_op", because // VIsual_active has already been reset, thus we can't check for "block" // being used. if (virtual_op != kNone) { return virtual_op; } - return ve_flags == VE_ALL - || ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) - || ((ve_flags & VE_INSERT) && (State & INSERT)); + return cur_ve_flags == VE_ALL + || ((cur_ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V) + || ((cur_ve_flags & VE_INSERT) && (State & INSERT)); } /// VISUAL, SELECTMODE and OP_PENDING State are never set, they are equal to diff --git a/src/nvim/testdir/test_virtualedit.vim b/src/nvim/testdir/test_virtualedit.vim index 0a218898ed..c19a9500bd 100644 --- a/src/nvim/testdir/test_virtualedit.vim +++ b/src/nvim/testdir/test_virtualedit.vim @@ -258,4 +258,124 @@ func Test_yank_paste_small_del_reg() set virtualedit= endfunc +" After calling s:TryVirtualeditReplace(), line 1 will contain one of these +" two strings, depending on whether virtual editing is on or off. +let s:result_ve_on = 'a x' +let s:result_ve_off = 'x' + +" Utility function for Test_global_local() +func s:TryVirtualeditReplace() + call setline(1, 'a') + normal gg7l + normal rx +endfunc + +" Test for :set and :setlocal +func Test_global_local() + new + + " Verify that 'virtualedit' is initialized to empty, can be set globally to + " all and to empty, and can be set locally to all and to empty. + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + set ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + set ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + " Verify that :set affects multiple buffers + new + set ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + set ve= + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + bwipe! + + " Verify that :setlocal affects only the current buffer + setlocal ve=all + new + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + wincmd p + setlocal ve= + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + bwipe! + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + " Verify that the buffer 'virtualedit' state follows the global value only + " when empty and that "none" works as expected. + " + " 'virtualedit' State + " +--------+--------------------------+ + " | Local | Global | + " | | | + " +--------+--------+--------+--------+ + " | | "" | "all" | "none" | + " +--------+--------+--------+--------+ + " | "" | off | on | off | + " | "all" | on | on | on | + " | "none" | off | off | off | + " +--------+--------+--------+--------+ + new + + setglobal ve= + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + setglobal ve=all + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=NONE + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + setglobal ve=none + setlocal ve= + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve=none + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + + bwipe! + + setlocal virtualedit& + set virtualedit& +endfunc + " vim: shiftwidth=2 sts=2 expandtab From 87e54f123aa1c9c769d3ff35bdd1b5a980ba701c Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 15 Jan 2022 19:21:17 +0800 Subject: [PATCH 2/3] vim-patch:8.2.3280: 'virtualedit' local to buffer is not the best solution Problem: 'virtualedit' local to buffer is not the best solution. Solution: Make it window-local. (Gary Johnson, closes vim/vim#8685) https://github.com/vim/vim/commit/51ad850f5fbafa7aa3f60affa74ec9c9f992c6cc --- runtime/doc/options.txt | 2 +- src/nvim/buffer.c | 1 - src/nvim/buffer_defs.h | 6 +++-- src/nvim/ops.c | 10 ++++---- src/nvim/option.c | 24 +++++++++-------- src/nvim/option_defs.h | 4 +-- src/nvim/screen.c | 6 ++--- src/nvim/testdir/test_virtualedit.vim | 37 +++++++++++++++++++-------- 8 files changed, 54 insertions(+), 36 deletions(-) diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index 08f719f16c..4536079cea 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -6782,7 +6782,7 @@ A jump table for the options with a short description can be found at |Q_op|. *'virtualedit'* *'ve'* 'virtualedit' 've' string (default "") - global or local to buffer |global-local| + global or local to window |global-local| A comma separated list of these words: block Allow virtual editing in Visual block mode. insert Allow virtual editing in Insert mode. diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 3753d67d54..abd22fba26 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1941,7 +1941,6 @@ void free_buf_options(buf_T *buf, int free_p_ff) clear_string_option(&buf->b_p_lw); clear_string_option(&buf->b_p_bkc); clear_string_option(&buf->b_p_menc); - clear_string_option(&buf->b_p_ve); } diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index a14dfff085..2856a7390d 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -204,6 +204,10 @@ typedef struct { #define w_p_nu w_onebuf_opt.wo_nu // 'number' int wo_rnu; #define w_p_rnu w_onebuf_opt.wo_rnu // 'relativenumber' + char_u *wo_ve; +#define w_p_ve w_onebuf_opt.wo_ve // 'virtualedit' + unsigned wo_ve_flags; +#define w_ve_flags w_onebuf_opt.wo_ve_flags // flags for 'virtualedit' long wo_nuw; #define w_p_nuw w_onebuf_opt.wo_nuw // 'numberwidth' int wo_wfh; @@ -772,8 +776,6 @@ struct file_buffer { long b_p_ul; ///< 'undolevels' local value int b_p_udf; ///< 'undofile' char_u *b_p_lw; ///< 'lispwords' local value - char_u *b_p_ve; ///< 'virtualedit' local value - unsigned b_ve_flags; ///< flags for 'virtualedit' // end of buffer options diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 74033df313..c845bd3717 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -2197,21 +2197,21 @@ void op_insert(oparg_T *oap, long count1) // already disabled, but still need it when calling // coladvance_force(). // coladvance_force() uses get_ve_flags() to get the 'virtualedit' - // state for the current buffer. To override that state, we need to - // set the buffer-local value of ve_flags rather than the global value. + // state for the current window. To override that state, we need to + // set the window-local value of ve_flags rather than the global value. if (curwin->w_cursor.coladd > 0) { - unsigned old_ve_flags = curbuf->b_ve_flags; + unsigned old_ve_flags = curwin->w_ve_flags; if (u_save_cursor() == FAIL) { return; } - curbuf->b_ve_flags = VE_ALL; + curwin->w_ve_flags = VE_ALL; coladvance_force(oap->op_type == OP_APPEND ? oap->end_vcol + 1 : getviscol()); if (oap->op_type == OP_APPEND) { --curwin->w_cursor.col; } - curbuf->b_ve_flags = old_ve_flags; + curwin->w_ve_flags = old_ve_flags; } // Get the info about the block before entering the text block_prep(oap, &bd, oap->start.lnum, true); diff --git a/src/nvim/option.c b/src/nvim/option.c index fd4cc9ebb2..a79523f800 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -2065,7 +2065,6 @@ void check_buf_options(buf_T *buf) check_string_option(&buf->b_p_menc); check_string_option(&buf->b_p_vsts); check_string_option(&buf->b_p_vts); - check_string_option(&buf->b_p_ve); } /// Free the string allocated for an option. @@ -3090,8 +3089,8 @@ ambw_end: unsigned int *flags = &ve_flags; if (opt_flags & OPT_LOCAL) { - ve = curbuf->b_p_ve; - flags = &curbuf->b_ve_flags; + ve = curwin->w_p_ve; + flags = &curwin->w_ve_flags; } if ((opt_flags & OPT_LOCAL) && *ve == NUL) { @@ -5763,8 +5762,8 @@ void unset_global_local_option(char *name, void *from) redraw_later((win_T *)from, NOT_VALID); break; case PV_VE: - clear_string_option(&buf->b_p_ve); - buf->b_ve_flags = 0; + clear_string_option(&((win_T *)from)->w_p_ve); + ((win_T *)from)->w_ve_flags = 0; break; } } @@ -5833,7 +5832,7 @@ static char_u *get_varp_scope(vimoption_T *p, int opt_flags) case PV_LCS: return (char_u *)&(curwin->w_p_lcs); case PV_VE: - return (char_u *)&(curbuf->b_p_ve); + return (char_u *)&(curwin->w_p_ve); } return NULL; // "cannot happen" } @@ -5928,6 +5927,9 @@ static char_u *get_varp(vimoption_T *p) case PV_LCS: return *curwin->w_p_lcs != NUL ? (char_u *)&(curwin->w_p_lcs) : p->var; + case PV_VE: + return *curwin->w_p_ve != NUL + ? (char_u *)&curwin->w_p_ve : p->var; case PV_ARAB: return (char_u *)&(curwin->w_p_arab); @@ -6126,8 +6128,6 @@ static char_u *get_varp(vimoption_T *p) return (char_u *)&(curbuf->b_p_vsts); case PV_VTS: return (char_u *)&(curbuf->b_p_vts); - case PV_VE: - return *curbuf->b_p_ve != NUL ? (char_u *)&curbuf->b_p_ve : p->var; case PV_KMAP: return (char_u *)&(curbuf->b_p_keymap); case PV_SCL: @@ -6170,6 +6170,8 @@ void copy_winopt(winopt_T *from, winopt_T *to) to->wo_list = from->wo_list; to->wo_nu = from->wo_nu; to->wo_rnu = from->wo_rnu; + to->wo_ve = vim_strsave(from->wo_ve); + to->wo_ve_flags = from->wo_ve_flags; to->wo_nuw = from->wo_nuw; to->wo_rl = from->wo_rl; to->wo_rlc = vim_strsave(from->wo_rlc); @@ -6246,6 +6248,7 @@ static void check_winopt(winopt_T *wop) check_string_option(&wop->wo_winhl); check_string_option(&wop->wo_fcs); check_string_option(&wop->wo_lcs); + check_string_option(&wop->wo_ve); } /// Free the allocated memory inside a winopt_T. @@ -6270,6 +6273,7 @@ void clear_winopt(winopt_T *wop) clear_string_option(&wop->wo_winhl); clear_string_option(&wop->wo_fcs); clear_string_option(&wop->wo_lcs); + clear_string_option(&wop->wo_ve); } void didset_window_options(win_T *wp) @@ -6460,8 +6464,6 @@ void buf_copy_options(buf_T *buf, int flags) buf->b_p_udf = p_udf; buf->b_p_lw = empty_option; buf->b_p_menc = empty_option; - buf->b_p_ve = empty_option; - buf->b_ve_flags = 0; /* * Don't copy the options set by ex_help(), use the saved values, @@ -7842,7 +7844,7 @@ unsigned int get_bkc_value(buf_T *buf) /// Get the local or global value of the 'virtualedit' flags. unsigned int get_ve_flags(void) { - return (curbuf->b_ve_flags ? curbuf->b_ve_flags : ve_flags) & ~(VE_NONE | VE_NONEU); + return (curwin->w_ve_flags ? curwin->w_ve_flags : ve_flags) & ~(VE_NONE | VE_NONEU); } /// Get the local or global value of 'showbreak'. diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index aed2868a09..db8bc83395 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -711,8 +711,8 @@ static char *(p_ve_values[]) = { "block", "insert", "all", "onemore", "none", "N #define VE_INSERT 6U // includes "all" #define VE_ALL 4U #define VE_ONEMORE 8U -#define VE_NONE 16U -#define VE_NONEU 32U // Upper-case NONE +#define VE_NONE 16U // "none" +#define VE_NONEU 32U // "NONE" EXTERN long p_verbose; // 'verbose' #ifdef IN_OPTION_C char_u *p_vfile = (char_u *)""; // used before options are initialized diff --git a/src/nvim/screen.c b/src/nvim/screen.c index 9aea42cda3..9b253fad01 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -1215,14 +1215,14 @@ static void win_update(win_T *wp, Providers *providers) */ if (VIsual_mode == Ctrl_V) { colnr_T fromc, toc; - unsigned int save_ve_flags = curbuf->b_ve_flags; + unsigned int save_ve_flags = curwin->w_ve_flags; if (curwin->w_p_lbr) { - curbuf->b_ve_flags = VE_ALL; + curwin->w_ve_flags = VE_ALL; } getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc); - curbuf->b_ve_flags = save_ve_flags; + curwin->w_ve_flags = save_ve_flags; toc++; // Highlight to the end of the line, unless 'virtualedit' has // "block". diff --git a/src/nvim/testdir/test_virtualedit.vim b/src/nvim/testdir/test_virtualedit.vim index c19a9500bd..d2a5258bd3 100644 --- a/src/nvim/testdir/test_virtualedit.vim +++ b/src/nvim/testdir/test_virtualedit.vim @@ -263,7 +263,7 @@ endfunc let s:result_ve_on = 'a x' let s:result_ve_off = 'x' -" Utility function for Test_global_local() +" Utility function for Test_global_local_virtualedit() func s:TryVirtualeditReplace() call setline(1, 'a') normal gg7l @@ -271,7 +271,7 @@ func s:TryVirtualeditReplace() endfunc " Test for :set and :setlocal -func Test_global_local() +func Test_global_local_virtualedit() new " Verify that 'virtualedit' is initialized to empty, can be set globally to @@ -291,8 +291,8 @@ func Test_global_local() call s:TryVirtualeditReplace() call assert_equal(s:result_ve_off, getline(1)) - " Verify that :set affects multiple buffers - new + " Verify that :set affects multiple windows. + split set ve=all call s:TryVirtualeditReplace() call assert_equal(s:result_ve_on, getline(1)) @@ -305,17 +305,15 @@ func Test_global_local() call assert_equal(s:result_ve_off, getline(1)) bwipe! - " Verify that :setlocal affects only the current buffer - setlocal ve=all + " Verify that :setlocal affects only the current window. new - call s:TryVirtualeditReplace() - call assert_equal(s:result_ve_off, getline(1)) + split setlocal ve=all - wincmd p - setlocal ve= - wincmd p call s:TryVirtualeditReplace() call assert_equal(s:result_ve_on, getline(1)) + wincmd p + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) bwipe! call s:TryVirtualeditReplace() call assert_equal(s:result_ve_off, getline(1)) @@ -374,6 +372,23 @@ func Test_global_local() bwipe! + " Verify that the 'virtualedit' state is copied to new windows. + new + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + split + setlocal ve=all + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + split + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_on, getline(1)) + setlocal ve= + split + call s:TryVirtualeditReplace() + call assert_equal(s:result_ve_off, getline(1)) + bwipe! + setlocal virtualedit& set virtualedit& endfunc From 6c22e5fd1a4e6d945bb76a4a8a558613f99ec793 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 15 Jan 2022 19:21:17 +0800 Subject: [PATCH 3/3] vim-patch:8.2.4094: 'virtualedit' is window-local but using buffer-local enum Problem: 'virtualedit' is window-local but using buffer-local enum. Solution: Use window-local enum. (closes vim/vim#9529) https://github.com/vim/vim/commit/e1833bfd01c100896d2a01f281762c285192d84b --- src/nvim/option_defs.h | 2 +- src/nvim/options.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/option_defs.h b/src/nvim/option_defs.h index db8bc83395..5d6aca9574 100644 --- a/src/nvim/option_defs.h +++ b/src/nvim/option_defs.h @@ -841,7 +841,6 @@ enum { BV_WM, BV_VSTS, BV_VTS, - BV_VE, BV_COUNT, // must be the last one }; @@ -872,6 +871,7 @@ enum { WV_LBR, WV_NU, WV_RNU, + WV_VE, WV_NUW, WV_PVW, WV_RL, diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 0f46d2de21..aea2179a61 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2736,7 +2736,7 @@ return { { full_name='virtualedit', abbreviation='ve', short_desc=N_("when to use virtual editing"), - type='string', list='onecomma', scope={'global', 'buffer'}, + type='string', list='onecomma', scope={'global', 'window'}, deny_duplicates=true, redraw={'curswant'}, varname='p_ve',