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)
53ba05b090

I changed some macros to unsigned integer literals to avoid compiler warnings.
This commit is contained in:
zeertzjq 2022-01-15 19:21:17 +08:00
parent 574a582202
commit d391940b9a
14 changed files with 213 additions and 44 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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++;

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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
};

View File

@ -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',

View File

@ -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;
}

View File

@ -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

View File

@ -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