aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/globals.h2
-rw-r--r--src/nvim/normal.c24
-rw-r--r--src/nvim/textobject.c8
-rw-r--r--test/old/testdir/test_visual.vim44
4 files changed, 72 insertions, 6 deletions
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
index e0bbba3d68..15152bc1a4 100644
--- a/src/nvim/globals.h
+++ b/src/nvim/globals.h
@@ -464,6 +464,8 @@ EXTERN bool VIsual_active INIT( = false);
EXTERN bool VIsual_select INIT( = false);
/// Register name for Select mode
EXTERN int VIsual_select_reg INIT( = 0);
+/// Whether incremented cursor during exclusive selection
+EXTERN bool VIsual_select_exclu_adj INIT( = false);
/// Restart Select mode when next cmd finished
EXTERN int restart_VIsual_select INIT( = 0);
/// Whether to restart the selection after a Select-mode mapping or menu.
diff --git a/src/nvim/normal.c b/src/nvim/normal.c
index 44e48e9e72..83ab0d5c24 100644
--- a/src/nvim/normal.c
+++ b/src/nvim/normal.c
@@ -1524,6 +1524,7 @@ static void set_vcount_ca(cmdarg_T *cap, bool *set_prevcount)
/// do_pending_operator().
void end_visual_mode(void)
{
+ VIsual_select_exclu_adj = false;
VIsual_active = false;
setmouse();
mouse_dragging = 0;
@@ -4035,17 +4036,24 @@ static int normal_search(cmdarg_T *cap, int dir, char *pat, size_t patlen, int o
/// cap->nchar is NUL for ',' and ';' (repeat the search)
static void nv_csearch(cmdarg_T *cap)
{
- bool t_cmd;
+ bool cursor_dec = false;
- if (cap->cmdchar == 't' || cap->cmdchar == 'T') {
- t_cmd = true;
- } else {
- t_cmd = false;
+ // If adjusted cursor position previously, unadjust it.
+ if (*p_sel == 'e' && VIsual_active && VIsual_mode == 'v'
+ && VIsual_select_exclu_adj) {
+ unadjust_for_sel();
+ cursor_dec = true;
}
+ bool t_cmd = cap->cmdchar == 't' || cap->cmdchar == 'T';
+
cap->oap->motion_type = kMTCharWise;
if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == false) {
clearopbeep(cap->oap);
+ // Revert unadjust when failed.
+ if (cursor_dec) {
+ adjust_for_sel(cap);
+ }
return;
}
@@ -5054,6 +5062,8 @@ static void nv_visual(cmdarg_T *cap)
n_start_visual_mode(cap->cmdchar);
if (VIsual_mode != 'V' && *p_sel == 'e') {
cap->count1++; // include one more char
+ } else {
+ VIsual_select_exclu_adj = false;
}
if (cap->count0 > 0 && --cap->count1 > 0) {
// With a count select that many characters or lines.
@@ -6021,6 +6031,7 @@ static void adjust_for_sel(cmdarg_T *cap)
&& gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor)) {
inc_cursor();
cap->oap->inclusive = false;
+ VIsual_select_exclu_adj = true;
}
}
@@ -6042,7 +6053,7 @@ bool unadjust_for_sel(void)
/// @return true when backed up to the previous line.
bool unadjust_for_sel_inner(pos_T *pp)
{
- colnr_T cs, ce;
+ VIsual_select_exclu_adj = false;
if (pp->coladd > 0) {
pp->coladd--;
@@ -6050,6 +6061,7 @@ bool unadjust_for_sel_inner(pos_T *pp)
pp->col--;
mark_mb_adjustpos(curbuf, pp);
if (virtual_active(curwin)) {
+ colnr_T cs, ce;
getvcol(curwin, pp, &cs, NULL, &ce);
pp->coladd = ce - cs;
}
diff --git a/src/nvim/textobject.c b/src/nvim/textobject.c
index e3b3bba7c1..fda8b45e21 100644
--- a/src/nvim/textobject.c
+++ b/src/nvim/textobject.c
@@ -19,6 +19,7 @@
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/move.h"
+#include "nvim/normal.h"
#include "nvim/option_vars.h"
#include "nvim/pos_defs.h"
#include "nvim/search.h"
@@ -427,6 +428,13 @@ int end_word(int count, bool bigword, bool stop, bool empty)
curwin->w_cursor.coladd = 0;
cls_bigword = bigword;
+
+ // If adjusted cursor position previously, unadjust it.
+ if (*p_sel == 'e' && VIsual_active && VIsual_mode == 'v'
+ && VIsual_select_exclu_adj) {
+ unadjust_for_sel();
+ }
+
while (--count >= 0) {
// When inside a range of folded lines, move to the last char of the
// last line.
diff --git a/test/old/testdir/test_visual.vim b/test/old/testdir/test_visual.vim
index 328ac502bf..f5a5970e24 100644
--- a/test/old/testdir/test_visual.vim
+++ b/test/old/testdir/test_visual.vim
@@ -1101,6 +1101,50 @@ func Test_exclusive_selection()
bw!
endfunc
+" Test for inclusive motion in visual mode with 'exclusive' selection
+func Test_inclusive_motion_selection_exclusive()
+ func s:compare_exclu_inclu(line, keys, expected_exclu)
+ let msg = "data: '" . a:line . "' operation: '" . a:keys . "'"
+ call setline(1, a:line)
+ set selection=exclusive
+ call feedkeys(a:keys, 'xt')
+ call assert_equal(a:expected_exclu, getpos('.'), msg)
+ let pos_ex = col('.')
+ set selection=inclusive
+ call feedkeys(a:keys, 'xt')
+ let pos_in = col('.')
+ call assert_equal(1, pos_ex - pos_in, msg)
+ endfunc
+
+ new
+ " Test 'e' motion
+ set selection=exclusive
+ call setline(1, 'eins zwei drei')
+ norm! ggvey
+ call assert_equal('eins', @")
+ call setline(1, 'abc(abc)abc')
+ norm! ggveeed
+ call assert_equal(')abc', getline(1))
+ call setline(1, 'abc(abc)abc')
+ norm! gg3lvey
+ call assert_equal('(abc', @")
+ call s:compare_exclu_inclu('abc(abc)abc', 'ggveee', [0, 1, 8, 0])
+ " Test 'f' motion
+ call s:compare_exclu_inclu('geschwindigkeit', 'ggvfefe', [0, 1, 14, 0])
+ call s:compare_exclu_inclu('loooooooooooong', 'ggv2fo2fo2fo', [0, 1, 8, 0])
+ " Test 't' motion
+ call s:compare_exclu_inclu('geschwindigkeit', 'ggv2te', [0, 1, 13, 0])
+ call s:compare_exclu_inclu('loooooooooooong', 'gglv2to2to2to', [0, 1, 6, 0])
+ " Test ';' motion
+ call s:compare_exclu_inclu('geschwindigkeit', 'ggvfi;;', [0, 1, 15, 0])
+ call s:compare_exclu_inclu('geschwindigkeit', 'ggvti;;', [0, 1, 14, 0])
+ call s:compare_exclu_inclu('loooooooooooong', 'ggv2fo;;', [0, 1, 6, 0])
+ call s:compare_exclu_inclu('loooooooooooong', 'ggvl2to;;', [0, 1, 6, 0])
+ " Clean up
+ set selection&
+ bw!
+endfunc
+
" Test for starting linewise visual with a count.
" This test needs to be run without any previous visual mode. Otherwise the
" count will use the count from the previous visual mode.