forked from aniani/vim
patch 7.4.754
Problem: Using CTRL-A in Visual mode does not work well. (Gary Johnson) Solution: Make it increment all numbers in the Visual area. (Christian Brabandt)
This commit is contained in:
30
src/normal.c
30
src/normal.c
@@ -4201,9 +4201,17 @@ nv_help(cap)
|
||||
nv_addsub(cap)
|
||||
cmdarg_T *cap;
|
||||
{
|
||||
if (!checkclearopq(cap->oap)
|
||||
&& do_addsub((int)cap->cmdchar, cap->count1) == OK)
|
||||
int visual = VIsual_active;
|
||||
if (cap->oap->op_type == OP_NOP
|
||||
&& do_addsub((int)cap->cmdchar, cap->count1, cap->arg) == OK)
|
||||
prep_redo_cmd(cap);
|
||||
else
|
||||
clearopbeep(cap->oap);
|
||||
if (visual)
|
||||
{
|
||||
VIsual_active = FALSE;
|
||||
redraw_later(CLEAR);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -7841,14 +7849,28 @@ nv_g_cmd(cap)
|
||||
|
||||
switch (cap->nchar)
|
||||
{
|
||||
case Ctrl_A:
|
||||
case Ctrl_X:
|
||||
#ifdef MEM_PROFILE
|
||||
/*
|
||||
* "g^A": dump log of used memory.
|
||||
*/
|
||||
case Ctrl_A:
|
||||
if (!VIsual_active && cap->nchar == Ctrl_A)
|
||||
vim_mem_profile_dump();
|
||||
break;
|
||||
else
|
||||
#endif
|
||||
/*
|
||||
* "g^A/g^X": sequentially increment visually selected region
|
||||
*/
|
||||
if (VIsual_active)
|
||||
{
|
||||
cap->arg = TRUE;
|
||||
cap->cmdchar = cap->nchar;
|
||||
nv_addsub(cap);
|
||||
}
|
||||
else
|
||||
clearopbeep(oap);
|
||||
break;
|
||||
|
||||
#ifdef FEAT_VREPLACE
|
||||
/*
|
||||
|
99
src/ops.c
99
src/ops.c
@@ -5375,9 +5375,10 @@ reverse_line(s)
|
||||
* return FAIL for failure, OK otherwise
|
||||
*/
|
||||
int
|
||||
do_addsub(command, Prenum1)
|
||||
do_addsub(command, Prenum1, g_cmd)
|
||||
int command;
|
||||
linenr_T Prenum1;
|
||||
int g_cmd; /* was g<c-a>/g<c-x> */
|
||||
{
|
||||
int col;
|
||||
char_u *buf1;
|
||||
@@ -5385,6 +5386,7 @@ do_addsub(command, Prenum1)
|
||||
int hex; /* 'X' or 'x': hex; '0': octal */
|
||||
static int hexupper = FALSE; /* 0xABC */
|
||||
unsigned long n;
|
||||
long offset = 0; /* line offset for Ctrl_V mode */
|
||||
long_u oldn;
|
||||
char_u *ptr;
|
||||
int c;
|
||||
@@ -5394,20 +5396,55 @@ do_addsub(command, Prenum1)
|
||||
int dooct;
|
||||
int doalp;
|
||||
int firstdigit;
|
||||
int negative;
|
||||
int subtract;
|
||||
int negative = FALSE;
|
||||
int visual = VIsual_active;
|
||||
int i;
|
||||
int lnum = curwin->w_cursor.lnum;
|
||||
int lnume = curwin->w_cursor.lnum;
|
||||
|
||||
dohex = (vim_strchr(curbuf->b_p_nf, 'x') != NULL); /* "heX" */
|
||||
dooct = (vim_strchr(curbuf->b_p_nf, 'o') != NULL); /* "Octal" */
|
||||
doalp = (vim_strchr(curbuf->b_p_nf, 'p') != NULL); /* "alPha" */
|
||||
|
||||
ptr = ml_get_curline();
|
||||
RLADDSUBFIX(ptr);
|
||||
|
||||
/*
|
||||
* First check if we are on a hexadecimal number, after the "0x".
|
||||
*/
|
||||
col = curwin->w_cursor.col;
|
||||
if (VIsual_active)
|
||||
{
|
||||
if (lt(curwin->w_cursor, VIsual))
|
||||
{
|
||||
pos_T t;
|
||||
t = curwin->w_cursor;
|
||||
curwin->w_cursor = VIsual;
|
||||
VIsual = t;
|
||||
}
|
||||
if (VIsual_mode == 'V')
|
||||
VIsual.col = 0;
|
||||
|
||||
ptr = ml_get(VIsual.lnum);
|
||||
RLADDSUBFIX(ptr);
|
||||
|
||||
/* store visual area for 'gv' */
|
||||
curbuf->b_visual.vi_start = VIsual;
|
||||
curbuf->b_visual.vi_end = curwin->w_cursor;
|
||||
curbuf->b_visual.vi_mode = VIsual_mode;
|
||||
|
||||
col = VIsual.col;
|
||||
lnum = VIsual.lnum;
|
||||
lnume = curwin->w_cursor.lnum;
|
||||
if (ptr[col] == '-')
|
||||
{
|
||||
negative = TRUE;
|
||||
col++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = ml_get_curline();
|
||||
RLADDSUBFIX(ptr);
|
||||
|
||||
if (dohex)
|
||||
while (col > 0 && vim_isxdigit(ptr[col]))
|
||||
--col;
|
||||
@@ -5418,9 +5455,7 @@ do_addsub(command, Prenum1)
|
||||
&& ptr[col - 1] == '0'
|
||||
&& vim_isxdigit(ptr[col + 1]))
|
||||
{
|
||||
/*
|
||||
* Found hexadecimal number, move to its start.
|
||||
*/
|
||||
/* Found hexadecimal number, move to its start. */
|
||||
--col;
|
||||
}
|
||||
else
|
||||
@@ -5440,7 +5475,15 @@ do_addsub(command, Prenum1)
|
||||
&& !(doalp && ASCII_ISALPHA(ptr[col])))
|
||||
--col;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = lnum; i <= lnume; i++)
|
||||
{
|
||||
curwin->w_cursor.lnum = i;
|
||||
ptr = ml_get_curline();
|
||||
RLADDSUBFIX(ptr);
|
||||
if ((int)STRLEN(ptr) <= col)
|
||||
col = 0;
|
||||
/*
|
||||
* If a number was found, and saving for undo works, replace the number.
|
||||
*/
|
||||
@@ -5449,11 +5492,13 @@ do_addsub(command, Prenum1)
|
||||
if ((!VIM_ISDIGIT(firstdigit) && !(doalp && ASCII_ISALPHA(firstdigit)))
|
||||
|| u_save_cursor() != OK)
|
||||
{
|
||||
if (lnum < lnume)
|
||||
/* Try again on next line */
|
||||
continue;
|
||||
beep_flush();
|
||||
return FAIL;
|
||||
}
|
||||
|
||||
/* get ptr again, because u_save() may have changed it */
|
||||
ptr = ml_get_curline();
|
||||
RLADDSUBFIX(ptr);
|
||||
|
||||
@@ -5498,9 +5543,9 @@ do_addsub(command, Prenum1)
|
||||
}
|
||||
else
|
||||
{
|
||||
negative = FALSE;
|
||||
if (col > 0 && ptr[col - 1] == '-') /* negative number */
|
||||
if (col > 0 && ptr[col - 1] == '-' && !visual)
|
||||
{
|
||||
/* negative number */
|
||||
--col;
|
||||
negative = TRUE;
|
||||
}
|
||||
@@ -5540,8 +5585,9 @@ do_addsub(command, Prenum1)
|
||||
negative ^= TRUE;
|
||||
}
|
||||
}
|
||||
else /* add */
|
||||
else
|
||||
{
|
||||
/* add */
|
||||
if (n < oldn)
|
||||
{
|
||||
n = (n ^ (unsigned long)-1);
|
||||
@@ -5558,9 +5604,10 @@ do_addsub(command, Prenum1)
|
||||
curwin->w_cursor.col = col;
|
||||
todel = length;
|
||||
c = gchar_cursor();
|
||||
|
||||
/*
|
||||
* Don't include the '-' in the length, only the length of the part
|
||||
* after it is kept the same.
|
||||
* Don't include the '-' in the length, only the length of the
|
||||
* part after it is kept the same.
|
||||
*/
|
||||
if (c == '-')
|
||||
--length;
|
||||
@@ -5580,14 +5627,15 @@ do_addsub(command, Prenum1)
|
||||
|
||||
/*
|
||||
* Prepare the leading characters in buf1[].
|
||||
* When there are many leading zeros it could be very long. Allocate
|
||||
* a bit too much.
|
||||
* When there are many leading zeros it could be very long.
|
||||
* Allocate a bit too much.
|
||||
*/
|
||||
buf1 = alloc((unsigned)length + NUMBUFLEN);
|
||||
if (buf1 == NULL)
|
||||
return FAIL;
|
||||
ptr = buf1;
|
||||
if (negative)
|
||||
/* do not add leading '-' for visual mode */
|
||||
if (negative && !visual)
|
||||
{
|
||||
*ptr++ = '-';
|
||||
}
|
||||
@@ -5606,15 +5654,23 @@ do_addsub(command, Prenum1)
|
||||
* Put the number characters in buf2[].
|
||||
*/
|
||||
if (hex == 0)
|
||||
sprintf((char *)buf2, "%lu", n);
|
||||
sprintf((char *)buf2, "%lu", n + offset);
|
||||
else if (hex == '0')
|
||||
sprintf((char *)buf2, "%lo", n);
|
||||
sprintf((char *)buf2, "%lo", n + offset);
|
||||
else if (hex && hexupper)
|
||||
sprintf((char *)buf2, "%lX", n);
|
||||
sprintf((char *)buf2, "%lX", n + offset);
|
||||
else
|
||||
sprintf((char *)buf2, "%lx", n);
|
||||
sprintf((char *)buf2, "%lx", n + offset);
|
||||
length -= (int)STRLEN(buf2);
|
||||
|
||||
if (g_cmd)
|
||||
{
|
||||
if (subtract)
|
||||
offset -= (unsigned long)Prenum1;
|
||||
else
|
||||
offset += (unsigned long)Prenum1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Adjust number of zeros to the new number of digits, so the
|
||||
* total length of the number remains the same.
|
||||
@@ -5635,6 +5691,7 @@ do_addsub(command, Prenum1)
|
||||
ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
|
||||
RLADDSUBFIX(ptr);
|
||||
#endif
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
@@ -43,7 +43,7 @@ void op_formatexpr __ARGS((oparg_T *oap));
|
||||
int fex_format __ARGS((linenr_T lnum, long count, int c));
|
||||
void format_lines __ARGS((linenr_T line_count, int avoid_fex));
|
||||
int paragraph_start __ARGS((linenr_T lnum));
|
||||
int do_addsub __ARGS((int command, linenr_T Prenum1));
|
||||
int do_addsub __ARGS((int command, linenr_T Prenum1, int g_cmd));
|
||||
int read_viminfo_register __ARGS((vir_T *virp, int force));
|
||||
void write_viminfo_registers __ARGS((FILE *fp));
|
||||
void x11_export_final_selection __ARGS((void));
|
||||
|
@@ -45,6 +45,7 @@ SCRIPTS = test1.out test3.out test4.out test5.out test6.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
@@ -192,6 +193,7 @@ test_close_count.out: test_close_count.in
|
||||
test_command_count.out: test_command_count.in
|
||||
test_erasebackword.out: test_erasebackword.in
|
||||
test_eval.out: test_eval.in
|
||||
test_increment.out: test_increment.in
|
||||
test_insertcount.out: test_insertcount.in
|
||||
test_listchars.out: test_listchars.in
|
||||
test_listlbr.out: test_listlbr.in
|
||||
|
@@ -44,6 +44,7 @@ SCRIPTS = test3.out test4.out test5.out test6.out test7.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
|
@@ -66,6 +66,7 @@ SCRIPTS = test3.out test4.out test5.out test6.out test7.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
|
@@ -46,6 +46,7 @@ SCRIPTS = test1.out test3.out test4.out test5.out test6.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
|
@@ -105,6 +105,7 @@ SCRIPT = test1.out test2.out test3.out test4.out test5.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
|
@@ -42,6 +42,7 @@ SCRIPTS = test1.out test2.out test3.out test4.out test5.out test6.out \
|
||||
test_command_count.out \
|
||||
test_erasebackword.out \
|
||||
test_eval.out \
|
||||
test_increment.out \
|
||||
test_insertcount.out \
|
||||
test_listchars.out \
|
||||
test_listlbr.out \
|
||||
|
143
src/testdir/test_increment.in
Normal file
143
src/testdir/test_increment.in
Normal file
@@ -0,0 +1,143 @@
|
||||
Tests for using Ctrl-A/Ctrl-X on visual selections
|
||||
|
||||
Test cases
|
||||
==========
|
||||
|
||||
1) Ctrl-A on visually selected number
|
||||
Text:
|
||||
foobar-10
|
||||
1) Ctrl-A on start of line:
|
||||
foobar-9
|
||||
2) Ctrl-A on visually selected "-10":
|
||||
foobar-9
|
||||
3) Ctrl-A on visually selected "10":
|
||||
foobar-11
|
||||
4) Ctrl-X on visually selected "-10"
|
||||
foobar-11
|
||||
5) Ctrl-X on visually selected "10"
|
||||
foobar-9
|
||||
|
||||
2) Ctrl-A on visually selected lines
|
||||
Text:
|
||||
10
|
||||
20
|
||||
30
|
||||
40
|
||||
|
||||
1) Ctrl-A on visually selected lines:
|
||||
11
|
||||
21
|
||||
31
|
||||
41
|
||||
|
||||
2) Ctrl-X on visually selected lines:
|
||||
9
|
||||
19
|
||||
29
|
||||
39
|
||||
|
||||
3) g Ctrl-A on visually selected lines, with non-numbers in between
|
||||
Text:
|
||||
10
|
||||
|
||||
20
|
||||
|
||||
30
|
||||
|
||||
40
|
||||
|
||||
1) 2 g Ctrl-A on visually selected lines:
|
||||
12
|
||||
|
||||
24
|
||||
|
||||
36
|
||||
|
||||
48
|
||||
2) 2 g Ctrl-X on visually selected lines
|
||||
8
|
||||
|
||||
16
|
||||
|
||||
24
|
||||
|
||||
32
|
||||
|
||||
4) Ctrl-A on non-number
|
||||
Text:
|
||||
foobar-10
|
||||
1) visually select foobar:
|
||||
foobar-10
|
||||
|
||||
STARTTEST
|
||||
:so small.vim
|
||||
|
||||
:" Test 1
|
||||
:/^S1=/+,/^E1=/-y a
|
||||
:/^E1/+put a
|
||||
:/^E1/+2put a
|
||||
f-v$:/^E1/+3put a
|
||||
f1v$:/^E1/+4put a
|
||||
f-v$:/^E1/+5put a
|
||||
f1v$
|
||||
|
||||
:" Test 22
|
||||
:/^S2=/+,/^E2=/-y a
|
||||
:/^E2/+put a
|
||||
V3k$:.+put a
|
||||
V3k$
|
||||
|
||||
:" Test 3
|
||||
:/^S3=/+,/^E3=/-y a
|
||||
:/^E3=/+put a
|
||||
V6k2g:.+put a
|
||||
V6k2g
|
||||
|
||||
:" Test 4
|
||||
:/^S4=/+,/^E4=/-y a
|
||||
:/^E4=/+put a
|
||||
vf-
|
||||
|
||||
:" Save the report
|
||||
:/^# Test 1/,$w! test.out
|
||||
:qa!
|
||||
|
||||
|
||||
# Test 1
|
||||
S1======
|
||||
foobar-10
|
||||
E1======
|
||||
|
||||
|
||||
|
||||
# Test 2
|
||||
S2=====
|
||||
10
|
||||
20
|
||||
30
|
||||
40
|
||||
E2=====
|
||||
|
||||
|
||||
|
||||
# Test 3
|
||||
S3=====
|
||||
10
|
||||
|
||||
20
|
||||
|
||||
30
|
||||
|
||||
40
|
||||
E3=====
|
||||
|
||||
|
||||
|
||||
# Test 4
|
||||
S4=====
|
||||
foobar-10
|
||||
E4=====
|
||||
|
||||
|
||||
ENDTEST
|
||||
|
66
src/testdir/test_increment.ok
Normal file
66
src/testdir/test_increment.ok
Normal file
@@ -0,0 +1,66 @@
|
||||
# Test 1
|
||||
S1======
|
||||
foobar-10
|
||||
E1======
|
||||
|
||||
foobar-9
|
||||
foobar-9
|
||||
foobar-11
|
||||
foobar-11
|
||||
foobar-9
|
||||
|
||||
|
||||
# Test 2
|
||||
S2=====
|
||||
10
|
||||
20
|
||||
30
|
||||
40
|
||||
E2=====
|
||||
|
||||
11
|
||||
21
|
||||
31
|
||||
41
|
||||
|
||||
9
|
||||
19
|
||||
29
|
||||
39
|
||||
|
||||
# Test 3
|
||||
S3=====
|
||||
10
|
||||
|
||||
20
|
||||
|
||||
30
|
||||
|
||||
40
|
||||
E3=====
|
||||
|
||||
12
|
||||
|
||||
24
|
||||
|
||||
36
|
||||
|
||||
48
|
||||
|
||||
8
|
||||
|
||||
16
|
||||
|
||||
24
|
||||
|
||||
32
|
||||
|
||||
# Test 4
|
||||
S4=====
|
||||
foobar-10
|
||||
E4=====
|
||||
|
||||
foobar-10
|
||||
|
||||
ENDTEST
|
||||
|
@@ -741,6 +741,8 @@ static char *(features[]) =
|
||||
|
||||
static int included_patches[] =
|
||||
{ /* Add new patch number below this line */
|
||||
/**/
|
||||
754,
|
||||
/**/
|
||||
753,
|
||||
/**/
|
||||
|
Reference in New Issue
Block a user