diff --git a/rc/tools/patch.kak b/rc/tools/patch.kak index 2feb85b3..a481ff41 100644 --- a/rc/tools/patch.kak +++ b/rc/tools/patch.kak @@ -10,56 +10,53 @@ define-command patch -params .. -docstring %{ is applied (unless the cursor is inside a diff header, in which case the entire diff is applied). To revert changes, must contain "--reverse" or "-R". -} %exp{ - evaluate-commands -draft -itersel -save-regs aefs|^ %%{ - set-register f %val{source} - %{ +} %{ + evaluate-commands -draft -itersel -save-regs aes|^ %{ + try %{ + execute-keys \n + } catch %{ + # The selection contains no newline. + execute-keys -save-regs '' Z + execute-keys ^diff try %{ - execute-keys \n + execute-keys ^@@ + # If the cursor is in a diff hunk, stage the entire hunk. + execute-keys z + execute-keys /.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)x^@@ } catch %{ - # The selection contains no newline. - execute-keys -save-regs '' Z - execute-keys ^diff - try %{ - execute-keys ^@@ - # If the cursor is in a diff hunk, stage the entire hunk. - execute-keys z - execute-keys /.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)x^@@ - } catch %{ - # If the cursor is in a diff header, stage the entire diff. - execute-keys ?.*?(?:(?=\ndiff)|(?=\n\n)|\z) - } + # If the cursor is in a diff header, stage the entire diff. + execute-keys ?.*?(?:(?=\ndiff)|(?=\n\n)|\z) } - # We want to apply only the selected lines. Remember them. - execute-keys - set-register s %val{selection_desc} - # Select forward until the end of the last hunk. - execute-keys H?.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)x - # Select backward to the beginning of the first hunk's diff header. - execute-keys ^diff - # Move cursor to the beginning so we know the diff's offset within the buffer. - execute-keys - 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 | - %reg{e} } + # We want to apply only the selected lines. Remember them. + execute-keys + set-register s %val{selection_desc} + # Select forward until the end of the last hunk. + execute-keys H?.*?(?:(?=\n@@)|(?=\ndiff)|(?=\n\n)|\z)x + # Select backward to the beginning of the first hunk's diff header. + execute-keys ^diff + # Move cursor to the beginning so we know the diff's offset within the buffer. + execute-keys + 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 | + %reg{e} } }