Skip to content

Commit ddd1f91

Browse files
committed
patch 8.1.0516: :move command marks buffer modified when nothing changed
Problem: :move command marks buffer modified when nothing changed. Solution: Do not set 'modified'. Add a test. (Jason Franklin)
1 parent ded5f1b commit ddd1f91

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

src/Make_all.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ NEW_TESTS = \
123123
test_mksession \
124124
test_mksession_utf8 \
125125
test_modeline \
126+
test_move \
126127
test_nested_function \
127128
test_netbeans \
128129
test_normal \

src/ex_cmds.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,20 +899,34 @@ do_move(linenr_T line1, linenr_T line2, linenr_T dest)
899899
{
900900
char_u *str;
901901
linenr_T l;
902-
linenr_T extra; /* Num lines added before line1 */
903-
linenr_T num_lines; /* Num lines moved */
904-
linenr_T last_line; /* Last line in file after adding new text */
902+
linenr_T extra; // Num lines added before line1
903+
linenr_T num_lines; // Num lines moved
904+
linenr_T last_line; // Last line in file after adding new text
905905
#ifdef FEAT_FOLDING
906906
win_T *win;
907907
tabpage_T *tp;
908908
#endif
909909

910910
if (dest >= line1 && dest < line2)
911911
{
912-
EMSG(_("E134: Move lines into themselves"));
912+
EMSG(_("E134: Cannot move a range of lines into itself"));
913913
return FAIL;
914914
}
915915

916+
// Do nothing if we are not actually moving any lines. This will prevent
917+
// the 'modified' flag from being set without cause.
918+
if (dest == line1 - 1 || dest == line2)
919+
{
920+
// Move the cursor as if lines were moved (see below) to be backwards
921+
// compatible.
922+
if (dest >= line1)
923+
curwin->w_cursor.lnum = dest;
924+
else
925+
curwin->w_cursor.lnum = dest + (line2 - line1) + 1;
926+
927+
return OK;
928+
}
929+
916930
num_lines = line2 - line1 + 1;
917931

918932
/*

src/testdir/test_alot.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ source test_match.vim
4141
source test_menu.vim
4242
source test_messages.vim
4343
source test_modeline.vim
44+
source test_move.vim
4445
source test_partial.vim
4546
source test_popup.vim
4647
source test_put.vim

src/testdir/test_move.vim

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
" Test the ":move" command.
2+
3+
func Test_move()
4+
enew!
5+
call append(0, ['line 1', 'line 2', 'line 3'])
6+
g /^$/ delete _
7+
set nomodified
8+
9+
move .
10+
call assert_equal(['line 1', 'line 2', 'line 3'], getline(1, 3))
11+
call assert_false(&modified)
12+
13+
1,2move 0
14+
call assert_equal(['line 1', 'line 2', 'line 3'], getline(1, 3))
15+
call assert_false(&modified)
16+
17+
1,3move 3
18+
call assert_equal(['line 1', 'line 2', 'line 3'], getline(1, 3))
19+
call assert_false(&modified)
20+
21+
1move 2
22+
call assert_equal(['line 2', 'line 1', 'line 3'], getline(1, 3))
23+
call assert_true(&modified)
24+
set nomodified
25+
26+
3move 0
27+
call assert_equal(['line 3', 'line 2', 'line 1'], getline(1, 3))
28+
call assert_true(&modified)
29+
set nomodified
30+
31+
2,3move 0
32+
call assert_equal(['line 2', 'line 1', 'line 3'], getline(1, 3))
33+
call assert_true(&modified)
34+
set nomodified
35+
36+
call assert_fails('1,2move 1', 'E134')
37+
call assert_fails('2,3move 2', 'E134')
38+
39+
%bwipeout!
40+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ static char *(features[]) =
792792

793793
static int included_patches[] =
794794
{ /* Add new patch number below this line */
795+
/**/
796+
516,
795797
/**/
796798
515,
797799
/**/

0 commit comments

Comments
 (0)