Skip to content

Commit dc9e955

Browse files
committed
patch 8.1.0120: buffer 'modified' set even when :sort has no changes
Problem: Buffer 'modified' set even when :sort has no changes. Solution: Only set 'modified' when lines are moved. (Jason Franklin)
1 parent ec12d64 commit dc9e955

3 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎src/ex_cmds.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ ex_sort(exarg_T *eap)
398398
colnr_T end_col;
399399
int sort_what = 0;
400400
int format_found = 0;
401+
int change_occurred = FALSE; // Buffer contents changed.
401402

402403
/* Sorting one line is really quick! */
403404
if (count <= 1)
@@ -616,12 +617,19 @@ ex_sort(exarg_T *eap)
616617
lnum = eap->line2;
617618
for (i = 0; i < count; ++i)
618619
{
619-
s = ml_get(nrs[eap->forceit ? count - i - 1 : i].lnum);
620+
linenr_T get_lnum = nrs[eap->forceit ? count - i - 1 : i].lnum;
621+
622+
// If the original line number of the line being placed is not the same
623+
// as "lnum" (accounting for offset), we know that the buffer changed.
624+
if (get_lnum + ((linenr_T)count - 1) != lnum)
625+
change_occurred = TRUE;
626+
627+
s = ml_get(get_lnum);
620628
if (!unique || i == 0
621629
|| (sort_ic ? STRICMP(s, sortbuf1) : STRCMP(s, sortbuf1)) != 0)
622630
{
623-
/* Copy the line into a buffer, it may become invalid in
624-
* ml_append(). And it's needed for "unique". */
631+
// Copy the line into a buffer, it may become invalid in
632+
// ml_append(). And it's needed for "unique".
625633
STRCPY(sortbuf1, s);
626634
if (ml_append(lnum++, sortbuf1, (colnr_T)0, FALSE) == FAIL)
627635
break;
@@ -644,7 +652,9 @@ ex_sort(exarg_T *eap)
644652
mark_adjust(eap->line2 - deleted, eap->line2, (long)MAXLNUM, -deleted);
645653
else if (deleted < 0)
646654
mark_adjust(eap->line2, MAXLNUM, -deleted, 0L);
647-
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
655+
656+
if (change_occurred || deleted != 0)
657+
changed_lines(eap->line1, 0, eap->line2 + 1, -deleted);
648658

649659
curwin->w_cursor.lnum = eap->line1;
650660
beginline(BL_WHITE | BL_FIX);

‎src/testdir/test_sort.vim‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
" Test sort()
1+
" Tests for the "sort()" function and for the ":sort" command.
22

3-
:func Compare1(a, b) abort
3+
func Compare1(a, b) abort
44
call sort(range(3), 'Compare2')
55
return a:a - a:b
6-
:endfunc
6+
endfunc
77

8-
:func Compare2(a, b) abort
8+
func Compare2(a, b) abort
99
return a:a - a:b
10-
:endfunc
10+
endfunc
1111

1212
func Test_sort_strings()
1313
" numbers compared as strings
@@ -45,7 +45,7 @@ func Test_sort_default()
4545
call assert_fails('call sort([3.3, 1, "2"], 3)', "E474")
4646
endfunc
4747

48-
" Tests for the :sort command
48+
" Tests for the ":sort" command.
4949
func Test_sort_cmd()
5050
let tests = [
5151
\ {
@@ -1167,15 +1167,54 @@ func Test_sort_cmd()
11671167
\ '1.234',
11681168
\ '123.456'
11691169
\ ]
1170-
\ }
1170+
\ },
1171+
\ {
1172+
\ 'name' : 'alphabetical, sorted input',
1173+
\ 'cmd' : 'sort',
1174+
\ 'input' : [
1175+
\ 'a',
1176+
\ 'b',
1177+
\ 'c',
1178+
\ ],
1179+
\ 'expected' : [
1180+
\ 'a',
1181+
\ 'b',
1182+
\ 'c',
1183+
\ ]
1184+
\ },
1185+
\ {
1186+
\ 'name' : 'alphabetical, sorted input, unique at end',
1187+
\ 'cmd' : 'sort u',
1188+
\ 'input' : [
1189+
\ 'aa',
1190+
\ 'bb',
1191+
\ 'cc',
1192+
\ 'cc',
1193+
\ ],
1194+
\ 'expected' : [
1195+
\ 'aa',
1196+
\ 'bb',
1197+
\ 'cc',
1198+
\ ]
1199+
\ },
11711200
\ ]
11721201

11731202
for t in tests
11741203
enew!
11751204
call append(0, t.input)
11761205
$delete _
1177-
exe t.cmd
1206+
setlocal nomodified
1207+
execute t.cmd
1208+
11781209
call assert_equal(t.expected, getline(1, '$'), t.name)
1210+
1211+
" Previously, the ":sort" command would set 'modified' even if the buffer
1212+
" contents did not change. Here, we check that this problem is fixed.
1213+
if t.input == t.expected
1214+
call assert_false(&modified, t.name . ': &mod is not correct')
1215+
else
1216+
call assert_true(&modified, t.name . ': &mod is not correct')
1217+
endif
11791218
endfor
11801219

11811220
call assert_fails('sort no', 'E474')

‎src/version.c‎

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

790790
static int included_patches[] =
791791
{ /* Add new patch number below this line */
792+
/**/
793+
120,
792794
/**/
793795
119,
794796
/**/

0 commit comments

Comments
 (0)