2012-07-31 14:22:57 +02:00
hook global BufCreate .*\.(diff|patch) %{
2017-11-03 08:34:41 +01:00
set-option buffer filetype diff
2012-07-31 14:22:57 +02:00
}
2012-06-27 14:29:12 +02:00
2022-01-24 17:09:44 +01:00
hook global WinSetOption filetype=diff %{
require-module diff
2022-07-30 22:28:02 +02:00
map buffer normal <ret> :diff-jump<ret>
2022-01-24 17:09:44 +01:00
}
2022-02-01 20:09:48 +01:00
hook -group diff-highlight global WinSetOption filetype=diff %{
add-highlighter window/diff ref diff
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/diff }
}
2022-01-24 17:09:44 +01:00
provide-module diff %§
2018-06-28 13:08:58 +02:00
add-highlighter shared/diff group
add-highlighter shared/diff/ regex "^\+[^\n]*\n" 0:green,default
add-highlighter shared/diff/ regex "^-[^\n]*\n" 0:red,default
add-highlighter shared/diff/ regex "^@@[^\n]*@@" 0:cyan,default
2022-06-11 11:23:10 +02:00
# If any trailing whitespace was introduced in diff, show it with red background
add-highlighter shared/diff/ regex "^\+[^\n]*?(\h+)\n" 1:default,red
2012-06-27 14:29:12 +02:00
2022-02-22 10:14:47 +01:00
define-command diff-jump -params .. -docstring %{
diff-jump [<switches>] [<directory>]: edit the diff's source file at the cursor position.
Paths are resolved relative to <directory>, or the current working directory if unspecified.
2022-01-24 18:10:39 +01:00
2022-02-22 10:14:47 +01:00
Switches:
- jump to the old file instead of the new file
-<num> strip <num> leading directory components, like -p<num> in patch(1). Defaults to 1 if there is a 'diff' line (as printed by 'diff -r'), or 0 otherwise.
} %{
2022-02-12 16:12:29 +01:00
evaluate-commands -draft -save-regs ac| %{
2022-01-24 18:10:39 +01:00
# Save the column because we will move the cursor.
set-register c %val{cursor_column}
# If there is a "diff" line, we don't need to look further back.
try %{
2022-03-16 23:20:07 +01:00
execute-keys %{<a-l><semicolon><a-?>^(?:> )*diff\b<ret>x}
2022-01-24 18:10:39 +01:00
} catch %{
# A single file diff won't have a diff line. Start parsing from
# the buffer start, so we can tell if +++/--- lines are headers
# or content.
execute-keys Gk
}
2022-02-12 16:12:29 +01:00
set-register a %arg{@}
set-register | %{
[ -n "$kak_reg_a" ] && eval set -- $kak_quoted_reg_a
cmd=$(column=$kak_reg_c perl -we '
sub quote {
$SQ = "'\''";
$token = shift;
$token =~ s/$SQ/$SQ$SQ/g;
return "$SQ$token$SQ";
}
sub fail {
$reason = shift;
print "fail " . quote("diff-jump: $reason");
exit;
}
$version = "+", $other_version = "-";
$strip = undef;
$directory = $ENV{PWD};
$seen_ddash = 0;
foreach (@ARGV) {
if ($seen_ddash or !m{^-}) {
$directory = $_;
} elsif ($_ eq "-") {
$version = "-", $other_version = "+";
} elsif (m{^-(\d+)$}) {
$strip = $1;
} elsif ($_ eq "--") {
$seen_ddash = 1;
} else {
fail "unknown option: $_";
2022-01-24 18:10:39 +01:00
}
2022-02-12 16:12:29 +01:00
}
$have_diff_line = 0;
$state = "header";
while (<STDIN>) {
s/^(> )*//g;
$last_line = $_;
if (m{^diff\b}) {
$state = "header";
$have_diff_line = 1;
if (m{^diff -\S* (\S+) (\S+)$}) {
$fallback_file = $version eq "+" ? $2 : $1;
2022-01-24 18:10:39 +01:00
}
2022-02-12 16:12:29 +01:00
next;
2022-01-24 18:10:39 +01:00
}
2022-02-12 16:12:29 +01:00
if ($state eq "header") {
if (m{^[$version]{3} ([^\t\n]+)}) {
$file = $1;
2022-01-24 18:10:39 +01:00
next;
}
2022-02-12 16:12:29 +01:00
if (m{^[$other_version]{3} ([^\t\n]+)}) {
$fallback_file = $1;
next;
2022-01-24 18:10:39 +01:00
}
}
2022-02-12 16:12:29 +01:00
if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
$state = "contents";
$line = ($version eq "+" ? $2 : $1) - 1;
} elsif (m{^[ $version]}) {
$line++ if defined $line;
2022-02-07 14:10:27 +01:00
}
2022-02-12 16:12:29 +01:00
}
if (not defined $file) {
$file = $fallback_file;
}
if (not defined $file) {
fail "missing diff header";
}
if (not defined $strip) {
# A "diff -r" or "git diff" adds "diff" lines to
# the output. If no such line is present, we have
# a plain diff between files (not directories), so
# there should be no need to strip the directory.
$strip = $have_diff_line ? 1 : 0;
}
if ($file !~ m{^/}) {
$file =~ s,^([^/]+/+){$strip},, or fail "directory prefix underflow";
$file = "$directory/$file";
}
2022-01-24 18:10:39 +01:00
2022-02-12 16:12:29 +01:00
if (defined $line) {
$column = $ENV{column} - 1; # Account for [ +-] diff prefix.
# If the cursor was on a hunk header, go to the section header if possible.
if ($last_line =~ m{^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+) @@ )([^\n]*)}) {
$hunk_header_prefix = $1;
$hunk_header_from_userdiff = $2;
open FILE, "<", $file or fail "failed to open file: $!: $file";
@lines = <FILE>;
for (my $i = $line - 1; $i >= 0 && $i < scalar @lines; $i--) {
if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
next;
2022-01-24 18:10:39 +01:00
}
2022-02-12 16:12:29 +01:00
$line = $i + 1;
# Re-add 1 because the @@ line does not have a [ +-] diff prefix.
$column = $column + 1 - length $hunk_header_prefix;
last;
2022-01-24 18:10:39 +01:00
}
}
2022-02-12 16:12:29 +01:00
}
2022-01-24 18:10:39 +01:00
2022-02-12 16:12:29 +01:00
printf "edit -existing -- %s $line $column", quote($file);
' -- "$@")
echo "set-register c $cmd" >"$kak_command_fifo"
2022-01-24 18:10:39 +01:00
}
2022-02-12 16:12:29 +01:00
execute-keys <a-|><ret>
2022-01-24 18:10:39 +01:00
evaluate-commands -client %val{client} %{
evaluate-commands -try-client %opt{jumpclient} %{
2022-02-12 16:12:29 +01:00
%reg{c}
2022-01-24 18:10:39 +01:00
}
}
}
}
2022-02-22 10:14:47 +01:00
complete-command diff-jump file
2022-01-24 18:10:39 +01:00
2022-01-24 17:09:44 +01:00
§
2020-03-22 21:52:40 +01:00
define-command \
-docstring %{diff-select-file: Select surrounding patch file} \
-params 0 \
diff-select-file %{
2022-02-12 16:12:29 +01:00
evaluate-commands -itersel -save-regs 'ose/' %{
2020-03-22 21:52:40 +01:00
try %{
execute-keys '"oZgl<a-?>^diff <ret>;"sZ' 'Ge"eZ'
try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' }
execute-keys '"ez'
} catch %{
execute-keys '"oz'
2022-01-29 03:57:22 +01:00
fail 'Not in a diff file'
2020-03-22 21:52:40 +01:00
}
}
}
define-command \
-docstring %{diff-select-hunk: Select surrounding patch hunk} \
-params 0 \
diff-select-hunk %{
evaluate-commands -itersel -save-regs 'ose/' %{
try %{
execute-keys '"oZgl<a-?>^@@ <ret>;"sZ' 'Ge"eZ'
try %{ execute-keys '"sz?\n(?=diff )<ret>"e<a-Z><lt>' }
try %{ execute-keys '"sz?\n(?=@@ )<ret>"e<a-Z><lt>' }
execute-keys '"ez'
} catch %{
execute-keys '"oz'
fail 'Not in a diff hunk'
}
}
}