Merge remote-tracking branch 'krobelus/mail-patch-goto-source'
This commit is contained in:
commit
49339749d1
|
@ -1,7 +1,14 @@
|
|||
hook global BufCreate .*\.(diff|patch) %{
|
||||
set-option buffer filetype diff
|
||||
map buffer normal <ret> %{: diff-jump<ret>}
|
||||
}
|
||||
|
||||
hook global WinSetOption filetype=diff %{
|
||||
require-module diff
|
||||
}
|
||||
|
||||
provide-module diff %§
|
||||
|
||||
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
|
||||
|
@ -12,6 +19,136 @@ hook -group diff-highlight global WinSetOption filetype=diff %{
|
|||
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/diff }
|
||||
}
|
||||
|
||||
define-command diff-jump \
|
||||
-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.
|
||||
|
||||
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.} \
|
||||
-params .. -file-completion %{
|
||||
evaluate-commands -draft -save-regs c %{
|
||||
# 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 %{
|
||||
execute-keys %{<a-l><semicolon><a-?>^(?:> )*diff\b<ret><a-x>}
|
||||
} 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
|
||||
}
|
||||
evaluate-commands %sh{
|
||||
printf %s "$kak_selection" |
|
||||
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 1;
|
||||
}
|
||||
$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: $_";
|
||||
}
|
||||
}
|
||||
$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;
|
||||
}
|
||||
next;
|
||||
}
|
||||
if ($state eq "header") {
|
||||
if (m{^[$version]{3} ([^\t\n]+)}) {
|
||||
$file = $1;
|
||||
next;
|
||||
}
|
||||
if (m{^[$other_version]{3} ([^\t\n]+)}) {
|
||||
$fallback_file = $1;
|
||||
next;
|
||||
}
|
||||
}
|
||||
if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
|
||||
$state = "contents";
|
||||
$line = ($version eq "+" ? $2 : $1) - 1;
|
||||
} elsif (m{^[ $version]}) {
|
||||
$line++ if defined $line;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
$file =~ s,^([^/]+/+){$strip},, or fail "directory prefix underflow";
|
||||
$filepath = "$directory/$file";
|
||||
|
||||
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, "<", $filepath or fail "failed to open file: $!: $filepath";
|
||||
@lines = <FILE>;
|
||||
for (my $i = $line - 1; $i >= 0 && $i < scalar @lines; $i--) {
|
||||
if ($lines[$i] !~ m{\Q$hunk_header_from_userdiff}) {
|
||||
next;
|
||||
}
|
||||
$line = $i + 1;
|
||||
# Re-add 1 because the @@ line does not have a [ +-] diff prefix.
|
||||
$column = $column + 1 - length $hunk_header_prefix;
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf "set-register c %s $line $column", quote($filepath);
|
||||
' -- "$@"
|
||||
}
|
||||
evaluate-commands -client %val{client} %{
|
||||
evaluate-commands -try-client %opt{jumpclient} %{
|
||||
edit -existing -- %reg{c}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
§
|
||||
|
||||
define-command \
|
||||
-docstring %{diff-select-file: Select surrounding patch file} \
|
||||
-params 0 \
|
||||
|
@ -23,7 +160,7 @@ define-command \
|
|||
execute-keys '"ez'
|
||||
} catch %{
|
||||
execute-keys '"oz'
|
||||
fail 'Not in a diff file'
|
||||
fail 'Not in a diff file'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ hook -group git-rebase-highlight global WinSetOption filetype=git-rebase %{
|
|||
|
||||
|
||||
provide-module git-commit %{
|
||||
require-module diff
|
||||
add-highlighter shared/git-commit regions
|
||||
add-highlighter shared/git-commit/diff region '^diff --git' '^(?=diff --git)' ref diff # highlight potential diffs from the -v option
|
||||
add-highlighter shared/git-commit/comments region ^# $ group
|
||||
|
|
|
@ -4,6 +4,10 @@ hook global BufCreate .+\.eml %{
|
|||
|
||||
hook global WinSetOption filetype=mail %{
|
||||
require-module mail
|
||||
map buffer normal <ret> %{: diff-jump<ret>}
|
||||
hook -once -always window WinSetOption filetype=.* %{
|
||||
unmap buffer normal <ret> %{: diff-jump<ret>}
|
||||
}
|
||||
}
|
||||
|
||||
hook -group mail-highlight global WinSetOption filetype=mail %{
|
||||
|
@ -14,6 +18,8 @@ hook -group mail-highlight global WinSetOption filetype=mail %{
|
|||
|
||||
provide-module mail %{
|
||||
|
||||
require-module diff
|
||||
|
||||
add-highlighter shared/mail group
|
||||
add-highlighter shared/mail/ regex ^(From|To|Cc|Bcc|Subject|Reply-To|In-Reply-To|References|Date):([^\n]*(?:\n\h+[^\n]+)*)$ 1:keyword 2:attribute
|
||||
add-highlighter shared/mail/ regex <[^@>]+@.*?> 0:string
|
||||
|
|
104
rc/tools/git.kak
104
rc/tools/git.kak
|
@ -2,6 +2,7 @@ declare-option -docstring "name of the client in which documentation is to be di
|
|||
str docsclient
|
||||
|
||||
hook -group git-log-highlight global WinSetOption filetype=git-log %{
|
||||
require-module diff
|
||||
add-highlighter window/git-log group
|
||||
add-highlighter window/git-log/ regex '^([*|\\ /_.-])*' 0:keyword
|
||||
add-highlighter window/git-log/ regex '^( ?[*|\\ /_.-])*\h{,3}(commit )?(\b[0-9a-f]{4,40}\b)' 2:keyword 3:comment
|
||||
|
@ -12,6 +13,7 @@ hook -group git-log-highlight global WinSetOption filetype=git-log %{
|
|||
}
|
||||
|
||||
hook -group git-status-highlight global WinSetOption filetype=git-status %{
|
||||
require-module diff
|
||||
add-highlighter window/git-status group
|
||||
add-highlighter window/git-status/ regex '^## ' 0:comment
|
||||
add-highlighter window/git-status/ regex '^## (\S*[^\s\.@])' 1:green
|
||||
|
@ -337,107 +339,9 @@ define-command -params 1.. \
|
|||
esac
|
||||
}}
|
||||
|
||||
# Options needed by git-diff-goto-source command
|
||||
declare-option -hidden str git_diff_hunk_filename
|
||||
declare-option -hidden int git_diff_hunk_line_num_start
|
||||
declare-option -hidden int git_diff_go_to_line_num
|
||||
declare-option -hidden str git_diff_git_dir
|
||||
declare-option -hidden str git_diff_section_heading
|
||||
declare-option -hidden int git_diff_cursor_column
|
||||
|
||||
# Works within :git diff and :git show
|
||||
define-command git-diff-goto-source \
|
||||
-docstring 'Navigate to source by pressing the enter key in hunks when git diff is displayed. Works within :git diff and :git show' %{
|
||||
try %{
|
||||
set-option global git_diff_git_dir %sh{
|
||||
git rev-parse --show-toplevel
|
||||
}
|
||||
# We will need this later. Need to subtract 1 because a diff has an initial column
|
||||
# for -,+,<space>
|
||||
set-option global git_diff_cursor_column %sh{ echo $(($kak_cursor_column-1)) }
|
||||
|
||||
# This function works_within a hunk or in the diff header.
|
||||
# - On a context line or added line, it will navigate to that line.
|
||||
# - On a deleted line, it will navigate to the context line or added line just above.
|
||||
# - On a @@ line (i.e. a "hunk header") this will navigate to section heading (see below).
|
||||
# - A diff header contains lines starting with "diff", "index", "+++", and "---".
|
||||
# Inside a diff header, this will navigate to the first line of the file.
|
||||
execute-keys -draft 'x<a-k>^[@ +-]|^diff|^index<ret>'
|
||||
|
||||
# Find the source filename for the current hunk (reverse search)
|
||||
evaluate-commands -draft %{
|
||||
# First look for the "diff" line. because "+++" may be part of a diff.
|
||||
execute-keys 'x<semicolon><a-/>^diff<ret></>^\+\+\+ \w([^\n]*)<ret>'
|
||||
set-option global git_diff_hunk_filename %reg{1}
|
||||
}
|
||||
|
||||
try %{
|
||||
# Are we inside the diff header? If so simply go to the first line of the file.
|
||||
# The diff header is everything before the first hunk header.
|
||||
execute-keys -draft 'x<semicolon><a-?>^diff<ret><a-K>^@@<ret>'
|
||||
edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" 1
|
||||
} catch %{
|
||||
# Find the source line at which the current hunk starts (reverse search)
|
||||
evaluate-commands -draft %{
|
||||
execute-keys 'x<semicolon><a-/>^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@<ret>'
|
||||
set-option buffer git_diff_hunk_line_num_start %reg{1}
|
||||
}
|
||||
# If we're already on a hunk header (i.e. a line that starts with @@) then
|
||||
# our behavior changes slightly: we need to go look for the section heading.
|
||||
# For example take this hunk header: @@ -123,4 +123,4 @@ fn some_function_name_possibly
|
||||
# Here the section heading is "fn some_function_name_possibly". Please note that the section
|
||||
# heading is NOT necessarily at the hunk start line so we can't trivially extract that.
|
||||
try %{
|
||||
# First things first, are we on a hunk header? If not, head to the nearest `catch`
|
||||
execute-keys -draft 'x<a-k>^@@<ret>'
|
||||
evaluate-commands -try-client %opt{jumpclient} %{
|
||||
# Now attempt to find the section heading!
|
||||
try %{
|
||||
# First extract the section heading.
|
||||
evaluate-commands -draft %{
|
||||
execute-keys 'xs^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@ ([^\n]*)<ret>'
|
||||
set-option global git_diff_section_heading %reg{2}
|
||||
}
|
||||
# Go to the hunk start in the source file. The section header will be above us.
|
||||
edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_hunk_line_num_start}
|
||||
# Search for the raw text of the section, like "fn some_function_name_possibly". That should work most of the time.
|
||||
set-register / "\Q%opt{git_diff_section_heading}"
|
||||
# Search backward from where the cursor is now.
|
||||
# Note that the hunk line number is NOT located at the same place as the section heading.
|
||||
# After we have found it, adjust the cursor and center the viewport as if we had directly jumped
|
||||
# to the first character of the section header with and `edit` command.
|
||||
execute-keys "<a-/><ret><a-semicolon><semicolon>vc"
|
||||
} catch %{
|
||||
# There is no section heading, or we can't find it in the source file,
|
||||
# so just go to the hunk start line.
|
||||
# NOTE that we DONT go to the saved cursor column,
|
||||
# because our cursor column will be fixed to the start of the section heading
|
||||
edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_hunk_line_num_start}
|
||||
}
|
||||
}
|
||||
} catch %{
|
||||
# This catch deals with the typical case. We're somewhere on either:
|
||||
# (a) A context line i.e. lines starting with ' '
|
||||
# or (b) On a line removal i.e. lines starting with '-'
|
||||
# or (c) On a line addition i.e. lines starting with '+'
|
||||
# So now try to figure out a line offset + git_diff_hunk_line_num_start that we need to go to
|
||||
# Ignoring any diff lines starting with `-`, how many lines from where we
|
||||
# pressed <ret> till the start of the hunk?
|
||||
evaluate-commands -draft %{
|
||||
execute-keys '<a-?>^@@<ret>J<a-s><a-K>^-<ret>'
|
||||
set-option global git_diff_go_to_line_num %sh{
|
||||
set -- $kak_reg_hash
|
||||
line=$(($#+$kak_opt_git_diff_hunk_line_num_start-1))
|
||||
echo $line
|
||||
}
|
||||
}
|
||||
evaluate-commands -try-client %opt{jumpclient} %{
|
||||
# Open the source file at the appropriate line number and cursor column
|
||||
edit -existing "%opt{git_diff_git_dir}%opt{git_diff_hunk_filename}" %opt{git_diff_go_to_line_num} %opt{git_diff_cursor_column}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch %{
|
||||
fail "git-diff-goto-source: unable to navigate to source. Use only inside a diff"
|
||||
}
|
||||
require-module diff
|
||||
diff-jump %sh{ git rev-parse --show-toplevel }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user