rc tools patch: figure out path to patch-range.pl without double-parsing

We double-parse a command definition to figure out the location of
a support script at load time. This feels a bit dangerous and is not
really necessary, so use %val{runtime}/rc/tools/... instead.

Reference: https://lists.sr.ht/~mawww/kakoune/%3CZbOSCK2JjJvo-RTt@gmail.com%3E
This commit is contained in:
Johannes Altmanninger 2024-02-01 10:14:51 +01:00 committed by Maxime Coste
parent 582c3c56b2
commit c2c978907f

View File

@ -10,56 +10,53 @@ define-command patch -params .. -docstring %{
is applied (unless the cursor is inside a diff header, in which case is applied (unless the cursor is inside a diff header, in which case
the entire diff is applied). the entire diff is applied).
To revert changes, <arguments> must contain "--reverse" or "-R". To revert changes, <arguments> must contain "--reverse" or "-R".
} %exp{ } %{
evaluate-commands -draft -itersel -save-regs aefs|^ %%{ evaluate-commands -draft -itersel -save-regs aes|^ %{
set-register f %val{source} try %{
%{ execute-keys <a-k>\n<ret>
} catch %{
# The selection contains no newline.
execute-keys -save-regs '' Z
execute-keys <a-l><semicolon><a-?>^diff<ret>
try %{ try %{
execute-keys <a-k>\n<ret> execute-keys <a-k>^@@<ret>
# If the cursor is in a diff hunk, stage the entire hunk.
execute-keys z
execute-keys /.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)<ret>x<semicolon><a-?>^@@<ret>
} catch %{ } catch %{
# The selection contains no newline. # If the cursor is in a diff header, stage the entire diff.
execute-keys -save-regs '' Z execute-keys <a-semicolon>?.*?(?:(?=\ndiff)|(?=\n\n)|\z)<ret>
execute-keys <a-l><semicolon><a-?>^diff<ret>
try %{
execute-keys <a-k>^@@<ret>
# If the cursor is in a diff hunk, stage the entire hunk.
execute-keys z
execute-keys /.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)<ret>x<semicolon><a-?>^@@<ret>
} catch %{
# If the cursor is in a diff header, stage the entire diff.
execute-keys <a-semicolon>?.*?(?:(?=\ndiff)|(?=\n\n)|\z)<ret>
}
} }
# We want to apply only the selected lines. Remember them.
execute-keys <a-:>
set-register s %val{selection_desc}
# Select forward until the end of the last hunk.
execute-keys H?.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)<ret>x
# Select backward to the beginning of the first hunk's diff header.
execute-keys <a-semicolon><a-L><a-?>^diff<ret>
# Move cursor to the beginning so we know the diff's offset within the buffer.
execute-keys <a-:><a-semicolon>
set-register a %arg{@}
set-register e nop
set-register | %{
# The selected range to apply.
IFS=' .,' read min_line _ max_line _ <<-EOF
$kak_reg_s
EOF
min_line=$((min_line - kak_cursor_line + 1))
max_line=$((max_line - kak_cursor_line + 1))
# Since registers are never empty, we get an empty arg even if
# there were no args. This does no harm because we pass it to
# a shell where it expands to nothing.
eval set -- "$kak_quoted_reg_a"
perl "${kak_reg_f%/*}/patch-range.pl" $min_line $max_line "$@" ||
echo >$kak_command_fifo "set-register e fail 'patch: failed to apply selections, see *debug* buffer'"
}
execute-keys |<ret>
%reg{e}
} }
# We want to apply only the selected lines. Remember them.
execute-keys <a-:>
set-register s %val{selection_desc}
# Select forward until the end of the last hunk.
execute-keys H?.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)<ret>x
# Select backward to the beginning of the first hunk's diff header.
execute-keys <a-semicolon><a-L><a-?>^diff<ret>
# Move cursor to the beginning so we know the diff's offset within the buffer.
execute-keys <a-:><a-semicolon>
set-register a %arg{@}
set-register e nop
set-register | %{
# The selected range to apply.
IFS=' .,' read min_line _ max_line _ <<-EOF
$kak_reg_s
EOF
min_line=$((min_line - kak_cursor_line + 1))
max_line=$((max_line - kak_cursor_line + 1))
# Since registers are never empty, we get an empty arg even if
# there were no args. This does no harm because we pass it to
# a shell where it expands to nothing.
eval set -- "$kak_quoted_reg_a"
perl "${kak_runtime}"/rc/tools/patch-range.pl $min_line $max_line "$@" ||
echo >$kak_command_fifo "set-register e fail 'patch: failed to apply selections, see *debug* buffer'"
}
execute-keys |<ret>
%reg{e}
} }
} }