@@ -5386,7 +5386,7 @@ do_addsub(command, Prenum1, g_cmd)
5386
5386
int hex ; /* 'X' or 'x': hex; '0': octal */
5387
5387
static int hexupper = FALSE; /* 0xABC */
5388
5388
unsigned long n ;
5389
- long offset = 0 ; /* line offset for Ctrl_V mode */
5389
+ unsigned long offset = 0 ; /* line offset for Ctrl_V mode */
5390
5390
long_u oldn ;
5391
5391
char_u * ptr ;
5392
5392
int c ;
@@ -5398,10 +5398,12 @@ do_addsub(command, Prenum1, g_cmd)
5398
5398
int firstdigit ;
5399
5399
int subtract ;
5400
5400
int negative = FALSE;
5401
+ int was_positive = TRUE;
5401
5402
int visual = VIsual_active ;
5402
5403
int i ;
5403
5404
int lnum = curwin -> w_cursor .lnum ;
5404
5405
int lnume = curwin -> w_cursor .lnum ;
5406
+ int startcol ;
5405
5407
5406
5408
dohex = (vim_strchr (curbuf -> b_p_nf , 'x' ) != NULL ); /* "heX" */
5407
5409
dooct = (vim_strchr (curbuf -> b_p_nf , 'o' ) != NULL ); /* "Octal" */
@@ -5431,14 +5433,14 @@ do_addsub(command, Prenum1, g_cmd)
5431
5433
curbuf -> b_visual .vi_end = curwin -> w_cursor ;
5432
5434
curbuf -> b_visual .vi_mode = VIsual_mode ;
5433
5435
5434
- col = VIsual .col ;
5436
+ if (VIsual_mode != 'v' )
5437
+ startcol = VIsual .col < curwin -> w_cursor .col ? VIsual .col
5438
+ : curwin -> w_cursor .col ;
5439
+ else
5440
+ startcol = VIsual .col ;
5441
+ col = startcol ;
5435
5442
lnum = VIsual .lnum ;
5436
5443
lnume = curwin -> w_cursor .lnum ;
5437
- if (ptr [col ] == '-' )
5438
- {
5439
- negative = TRUE;
5440
- col ++ ;
5441
- }
5442
5444
}
5443
5445
else
5444
5446
{
@@ -5481,9 +5483,16 @@ do_addsub(command, Prenum1, g_cmd)
5481
5483
{
5482
5484
curwin -> w_cursor .lnum = i ;
5483
5485
ptr = ml_get_curline ();
5484
- RLADDSUBFIX (ptr );
5485
5486
if ((int )STRLEN (ptr ) <= col )
5486
- col = 0 ;
5487
+ /* try again on next line */
5488
+ continue ;
5489
+ if (visual && ptr [col ] == '-' )
5490
+ {
5491
+ negative = TRUE;
5492
+ was_positive = FALSE;
5493
+ col ++ ;
5494
+ }
5495
+ RLADDSUBFIX (ptr );
5487
5496
/*
5488
5497
* If a number was found, and saving for undo works, replace the number.
5489
5498
*/
@@ -5598,6 +5607,14 @@ do_addsub(command, Prenum1, g_cmd)
5598
5607
negative = FALSE;
5599
5608
}
5600
5609
5610
+ if (visual && !was_positive && !negative )
5611
+ {
5612
+ /* need to remove the '-' */
5613
+ col -- ;
5614
+ length ++ ;
5615
+ }
5616
+
5617
+
5601
5618
/*
5602
5619
* Delete the old number.
5603
5620
*/
@@ -5634,8 +5651,7 @@ do_addsub(command, Prenum1, g_cmd)
5634
5651
if (buf1 == NULL )
5635
5652
return FAIL ;
5636
5653
ptr = buf1 ;
5637
- /* do not add leading '-' for visual mode */
5638
- if (negative && !visual )
5654
+ if (negative && (!visual || (visual && was_positive )))
5639
5655
{
5640
5656
* ptr ++ = '-' ;
5641
5657
}
@@ -5654,23 +5670,15 @@ do_addsub(command, Prenum1, g_cmd)
5654
5670
* Put the number characters in buf2[].
5655
5671
*/
5656
5672
if (hex == 0 )
5657
- sprintf ((char * )buf2 , "%lu" , n + offset );
5673
+ sprintf ((char * )buf2 , "%lu" , n );
5658
5674
else if (hex == '0' )
5659
- sprintf ((char * )buf2 , "%lo" , n + offset );
5675
+ sprintf ((char * )buf2 , "%lo" , n );
5660
5676
else if (hex && hexupper )
5661
- sprintf ((char * )buf2 , "%lX" , n + offset );
5677
+ sprintf ((char * )buf2 , "%lX" , n );
5662
5678
else
5663
- sprintf ((char * )buf2 , "%lx" , n + offset );
5679
+ sprintf ((char * )buf2 , "%lx" , n );
5664
5680
length -= (int )STRLEN (buf2 );
5665
5681
5666
- if (g_cmd )
5667
- {
5668
- if (subtract )
5669
- offset -= (unsigned long )Prenum1 ;
5670
- else
5671
- offset += (unsigned long )Prenum1 ;
5672
- }
5673
-
5674
5682
/*
5675
5683
* Adjust number of zeros to the new number of digits, so the
5676
5684
* total length of the number remains the same.
@@ -5685,13 +5693,27 @@ do_addsub(command, Prenum1, g_cmd)
5685
5693
ins_str (buf1 ); /* insert the new number */
5686
5694
vim_free (buf1 );
5687
5695
}
5688
- -- curwin -> w_cursor .col ;
5696
+
5697
+ if (g_cmd )
5698
+ {
5699
+ offset = (unsigned long )Prenum1 ;
5700
+ g_cmd = 0 ;
5701
+ }
5702
+ /* reset */
5703
+ subtract = FALSE;
5704
+ negative = FALSE;
5705
+ if (visual && VIsual_mode != Ctrl_V )
5706
+ col = 0 ;
5707
+ else
5708
+ col = startcol ;
5709
+ Prenum1 += offset ;
5689
5710
curwin -> w_set_curswant = TRUE;
5690
5711
#ifdef FEAT_RIGHTLEFT
5691
5712
ptr = ml_get_buf (curbuf , curwin -> w_cursor .lnum , TRUE);
5692
5713
RLADDSUBFIX (ptr );
5693
5714
#endif
5694
5715
}
5716
+ -- curwin -> w_cursor .col ;
5695
5717
return OK ;
5696
5718
}
5697
5719
0 commit comments