938
parse_hunk_header($text->[0]);
939
unless ($_->{USE}) {
940
$ofs_delta += $o_cnt - $n_cnt;
941
+ # If this hunk has been edited then subtract
942
+ # the delta that is due to the edit.
943
+ if ($_->{OFS_DELTA}) {
944
+ $ofs_delta -= $_->{OFS_DELTA};
945
+ }
946
next;
947
}
948
if ($ofs_delta) {
950
$_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
951
$n_ofs, $n_cnt);
952
}
953
+ # If this hunk was edited then adjust the offset delta
954
+ # to reflect the edit.
955
+ if ($_->{OFS_DELTA}) {
956
+ $ofs_delta += $_->{OFS_DELTA};
957
+ }
958
if (defined $last_o_ctx &&
959
$o_ofs <= $last_o_ctx &&
960
!$_->{DIRTY} &&
1026
marked for applying."),
1027
);
1028
1029
+sub recount_edited_hunk {
1030
+ local $_;
1031
+ my ($oldtext, $newtext) = @_;
1032
+ my ($o_cnt, $n_cnt) = (0, 0);
1033
+ for (@{$newtext}[1..$#{$newtext}]) {
1034
+ my $mode = substr($_, 0, 1);
1035
+ if ($mode eq '-') {
1036
+ $o_cnt++;
1037
+ } elsif ($mode eq '+') {
1038
+ $n_cnt++;
1039
+ } elsif ($mode eq ' ') {
1040
+ $o_cnt++;
1041
+ $n_cnt++;
1042
+ }
1043
+ }
1044
+ my ($o_ofs, undef, $n_ofs, undef) =
1045
+ parse_hunk_header($newtext->[0]);
1046
+ $newtext->[0] = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
1047
+ my (undef, $orig_o_cnt, undef, $orig_n_cnt) =
1048
+ parse_hunk_header($oldtext->[0]);
1049
+ # Return the change in the number of lines inserted by this hunk
1050
+ return $orig_o_cnt - $orig_n_cnt - $o_cnt + $n_cnt;
1051
+}
1052
+
1053
sub edit_hunk_manually {
1054
my ($oldtext) = @_;
1055
1148
}
1149
1150
sub edit_hunk_loop {
1117
- my ($head, $hunk, $ix) = @_;
1118
- my $text = $hunk->[$ix]->{TEXT};
1151
+ my ($head, $hunks, $ix) = @_;
1152
+ my $hunk = $hunks->[$ix];
1153
+ my $text = $hunk->{TEXT};
1154
1155
while (1) {
1121
- $text = edit_hunk_manually($text);
1122
- if (!defined $text) {
1156
+ my $newtext = edit_hunk_manually($text);
1157
+ if (!defined $newtext) {
1158
return undef;
1159
}
1160
my $newhunk = {
1126
- TEXT => $text,
1127
- TYPE => $hunk->[$ix]->{TYPE},
1161
+ TEXT => $newtext,
1162
+ TYPE => $hunk->{TYPE},
1163
USE => 1,
1164
DIRTY => 1,
1165
};
1166
+ $newhunk->{OFS_DELTA} = recount_edited_hunk($text, $newtext);
1167
+ # If this hunk has already been edited then add the
1168
+ # offset delta of the previous edit to get the real
1169
+ # delta from the original unedited hunk.
1170
+ $hunk->{OFS_DELTA} and
1171
+ $newhunk->{OFS_DELTA} += $hunk->{OFS_DELTA};
1172
if (diff_applies($head,
1132
- @{$hunk}[0..$ix-1],
1173
+ @{$hunks}[0..$ix-1],
1174
$newhunk,
1134
- @{$hunk}[$ix+1..$#{$hunk}])) {
1135
- $newhunk->{DISPLAY} = [color_diff(@{$text})];
1175
+ @{$hunks}[$ix+1..$#{$hunks}])) {
1176
+ $newhunk->{DISPLAY} = [color_diff(@{$newtext})];
1177
return $newhunk;
1178
}
1179
else {