From b84abd57de39facb8159b6a0f6f6390268ff54d0 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 24 Jan 2022 17:09:44 +0100 Subject: [PATCH 1/4] rc diff: make it a module We want to move git-diff-goto-source from rc/tools/git.kak to rc/filetype/diff.kak (or should we could create rc/tools/diff.kak?). Either way, create the diff module so we can formalize this dependency. Currently this module only provides highlighters, so require it wherever we reference them. Keep the diff-select-{file,hunk} commands outside the module because people might already use them in git buffers. --- rc/filetype/diff.kak | 8 ++++++++ rc/filetype/git.kak | 1 + rc/tools/git.kak | 2 ++ 3 files changed, 11 insertions(+) diff --git a/rc/filetype/diff.kak b/rc/filetype/diff.kak index d904ce5a..c0cb4523 100644 --- a/rc/filetype/diff.kak +++ b/rc/filetype/diff.kak @@ -2,6 +2,12 @@ hook global BufCreate .*\.(diff|patch) %{ set-option buffer filetype diff } +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 +18,8 @@ hook -group diff-highlight global WinSetOption filetype=diff %{ hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/diff } } +§ + define-command \ -docstring %{diff-select-file: Select surrounding patch file} \ -params 0 \ diff --git a/rc/filetype/git.kak b/rc/filetype/git.kak index aa53de2d..52304582 100644 --- a/rc/filetype/git.kak +++ b/rc/filetype/git.kak @@ -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 diff --git a/rc/tools/git.kak b/rc/tools/git.kak index 0cd505d0..c79b620f 100644 --- a/rc/tools/git.kak +++ b/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 From bf239ba77afee503f5b9bd9cf392ca2ad0fd4f8f Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Mon, 24 Jan 2022 18:10:39 +0100 Subject: [PATCH 2/4] rc diff: introduce diff-jump, replacing git-diff-goto-source git-diff-goto-source is specific to diffs produced by Git. This patch generalizes the logic and moves it to a new diff-jump in diff.kak. The main differences are: - diff-jump handles plain file diffs (i.e. without the -r option). These have no "diff" line. This means that it needs to parse +++/--- instead. - diff-jump can go to the old file, not just the new one. - diff-jump allows to override the base directory and the number of directory components to strip. git-diff-goto-source was implemented with several nested try/catch blocks. Implementing the extra features would have added more nesting, redundancy or hidden options. To avoid that, I ported the parsing logic to Perl (which git.kak already depends on). Maybe it's possible to do the same in awk. Potential concerns: - We could move diff-jump to a new rc/tools/diff.kak but then it's not obvious where the "diff" module belongs to. - Should diff "diff-jump -1" be spelled "diff-jump -p1"? In future, the diff parser could be reused to implement a vimdiff-style feature: given a diff and the "old" line number, we can compute the corresponding "new" line number. Perhaps diff-jump should get a -client argument. --- rc/filetype/diff.kak | 130 ++++++++++++++++++++++++++++++++++++++++++- rc/tools/git.kak | 102 +-------------------------------- 2 files changed, 131 insertions(+), 101 deletions(-) diff --git a/rc/filetype/diff.kak b/rc/filetype/diff.kak index c0cb4523..586bb32c 100644 --- a/rc/filetype/diff.kak +++ b/rc/filetype/diff.kak @@ -1,5 +1,6 @@ hook global BufCreate .*\.(diff|patch) %{ set-option buffer filetype diff + map buffer normal %{: diff-jump} } hook global WinSetOption filetype=diff %{ @@ -18,6 +19,133 @@ 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 [] []: edit the diff's source file at the cursor position. +Paths are resolved relative to , or the current working directory if unspecified. + +Switches: + - jump to the old file instead of the new file + - strip leading directory components, like -p 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 %{^diff\b} + } 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 () { + $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 = ; + 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 \ @@ -31,7 +159,7 @@ define-command \ execute-keys '"ez' } catch %{ execute-keys '"oz' - fail 'Not in a diff file' + fail 'Not in a diff file' } } } diff --git a/rc/tools/git.kak b/rc/tools/git.kak index c79b620f..aa2da10f 100644 --- a/rc/tools/git.kak +++ b/rc/tools/git.kak @@ -339,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 -,+, - 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^[@ +-]|^diff|^index' - - # 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^diff^\+\+\+ \w([^\n]*)' - 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^diff^@@' - 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^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@' - 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^@@' - 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]*)' - 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 "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 till the start of the hunk? - evaluate-commands -draft %{ - execute-keys '^@@J^-' - 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 } } From 90b070034d3f8094df405cf331367fde3e9c4e50 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 25 Jan 2022 08:07:29 +0100 Subject: [PATCH 3/4] rc diff: skip email quotes in diff-jump When reading and writing emails that contain patches (possibly email-quoted), it can be convenient to the jump to the source file. Allow this by making diff-jump (bound to in git-diff buffers) ignore leading email quotes ("> "). A line that starts with "> " should not occur in a unified diff, so this won't affect other use cases. Observe that diff-jump even works around interleaved replies; they will not affect the computed line numbers because we ignore lines that don't match ^(> )*[ +-]. --- rc/filetype/diff.kak | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rc/filetype/diff.kak b/rc/filetype/diff.kak index 586bb32c..6658bab0 100644 --- a/rc/filetype/diff.kak +++ b/rc/filetype/diff.kak @@ -32,7 +32,7 @@ Switches: set-register c %val{cursor_column} # If there is a "diff" line, we don't need to look further back. try %{ - execute-keys %{^diff\b} + execute-keys %{^(?:> )*diff\b} } 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 @@ -73,6 +73,7 @@ Switches: $have_diff_line = 0; $state = "header"; while () { + s/^(> )*//g; $last_line = $_; if (m{^diff\b}) { $state = "header"; From 3843163e2e1202f12486edb22eb545170b806630 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 31 Dec 2021 09:26:49 +0100 Subject: [PATCH 4/4] rc mail: enable jumping from inline diff to source file This allows to jump from a mail buffer that contains an inline diff to the source files (most accurate when the patch has been applied locally). This makes the diff module a mandatory dependency; we could relax that. --- rc/filetype/mail.kak | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rc/filetype/mail.kak b/rc/filetype/mail.kak index c99bd68f..9fb2d14e 100644 --- a/rc/filetype/mail.kak +++ b/rc/filetype/mail.kak @@ -4,6 +4,10 @@ hook global BufCreate .+\.eml %{ hook global WinSetOption filetype=mail %{ require-module mail + map buffer normal %{: diff-jump} + hook -once -always window WinSetOption filetype=.* %{ + unmap buffer normal %{: diff-jump} + } } 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